| 53 | reject(new Error(`timeout waiting for response id=${msg.id}`)); |
| 54 | }, timeoutMs); |
| 55 | const onData = (chunk: Buffer) => { |
| 56 | buf += chunk.toString(); |
| 57 | let idx: number; |
| 58 | while ((idx = buf.indexOf('\n')) !== -1) { |
| 59 | const line = buf.slice(0, idx).trim(); |
| 60 | buf = buf.slice(idx + 1); |
| 61 | if (!line) continue; |
| 62 | try { |
| 63 | const parsed = JSON.parse(line) as Record<string, unknown>; |
| 64 | if (parsed.id === msg.id) { |
| 65 | clearTimeout(timer); |
| 66 | child.stdout.off('data', onData); |
| 67 | resolve(parsed); |
| 68 | return; |
| 69 | } |
| 70 | } catch { |
| 71 | // non-JSON noise on stdout — ignore |
| 72 | } |
| 73 | } |
| 74 | }; |
| 75 | child.stdout.on('data', onData); |
| 76 | child.stdin.write(JSON.stringify({ jsonrpc: '2.0', ...msg }) + '\n'); |
| 77 | }); |