@@ -6,62 +6,51 @@ Subject: fix: lazyload fs in esm loaders to apply asar patches
66Changes { foo } from fs to just "fs.foo" so that our patching of fs is applied to esm loaders
77
88diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js
9- index d8a072cf6af3b0d3a47ee69be04b26875683d261..7991703df940634c62d798210b5e7b94412f9bde 100644
9+ index 9be4495726597bb48c544b362e3cf0e79e0291e1..8c800d8790d59465b0d8b807735953ea1b5692bc 100644
1010--- a/lib/internal/modules/esm/load.js
1111+++ b/lib/internal/modules/esm/load.js
12- @@ -20 ,7 +20 ,7 @@ const experimentalNetworkImports =
13-
14- const { Buffer: { from: BufferFrom } } = require('buffer ');
15-
16- - const { readFile: readFileAsync } = require('internal/fs/promises').exports ;
12+ @@ -10 ,7 +10 ,7 @@ const { kEmptyObject } = require('internal/util');
13+ const { defaultGetFormat } = require('internal/modules/esm/get_format');
14+ const { validateAssertions } = require('internal/modules/esm/assert ');
15+ const { getOptionValue } = require('internal/options');
16+ - const { readFileSync } = require('fs') ;
1717+ const fs = require('fs');
18- const { URL } = require('internal/url');
19- const {
20- ERR_INVALID_URL,
21- @@ -39,7 +39 ,7 @@ async function getSource(url, context) {
18+
19+ // Do not eagerly grab .manifest, it may be in TDZ
20+ const policy = getOptionValue('--experimental-policy') ?
21+ @@ -40,8 +40 ,7 @@ async function getSource(url, context) {
2222 let responseURL = href;
2323 let source;
2424 if (protocol === 'file:') {
25+ - const { readFile: readFileAsync } = require('internal/fs/promises').exports;
2526- source = await readFileAsync(url);
2627+ source = await fs.promises.readFile(url);
2728 } else if (protocol === 'data:') {
2829 const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
2930 if (!match) {
31+ @@ -80,7 +79,7 @@ function getSourceSync(url, context) {
32+ const responseURL = href;
33+ let source;
34+ if (protocol === 'file:') {
35+ - source = readFileSync(url);
36+ + source = fs.readFileSync(url);
37+ } else if (protocol === 'data:') {
38+ const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
39+ if (!match) {
3040diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js
31- index a93d93b3c2aae3ef790ffa4f417d50b884451549..4072321e6bc3c9f0c8428d8159670950886c3404 100644
41+ index 6a5d92d932df4f0b34840283821cf18839fbe1a5..fb47a40e04a9851db5287c7f61300feb85511ea3 100644
3242--- a/lib/internal/modules/esm/resolve.js
3343+++ b/lib/internal/modules/esm/resolve.js
34- @@ -26,11 +26 ,7 @@ const {
44+ @@ -24,7 +24 ,7 @@ const {
3545 } = primordials;
3646 const internalFS = require('internal/fs/utils');
37- const { BuiltinModule } = require('internal/bootstrap/loaders');
38- - const {
39- - realpathSync,
40- - statSync,
41- - Stats,
42- - } = require('fs');
47+ const { BuiltinModule } = require('internal/bootstrap/realm');
48+ - const { realpathSync } = require('fs');
4349+ const fs = require('fs');
4450 const { getOptionValue } = require('internal/options');
45- const pendingDeprecation = getOptionValue('--pending-deprecation');
4651 // Do not eagerly grab .manifest, it may be in TDZ
47- @@ -172,14 +168,14 @@ const realpathCache = new SafeMap();
48- * @returns {import('fs').Stats}
49- */
50- const tryStatSync =
51- - (path) => statSync(path, { throwIfNoEntry: false }) ?? new Stats();
52- + (path) => fs.statSync(path, { throwIfNoEntry: false }) ?? new fs.Stats();
53-
54- /**
55- * @param {string | URL} url
56- * @returns {boolean}
57- */
58- function fileExists(url) {
59- - return statSync(url, { throwIfNoEntry: false })?.isFile() ?? false;
60- + return fs.statSync(url, { throwIfNoEntry: false })?.isFile() ?? false;
61- }
62-
63- /**
64- @@ -329,7 +325,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
52+ const policy = getOptionValue('--experimental-policy') ?
53+ @@ -235,7 +235,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
6554 }
6655
6756 if (!preserveSymlinks) {
@@ -71,19 +60,19 @@ index a93d93b3c2aae3ef790ffa4f417d50b884451549..4072321e6bc3c9f0c8428d8159670950
7160 });
7261 const { search, hash } = resolved;
7362diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
74- index 1ceb89da21610c703f4a18be5888373c7feaa370..347558c805c8ecd3f7ff4f6324ef7df68badc52f 100644
63+ index 4020813f061d85ee27d50b938825319ab455c311..0ab3100a6fdb729f977888332d6765a9c6e8953c 100644
7564--- a/lib/internal/modules/esm/translators.js
7665+++ b/lib/internal/modules/esm/translators.js
77- @@ -24,7 +24,7 @@ function lazyTypes() {
78- return _TYPES = require('internal/util/types');
66+ @@ -25,7 +25,7 @@ function lazyTypes() {
7967 }
8068
69+ const assert = require('internal/assert');
8170- const { readFileSync } = require('fs');
8271+ const fs = require('fs');
83- const { extname, isAbsolute } = require('path');
72+ const { dirname, extname, isAbsolute } = require('path');
8473 const {
8574 hasEsmSyntax,
86- @@ -131 ,7 +131 ,7 @@ translators.set('module', async function moduleStrategy(url, source, isMain) {
75+ @@ -132 ,7 +132 ,7 @@ translators.set('module', async function moduleStrategy(url, source, isMain) {
8776 */
8877 function enrichCJSError(err, content, filename) {
8978 if (err != null && ObjectGetPrototypeOf(err) === SyntaxErrorPrototype &&
@@ -92,12 +81,21 @@ index 1ceb89da21610c703f4a18be5888373c7feaa370..347558c805c8ecd3f7ff4f6324ef7df6
9281 // Emit the warning synchronously because we are in the middle of handling
9382 // a SyntaxError that will throw and likely terminate the process before an
9483 // asynchronous warning would be emitted.
95- @@ -207 ,7 +207 ,7 @@ function cjsPreparseModuleExports(filename) {
84+ @@ -290 ,7 +290 ,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
9685
97- let source;
9886 try {
99- - source = readFileSync(filename, 'utf8');
100- + source = fs.readFileSync(filename, 'utf8');
87+ // We still need to read the FS to detect the exports.
88+ - source ??= readFileSync(new URL(url), 'utf8');
89+ + source ??= fs.readFileSync(new URL(url), 'utf8');
10190 } catch {
10291 // Continue regardless of error.
10392 }
93+ @@ -353,7 +353,7 @@ function cjsPreparseModuleExports(filename, source) {
94+ isAbsolute(resolved)) {
95+ // TODO: this should be calling the `load` hook chain to get the source
96+ // (and fallback to reading the FS only if the source is nullish).
97+ - const source = readFileSync(resolved, 'utf-8');
98+ + const source = fs.readFileSync(resolved, 'utf-8');
99+ const { exportNames: reexportNames } = cjsPreparseModuleExports(resolved, source);
100+ for (const name of reexportNames)
101+ exportNames.add(name);
0 commit comments