Skip to content

Commit d63f98e

Browse files
committed
Update relative pathToFileURL to match node
1 parent ebeb169 commit d63f98e

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

lib/src/utils.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,29 @@ 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+
return unsafePathToFileURL
99+
? encodeURI(path)
100+
.replace(/[#?]/g, encodeURIComponent)
101+
.replace(
102+
process.platform === 'win32'
103+
? /%(5B|5C|5D|5E|7C)/g
104+
: /%(5B|5D|5E|7C)/g,
105+
decodeURIComponent,
106+
)
107+
.replace(/\\/g, '/')
108+
: encodeURI(path)
109+
.replace(/[#?]/g, encodeURIComponent)
110+
.replace(/~/g, '%7E')
111+
.replace(/\\/g, '/');
101112
}
102113

103114
/**

0 commit comments

Comments
 (0)