(
stream: ReadableStream<Uint8Array<ArrayBufferLike>>,
signal?: AbortSignal,
)
| 4 | * Reads a ReadableStream into a Uint8Array. |
| 5 | */ |
| 6 | export 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 | } |
no test coverage detected