diff options
author | Bradley Farias <bradley.meck@gmail.com> | 2020-07-17 20:39:02 +0300 |
---|---|---|
committer | Bradley Farias <bradley.meck@gmail.com> | 2020-08-13 21:17:40 +0300 |
commit | e155d9601429057a34adbcdcb790a1c12bd2a163 (patch) | |
tree | 330623d1c4116c229e3b47c05c627522a4c1f27e /lib/internal/modules | |
parent | f8976a76bbb49d678056d6d2ddd57064cf9e63c2 (diff) |
policy: support conditions for redirects
PR-URL: https://github.com/nodejs/node/pull/34414
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Diffstat (limited to 'lib/internal/modules')
-rw-r--r-- | lib/internal/modules/cjs/helpers.js | 24 | ||||
-rw-r--r-- | lib/internal/modules/cjs/loader.js | 4 | ||||
-rw-r--r-- | lib/internal/modules/esm/resolve.js | 25 |
3 files changed, 45 insertions, 8 deletions
diff --git a/lib/internal/modules/cjs/helpers.js b/lib/internal/modules/cjs/helpers.js index 3d224fb5142..9b6944c5939 100644 --- a/lib/internal/modules/cjs/helpers.js +++ b/lib/internal/modules/cjs/helpers.js @@ -1,9 +1,11 @@ 'use strict'; const { + ArrayPrototypeJoin, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, SafeMap, + SafeSet, } = primordials; const { ERR_MANIFEST_DEPENDENCY_MISSING, @@ -16,10 +18,16 @@ const path = require('path'); const { pathToFileURL, fileURLToPath } = require('internal/url'); const { URL } = require('url'); +const { getOptionValue } = require('internal/options'); +const userConditions = getOptionValue('--conditions'); + let debug = require('internal/util/debuglog').debuglog('module', (fn) => { debug = fn; }); +// TODO: Use this set when resolving pkg#exports conditions in loader.js. +const cjsConditions = new SafeSet(['require', 'node', ...userConditions]); + function loadNativeModule(filename, request) { const mod = NativeModule.map.get(filename); if (mod) { @@ -38,11 +46,12 @@ function makeRequireFunction(mod, redirects) { let require; if (redirects) { - const { resolve, reaction } = redirects; const id = mod.filename || mod.id; - require = function require(path) { + const conditions = cjsConditions; + const { resolve, reaction } = redirects; + require = function require(specifier) { let missing = true; - const destination = resolve(path); + const destination = resolve(specifier, conditions); if (destination === true) { missing = false; } else if (destination) { @@ -66,9 +75,13 @@ function makeRequireFunction(mod, redirects) { } } if (missing) { - reaction(new ERR_MANIFEST_DEPENDENCY_MISSING(id, path)); + reaction(new ERR_MANIFEST_DEPENDENCY_MISSING( + id, + specifier, + ArrayPrototypeJoin([...conditions], ', ') + )); } - return mod.require(path); + return mod.require(specifier); }; } else { require = function require(path) { @@ -168,6 +181,7 @@ function normalizeReferrerURL(referrer) { module.exports = { addBuiltinLibsToObject, + cjsConditions, loadNativeModule, makeRequireFunction, normalizeReferrerURL, diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index dcad0bcf632..9aad5318d71 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -45,7 +45,6 @@ const { RegExpPrototypeTest, SafeMap, SafeWeakMap, - SafeSet, String, StringPrototypeMatch, StringPrototypeSlice, @@ -73,6 +72,7 @@ const { makeRequireFunction, normalizeReferrerURL, stripBOM, + cjsConditions, loadNativeModule } = require('internal/modules/cjs/helpers'); const { getOptionValue } = require('internal/options'); @@ -82,7 +82,6 @@ const manifest = getOptionValue('--experimental-policy') ? require('internal/process/policy').manifest : null; const { compileFunction } = internalBinding('contextify'); -const userConditions = getOptionValue('--conditions'); // Whether any user-provided CJS modules had been loaded (executed). // Used for internal assertions. @@ -817,7 +816,6 @@ Module._load = function(request, parent, isMain) { return module.exports; }; -const cjsConditions = new SafeSet(['require', 'node', ...userConditions]); Module._resolveFilename = function(request, parent, isMain, options) { if (NativeModule.canBeRequiredByUsers(request)) { return request; diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index f6879465451..04a17c908ad 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -30,6 +30,9 @@ const { Stats, } = require('fs'); const { getOptionValue } = require('internal/options'); +const manifest = getOptionValue('--experimental-policy') ? + require('internal/process/policy').manifest : + null; const { sep, relative } = require('path'); const preserveSymlinks = getOptionValue('--preserve-symlinks'); const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); @@ -41,6 +44,7 @@ const { ERR_INVALID_MODULE_SPECIFIER, ERR_INVALID_PACKAGE_CONFIG, ERR_INVALID_PACKAGE_TARGET, + ERR_MANIFEST_DEPENDENCY_MISSING, ERR_MODULE_NOT_FOUND, ERR_PACKAGE_IMPORT_NOT_DEFINED, ERR_PACKAGE_PATH_NOT_EXPORTED, @@ -710,6 +714,27 @@ function resolveAsCommonJS(specifier, parentURL) { function defaultResolve(specifier, context = {}, defaultResolveUnused) { let { parentURL, conditions } = context; + if (manifest) { + const redirects = manifest.getRedirector(parentURL); + if (redirects) { + const { resolve, reaction } = redirects; + const destination = resolve(specifier, new SafeSet(conditions)); + let missing = true; + if (destination === true) { + missing = false; + } else if (destination) { + const href = destination.href; + return { url: href }; + } + if (missing) { + reaction(new ERR_MANIFEST_DEPENDENCY_MISSING( + parentURL, + specifier, + ArrayPrototypeJoin([...conditions], ', ')) + ); + } + } + } let parsed; try { parsed = new URL(specifier); |