UNPKG

17.8 kBTypeScriptView Raw
1// Type definitions for readable-stream 2.3
2// Project: https://github.com/nodejs/readable-stream
3// Definitions by: TeamworkGuy2 <https://github.com/TeamworkGuy2>
4// markdreyer <https://github.com/markdreyer>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6// TypeScript Version: 2.3
7
8/// <reference types="node" />
9
10import * as SafeBuffer from "safe-buffer";
11
12declare class StringDecoder {
13 constructor(encoding?: BufferEncoding | string);
14 write(buffer: Buffer): string;
15 end(buffer?: Buffer): string;
16}
17
18declare class _Readable {
19 readable: boolean;
20 readonly readableFlowing: boolean | null;
21 readonly readableHighWaterMark: number;
22 readonly readableLength: number;
23 _read(size: number): void;
24 read(size?: number): any;
25 setEncoding(encoding: string): this;
26 pause(): this;
27 resume(): this;
28 isPaused(): boolean;
29 unpipe(destination?: _Readable.Writable): this;
30 unshift(chunk: any): void;
31 wrap(oldStream: _Readable.Readable): this;
32 push(chunk: any, encoding?: string): boolean;
33 _destroy(error: Error | null, callback: (error: Error | null) => void): void;
34 destroy(error?: Error): void;
35
36 /**
37 * Event emitter
38 * The defined events on documents including:
39 * 1. close
40 * 2. data
41 * 3. end
42 * 4. readable
43 * 5. error
44 */
45 addListener(event: "close", listener: () => void): this;
46 addListener(event: "data", listener: (chunk: any) => void): this;
47 addListener(event: "end", listener: () => void): this;
48 addListener(event: "readable", listener: () => void): this;
49 addListener(event: "error", listener: (err: Error) => void): this;
50 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
51
52 emit(event: "close"): boolean;
53 emit(event: "data", chunk: any): boolean;
54 emit(event: "end"): boolean;
55 emit(event: "readable"): boolean;
56 emit(event: "error", err: Error): boolean;
57 emit(event: string | symbol, ...args: any[]): boolean;
58
59 on(event: "close", listener: () => void): this;
60 on(event: "data", listener: (chunk: any) => void): this;
61 on(event: "end", listener: () => void): this;
62 on(event: "readable", listener: () => void): this;
63 on(event: "error", listener: (err: Error) => void): this;
64 on(event: string | symbol, listener: (...args: any[]) => void): this;
65
66 once(event: "close", listener: () => void): this;
67 once(event: "data", listener: (chunk: any) => void): this;
68 once(event: "end", listener: () => void): this;
69 once(event: "readable", listener: () => void): this;
70 once(event: "error", listener: (err: Error) => void): this;
71 once(event: string | symbol, listener: (...args: any[]) => void): this;
72
73 prependListener(event: "close", listener: () => void): this;
74 prependListener(event: "data", listener: (chunk: any) => void): this;
75 prependListener(event: "end", listener: () => void): this;
76 prependListener(event: "readable", listener: () => void): this;
77 prependListener(event: "error", listener: (err: Error) => void): this;
78 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
79
80 prependOnceListener(event: "close", listener: () => void): this;
81 prependOnceListener(event: "data", listener: (chunk: any) => void): this;
82 prependOnceListener(event: "end", listener: () => void): this;
83 prependOnceListener(event: "readable", listener: () => void): this;
84 prependOnceListener(event: "error", listener: (err: Error) => void): this;
85 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
86
87 removeListener(event: "close", listener: () => void): this;
88 removeListener(event: "data", listener: (chunk: any) => void): this;
89 removeListener(event: "end", listener: () => void): this;
90 removeListener(event: "readable", listener: () => void): this;
91 removeListener(event: "error", listener: (err: Error) => void): this;
92 removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
93
94 [Symbol.asyncIterator](): AsyncIterableIterator<any>;
95
96 // static ReadableState: _Readable.ReadableState;
97 _readableState: _Readable.ReadableState;
98 destroyed: boolean;
99
100 constructor(options?: _Readable.ReadableOptions);
101
102 _undestroy(): void;
103}
104
105declare namespace _Readable {
106 // ==== BufferList ====
107 interface Entry<D> {
108 data: D;
109 next: Entry<D> | null;
110 }
111
112 interface BufferList<D extends SafeBuffer.Buffer = SafeBuffer.Buffer> {
113 head: Entry<D>;
114 tail: Entry<D>;
115 length: number;
116
117 push(v: D): void;
118 unshift(v: D): void;
119 shift(): D;
120 clear(): void;
121 join(s: any): string;
122 concat(n: number): D;
123 }
124
125 // ==== destroy ====
126 interface Destroy {
127 destroy(this: Readable | Writable, error: Error | null, callback?: (error: Error | null) => void): Readable | Writable;
128 undestroy(this: Readable | Writable): void;
129 }
130
131 // ==== _stream_duplex ====
132 type DuplexOptions = ReadableOptions & WritableOptions & {
133 allowHalfOpen?: boolean | undefined;
134 readable?: boolean | undefined;
135 writable?: boolean | undefined;
136 read?(this: Duplex, size: number): void;
137 write?(this: Duplex, chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
138 writev?(this: Duplex, chunks: Array<{ chunk: any, encoding: BufferEncoding }>, callback: (error?: Error | null) => void): void;
139 final?(this: Duplex, callback: (error?: Error | null) => void): void;
140 destroy?(this: Duplex, error: Error | null, callback: (error: Error | null) => void): void;
141 };
142
143 class Duplex extends Writable implements /*extends*/_Readable, Duplex {
144 /**
145 * This is a dummy function required to retain type compatibility to node.
146 * @deprecated DO NOT USE
147 */
148 static from(source: any): any;
149 allowHalfOpen: boolean;
150 destroyed: boolean;
151 // Readable
152 readable: boolean;
153 readonly readableEncoding: BufferEncoding | null;
154 readonly readableEnded: boolean;
155 readonly readableFlowing: boolean | null;
156 readonly readableHighWaterMark: number;
157 readonly readableLength: number;
158 readonly readableObjectMode: boolean;
159 readonly writableObjectMode: boolean;
160 _readableState: ReadableState;
161
162 _read(size?: number): void;
163 read(size?: number): any;
164 setEncoding(enc: BufferEncoding): this;
165 resume(): this;
166 pause(): this;
167 isPaused(): boolean;
168 unpipe(dest?: Writable): this;
169 unshift(chunk: any): boolean;
170 wrap(oldStream: Readable): this;
171 push(chunk: any, encoding?: BufferEncoding): boolean;
172 _destroy(err: Error | null, callback: (error: Error | null) => void): void;
173 destroy(err?: Error, callback?: (error: Error | null) => void): this;
174 pipe<S extends Writable>(dest: S, pipeOpts?: { end?: boolean | undefined }): S;
175 addListener(ev: string | symbol, fn: (...args: any[]) => void): this;
176 on(ev: string | symbol, fn: (...args: any[]) => void): this;
177
178 _undestroy(): void;
179 [Symbol.asyncIterator](): AsyncIterableIterator<any>;
180 // end-Readable
181
182 constructor(options?: DuplexOptions);
183 }
184
185 // ==== _stream_passthrough ====
186 class PassThrough extends Transform {
187 constructor(options?: TransformOptions);
188
189 _transform<T>(chunk: T, encoding: BufferEncoding | string | null | undefined, callback: (error?: Error, data?: T) => void): void;
190 }
191
192 // ==== _stream_readable ====
193 interface ReadableStateOptions {
194 defaultEncoding?: BufferEncoding | undefined;
195 encoding?: BufferEncoding | undefined;
196 highWaterMark?: number | undefined;
197 objectMode?: boolean | undefined;
198 readableObjectMode?: boolean | undefined;
199 readableHighWaterMark?: number | undefined;
200 }
201
202 interface ReadableState {
203 objectMode: boolean;
204 highWaterMark: number;
205 buffer: BufferList<any>;
206 length: number;
207 pipes: any;
208 pipesCount: number;
209 flowing: any;
210 ended: boolean;
211 endEmitted: boolean;
212 reading: boolean;
213 sync: boolean;
214 needReadable: boolean;
215 emittedReadable: boolean;
216 readableListening: boolean;
217 resumeScheduled: boolean;
218 destroyed: boolean;
219 awaitDrain: number;
220 defaultEncoding: BufferEncoding;
221 readingMore: boolean;
222 decoder: StringDecoder | null;
223 encoding: BufferEncoding | null;
224
225 // new (options: ReadableStateOptions, stream: _Readable): ReadableState;
226 }
227
228 type ReadableOptions = ReadableStateOptions & {
229 read?(this: _Readable, size: number): void;
230 destroy?(this: _Readable, error: Error | null, callback: (error: Error | null) => void): void;
231 };
232
233 class Readable extends _Readable {
234 constructor(options?: ReadableOptions);
235 }
236
237 // ==== _stream_transform ====
238 type TransformOptions = DuplexOptions & {
239 read?(this: Transform, size: number): void;
240 write?(this: Transform, chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
241 writev?(this: Transform, chunks: Array<{ chunk: any, encoding: BufferEncoding }>, callback: (error?: Error | null) => void): void;
242 final?(this: Transform, callback: (error?: Error | null) => void): void;
243 destroy?(this: Transform, error: Error | null, callback: (error: Error | null) => void): void;
244 transform?(this: Transform, chunk: any, encoding: BufferEncoding, callback: (error?: Error, data?: any) => void): void;
245 flush?(this: Transform, callback: (er: any, data: any) => void): void;
246 };
247
248 class Transform extends Duplex {
249 _transformState: {
250 afterTransform: (this: Transform, er: any, data: any) => void | boolean;
251 needTransform: boolean;
252 transforming: boolean;
253 writecb: ((err: any) => any) | null;
254 writechunk: any; // TODO
255 writeencoding: BufferEncoding | null;
256 };
257
258 constructor(options?: TransformOptions);
259
260 _transform(chunk: any, encoding: BufferEncoding, callback: (error?: Error, data?: any) => void): void;
261 _flush(callback: (error?: Error, data?: any) => void): void;
262 }
263
264 // ==== _stream_writable ====
265 interface CorkedRequest {
266 next: any;
267 entry: any;
268 finish(): void;
269 }
270
271 interface BufferRequest {
272 chunk: any; // TODO
273 encoding: BufferEncoding;
274 isBuf: boolean;
275 callback: (error?: Error | null) => void;
276 next: BufferRequest | null;
277 }
278
279 interface WritableStateOptions {
280 decodeStrings?: boolean | undefined;
281 defaultEncoding?: BufferEncoding | undefined;
282 highWaterMark?: number | undefined;
283 objectMode?: boolean | undefined;
284 writableObjectMode?: boolean | undefined;
285 writableHighWaterMark?: number | undefined;
286 }
287
288 interface WritableState {
289 buffer: BufferRequest[];
290 objectMode: boolean;
291 highWaterMark: number;
292 finalCalled: boolean;
293 needDrain: boolean;
294 ending: boolean;
295 ended: boolean;
296 finished: boolean;
297 destroyed: boolean;
298 decodeStrings: boolean;
299 defaultEncoding: BufferEncoding;
300 length: number;
301 writing: boolean;
302 corked: number;
303 sync: boolean;
304 bufferProcessing: boolean;
305 writelen: number;
306 pendingcb: number;
307 prefinished: boolean;
308 errorEmitted: boolean;
309 bufferedRequestCount: number;
310 writecb: ((err?: Error | null) => void) | null;
311 onwrite: (er?: Error | null) => any;
312 bufferedRequest: BufferRequest | null;
313 lastBufferedRequest: BufferRequest | null;
314 corkedRequestsFree: CorkedRequest;
315
316 // new (options: WritableStateOptions, stream: Writable): WritableState;
317
318 getBuffer(): BufferRequest[];
319 }
320
321 type WritableOptions = WritableStateOptions & {
322 write?(this: Writable, chunk: any, encoding: BufferEncoding | string, callback: (error?: Error | null) => void): void;
323 writev?(this: Writable, chunk: ArrayLike<{ chunk: any; encoding: BufferEncoding | string }>, callback: (error?: Error | null) => void): void;
324 destroy?(this: Writable, error: Error | null, callback: (error: Error | null) => void): void;
325 final?(this: Writable, callback: (error?: Error | null) => void): void;
326 };
327
328 class Writable extends Stream {
329 writable: boolean;
330 readonly writableHighWaterMark: number;
331 readonly writableLength: number;
332 constructor(opts?: WritableOptions);
333 _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
334 _writev?(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
335 _destroy(error: Error | null, callback: (error: Error | null) => void): void;
336 _final(callback: (error?: Error | null) => void): void;
337 write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
338 write(chunk: any, encoding?: string, cb?: (error: Error | null | undefined) => void): boolean;
339 setDefaultEncoding(encoding: string): this;
340 end(cb?: () => void): void;
341 end(chunk: any, cb?: () => void): void;
342 end(chunk: any, encoding?: string, cb?: () => void): void;
343 cork(): void;
344 uncork(): void;
345 destroy(error?: Error): void;
346
347 /**
348 * Event emitter
349 * The defined events on documents including:
350 * 1. close
351 * 2. drain
352 * 3. error
353 * 4. finish
354 * 5. pipe
355 * 6. unpipe
356 */
357 addListener(event: "close", listener: () => void): this;
358 addListener(event: "drain", listener: () => void): this;
359 addListener(event: "error", listener: (err: Error) => void): this;
360 addListener(event: "finish", listener: () => void): this;
361 addListener(event: "pipe", listener: (src: Readable) => void): this;
362 addListener(event: "unpipe", listener: (src: Readable) => void): this;
363 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
364
365 emit(event: "close"): boolean;
366 emit(event: "drain"): boolean;
367 emit(event: "error", err: Error): boolean;
368 emit(event: "finish"): boolean;
369 emit(event: "pipe", src: Readable): boolean;
370 emit(event: "unpipe", src: Readable): boolean;
371 emit(event: string | symbol, ...args: any[]): boolean;
372
373 on(event: "close", listener: () => void): this;
374 on(event: "drain", listener: () => void): this;
375 on(event: "error", listener: (err: Error) => void): this;
376 on(event: "finish", listener: () => void): this;
377 on(event: "pipe", listener: (src: Readable) => void): this;
378 on(event: "unpipe", listener: (src: Readable) => void): this;
379 on(event: string | symbol, listener: (...args: any[]) => void): this;
380
381 once(event: "close", listener: () => void): this;
382 once(event: "drain", listener: () => void): this;
383 once(event: "error", listener: (err: Error) => void): this;
384 once(event: "finish", listener: () => void): this;
385 once(event: "pipe", listener: (src: Readable) => void): this;
386 once(event: "unpipe", listener: (src: Readable) => void): this;
387 once(event: string | symbol, listener: (...args: any[]) => void): this;
388
389 prependListener(event: "close", listener: () => void): this;
390 prependListener(event: "drain", listener: () => void): this;
391 prependListener(event: "error", listener: (err: Error) => void): this;
392 prependListener(event: "finish", listener: () => void): this;
393 prependListener(event: "pipe", listener: (src: Readable) => void): this;
394 prependListener(event: "unpipe", listener: (src: Readable) => void): this;
395 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
396
397 prependOnceListener(event: "close", listener: () => void): this;
398 prependOnceListener(event: "drain", listener: () => void): this;
399 prependOnceListener(event: "error", listener: (err: Error) => void): this;
400 prependOnceListener(event: "finish", listener: () => void): this;
401 prependOnceListener(event: "pipe", listener: (src: Readable) => void): this;
402 prependOnceListener(event: "unpipe", listener: (src: Readable) => void): this;
403 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
404
405 removeListener(event: "close", listener: () => void): this;
406 removeListener(event: "drain", listener: () => void): this;
407 removeListener(event: "error", listener: (err: Error) => void): this;
408 removeListener(event: "finish", listener: () => void): this;
409 removeListener(event: "pipe", listener: (src: Readable) => void): this;
410 removeListener(event: "unpipe", listener: (src: Readable) => void): this;
411 removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
412
413 // static WritableState: WritableState;
414 // private static realHasInstance: (obj: any) => boolean;
415 destroyed: boolean;
416 _writableState: WritableState;
417
418 _undestroy(): void;
419 }
420
421 class Stream extends _Readable {
422 constructor(options?: ReadableOptions);
423 pipe<T extends Writable>(destination: T, options?: { end?: boolean | undefined; }): T;
424 }
425}
426
427export = _Readable;
428export as namespace _Readable;