GeezerVison is showing github.com — sea: allow requiring core modules with the "node:" prefix · nodejs/node@1a7fc18
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 +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -285,6 +285,20 @@ class BuiltinModule { | 285 | 285 | return canBeRequiredByUsersWithoutSchemeList.has(id); | 286 | 286 | } | 287 | 287 |
| | 288 | + static normalizeRequirableId(id) { | | 289 | + let normalizedId = id; | | 290 | + if (StringPrototypeStartsWith(id, 'node:')) { | | 291 | + normalizedId = StringPrototypeSlice(id, 5); | | 292 | + } | | 293 | + | | 294 | + if (!BuiltinModule.canBeRequiredByUsers(normalizedId) || | | 295 | + (id === normalizedId && !BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) { | | 296 | + return undefined; | | 297 | + } | | 298 | + | | 299 | + return normalizedId; | | 300 | + } | | 301 | + | 288 | 302 | static isBuiltin(id) { | 289 | 303 | return BuiltinModule.canBeRequiredWithoutScheme(id) || ( | 290 | 304 | typeof id === 'string' && | |
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 | getEmbedderEntryFunction, | 18 | 16 | compileSerializeMain, | @@ -98,13 +96,8 @@ function supportedInUserSnapshot(id) { | 98 | 96 | } | 99 | 97 |
| 100 | 98 | function requireForUserSnapshot(id) { | 101 | | - let normalizedId = id; | 102 | | - if (StringPrototypeStartsWith(id, 'node:')) { | 103 | | - normalizedId = StringPrototypeSlice(id, 5); | 104 | | - } | 105 | | - if (!BuiltinModule.canBeRequiredByUsers(normalizedId) || | 106 | | - (id !== normalizedId && | 107 | | - !BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) { | | 99 | + const normalizedId = normalizeRequirableId(id); | | 100 | + if (!normalizedId) { | 108 | 101 | // eslint-disable-next-line no-restricted-syntax | 109 | 102 | const err = new Error( | 110 | 103 | `Cannot find module '${id}'. `, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/embedding.js +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 | const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors'); | | 3 | +const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm'); | 3 | 4 | const { Module, wrapSafe } = require('internal/modules/cjs/loader'); | 4 | 5 |
| 5 | 6 | // This is roughly the same as: | @@ -36,12 +37,12 @@ function embedderRunCjs(contents) { | 36 | 37 | customDirname); | 37 | 38 | } | 38 | 39 |
| 39 | | -function embedderRequire(path) { | 40 | | - if (!Module.isBuiltin(path)) { | 41 | | - throw new ERR_UNKNOWN_BUILTIN_MODULE(path); | | 40 | +function embedderRequire(id) { | | 41 | + const normalizedId = normalizeRequirableId(id); | | 42 | + if (!normalizedId) { | | 43 | + throw new ERR_UNKNOWN_BUILTIN_MODULE(id); | 42 | 44 | } | 43 | | - | 44 | | - return require(path); | | 45 | + return require(normalizedId); | 45 | 46 | } | 46 | 47 |
| 47 | 48 | module.exports = { embedderRequire, embedderRunCjs }; |
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 |
| |
|