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