Skip to content

Commit c258d68

Browse files
Refactor filesystem operations to
async for concurrent I/O (concurrent optimisations not yet done) Replace synchronous filesystem operations with async/await throughout the codebase to enable concurrent file reads and improve bundler performance. The Context interface now uses AsyncFilesystem exclusively, allowing multiple file operations to run in parallel using Promise.all() where beneficial. Key changes: - Replace Filesystem with AsyncFilesystem in Context interface - Add AsyncNodeFs and AsyncRecordingFs implementations using fs/promises - Update all filesystem operations to use async/await - Parallelize file existence checks and reads where possible - Maintain backward compatibility with synchronous RecordingFs for tests This sets the foundation for improved build performance through concurrent I/O operations, particularly beneficial when reading many files during bundling.
1 parent 9a11ee9 commit c258d68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1408
-467
lines changed

bun.lock

Lines changed: 77 additions & 0 deletions
Large diffs are not rendered by default.

src/bundler/context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Sentry from "@sentry/node";
22
import { Ora } from "ora";
3-
import { Filesystem, nodeFs } from "./fs.js";
3+
import { AsyncFilesystem, asyncNodeFs } from "./fs.js";
44
import { initializeBigBrainAuth } from "../cli/lib/deploymentSelection.js";
55
import { logFailure, logVerbose } from "./log.js";
66
// How the error should be handled when running `npx convex dev`.
@@ -54,7 +54,7 @@ export type BigBrainAuth = {
5454
);
5555

5656
export interface Context {
57-
fs: Filesystem;
57+
fs: AsyncFilesystem;
5858
deprecationMessagePrinted: boolean;
5959
// Reports to Sentry and either throws FatalError or exits the process.
6060
// Prints the `printedMessage` if provided
@@ -97,7 +97,7 @@ class OneoffContextImpl {
9797
string,
9898
(exitCode: number, err?: any) => Promise<void>
9999
> = {};
100-
public fs: Filesystem = nodeFs;
100+
public fs: AsyncFilesystem = asyncNodeFs;
101101
public deprecationMessagePrinted: boolean = false;
102102
public spinner: Ora | undefined = undefined;
103103
private _bigBrainAuth: BigBrainAuth | null = null;

src/bundler/external.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function resolveNodeModule(
2525
}))
2626
) {
2727
const maybePath = path.join(nodeModulesPath, moduleDir);
28-
if (ctx.fs.exists(maybePath)) {
28+
if (await ctx.fs.exists(maybePath)) {
2929
return maybePath;
3030
}
3131
resolveDir = path.dirname(path.dirname(nodeModulesPath));
@@ -116,7 +116,7 @@ export async function computeExternalPackages(
116116
const externalPackages = new Map<string, ExternalPackage>();
117117
let packageJson: any;
118118
try {
119-
const packageJsonString = ctx.fs.readUtf8File(packageJsonPath);
119+
const packageJsonString = await ctx.fs.readUtf8File(packageJsonPath);
120120
packageJson = JSON.parse(packageJsonString);
121121
} catch (error: any) {
122122
return await ctx.crash({
@@ -165,7 +165,7 @@ export async function computeExternalPackages(
165165
"node_modules",
166166
getModule(packageName).dirName,
167167
);
168-
if (ctx.fs.exists(packagePath)) {
168+
if (await ctx.fs.exists(packagePath)) {
169169
externalPackages.set(packageName, {
170170
path: packagePath,
171171
});
@@ -223,7 +223,7 @@ export async function findExactVersionAndDependencies(
223223
const modulePackageJsonPath = path.join(modulePath, "package.json");
224224
let modulePackageJson: any;
225225
try {
226-
const packageJsonString = ctx.fs.readUtf8File(modulePackageJsonPath);
226+
const packageJsonString = await ctx.fs.readUtf8File(modulePackageJsonPath);
227227
modulePackageJson = JSON.parse(packageJsonString);
228228
} catch {
229229
return await ctx.crash({

0 commit comments

Comments
 (0)