Parse every JSON-RPC message the server writes to stdout into an array.
(child: ChildProcessWithoutNullStreams)
| 34 | |
| 35 | /** Parse every JSON-RPC message the server writes to stdout into an array. */ |
| 36 | function collectMessages(child: ChildProcessWithoutNullStreams): Array<Record<string, any>> { |
| 37 | const messages: Array<Record<string, any>> = []; |
| 38 | let buf = ''; |
| 39 | child.stdout.on('data', (chunk) => { |
| 40 | buf += chunk.toString('utf8'); |
| 41 | let idx; |
| 42 | while ((idx = buf.indexOf('\n')) !== -1) { |
| 43 | const line = buf.slice(0, idx).trim(); |
| 44 | buf = buf.slice(idx + 1); |
| 45 | if (!line) continue; |
| 46 | try { messages.push(JSON.parse(line)); } catch { /* ignore non-JSON */ } |
| 47 | } |
| 48 | }); |
| 49 | return messages; |
| 50 | } |
| 51 | |
| 52 | function waitForMessage( |
| 53 | messages: ReadonlyArray<Record<string, any>>, |