Skip to content

Commit 17b0873

Browse files
committed
fix: handle RangeError in isbinaryfile library
- Add try-catch blocks around all isBinaryFile calls - Treat files as binary when isbinaryfile throws RangeError - Prevents extension from freezing when processing certain files - Fixes #6242
1 parent 6216075 commit 17b0873

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/core/mentions/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,14 @@ async function getFileOrFolderContent(
268268
fileContentPromises.push(
269269
(async () => {
270270
try {
271-
const isBinary = await isBinaryFile(absoluteFilePath).catch(() => false)
271+
let isBinary = false
272+
try {
273+
isBinary = await isBinaryFile(absoluteFilePath)
274+
} catch (error) {
275+
// If isBinaryFile throws an error (e.g., RangeError), treat as binary
276+
console.warn(`Error checking if file is binary for ${absoluteFilePath}:`, error)
277+
isBinary = true
278+
}
272279
if (isBinary) {
273280
return undefined
274281
}

src/core/tools/readFileTool.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,17 @@ export async function readFileTool(
433433

434434
// Process approved files
435435
try {
436-
const [totalLines, isBinary] = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)])
436+
let totalLines: number
437+
let isBinary: boolean
438+
439+
try {
440+
;[totalLines, isBinary] = await Promise.all([countFileLines(fullPath), isBinaryFile(fullPath)])
441+
} catch (error) {
442+
// If isBinaryFile throws an error (e.g., RangeError), treat the file as binary
443+
console.warn(`Error checking if file is binary for ${relPath}:`, error)
444+
totalLines = await countFileLines(fullPath)
445+
isBinary = true
446+
}
437447

438448
// Handle binary files (but allow specific file types that extractTextFromFile can handle)
439449
if (isBinary) {

src/integrations/misc/extract-text.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ export async function extractTextFromFile(filePath: string, maxReadFileLine?: nu
8686
}
8787

8888
// Handle other files
89-
const isBinary = await isBinaryFile(filePath).catch(() => false)
89+
let isBinary = false
90+
try {
91+
isBinary = await isBinaryFile(filePath)
92+
} catch (error) {
93+
// If isBinaryFile throws an error (e.g., RangeError), treat as binary
94+
console.warn(`Error checking if file is binary for ${filePath}:`, error)
95+
isBinary = true
96+
}
9097

9198
if (!isBinary) {
9299
// Check if we need to apply line limit

0 commit comments

Comments
 (0)