Skip to content

Commit 9531947

Browse files
authored
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 d1ac02f commit 9531947

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+
const fs = require('fs');
1313

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

@@ -34,7 +34,11 @@ function getSourceSync(url, context) {
3434
const responseURL = href;
3535
let source;
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
} else if (protocol === 'data:') {
3943
const result = 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
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');
@@ -273,7 +273,11 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
273273
}
274274

275275
if (!preserveSymlinks) {
276-
const real = 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+
const real = 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
@@ -22,7 +22,7 @@ const {
2222

2323
const { BuiltinModule } = require('internal/bootstrap/realm');
2424
const assert = require('internal/assert');
25-
const { readFileSync } = require('fs');
25+
const fs = require('fs');
2626
const { dirname, extname } = require('path');
2727
const {
2828
assertBufferSource,
@@ -343,7 +343,11 @@ translators.set('commonjs', function commonjsStrategy(url, translateContext, par
343343

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

0 commit comments

Comments
 (0)