Skip to content

Commit 824962c

Browse files
committed
Optimize file extension detection performance
1 parent 8189f0f commit 824962c

1 file changed

Lines changed: 29 additions & 31 deletions

File tree

packages/opencode/src/file/index.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -204,37 +204,34 @@ export namespace File {
204204
"x3f",
205205
])
206206

207-
function isImageByExtension(filepath: string): boolean {
208-
const ext = path.extname(filepath).toLowerCase().slice(1)
209-
return imageExtensions.has(ext)
207+
const imageMimeTypes: Record<string, string> = {
208+
png: "image/png",
209+
jpg: "image/jpeg",
210+
jpeg: "image/jpeg",
211+
gif: "image/gif",
212+
bmp: "image/bmp",
213+
webp: "image/webp",
214+
ico: "image/x-icon",
215+
tif: "image/tiff",
216+
tiff: "image/tiff",
217+
svg: "image/svg+xml",
218+
svgz: "image/svg+xml",
219+
avif: "image/avif",
220+
apng: "image/apng",
221+
jxl: "image/jxl",
222+
heic: "image/heic",
223+
heif: "image/heif",
210224
}
211225

212-
function getImageMimeType(filepath: string): string {
226+
function getFileTypeByExtension(filepath: string): { isImage: boolean; isBinary: boolean; mimeType?: string } {
213227
const ext = path.extname(filepath).toLowerCase().slice(1)
214-
const mimeTypes: Record<string, string> = {
215-
png: "image/png",
216-
jpg: "image/jpeg",
217-
jpeg: "image/jpeg",
218-
gif: "image/gif",
219-
bmp: "image/bmp",
220-
webp: "image/webp",
221-
ico: "image/x-icon",
222-
tif: "image/tiff",
223-
tiff: "image/tiff",
224-
svg: "image/svg+xml",
225-
svgz: "image/svg+xml",
226-
avif: "image/avif",
227-
apng: "image/apng",
228-
jxl: "image/jxl",
229-
heic: "image/heic",
230-
heif: "image/heif",
228+
if (imageExtensions.has(ext)) {
229+
return { isImage: true, isBinary: false, mimeType: imageMimeTypes[ext] || "image/" + ext }
231230
}
232-
return mimeTypes[ext] || "image/" + ext
233-
}
234-
235-
function isBinaryByExtension(filepath: string): boolean {
236-
const ext = path.extname(filepath).toLowerCase().slice(1)
237-
return binaryExtensions.has(ext)
231+
if (binaryExtensions.has(ext)) {
232+
return { isImage: false, isBinary: true }
233+
}
234+
return { isImage: false, isBinary: false }
238235
}
239236

240237
function isImage(mimeType: string): boolean {
@@ -436,18 +433,19 @@ export namespace File {
436433
}
437434

438435
// Fast path: check extension before any filesystem operations
439-
if (isImageByExtension(file)) {
436+
const fileType = getFileTypeByExtension(file)
437+
438+
if (fileType.isImage) {
440439
const bunFile = Bun.file(full)
441440
if (await bunFile.exists()) {
442441
const buffer = await bunFile.arrayBuffer().catch(() => new ArrayBuffer(0))
443442
const content = Buffer.from(buffer).toString("base64")
444-
const mimeType = getImageMimeType(file)
445-
return { type: "text", content, mimeType, encoding: "base64" }
443+
return { type: "text", content, mimeType: fileType.mimeType, encoding: "base64" }
446444
}
447445
return { type: "text", content: "" }
448446
}
449447

450-
if (isBinaryByExtension(file)) {
448+
if (fileType.isBinary) {
451449
return { type: "binary", content: "" }
452450
}
453451

0 commit comments

Comments
 (0)