From f545020441ea940393925e5d21615af365c85a4c Mon Sep 17 00:00:00 2001 From: mufazalov <mufazalov@yandex-team.ru> Date: Tue, 22 Apr 2025 14:43:42 +0300 Subject: [PATCH 1/3] fix: normalizePathSlashes should normalize multiple leading slashes --- src/utils/__test__/index.test.ts | 14 +++++++++++--- src/utils/index.ts | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/utils/__test__/index.test.ts b/src/utils/__test__/index.test.ts index 7fb6a0ce4..83c510f33 100644 --- a/src/utils/__test__/index.test.ts +++ b/src/utils/__test__/index.test.ts @@ -16,6 +16,9 @@ describe('normalizePathSlashes', () => { test('should handle paths with multiple trailing slashes', () => { expect(normalizePathSlashes('path////')).toBe('path/'); }); + test('should handle paths with multiple leading slashes', () => { + expect(normalizePathSlashes('////path')).toBe('/path'); + }); test('should handle full paths with normal slashes', () => { expect(normalizePathSlashes('http://example.com/path/to/resource')).toBe( 'http://example.com/path/to/resource', @@ -26,9 +29,14 @@ describe('normalizePathSlashes', () => { 'http://example.com/path/to/resource', ); }); + test('shoudl not replace double slashes near protocols (after a colon)', () => { + expect(normalizePathSlashes('http://host.ydb.com')).toBe('http://host.ydb.com'); + expect(normalizePathSlashes('https://host.ydb.com')).toBe('https://host.ydb.com'); + expect(normalizePathSlashes('grpc://host.ydb.com')).toBe('grpc://host.ydb.com'); + }); test('should replace slashes more than two slashes after a colon', () => { - expect(normalizePathSlashes('http://///example.com/path/to/resource')).toBe( - 'http://example.com/path/to/resource', - ); + expect(normalizePathSlashes('http:////host.ydb.com')).toBe('http://host.ydb.com'); + expect(normalizePathSlashes('https://///host.ydb.com')).toBe('https://host.ydb.com'); + expect(normalizePathSlashes('grpc://///host.ydb.com')).toBe('grpc://host.ydb.com'); }); }); diff --git a/src/utils/index.ts b/src/utils/index.ts index 12eb24a54..8de8c148c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -14,5 +14,6 @@ export async function wait<T = unknown>(time: number, value?: T): Promise<T | un export function normalizePathSlashes(path: string) { // Prevent multiple slashes when concatenating path parts - return path.replaceAll(/([^:])(\/\/+)/g, '$1/'); + // (?<!:) - negative lookbehind - ignore parts that start with : + return path.replaceAll(/(?<!:)\/\/+/g, '/'); } From c4cc1b0c4950425b9a8f3a697ffa4e2edcd8b543 Mon Sep 17 00:00:00 2001 From: mufazalov <mufazalov@yandex-team.ru> Date: Tue, 22 Apr 2025 14:45:55 +0300 Subject: [PATCH 2/3] fix: fix typo --- src/utils/__test__/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/__test__/index.test.ts b/src/utils/__test__/index.test.ts index 83c510f33..68ee8371e 100644 --- a/src/utils/__test__/index.test.ts +++ b/src/utils/__test__/index.test.ts @@ -29,7 +29,7 @@ describe('normalizePathSlashes', () => { 'http://example.com/path/to/resource', ); }); - test('shoudl not replace double slashes near protocols (after a colon)', () => { + test('should not replace double slashes near protocols (after a colon)', () => { expect(normalizePathSlashes('http://host.ydb.com')).toBe('http://host.ydb.com'); expect(normalizePathSlashes('https://host.ydb.com')).toBe('https://host.ydb.com'); expect(normalizePathSlashes('grpc://host.ydb.com')).toBe('grpc://host.ydb.com'); From 7842a658a0ce06e38325bcbf67996376988cb240 Mon Sep 17 00:00:00 2001 From: mufazalov <mufazalov@yandex-team.ru> Date: Tue, 22 Apr 2025 15:18:35 +0300 Subject: [PATCH 3/3] fix: do not append empty basename to path --- src/routes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes.ts b/src/routes.ts index acd87cf1c..2dcee0832 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -86,7 +86,7 @@ export function createHref( const compiledRoute = `${compile(preparedRoute)(params)}${search}`; - if (options.withBasename) { + if (options.withBasename && basename) { // For SPA links react-router adds basename itself // It is needed for external links - <a> or uikit <Link> return normalizePathSlashes(`${basename}/${compiledRoute}`);