GeezerVison is showing github.com — url: modify pathToFileURL to handle extended UNC path · nodejs/node@0bed600
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1540,7 +1540,12 @@ function pathToFileURL(filepath, options = kEmptyObject) { | 1540 | 1540 | if ((windows ?? isWindows) && StringPrototypeStartsWith(filepath, '\\\\')) { | 1541 | 1541 | const outURL = new URL('file://'); | 1542 | 1542 | // UNC path format: \\server\share\resource | 1543 | | - const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', 2); | | 1543 | + // Handle extended UNC path and standard UNC path | | 1544 | + // "\\?\UNC\" path prefix should be ignored. | | 1545 | + // Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation | | 1546 | + const isExtendedUNC = StringPrototypeStartsWith(filepath, '\\\\?\\UNC\\'); | | 1547 | + const prefixLength = isExtendedUNC ? 8 : 2; | | 1548 | + const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', prefixLength); | 1544 | 1549 | if (hostnameEndIndex === -1) { | 1545 | 1550 | throw new ERR_INVALID_ARG_VALUE( | 1546 | 1551 | 'path', | @@ -1555,7 +1560,7 @@ function pathToFileURL(filepath, options = kEmptyObject) { | 1555 | 1560 | 'Empty UNC servername', | 1556 | 1561 | ); | 1557 | 1562 | } | 1558 | | - const hostname = StringPrototypeSlice(filepath, 2, hostnameEndIndex); | | 1563 | + const hostname = StringPrototypeSlice(filepath, prefixLength, hostnameEndIndex); | 1559 | 1564 | outURL.hostname = domainToASCII(hostname); | 1560 | 1565 | outURL.pathname = encodePathChars( | 1561 | 1566 | RegExpPrototypeSymbolReplace(backslashRegEx, StringPrototypeSlice(filepath, hostnameEndIndex), '/'), | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-url-pathtofileurl.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -104,8 +104,12 @@ const windowsTestCases = [ | 104 | 104 | { path: 'C:\\€', expected: 'file:///C:/%E2%82%AC' }, | 105 | 105 | // Rocket emoji (non-BMP code point) | 106 | 106 | { 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' }, | 107 | 109 | // UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows) | 108 | 110 | { 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' }, | 109 | 113 | ]; | 110 | 114 | const posixTestCases = [ | 111 | 115 | // Lowercase ascii alpha | |
|