◐ Shell
clean mode source ↗

fs: restore fs patchability in ESM loader · nodejs/node@f4ea495

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') {

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;

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

}