GeezerVison is showing github.com — fs: restore fs patchability in ESM loader · nodejs/node@873c2bc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/load.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | const { defaultGetFormat } = require('internal/modules/esm/get_format'); | 11 | 11 | const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert'); | 12 | | -const { readFileSync } = require('fs'); | | 12 | +const fs = require('fs'); | 13 | 13 |
| 14 | 14 | const { Buffer: { from: BufferFrom } } = require('buffer'); | 15 | 15 |
| @@ -34,7 +34,11 @@ function getSourceSync(url, context) { | 34 | 34 | const responseURL = href; | 35 | 35 | let source; | 36 | 36 | 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); | 38 | 42 | } else if (protocol === 'data:') { | 39 | 43 | const result = dataURLProcessor(url); | 40 | 44 | if (result === 'failure') { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,7 +25,7 @@ const { | 25 | 25 | const assert = require('internal/assert'); | 26 | 26 | const internalFS = require('internal/fs/utils'); | 27 | 27 | const { BuiltinModule } = require('internal/bootstrap/realm'); | 28 | | -const { realpathSync } = require('fs'); | | 28 | +const fs = require('fs'); | 29 | 29 | const { getOptionValue } = require('internal/options'); | 30 | 30 | // Do not eagerly grab .manifest, it may be in TDZ | 31 | 31 | const { sep, posix: { relative: relativePosixPath }, resolve } = require('path'); | @@ -273,7 +273,11 @@ function finalizeResolution(resolved, base, preserveSymlinks) { | 273 | 273 | } | 274 | 274 |
| 275 | 275 | 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, { | 277 | 281 | [internalFS.realpathCacheKey]: realpathCache, | 278 | 282 | }); | 279 | 283 | const { search, hash } = resolved; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,7 +22,7 @@ const { | 22 | 22 |
| 23 | 23 | const { BuiltinModule } = require('internal/bootstrap/realm'); | 24 | 24 | const assert = require('internal/assert'); | 25 | | -const { readFileSync } = require('fs'); | | 25 | +const fs = require('fs'); | 26 | 26 | const { dirname, extname } = require('path'); | 27 | 27 | const { | 28 | 28 | assertBufferSource, | @@ -343,7 +343,11 @@ translators.set('commonjs', function commonjsStrategy(url, translateContext, par | 343 | 343 |
| 344 | 344 | try { | 345 | 345 | // 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'); | 347 | 351 | } catch { | 348 | 352 | // Continue regardless of error. | 349 | 353 | } | |
|