|
| 1 | +{{>partial_header}} |
| 2 | + |
| 3 | +import type { Readable } from "node:stream"; |
| 4 | + |
| 5 | +// Helper: create async iterable from classic EventEmitter-style Readable streams |
| 6 | +const createAsyncIterableFromReadable = (readable: any): AsyncIterable<any> => { |
| 7 | + return { |
| 8 | + [Symbol.asyncIterator](): AsyncIterator<any> { |
| 9 | + const chunkQueue: any[] = []; |
| 10 | + const pendings: Array<{ resolve: (v: IteratorResult<any>) => void; reject: (e?: any) => void }> = []; |
| 11 | + let ended = false; |
| 12 | + let error: any = null; |
| 13 | + |
| 14 | + const onData = (chunk: any) => { |
| 15 | + if (pendings.length > 0) { |
| 16 | + const { resolve } = pendings.shift()!; |
| 17 | + resolve({ value: chunk, done: false }); |
| 18 | + } else { |
| 19 | + chunkQueue.push(chunk); |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + const onEnd = () => { |
| 24 | + if (error) return; // Don't process end if error already occurred |
| 25 | + ended = true; |
| 26 | + while (pendings.length > 0) { |
| 27 | + const { resolve } = pendings.shift()!; |
| 28 | + resolve({ value: undefined, done: true }); |
| 29 | + } |
| 30 | + }; |
| 31 | +
|
| 32 | + const onError = (err: any) => { |
| 33 | + error = err; |
| 34 | + while (pendings.length > 0) { |
| 35 | + const { reject } = pendings.shift()!; |
| 36 | + reject(err); |
| 37 | + } |
| 38 | + cleanup(); |
| 39 | + }; |
| 40 | +
|
| 41 | + readable.on("data", onData); |
| 42 | + readable.once("end", onEnd); |
| 43 | + readable.once("error", onError); |
| 44 | +
|
| 45 | + const cleanup = () => { |
| 46 | + readable.off("data", onData); |
| 47 | + readable.off("end", onEnd); |
| 48 | + readable.off("error", onError); |
| 49 | + }; |
| 50 | +
|
| 51 | + return { |
| 52 | + next(): Promise<IteratorResult<any>> { |
| 53 | + if (error) { |
| 54 | + return Promise.reject(error); |
| 55 | + } |
| 56 | + if (chunkQueue.length > 0) { |
| 57 | + const value = chunkQueue.shift(); |
| 58 | + return Promise.resolve({ value, done: false }); |
| 59 | + } |
| 60 | + if (ended) { |
| 61 | + cleanup(); |
| 62 | + return Promise.resolve({ value: undefined, done: true }); |
| 63 | + } |
| 64 | + return new Promise<IteratorResult<any>>((resolve, reject) => { |
| 65 | + pendings.push({ resolve, reject }); |
| 66 | + }); |
| 67 | + }, |
| 68 | + return(): Promise<IteratorResult<any>> { |
| 69 | + try { |
| 70 | + cleanup(); |
| 71 | + } finally { |
| 72 | + if (readable && typeof readable.destroy === "function") { |
| 73 | + readable.destroy(); |
| 74 | + } |
| 75 | + } |
| 76 | + return Promise.resolve({ value: undefined, done: true }); |
| 77 | + }, |
| 78 | + throw(e?: any): Promise<IteratorResult<any>> { |
| 79 | + try { |
| 80 | + cleanup(); |
| 81 | + } finally { |
| 82 | + if (readable && typeof readable.destroy === "function") { |
| 83 | + readable.destroy(e); |
| 84 | + } |
| 85 | + } |
| 86 | + return Promise.reject(e); |
| 87 | + } |
| 88 | + }; |
| 89 | + } |
| 90 | + }; |
| 91 | +}; |
| 92 | +
|
| 93 | +/** |
| 94 | + * Parse newline-delimited JSON (NDJSON) from a Node.js readable stream |
| 95 | + * @param stream - Node.js readable stream, AsyncIterable, string, or Buffer |
| 96 | + * @returns AsyncGenerator that yields parsed JSON objects |
| 97 | + */ |
| 98 | +export async function* parseNDJSONStream( |
| 99 | + stream: Readable | AsyncIterable<Uint8Array | string | Buffer> | string | Uint8Array | Buffer |
| 100 | +): AsyncGenerator<any> { |
| 101 | + const decoder = new TextDecoder("utf-8"); |
| 102 | + let buffer = ""; |
| 103 | +
|
| 104 | + // If stream is actually a string or Buffer-like, handle as whole payload |
| 105 | + const isString = typeof stream === "string"; |
| 106 | + const isBuffer = typeof Buffer !== "undefined" && Buffer.isBuffer && Buffer.isBuffer(stream); |
| 107 | + const isUint8Array = typeof Uint8Array !== "undefined" && stream instanceof Uint8Array; |
| 108 | +
|
| 109 | + if (isString || isBuffer || isUint8Array) { |
| 110 | + const text = isString |
| 111 | + ? (stream as string) |
| 112 | + : decoder.decode(isBuffer ? new Uint8Array(stream as Buffer) : (stream as Uint8Array)); |
| 113 | + const lines = text.split("\n"); |
| 114 | +
|
| 115 | + for (const line of lines) { |
| 116 | + const trimmed = line.trim(); |
| 117 | + if (!trimmed) { |
| 118 | + continue; |
| 119 | + } |
| 120 | +
|
| 121 | + try { |
| 122 | + yield JSON.parse(trimmed); |
| 123 | + } catch (err) { |
| 124 | + console.warn("Failed to parse JSON line:", err); |
| 125 | + } |
| 126 | + } |
| 127 | + return; |
| 128 | + } |
| 129 | +
|
| 130 | + const isAsyncIterable = stream && typeof (stream as any)[Symbol.asyncIterator] === "function"; |
| 131 | + const source: AsyncIterable<any> = isAsyncIterable ? (stream as any) : createAsyncIterableFromReadable(stream as any); |
| 132 | +
|
| 133 | + for await (const chunk of source) { |
| 134 | + // Node.js streams can return Buffer or string chunks |
| 135 | + // Convert to Uint8Array if needed for TextDecoder |
| 136 | + const uint8Chunk = typeof chunk === "string" |
| 137 | + ? new TextEncoder().encode(chunk) |
| 138 | + : chunk instanceof Buffer |
| 139 | + ? new Uint8Array(chunk) |
| 140 | + : chunk; |
| 141 | +
|
| 142 | + // Append decoded chunk to buffer |
| 143 | + buffer += decoder.decode(uint8Chunk, { stream: true }); |
| 144 | +
|
| 145 | + // Split on newlines |
| 146 | + const lines = buffer.split("\n"); |
| 147 | +
|
| 148 | + // Keep the last (potentially incomplete) line in the buffer |
| 149 | + buffer = lines.pop() || ""; |
| 150 | +
|
| 151 | + // Parse and yield complete lines |
| 152 | + for (const line of lines) { |
| 153 | + const trimmed = line.trim(); |
| 154 | + if (trimmed) { |
| 155 | + try { |
| 156 | + yield JSON.parse(trimmed); |
| 157 | + } catch (err) { |
| 158 | + console.warn("Failed to parse JSON line:", err); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +
|
| 164 | + // Flush any remaining decoder state |
| 165 | + buffer += decoder.decode(); |
| 166 | +
|
| 167 | + // Handle any remaining data in buffer |
| 168 | + if (buffer.trim()) { |
| 169 | + try { |
| 170 | + yield JSON.parse(buffer); |
| 171 | + } catch (err) { |
| 172 | + console.warn("Failed to parse final JSON buffer:", err); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | +
|
0 commit comments