| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common.js'); | ||
| 3 | + const { fileURLToPath, pathToFileURL } = require('node:url'); | ||
| 4 | + const isWindows = process.platform === 'win32'; | ||
| 5 | + | ||
| 6 | + const bench = common.createBenchmark(main, { | ||
| 7 | + input: isWindows ? [ | ||
| 8 | + 'file:///c/', | ||
| 9 | + ] : [ | ||
| 10 | + 'file:///dev/null', | ||
| 11 | + 'file:///dev/null?key=param&bool', | ||
| 12 | + 'file:///dev/null?key=param&bool#hash', | ||
| 13 | + ], | ||
| 14 | + method: isWindows ? [ | ||
| 15 | + 'fileURLToPath', | ||
| 16 | + ] : [ | ||
| 17 | + 'fileURLToPath', | ||
| 18 | + 'pathToFileURL', | ||
| 19 | + ], | ||
| 20 | + n: [5e6], | ||
| 21 | + }); | ||
| 22 | + | ||
| 23 | + function main({ n, input, method }) { | ||
| 24 | + method = method === 'fileURLOrPath' ? fileURLToPath : pathToFileURL; | ||
| 25 | + bench.start(); | ||
| 26 | + for (let i = 0; i < n; i++) { | ||
| 27 | + method(input); | ||
| 28 | + } | ||
| 29 | + bench.end(n); | ||
| 30 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,6 +61,7 @@ const { | |||
| 61 | 61 | Int32ArrayPrototype, | |
| 62 | 62 | Int8Array, | |
| 63 | 63 | Int8ArrayPrototype, | |
| 64 | + IteratorPrototype, | ||
| 64 | 65 | Map, | |
| 65 | 66 | MapPrototype, | |
| 66 | 67 | Number, | |
@@ -211,7 +212,7 @@ module.exports = function() { | |||
| 211 | 212 | ||
| 212 | 213 | // 27 Control Abstraction Objects | |
| 213 | 214 | // 27.1 Iteration | |
| 214 | - ObjectGetPrototypeOf(ArrayIteratorPrototype), // 27.1.2 IteratorPrototype | ||
| 215 | + IteratorPrototype, // 27.1.2 IteratorPrototype | ||
| 215 | 216 | // 27.1.3 AsyncIteratorPrototype | |
| 216 | 217 | ObjectGetPrototypeOf(ObjectGetPrototypeOf(ObjectGetPrototypeOf( | |
| 217 | 218 | (async function*() {})() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -260,6 +260,8 @@ function copyPrototype(src, dest, prefix) { | |||
| 260 | 260 | copyPrototype(original.prototype, primordials, `${name}Prototype`); | |
| 261 | 261 | }); | |
| 262 | 262 | ||
| 263 | + primordials.IteratorPrototype = Reflect.getPrototypeOf(primordials.ArrayIteratorPrototype); | ||
| 264 | + | ||
| 263 | 265 | /* eslint-enable node-core/prefer-primordials */ | |
| 264 | 266 | ||
| 265 | 267 | const { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ const { | |||
| 9 | 9 | ArrayPrototypeSlice, | |
| 10 | 10 | FunctionPrototypeBind, | |
| 11 | 11 | Int8Array, | |
| 12 | + IteratorPrototype, | ||
| 12 | 13 | Number, | |
| 13 | 14 | ObjectCreate, | |
| 14 | 15 | ObjectDefineProperties, | |
@@ -19,11 +20,13 @@ const { | |||
| 19 | 20 | ReflectApply, | |
| 20 | 21 | ReflectGetOwnPropertyDescriptor, | |
| 21 | 22 | ReflectOwnKeys, | |
| 23 | + RegExpPrototypeSymbolReplace, | ||
| 22 | 24 | String, | |
| 25 | + StringPrototypeCharAt, | ||
| 23 | 26 | StringPrototypeCharCodeAt, | |
| 27 | + StringPrototypeCodePointAt, | ||
| 24 | 28 | StringPrototypeIncludes, | |
| 25 | - StringPrototypeReplace, | ||
| 26 | - StringPrototypeReplaceAll, | ||
| 29 | + StringPrototypeIndexOf, | ||
| 27 | 30 | StringPrototypeSlice, | |
| 28 | 31 | StringPrototypeSplit, | |
| 29 | 32 | StringPrototypeStartsWith, | |
@@ -45,6 +48,7 @@ const { | |||
| 45 | 48 | removeColors, | |
| 46 | 49 | toUSVString, | |
| 47 | 50 | kEnumerableProperty, | |
| 51 | + SideEffectFreeRegExpPrototypeSymbolReplace, | ||
| 48 | 52 | } = require('internal/util'); | |
| 49 | 53 | ||
| 50 | 54 | const { | |
@@ -113,6 +117,8 @@ const { | |||
| 113 | 117 | revokeDataObject, | |
| 114 | 118 | } = internalBinding('blob'); | |
| 115 | 119 | ||
| 120 | + const FORWARD_SLASH = /\//g; | ||
| 121 | + | ||
| 116 | 122 | const context = Symbol('context'); | |
| 117 | 123 | const cannotBeBase = Symbol('cannot-be-base'); | |
| 118 | 124 | const cannotHaveUsernamePasswordPort = | |
@@ -139,11 +145,6 @@ function lazyCryptoRandom() { | |||
| 139 | 145 | return cryptoRandom; | |
| 140 | 146 | } | |
| 141 | 147 | ||
| 142 | - // https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object | ||
| 143 | - const IteratorPrototype = ObjectGetPrototypeOf( | ||
| 144 | - ObjectGetPrototypeOf([][SymbolIterator]()) | ||
| 145 | - ); | ||
| 146 | - | ||
| 147 | 148 | // Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque | |
| 148 | 149 | const kOpaqueOrigin = 'null'; | |
| 149 | 150 | ||
@@ -1377,7 +1378,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', { | |||
| 1377 | 1378 | }, | |
| 1378 | 1379 | [] | |
| 1379 | 1380 | ); | |
| 1380 | - const breakLn = inspect(output, innerOpts).includes('\n'); | ||
| 1381 | + const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n'); | ||
| 1381 | 1382 | const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts)); | |
| 1382 | 1383 | let outputStr; | |
| 1383 | 1384 | if (breakLn) { | |
@@ -1435,7 +1436,7 @@ function getPathFromURLWin32(url) { | |||
| 1435 | 1436 | let pathname = url.pathname; | |
| 1436 | 1437 | for (let n = 0; n < pathname.length; n++) { | |
| 1437 | 1438 | if (pathname[n] === '%') { | |
| 1438 | - const third = pathname.codePointAt(n + 2) | 0x20; | ||
| 1439 | + const third = StringPrototypeCodePointAt(pathname, n + 2) | 0x20; | ||
| 1439 | 1440 | if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F / | |
| 1440 | 1441 | (pathname[n + 1] === '5' && third === 99)) { // 5c 5C \ | |
| 1441 | 1442 | throw new ERR_INVALID_FILE_URL_PATH( | |
@@ -1444,7 +1445,7 @@ function getPathFromURLWin32(url) { | |||
| 1444 | 1445 | } | |
| 1445 | 1446 | } | |
| 1446 | 1447 | } | |
| 1447 | - pathname = StringPrototypeReplaceAll(pathname, '/', '\\'); | ||
| 1448 | + pathname = SideEffectFreeRegExpPrototypeSymbolReplace(FORWARD_SLASH, pathname, '\\'); | ||
| 1448 | 1449 | pathname = decodeURIComponent(pathname); | |
| 1449 | 1450 | if (hostname !== '') { | |
| 1450 | 1451 | // If hostname is set, then we have a UNC path | |
@@ -1456,13 +1457,13 @@ function getPathFromURLWin32(url) { | |||
| 1456 | 1457 | return `\\\\${domainToUnicode(hostname)}${pathname}`; | |
| 1457 | 1458 | } | |
| 1458 | 1459 | // Otherwise, it's a local path that requires a drive letter | |
| 1459 | - const letter = pathname.codePointAt(1) | 0x20; | ||
| 1460 | - const sep = pathname[2]; | ||
| 1460 | + const letter = StringPrototypeCodePointAt(pathname, 1) | 0x20; | ||
| 1461 | + const sep = StringPrototypeCharAt(pathname, 2); | ||
| 1461 | 1462 | if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z | |
| 1462 | 1463 | (sep !== ':')) { | |
| 1463 | 1464 | throw new ERR_INVALID_FILE_URL_PATH('must be absolute'); | |
| 1464 | 1465 | } | |
| 1465 | - return pathname.slice(1); | ||
| 1466 | + return StringPrototypeSlice(pathname, 1); | ||
| 1466 | 1467 | } | |
| 1467 | 1468 | ||
| 1468 | 1469 | function getPathFromURLPosix(url) { | |
@@ -1472,7 +1473,7 @@ function getPathFromURLPosix(url) { | |||
| 1472 | 1473 | const pathname = url.pathname; | |
| 1473 | 1474 | for (let n = 0; n < pathname.length; n++) { | |
| 1474 | 1475 | if (pathname[n] === '%') { | |
| 1475 | - const third = pathname.codePointAt(n + 2) | 0x20; | ||
| 1476 | + const third = StringPrototypeCodePointAt(pathname, n + 2) | 0x20; | ||
| 1476 | 1477 | if (pathname[n + 1] === '2' && third === 102) { | |
| 1477 | 1478 | throw new ERR_INVALID_FILE_URL_PATH( | |
| 1478 | 1479 | 'must not include encoded / characters' | |
@@ -1512,42 +1513,42 @@ const tabRegEx = /\t/g; | |||
| 1512 | 1513 | ||
| 1513 | 1514 | function encodePathChars(filepath) { | |
| 1514 | 1515 | if (StringPrototypeIncludes(filepath, '%')) | |
| 1515 | - filepath = StringPrototypeReplace(filepath, percentRegEx, '%25'); | ||
| 1516 | + filepath = RegExpPrototypeSymbolReplace(percentRegEx, filepath, '%25'); | ||
| 1516 | 1517 | // In posix, backslash is a valid character in paths: | |
| 1517 | 1518 | if (!isWindows && StringPrototypeIncludes(filepath, '\\')) | |
| 1518 | - filepath = StringPrototypeReplace(filepath, backslashRegEx, '%5C'); | ||
| 1519 | + filepath = RegExpPrototypeSymbolReplace(backslashRegEx, filepath, '%5C'); | ||
| 1519 | 1520 | if (StringPrototypeIncludes(filepath, '\n')) | |
| 1520 | - filepath = StringPrototypeReplace(filepath, newlineRegEx, '%0A'); | ||
| 1521 | + filepath = RegExpPrototypeSymbolReplace(newlineRegEx, filepath, '%0A'); | ||
| 1521 | 1522 | if (StringPrototypeIncludes(filepath, '\r')) | |
| 1522 | - filepath = StringPrototypeReplace(filepath, carriageReturnRegEx, '%0D'); | ||
| 1523 | + filepath = RegExpPrototypeSymbolReplace(carriageReturnRegEx, filepath, '%0D'); | ||
| 1523 | 1524 | if (StringPrototypeIncludes(filepath, '\t')) | |
| 1524 | - filepath = StringPrototypeReplace(filepath, tabRegEx, '%09'); | ||
| 1525 | + filepath = RegExpPrototypeSymbolReplace(tabRegEx, filepath, '%09'); | ||
| 1525 | 1526 | return filepath; | |
| 1526 | 1527 | } | |
| 1527 | 1528 | ||
| 1528 | 1529 | function pathToFileURL(filepath) { | |
| 1529 | 1530 | const outURL = new URL('file://'); | |
| 1530 | 1531 | if (isWindows && StringPrototypeStartsWith(filepath, '\\\\')) { | |
| 1531 | 1532 | // UNC path format: \\server\share\resource | |
| 1532 | - const paths = StringPrototypeSplit(filepath, '\\'); | ||
| 1533 | - if (paths.length <= 3) { | ||
| 1533 | + const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', 2); | ||
| 1534 | + if (hostnameEndIndex === -1) { | ||
| 1534 | 1535 | throw new ERR_INVALID_ARG_VALUE( | |
| 1535 | 1536 | 'filepath', | |
| 1536 | 1537 | filepath, | |
| 1537 | 1538 | 'Missing UNC resource path' | |
| 1538 | 1539 | ); | |
| 1539 | 1540 | } | |
| 1540 | - const hostname = paths[2]; | ||
| 1541 | - if (hostname.length === 0) { | ||
| 1541 | + if (hostnameEndIndex === 2) { | ||
| 1542 | 1542 | throw new ERR_INVALID_ARG_VALUE( | |
| 1543 | 1543 | 'filepath', | |
| 1544 | 1544 | filepath, | |
| 1545 | 1545 | 'Empty UNC servername' | |
| 1546 | 1546 | ); | |
| 1547 | 1547 | } | |
| 1548 | + const hostname = StringPrototypeSlice(filepath, 2, hostnameEndIndex); | ||
| 1548 | 1549 | outURL.hostname = domainToASCII(hostname); | |
| 1549 | 1550 | outURL.pathname = encodePathChars( | |
| 1550 | - ArrayPrototypeJoin(ArrayPrototypeSlice(paths, 3), '/')); | ||
| 1551 | + RegExpPrototypeSymbolReplace(backslashRegEx, StringPrototypeSlice(filepath, hostnameEndIndex), '/')); | ||
| 1551 | 1552 | } else { | |
| 1552 | 1553 | let resolved = path.resolve(filepath); | |
| 1553 | 1554 | // path.resolve strips trailing slashes so we must add them back | |
| Back | FazBrowse Home | New Git URL |
0 commit comments