module: fixing url change in load sync hook chain · nodejs/node@926b887 · GitHub
Skip to content

Commit 926b887

Browse files
awtoRafaelGSS
authored andcommitted
module: fixing url change in load sync hook chain
Fixes: #56376 PR-URL: #56402 Backport-PR-URL: #57130 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent eff4295 commit 926b887

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 1 deletion
Lines changed: 25 additions & 0 deletions
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();
Lines changed: 30 additions & 0 deletions

0 commit comments

Comments
 (0)