(filePath: string)
| 199 | * is ~2M tokens, which is well under 100 MB of JSONL. |
| 200 | */ |
| 201 | export async function readJSONLFile<T>(filePath: string): Promise<T[]> { |
| 202 | const { size } = await stat(filePath) |
| 203 | if (size <= MAX_JSONL_READ_BYTES) { |
| 204 | return parseJSONL<T>(await readFile(filePath)) |
| 205 | } |
| 206 | await using fd = await open(filePath, 'r') |
| 207 | const buf = Buffer.allocUnsafe(MAX_JSONL_READ_BYTES) |
| 208 | let totalRead = 0 |
| 209 | const fileOffset = size - MAX_JSONL_READ_BYTES |
| 210 | while (totalRead < MAX_JSONL_READ_BYTES) { |
| 211 | const { bytesRead } = await fd.read( |
| 212 | buf, |
| 213 | totalRead, |
| 214 | MAX_JSONL_READ_BYTES - totalRead, |
| 215 | fileOffset + totalRead, |
| 216 | ) |
| 217 | if (bytesRead === 0) break |
| 218 | totalRead += bytesRead |
| 219 | } |
| 220 | // Skip the first partial line |
| 221 | const newlineIndex = buf.indexOf(0x0a) |
| 222 | if (newlineIndex !== -1 && newlineIndex < totalRead - 1) { |
| 223 | return parseJSONL<T>(buf.subarray(newlineIndex + 1, totalRead)) |
| 224 | } |
| 225 | return parseJSONL<T>(buf.subarray(0, totalRead)) |
| 226 | } |
| 227 | |
| 228 | export function addItemToJSONCArray(content: string, newItem: unknown): string { |
| 229 | try { |
nothing calls this directly
no test coverage detected