-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathdecodeAsync.ts
44 lines (41 loc) · 1.8 KB
/
decodeAsync.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
39
40
41
42
43
44
import { Decoder } from "./Decoder.ts";
import { ensureAsyncIterable } from "./utils/stream.ts";
import type { DecoderOptions } from "./Decoder.ts";
import type { ReadableStreamLike } from "./utils/stream.ts";
import type { SplitUndefined } from "./context.ts";
/**
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export async function decodeAsync<ContextType = undefined>(
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
options?: DecoderOptions<SplitUndefined<ContextType>>,
): Promise<unknown> {
const stream = ensureAsyncIterable(streamLike);
const decoder = new Decoder(options);
return decoder.decodeAsync(stream);
}
/**
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export function decodeArrayStream<ContextType>(
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
options?: DecoderOptions<SplitUndefined<ContextType>>,
): AsyncGenerator<unknown, void, unknown> {
const stream = ensureAsyncIterable(streamLike);
const decoder = new Decoder(options);
return decoder.decodeArrayStream(stream);
}
/**
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export function decodeMultiStream<ContextType>(
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
options?: DecoderOptions<SplitUndefined<ContextType>>,
): AsyncGenerator<unknown, void, unknown> {
const stream = ensureAsyncIterable(streamLike);
const decoder = new Decoder(options);
return decoder.decodeStream(stream);
}