@@ -6,62 +6,51 @@ Subject: fix: lazyload fs in esm loaders to apply asar patches
6
6
Changes { foo } from fs to just "fs.foo" so that our patching of fs is applied to esm loaders
7
7
8
8
diff --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
10
10
--- a/lib/internal/modules/esm/load.js
11
11
+++ 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') ;
17
17
+ 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) {
22
22
let responseURL = href;
23
23
let source;
24
24
if (protocol === 'file:') {
25
+ - const { readFile: readFileAsync } = require('internal/fs/promises').exports;
25
26
- source = await readFileAsync(url);
26
27
+ source = await fs.promises.readFile(url);
27
28
} else if (protocol === 'data:') {
28
29
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
29
30
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) {
30
40
diff --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
32
42
--- a/lib/internal/modules/esm/resolve.js
33
43
+++ b/lib/internal/modules/esm/resolve.js
34
- @@ -26,11 +26 ,7 @@ const {
44
+ @@ -24,7 +24 ,7 @@ const {
35
45
} = primordials;
36
46
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');
43
49
+ const fs = require('fs');
44
50
const { getOptionValue } = require('internal/options');
45
- const pendingDeprecation = getOptionValue('--pending-deprecation');
46
51
// 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) {
65
54
}
66
55
67
56
if (!preserveSymlinks) {
@@ -71,19 +60,19 @@ index a93d93b3c2aae3ef790ffa4f417d50b884451549..4072321e6bc3c9f0c8428d8159670950
71
60
});
72
61
const { search, hash } = resolved;
73
62
diff --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
75
64
--- a/lib/internal/modules/esm/translators.js
76
65
+++ 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() {
79
67
}
80
68
69
+ const assert = require('internal/assert');
81
70
- const { readFileSync } = require('fs');
82
71
+ const fs = require('fs');
83
- const { extname, isAbsolute } = require('path');
72
+ const { dirname, extname, isAbsolute } = require('path');
84
73
const {
85
74
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) {
87
76
*/
88
77
function enrichCJSError(err, content, filename) {
89
78
if (err != null && ObjectGetPrototypeOf(err) === SyntaxErrorPrototype &&
@@ -92,12 +81,21 @@ index 1ceb89da21610c703f4a18be5888373c7feaa370..347558c805c8ecd3f7ff4f6324ef7df6
92
81
// Emit the warning synchronously because we are in the middle of handling
93
82
// a SyntaxError that will throw and likely terminate the process before an
94
83
// 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,
96
85
97
- let source;
98
86
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');
101
90
} catch {
102
91
// Continue regardless of error.
103
92
}
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