Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f4ea495

Browse filesBrowse 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
Copy full SHA for f4ea495

3 files changed

+18-6Lines changed: 18 additions & 6 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/load.js
+6-2Lines 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') {
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+6-2Lines 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;
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+6-2Lines 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
const assert = require('internal/assert');
26-
const { readFileSync } = require('fs');
26+
const fs = 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(new URL(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(new URL(url), 'utf8');
346350
} catch {
347351
// Continue regardless of error.
348352
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.