Skip to content

Commit f4ea495

Browse files
joyeecheungaduh95
authored andcommitted
fs: restore fs patchability in ESM loader
Temporarily restore fs patchability in ESM loader as a workaround for helping downstream projects that depend on this undocumented hidden contract transition into using hook proper APIs. This patch intentionally avoids adding a test and instead adds warning comments to hopefully steer new code away from depending on it. PR-URL: #62835 Refs: #62012 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 01a9552 commit f4ea495

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎lib/internal/modules/esm/load.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99

1010
const{ defaultGetFormat }=require('internal/modules/esm/get_format');
1111
const{ validateAttributes, emitImportAssertionWarning }=require('internal/modules/esm/assert');
12-
const{ readFileSync }=require('fs');
12+
constfs=require('fs');
1313

1414
const{Buffer: {from: BufferFrom}}=require('buffer');
1515

@@ -34,7 +34,11 @@ function getSourceSync(url, context) {
3434
constresponseURL=href;
3535
letsource;
3636
if(protocol==='file:'){
37-
source=readFileSync(url);
37+
// If you are reading this code to figure out how to patch Node.js module loading
38+
// behavior - DO NOT depend on the patchability in new code: Node.js
39+
// internals may stop going through the JavaScript fs module entirely.
40+
// Prefer module.registerHooks() or other more formal fs hooks released in the future.
41+
source=fs.readFileSync(url);
3842
}elseif(protocol==='data:'){
3943
constresult=dataURLProcessor(url);
4044
if(result==='failure'){

‎lib/internal/modules/esm/resolve.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const {
2525
constassert=require('internal/assert');
2626
constinternalFS=require('internal/fs/utils');
2727
const{ BuiltinModule }=require('internal/bootstrap/realm');
28-
const{ realpathSync }=require('fs');
28+
constfs=require('fs');
2929
const{ getOptionValue }=require('internal/options');
3030
// Do not eagerly grab .manifest, it may be in TDZ
3131
const{ sep,posix: {relative: relativePosixPath}, resolve }=require('path');
@@ -273,7 +273,11 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
273273
}
274274

275275
if(!preserveSymlinks){
276-
constreal=realpathSync(path,{
276+
// If you are reading this code to figure out how to patch Node.js module loading
277+
// behavior - DO NOT depend on the patchability in new code: Node.js
278+
// internals may stop going through the JavaScript fs module entirely.
279+
// Prefer module.registerHooks() or other more formal fs hooks released in the future.
280+
constreal=fs.realpathSync(path,{
277281
[internalFS.realpathCacheKey]: realpathCache,
278282
});
279283
const{ search, hash }=resolved;

‎lib/internal/modules/esm/translators.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const {
2323

2424
const{ BuiltinModule }=require('internal/bootstrap/realm');
2525
constassert=require('internal/assert');
26-
const{ readFileSync }=require('fs');
26+
constfs=require('fs');
2727
const{ dirname, extname }=require('path');
2828
const{
2929
assertBufferSource,
@@ -342,7 +342,11 @@ translators.set('commonjs', function commonjsStrategy(url, translateContext, par
342342

343343
try{
344344
// We still need to read the FS to detect the exports.
345-
translateContext.source??=readFileSync(newURL(url),'utf8');
345+
// If you are reading this code to figure out how to patch Node.js module loading
346+
// behavior - DO NOT depend on the patchability in new code: Node.js
347+
// internals may stop going through the JavaScript fs module entirely.
348+
// Prefer module.registerHooks() or other more formal fs hooks released in the future.
349+
translateContext.source??=fs.readFileSync(newURL(url),'utf8');
346350
}catch{
347351
// Continue regardless of error.
348352
}

0 commit comments

Comments
 (0)