/** * Messages appended to a JSONL bus file after byte `from`, each with the absolute byte offset just * past its own line. Shared by the send-time herdr path and the seat's own tail, so both agree on * exactly which bytes a delivery covers — a second reader would be a second truth about where a * seat's cursor may move. * * A partial trailing line is left for the next read rather than guessed at, and a line that is not * JSON is skipped with the offset moving past it. */ import { readFileSync } from "node:fs"; export type OffsetMessage = { msg: Record; end: number }; export function newMessagesIn(file: string, from: number): OffsetMessage[] { let buf: Buffer; try { buf = readFileSync(file); } catch { return []; } const out: OffsetMessage[] = []; let pos = Math.min(Math.max(0, from), buf.length); while (pos < buf.length) { const nl = buf.indexOf(0x0a, pos); if (nl === -1) break; const line = buf.subarray(pos, nl).toString("utf8"); const end = nl + 1; pos = end; if (!line.trim()) continue; try { out.push({ msg: JSON.parse(line), end }); } catch { /* not a message; the offset moves past it */ } } return out; }