* Read exactly `n` bytes from stdin.
(n: number)
| 85 | * Read exactly `n` bytes from stdin. |
| 86 | */ |
| 87 | async function readExact(n: number): Promise<Uint8Array> { |
| 88 | const chunks: Uint8Array[] = []; |
| 89 | let remaining = n; |
| 90 | |
| 91 | const reader = process.stdin as unknown as { |
| 92 | read(size: number): Uint8Array | null; |
| 93 | once(event: string, cb: () => void): void; |
| 94 | }; |
| 95 | |
| 96 | while (remaining > 0) { |
| 97 | const chunk: Uint8Array | null = reader.read(remaining); |
| 98 | if (chunk !== null) { |
| 99 | chunks.push(chunk); |
| 100 | remaining -= chunk.length; |
| 101 | } else { |
| 102 | await new Promise<void>((resolve) => reader.once("readable", resolve)); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (chunks.length === 1) return chunks[0]; |
| 107 | const result = new Uint8Array(n); |
| 108 | let offset = 0; |
| 109 | for (const c of chunks) { |
| 110 | result.set(c, offset); |
| 111 | offset += c.length; |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Read one Content-Length framed JSON-RPC message from stdin. |
no test coverage detected