Skip to content

Commit a79df89

Browse files
module: convert schema-only core module on convertCJSFilenameToURL
Co-authored-by: Joyee Cheung <[email protected]>
1 parent d6dade5 commit a79df89

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

lib/internal/modules/customization_hooks.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayPrototypeSplice,
77
ObjectAssign,
88
ObjectFreeze,
9+
StringPrototypeSlice,
910
StringPrototypeStartsWith,
1011
Symbol,
1112
} = primordials;
@@ -126,9 +127,12 @@ function registerHooks(hooks) {
126127
*/
127128
function convertCJSFilenameToURL(filename) {
128129
if (!filename) { return filename; }
129-
const builtinId = BuiltinModule.normalizeRequirableId(filename);
130-
if (builtinId) {
131-
return `node:${builtinId}`;
130+
let normalizedId = filename;
131+
if (StringPrototypeStartsWith(filename, 'node:')) {
132+
normalizedId = StringPrototypeSlice(filename, 5);
133+
}
134+
if (BuiltinModule.canBeRequiredByUsers(normalizedId)) {
135+
return `node:${normalizedId}`;
132136
}
133137
// Handle the case where filename is neither a path, nor a built-in id,
134138
// which is possible via monkey-patching.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
// This tests that when builtins that demand the `node:` prefix are
4+
// required, the URL that gets passed to the load hook is still
5+
// the one with the `node:` prefix, and the one with the prefix
6+
// stripped for internal lookups don't get passed into the hooks.
7+
8+
const common = require('../common');
9+
const assert = require('assert');
10+
const { registerHooks } = require('module');
11+
12+
const schemelessBlockList = new Set([
13+
'sea',
14+
'sqlite',
15+
'test',
16+
'test/reporters',
17+
]);
18+
19+
const testModules = [];
20+
for (const mod of schemelessBlockList) {
21+
testModules.push(`node:${mod}`);
22+
}
23+
24+
const hook = registerHooks({
25+
resolve: common.mustCall((specifier, context, nextResolve) => {
26+
return nextResolve(specifier, context);
27+
}, testModules.length),
28+
load: common.mustCall(function load(url, context, nextLoad) {
29+
assert.match(url, /^node:/);
30+
assert.strictEqual(schemelessBlockList.has(url.slice(5, url.length)), true);
31+
const result = nextLoad(url, context);
32+
assert.strictEqual(result.source, null);
33+
return {
34+
source: 'throw new Error("I should not be thrown because the loader ignores user-supplied source for builtins")',
35+
format: 'builtin',
36+
};
37+
}, testModules.length),
38+
});
39+
40+
for (const mod of testModules) {
41+
require(mod);
42+
}
43+
44+
hook.deregister();

0 commit comments

Comments
 (0)