* Read one Content-Length framed JSON-RPC message from stdin. * Returns null on EOF.
()
| 118 | * Returns null on EOF. |
| 119 | */ |
| 120 | async function readMessage(): Promise<JsonRpcRequest | null> { |
| 121 | // Read header lines until empty line |
| 122 | let header = ""; |
| 123 | while (true) { |
| 124 | const byte = await readExact(1); |
| 125 | if (byte.length === 0) return null; |
| 126 | header += decoder.decode(byte); |
| 127 | if (header.endsWith("\r\n\r\n")) break; |
| 128 | } |
| 129 | |
| 130 | const match = header.match(/Content-Length:\s*(\d+)/i); |
| 131 | if (!match) { |
| 132 | throw new Error(`Invalid header: ${header}`); |
| 133 | } |
| 134 | |
| 135 | const contentLength = parseInt(match[1], 10); |
| 136 | const body = await readExact(contentLength); |
| 137 | return JSON.parse(decoder.decode(body)); |
| 138 | } |
| 139 | |
| 140 | function send(response: JsonRpcResponse): void { |
| 141 | process.stdout.write(encodeResponse(response)); |