Skip to content

Commit 9ec7bed

Browse files
awtojasnell
authored andcommitted
module: fixing url change in load sync hook chain
Fixes: nodejs#56376 PR-URL: nodejs#56402 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 01dd7c4 commit 9ec7bed

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

lib/internal/modules/cjs/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ function getDefaultLoad(url, filename) {
11441144
return function defaultLoad(urlFromHook, context) {
11451145
// If the url is the same as the original one, save the conversion.
11461146
const isLoadingOriginalModule = (urlFromHook === url);
1147-
const filenameFromHook = isLoadingOriginalModule ? filename : convertURLToCJSFilename(url);
1147+
const filenameFromHook = isLoadingOriginalModule ? filename : convertURLToCJSFilename(urlFromHook);
11481148
const source = defaultLoadImpl(filenameFromHook, context.format);
11491149
// Format from context is directly returned, because format detection should only be
11501150
// done after the entire load chain is completed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import assert from 'node:assert';
3+
import { registerHooks } from 'node:module';
4+
import { fileURL } from '../common/fixtures.mjs';
5+
6+
// This tests shows the url parameter in `load` can be changed in the `nextLoad` call
7+
// It changes `foo` package name into `redirected-fs` and then loads `redirected-fs`
8+
9+
const hook = registerHooks({
10+
resolve(specifier, context, nextResolve) {
11+
assert.strictEqual(specifier, 'foo');
12+
return {
13+
url: 'foo://bar',
14+
shortCircuit: true,
15+
};
16+
},
17+
load: mustCall(function load(url, context, nextLoad) {
18+
assert.strictEqual(url, 'foo://bar');
19+
return nextLoad(fileURL('module-hooks', 'redirected-fs.js').href, context);
20+
}),
21+
});
22+
23+
assert.strictEqual((await import('foo')).exports_for_test, 'redirected fs');
24+
25+
hook.deregister();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { registerHooks } = require('module');
6+
const fixtures = require('../common/fixtures');
7+
8+
// This tests shows the url parameter in `load` can be changed in the `nextLoad` call
9+
// It changes `foo` package name into `redirected-fs` and then loads `redirected-fs`
10+
11+
const hook = registerHooks({
12+
resolve(specifier, context, nextResolve) {
13+
assert.strictEqual(specifier, 'foo');
14+
return {
15+
url: 'foo://bar',
16+
shortCircuit: true,
17+
};
18+
},
19+
load: common.mustCall(function load(url, context, nextLoad) {
20+
assert.strictEqual(url, 'foo://bar');
21+
return nextLoad(
22+
fixtures.fileURL('module-hooks', 'redirected-fs.js').href,
23+
context,
24+
);
25+
}),
26+
});
27+
28+
assert.strictEqual(require('foo').exports_for_test, 'redirected fs');
29+
30+
hook.deregister();

0 commit comments

Comments
 (0)