Skip to content

Commit 15c3655

Browse files
joyeecheungRafaelGSS
authored andcommitted
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: #60974Fixes: #60973 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 657cbd4 commit 15c3655

4 files changed

Lines changed: 50 additions & 13 deletions

File tree

‎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-
functioncreateRequireFromPath(filename){
1925+
functioncreateRequireFromPath(filename,fileURL){
19261926
// Allow a directory to be passed as the filename
19271927
consttrailingSlash=
19281928
StringPrototypeEndsWith(filename,'/')||
@@ -1934,6 +1934,10 @@ function createRequireFromPath(filename) {
19341934

19351935
constm=newModule(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
returnmakeRequireFunction(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-
functioncreateRequire(filename){
1953-
letfilepath;
1956+
functioncreateRequire(filenameOrURL){
1957+
letfilepath,fileURL;
19541958

1955-
if(isURL(filename)||
1956-
(typeoffilename==='string'&&!path.isAbsolute(filename))){
1959+
if(isURL(filenameOrURL)||
1960+
(typeoffilenameOrURL==='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=newURL(filenameOrURL);
1966+
filepath=fileURLToPath(fileURL);
19591967
}catch{
1960-
thrownewERR_INVALID_ARG_VALUE('filename',filename,
1968+
thrownewERR_INVALID_ARG_VALUE('filename',filenameOrURL,
19611969
createRequireError);
19621970
}
1963-
}elseif(typeoffilename!=='string'){
1964-
thrownewERR_INVALID_ARG_VALUE('filename',filename,createRequireError);
1971+
}elseif(typeoffilenameOrURL!=='string'){
1972+
thrownewERR_INVALID_ARG_VALUE('filename',filenameOrURL,createRequireError);
19651973
}else{
1966-
filepath=filename;
1974+
filepath=filenameOrURL;
19671975
}
1968-
returncreateRequireFromPath(filepath);
1976+
returncreateRequireFromPath(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+
constrequire=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*ascommonfrom'../common/index.mjs';
4+
importassertfrom'node:assert';
5+
import{registerHooks}from'node:module';
6+
import*asfixturesfrom'../common/fixtures.mjs';
7+
8+
constfixtureURL=fixtures.fileURL('module-hooks/create-require-with-url.mjs').href+'?test=1';
9+
registerHooks({
10+
resolve: common.mustCall((specifier,context,defaultResolve)=>{
11+
constresolved=defaultResolve(specifier,context,defaultResolve);
12+
if(specifier.startsWith('node:')){
13+
returnresolved;
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+
returnresolved;
23+
},3),
24+
});
25+
26+
awaitimport(fixtureURL);

0 commit comments

Comments
 (0)