|
23 | 23 |
|
24 | 24 | const { |
25 | 25 | ArrayIsArray, |
| 26 | + ArrayPrototypeConcat, |
| 27 | + ArrayPrototypeFilter, |
| 28 | + ArrayPrototypeIncludes, |
| 29 | + ArrayPrototypeIndexOf, |
26 | 30 | ArrayPrototypeJoin, |
| 31 | + ArrayPrototypePush, |
| 32 | + ArrayPrototypeSlice, |
| 33 | + ArrayPrototypeSplice, |
| 34 | + Boolean, |
27 | 35 | Error, |
28 | 36 | JSONParse, |
29 | | - Map, |
30 | 37 | ObjectCreate, |
31 | 38 | ObjectDefineProperty, |
32 | 39 | ObjectFreeze, |
@@ -36,16 +43,20 @@ const { |
36 | 43 | ObjectPrototype, |
37 | 44 | ObjectPrototypeHasOwnProperty, |
38 | 45 | ObjectSetPrototypeOf, |
| 46 | + ReflectApply, |
39 | 47 | ReflectSet, |
40 | 48 | RegExpPrototypeTest, |
41 | 49 | SafeMap, |
42 | 50 | SafeWeakMap, |
43 | 51 | String, |
| 52 | + StringPrototypeCharAt, |
| 53 | + StringPrototypeCharCodeAt, |
44 | 54 | StringPrototypeEndsWith, |
45 | 55 | StringPrototypeLastIndexOf, |
46 | 56 | StringPrototypeIndexOf, |
47 | 57 | StringPrototypeMatch, |
48 | 58 | StringPrototypeSlice, |
| 59 | + StringPrototypeSplit, |
49 | 60 | StringPrototypeStartsWith, |
50 | 61 | } = primordials; |
51 | 62 |
|
@@ -144,8 +155,8 @@ function stat(filename) { |
144 | 155 |
|
145 | 156 | function updateChildren(parent, child, scan) { |
146 | 157 | const children = parent && parent.children; |
147 | | - if (children && !(scan && children.includes(child))) |
148 | | - children.push(child); |
| 158 | + if (children && !(scan && ArrayPrototypeIncludes(children, child))) |
| 159 | + ArrayPrototypePush(children, child); |
149 | 160 | } |
150 | 161 |
|
151 | 162 | function Module(id = '', parent) { |
@@ -162,7 +173,7 @@ function Module(id = '', parent) { |
162 | 173 | const builtinModules = []; |
163 | 174 | for (const [id, mod] of NativeModule.map) { |
164 | 175 | if (mod.canBeRequiredByUsers) { |
165 | | - builtinModules.push(id); |
| 176 | + ArrayPrototypePush(builtinModules, id); |
166 | 177 | } |
167 | 178 | } |
168 | 179 |
|
@@ -331,7 +342,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) { |
331 | 342 | // In order to minimize unnecessary lstat() calls, |
332 | 343 | // this cache is a list of known-real paths. |
333 | 344 | // Set to an empty Map to reset. |
334 | | -const realpathCache = new Map(); |
| 345 | +const realpathCache = new SafeMap(); |
335 | 346 |
|
336 | 347 | // Check if the file exists and is not a directory |
337 | 348 | // if using --preserve-symlinks and isMain is false, |
@@ -371,10 +382,10 @@ function findLongestRegisteredExtension(filename) { |
371 | 382 | let currentExtension; |
372 | 383 | let index; |
373 | 384 | let startIndex = 0; |
374 | | - while ((index = name.indexOf('.', startIndex)) !== -1) { |
| 385 | + while ((index = StringPrototypeIndexOf(name, '.', startIndex)) !== -1) { |
375 | 386 | startIndex = index + 1; |
376 | 387 | if (index === 0) continue; // Skip dotfiles like .gitignore |
377 | | - currentExtension = name.slice(index); |
| 388 | + currentExtension = StringPrototypeSlice(name, index); |
378 | 389 | if (Module._extensions[currentExtension]) return currentExtension; |
379 | 390 | } |
380 | 391 | return '.js'; |
@@ -455,15 +466,15 @@ Module._findPath = function(request, paths, isMain) { |
455 | 466 | return false; |
456 | 467 | } |
457 | 468 |
|
458 | | - const cacheKey = request + '\x00' + |
459 | | - (paths.length === 1 ? paths[0] : paths.join('\x00')); |
| 469 | + const cacheKey = request + '\x00' + ArrayPrototypeJoin(paths, '\x00'); |
460 | 470 | const entry = Module._pathCache[cacheKey]; |
461 | 471 | if (entry) |
462 | 472 | return entry; |
463 | 473 |
|
464 | 474 | let exts; |
465 | 475 | let trailingSlash = request.length > 0 && |
466 | | - request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH; |
| 476 | + StringPrototypeCharCodeAt(request, request.length - 1) === |
| 477 | + CHAR_FORWARD_SLASH; |
467 | 478 | if (!trailingSlash) { |
468 | 479 | trailingSlash = RegExpPrototypeTest(trailingSlashRegex, request); |
469 | 480 | } |
@@ -546,13 +557,14 @@ if (isWindows) { |
546 | 557 |
|
547 | 558 | // return root node_modules when path is 'D:\\'. |
548 | 559 | // path.resolve will make sure from.length >=3 in Windows. |
549 | | - if (from.charCodeAt(from.length - 1) === CHAR_BACKWARD_SLASH && |
550 | | - from.charCodeAt(from.length - 2) === CHAR_COLON) |
| 560 | + if (StringPrototypeCharCodeAt(from, from.length - 1) === |
| 561 | + CHAR_BACKWARD_SLASH && |
| 562 | + StringPrototypeCharCodeAt(from, from.length - 2) === CHAR_COLON) |
551 | 563 | return [from + 'node_modules']; |
552 | 564 |
|
553 | 565 | const paths = []; |
554 | 566 | for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) { |
555 | | - const code = from.charCodeAt(i); |
| 567 | + const code = StringPrototypeCharCodeAt(from, i); |
556 | 568 | // The path segment separator check ('\' and '/') was used to get |
557 | 569 | // node_modules path for every path segment. |
558 | 570 | // Use colon as an extra condition since we can get node_modules |
@@ -562,7 +574,10 @@ if (isWindows) { |
562 | 574 | code === CHAR_FORWARD_SLASH || |
563 | 575 | code === CHAR_COLON) { |
564 | 576 | if (p !== nmLen) |
565 | | - paths.push(from.slice(0, last) + '\\node_modules'); |
| 577 | + ArrayPrototypePush( |
| 578 | + paths, |
| 579 | + StringPrototypeSlice(from, 0, last) + '\\node_modules' |
| 580 | + ); |
566 | 581 | last = i; |
567 | 582 | p = 0; |
568 | 583 | } else if (p !== -1) { |
@@ -591,10 +606,13 @@ if (isWindows) { |
591 | 606 | // that works on both Windows and Posix is non-trivial. |
592 | 607 | const paths = []; |
593 | 608 | for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) { |
594 | | - const code = from.charCodeAt(i); |
| 609 | + const code = StringPrototypeCharCodeAt(from, i); |
595 | 610 | if (code === CHAR_FORWARD_SLASH) { |
596 | 611 | if (p !== nmLen) |
597 | | - paths.push(from.slice(0, last) + '/node_modules'); |
| 612 | + ArrayPrototypePush( |
| 613 | + paths, |
| 614 | + StringPrototypeSlice(from, 0, last) + '/node_modules' |
| 615 | + ); |
598 | 616 | last = i; |
599 | 617 | p = 0; |
600 | 618 | } else if (p !== -1) { |
@@ -607,7 +625,7 @@ if (isWindows) { |
607 | 625 | } |
608 | 626 |
|
609 | 627 | // Append /node_modules to handle root paths. |
610 | | - paths.push('/node_modules'); |
| 628 | + ArrayPrototypePush(paths, '/node_modules'); |
611 | 629 |
|
612 | 630 | return paths; |
613 | 631 | }; |
@@ -620,15 +638,15 @@ Module._resolveLookupPaths = function(request, parent) { |
620 | 638 | } |
621 | 639 |
|
622 | 640 | // Check for node modules paths. |
623 | | - if (request.charAt(0) !== '.' || |
| 641 | + if (StringPrototypeCharAt(request, 0) !== '.' || |
624 | 642 | (request.length > 1 && |
625 | | - request.charAt(1) !== '.' && |
626 | | - request.charAt(1) !== '/' && |
627 | | - (!isWindows || request.charAt(1) !== '\\'))) { |
| 643 | + StringPrototypeCharAt(request, 1) !== '.' && |
| 644 | + StringPrototypeCharAt(request, 1) !== '/' && |
| 645 | + (!isWindows || StringPrototypeCharAt(request, 1) !== '\\'))) { |
628 | 646 |
|
629 | 647 | let paths = modulePaths; |
630 | 648 | if (parent != null && parent.paths && parent.paths.length) { |
631 | | - paths = parent.paths.concat(paths); |
| 649 | + paths = ArrayPrototypeConcat(parent.paths, paths); |
632 | 650 | } |
633 | 651 |
|
634 | 652 | debug('looking for %j in %j', request, paths); |
@@ -778,9 +796,9 @@ Module._load = function(request, parent, isMain) { |
778 | 796 | delete relativeResolveCache[relResolveCacheIdentifier]; |
779 | 797 | const children = parent && parent.children; |
780 | 798 | if (ArrayIsArray(children)) { |
781 | | - const index = children.indexOf(module); |
| 799 | + const index = ArrayPrototypeIndexOf(children, module); |
782 | 800 | if (index !== -1) { |
783 | | - children.splice(index, 1); |
| 801 | + ArrayPrototypeSplice(children, index, 1); |
784 | 802 | } |
785 | 803 | } |
786 | 804 | } |
@@ -804,10 +822,10 @@ Module._resolveFilename = function(request, parent, isMain, options) { |
804 | 822 |
|
805 | 823 | if (typeof options === 'object' && options !== null) { |
806 | 824 | if (ArrayIsArray(options.paths)) { |
807 | | - const isRelative = request.startsWith('./') || |
808 | | - request.startsWith('../') || |
809 | | - ((isWindows && request.startsWith('.\\')) || |
810 | | - request.startsWith('..\\')); |
| 825 | + const isRelative = StringPrototypeStartsWith(request, './') || |
| 826 | + StringPrototypeStartsWith(request, '../') || |
| 827 | + ((isWindows && StringPrototypeStartsWith(request, '.\\')) || |
| 828 | + StringPrototypeStartsWith(request, '..\\')); |
811 | 829 |
|
812 | 830 | if (isRelative) { |
813 | 831 | paths = options.paths; |
@@ -822,8 +840,8 @@ Module._resolveFilename = function(request, parent, isMain, options) { |
822 | 840 | const lookupPaths = Module._resolveLookupPaths(request, fakeParent); |
823 | 841 |
|
824 | 842 | for (let j = 0; j < lookupPaths.length; j++) { |
825 | | - if (!paths.includes(lookupPaths[j])) |
826 | | - paths.push(lookupPaths[j]); |
| 843 | + if (!ArrayPrototypeIncludes(paths, lookupPaths[j])) |
| 844 | + ArrayPrototypePush(paths, lookupPaths[j]); |
827 | 845 | } |
828 | 846 | } |
829 | 847 | } |
@@ -872,11 +890,12 @@ Module._resolveFilename = function(request, parent, isMain, options) { |
872 | 890 | for (let cursor = parent; |
873 | 891 | cursor; |
874 | 892 | cursor = cursor.parent) { |
875 | | - requireStack.push(cursor.filename || cursor.id); |
| 893 | + ArrayPrototypePush(requireStack, cursor.filename || cursor.id); |
876 | 894 | } |
877 | 895 | let message = `Cannot find module '${request}'`; |
878 | 896 | if (requireStack.length > 0) { |
879 | | - message = message + '\nRequire stack:\n- ' + requireStack.join('\n- '); |
| 897 | + message = message + '\nRequire stack:\n- ' + |
| 898 | + ArrayPrototypeJoin(requireStack, '\n- '); |
880 | 899 | } |
881 | 900 | // eslint-disable-next-line no-restricted-syntax |
882 | 901 | const err = new Error(message); |
@@ -887,7 +906,7 @@ Module._resolveFilename = function(request, parent, isMain, options) { |
887 | 906 |
|
888 | 907 | function finalizeEsmResolution(match, request, parentPath, pkgPath) { |
889 | 908 | const { resolved, exact } = match; |
890 | | - if (StringPrototypeMatch(resolved, encodedSepRegEx)) |
| 909 | + if (RegExpPrototypeTest(encodedSepRegEx, resolved)) |
891 | 910 | throw new ERR_INVALID_MODULE_SPECIFIER( |
892 | 911 | resolved, 'must not include encoded "/" or "\\" characters', parentPath); |
893 | 912 | const filename = fileURLToPath(resolved); |
@@ -924,9 +943,9 @@ Module.prototype.load = function(filename) { |
924 | 943 |
|
925 | 944 | const extension = findLongestRegisteredExtension(filename); |
926 | 945 | // allow .mjs to be overridden |
927 | | - if (filename.endsWith('.mjs') && !Module._extensions['.mjs']) { |
| 946 | + if (StringPrototypeEndsWith(filename, '.mjs') && !Module._extensions['.mjs']) |
928 | 947 | throw new ERR_REQUIRE_ESM(filename); |
929 | | - } |
| 948 | + |
930 | 949 | Module._extensions[extension](this, filename); |
931 | 950 | this.loaded = true; |
932 | 951 |
|
@@ -1057,13 +1076,13 @@ Module.prototype._compile = function(content, filename) { |
1057 | 1076 | const exports = this.exports; |
1058 | 1077 | const thisValue = exports; |
1059 | 1078 | const module = this; |
1060 | | - if (requireDepth === 0) statCache = new Map(); |
| 1079 | + if (requireDepth === 0) statCache = new SafeMap(); |
1061 | 1080 | if (inspectorWrapper) { |
1062 | 1081 | result = inspectorWrapper(compiledWrapper, thisValue, exports, |
1063 | 1082 | require, module, filename, dirname); |
1064 | 1083 | } else { |
1065 | | - result = compiledWrapper.call(thisValue, exports, require, module, |
1066 | | - filename, dirname); |
| 1084 | + result = ReflectApply(compiledWrapper, thisValue, |
| 1085 | + [exports, require, module, filename, dirname]); |
1067 | 1086 | } |
1068 | 1087 | hasLoadedAnyUserCJSModule = true; |
1069 | 1088 | if (requireDepth === 0) statCache = null; |
@@ -1072,7 +1091,7 @@ Module.prototype._compile = function(content, filename) { |
1072 | 1091 |
|
1073 | 1092 | // Native extension for .js |
1074 | 1093 | Module._extensions['.js'] = function(module, filename) { |
1075 | | - if (filename.endsWith('.js')) { |
| 1094 | + if (StringPrototypeEndsWith(filename, '.js')) { |
1076 | 1095 | const pkg = readPackageScope(filename); |
1077 | 1096 | // Function require shouldn't be used in ES modules. |
1078 | 1097 | if (pkg && pkg.data && pkg.data.type === 'module') { |
@@ -1127,7 +1146,8 @@ Module._extensions['.node'] = function(module, filename) { |
1127 | 1146 | function createRequireFromPath(filename) { |
1128 | 1147 | // Allow a directory to be passed as the filename |
1129 | 1148 | const trailingSlash = |
1130 | | - filename.endsWith('/') || (isWindows && filename.endsWith('\\')); |
| 1149 | + StringPrototypeEndsWith(filename, '/') || |
| 1150 | + (isWindows && StringPrototypeEndsWith(filename, '\\')); |
1131 | 1151 |
|
1132 | 1152 | const proxyPath = trailingSlash ? |
1133 | 1153 | path.join(filename, 'noop.js') : |
@@ -1189,15 +1209,16 @@ Module._initPaths = function() { |
1189 | 1209 | } |
1190 | 1210 |
|
1191 | 1211 | if (nodePath) { |
1192 | | - paths = nodePath.split(path.delimiter).filter(function pathsFilterCB(path) { |
1193 | | - return !!path; |
1194 | | - }).concat(paths); |
| 1212 | + paths = ArrayPrototypeConcat(ArrayPrototypeFilter( |
| 1213 | + StringPrototypeSplit(nodePath, path.delimiter), |
| 1214 | + Boolean |
| 1215 | + ), paths); |
1195 | 1216 | } |
1196 | 1217 |
|
1197 | 1218 | modulePaths = paths; |
1198 | 1219 |
|
1199 | 1220 | // Clone as a shallow copy, for introspection. |
1200 | | - Module.globalPaths = modulePaths.slice(0); |
| 1221 | + Module.globalPaths = ArrayPrototypeSlice(modulePaths); |
1201 | 1222 | }; |
1202 | 1223 |
|
1203 | 1224 | Module._preloadModules = function(requests) { |
|