9
9
ArrayPrototypeSlice,
10
10
FunctionPrototypeBind,
11
11
Int8Array,
12
+ IteratorPrototype,
12
13
Number,
13
14
ObjectDefineProperties,
14
15
ObjectDefineProperty,
@@ -18,11 +19,13 @@ const {
18
19
ReflectApply,
19
20
ReflectGetOwnPropertyDescriptor,
20
21
ReflectOwnKeys,
22
+ RegExpPrototypeSymbolReplace,
21
23
String,
24
+ StringPrototypeCharAt,
22
25
StringPrototypeCharCodeAt,
26
+ StringPrototypeCodePointAt,
23
27
StringPrototypeIncludes,
24
- StringPrototypeReplace,
25
- StringPrototypeReplaceAll,
28
+ StringPrototypeIndexOf,
26
29
StringPrototypeSlice,
27
30
StringPrototypeSplit,
28
31
StringPrototypeStartsWith,
@@ -44,6 +47,7 @@ const {
44
47
removeColors,
45
48
toUSVString,
46
49
kEnumerableProperty,
50
+ SideEffectFreeRegExpPrototypeSymbolReplace,
47
51
} = require ( 'internal/util' ) ;
48
52
49
53
const {
@@ -112,6 +116,8 @@ const {
112
116
revokeDataObject,
113
117
} = internalBinding ( 'blob' ) ;
114
118
119
+ const FORWARD_SLASH = / \/ / g;
120
+
115
121
const context = Symbol ( 'context' ) ;
116
122
const cannotBeBase = Symbol ( 'cannot-be-base' ) ;
117
123
const cannotHaveUsernamePasswordPort =
@@ -138,11 +144,6 @@ function lazyCryptoRandom() {
138
144
return cryptoRandom ;
139
145
}
140
146
141
- // https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object
142
- const IteratorPrototype = ObjectGetPrototypeOf (
143
- ObjectGetPrototypeOf ( [ ] [ SymbolIterator ] ( ) )
144
- ) ;
145
-
146
147
// Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque
147
148
const kOpaqueOrigin = 'null' ;
148
149
@@ -1376,7 +1377,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', {
1376
1377
} ,
1377
1378
[ ]
1378
1379
) ;
1379
- const breakLn = inspect ( output , innerOpts ) . includes ( '\n' ) ;
1380
+ const breakLn = StringPrototypeIncludes ( inspect ( output , innerOpts ) , '\n' ) ;
1380
1381
const outputStrs = ArrayPrototypeMap ( output , ( p ) => inspect ( p , innerOpts ) ) ;
1381
1382
let outputStr ;
1382
1383
if ( breakLn ) {
@@ -1434,7 +1435,7 @@ function getPathFromURLWin32(url) {
1434
1435
let pathname = url . pathname ;
1435
1436
for ( let n = 0 ; n < pathname . length ; n ++ ) {
1436
1437
if ( pathname [ n ] === '%' ) {
1437
- const third = pathname . codePointAt ( n + 2 ) | 0x20 ;
1438
+ const third = StringPrototypeCodePointAt ( pathname , n + 2 ) | 0x20 ;
1438
1439
if ( ( pathname [ n + 1 ] === '2' && third === 102 ) || // 2f 2F /
1439
1440
( pathname [ n + 1 ] === '5' && third === 99 ) ) { // 5c 5C \
1440
1441
throw new ERR_INVALID_FILE_URL_PATH (
@@ -1443,7 +1444,7 @@ function getPathFromURLWin32(url) {
1443
1444
}
1444
1445
}
1445
1446
}
1446
- pathname = StringPrototypeReplaceAll ( pathname , '/' , '\\' ) ;
1447
+ pathname = SideEffectFreeRegExpPrototypeSymbolReplace ( FORWARD_SLASH , pathname , '\\' ) ;
1447
1448
pathname = decodeURIComponent ( pathname ) ;
1448
1449
if ( hostname !== '' ) {
1449
1450
// If hostname is set, then we have a UNC path
@@ -1455,13 +1456,13 @@ function getPathFromURLWin32(url) {
1455
1456
return `\\\\${ domainToUnicode ( hostname ) } ${ pathname } ` ;
1456
1457
}
1457
1458
// Otherwise, it's a local path that requires a drive letter
1458
- const letter = pathname . codePointAt ( 1 ) | 0x20 ;
1459
- const sep = pathname [ 2 ] ;
1459
+ const letter = StringPrototypeCodePointAt ( pathname , 1 ) | 0x20 ;
1460
+ const sep = StringPrototypeCharAt ( pathname , 2 ) ;
1460
1461
if ( letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z
1461
1462
( sep !== ':' ) ) {
1462
1463
throw new ERR_INVALID_FILE_URL_PATH ( 'must be absolute' ) ;
1463
1464
}
1464
- return pathname . slice ( 1 ) ;
1465
+ return StringPrototypeSlice ( pathname , 1 ) ;
1465
1466
}
1466
1467
1467
1468
function getPathFromURLPosix ( url ) {
@@ -1471,7 +1472,7 @@ function getPathFromURLPosix(url) {
1471
1472
const pathname = url . pathname ;
1472
1473
for ( let n = 0 ; n < pathname . length ; n ++ ) {
1473
1474
if ( pathname [ n ] === '%' ) {
1474
- const third = pathname . codePointAt ( n + 2 ) | 0x20 ;
1475
+ const third = StringPrototypeCodePointAt ( pathname , n + 2 ) | 0x20 ;
1475
1476
if ( pathname [ n + 1 ] === '2' && third === 102 ) {
1476
1477
throw new ERR_INVALID_FILE_URL_PATH (
1477
1478
'must not include encoded / characters'
@@ -1511,42 +1512,42 @@ const tabRegEx = /\t/g;
1511
1512
1512
1513
function encodePathChars ( filepath ) {
1513
1514
if ( StringPrototypeIncludes ( filepath , '%' ) )
1514
- filepath = StringPrototypeReplace ( filepath , percentRegEx , '%25' ) ;
1515
+ filepath = RegExpPrototypeSymbolReplace ( percentRegEx , filepath , '%25' ) ;
1515
1516
// In posix, backslash is a valid character in paths:
1516
1517
if ( ! isWindows && StringPrototypeIncludes ( filepath , '\\' ) )
1517
- filepath = StringPrototypeReplace ( filepath , backslashRegEx , '%5C' ) ;
1518
+ filepath = RegExpPrototypeSymbolReplace ( backslashRegEx , filepath , '%5C' ) ;
1518
1519
if ( StringPrototypeIncludes ( filepath , '\n' ) )
1519
- filepath = StringPrototypeReplace ( filepath , newlineRegEx , '%0A' ) ;
1520
+ filepath = RegExpPrototypeSymbolReplace ( newlineRegEx , filepath , '%0A' ) ;
1520
1521
if ( StringPrototypeIncludes ( filepath , '\r' ) )
1521
- filepath = StringPrototypeReplace ( filepath , carriageReturnRegEx , '%0D' ) ;
1522
+ filepath = RegExpPrototypeSymbolReplace ( carriageReturnRegEx , filepath , '%0D' ) ;
1522
1523
if ( StringPrototypeIncludes ( filepath , '\t' ) )
1523
- filepath = StringPrototypeReplace ( filepath , tabRegEx , '%09' ) ;
1524
+ filepath = RegExpPrototypeSymbolReplace ( tabRegEx , filepath , '%09' ) ;
1524
1525
return filepath ;
1525
1526
}
1526
1527
1527
1528
function pathToFileURL ( filepath ) {
1528
1529
const outURL = new URL ( 'file://' ) ;
1529
1530
if ( isWindows && StringPrototypeStartsWith ( filepath , '\\\\' ) ) {
1530
1531
// UNC path format: \\server\share\resource
1531
- const paths = StringPrototypeSplit ( filepath , '\\' ) ;
1532
- if ( paths . length <= 3 ) {
1532
+ const hostnameEndIndex = StringPrototypeIndexOf ( filepath , '\\' , 2 ) ;
1533
+ if ( hostnameEndIndex === - 1 ) {
1533
1534
throw new ERR_INVALID_ARG_VALUE (
1534
1535
'filepath' ,
1535
1536
filepath ,
1536
1537
'Missing UNC resource path'
1537
1538
) ;
1538
1539
}
1539
- const hostname = paths [ 2 ] ;
1540
- if ( hostname . length === 0 ) {
1540
+ if ( hostnameEndIndex === 2 ) {
1541
1541
throw new ERR_INVALID_ARG_VALUE (
1542
1542
'filepath' ,
1543
1543
filepath ,
1544
1544
'Empty UNC servername'
1545
1545
) ;
1546
1546
}
1547
+ const hostname = StringPrototypeSlice ( filepath , 2 , hostnameEndIndex ) ;
1547
1548
outURL . hostname = domainToASCII ( hostname ) ;
1548
1549
outURL . pathname = encodePathChars (
1549
- ArrayPrototypeJoin ( ArrayPrototypeSlice ( paths , 3 ) , '/' ) ) ;
1550
+ RegExpPrototypeSymbolReplace ( backslashRegEx , StringPrototypeSlice ( filepath , hostnameEndIndex ) , '/' ) ) ;
1550
1551
} else {
1551
1552
let resolved = path . resolve ( filepath ) ;
1552
1553
// path.resolve strips trailing slashes so we must add them back
0 commit comments