Skip to content

Commit 37f2106

Browse files
joyeecheungjuanarbol
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 Backport-PR-URL: #64722 Refs: #62012 Signed-off-by: Ash <[email protected]> Reviewed-By: Aviv Keller <[email protected]> Reviewed-By: Mike McCready <[email protected]>
1 parent 2a3548e commit 37f2106

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
@@ -10,7 +10,7 @@ const {
1010
const { defaultGetFormat } = require('internal/modules/esm/get_format');
1111
const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert');
1212
const { getOptionValue } = require('internal/options');
13-
const { readFileSync } = require('fs');
13+
const fs = require('fs');
1414

1515
const defaultType =
1616
getOptionValue('--experimental-default-type');
@@ -38,7 +38,11 @@ function getSourceSync(url, context) {
3838
const responseURL = href;
3939
let source;
4040
if (protocol === 'file:') {
41-
source = readFileSync(url);
41+
// If you are reading this code to figure out how to patch Node.js module loading
42+
// behavior - DO NOT depend on the patchability in new code: Node.js
43+
// internals may stop going through the JavaScript fs module entirely.
44+
// Prefer module.registerHooks() or other more formal fs hooks released in the future.
45+
source = fs.readFileSync(url);
4246
} else if (protocol === 'data:') {
4347
const result = dataURLProcessor(url);
4448
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
const assert = require('internal/assert');
2626
const internalFS = require('internal/fs/utils');
2727
const { BuiltinModule } = require('internal/bootstrap/realm');
28-
const { realpathSync } = require('fs');
28+
const fs = 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');
@@ -277,7 +277,11 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
277277
}
278278

279279
if (!preserveSymlinks) {
280-
const real = realpathSync(path, {
280+
// If you are reading this code to figure out how to patch Node.js module loading
281+
// behavior - DO NOT depend on the patchability in new code: Node.js
282+
// internals may stop going through the JavaScript fs module entirely.
283+
// Prefer module.registerHooks() or other more formal fs hooks released in the future.
284+
const real = fs.realpathSync(path, {
281285
[internalFS.realpathCacheKey]: realpathCache,
282286
});
283287
const { search, hash } = resolved;

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

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

2525
const { BuiltinModule } = require('internal/bootstrap/realm');
2626
const assert = require('internal/assert');
27-
const { readFileSync } = require('fs');
27+
const fs = require('fs');
2828
const { dirname, extname } = require('path');
2929
const {
3030
assertBufferSource,
@@ -355,7 +355,11 @@ translators.set('commonjs', function commonjsStrategy(url, translateContext, par
355355

356356
try {
357357
// We still need to read the FS to detect the exports.
358-
translateContext.source ??= readFileSync(new URL(url), 'utf8');
358+
// If you are reading this code to figure out how to patch Node.js module loading
359+
// behavior - DO NOT depend on the patchability in new code: Node.js
360+
// internals may stop going through the JavaScript fs module entirely.
361+
// Prefer module.registerHooks() or other more formal fs hooks released in the future.
362+
translateContext.source ??= fs.readFileSync(new URL(url), 'utf8');
359363
} catch {
360364
// Continue regardless of error.
361365
}

0 commit comments

Comments
 (0)