GeezerVison is showing github.com — sea: allow requiring core modules with the "node:" prefix · nodejs/node@b8634ee
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/realm.js +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,8 +63,8 @@ const { | 63 | 63 | SafeMap, | 64 | 64 | SafeSet, | 65 | 65 | String, | 66 | | - StringPrototypeStartsWith, | 67 | 66 | StringPrototypeSlice, | | 67 | + StringPrototypeStartsWith, | 68 | 68 | TypeError, | 69 | 69 | } = primordials; | 70 | 70 |
| @@ -299,6 +299,20 @@ class BuiltinModule { | 299 | 299 | return ArrayFrom(canBeRequiredByUsersWithoutSchemeList); | 300 | 300 | } | 301 | 301 |
| | 302 | + static normalizeRequirableId(id) { | | 303 | + let normalizedId = id; | | 304 | + if (StringPrototypeStartsWith(id, 'node:')) { | | 305 | + normalizedId = StringPrototypeSlice(id, 5); | | 306 | + } | | 307 | + | | 308 | + if (!BuiltinModule.canBeRequiredByUsers(normalizedId) || | | 309 | + (id === normalizedId && !BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) { | | 310 | + return undefined; | | 311 | + } | | 312 | + | | 313 | + return normalizedId; | | 314 | + } | | 315 | + | 302 | 316 | static getSchemeOnlyModuleNames() { | 303 | 317 | return ArrayFrom(schemelessBlockList); | 304 | 318 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/mksnapshot.js +3-10Lines changed: 3 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,12 +7,10 @@ const { | 7 | 7 | ObjectSetPrototypeOf, | 8 | 8 | SafeArrayIterator, | 9 | 9 | SafeSet, | 10 | | - StringPrototypeStartsWith, | 11 | | - StringPrototypeSlice, | 12 | 10 | } = primordials; | 13 | 11 |
| 14 | 12 | const binding = internalBinding('mksnapshot'); | 15 | | -const { BuiltinModule } = require('internal/bootstrap/realm'); | | 13 | +const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm'); | 16 | 14 | const { | 17 | 15 | compileSerializeMain, | 18 | 16 | } = binding; | @@ -97,13 +95,8 @@ function supportedInUserSnapshot(id) { | 97 | 95 | } | 98 | 96 |
| 99 | 97 | function requireForUserSnapshot(id) { | 100 | | - let normalizedId = id; | 101 | | - if (StringPrototypeStartsWith(id, 'node:')) { | 102 | | - normalizedId = StringPrototypeSlice(id, 5); | 103 | | - } | 104 | | - if (!BuiltinModule.canBeRequiredByUsers(normalizedId) || | 105 | | - (id !== normalizedId && | 106 | | - !BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) { | | 98 | + const normalizedId = normalizeRequirableId(id); | | 99 | + if (!normalizedId) { | 107 | 100 | // eslint-disable-next-line no-restricted-syntax | 108 | 101 | const err = new Error( | 109 | 102 | `Cannot find module '${id}'. `, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/single_executable_application.js +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,7 @@ const { getSingleExecutableCode } = internalBinding('sea'); | 7 | 7 | const { emitExperimentalWarning } = require('internal/util'); | 8 | 8 | const { Module, wrapSafe } = require('internal/modules/cjs/loader'); | 9 | 9 | const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors'); | | 10 | +const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm'); | 10 | 11 |
| 11 | 12 | prepareMainThreadExecution(false, true); | 12 | 13 | markBootstrapComplete(); | @@ -33,12 +34,13 @@ customModule.paths = Module._nodeModulePaths(customModule.path); | 33 | 34 |
| 34 | 35 | const customExports = customModule.exports; | 35 | 36 |
| 36 | | -function customRequire(path) { | 37 | | - if (!Module.isBuiltin(path)) { | 38 | | - throw new ERR_UNKNOWN_BUILTIN_MODULE(path); | | 37 | +function customRequire(id) { | | 38 | + const normalizedId = normalizeRequirableId(id); | | 39 | + if (!normalizedId) { | | 40 | + throw new ERR_UNKNOWN_BUILTIN_MODULE(id); | 39 | 41 | } | 40 | 42 |
| 41 | | - return require(path); | | 43 | + return require(normalizedId); | 42 | 44 | } | 43 | 45 |
| 44 | 46 | customRequire.main = customModule; | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/sea.js +16-1Lines changed: 16 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,8 +9,23 @@ expectWarning('ExperimentalWarning', | 9 | 9 | 'Single executable application is an experimental feature and ' + | 10 | 10 | 'might change at any time'); | 11 | 11 |
| | 12 | +// Should be possible to require core modules that optionally require the | | 13 | +// "node:" scheme. | 12 | 14 | const { deepStrictEqual, strictEqual, throws } = require('assert'); | 13 | | -const { dirname } = require('path'); | | 15 | +const { dirname } = require('node:path'); | | 16 | + | | 17 | +// Should be possible to require a core module that requires using the "node:" | | 18 | +// scheme. | | 19 | +{ | | 20 | + const { test } = require('node:test'); | | 21 | + strictEqual(typeof test, 'function'); | | 22 | +} | | 23 | + | | 24 | +// Should not be possible to require a core module without the "node:" scheme if | | 25 | +// it requires using the "node:" scheme. | | 26 | +throws(() => require('test'), { | | 27 | + code: 'ERR_UNKNOWN_BUILTIN_MODULE', | | 28 | +}); | 14 | 29 |
| 15 | 30 | deepStrictEqual(process.argv, [process.execPath, process.execPath, '-a', '--b=c', 'd']); | 16 | 31 |
| |
|