Skip to content

Commit 728c3fd

Browse files
injunchoi98targos
authored andcommitted
url: modify pathToFileURL to handle extended UNC path
PR-URL: #54262 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 8b71fa7 commit 728c3fd

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,12 @@ function pathToFileURL(filepath, options = kEmptyObject) {
15301530
if((windows??isWindows)&&StringPrototypeStartsWith(filepath,'\\\\')){
15311531
constoutURL=newURL('file://');
15321532
// UNC path format: \\server\share\resource
1533-
consthostnameEndIndex=StringPrototypeIndexOf(filepath,'\\',2);
1533+
// Handle extended UNC path and standard UNC path
1534+
// "\\?\UNC\" path prefix should be ignored.
1535+
// Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
1536+
constisExtendedUNC=StringPrototypeStartsWith(filepath,'\\\\?\\UNC\\');
1537+
constprefixLength=isExtendedUNC ? 8 : 2;
1538+
consthostnameEndIndex=StringPrototypeIndexOf(filepath,'\\',prefixLength);
15341539
if(hostnameEndIndex===-1){
15351540
thrownewERR_INVALID_ARG_VALUE(
15361541
'path',
@@ -1545,7 +1550,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
15451550
'Empty UNC servername',
15461551
);
15471552
}
1548-
consthostname=StringPrototypeSlice(filepath,2,hostnameEndIndex);
1553+
consthostname=StringPrototypeSlice(filepath,prefixLength,hostnameEndIndex);
15491554
outURL.hostname=domainToASCII(hostname);
15501555
outURL.pathname=encodePathChars(
15511556
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
constposixTestCases=[
111115
// Lowercase ascii alpha

0 commit comments

Comments
 (0)