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: https://github.com/nodejs/node/pull/60974
Fixes: https://github.com/nodejs/node/issues/60973
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
Joyee Cheung
2025-12-13 08:36:20 +01:00
committed by GitHub
parent 28b11396fe
commit 4f24aff94a
4 changed files with 50 additions and 13 deletions

View File

@@ -135,7 +135,7 @@ const { BuiltinModule } = require('internal/bootstrap/realm');
const {
maybeCacheSourceMap,
} = require('internal/source_map/source_map_cache');
const { pathToFileURL, fileURLToPath, isURL } = require('internal/url');
const { pathToFileURL, fileURLToPath, isURL, URL } = require('internal/url');
const {
pendingDeprecate,
emitExperimentalWarning,
@@ -1922,7 +1922,7 @@ Module._extensions['.node'] = function(module, filename) {
* @param {string} filename The path to the module
* @returns {any}
*/
function createRequireFromPath(filename) {
function createRequireFromPath(filename, fileURL) {
// Allow a directory to be passed as the filename
const trailingSlash =
StringPrototypeEndsWith(filename, '/') ||
@@ -1934,6 +1934,10 @@ function createRequireFromPath(filename) {
const m = new Module(proxyPath);
m.filename = proxyPath;
if (fileURL !== undefined) {
// Save the URL if createRequire() was given a URL, to preserve search params, if any.
m[kURL] = fileURL.href;
}
m.paths = Module._nodeModulePaths(m.path);
return makeRequireFunction(m, null);
@@ -1944,28 +1948,32 @@ const createRequireError = 'must be a file URL object, file URL string, or ' +
/**
* Creates a new `require` function that can be used to load modules.
* @param {string | URL} filename The path or URL to the module context for this `require`
* @param {string | URL} filenameOrURL The path or URL to the module context for this `require`
* @throws {ERR_INVALID_ARG_VALUE} If `filename` is not a string or URL, or if it is a relative path that cannot be
* resolved to an absolute path.
* @returns {object}
*/
function createRequire(filename) {
let filepath;
function createRequire(filenameOrURL) {
let filepath, fileURL;
if (isURL(filename) ||
(typeof filename === 'string' && !path.isAbsolute(filename))) {
if (isURL(filenameOrURL) ||
(typeof filenameOrURL === 'string' && !path.isAbsolute(filenameOrURL))) {
try {
filepath = fileURLToPath(filename);
// It might be an URL, try to convert it.
// If it's a relative path, it would not parse and would be considered invalid per
// the documented contract.
fileURL = new URL(filenameOrURL);
filepath = fileURLToPath(fileURL);
} catch {
throw new ERR_INVALID_ARG_VALUE('filename', filename,
throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL,
createRequireError);
}
} else if (typeof filename !== 'string') {
throw new ERR_INVALID_ARG_VALUE('filename', filename, createRequireError);
} else if (typeof filenameOrURL !== 'string') {
throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL, createRequireError);
} else {
filepath = filename;
filepath = filenameOrURL;
}
return createRequireFromPath(filepath);
return createRequireFromPath(filepath, fileURL);
}
/**

View File

@@ -0,0 +1,3 @@
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url);
require('./empty.mjs');

0
test/fixtures/module-hooks/empty.mjs vendored Normal file
View File

View File

@@ -0,0 +1,26 @@
// Verify that if URL is used to createRequire, that URL is passed to the resolve hook
// as parentURL.
import * as common from '../common/index.mjs';
import assert from 'node:assert';
import { registerHooks } from 'node:module';
import * as fixtures from '../common/fixtures.mjs';
const fixtureURL = fixtures.fileURL('module-hooks/create-require-with-url.mjs').href + '?test=1';
registerHooks({
resolve: common.mustCall((specifier, context, defaultResolve) => {
const resolved = defaultResolve(specifier, context, defaultResolve);
if (specifier.startsWith('node:')) {
return resolved;
}
if (specifier === fixtureURL) {
assert.strictEqual(context.parentURL, import.meta.url);
} else { // From the createRequire call.
assert.strictEqual(specifier, './empty.mjs');
assert.strictEqual(context.parentURL, fixtureURL);
}
return resolved;
}, 3),
});
await import(fixtureURL);