Skip to content

Commit

Permalink
chore: address changes to CJS/ESM loading
Browse files Browse the repository at this point in the history
* module: make CJS load from ESM loader (nodejs/node#47999)
* lib: improve esm resolve performance (nodejs/node#46652)
  • Loading branch information
codebytere committed Nov 29, 2023
1 parent fd9420e commit f9e4508
Showing 1 changed file with 44 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,51 @@ Subject: fix: lazyload fs in esm loaders to apply asar patches
Changes { foo } from fs to just "fs.foo" so that our patching of fs is applied to esm loaders

diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js
index d8a072cf6af3b0d3a47ee69be04b26875683d261..7991703df940634c62d798210b5e7b94412f9bde 100644
index 9be4495726597bb48c544b362e3cf0e79e0291e1..8c800d8790d59465b0d8b807735953ea1b5692bc 100644
--- a/lib/internal/modules/esm/load.js
+++ b/lib/internal/modules/esm/load.js
@@ -20,7 +20,7 @@ const experimentalNetworkImports =

const { Buffer: { from: BufferFrom } } = require('buffer');

-const { readFile: readFileAsync } = require('internal/fs/promises').exports;
@@ -10,7 +10,7 @@ const { kEmptyObject } = require('internal/util');
const { defaultGetFormat } = require('internal/modules/esm/get_format');
const { validateAssertions } = require('internal/modules/esm/assert');
const { getOptionValue } = require('internal/options');
-const { readFileSync } = require('fs');
+const fs = require('fs');
const { URL } = require('internal/url');
const {
ERR_INVALID_URL,
@@ -39,7 +39,7 @@ async function getSource(url, context) {

// Do not eagerly grab .manifest, it may be in TDZ
const policy = getOptionValue('--experimental-policy') ?
@@ -40,8 +40,7 @@ async function getSource(url, context) {
let responseURL = href;
let source;
if (protocol === 'file:') {
- const { readFile: readFileAsync } = require('internal/fs/promises').exports;
- source = await readFileAsync(url);
+ source = await fs.promises.readFile(url);
} else if (protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
if (!match) {
@@ -80,7 +79,7 @@ function getSourceSync(url, context) {
const responseURL = href;
let source;
if (protocol === 'file:') {
- source = readFileSync(url);
+ source = fs.readFileSync(url);
} else if (protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
if (!match) {
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js
index a93d93b3c2aae3ef790ffa4f417d50b884451549..4072321e6bc3c9f0c8428d8159670950886c3404 100644
index 6a5d92d932df4f0b34840283821cf18839fbe1a5..fb47a40e04a9851db5287c7f61300feb85511ea3 100644
--- a/lib/internal/modules/esm/resolve.js
+++ b/lib/internal/modules/esm/resolve.js
@@ -26,11 +26,7 @@ const {
@@ -24,7 +24,7 @@ const {
} = primordials;
const internalFS = require('internal/fs/utils');
const { BuiltinModule } = require('internal/bootstrap/loaders');
-const {
- realpathSync,
- statSync,
- Stats,
-} = require('fs');
const { BuiltinModule } = require('internal/bootstrap/realm');
-const { realpathSync } = require('fs');
+const fs = require('fs');
const { getOptionValue } = require('internal/options');
const pendingDeprecation = getOptionValue('--pending-deprecation');
// Do not eagerly grab .manifest, it may be in TDZ
@@ -172,14 +168,14 @@ const realpathCache = new SafeMap();
* @returns {import('fs').Stats}
*/
const tryStatSync =
- (path) => statSync(path, { throwIfNoEntry: false }) ?? new Stats();
+ (path) => fs.statSync(path, { throwIfNoEntry: false }) ?? new fs.Stats();

/**
* @param {string | URL} url
* @returns {boolean}
*/
function fileExists(url) {
- return statSync(url, { throwIfNoEntry: false })?.isFile() ?? false;
+ return fs.statSync(url, { throwIfNoEntry: false })?.isFile() ?? false;
}

/**
@@ -329,7 +325,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
const policy = getOptionValue('--experimental-policy') ?
@@ -235,7 +235,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
}

if (!preserveSymlinks) {
Expand All @@ -71,19 +60,19 @@ index a93d93b3c2aae3ef790ffa4f417d50b884451549..4072321e6bc3c9f0c8428d8159670950
});
const { search, hash } = resolved;
diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
index 1ceb89da21610c703f4a18be5888373c7feaa370..347558c805c8ecd3f7ff4f6324ef7df68badc52f 100644
index 4020813f061d85ee27d50b938825319ab455c311..0ab3100a6fdb729f977888332d6765a9c6e8953c 100644
--- a/lib/internal/modules/esm/translators.js
+++ b/lib/internal/modules/esm/translators.js
@@ -24,7 +24,7 @@ function lazyTypes() {
return _TYPES = require('internal/util/types');
@@ -25,7 +25,7 @@ function lazyTypes() {
}

const assert = require('internal/assert');
-const { readFileSync } = require('fs');
+const fs = require('fs');
const { extname, isAbsolute } = require('path');
const { dirname, extname, isAbsolute } = require('path');
const {
hasEsmSyntax,
@@ -131,7 +131,7 @@ translators.set('module', async function moduleStrategy(url, source, isMain) {
@@ -132,7 +132,7 @@ translators.set('module', async function moduleStrategy(url, source, isMain) {
*/
function enrichCJSError(err, content, filename) {
if (err != null && ObjectGetPrototypeOf(err) === SyntaxErrorPrototype &&
Expand All @@ -92,12 +81,21 @@ index 1ceb89da21610c703f4a18be5888373c7feaa370..347558c805c8ecd3f7ff4f6324ef7df6
// Emit the warning synchronously because we are in the middle of handling
// a SyntaxError that will throw and likely terminate the process before an
// asynchronous warning would be emitted.
@@ -207,7 +207,7 @@ function cjsPreparseModuleExports(filename) {
@@ -290,7 +290,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source,

let source;
try {
- source = readFileSync(filename, 'utf8');
+ source = fs.readFileSync(filename, 'utf8');
// We still need to read the FS to detect the exports.
- source ??= readFileSync(new URL(url), 'utf8');
+ source ??= fs.readFileSync(new URL(url), 'utf8');
} catch {
// Continue regardless of error.
}
@@ -353,7 +353,7 @@ function cjsPreparseModuleExports(filename, source) {
isAbsolute(resolved)) {
// TODO: this should be calling the `load` hook chain to get the source
// (and fallback to reading the FS only if the source is nullish).
- const source = readFileSync(resolved, 'utf-8');
+ const source = fs.readFileSync(resolved, 'utf-8');
const { exportNames: reexportNames } = cjsPreparseModuleExports(resolved, source);
for (const name of reexportNames)
exportNames.add(name);

0 comments on commit f9e4508

Please sign in to comment.