| 140 | } |
| 141 | |
| 142 | function parseJSONLBuffer<T>(buf: Buffer, strict?: boolean): T[] { |
| 143 | const bufLen = buf.length |
| 144 | let start = 0 |
| 145 | |
| 146 | // Strip UTF-8 BOM (EF BB BF) |
| 147 | if (buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) { |
| 148 | start = 3 |
| 149 | } |
| 150 | |
| 151 | const results: T[] = [] |
| 152 | let lineNumber = 0 |
| 153 | while (start < bufLen) { |
| 154 | let end = buf.indexOf(0x0a, start) |
| 155 | if (end === -1) end = bufLen |
| 156 | |
| 157 | const line = buf.toString('utf8', start, end).trim() |
| 158 | start = end + 1 |
| 159 | lineNumber++ |
| 160 | if (!line) continue |
| 161 | try { |
| 162 | results.push(JSON.parse(line) as T) |
| 163 | } catch (e) { |
| 164 | if (strict) { |
| 165 | throw new JSONLParseError(lineNumber, line, e) |
| 166 | } |
| 167 | // Skip malformed lines |
| 168 | } |
| 169 | } |
| 170 | return results |
| 171 | } |
| 172 | |
| 173 | function parseJSONLString<T>(data: string, strict?: boolean): T[] { |
| 174 | const stripped = stripBOM(data) |