UNPKG

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