| 113 | } |
| 114 | |
| 115 | private readStream(): void { |
| 116 | if (!this.proc?.stdout) return; |
| 117 | |
| 118 | this.proc.stdout.on("data", (chunk: Buffer) => { |
| 119 | this.buffer += chunk.toString(); |
| 120 | const lines = this.buffer.split("\n"); |
| 121 | this.buffer = lines.pop() ?? ""; |
| 122 | |
| 123 | for (const line of lines) { |
| 124 | const trimmed = line.replace(/\r$/, ""); |
| 125 | if (!trimmed) continue; |
| 126 | try { |
| 127 | const parsed = JSON.parse(trimmed); |
| 128 | this.routeMessage(parsed); |
| 129 | } catch { |
| 130 | // Ignore malformed lines |
| 131 | } |
| 132 | } |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | private routeMessage(msg: Record<string, unknown>): void { |
| 137 | if (msg.type === "response" && typeof msg.id === "string") { |