Skip to content

Commit 6f0458d

Browse files
RaisinTentargos
authored andcommitted
module: refactor to use normalizeRequirableId in the CJS module loader
`BuiltinModule.normalizeRequirableId()` was introduced in #47779 to fix a bug in the require function of SEAs and Snapshots, so that built-in modules with the `node:` scheme could be required correctly. This change makes more use of this API instead of `BuiltinModule.canBeRequiredByUsers()` and `BuiltinModule.canBeRequiredWithoutScheme()` to reduce chances of such bugs. Signed-off-by: Darshan Sen <[email protected]> PR-URL: #47896 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent 12d731e commit 6f0458d

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

lib/internal/bootstrap/realm.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,16 @@ class BuiltinModule {
300300
}
301301

302302
static normalizeRequirableId(id) {
303-
let normalizedId = id;
304303
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;
304+
const normalizedId = StringPrototypeSlice(id, 5);
305+
if (BuiltinModule.canBeRequiredByUsers(normalizedId)) {
306+
return normalizedId;
307+
}
308+
} else if (BuiltinModule.canBeRequiredWithoutScheme(id)) {
309+
return id;
311310
}
312311

313-
return normalizedId;
312+
return undefined;
314313
}
315314

316315
static getSchemeOnlyModuleNames() {

lib/internal/modules/cjs/loader.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -782,12 +782,7 @@ if (isWindows) {
782782
}
783783

784784
Module._resolveLookupPaths = function(request, parent) {
785-
if ((
786-
StringPrototypeStartsWith(request, 'node:') &&
787-
BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5))
788-
) || (
789-
BuiltinModule.canBeRequiredWithoutScheme(request)
790-
)) {
785+
if (BuiltinModule.normalizeRequirableId(request)) {
791786
debug('looking for %j in []', request);
792787
return null;
793788
}
@@ -979,14 +974,7 @@ Module._load = function(request, parent, isMain) {
979974
};
980975

981976
Module._resolveFilename = function(request, parent, isMain, options) {
982-
if (
983-
(
984-
StringPrototypeStartsWith(request, 'node:') &&
985-
BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5))
986-
) || (
987-
BuiltinModule.canBeRequiredWithoutScheme(request)
988-
)
989-
) {
977+
if (BuiltinModule.normalizeRequirableId(request)) {
990978
return request;
991979
}
992980

test/fixtures/errors/force_colors.snapshot

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ throw new Error('Should include grayed stack trace')
44

55
Error: Should include grayed stack trace
66
at Object.<anonymous> (/test*force_colors.js:1:7)
7-
 at Module._compile (node:internal*modules*cjs*loader:1257:14)
8-
 at Module._extensions..js (node:internal*modules*cjs*loader:1311:10)
9-
 at Module.load (node:internal*modules*cjs*loader:1115:32)
10-
 at Module._load (node:internal*modules*cjs*loader:955:12)
7+
 at Module._compile (node:internal*modules*cjs*loader:1245:14)
8+
 at Module._extensions..js (node:internal*modules*cjs*loader:1299:10)
9+
 at Module.load (node:internal*modules*cjs*loader:1103:32)
10+
 at Module._load (node:internal*modules*cjs*loader:950:12)
1111
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:88:12)
1212
 at node:internal*main*run_main_module:23:47
1313

0 commit comments

Comments
 (0)