Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ function loadNativeModule(filename, request) {
// 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) {
function makeRequireFunction(mod, redirects, id) {
const Module = mod.constructor;

let require;
if (redirects) {
const id = mod.filename || mod.id;
id = id || mod.filename || mod.id;
const conditions = cjsConditions;
const { resolve, reaction } = redirects;
require = function require(specifier) {
Expand Down
21 changes: 12 additions & 9 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const cjsParseCache = new SafeWeakMap();
// Set first due to cycle with ESM loader functions.
module.exports = {
wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache,
ModuleCompile,
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; }
};

Expand Down Expand Up @@ -849,7 +850,6 @@ Module._resolveFilename = function(request, parent, isMain, options) {
StringPrototypeStartsWith(request, '../') ||
((isWindows && StringPrototypeStartsWith(request, '.\\')) ||
StringPrototypeStartsWith(request, '..\\'));

if (isRelative) {
paths = options.paths;
} else {
Expand Down Expand Up @@ -899,6 +899,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
// Try module self resoultion first
const parentPath = trySelfParentPath(parent);
const selfResolved = trySelf(parentPath, request);

if (selfResolved) {
const cacheKey = request + '\x00' +
(paths.length === 1 ? paths[0] : ArrayPrototypeJoin(paths, '\x00'));
Expand All @@ -907,7 +908,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
}

// Look up the filename first, since that's the cache key.
const filename = Module._findPath(request, paths, isMain, false);
const filename = Module._findPath(request, paths, isMain);
if (filename) return filename;
const requireStack = [];
for (let cursor = parent;
Expand Down Expand Up @@ -1058,17 +1059,17 @@ function wrapSafe(filename, content, cjsModuleInstance) {
// the correct helper variables (require, module, exports) to
// the file.
// Returns exception, if any.
Module.prototype._compile = function(content, filename) {
function ModuleCompile(module, content, filename, wrapping) {
let moduleURL;
let redirects;
if (policy?.manifest) {
if (!wrapping && policy?.manifest) {
moduleURL = pathToFileURL(filename);
redirects = policy.manifest.getDependencyMapper(moduleURL);
policy.manifest.assertIntegrity(moduleURL, content);
}

maybeCacheSourceMap(filename, content, this);
const compiledWrapper = wrapSafe(filename, content, this);
maybeCacheSourceMap(filename, content, module);
const compiledWrapper = wrapSafe(filename, content, module);

let inspectorWrapper = null;
if (getOptionValue('--inspect-brk') && process._eval == null) {
Expand All @@ -1094,11 +1095,10 @@ Module.prototype._compile = function(content, filename) {
}
}
const dirname = path.dirname(filename);
const require = makeRequireFunction(this, redirects);
const require = makeRequireFunction(module, redirects, wrapping);
let result;
const exports = this.exports;
const exports = module.exports;
const thisValue = exports;
const module = this;
if (requireDepth === 0) statCache = new SafeMap();
if (inspectorWrapper) {
result = inspectorWrapper(compiledWrapper, thisValue, exports,
Expand All @@ -1110,6 +1110,9 @@ Module.prototype._compile = function(content, filename) {
hasLoadedAnyUserCJSModule = true;
if (requireDepth === 0) statCache = null;
return result;
}
Module.prototype._compile = function(contents, filename) {
return ModuleCompile(this, contents, filename, null);
};

// Native extension for .js
Expand Down
35 changes: 23 additions & 12 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ function evalModule(source, print) {
}

function evalScript(name, body, breakFirstLine, print) {
const CJSModule = require('internal/modules/cjs/loader').Module;
const {
Module: CJSModule,
ModuleCompile
} = require('internal/modules/cjs/loader');
const { kVmBreakFirstLineSymbol } = require('internal/util');
const { pathToFileURL } = require('url');

Expand All @@ -62,7 +65,12 @@ function evalScript(name, body, breakFirstLine, print) {
module.paths = CJSModule._nodeModulePaths(cwd);

const asyncESM = require('internal/process/esm_loader');
const baseUrl = pathToFileURL(module.filename).href;
const baseHREF = pathToFileURL(module.filename).href;
const { getOptionValue } = require('internal/options');
const policy = getOptionValue('--experimental-policy') ?
require('internal/process/policy') :
null;
policy?.assertIntegrity(baseHREF, body);

// Create wrapper for cache entry
const script = `
Expand All @@ -73,16 +81,19 @@ function evalScript(name, body, breakFirstLine, print) {
return (main) => main();
`;
globalThis.__filename = name;
const result = module._compile(script, `${name}-wrapper`)(() =>
require('vm').runInThisContext(body, {
filename: name,
displayErrors: true,
[kVmBreakFirstLineSymbol]: !!breakFirstLine,
async importModuleDynamically(specifier) {
const loader = await asyncESM.ESMLoader;
return loader.import(specifier, baseUrl);
}
}));
const result = ModuleCompile(module, script, `${name}-wrapper`, baseHREF)(
() => {
return require('vm').runInThisContext(body, {
filename: name,
displayErrors: true,
[kVmBreakFirstLineSymbol]: !!breakFirstLine,
async importModuleDynamically(specifier) {
const loader = await asyncESM.ESMLoader;
return loader.import(specifier, baseHREF);
}
});
}
);
if (print) {
const { log } = require('internal/console/global');
log(result);
Expand Down
1 change: 1 addition & 0 deletions test/message/assert_throws_stack.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
at *
at *
at *
at *
at * {
generatedMessage: true,
code: 'ERR_ASSERTION',
Expand Down
1 change: 1 addition & 0 deletions test/message/console.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Trace: foo
at *
at *
at *
at *
1 change: 1 addition & 0 deletions test/message/core_line_numbers.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ RangeError: Invalid input
at error (node:punycode:42:8)
at Object.decode (node:punycode:*:*)
at Object.<anonymous> (*test*message*core_line_numbers.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand Down
1 change: 1 addition & 0 deletions test/message/error_exit.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
1 !== 2

at Object.<anonymous> (*test*message*error_exit.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand Down
Binary file modified test/message/error_with_nul.out
Binary file not shown.
3 changes: 2 additions & 1 deletion test/message/events_unhandled_error_common_trace.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Error: foo:bar
at bar (*events_unhandled_error_common_trace.js:*:*)
at foo (*events_unhandled_error_common_trace.js:*:*)
at Object.<anonymous> (*events_unhandled_error_common_trace.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand All @@ -15,6 +16,6 @@ Error: foo:bar
Emitted 'error' event at:
at quux (*events_unhandled_error_common_trace.js:*:*)
at Object.<anonymous> (*events_unhandled_error_common_trace.js:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
[... lines matching original stack trace ...]
at node:internal/main/run_main_module:*:*
1 change: 1 addition & 0 deletions test/message/events_unhandled_error_nexttick.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node:events:*

Error
at Object.<anonymous> (*events_unhandled_error_nexttick.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand Down
3 changes: 2 additions & 1 deletion test/message/events_unhandled_error_sameline.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node:events:*

Error
at Object.<anonymous> (*events_unhandled_error_sameline.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand All @@ -12,6 +13,6 @@ Error
at node:internal/main/run_main_module:*:*
Emitted 'error' event at:
at Object.<anonymous> (*events_unhandled_error_sameline.js:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
[... lines matching original stack trace ...]
at node:internal/main/run_main_module:*:*
3 changes: 2 additions & 1 deletion test/message/events_unhandled_error_subclass.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node:events:*

Error
at Object.<anonymous> (*events_unhandled_error_subclass.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand All @@ -12,6 +13,6 @@ Error
at node:internal/main/run_main_module:*:*
Emitted 'error' event on Foo instance at:
at Object.<anonymous> (*events_unhandled_error_subclass.js:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
[... lines matching original stack trace ...]
at node:internal/main/run_main_module:*:*
8 changes: 4 additions & 4 deletions test/message/if-error-has-good-stack.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
at b (*if-error-has-good-stack.js:*:*)
at a (*if-error-has-good-stack.js:*:*)
at Object.<anonymous> (*if-error-has-good-stack.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
at Function.Module._load (node:internal/modules/cjs/loader:*:*)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*:*)
at node:internal/main/run_main_module:*:* {
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*:*) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: Error: test error
at c (*if-error-has-good-stack.js:*:*)
at b (*if-error-has-good-stack.js:*:*)
at a (*if-error-has-good-stack.js:*:*)
at Object.<anonymous> (*if-error-has-good-stack.js:*:*)
at ModuleCompile (node:internal/modules/cjs/loader:*:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
at Function.Module._load (node:internal/modules/cjs/loader:*:*)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*:*)
at node:internal/main/run_main_module:*:*
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*:*),
expected: null,
operator: 'ifError'
}
1 change: 1 addition & 0 deletions test/message/internal_assert.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Please open an issue with this stack trace at https://github.com/nodejs/node/iss
at *
at *
at *
at *
at * {
code: 'ERR_INTERNAL_ASSERTION'
}
1 change: 1 addition & 0 deletions test/message/internal_assert_fail.out
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Please open an issue with this stack trace at https://github.com/nodejs/node/iss
at *
at *
at *
at *
at * {
code: 'ERR_INTERNAL_ASSERTION'
}
1 change: 1 addition & 0 deletions test/message/promise_always_throw_unhandled.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Error: One
at *
at *
at *
at *
1 change: 1 addition & 0 deletions test/message/promise_unhandled_warn_with_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
at *
at *
at *
at *
(Use `* --trace-warnings ...` to show where the warning was created)
*UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2 changes: 1 addition & 1 deletion test/message/source_map_enclosing_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Error: an error!
-> (*enclosing-call-site.js:2:3)
at Object.<anonymous> (*enclosing-call-site-min.js:1:199)
-> (*enclosing-call-site.js:24:3)
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*)
at Module.load (node:internal/modules/cjs/loader:*)
at Function.Module._load (node:internal/modules/cjs/loader:*)
at Module.require (node:internal/modules/cjs/loader:*)
2 changes: 1 addition & 1 deletion test/message/source_map_reference_error_tabs.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ ReferenceError: alert is not defined
-> *tabs.coffee:26:2*
at Object.<anonymous> (*tabs.coffee:53:4)
-> *tabs.coffee:1:14*
at ModuleCompile (node:internal/modules/cjs/loader:*
at Module._compile (node:internal/modules/cjs/loader:*
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*
at Module.load (node:internal/modules/cjs/loader:*
at Function.Module._load (node:internal/modules/cjs/loader:*
at Module.require (node:internal/modules/cjs/loader:*
at require (node:internal/modules/cjs/helpers:*
at Object.<anonymous> (*source_map_reference_error_tabs.js:*
at Module._compile (node:internal/modules/cjs/loader:*
2 changes: 1 addition & 1 deletion test/message/source_map_throw_catch.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Error: an exception
-> *typescript-throw.ts:18:11*
at Object.<anonymous> (*typescript-throw.js:26:1)
-> *typescript-throw.ts:24:1*
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*)
at Module.load (node:internal/modules/cjs/loader:*)
at Function.Module._load (node:internal/modules/cjs/loader:*)
at Module.require (node:internal/modules/cjs/loader:*)
at require (node:internal/modules/cjs/helpers:*)
at Object.<anonymous> (*source_map_throw_catch.js:6:3)
at Module._compile (node:internal/modules/cjs/loader:*)
2 changes: 1 addition & 1 deletion test/message/source_map_throw_first_tick.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Error: an exception
-> *typescript-throw.ts:18:11*
at Object.<anonymous> (*typescript-throw.js:26:1)
-> *typescript-throw.ts:24:1*
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*)
at Module.load (node:internal/modules/cjs/loader:*)
at Function.Module._load (node:internal/modules/cjs/loader:*)
at Module.require (node:internal/modules/cjs/loader:*)
at require (node:internal/modules/cjs/helpers:*)
at Object.<anonymous> (*source_map_throw_first_tick.js:5:1)
at Module._compile (node:internal/modules/cjs/loader:*)
2 changes: 1 addition & 1 deletion test/message/source_map_throw_icu.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Error: an error
-> *icu.jsx:3:23*
at Object.<anonymous> (*icu.js:8:82)
-> *icu.jsx:9:5*
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*
at Module.load (node:internal/modules/cjs/loader:*
at Function.Module._load (node:internal/modules/cjs/loader:*
at Module.require (node:internal/modules/cjs/loader:*
at require (node:internal/modules/cjs/helpers:*
at Object.<anonymous> (*source_map_throw_icu.js:*
at Module._compile (node:internal/modules/cjs/loader:*
1 change: 1 addition & 0 deletions test/message/throw_error_with_getter_throw_traced.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ throw { // eslint-disable-line no-throw-literal
[object Object]
Thrown at:
at *throw_error_with_getter_throw_traced.js:*:*
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand Down
1 change: 1 addition & 0 deletions test/message/throw_null_traced.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ throw null;
null
Thrown at:
at *throw_null_traced.js:*:*
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand Down
1 change: 1 addition & 0 deletions test/message/throw_undefined_traced.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ throw undefined;
undefined
Thrown at:
at *throw_undefined_traced.js:*:*
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*:*)
at Module._extensions..js (node:internal/modules/cjs/loader:*:*)
at Module.load (node:internal/modules/cjs/loader:*:*)
Expand Down
2 changes: 1 addition & 1 deletion test/message/undefined_reference_in_new_context.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ ReferenceError: foo is not defined
at Script.runInNewContext (node:vm:*)
at Object.runInNewContext (node:vm:*)
at Object.<anonymous> (*test*message*undefined_reference_in_new_context.js:*)
at ModuleCompile (node:internal/modules/cjs/loader:*)
at Module._compile (node:internal/modules/cjs/loader:*)
at *..js (node:internal/modules/cjs/loader:*)
at Module.load (node:internal/modules/cjs/loader:*)
at Function.Module._load (node:internal/modules/cjs/loader:*:*)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*:*)
Loading