fix: bound document extraction

This commit is contained in:
lofyer
2026-08-13 04:46:30 +08:00
parent 5b579ae100
commit e3b5702767
13 changed files with 1106 additions and 194 deletions
+39 -12
View File
@@ -52,27 +52,54 @@ export async function readBoundedUtf8File(
tooLargeMessage: string,
invalidUtf8Message: string
): Promise<{ content: string; size: number }> {
const data = await readBoundedFile(
filePath,
maximumBytes,
tooLargeMessage
)
try {
return {
content: new TextDecoder('utf-8', { fatal: true }).decode(data),
size: data.byteLength
}
} catch (error) {
throw new Error(invalidUtf8Message, { cause: error })
}
}
export async function readBoundedFile(
filePath: string,
maximumBytes: number,
tooLargeMessage: string,
invalidFileMessage = tooLargeMessage
): Promise<Buffer> {
const handle = await open(filePath, 'r')
try {
const metadata = await handle.stat()
if (!metadata.isFile()) {
throw new Error(invalidFileMessage)
}
if (metadata.size > maximumBytes) {
throw new Error(tooLargeMessage)
}
const data = Buffer.alloc(metadata.size + 1)
const result = await handle.read(data, 0, data.length, 0)
if (result.bytesRead > maximumBytes) {
throw new Error(tooLargeMessage)
}
try {
return {
content: new TextDecoder('utf-8', { fatal: true }).decode(
data.subarray(0, result.bytesRead)
),
size: result.bytesRead
let bytesRead = 0
while (bytesRead < data.length) {
const result = await handle.read(
data,
bytesRead,
data.length - bytesRead,
bytesRead
)
if (result.bytesRead === 0) {
break
}
bytesRead += result.bytesRead
if (bytesRead > maximumBytes) {
throw new Error(tooLargeMessage)
}
} catch (error) {
throw new Error(invalidUtf8Message, { cause: error })
}
return data.subarray(0, bytesRead)
} finally {
await handle.close()
}