MCPcopy Create free account
hub / github.com/ProtonDriveApps/sdk / readStreamToUint8Array

Function readStreamToUint8Array

client/js/src/internal/upload/streamReader.ts:6–38  ·  view source on GitHub ↗
(
    stream: ReadableStream<Uint8Array<ArrayBufferLike>>,
    signal?: AbortSignal,
)

Source from the content-addressed store, hash-verified

4 * Reads a ReadableStream into a Uint8Array.
5 */
6export async function readStreamToUint8Array(
7 stream: ReadableStream<Uint8Array<ArrayBufferLike>>,
8 signal?: AbortSignal,
9): Promise<Uint8Array<ArrayBuffer>> {
10 const reader = stream.getReader();
11 const chunks: Uint8Array[] = [];
12 let totalLength = 0;
13
14 try {
15 while (true) {
16 const { done, value } = await reader.read();
17 if (done) {
18 break;
19 }
20 if (signal?.aborted) {
21 throw new AbortError();
22 }
23 const chunk = value;
24 totalLength += chunk.length;
25 chunks.push(chunk);
26 }
27
28 const result = new Uint8Array(totalLength);
29 let offset = 0;
30 for (const chunk of chunks) {
31 result.set(chunk, offset);
32 offset += chunk.length;
33 }
34 return result;
35 } finally {
36 reader.releaseLock();
37 }
38}

Callers 3

readStreamContentFunction · 0.90
downloadBlockMethod · 0.90

Calls 1

readMethod · 0.65

Tested by

no test coverage detected