mirror of
https://github.com/nodejs/node.git
synced 2025-08-18 07:08:50 +02:00

Previously, the `require()` function exposed to the embedded SEA code was calling the internal `require()` function if the module name belonged to the list of public core modules but the internal `require()` function does not support loading modules with the "node:" prefix, so this change forwards the calls to another `require()` function that supports this. Fixes: https://github.com/nodejs/single-executable/issues/69 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: https://github.com/nodejs/node/pull/47779 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
'use strict';
|
|
const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors');
|
|
const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm');
|
|
const { Module, wrapSafe } = require('internal/modules/cjs/loader');
|
|
|
|
// This is roughly the same as:
|
|
//
|
|
// const mod = new Module(filename);
|
|
// mod._compile(contents, filename);
|
|
//
|
|
// but the code has been duplicated because currently there is no way to set the
|
|
// value of require.main to module.
|
|
//
|
|
// TODO(RaisinTen): Find a way to deduplicate this.
|
|
|
|
function embedderRunCjs(contents) {
|
|
const filename = process.execPath;
|
|
const compiledWrapper = wrapSafe(filename, contents);
|
|
|
|
const customModule = new Module(filename, null);
|
|
customModule.filename = filename;
|
|
customModule.paths = Module._nodeModulePaths(customModule.path);
|
|
|
|
const customExports = customModule.exports;
|
|
|
|
embedderRequire.main = customModule;
|
|
|
|
const customFilename = customModule.filename;
|
|
|
|
const customDirname = customModule.path;
|
|
|
|
return compiledWrapper(
|
|
customExports,
|
|
embedderRequire,
|
|
customModule,
|
|
customFilename,
|
|
customDirname);
|
|
}
|
|
|
|
function embedderRequire(id) {
|
|
const normalizedId = normalizeRequirableId(id);
|
|
if (!normalizedId) {
|
|
throw new ERR_UNKNOWN_BUILTIN_MODULE(id);
|
|
}
|
|
return require(normalizedId);
|
|
}
|
|
|
|
module.exports = { embedderRequire, embedderRunCjs };
|