-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_all.ts
32 lines (31 loc) · 1 KB
/
read_all.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
import { concat } from "@std/bytes";
import { collect } from "./collect.ts";
/**
* Reads all available bytes from a given `ReadableStream<Uint8Array>` and concatenates them into a single `Uint8Array`.
*
* ```ts
* import { assertEquals } from "@std/assert";
* import { readAll } from "@core/streamutil/read-all";
*
* const encoder = new TextEncoder();
* const stream = new ReadableStream({
* start(controller) {
* controller.enqueue(encoder.encode("Hello"));
* controller.enqueue(encoder.encode("World"));
* controller.close();
* },
* });
* const result = await readAll(stream);
* assertEquals(result, encoder.encode("HelloWorld"));
* ```
*
* @param stream The stream to read from.
* @returns A promise that resolves to a `Uint8Array` containing all the bytes read from the stream.
*/
export async function readAll(
stream: ReadableStream<Uint8Array>,
options: StreamPipeOptions = {},
): Promise<Uint8Array> {
const chunks = await collect(stream, options);
return concat(chunks);
}