MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / readExact

Function readExact

crates/opencode-plugin/host/plugin-host.ts:87–114  ·  view source on GitHub ↗

* Read exactly `n` bytes from stdin.

(n: number)

Source from the content-addressed store, hash-verified

85 * Read exactly `n` bytes from stdin.
86 */
87async 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.

Callers 1

readMessageFunction · 0.85

Calls 2

readMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected