Skip to content

Commit 4f24aff

Browse files
authored
module: preserve URL in the parent created by createRequire()
Previously, createRequire() does not preserve the URL it gets passed in the mock parent module created, which can be observable if it's used together with module.registerHooks(). This patch adds preservation of the URL if createRequire() is invoked with one. PR-URL: #60974 Fixes: #60973 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 28b1139 commit 4f24aff

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ const { BuiltinModule } = require('internal/bootstrap/realm');
135135
const {
136136
maybeCacheSourceMap,
137137
} = require('internal/source_map/source_map_cache');
138-
const { pathToFileURL, fileURLToPath, isURL } = require('internal/url');
138+
const { pathToFileURL, fileURLToPath, isURL, URL } = require('internal/url');
139139
const {
140140
pendingDeprecate,
141141
emitExperimentalWarning,
@@ -1922,7 +1922,7 @@ Module._extensions['.node'] = function(module, filename) {
19221922
* @param {string} filename The path to the module
19231923
* @returns {any}
19241924
*/
1925-
function createRequireFromPath(filename) {
1925+
function createRequireFromPath(filename, fileURL) {
19261926
// Allow a directory to be passed as the filename
19271927
const trailingSlash =
19281928
StringPrototypeEndsWith(filename, '/') ||
@@ -1934,6 +1934,10 @@ function createRequireFromPath(filename) {
19341934

19351935
const m = new Module(proxyPath);
19361936
m.filename = proxyPath;
1937+
if (fileURL !== undefined) {
1938+
// Save the URL if createRequire() was given a URL, to preserve search params, if any.
1939+
m[kURL] = fileURL.href;
1940+
}
19371941

19381942
m.paths = Module._nodeModulePaths(m.path);
19391943
return makeRequireFunction(m, null);
@@ -1944,28 +1948,32 @@ const createRequireError = 'must be a file URL object, file URL string, or ' +
19441948

19451949
/**
19461950
* Creates a new `require` function that can be used to load modules.
1947-
* @param {string | URL} filename The path or URL to the module context for this `require`
1951+
* @param {string | URL} filenameOrURL The path or URL to the module context for this `require`
19481952
* @throws {ERR_INVALID_ARG_VALUE} If `filename` is not a string or URL, or if it is a relative path that cannot be
19491953
* resolved to an absolute path.
19501954
* @returns {object}
19511955
*/
1952-
function createRequire(filename) {
1953-
let filepath;
1956+
function createRequire(filenameOrURL) {
1957+
let filepath, fileURL;
19541958

1955-
if (isURL(filename) ||
1956-
(typeof filename === 'string' && !path.isAbsolute(filename))) {
1959+
if (isURL(filenameOrURL) ||
1960+
(typeof filenameOrURL === 'string' && !path.isAbsolute(filenameOrURL))) {
19571961
try {
1958-
filepath = fileURLToPath(filename);
1962+
// It might be an URL, try to convert it.
1963+
// If it's a relative path, it would not parse and would be considered invalid per
1964+
// the documented contract.
1965+
fileURL = new URL(filenameOrURL);
1966+
filepath = fileURLToPath(fileURL);
19591967
} catch {
1960-
throw new ERR_INVALID_ARG_VALUE('filename', filename,
1968+
throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL,
19611969
createRequireError);
19621970
}
1963-
} else if (typeof filename !== 'string') {
1964-
throw new ERR_INVALID_ARG_VALUE('filename', filename, createRequireError);
1971+
} else if (typeof filenameOrURL !== 'string') {
1972+
throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL, createRequireError);
19651973
} else {
1966-
filepath = filename;
1974+
filepath = filenameOrURL;
19671975
}
1968-
return createRequireFromPath(filepath);
1976+
return createRequireFromPath(filepath, fileURL);
19691977
}
19701978

19711979
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createRequire } from 'node:module'
2+
const require = createRequire(import.meta.url);
3+
require('./empty.mjs');

test/fixtures/module-hooks/empty.mjs

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Verify that if URL is used to createRequire, that URL is passed to the resolve hook
2+
// as parentURL.
3+
import * as common from '../common/index.mjs';
4+
import assert from 'node:assert';
5+
import { registerHooks } from 'node:module';
6+
import * as fixtures from '../common/fixtures.mjs';
7+
8+
const fixtureURL = fixtures.fileURL('module-hooks/create-require-with-url.mjs').href + '?test=1';
9+
registerHooks({
10+
resolve: common.mustCall((specifier, context, defaultResolve) => {
11+
const resolved = defaultResolve(specifier, context, defaultResolve);
12+
if (specifier.startsWith('node:')) {
13+
return resolved;
14+
}
15+
16+
if (specifier === fixtureURL) {
17+
assert.strictEqual(context.parentURL, import.meta.url);
18+
} else { // From the createRequire call.
19+
assert.strictEqual(specifier, './empty.mjs');
20+
assert.strictEqual(context.parentURL, fixtureURL);
21+
}
22+
return resolved;
23+
}, 3),
24+
});
25+
26+
await import(fixtureURL);

0 commit comments

Comments
 (0)