mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
module: refactor to use normalizeRequirableId in the CJS module loader
`BuiltinModule.normalizeRequirableId()` was introduced in https://github.com/nodejs/node/pull/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 <raisinten@gmail.com> PR-URL: https://github.com/nodejs/node/pull/47896 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
This commit is contained in:
@@ -287,17 +287,16 @@ class BuiltinModule {
|
||||
}
|
||||
|
||||
static normalizeRequirableId(id) {
|
||||
let normalizedId = id;
|
||||
if (StringPrototypeStartsWith(id, 'node:')) {
|
||||
normalizedId = StringPrototypeSlice(id, 5);
|
||||
const normalizedId = StringPrototypeSlice(id, 5);
|
||||
if (BuiltinModule.canBeRequiredByUsers(normalizedId)) {
|
||||
return normalizedId;
|
||||
}
|
||||
} else if (BuiltinModule.canBeRequiredWithoutScheme(id)) {
|
||||
return id;
|
||||
}
|
||||
|
||||
if (!BuiltinModule.canBeRequiredByUsers(normalizedId) ||
|
||||
(id === normalizedId && !BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return normalizedId;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
static isBuiltin(id) {
|
||||
|
||||
@@ -792,12 +792,7 @@ if (isWindows) {
|
||||
}
|
||||
|
||||
Module._resolveLookupPaths = function(request, parent) {
|
||||
if ((
|
||||
StringPrototypeStartsWith(request, 'node:') &&
|
||||
BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5))
|
||||
) || (
|
||||
BuiltinModule.canBeRequiredWithoutScheme(request)
|
||||
)) {
|
||||
if (BuiltinModule.normalizeRequirableId(request)) {
|
||||
debug('looking for %j in []', request);
|
||||
return null;
|
||||
}
|
||||
@@ -989,14 +984,7 @@ Module._load = function(request, parent, isMain) {
|
||||
};
|
||||
|
||||
Module._resolveFilename = function(request, parent, isMain, options) {
|
||||
if (
|
||||
(
|
||||
StringPrototypeStartsWith(request, 'node:') &&
|
||||
BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5))
|
||||
) || (
|
||||
BuiltinModule.canBeRequiredWithoutScheme(request)
|
||||
)
|
||||
) {
|
||||
if (BuiltinModule.normalizeRequirableId(request)) {
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user