* Newline-buffered scanner over a byte stream: feeds COMPLETE lines to * `extract` until it returns an id, then goes inert. A pathological "line" * exceeding the cap without a newline is discarded (the id announcement is a * small JSONL event in the first lines of every adapter's headless output)
( extract: (line: string) => string | null, onFound: (id: string) => void, )
| 107 | * small JSONL event in the first lines of every adapter's headless output). |
| 108 | */ |
| 109 | function makeSessionIdScanner( |
| 110 | extract: (line: string) => string | null, |
| 111 | onFound: (id: string) => void, |
| 112 | ): { push(c: Buffer): void } { |
| 113 | let buf = ''; |
| 114 | let done = false; |
| 115 | return { |
| 116 | push(c) { |
| 117 | if (done) return; |
| 118 | buf += c.toString('utf8'); |
| 119 | let nl: number; |
| 120 | while ((nl = buf.indexOf('\n')) !== -1) { |
| 121 | const line = buf.slice(0, nl).trim(); |
| 122 | buf = buf.slice(nl + 1); |
| 123 | if (!line) continue; |
| 124 | const id = extract(line); |
| 125 | if (id) { |
| 126 | done = true; |
| 127 | onFound(id); |
| 128 | return; |
| 129 | } |
| 130 | } |
| 131 | if (buf.length > SCAN_LINE_MAX_BYTES) buf = ''; |
| 132 | }, |
| 133 | }; |
| 134 | } |
| 135 | |
| 136 | /** Open a log write-stream, creating the parent dir; null (+ warn) on failure. */ |
| 137 | async function openLogStream(path: string, logger: Logger, name: string): Promise<WriteStream | null> { |