Skip to content

Commit 2645ec4

Browse files
committed
url: modify pathToFileURL to handle extended UNC paths
1 parent 2bcf999 commit 2645ec4

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/internal/url.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,8 +1539,11 @@ function pathToFileURL(filepath, options = kEmptyObject) {
15391539
const windows = options?.windows;
15401540
if ((windows ?? isWindows) && StringPrototypeStartsWith(filepath, '\\\\')) {
15411541
const outURL = new URL('file://');
1542+
// Handle extended UNC path and standard UNC path
15421543
// UNC path format: \\server\share\resource
1543-
const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', 2);
1544+
const isExtendedUNC = StringPrototypeStartsWith(filepath, '\\\\?\\UNC\\');
1545+
const prefixLength = isExtendedUNC ? 8 : 2;
1546+
const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', prefixLength);
15441547
if (hostnameEndIndex === -1) {
15451548
throw new ERR_INVALID_ARG_VALUE(
15461549
'path',
@@ -1555,7 +1558,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
15551558
'Empty UNC servername',
15561559
);
15571560
}
1558-
const hostname = StringPrototypeSlice(filepath, 2, hostnameEndIndex);
1561+
const hostname = StringPrototypeSlice(filepath, prefixLength, hostnameEndIndex);
15591562
outURL.hostname = domainToASCII(hostname);
15601563
outURL.pathname = encodePathChars(
15611564
RegExpPrototypeSymbolReplace(backslashRegEx, StringPrototypeSlice(filepath, hostnameEndIndex), '/'),

test/parallel/test-url-pathtofileurl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ const windowsTestCases = [
104104
{ path: 'C:\\€', expected: 'file:///C:/%E2%82%AC' },
105105
// Rocket emoji (non-BMP code point)
106106
{ path: 'C:\\🚀', expected: 'file:///C:/%F0%9F%9A%80' },
107+
// Local extended path
108+
{ path: '\\\\?\\C:\\path\\to\\file.txt', expected: 'file:///C:/path/to/file.txt' },
107109
// UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows)
108110
{ path: '\\\\nas\\My Docs\\File.doc', expected: 'file://nas/My%20Docs/File.doc' },
111+
// Extended UNC path
112+
{ path: '\\\\?\\UNC\\server\\share\\folder\\file.txt', expected: 'file://server/share/folder/file.txt' }
109113
];
110114
const posixTestCases = [
111115
// Lowercase ascii alpha

0 commit comments

Comments
 (0)