Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7347d34

Browse filesBrowse files
awtoaduh95
authored andcommitted
module: fixing url change in load sync hook chain
Fixes: #56376 PR-URL: #56402 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 eb3148f commit 7347d34
Copy full SHA for 7347d34

File tree

Expand file treeCollapse file tree

3 files changed

+56
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+56
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/cjs/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js
+1-1Lines changed: 1 addition & 1 deletion
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.
Collapse file
+25Lines 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();
Collapse file
+30Lines changed: 30 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.