-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.d.ts
169 lines (127 loc) · 4.11 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
export const A: PromiseLike<never>
export const enum QuickReaderErrorCode {
NO_MORE_DATA = 1,
FAILED_TO_PULL,
OUT_OF_RANGE,
MAX_QUEUE_EXCEED,
}
export class QuickReaderError extends Error {
public readonly name = 'QuickReaderError'
public readonly code: QuickReaderErrorCode
}
export class QuickReader<T extends Uint8Array = Uint8Array> {
/**
`stream` can be a NodeStream or WebStream.
*/
public constructor(stream: AsyncIterable<T> | ReadableStream<T>)
/**
Indicates whether the stream is closed and the buffer is empty.
If true, it can no longer be read, otherwise a `QuickReaderError` with
code `NO_MORE_DATA` will be thrown.
Since the property is synchronized, it's meaningless until the first
reading.
*/
public readonly eof: boolean
/**
* If true, the EOF can be used as a delimiter.
* @default false
*/
public eofAsDelim: boolean
/**
Read a chunk from the stream.
*/
public chunk() : Promise<T>
/**
Read data of specified length using async iteration
*/
public chunks(len: number) : AsyncGenerator<T>
/**
Read all data until `len` bytes remaining.
*/
public chunksToEnd(len: number) : AsyncGenerator<T>
/**
Pull a chunk from stream to the buffer.
This method is called at initialization so that eof can be detected
before the first read.
*/
public pull() : Promise<void>
/**
Read *len* bytes.
If the stream cannot provide enough data, a `QuickReaderError` with code
`NO_MORE_DATA` will be thrown.
Note: for shorter bytes it is likely reference to the underlying buffer.
If the result will be used for a long time, it is better to make a copy
before using it, e.g. `result.slice()`; otherwise the underlying buffer
will not be GCed.
*/
public bytes(len: number) : T | undefined
/**
Read the bytes before *delim*. The delimiter will be consumed, but not
included in the result.
If more than `QuickReader.maxQueueLen` (default 64Mi) bytes have been read
before the delimiter is found, a `QuickReaderError` with code
`MAX_QUEUE_EXCEED` will be thrown.
If the `eofAsDelim` property is true, the EOF can be used as a delimiter;
otherwise, a `QuickReaderError` with code `NO_MORE_DATA` will be thrown if
the delimiter is not found after reading all the data.
Like the `bytes` method, this method also needs to consider the GC issue.
*/
public bytesTo(delim: number) : T | undefined
/**
Read *len* bytes and return the length.
Similar to the `bytes` method, but faster.
*/
public skip(len: number) : number | undefined
/**
Read to *delim* and return the length (including the delimiter).
Similar to the `bytesTo` method, but faster.
*/
public skipTo(delim: number) : number | undefined
/**
Read *len* bytes and convert to UTF-8 string.
Similar to `bytes` method.
*/
public txtNum(len: number) : string | undefined
/**
Read the bytes before *delim* and convert to UTF-8 string.
Similar to `bytesTo` method.
*/
public txtTo(delim: number) : string | undefined
/**
Read the bytes before '\0' and convert to UTF-8 string.
Equivalent to `txtTo(0)`.
*/
public txt() : string | undefined
/**
Read the bytes before '\n' and convert to UTF-8 string.
Equivalent to `txtTo(10)`.
*/
public txtLn() : string | undefined
/**
Read a byte.
*/
public u8() : number | undefined
public i8() : number | undefined
/**
Read a number, little-endian.
*/
public u16() : number | undefined
public i16() : number | undefined
public u32() : number | undefined
public i32() : number | undefined
public u64() : bigint | undefined
public i64() : bigint | undefined
public f64() : number | undefined
public f32() : number | undefined
/**
Read a number, big-endian.
*/
public u16be() : number | undefined
public i16be() : number | undefined
public u32be() : number | undefined
public i32be() : number | undefined
public u64be() : bigint | undefined
public i64be() : bigint | undefined
public f64be() : number | undefined
public f32be() : number | undefined
}