GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: fixing url change in load sync hook chain · nodejs/node@7347d34

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/modules/cjs
    • loader.js
  • test/module-hooks
    • test-module-hooks-load-url-change-import.mjs
    • test-module-hooks-load-url-change-require.js
Expand file treeCollapse file tree
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

‎test/module-hooks/test-module-hooks-load-url-change-import.mjs‎

Copy file name to clipboard
+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

‎test/module-hooks/test-module-hooks-load-url-change-require.js‎

Copy file name to clipboard
+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)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.