| 113 | } |
| 114 | |
| 115 | function parseJSONLBun<T>(data: string | Buffer): T[] { |
| 116 | const parse = bunJSONLParse as BunJSONLParseChunk |
| 117 | const len = data.length |
| 118 | const result = parse(data) |
| 119 | if (!result.error || result.done || result.read >= len) { |
| 120 | return result.values as T[] |
| 121 | } |
| 122 | // Had an error mid-stream — collect what we got and keep going |
| 123 | let values = result.values as T[] |
| 124 | let offset = result.read |
| 125 | while (offset < len) { |
| 126 | const newlineIndex = |
| 127 | typeof data === 'string' |
| 128 | ? data.indexOf('\n', offset) |
| 129 | : data.indexOf(0x0a, offset) |
| 130 | if (newlineIndex === -1) break |
| 131 | offset = newlineIndex + 1 |
| 132 | const next = parse(data, offset) |
| 133 | if (next.values.length > 0) { |
| 134 | values = values.concat(next.values as T[]) |
| 135 | } |
| 136 | if (!next.error || next.done || next.read >= len) break |
| 137 | offset = next.read |
| 138 | } |
| 139 | return values |
| 140 | } |
| 141 | |
| 142 | function parseJSONLBuffer<T>(buf: Buffer, strict?: boolean): T[] { |
| 143 | const bufLen = buf.length |