-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.d.ts
90 lines (82 loc) · 2.55 KB
/
types.d.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
* Copyright (C) Oliver Lenehan (sunsetkookaburra), 2022 */
/** A destination for stream data, such as a `Response` body. */
export interface Sink<T> {
readonly writable: WritableStream<T>;
}
/** A source of stream data such as a `Request` body. */
export interface Source<T> {
readonly readable: ReadableStream<T>;
}
/** Convenience type on `Sink<I> & Source<O>`.
* Distinguished from `TransformStream<I, O>` by `O` defaulting
* to `I`.
*
* Both a source of stream data such as a `Request` body,
* and a sink of stream data such as a `Response` body. */
export interface Basin<I, O = I> extends Sink<I>, Source<O> {}
/** An `Enc<T>` implements a binary data encoder
* which can write data of type `T`. It may also
* provide a `label` for debugging purposes.
*
* ```ts
* const U8: Enc<number> = {
* writeTo: async (sink: Sink<Uint8Array>, value: number) => {
* await write(sink, new Uint8Array([value]));
* },
* };
* const buf = new Buffer();
* await MyCodec.writeTo(buf, 42);
*
* buf.bytes()[0] === 42; // true
* ```
*/
export interface Enc<T> {
readonly label?: string,
readonly writeTo: (sink: Sink<Uint8Array>, value: T) => Promise<void>;
}
/** A `Dec<T>` implements a binary data decoder
* which can read data of type `T`. It may also
* provide a `label` for debugging purposes.
*
* ```ts
* const U8: Dec<number> = {
* readFrom: async (sink: Sink<Uint8Array>) => {
* return (await readBytes(sink, 1))[0]
* },
* };
* const buf = new Buffer([42]);
*
* await U8.readFrom(buf) === 42; // true
* ```
*/
export interface Dec<T> {
readonly label?: string,
readonly readFrom: (source: Source<Uint8Array>) => Promise<T>;
}
/** A convenience type on `Enc<T> & Dec<U>`,
* a `Codec<T>` implements a binary data encoder and decoder
* which can read data of type `U` and write data of type `T`.
* It may also provide a `label` for debugging purposes.
*
* ```ts
* const U8: Codec<number> = {
* writeTo: async (sink: Sink<Uint8Array>, value: number) => {
* await write(sink, new Uint8Array([value]));
* },
* readFrom: async (sink: Sink<Uint8Array>) => {
* return (await readBytes(sink, 1))[0]
* },
* };
*
* const buf = new Buffer();
*
* await U8.writeTo(buf, 42);
* buf.bytes()[0] === 42; // true
*
* await U8.readFrom(buf) === 42; // true
* ```
*/
export interface Codec<T, U = T> extends Enc<T>, Dec<U> {}