MCPcopy
hub / github.com/coder/mux / streamToString

Function streamToString

src/node/runtime/streamUtils.ts:23–45  ·  view source on GitHub ↗
(stream: ReadableStream<Uint8Array>)

Source from the content-addressed store, hash-verified

21 * Used by SSH and Docker runtimes for capturing command output.
22 */
23export async function streamToString(stream: ReadableStream<Uint8Array>): Promise<string> {
24 const reader = stream.getReader();
25 const decoder = new TextDecoder("utf-8");
26 // Collect decoded chunks into an array and join at the end.
27 // Using += would build a deep V8 ConsString rope; subsequent regex/indexOf
28 // on that rope dereferences one pointer per character, causing O(n²)-class
29 // hangs on large newline-free payloads (e.g. minified CSS from web_fetch).
30 const chunks: string[] = [];
31
32 try {
33 while (true) {
34 const { done, value } = await reader.read();
35 if (done) break;
36 chunks.push(decoder.decode(value, { stream: true }));
37 }
38 // Final flush
39 const tail = decoder.decode();
40 if (tail) chunks.push(tail);
41 return chunks.join("");
42 } finally {
43 reader.releaseLock();
44 }
45}

Callers 15

execBufferedFunction · 0.90
readFileStringFunction · 0.90
generateMethod · 0.90
readFileFunction · 0.90
writeFileFunction · 0.90
ensureDirFunction · 0.90
statFunction · 0.90
setupCredentialsMethod · 0.90
fetchRemoteHomeMethod · 0.90
readFileViaExecMethod · 0.90
writeFileViaExecMethod · 0.90
ensureDirViaExecMethod · 0.90

Calls 2

pushMethod · 0.65
readMethod · 0.45

Tested by

no test coverage detected