refactor: use existing strip-bom package instead of custom bomUtils

- Remove src/utils/bomUtils.ts and its test file
- Update imports in extract-text.ts to use strip-bom package
- Update imports in read-lines.ts to use strip-bom package
- Keep the empty file detection logic in readFileTool.ts unchanged

This eliminates code duplication by using the existing strip-bom package
that is already used in DiffViewProvider.ts and CustomModesManager.ts.
This commit is contained in:
Roo Code 2025-07-19 17:34:52 +00:00
parent 5063f3190a
commit c0b2a6ce11
4 changed files with 4 additions and 127 deletions

View file

@ -5,7 +5,7 @@ import mammoth from "mammoth"
import fs from "fs/promises"
import { isBinaryFile } from "isbinaryfile"
import { extractTextFromXLSX } from "./extract-text-from-xlsx"
import { stripBOM } from "../../utils/bomUtils"
import stripBom from "strip-bom"
async function extractTextFromPDF(filePath: string): Promise<string> {
const dataBuffer = await fs.readFile(filePath)
@ -70,7 +70,7 @@ export async function extractTextFromFile(filePath: string): Promise<string> {
if (!isBinary) {
const content = await fs.readFile(filePath, "utf8")
// Strip BOM if present before adding line numbers
return addLineNumbers(stripBOM(content))
return addLineNumbers(stripBom(content))
} else {
throw new Error(`Cannot read text for file type: ${fileExtension}`)
}

View file

@ -7,7 +7,7 @@
* Now you can read a range of lines from a file
*/
import { createReadStream } from "fs"
import { stripBOM } from "../../utils/bomUtils"
import stripBom from "strip-bom"
const outOfRangeError = (filepath: string, n: number) => {
return new RangeError(`Line with index ${n} does not exist in '${filepath}'. Note that line indexing is zero-based`)
@ -70,7 +70,7 @@ export function readLines(filepath: string, endLine?: number, startLine?: number
// Strip BOM from the first chunk if present
if (isFirstChunk) {
chunkStr = stripBOM(chunkStr)
chunkStr = stripBom(chunkStr)
isFirstChunk = false
}

View file

@ -1,81 +0,0 @@
import { describe, it, expect } from "vitest"
import { stripBOM, hasBOM, stripBOMFromBuffer, UTF8_BOM, UTF8_BOM_BYTES } from "../bomUtils"
describe("bomUtils", () => {
describe("stripBOM", () => {
it("should strip BOM from string with BOM", () => {
const contentWithBOM = UTF8_BOM + "Hello World"
const result = stripBOM(contentWithBOM)
expect(result).toBe("Hello World")
})
it("should return unchanged string without BOM", () => {
const contentWithoutBOM = "Hello World"
const result = stripBOM(contentWithoutBOM)
expect(result).toBe("Hello World")
})
it("should handle empty string", () => {
const result = stripBOM("")
expect(result).toBe("")
})
it("should handle string with only BOM", () => {
const result = stripBOM(UTF8_BOM)
expect(result).toBe("")
})
it("should only strip BOM from beginning", () => {
const content = UTF8_BOM + "Hello" + UTF8_BOM + "World"
const result = stripBOM(content)
expect(result).toBe("Hello" + UTF8_BOM + "World")
})
})
describe("hasBOM", () => {
it("should detect BOM in buffer", () => {
const bufferWithBOM = Buffer.concat([UTF8_BOM_BYTES, Buffer.from("Hello")])
expect(hasBOM(bufferWithBOM)).toBe(true)
})
it("should return false for buffer without BOM", () => {
const bufferWithoutBOM = Buffer.from("Hello")
expect(hasBOM(bufferWithoutBOM)).toBe(false)
})
it("should return false for empty buffer", () => {
const emptyBuffer = Buffer.alloc(0)
expect(hasBOM(emptyBuffer)).toBe(false)
})
it("should return false for buffer too short to contain BOM", () => {
const shortBuffer = Buffer.from([0xef, 0xbb]) // Only 2 bytes
expect(hasBOM(shortBuffer)).toBe(false)
})
})
describe("stripBOMFromBuffer", () => {
it("should strip BOM from buffer with BOM", () => {
const bufferWithBOM = Buffer.concat([UTF8_BOM_BYTES, Buffer.from("Hello")])
const result = stripBOMFromBuffer(bufferWithBOM)
expect(result.toString()).toBe("Hello")
})
it("should return unchanged buffer without BOM", () => {
const bufferWithoutBOM = Buffer.from("Hello")
const result = stripBOMFromBuffer(bufferWithoutBOM)
expect(result.toString()).toBe("Hello")
})
it("should handle empty buffer", () => {
const emptyBuffer = Buffer.alloc(0)
const result = stripBOMFromBuffer(emptyBuffer)
expect(result.length).toBe(0)
})
it("should handle buffer with only BOM", () => {
const result = stripBOMFromBuffer(UTF8_BOM_BYTES)
expect(result.length).toBe(0)
})
})
})

View file

@ -1,42 +0,0 @@
/**
* UTF-8 BOM (Byte Order Mark) utilities
*/
// UTF-8 BOM as a string
export const UTF8_BOM = "\uFEFF"
// UTF-8 BOM as bytes
export const UTF8_BOM_BYTES = Buffer.from([0xef, 0xbb, 0xbf])
/**
* Strips UTF-8 BOM from the beginning of a string if present
* @param content The string content to process
* @returns The content with BOM removed if it was present
*/
export function stripBOM(content: string): string {
if (content.charCodeAt(0) === 0xfeff) {
return content.slice(1)
}
return content
}
/**
* Checks if a buffer starts with UTF-8 BOM
* @param buffer The buffer to check
* @returns True if the buffer starts with UTF-8 BOM
*/
export function hasBOM(buffer: Buffer): boolean {
return buffer.length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf
}
/**
* Strips UTF-8 BOM from the beginning of a buffer if present
* @param buffer The buffer to process
* @returns A new buffer with BOM removed if it was present
*/
export function stripBOMFromBuffer(buffer: Buffer): Buffer {
if (hasBOM(buffer)) {
return buffer.slice(3)
}
return buffer
}