From b3868e537371e9118fdbe3aacb5eb6cb833c39c5 Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Thu, 30 Jan 2025 20:27:46 +0700 Subject: [PATCH] Add fallback validation for Safari 14.0 --- packages/core/src/fetch/__tests__/lib.test.ts | 20 +++++++++- packages/core/src/fetch/lib.ts | 37 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/core/src/fetch/__tests__/lib.test.ts b/packages/core/src/fetch/__tests__/lib.test.ts index 847dd7bc5..972bacadf 100644 --- a/packages/core/src/fetch/__tests__/lib.test.ts +++ b/packages/core/src/fetch/__tests__/lib.test.ts @@ -1,6 +1,6 @@ import { describe, test, expect } from 'vitest'; -import { mergeRecords } from '../lib'; +import { mergeRecords, splitUrl } from '../lib'; describe('mergeRecords', () => { test('empty to empty object', () => { @@ -41,3 +41,21 @@ describe('mergeRecords', () => { }); }); }); + +describe('Safari 14.0 bug', () => { + test('splitUrl', () => { + const url = 'https://example.com/api?foo=bar'; + + const split = splitUrl(url); + + expect(split).toEqual({ + base: 'https://example.com', + path: '/api?foo=bar', + }); + + const url1 = new URL(url); + const url2 = new URL(split.path, split.base); + + expect(url1.href).toBe(url2.href); + }); +}); diff --git a/packages/core/src/fetch/lib.ts b/packages/core/src/fetch/lib.ts index f2c5a9b56..3f1558597 100644 --- a/packages/core/src/fetch/lib.ts +++ b/packages/core/src/fetch/lib.ts @@ -96,6 +96,27 @@ export function formatUrl( try { return new URL(urlString, urlBase); } catch (e) { + if (!urlBase) { + try { + /** + * Fallback branch for Safari 14.0 + * @see https://github.com/igorkamyshev/farfetched/issues/528 + * + * If url is full path, but we're in Safari 14.0, we will have a TypeError for new URL(urlString, undefined) + * + * So we have to manually split url into base and path parts first + */ + const { base, path } = splitUrl(urlString); + + return new URL(path, base); + } catch (_e) { + throw configurationError({ + reason: 'Invalid URL', + validationErrors: [`"${urlString}" is not valid URL`], + }); + } + } + throw configurationError({ reason: 'Invalid URL', validationErrors: [`"${urlString}" is not valid URL`], @@ -129,3 +150,19 @@ function clearValue( return value ?? null; } + +/** + * @see https://github.com/igorkamyshev/farfetched/issues/528 + */ +export function splitUrl(urlString: string): { base: string; path: string } { + const urlPattern = /^(https?:\/\/[^\/]+)(\/.*)?$/; + const match = urlString.match(urlPattern); + + if (!match) { + throw new Error(`Invalid URL: ${urlString}`); + } + + const base = match[1]; + const path = match[2] || ''; + return { base, path }; +}