MCPcopy
hub / github.com/CapSoftware/Cap / readStreamWithLimit

Function readStreamWithLimit

apps/media-server/src/lib/media-audio.ts:56–86  ·  view source on GitHub ↗
(
	stream: ReadableStream<Uint8Array>,
	maxBytes: number,
)

Source from the content-addressed store, hash-verified

54}
55
56async function readStreamWithLimit(
57 stream: ReadableStream<Uint8Array>,
58 maxBytes: number,
59): Promise<string> {
60 const reader = stream.getReader();
61 const chunks: Uint8Array[] = [];
62 let totalBytes = 0;
63
64 try {
65 while (true) {
66 const { done, value } = await reader.read();
67 if (done) break;
68 if (totalBytes < maxBytes) {
69 const remainingBytes = maxBytes - totalBytes;
70 const chunk =
71 value.length > remainingBytes
72 ? value.slice(0, remainingBytes)
73 : value;
74 chunks.push(chunk);
75 totalBytes += chunk.length;
76 }
77 }
78 } finally {
79 reader.releaseLock();
80 }
81
82 const decoder = new TextDecoder();
83 return chunks
84 .map((chunk) => decoder.decode(chunk, { stream: true }))
85 .join("");
86}
87
88function redactUrl(value: string): string {
89 try {

Callers 1

extractAudioFunction · 0.70

Calls 1

readMethod · 0.80

Tested by

no test coverage detected