module: convert schema-only core module on `convertCJSFilenameToURL` · nodejs/node@bfc68c8 · GitHub
Skip to content

Commit bfc68c8

Browse files
himself65joyeecheung
authored andcommitted
module: convert schema-only core module on convertCJSFilenameToURL
Co-authored-by: Joyee Cheung <joyeec9h3@gmail.com> PR-URL: #58612 Fixes: #58607 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent e8f4a7d commit bfc68c8

3 files changed

Lines changed: 93 additions & 4 deletions

File tree

lib/internal/modules/customization_hooks.js

Lines changed: 7 additions & 3 deletions
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
// This tests that when builtins that demand the `node:` prefix are
4+
// required, the URL returned by the default resolution step is still
5+
// prefixed and valid, and that gets passed to the load hook is still
6+
// the one with the `node:` prefix. The one with the prefix
7+
// stripped for internal lookups should not get passed into the hooks.
8+
9+
const common = require('../common');
10+
const assert = require('assert');
11+
const { registerHooks } = require('module');
12+
13+
const schemelessBlockList = new Set([
14+
'sea',
15+
'sqlite',
16+
'test',
17+
'test/reporters',
18+
]);
19+
20+
const testModules = [];
21+
for (const mod of schemelessBlockList) {
22+
testModules.push(`node:${mod}`);
23+
}
24+
25+
const hook = registerHooks({
26+
resolve: common.mustCall((specifier, context, nextResolve) => {
27+
const result = nextResolve(specifier, context);
28+
assert.match(result.url, /^node:/);
29+
assert.strictEqual(schemelessBlockList.has(result.url.slice(5, result.url.length)), true);
30+
return result;
31+
}, testModules.length),
32+
load: common.mustCall(function load(url, context, nextLoad) {
33+
assert.match(url, /^node:/);
34+
assert.strictEqual(schemelessBlockList.has(url.slice(5, url.length)), true);
35+
const result = nextLoad(url, context);
36+
assert.strictEqual(result.source, null);
37+
return {
38+
source: 'throw new Error("I should not be thrown because the loader ignores user-supplied source for builtins")',
39+
format: 'builtin',
40+
};
41+
}, testModules.length),
42+
});
43+
44+
for (const mod of testModules) {
45+
require(mod);
46+
}
47+
48+
hook.deregister();

test/module-hooks/test-module-hooks-load-builtin-require.js

Lines changed: 38 additions & 1 deletion

0 commit comments

Comments
 (0)