@@ -27,6 +27,7 @@ const { getConstructorOf, removeColors } = require('internal/util');
27
27
const {
28
28
ERR_ARG_NOT_ITERABLE ,
29
29
ERR_INVALID_ARG_TYPE ,
30
+ ERR_INVALID_ARG_VALUE ,
30
31
ERR_INVALID_CALLBACK ,
31
32
ERR_INVALID_FILE_URL_HOST ,
32
33
ERR_INVALID_FILE_URL_PATH ,
@@ -1365,27 +1366,54 @@ const backslashRegEx = /\\/g;
1365
1366
const newlineRegEx = / \n / g;
1366
1367
const carriageReturnRegEx = / \r / g;
1367
1368
const tabRegEx = / \t / g;
1369
+
1370
+ function encodePathChars ( filepath ) {
1371
+ if ( filepath . includes ( '%' ) )
1372
+ filepath = filepath . replace ( percentRegEx , '%25' ) ;
1373
+ // In posix, backslash is a valid character in paths:
1374
+ if ( ! isWindows && filepath . includes ( '\\' ) )
1375
+ filepath = filepath . replace ( backslashRegEx , '%5C' ) ;
1376
+ if ( filepath . includes ( '\n' ) )
1377
+ filepath = filepath . replace ( newlineRegEx , '%0A' ) ;
1378
+ if ( filepath . includes ( '\r' ) )
1379
+ filepath = filepath . replace ( carriageReturnRegEx , '%0D' ) ;
1380
+ if ( filepath . includes ( '\t' ) )
1381
+ filepath = filepath . replace ( tabRegEx , '%09' ) ;
1382
+ return filepath ;
1383
+ }
1384
+
1368
1385
function pathToFileURL ( filepath ) {
1369
- let resolved = path . resolve ( filepath ) ;
1370
- // path.resolve strips trailing slashes so we must add them back
1371
- const filePathLast = filepath . charCodeAt ( filepath . length - 1 ) ;
1372
- if ( ( filePathLast === CHAR_FORWARD_SLASH ||
1373
- ( isWindows && filePathLast === CHAR_BACKWARD_SLASH ) ) &&
1374
- resolved [ resolved . length - 1 ] !== path . sep )
1375
- resolved += '/' ;
1376
1386
const outURL = new URL ( 'file://' ) ;
1377
- if ( resolved . includes ( '%' ) )
1378
- resolved = resolved . replace ( percentRegEx , '%25' ) ;
1379
- // In posix, "/" is a valid character in paths
1380
- if ( ! isWindows && resolved . includes ( '\\' ) )
1381
- resolved = resolved . replace ( backslashRegEx , '%5C' ) ;
1382
- if ( resolved . includes ( '\n' ) )
1383
- resolved = resolved . replace ( newlineRegEx , '%0A' ) ;
1384
- if ( resolved . includes ( '\r' ) )
1385
- resolved = resolved . replace ( carriageReturnRegEx , '%0D' ) ;
1386
- if ( resolved . includes ( '\t' ) )
1387
- resolved = resolved . replace ( tabRegEx , '%09' ) ;
1388
- outURL . pathname = resolved ;
1387
+ if ( isWindows && filepath . startsWith ( '\\\\' ) ) {
1388
+ // UNC path format: \\server\share\resource
1389
+ const paths = filepath . split ( '\\' ) ;
1390
+ if ( paths . length <= 3 ) {
1391
+ throw new ERR_INVALID_ARG_VALUE (
1392
+ 'filepath' ,
1393
+ filepath ,
1394
+ 'Missing UNC resource path'
1395
+ ) ;
1396
+ }
1397
+ const hostname = paths [ 2 ] ;
1398
+ if ( hostname . length === 0 ) {
1399
+ throw new ERR_INVALID_ARG_VALUE (
1400
+ 'filepath' ,
1401
+ filepath ,
1402
+ 'Empty UNC servername'
1403
+ ) ;
1404
+ }
1405
+ outURL . hostname = domainToASCII ( hostname ) ;
1406
+ outURL . pathname = encodePathChars ( paths . slice ( 3 ) . join ( '/' ) ) ;
1407
+ } else {
1408
+ let resolved = path . resolve ( filepath ) ;
1409
+ // path.resolve strips trailing slashes so we must add them back
1410
+ const filePathLast = filepath . charCodeAt ( filepath . length - 1 ) ;
1411
+ if ( ( filePathLast === CHAR_FORWARD_SLASH ||
1412
+ ( isWindows && filePathLast === CHAR_BACKWARD_SLASH ) ) &&
1413
+ resolved [ resolved . length - 1 ] !== path . sep )
1414
+ resolved += '/' ;
1415
+ outURL . pathname = encodePathChars ( resolved ) ;
1416
+ }
1389
1417
return outURL ;
1390
1418
}
1391
1419
0 commit comments