Download mobile/modules/engine/src/utils/boundedByteStream.ts from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 995 Bytes
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/utils/boundedByteStream.ts
- Command line
-
hf download hf://Anirudh1950/MentraOS/mobile/modules/engine/src/utils/boundedByteStream.ts
-
curl -L -o boundedByteStream.ts https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/utils/boundedByteStream.ts
995 Bytes
| /** Read a byte stream without allowing a chunked response to bypass its size cap. */ | |
| export async function readBoundedByteStream(stream: ReadableStream<Uint8Array>, maxBytes: number): Promise<Uint8Array> { | |
| if (!Number.isSafeInteger(maxBytes) || maxBytes < 1) throw new Error("maxBytes must be a positive integer") | |
| const reader = stream.getReader() | |
| try { | |
| const chunks: Uint8Array[] = [] | |
| let received = 0 | |
| while (true) { | |
| const {done, value} = await reader.read() | |
| if (done) break | |
| received += value.byteLength | |
| if (received > maxBytes) { | |
| await reader.cancel("response exceeds host limit") | |
| throw new Error(`response exceeds ${maxBytes} bytes`) | |
| } | |
| chunks.push(value) | |
| } | |
| const bytes = new Uint8Array(received) | |
| let offset = 0 | |
| for (const chunk of chunks) { | |
| bytes.set(chunk, offset) | |
| offset += chunk.byteLength | |
| } | |
| return bytes | |
| } finally { | |
| reader.releaseLock() | |
| } | |
| } | |