diff options
Diffstat (limited to 'lib/internal/modules/cjs/loader.js')
-rw-r--r-- | lib/internal/modules/cjs/loader.js | 97 |
1 files changed, 53 insertions, 44 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 9728cd2cb9..671006f9a9 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -22,12 +22,21 @@ 'use strict'; const { - JSON, - Object, - ObjectPrototype, - Reflect, + JSONParse, + ObjectCreate, + ObjectDefineProperty, + ObjectFreeze, + ObjectGetOwnPropertyDescriptor, + ObjectGetPrototypeOf, + ObjectKeys, + ObjectPrototypeHasOwnProperty, + ObjectSetPrototypeOf, + ReflectSet, SafeMap, - StringPrototype, + StringPrototypeIndexOf, + StringPrototypeMatch, + StringPrototypeSlice, + StringPrototypeStartsWith, } = primordials; const { NativeModule } = require('internal/bootstrap/loaders'); @@ -93,7 +102,7 @@ const { const isWindows = process.platform === 'win32'; -const relativeResolveCache = Object.create(null); +const relativeResolveCache = ObjectCreate(null); let requireDepth = 0; let statCache = null; @@ -155,12 +164,12 @@ for (const [id, mod] of NativeModule.map) { } } -Object.freeze(builtinModules); +ObjectFreeze(builtinModules); Module.builtinModules = builtinModules; -Module._cache = Object.create(null); -Module._pathCache = Object.create(null); -Module._extensions = Object.create(null); +Module._cache = ObjectCreate(null); +Module._pathCache = ObjectCreate(null); +Module._extensions = ObjectCreate(null); let modulePaths = []; Module.globalPaths = []; @@ -179,16 +188,16 @@ const wrapper = [ let wrapperProxy = new Proxy(wrapper, { set(target, property, value, receiver) { patched = true; - return Reflect.set(target, property, value, receiver); + return ReflectSet(target, property, value, receiver); }, defineProperty(target, property, descriptor) { patched = true; - return Object.defineProperty(target, property, descriptor); + return ObjectDefineProperty(target, property, descriptor); } }); -Object.defineProperty(Module, 'wrap', { +ObjectDefineProperty(Module, 'wrap', { get() { return wrap; }, @@ -199,7 +208,7 @@ Object.defineProperty(Module, 'wrap', { } }); -Object.defineProperty(Module, 'wrapper', { +ObjectDefineProperty(Module, 'wrapper', { get() { return wrapperProxy; }, @@ -244,7 +253,7 @@ function readPackage(requestPath) { } try { - const parsed = JSON.parse(json); + const parsed = JSONParse(json); const filtered = { name: parsed.name, main: parsed.main, @@ -406,7 +415,7 @@ function resolveBasePath(basePath, exts, isMain, trailingSlash, request) { if (!filename) { // Try it with each of the extensions if (exts === undefined) - exts = Object.keys(Module._extensions); + exts = ObjectKeys(Module._extensions); filename = tryExtensions(basePath, exts, isMain); } } @@ -414,7 +423,7 @@ function resolveBasePath(basePath, exts, isMain, trailingSlash, request) { if (!filename && rc === 1) { // Directory. // try it with each of the extensions at "index" if (exts === undefined) - exts = Object.keys(Module._extensions); + exts = ObjectKeys(Module._extensions); filename = tryPackage(basePath, exts, isMain, request); } @@ -433,14 +442,14 @@ function trySelf(paths, exts, isMain, trailingSlash, request) { let expansion; if (request === pkg.name) { expansion = ''; - } else if (StringPrototype.startsWith(request, `${pkg.name}/`)) { - expansion = StringPrototype.slice(request, pkg.name.length); + } else if (StringPrototypeStartsWith(request, `${pkg.name}/`)) { + expansion = StringPrototypeSlice(request, pkg.name.length); } else { return false; } if (exts === undefined) - exts = Object.keys(Module._extensions); + exts = ObjectKeys(Module._extensions); if (expansion) { // Use exports @@ -461,7 +470,7 @@ function isConditionalDotExportSugar(exports, basePath) { return false; let isConditional = false; let firstCheck = true; - for (const key of Object.keys(exports)) { + for (const key of ObjectKeys(exports)) { const curIsConditional = key[0] !== '.'; if (firstCheck) { firstCheck = false; @@ -487,7 +496,7 @@ function applyExports(basePath, expansion) { pkgExports = { '.': pkgExports }; if (typeof pkgExports === 'object') { - if (ObjectPrototype.hasOwnProperty(pkgExports, mappingKey)) { + if (ObjectPrototypeHasOwnProperty(pkgExports, mappingKey)) { const mapping = pkgExports[mappingKey]; return resolveExportsTarget(pathToFileURL(basePath + '/'), mapping, '', basePath, mappingKey); @@ -498,17 +507,17 @@ function applyExports(basePath, expansion) { return basePath; let dirMatch = ''; - for (const candidateKey of Object.keys(pkgExports)) { + for (const candidateKey of ObjectKeys(pkgExports)) { if (candidateKey[candidateKey.length - 1] !== '/') continue; if (candidateKey.length > dirMatch.length && - StringPrototype.startsWith(mappingKey, candidateKey)) { + StringPrototypeStartsWith(mappingKey, candidateKey)) { dirMatch = candidateKey; } } if (dirMatch !== '') { const mapping = pkgExports[dirMatch]; - const subpath = StringPrototype.slice(mappingKey, dirMatch.length); + const subpath = StringPrototypeSlice(mappingKey, dirMatch.length); return resolveExportsTarget(pathToFileURL(basePath + '/'), mapping, subpath, basePath, mappingKey); } @@ -532,7 +541,7 @@ function resolveExports(nmPath, request, absoluteRequest) { // The implementation's behavior is meant to mirror resolution in ESM. if (!absoluteRequest) { const [, name, expansion = ''] = - StringPrototype.match(request, EXPORTS_PATTERN) || []; + StringPrototypeMatch(request, EXPORTS_PATTERN) || []; if (!name) { return path.resolve(nmPath, request); } @@ -551,14 +560,14 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { const resolvedTarget = new URL(target, pkgPath); const pkgPathPath = pkgPath.pathname; const resolvedTargetPath = resolvedTarget.pathname; - if (StringPrototype.startsWith(resolvedTargetPath, pkgPathPath) && - StringPrototype.indexOf(resolvedTargetPath, '/node_modules/', - pkgPathPath.length - 1) === -1) { + if (StringPrototypeStartsWith(resolvedTargetPath, pkgPathPath) && + StringPrototypeIndexOf(resolvedTargetPath, '/node_modules/', + pkgPathPath.length - 1) === -1) { const resolved = new URL(subpath, resolvedTarget); const resolvedPath = resolved.pathname; - if (StringPrototype.startsWith(resolvedPath, resolvedTargetPath) && - StringPrototype.indexOf(resolvedPath, '/node_modules/', - pkgPathPath.length - 1) === -1) { + if (StringPrototypeStartsWith(resolvedPath, resolvedTargetPath) && + StringPrototypeIndexOf(resolvedPath, '/node_modules/', + pkgPathPath.length - 1) === -1) { return fileURLToPath(resolved); } } @@ -575,7 +584,7 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { } } else if (typeof target === 'object' && target !== null) { if (experimentalConditionalExports && - ObjectPrototype.hasOwnProperty(target, 'require')) { + ObjectPrototypeHasOwnProperty(target, 'require')) { try { return resolveExportsTarget(pkgPath, target.require, subpath, basePath, mappingKey); @@ -584,7 +593,7 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { } } if (experimentalConditionalExports && - ObjectPrototype.hasOwnProperty(target, 'node')) { + ObjectPrototypeHasOwnProperty(target, 'node')) { try { return resolveExportsTarget(pkgPath, target.node, subpath, basePath, mappingKey); @@ -592,7 +601,7 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { if (e.code !== 'MODULE_NOT_FOUND') throw e; } } - if (ObjectPrototype.hasOwnProperty(target, 'default')) { + if (ObjectPrototypeHasOwnProperty(target, 'default')) { try { return resolveExportsTarget(pkgPath, target.default, subpath, basePath, mappingKey); @@ -670,7 +679,7 @@ Module._findPath = function(request, paths, isMain) { if (!filename) { // Try it with each of the extensions if (exts === undefined) - exts = Object.keys(Module._extensions); + exts = ObjectKeys(Module._extensions); filename = tryExtensions(basePath, exts, isMain); } } @@ -678,7 +687,7 @@ Module._findPath = function(request, paths, isMain) { if (!filename && rc === 1) { // Directory. // try it with each of the extensions at "index" if (exts === undefined) - exts = Object.keys(Module._extensions); + exts = ObjectKeys(Module._extensions); filename = tryPackage(basePath, exts, isMain, request); } @@ -838,8 +847,8 @@ const CircularRequirePrototypeWarningProxy = new Proxy({}, { }, getOwnPropertyDescriptor(target, prop) { - if (ObjectPrototype.hasOwnProperty(target, prop)) - return Object.getOwnPropertyDescriptor(target, prop); + if (ObjectPrototypeHasOwnProperty(target, prop)) + return ObjectGetOwnPropertyDescriptor(target, prop); emitCircularRequireWarning(prop); return undefined; } @@ -851,14 +860,14 @@ const PublicObjectPrototype = global.Object.prototype; function getExportsForCircularRequire(module) { if (module.exports && - Object.getPrototypeOf(module.exports) === PublicObjectPrototype && + ObjectGetPrototypeOf(module.exports) === PublicObjectPrototype && // Exclude transpiled ES6 modules / TypeScript code because those may // employ unusual patterns for accessing 'module.exports'. That should be // okay because ES6 modules have a different approach to circular // dependencies anyway. !module.exports.__esModule) { // This is later unset once the module is done loading. - Object.setPrototypeOf(module.exports, CircularRequirePrototypeWarningProxy); + ObjectSetPrototypeOf(module.exports, CircularRequirePrototypeWarningProxy); } return module.exports; @@ -941,9 +950,9 @@ Module._load = function(request, parent, isMain) { delete relativeResolveCache[relResolveCacheIdentifier]; } } else if (module.exports && - Object.getPrototypeOf(module.exports) === + ObjectGetPrototypeOf(module.exports) === CircularRequirePrototypeWarningProxy) { - Object.setPrototypeOf(module.exports, PublicObjectPrototype); + ObjectSetPrototypeOf(module.exports, PublicObjectPrototype); } } @@ -1225,7 +1234,7 @@ Module._extensions['.json'] = function(module, filename) { } try { - module.exports = JSON.parse(stripBOM(content)); + module.exports = JSONParse(stripBOM(content)); } catch (err) { err.message = filename + ': ' + err.message; throw err; |