| 243 | // Create a stream of JSON chunks (NDJSON style) |
| 244 | const jsonStream = new ReadableStream<string>({ |
| 245 | start(controller) { |
| 246 | callbacks.onParse = (value) => { |
| 247 | controller.enqueue(JSON.stringify(value) + '\n') |
| 248 | } |
| 249 | callbacks.onDone = () => { |
| 250 | try { |
| 251 | controller.close() |
| 252 | } catch { |
| 253 | // Already closed |
| 254 | } |
| 255 | } |
| 256 | callbacks.onError = (error) => controller.error(error) |
| 257 | // Emit initial body if we have one |
| 258 | if (nonStreamingBody !== undefined) { |
| 259 | callbacks.onParse(nonStreamingBody) |
| 260 | } |
| 261 | }, |
| 262 | }) |
| 263 | |
| 264 | // Create multiplexed stream with JSON and raw streams |