| 102 | })() |
| 103 | |
| 104 | function parseJSONLBun<T>(data: string | Buffer): T[] { |
| 105 | const parse = bunJSONLParse as BunJSONLParseChunk |
| 106 | const len = data.length |
| 107 | const result = parse(data) |
| 108 | if (!result.error || result.done || result.read >= len) { |
| 109 | return result.values as T[] |
| 110 | } |
| 111 | // Had an error mid-stream — collect what we got and keep going |
| 112 | let values = result.values as T[] |
| 113 | let offset = result.read |
| 114 | while (offset < len) { |
| 115 | const newlineIndex = |
| 116 | typeof data === 'string' |
| 117 | ? data.indexOf('\n', offset) |
| 118 | : data.indexOf(0x0a, offset) |
| 119 | if (newlineIndex === -1) break |
| 120 | offset = newlineIndex + 1 |
| 121 | const next = parse(data, offset) |
| 122 | if (next.values.length > 0) { |
| 123 | values = values.concat(next.values as T[]) |
| 124 | } |
| 125 | if (!next.error || next.done || next.read >= len) break |
| 126 | offset = next.read |
| 127 | } |
| 128 | return values |
| 129 | } |
| 130 | |
| 131 | function parseJSONLBuffer<T>(buf: Buffer): T[] { |
| 132 | const bufLen = buf.length |