From d63f98e52696e98e7831a5248b2304d70920ec09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Tue, 10 Dec 2024 11:02:27 -0800 Subject: [PATCH] Update relative pathToFileURL to match node --- lib/src/utils.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/src/utils.ts b/lib/src/utils.ts index 055df546..cc005843 100644 --- a/lib/src/utils.ts +++ b/lib/src/utils.ts @@ -86,18 +86,29 @@ export function valueError(message: string, name?: string): Error { return Error(name ? `$${name}: ${message}.` : `${message}.`); } +// Node changed its implementation of pathToFileURL: +// https://github.com/nodejs/node/pull/54545 +const unsafePathToFileURL = url.pathToFileURL('~').pathname.endsWith('~'); + /** Converts a (possibly relative) path on the local filesystem to a URL. */ export function pathToUrlString(path: string): string { if (p.isAbsolute(path)) return url.pathToFileURL(path).toString(); // 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, '/'); + return unsafePathToFileURL + ? encodeURI(path) + .replace(/[#?]/g, encodeURIComponent) + .replace( + process.platform === 'win32' + ? /%(5B|5C|5D|5E|7C)/g + : /%(5B|5D|5E|7C)/g, + decodeURIComponent, + ) + .replace(/\\/g, '/') + : encodeURI(path) + .replace(/[#?]/g, encodeURIComponent) + .replace(/~/g, '%7E') + .replace(/\\/g, '/'); } /**