-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.d.ts
233 lines (210 loc) · 7.87 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { Stream, TransformOptions, Transform } from 'stream';
export interface Options extends TransformOptions {
/**
* The maximum number of concurrent promises that are allowed.
* When this limit is reached, the stream will stop processing data and will
* start buffering incoming objects. Defaults to `1`
*/
concurrent: number;
}
export interface PromiseStream<T> extends Transform {
/**
* Like `this.push` in [through2](//github.com/rvagg/through2), but takes promise
* arguments. It returns a promise that resolves when the pushed promise resolves,
* to make it possible to use `return this.push(data)`
*/
push(val: T | Promise<T>): void;
push(chunk: any, encoding?: string): boolean;
/**
* Creates a new mapping promise stream and pipes this promise stream to it.
*/
map(
opts: Options,
fn: (data: T, enc?: string) => T | Promise<T>,
end?: () => void | Promise<void>
): PromiseStream<T>;
/**
* Creates a new mapping promise stream and pipes this promise stream to it.
*/
map(
fn: (data: T, enc?: string) => T | Promise<T>,
end?: () => void | Promise<void>
): PromiseStream<T>;
/**
* Creates a filtering promise stream
* @param opts - the Options for the promise stream. Transform options are passed to the node
* Transform constructor
* @param fn - function should return a boolean to indicate whether the data value should pass to
* the next stream
* @param end callback to be called when the source stream completes
*/
filter(
opts: Options,
fn: (data: T, enc?: string) => boolean | Promise<boolean>,
end?: () => void | Promise<void>
): PromiseStream<T>;
filter(
fn: (data: T, enc?: string) => boolean | Promise<boolean>,
end?: () => void | Promise<void>
): PromiseStream<T>;
/**
* Reduces the objects in this promise stream. The function takes the resolved
* current accumulator and data object and should return the next accumulator
* or a promise for the next accumulator.
*
* Example:
*
* ```js
* process.stdin.pipe(split()).pipe(es.reduce(function(acc, el) {
* return acc + el;
* })).promise().then(function(sum) {
*
* });
* ```
*
* @param opts - the Options for the promise stream. Transform options are passed to the node
* Transform constructor
* @param fn - A function that takes the resolved current accumulator
* and data object and should return the next accumulator
* or a promise for the next accumulator
* @param end
*/
reduce<U>(
opts: Options,
fn: (acc: U, data: T, enc?: string) => U | Promise<U>,
end?: () => void | Promise<void>
): ReducePromiseStream<U>;
reduce<U>(
fn: (acc: U, data: T, enc?: string) => U | Promise<U>,
end?: () => void | Promise<void>
): ReducePromiseStream<U>;
/**
*
* Returns a promise fulfilled at the end of the stream, rejected if any errors
* events are emitted by the stream.
*
* For ReducePromiseStreams, the promise is for the final reduction result. Any
* stream errors or exceptions encountered while reducing will result with a
* rejection of the promise.
*/
promise(): Promise<any>;
}
export interface ReducePromiseStream<T> extends PromiseStream<T> {
/**
* The final accumulator value. Any
* stream errors or exceptions encountered while reducing will result with a
* rejection of the promise.
*/
promise(): Promise<T>;
}
/**
* Create a through-promise stream. Pass it a function that takes data and
* encoding and uses `this.push` to push values or promises. This function should
* return a promise that indicates when the object/chunk are fully processed.
* @param opts - the Options for the promise stream. Transform options are passed to the node
* Transform constructor
* @param fn - callback that takes data and encoding and uses this.push to push values/promises.
* Returns a promise indicating when the processing of that chunk has fully completed
* @param end - callback to be called when the source stream completes.
* @return A promise stream.
*/
export function through<T>(
opts: Options,
fn: (this: PromiseStream<T>, data: T, enc?: string) => Promise<any>,
end?: () => Promise<any>
): PromiseStream<T>;
export function through<T>(
fn: (this: PromiseStream<T>, data: T, enc?: string) => Promise<any>,
end?: () => Promise<any>
): PromiseStream<T>;
/**
* Create a new MapPromiseStream. The function
* @param opts - the Options for the promise stream. Transform options are passed to the node
* Transform constructor
* @param fn - function that returns a promise for the next object that will be pushed to the stream.
* @param end - callback to be called when the source stream completes
*/
export function map<T>(
opts: Options,
fn: (data: T, enc?: string) => T | Promise<T>,
end?: () => void | Promise<void>
): PromiseStream<T>;
export function map<T>(
fn: (data: T, enc?: string) => T | Promise<T>,
end?: () => void | Promise<void>
): PromiseStream<T>;
/**
* Creates a filtering promise stream
* @param opts - the Options for the promise stream. Transform options are passed to the node
* Transform constructor
* @param fn - function should return a boolean to indicate whether the data value should pass to the
* next stream
* @param end - callback to be called when the source stream completes
*/
export function filter<T>(
opts: Options,
fn: (data: T, enc?: string) => boolean | Promise<boolean>,
end?: () => void | Promise<void>
): PromiseStream<T>;
export function filter<T>(
fn: (data: T, enc?: string) => boolean | Promise<boolean>,
end?: () => void | Promise<void>
): PromiseStream<T>;
/**
* Reduces the objects in this promise stream. The function takes the resolved
* current accumulator and data object and should return the next accumulator
* or a promise for the next accumulator.
*
* Example:
*
* ```js
* process.stdin.pipe(split()).pipe(es.reduce(function(acc, el) {
* return acc + el;
* })).promise().then(function(sum) {
*
* });
* ```
*
* @param opts - the Options for the promise stream. Transform options are passed to the node
* Transform constructor
* @param fn - A function that takes the resolved current accumulator
* and data object and should return the next accumulator
* or a promise for the next accumulator
* @param end
*/
export function reduce<T, U>(
opts: Options,
fn: (acc: U, data: T, enc?: string) => U | Promise<U>,
end?: () => void | Promise<void>
): ReducePromiseStream<U>;
export function reduce<T, U>(
fn: (acc: U, data: T, enc?: string) => U | Promise<U>,
end?: () => void | Promise<void>
): ReducePromiseStream<U>;
/**
* Waits for a stream to end, capturing any errors
* @param s - the stream to wait for
*/
export function wait(s: Stream): Promise<void>;
/**
* Collects all data from a stream into a single buffer or string, depending on the encoding of
* the passed stream.
* @param s - the stream to collect
*/
export function collect(s: Stream): Promise<Buffer | string>;
/**
* Pipes the source stream to the destination stream and forwards all errors to the resulting
* promise. The promise is fulfilled when the source stream ends.
*
* @param source - the source stream
* @param destination - the destination stream
*/
export function pipe(source: Stream, destination: Stream): Promise<void>;
/**
* Creates a pipeline by piping the source stream through multiple trasnform streams. Forwards all
* errors to the resulting promise, which is resolved when all the transform streams complete.
* @param source - stream that emits the source of data
* @param destinations - series of transform streams ending with a sink through which the data
* should be piped.
*/
export function pipeline(source: Stream, ...destinations: Stream[]): Promise<void>;