import { closeSync, openSync, readSync } from "node:fs"; import { StringDecoder } from "node:string_decoder"; export function forEachSessionJsonlLine(sessionPath: string, onLine: (line: string) => void, chunkSize = 64 * 1024): void { const fd = openSync(sessionPath, "r"); const buffer = Buffer.allocUnsafe(Math.max(1, Math.floor(chunkSize))); const decoder = new StringDecoder("utf8"); let pending = ""; try { for (;;) { const bytesRead = readSync(fd, buffer, 0, buffer.length, null); if (bytesRead === 0) break; pending += decoder.write(buffer.subarray(0, bytesRead)); let start = 0; for (;;) { const newline = pending.indexOf("\n", start); if (newline < 0) { pending = pending.slice(start); break; } const line = pending.slice(start, newline); onLine(line.endsWith("\r") ? line.slice(0, -1) : line); start = newline + 1; } } pending += decoder.end(); if (pending.length > 0) onLine(pending.endsWith("\r") ? pending.slice(0, -1) : pending); } finally { closeSync(fd); } }