Skip to content

Commit 0e3b796

Browse files
RafaelGSSjuanarbol
authored andcommitted
lib: makeRequireFunction patch when experimental policy
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>
1 parent 7cccd55 commit 0e3b796

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

lib/internal/modules/cjs/helpers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ function makeRequireFunction(mod, redirects) {
106106
};
107107
} else {
108108
require = function require(path) {
109-
return mod[require_private_symbol](mod, path);
109+
// When no policy manifest, the original prototype.require is sustained
110+
return mod.require(path);
110111
};
111112
}
112113

lib/internal/modules/cjs/loader.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,9 @@ function Module(id = '', parent) {
235235
if (policy?.manifest) {
236236
const moduleURL = pathToFileURL(id);
237237
redirects = policy.manifest.getDependencyMapper(moduleURL);
238+
// TODO(rafaelgss): remove the necessity of this branch
239+
setOwnProperty(this, 'require', makeRequireFunction(this, redirects));
238240
}
239-
setOwnProperty(this, 'require', makeRequireFunction(this, redirects));
240-
// Loads a module at the given file path. Returns that module's
241-
// `exports` property.
242241
this[require_private_symbol] = internalRequire;
243242
}
244243

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

1130+
// Loads a module at the given file path. Returns that module's
1131+
// `exports` property.
1132+
// Note: when using the experimental policy mechanism this function is overridden
1133+
Module.prototype.require = function(id) {
1134+
validateString(id, 'id');
1135+
if (id === '') {
1136+
throw new ERR_INVALID_ARG_VALUE('id', id,
1137+
'must be a non-empty string');
1138+
}
1139+
requireDepth++;
1140+
try {
1141+
return Module._load(id, this, /* isMain */ false);
1142+
} finally {
1143+
requireDepth--;
1144+
}
1145+
};
1146+
11311147
// Resolved path to process.argv[1] will be lazily placed here
11321148
// (needed for setting breakpoint when called with --inspect-brk)
11331149
let resolvedArgv;
@@ -1191,10 +1207,12 @@ function wrapSafe(filename, content, cjsModuleInstance) {
11911207
// Returns exception, if any.
11921208
Module.prototype._compile = function(content, filename) {
11931209
let moduleURL;
1194-
if (policy?.manifest) {
1210+
let redirects;
1211+
const manifest = policy?.manifest;
1212+
if (manifest) {
11951213
moduleURL = pathToFileURL(filename);
1196-
policy.manifest.getDependencyMapper(moduleURL);
1197-
policy.manifest.assertIntegrity(moduleURL, content);
1214+
redirects = manifest.getDependencyMapper(moduleURL);
1215+
manifest.assertIntegrity(moduleURL, content);
11981216
}
11991217

12001218
const compiledWrapper = wrapSafe(filename, content, this);
@@ -1223,17 +1241,18 @@ Module.prototype._compile = function(content, filename) {
12231241
}
12241242
}
12251243
const dirname = path.dirname(filename);
1244+
const require = makeRequireFunction(this, redirects);
12261245
let result;
12271246
const exports = this.exports;
12281247
const thisValue = exports;
12291248
const module = this;
12301249
if (requireDepth === 0) statCache = new SafeMap();
12311250
if (inspectorWrapper) {
12321251
result = inspectorWrapper(compiledWrapper, thisValue, exports,
1233-
module.require, module, filename, dirname);
1252+
require, module, filename, dirname);
12341253
} else {
12351254
result = ReflectApply(compiledWrapper, thisValue,
1236-
[exports, module.require, module, filename, dirname]);
1255+
[exports, require, module, filename, dirname]);
12371256
}
12381257
hasLoadedAnyUserCJSModule = true;
12391258
if (requireDepth === 0) statCache = null;

0 commit comments

Comments
 (0)