| 129 | } |
| 130 | |
| 131 | function parseJSONLBuffer<T>(buf: Buffer): T[] { |
| 132 | const bufLen = buf.length |
| 133 | let start = 0 |
| 134 | |
| 135 | // Strip UTF-8 BOM (EF BB BF) |
| 136 | if (buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) { |
| 137 | start = 3 |
| 138 | } |
| 139 | |
| 140 | const results: T[] = [] |
| 141 | while (start < bufLen) { |
| 142 | let end = buf.indexOf(0x0a, start) |
| 143 | if (end === -1) end = bufLen |
| 144 | |
| 145 | const line = buf.toString('utf8', start, end).trim() |
| 146 | start = end + 1 |
| 147 | if (!line) continue |
| 148 | try { |
| 149 | results.push(JSON.parse(line) as T) |
| 150 | } catch { |
| 151 | // Skip malformed lines |
| 152 | } |
| 153 | } |
| 154 | return results |
| 155 | } |
| 156 | |
| 157 | function parseJSONLString<T>(data: string): T[] { |
| 158 | const stripped = stripBOM(data) |