Skip to content

lib: lazyload fs to ease Electron's monkey-patching #58020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/internal/modules/esm/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {

const { defaultGetFormat } = require('internal/modules/esm/get_format');
const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert');
const { readFileSync } = require('fs');
const fs = require('fs');

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

Expand All @@ -34,8 +34,7 @@ async function getSource(url, context) {
const 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 result = dataURLProcessor(url);
if (result === 'failure') {
Expand All @@ -59,7 +58,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 result = dataURLProcessor(url);
if (result === 'failure') {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const {
const assert = require('internal/assert');
const internalFS = require('internal/fs/utils');
const { BuiltinModule } = require('internal/bootstrap/realm');
const { realpathSync } = require('fs');
const fs = require('fs');
const { getOptionValue } = require('internal/options');
// Do not eagerly grab .manifest, it may be in TDZ
const { sep, posix: { relative: relativePosixPath }, resolve } = require('path');
Expand Down Expand Up @@ -277,7 +277,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
}

if (!preserveSymlinks) {
const real = realpathSync(path, {
const real = fs.realpathSync(path, {
[internalFS.realpathCacheKey]: realpathCache,
});
const { search, hash } = resolved;
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const {

const { BuiltinModule } = require('internal/bootstrap/realm');
const assert = require('internal/assert');
const { readFileSync } = require('fs');
const fs = require('fs');
const { dirname, extname } = require('path');
const {
assertBufferSource,
Expand Down Expand Up @@ -317,7 +317,7 @@ translators.set('commonjs', function commonjsStrategy(url, source, isMain) {

try {
// 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.
}
Expand Down
Loading