Skip to content

Commit 58a4020

Browse files
anonrigdevin-ai-integration[bot]petebacondarwin
authored
perf: reduce number of errors thrown (#12602)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent 1714b9b commit 58a4020

File tree

16 files changed

+60
-85
lines changed

16 files changed

+60
-85
lines changed

.changeset/reduce-fs-errors.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"create-cloudflare": patch
3+
"miniflare": patch
4+
"@cloudflare/vitest-pool-workers": patch
5+
"@cloudflare/workers-utils": patch
6+
"wrangler": patch
7+
---
8+
9+
Optimize filesystem operations by using Node.js's throwIfNoEntry: false option
10+
11+
This reduces the number of system calls made when checking for file existence by avoiding the overhead of throwing and catching errors for missing paths. This is an internal performance optimization with no user-visible behavioral changes.

packages/create-cloudflare/src/helpers/codemod.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, lstatSync, readdirSync } from "node:fs";
1+
import { lstatSync, readdirSync } from "node:fs";
22
import nodePath, { extname, join } from "node:path";
33
import * as recast from "recast";
44
import * as esprimaParser from "recast/parsers/esprima";
@@ -82,11 +82,7 @@ export const transformFile = (
8282
export const loadSnippets = (parentFolder: string) => {
8383
const snippetsPath = join(parentFolder, "snippets");
8484

85-
if (!existsSync(snippetsPath)) {
86-
return {};
87-
}
88-
89-
if (!lstatSync(snippetsPath).isDirectory) {
85+
if (!lstatSync(snippetsPath, { throwIfNoEntry: false })?.isDirectory()) {
9086
return {};
9187
}
9288

@@ -95,7 +91,7 @@ export const loadSnippets = (parentFolder: string) => {
9591
return (
9692
files
9793
// don't try loading directories
98-
.filter((fileName) => lstatSync(join(snippetsPath, fileName)).isFile)
94+
.filter((fileName) => lstatSync(join(snippetsPath, fileName)).isFile())
9995
// only load js or ts files
10096
.filter((fileName) => [".js", ".ts"].includes(extname(fileName)))
10197
.reduce((acc, snippetPath) => {

packages/create-cloudflare/src/helpers/files.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ export const removeFile = (path: string) => {
4646

4747
export const directoryExists = (path: string): boolean => {
4848
try {
49-
const stat = statSync(path);
50-
return stat.isDirectory();
49+
const stat = statSync(path, { throwIfNoEntry: false });
50+
return stat?.isDirectory() ?? false;
5151
} catch (error) {
52-
if ((error as { code: string }).code === "ENOENT") {
53-
return false;
54-
}
5552
throw new Error(error as string);
5653
}
5754
};

packages/create-cloudflare/src/helpers/global-wrangler-config-path.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import nodePath from "node:path";
55
import xdgAppPaths from "xdg-app-paths";
66

77
function isDirectory(configPath: string) {
8-
try {
9-
return fs.statSync(configPath).isDirectory();
10-
} catch {
11-
// ignore error
12-
return false;
13-
}
8+
return (
9+
fs.statSync(configPath, { throwIfNoEntry: false })?.isDirectory() ?? false
10+
);
1411
}
1512

1613
export function getGlobalWranglerConfigPath() {

packages/miniflare/src/shared/wrangler.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import path from "node:path";
44
import xdgAppPaths from "xdg-app-paths";
55

66
function isDirectory(configPath: string) {
7-
try {
8-
return fs.statSync(configPath).isDirectory();
9-
} catch {
10-
// ignore error
11-
return false;
12-
}
7+
return (
8+
fs.statSync(configPath, { throwIfNoEntry: false })?.isDirectory() ?? false
9+
);
1310
}
1411

1512
export function getGlobalWranglerConfigPath() {

packages/vitest-pool-workers/src/pool/module-fallback.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,13 @@ const forceModuleTypeRegexp = new RegExp(
6868
);
6969

7070
function isFile(filePath: string): boolean {
71-
try {
72-
return fs.statSync(filePath).isFile();
73-
} catch (e) {
74-
if (isFileNotFoundError(e)) {
75-
return false;
76-
}
77-
throw e;
78-
}
71+
return fs.statSync(filePath, { throwIfNoEntry: false })?.isFile() ?? false;
7972
}
8073

8174
function isDirectory(filePath: string): boolean {
82-
try {
83-
return fs.statSync(filePath).isDirectory();
84-
} catch (e) {
85-
if (isFileNotFoundError(e)) {
86-
return false;
87-
}
88-
throw e;
89-
}
75+
return (
76+
fs.statSync(filePath, { throwIfNoEntry: false })?.isDirectory() ?? false
77+
);
9078
}
9179

9280
function getParentPaths(filePath: string): string[] {

packages/workers-utils/src/fs-helpers.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,5 @@ import fs from "node:fs";
99
* @returns `true` if the path is a directory, `false` otherwise
1010
*/
1111
export function isDirectory(path: string) {
12-
try {
13-
return fs.statSync(path).isDirectory();
14-
} catch {
15-
// ignore error
16-
return false;
17-
}
12+
return fs.statSync(path, { throwIfNoEntry: false })?.isDirectory() ?? false;
1813
}

packages/wrangler/src/api/pages/deploy.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,11 @@ export async function deploy({
144144
_routesCustom = readFileSync(join(directory, "_routes.json"), "utf-8");
145145
} catch {}
146146

147-
try {
148-
_workerJSIsDirectory = lstatSync(_workerPath).isDirectory();
149-
if (!_workerJSIsDirectory) {
150-
_workerJS = readFileSync(_workerPath, "utf-8");
151-
}
152-
} catch {}
147+
const workerJSStats = lstatSync(_workerPath, { throwIfNoEntry: false });
148+
_workerJSIsDirectory = workerJSStats?.isDirectory() ?? false;
149+
if (workerJSStats !== undefined && !_workerJSIsDirectory) {
150+
_workerJS = readFileSync(_workerPath, "utf-8");
151+
}
153152

154153
// Grab the bindings from the API, we need these for shims and other such hacky inserts
155154
const project = await fetchResult<Project>(

packages/wrangler/src/autoconfig/c3-vendor/codemod.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, lstatSync, readdirSync, writeFileSync } from "node:fs";
1+
import { lstatSync, readdirSync, writeFileSync } from "node:fs";
22
import path, { extname, join } from "node:path";
33
import { readFileSync } from "@cloudflare/workers-utils";
44
import * as recast from "recast";
@@ -80,11 +80,7 @@ export const transformFile = (
8080
export const loadSnippets = (parentFolder: string) => {
8181
const snippetsPath = join(parentFolder, "snippets");
8282

83-
if (!existsSync(snippetsPath)) {
84-
return {};
85-
}
86-
87-
if (!lstatSync(snippetsPath).isDirectory) {
83+
if (!lstatSync(snippetsPath, { throwIfNoEntry: false })?.isDirectory()) {
8884
return {};
8985
}
9086

@@ -93,7 +89,7 @@ export const loadSnippets = (parentFolder: string) => {
9389
return (
9490
files
9591
// don't try loading directories
96-
.filter((fileName) => lstatSync(join(snippetsPath, fileName)).isFile)
92+
.filter((fileName) => lstatSync(join(snippetsPath, fileName)).isFile())
9793
// only load js or ts files
9894
.filter((fileName) => [".js", ".ts"].includes(extname(fileName)))
9995
.reduce((acc, snippetPath) => {

packages/wrangler/src/autoconfig/git.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ import { spinner } from "@cloudflare/cli/interactive";
88
// we should clean this duplication up
99

1010
function directoryExists(path: string): boolean {
11-
try {
12-
const stat = statSync(path);
13-
return stat.isDirectory();
14-
} catch (error) {
15-
if ((error as { code: string }).code === "ENOENT") {
16-
return false;
17-
}
18-
throw new Error(error as string);
19-
}
11+
const stat = statSync(path, { throwIfNoEntry: false });
12+
return stat?.isDirectory() ?? false;
2013
}
2114

2215
export async function appendToGitIgnore(

0 commit comments

Comments
 (0)