Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3bbcf64

Browse files
committedDec 29, 2024·
module: fixing url change in load sync hook chain
Fixes: #56376
1 parent 0dbbaba commit 3bbcf64

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-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,29 @@
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+
10+
const hook = registerHooks({
11+
resolve(specifier, context, nextLoad) {
12+
assert.strictEqual(specifier, 'rfs');
13+
return {
14+
url: 'file:///rfs',
15+
shortCircuit: true,
16+
};
17+
},
18+
load: common.mustCall(function load(url, context, nextLoad) {
19+
assert.strictEqual(url, 'file:///rfs');
20+
return nextLoad(
21+
fixtures.fileURL('module-hooks', `redirected-fs.js`).href,
22+
context
23+
);
24+
}),
25+
});
26+
27+
assert.strictEqual(require('rfs').exports_for_test, 'redirected fs');
28+
29+
hook.deregister();

0 commit comments

Comments
 (0)
Please sign in to comment.