Skip to content

Commit

Permalink
url: modify pathToFileURL to handle extended UNC paths
Browse files Browse the repository at this point in the history
  • Loading branch information
EarlyRiser42 committed Aug 8, 2024
1 parent 2bcf999 commit 2645ec4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1539,8 +1539,11 @@ function pathToFileURL(filepath, options = kEmptyObject) {
const windows = options?.windows;
if ((windows ?? isWindows) && StringPrototypeStartsWith(filepath, '\\\\')) {
const outURL = new URL('file://');
// Handle extended UNC path and standard UNC path
// UNC path format: \\server\share\resource
const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', 2);
const isExtendedUNC = StringPrototypeStartsWith(filepath, '\\\\?\\UNC\\');
const prefixLength = isExtendedUNC ? 8 : 2;
const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', prefixLength);
if (hostnameEndIndex === -1) {
throw new ERR_INVALID_ARG_VALUE(
'path',
Expand All @@ -1555,7 +1558,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
'Empty UNC servername',
);
}
const hostname = StringPrototypeSlice(filepath, 2, hostnameEndIndex);
const hostname = StringPrototypeSlice(filepath, prefixLength, hostnameEndIndex);
outURL.hostname = domainToASCII(hostname);
outURL.pathname = encodePathChars(
RegExpPrototypeSymbolReplace(backslashRegEx, StringPrototypeSlice(filepath, hostnameEndIndex), '/'),
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-url-pathtofileurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ const windowsTestCases = [
{ path: 'C:\\€', expected: 'file:///C:/%E2%82%AC' },
// Rocket emoji (non-BMP code point)
{ path: 'C:\\🚀', expected: 'file:///C:/%F0%9F%9A%80' },
// Local extended path
{ path: '\\\\?\\C:\\path\\to\\file.txt', expected: 'file:///C:/path/to/file.txt' },
// UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows)
{ path: '\\\\nas\\My Docs\\File.doc', expected: 'file://nas/My%20Docs/File.doc' },
// Extended UNC path
{ path: '\\\\?\\UNC\\server\\share\\folder\\file.txt', expected: 'file://server/share/folder/file.txt' }

Check failure on line 112 in test/parallel/test-url-pathtofileurl.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
];
const posixTestCases = [
// Lowercase ascii alpha
Expand Down

0 comments on commit 2645ec4

Please sign in to comment.