Skip to content

Commit 9bcd67a

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

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/internal/modules/customization_hooks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ function convertCJSFilenameToURL(filename) {
130130
if (builtinId) {
131131
return `node:${builtinId}`;
132132
}
133+
if (BuiltinModule.canBeRequiredByUsers(filename)) {
134+
return `node:${filename}`;
135+
}
133136
// Handle the case where filename is neither a path, nor a built-in id,
134137
// which is possible via monkey-patching.
135138
if (isAbsolute(filename)) {
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)