Skip to content
Merged
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
55 changes: 46 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"globby": "^16.0.0",
"handlebars": "^4.7.8",
"iconv-lite": "^0.7.0",
"istextorbinary": "^9.5.0",
"is-binary-path": "^3.0.0",
"isbinaryfile": "^6.0.0",
"jiti": "^2.6.1",
"jschardet": "^3.1.4",
"json5": "^2.2.3",
Expand Down
7 changes: 4 additions & 3 deletions src/core/file/fileRead.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'node:fs/promises';
import iconv from 'iconv-lite';
import { isBinary } from 'istextorbinary';
import isBinaryPath from 'is-binary-path';
import { isBinaryFile } from 'isbinaryfile';
import jschardet from 'jschardet';
import { logger } from '../../shared/logger.js';

Expand Down Expand Up @@ -28,7 +29,7 @@ export const readRawFile = async (filePath: string, maxFileSize: number): Promis
return { content: null, skippedReason: 'size-limit' };
}

if (isBinary(filePath)) {
if (isBinaryPath(filePath)) {
logger.debug(`Skipping binary file: ${filePath}`);
return { content: null, skippedReason: 'binary-extension' };
}
Expand All @@ -37,7 +38,7 @@ export const readRawFile = async (filePath: string, maxFileSize: number): Promis

const buffer = await fs.readFile(filePath);

if (isBinary(null, buffer)) {
if (await isBinaryFile(buffer)) {
logger.debug(`Skipping binary file (content check): ${filePath}`);
return { content: null, skippedReason: 'binary-content' };
}
Expand Down
26 changes: 17 additions & 9 deletions tests/core/file/fileCollect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { Stats } from 'node:fs';
import * as fs from 'node:fs/promises';
import path from 'node:path';
import iconv from 'iconv-lite';
import { isBinary } from 'istextorbinary';
import isBinaryPath from 'is-binary-path';
import { isBinaryFile } from 'isbinaryfile';
import jschardet from 'jschardet';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { collectFiles } from '../../../src/core/file/fileCollect.js';
Expand All @@ -16,7 +17,8 @@ import { createMockConfig } from '../../testing/testUtils.js';
const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB

vi.mock('node:fs/promises');
vi.mock('istextorbinary');
vi.mock('is-binary-path');
vi.mock('isbinaryfile');
vi.mock('jschardet');
vi.mock('iconv-lite');
vi.mock('../../../src/shared/logger');
Expand Down Expand Up @@ -64,7 +66,8 @@ describe('fileCollect', () => {
const mockRootDir = '/root';
const mockConfig = createMockConfig();

vi.mocked(isBinary).mockReturnValue(false);
vi.mocked(isBinaryPath).mockReturnValue(false);
vi.mocked(isBinaryFile).mockResolvedValue(false);
vi.mocked(fs.readFile).mockResolvedValue(Buffer.from('file content'));
vi.mocked(jschardet.detect).mockReturnValue({ encoding: 'utf-8', confidence: 0.99 });
vi.mocked(iconv.decode).mockReturnValue('decoded content');
Expand All @@ -87,9 +90,10 @@ describe('fileCollect', () => {
const mockRootDir = '/root';
const mockConfig = createMockConfig();

vi.mocked(isBinary)
.mockReturnValueOnce(true) // for binary.bin
vi.mocked(isBinaryPath)
.mockReturnValueOnce(true) // for binary.bin (skip by extension)
.mockReturnValueOnce(false); // for text.txt
vi.mocked(isBinaryFile).mockResolvedValue(false);
vi.mocked(fs.readFile).mockResolvedValue(Buffer.from('file content'));
vi.mocked(jschardet.detect).mockReturnValue({ encoding: 'utf-8', confidence: 0.99 });
vi.mocked(iconv.decode).mockReturnValue('decoded content');
Expand Down Expand Up @@ -122,7 +126,8 @@ describe('fileCollect', () => {
size: 1024,
isFile: () => true,
} as Stats);
vi.mocked(isBinary).mockReturnValue(false);
vi.mocked(isBinaryPath).mockReturnValue(false);
vi.mocked(isBinaryFile).mockResolvedValue(false);
vi.mocked(fs.readFile).mockResolvedValue(Buffer.from('file content'));
vi.mocked(jschardet.detect).mockReturnValue({ encoding: 'utf-8', confidence: 0.99 });
vi.mocked(iconv.decode).mockReturnValue('decoded content');
Expand Down Expand Up @@ -165,7 +170,8 @@ describe('fileCollect', () => {
size: 1024, // 1KB (within limit)
isFile: () => true,
} as Stats);
vi.mocked(isBinary).mockReturnValue(false);
vi.mocked(isBinaryPath).mockReturnValue(false);
vi.mocked(isBinaryFile).mockResolvedValue(false);
vi.mocked(fs.readFile).mockResolvedValue(Buffer.from('file content'));
vi.mocked(jschardet.detect).mockReturnValue({ encoding: 'utf-8', confidence: 0.99 });
vi.mocked(iconv.decode).mockReturnValue('decoded content');
Expand All @@ -192,7 +198,8 @@ describe('fileCollect', () => {
const mockRootDir = '/root';
const mockConfig = createMockConfig();

vi.mocked(isBinary).mockReturnValue(false);
vi.mocked(isBinaryPath).mockReturnValue(false);
vi.mocked(isBinaryFile).mockResolvedValue(false);
vi.mocked(fs.readFile).mockRejectedValue(new Error('Read error'));

const result = await collectFiles(mockFilePaths, mockRootDir, mockConfig, () => {}, {
Expand All @@ -214,7 +221,8 @@ describe('fileCollect', () => {
const mockRootDir = '/root';
const mockConfig = createMockConfig();

vi.mocked(isBinary).mockReturnValue(false);
vi.mocked(isBinaryPath).mockReturnValue(false);
vi.mocked(isBinaryFile).mockResolvedValue(false);
vi.mocked(fs.readFile).mockResolvedValue(Buffer.from('file content'));
vi.mocked(jschardet.detect).mockReturnValue({ encoding: 'utf-8', confidence: 0.99 });
vi.mocked(iconv.decode).mockReturnValue('decoded content');
Expand Down
Loading