diff options
author | Bradley Farias <bradley.meck@gmail.com> | 2019-08-26 10:40:45 -0500 |
---|---|---|
committer | Bradley Farias <bradley.meck@gmail.com> | 2019-09-03 11:12:05 -0500 |
commit | 6ce87c027dc2a16e1b8d85c753b52270ae0c6054 (patch) | |
tree | 420c362071bd29603eb52eb1ce08da56a36853f9 /lib/internal/modules/cjs/helpers.js | |
parent | f2c573cf8bedea85ab4505c839b2332517e4b0b9 (diff) | |
download | android-node-v8-6ce87c027dc2a16e1b8d85c753b52270ae0c6054.tar.gz android-node-v8-6ce87c027dc2a16e1b8d85c753b52270ae0c6054.tar.bz2 android-node-v8-6ce87c027dc2a16e1b8d85c753b52270ae0c6054.zip |
policy: minor perf opts and cleanup
PR-URL: https://github.com/nodejs/node/pull/29322
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/modules/cjs/helpers.js')
-rw-r--r-- | lib/internal/modules/cjs/helpers.js | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/lib/internal/modules/cjs/helpers.js b/lib/internal/modules/cjs/helpers.js index 5e0e1b06ae..7a98700245 100644 --- a/lib/internal/modules/cjs/helpers.js +++ b/lib/internal/modules/cjs/helpers.js @@ -1,6 +1,6 @@ 'use strict'; -const { Object } = primordials; +const { Object, SafeMap } = primordials; const { ERR_MANIFEST_DEPENDENCY_MISSING, ERR_UNKNOWN_BUILTIN_MODULE @@ -28,34 +28,40 @@ function loadNativeModule(filename, request, experimentalModules) { // Invoke with makeRequireFunction(module) where |module| is the Module object // to use as the context for the require() function. // Use redirects to set up a mapping from a policy and restrict dependencies +const urlToFileCache = new SafeMap(); function makeRequireFunction(mod, redirects) { const Module = mod.constructor; let require; if (redirects) { - const { map, reaction } = redirects; + const { resolve, reaction } = redirects; const id = mod.filename || mod.id; require = function require(path) { let missing = true; - if (map === true) { + const destination = resolve(path); + if (destination === true) { missing = false; - } else if (map.has(path)) { - const redirect = map.get(path); - if (redirect === true) { - missing = false; - } else if (typeof redirect === 'string') { - const parsed = new URL(redirect); - if (parsed.protocol === 'node:') { - const specifier = parsed.pathname; - const mod = loadNativeModule( - specifier, - redirect, - experimentalModules); - if (mod && mod.canBeRequiredByUsers) return mod.exports; - throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); - } else if (parsed.protocol === 'file:') { - return mod.require(fileURLToPath(parsed)); + } else if (destination) { + const href = destination.href; + if (destination.protocol === 'node:') { + const specifier = destination.pathname; + const mod = loadNativeModule( + specifier, + href, + experimentalModules); + if (mod && mod.canBeRequiredByUsers) { + return mod.exports; } + throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); + } else if (destination.protocol === 'file:') { + let filepath; + if (urlToFileCache.has(href)) { + filepath = urlToFileCache.get(href); + } else { + filepath = fileURLToPath(destination); + urlToFileCache.set(href, filepath); + } + return mod.require(filepath); } } if (missing) { |