Skip to content

Commit 5a10661

Browse files
authored
Update relative pathToFileURL to match node (#352)
1 parent ebeb169 commit 5a10661

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lib/src/utils.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,28 @@ export function valueError(message: string, name?: string): Error {
8686
return Error(name ? `$${name}: ${message}.` : `${message}.`);
8787
}
8888

89+
// Node changed its implementation of pathToFileURL:
90+
// https://github.com/nodejs/node/pull/54545
91+
const unsafePathToFileURL = url.pathToFileURL('~').pathname.endsWith('~');
92+
8993
/** Converts a (possibly relative) path on the local filesystem to a URL. */
9094
export function pathToUrlString(path: string): string {
9195
if (p.isAbsolute(path)) return url.pathToFileURL(path).toString();
9296

9397
// percent encode relative path like `pathToFileURL`
94-
return encodeURI(path)
95-
.replace(/[#?]/g, encodeURIComponent)
96-
.replace(
97-
process.platform === 'win32' ? /%(5B|5C|5D|5E|7C)/g : /%(5B|5D|5E|7C)/g,
98-
decodeURIComponent,
99-
)
100-
.replace(/\\/g, '/');
98+
let fileUrl = encodeURI(path).replace(/[#?]/g, encodeURIComponent);
99+
100+
if (unsafePathToFileURL) {
101+
fileUrl = fileUrl.replace(/%(5B|5D|5E|7C)/g, decodeURIComponent);
102+
} else {
103+
fileUrl = fileUrl.replace(/~/g, '%7E');
104+
}
105+
106+
if (process.platform === 'win32') {
107+
fileUrl = fileUrl.replace(/%5C/g, '/');
108+
}
109+
110+
return fileUrl;
101111
}
102112

103113
/**

0 commit comments

Comments
 (0)