diff options
author | Darshan Sen <raisinten@gmail.com> | 2023-05-04 20:02:35 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-04 14:32:35 +0000 |
commit | c868aad15a49d4ea20a06e47eb9cdafc0c218fb4 (patch) | |
tree | 539429e72173581b53e5baf8425bf3940f640218 /lib | |
parent | b7def8ef5bfc5b098c0426ecbdc4fead5bdbb29c (diff) | |
download | node-new-c868aad15a49d4ea20a06e47eb9cdafc0c218fb4.tar.gz |
sea: allow requiring core modules with the "node:" prefix
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/bootstrap/realm.js | 14 | ||||
-rw-r--r-- | lib/internal/main/mksnapshot.js | 13 | ||||
-rw-r--r-- | lib/internal/util/embedding.js | 11 |
3 files changed, 23 insertions, 15 deletions
diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index c8cdaeff7d..9dc0aee751 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -285,6 +285,20 @@ class BuiltinModule { return canBeRequiredByUsersWithoutSchemeList.has(id); } + static normalizeRequirableId(id) { + let normalizedId = id; + if (StringPrototypeStartsWith(id, 'node:')) { + normalizedId = StringPrototypeSlice(id, 5); + } + + if (!BuiltinModule.canBeRequiredByUsers(normalizedId) || + (id === normalizedId && !BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) { + return undefined; + } + + return normalizedId; + } + static isBuiltin(id) { return BuiltinModule.canBeRequiredWithoutScheme(id) || ( typeof id === 'string' && diff --git a/lib/internal/main/mksnapshot.js b/lib/internal/main/mksnapshot.js index 376639ef91..2a9b5d9851 100644 --- a/lib/internal/main/mksnapshot.js +++ b/lib/internal/main/mksnapshot.js @@ -7,12 +7,10 @@ const { ObjectSetPrototypeOf, SafeArrayIterator, SafeSet, - StringPrototypeStartsWith, - StringPrototypeSlice, } = primordials; const binding = internalBinding('mksnapshot'); -const { BuiltinModule } = require('internal/bootstrap/realm'); +const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm'); const { getEmbedderEntryFunction, compileSerializeMain, @@ -98,13 +96,8 @@ function supportedInUserSnapshot(id) { } function requireForUserSnapshot(id) { - let normalizedId = id; - if (StringPrototypeStartsWith(id, 'node:')) { - normalizedId = StringPrototypeSlice(id, 5); - } - if (!BuiltinModule.canBeRequiredByUsers(normalizedId) || - (id !== normalizedId && - !BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) { + const normalizedId = normalizeRequirableId(id); + if (!normalizedId) { // eslint-disable-next-line no-restricted-syntax const err = new Error( `Cannot find module '${id}'. `, diff --git a/lib/internal/util/embedding.js b/lib/internal/util/embedding.js index 139d4c7a25..e2e6720247 100644 --- a/lib/internal/util/embedding.js +++ b/lib/internal/util/embedding.js @@ -1,5 +1,6 @@ '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: @@ -36,12 +37,12 @@ function embedderRunCjs(contents) { customDirname); } -function embedderRequire(path) { - if (!Module.isBuiltin(path)) { - throw new ERR_UNKNOWN_BUILTIN_MODULE(path); +function embedderRequire(id) { + const normalizedId = normalizeRequirableId(id); + if (!normalizedId) { + throw new ERR_UNKNOWN_BUILTIN_MODULE(id); } - - return require(path); + return require(normalizedId); } module.exports = { embedderRequire, embedderRunCjs }; |