From da013c2923490b2194f057837580197eb22954d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Wed, 20 Apr 2022 05:25:18 -0700 Subject: [PATCH 1/3] Percent encode relative path like url.pathToFileURL --- lib/src/utils.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/src/utils.ts b/lib/src/utils.ts index 56c02dfa..ef108bf9 100644 --- a/lib/src/utils.ts +++ b/lib/src/utils.ts @@ -90,8 +90,14 @@ export function valueError(message: string, name?: string): Error { export function pathToUrlString(path: string): string { if (p.isAbsolute(path)) return url.pathToFileURL(path).toString(); - const components = p.sep === '\\' ? path.split(/[/\\]/) : path.split('/'); - return components.map(encodeURIComponent).join('/'); + // percent encode relative path like `pathToFileURL` + return encodeURI(path) + .replace(/[#?]/g, encodeURIComponent) + .replace( + process.platform === 'win32' ? /%(5B|5C|5D|5E|7C)/g : /%(5B|5D|5E|7C)/g, + decodeURIComponent + ) + .replace(/\\/g, '/'); } /** From f5b0e265370dbfe1f2654678b58dea3ef36e316e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Wed, 20 Apr 2022 09:44:27 -0700 Subject: [PATCH 2/3] Add test for pathToUrlString --- lib/src/utils.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/src/utils.test.ts diff --git a/lib/src/utils.test.ts b/lib/src/utils.test.ts new file mode 100644 index 00000000..b3110947 --- /dev/null +++ b/lib/src/utils.test.ts @@ -0,0 +1,19 @@ +import {pathToFileURL} from 'url'; +import {pathToUrlString} from './utils'; + +describe('utils', () => { + describe('pathToUrlString', () => { + it('encode relative path like `pathToFileURL`', () => { + const baseURL = pathToFileURL('').toString(); + for (let i = 0; i < 128; i++) { + const char = String.fromCharCode(i); + const filename = `${i}-${char}`; + expect(pathToUrlString(filename)).toEqual( + pathToFileURL(filename) + .toString() + .slice(baseURL.length + 1) + ); + } + }); + }); +}); From 60b76a1bbde7156d235e1947c5815c6ea992504e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Mon, 25 Apr 2022 23:52:24 -0700 Subject: [PATCH 3/3] Add test cases for already encoded string --- lib/src/utils.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/src/utils.test.ts b/lib/src/utils.test.ts index b3110947..0e7f29c7 100644 --- a/lib/src/utils.test.ts +++ b/lib/src/utils.test.ts @@ -15,5 +15,23 @@ describe('utils', () => { ); } }); + + it('encode percent encoded string like `pathToFileURL`', () => { + const baseURL = pathToFileURL('').toString(); + for (let i = 0; i < 128; i++) { + const lowercase = `%${i < 10 ? '0' : ''}${i.toString(16)}`; + expect(pathToUrlString(lowercase)).toEqual( + pathToFileURL(lowercase) + .toString() + .slice(baseURL.length + 1) + ); + const uppercase = lowercase.toUpperCase(); + expect(pathToUrlString(uppercase)).toEqual( + pathToFileURL(uppercase) + .toString() + .slice(baseURL.length + 1) + ); + } + }); }); });