@@ -1512,32 +1512,35 @@ function fileURLToPath(path, options = kEmptyObject) {
15121512
15131513function pathToFileURL ( filepath , options = kEmptyObject ) {
15141514 const windows = options ?. windows ?? isWindows ;
1515- if ( windows && StringPrototypeStartsWith ( filepath , '\\\\' ) ) {
1515+ const isUNC = windows && StringPrototypeStartsWith ( filepath , '\\\\' ) ;
1516+ let resolved = isUNC ?
1517+ filepath :
1518+ ( windows ? path . win32 . resolve ( filepath ) : path . posix . resolve ( filepath ) ) ;
1519+ if ( isUNC || ( windows && StringPrototypeStartsWith ( resolved , '\\\\' ) ) ) {
15161520 // UNC path format: \\server\share\resource
15171521 // Handle extended UNC path and standard UNC path
15181522 // "\\?\UNC\" path prefix should be ignored.
15191523 // Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
1520- const isExtendedUNC = StringPrototypeStartsWith ( filepath , '\\\\?\\UNC\\' ) ;
1524+ const isExtendedUNC = StringPrototypeStartsWith ( resolved , '\\\\?\\UNC\\' ) ;
15211525 const prefixLength = isExtendedUNC ? 8 : 2 ;
1522- const hostnameEndIndex = StringPrototypeIndexOf ( filepath , '\\' , prefixLength ) ;
1526+ const hostnameEndIndex = StringPrototypeIndexOf ( resolved , '\\' , prefixLength ) ;
15231527 if ( hostnameEndIndex === - 1 ) {
15241528 throw new ERR_INVALID_ARG_VALUE (
15251529 'path' ,
1526- filepath ,
1530+ resolved ,
15271531 'Missing UNC resource path' ,
15281532 ) ;
15291533 }
15301534 if ( hostnameEndIndex === 2 ) {
15311535 throw new ERR_INVALID_ARG_VALUE (
15321536 'path' ,
1533- filepath ,
1537+ resolved ,
15341538 'Empty UNC servername' ,
15351539 ) ;
15361540 }
1537- const hostname = StringPrototypeSlice ( filepath , prefixLength , hostnameEndIndex ) ;
1538- return new URL ( StringPrototypeSlice ( filepath , hostnameEndIndex ) , hostname , kCreateURLFromWindowsPathSymbol ) ;
1541+ const hostname = StringPrototypeSlice ( resolved , prefixLength , hostnameEndIndex ) ;
1542+ return new URL ( StringPrototypeSlice ( resolved , hostnameEndIndex ) , hostname , kCreateURLFromWindowsPathSymbol ) ;
15391543 }
1540- let resolved = windows ? path . win32 . resolve ( filepath ) : path . posix . resolve ( filepath ) ;
15411544 // path.resolve strips trailing slashes so we must add them back
15421545 const filePathLast = StringPrototypeCharCodeAt ( filepath ,
15431546 filepath . length - 1 ) ;
0 commit comments