-
Notifications
You must be signed in to change notification settings - Fork 577
/
to_array_buffer.ts
38 lines (32 loc) · 1011 Bytes
/
to_array_buffer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { concat } from "../bytes/concat.ts";
/**
* Converts a {@linkcode ReadableStream} of {@linkcode Uint8Array}s to an
* {@linkcode ArrayBuffer}. Works the same as{@linkcode Response.arrayBuffer}.
*
* @example
* ```ts
* import { toArrayBuffer } from "https://deno.land/std@$STD_VERSION/streams/to_array_buffer.ts";
*
* const stream = ReadableStream.from([
* new Uint8Array([1, 2]),
* new Uint8Array([3, 4]),
* ]);
* await toArrayBuffer(stream); // ArrayBuffer { [Uint8Contents]: <01 02 03 04>, byteLength: 4 }
* ```
*/
export async function toArrayBuffer(
readableStream: ReadableStream<Uint8Array>,
): Promise<ArrayBuffer> {
const reader = readableStream.getReader();
const chunks: Uint8Array[] = [];
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
chunks.push(value);
}
return concat(chunks).buffer;
}