Skip to content

Commit

Permalink
lib: makeRequireFunction patch when experimental policy
Browse files Browse the repository at this point in the history
PR-URL: nodejs-private/node-private#358
Backport-PR-URL: nodejs-private/node-private#371
Reviewed-by: Bradley Farias <bradley.meck@gmail.com>
Reviewed-by: Michael Dawson <midawson@redhat.com>
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
  • Loading branch information
RafaelGSS authored and juanarbol committed Feb 15, 2023
1 parent 7cccd55 commit 0e3b796
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ function makeRequireFunction(mod, redirects) {
};
} else {
require = function require(path) {
return mod[require_private_symbol](mod, path);
// When no policy manifest, the original prototype.require is sustained
return mod.require(path);
};
}

Expand Down
35 changes: 27 additions & 8 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ function Module(id = '', parent) {
if (policy?.manifest) {
const moduleURL = pathToFileURL(id);
redirects = policy.manifest.getDependencyMapper(moduleURL);
// TODO(rafaelgss): remove the necessity of this branch
setOwnProperty(this, 'require', makeRequireFunction(this, redirects));
}
setOwnProperty(this, 'require', makeRequireFunction(this, redirects));
// Loads a module at the given file path. Returns that module's
// `exports` property.
this[require_private_symbol] = internalRequire;
}

Expand Down Expand Up @@ -1128,6 +1127,23 @@ Module.prototype.load = function(filename) {
esmLoader.cjsCache.set(this, exports);
};

// Loads a module at the given file path. Returns that module's
// `exports` property.
// Note: when using the experimental policy mechanism this function is overridden
Module.prototype.require = function(id) {
validateString(id, 'id');
if (id === '') {
throw new ERR_INVALID_ARG_VALUE('id', id,
'must be a non-empty string');
}
requireDepth++;
try {
return Module._load(id, this, /* isMain */ false);
} finally {
requireDepth--;
}
};

// Resolved path to process.argv[1] will be lazily placed here
// (needed for setting breakpoint when called with --inspect-brk)
let resolvedArgv;
Expand Down Expand Up @@ -1191,10 +1207,12 @@ function wrapSafe(filename, content, cjsModuleInstance) {
// Returns exception, if any.
Module.prototype._compile = function(content, filename) {
let moduleURL;
if (policy?.manifest) {
let redirects;
const manifest = policy?.manifest;
if (manifest) {
moduleURL = pathToFileURL(filename);
policy.manifest.getDependencyMapper(moduleURL);
policy.manifest.assertIntegrity(moduleURL, content);
redirects = manifest.getDependencyMapper(moduleURL);
manifest.assertIntegrity(moduleURL, content);
}

const compiledWrapper = wrapSafe(filename, content, this);
Expand Down Expand Up @@ -1223,17 +1241,18 @@ Module.prototype._compile = function(content, filename) {
}
}
const dirname = path.dirname(filename);
const require = makeRequireFunction(this, redirects);
let result;
const exports = this.exports;
const thisValue = exports;
const module = this;
if (requireDepth === 0) statCache = new SafeMap();
if (inspectorWrapper) {
result = inspectorWrapper(compiledWrapper, thisValue, exports,
module.require, module, filename, dirname);
require, module, filename, dirname);
} else {
result = ReflectApply(compiledWrapper, thisValue,
[exports, module.require, module, filename, dirname]);
[exports, require, module, filename, dirname]);
}
hasLoadedAnyUserCJSModule = true;
if (requireDepth === 0) statCache = null;
Expand Down

0 comments on commit 0e3b796

Please sign in to comment.