UNPKG

18.3 kBTypeScriptView Raw
1declare module 'stream' {
2 import EventEmitter = require('events');
3
4 class internal extends EventEmitter {
5 pipe<T extends NodeJS.WritableStream>(destination: T, options?: { end?: boolean | undefined; }): T;
6 }
7
8 namespace internal {
9 class Stream extends internal { }
10
11 interface ReadableOptions {
12 highWaterMark?: number | undefined;
13 encoding?: string | undefined;
14 objectMode?: boolean | undefined;
15 read?(this: Readable, size: number): void;
16 destroy?(this: Readable, error: Error | null, callback: (error: Error | null) => void): void;
17 autoDestroy?: boolean | undefined;
18 }
19
20 class Readable extends Stream implements NodeJS.ReadableStream {
21 /**
22 * A utility method for creating Readable Streams out of iterators.
23 */
24 static from(iterable: Iterable<any> | AsyncIterable<any>, options?: ReadableOptions): Readable;
25
26 readable: boolean;
27 readonly readableEncoding: BufferEncoding | null;
28 readonly readableEnded: boolean;
29 readonly readableFlowing: boolean | null;
30 readonly readableHighWaterMark: number;
31 readonly readableLength: number;
32 readonly readableObjectMode: boolean;
33 destroyed: boolean;
34 constructor(opts?: ReadableOptions);
35 _read(size: number): void;
36 read(size?: number): any;
37 setEncoding(encoding: string): this;
38 pause(): this;
39 resume(): this;
40 isPaused(): boolean;
41 unpipe(destination?: NodeJS.WritableStream): this;
42 unshift(chunk: any, encoding?: BufferEncoding): void;
43 wrap(oldStream: NodeJS.ReadableStream): this;
44 push(chunk: any, encoding?: string): boolean;
45 _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
46 destroy(error?: Error): this;
47
48 /**
49 * Event emitter
50 * The defined events on documents including:
51 * 1. close
52 * 2. data
53 * 3. end
54 * 4. readable
55 * 5. error
56 */
57 addListener(event: "close", listener: () => void): this;
58 addListener(event: "data", listener: (chunk: any) => void): this;
59 addListener(event: "end", listener: () => void): this;
60 addListener(event: "readable", listener: () => void): this;
61 addListener(event: "error", listener: (err: Error) => void): this;
62 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
63
64 emit(event: "close"): boolean;
65 emit(event: "data", chunk: any): boolean;
66 emit(event: "end"): boolean;
67 emit(event: "readable"): boolean;
68 emit(event: "error", err: Error): boolean;
69 emit(event: string | symbol, ...args: any[]): boolean;
70
71 on(event: "close", listener: () => void): this;
72 on(event: "data", listener: (chunk: any) => void): this;
73 on(event: "end", listener: () => void): this;
74 on(event: "readable", listener: () => void): this;
75 on(event: "error", listener: (err: Error) => void): this;
76 on(event: string | symbol, listener: (...args: any[]) => void): this;
77
78 once(event: "close", listener: () => void): this;
79 once(event: "data", listener: (chunk: any) => void): this;
80 once(event: "end", listener: () => void): this;
81 once(event: "readable", listener: () => void): this;
82 once(event: "error", listener: (err: Error) => void): this;
83 once(event: string | symbol, listener: (...args: any[]) => void): this;
84
85 prependListener(event: "close", listener: () => void): this;
86 prependListener(event: "data", listener: (chunk: any) => void): this;
87 prependListener(event: "end", listener: () => void): this;
88 prependListener(event: "readable", listener: () => void): this;
89 prependListener(event: "error", listener: (err: Error) => void): this;
90 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
91
92 prependOnceListener(event: "close", listener: () => void): this;
93 prependOnceListener(event: "data", listener: (chunk: any) => void): this;
94 prependOnceListener(event: "end", listener: () => void): this;
95 prependOnceListener(event: "readable", listener: () => void): this;
96 prependOnceListener(event: "error", listener: (err: Error) => void): this;
97 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
98
99 removeListener(event: "close", listener: () => void): this;
100 removeListener(event: "data", listener: (chunk: any) => void): this;
101 removeListener(event: "end", listener: () => void): this;
102 removeListener(event: "readable", listener: () => void): this;
103 removeListener(event: "error", listener: (err: Error) => void): this;
104 removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
105
106 [Symbol.asyncIterator](): AsyncIterableIterator<any>;
107 }
108
109 interface WritableOptions {
110 highWaterMark?: number | undefined;
111 decodeStrings?: boolean | undefined;
112 defaultEncoding?: string | undefined;
113 objectMode?: boolean | undefined;
114 emitClose?: boolean | undefined;
115 write?(this: Writable, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
116 writev?(this: Writable, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
117 destroy?(this: Writable, error: Error | null, callback: (error: Error | null) => void): void;
118 final?(this: Writable, callback: (error?: Error | null) => void): void;
119 autoDestroy?: boolean | undefined;
120 }
121
122 class Writable extends Stream implements NodeJS.WritableStream {
123 readonly writable: boolean;
124 readonly writableEnded: boolean;
125 readonly writableFinished: boolean;
126 readonly writableHighWaterMark: number;
127 readonly writableLength: number;
128 readonly writableObjectMode: boolean;
129 destroyed: boolean;
130 constructor(opts?: WritableOptions);
131 _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
132 _writev?(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
133 _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
134 _final(callback: (error?: Error | null) => void): void;
135 write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
136 write(chunk: any, encoding: string, cb?: (error: Error | null | undefined) => void): boolean;
137 setDefaultEncoding(encoding: string): this;
138 end(cb?: () => void): this;
139 end(chunk: any, cb?: () => void): this;
140 end(chunk: any, encoding: string, cb?: () => void): this;
141 cork(): void;
142 uncork(): void;
143 destroy(error?: Error): this;
144
145 /**
146 * Event emitter
147 * The defined events on documents including:
148 * 1. close
149 * 2. drain
150 * 3. error
151 * 4. finish
152 * 5. pipe
153 * 6. unpipe
154 */
155 addListener(event: "close", listener: () => void): this;
156 addListener(event: "drain", listener: () => void): this;
157 addListener(event: "error", listener: (err: Error) => void): this;
158 addListener(event: "finish", listener: () => void): this;
159 addListener(event: "pipe", listener: (src: Readable) => void): this;
160 addListener(event: "unpipe", listener: (src: Readable) => void): this;
161 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
162
163 emit(event: "close"): boolean;
164 emit(event: "drain"): boolean;
165 emit(event: "error", err: Error): boolean;
166 emit(event: "finish"): boolean;
167 emit(event: "pipe", src: Readable): boolean;
168 emit(event: "unpipe", src: Readable): boolean;
169 emit(event: string | symbol, ...args: any[]): boolean;
170
171 on(event: "close", listener: () => void): this;
172 on(event: "drain", listener: () => void): this;
173 on(event: "error", listener: (err: Error) => void): this;
174 on(event: "finish", listener: () => void): this;
175 on(event: "pipe", listener: (src: Readable) => void): this;
176 on(event: "unpipe", listener: (src: Readable) => void): this;
177 on(event: string | symbol, listener: (...args: any[]) => void): this;
178
179 once(event: "close", listener: () => void): this;
180 once(event: "drain", listener: () => void): this;
181 once(event: "error", listener: (err: Error) => void): this;
182 once(event: "finish", listener: () => void): this;
183 once(event: "pipe", listener: (src: Readable) => void): this;
184 once(event: "unpipe", listener: (src: Readable) => void): this;
185 once(event: string | symbol, listener: (...args: any[]) => void): this;
186
187 prependListener(event: "close", listener: () => void): this;
188 prependListener(event: "drain", listener: () => void): this;
189 prependListener(event: "error", listener: (err: Error) => void): this;
190 prependListener(event: "finish", listener: () => void): this;
191 prependListener(event: "pipe", listener: (src: Readable) => void): this;
192 prependListener(event: "unpipe", listener: (src: Readable) => void): this;
193 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
194
195 prependOnceListener(event: "close", listener: () => void): this;
196 prependOnceListener(event: "drain", listener: () => void): this;
197 prependOnceListener(event: "error", listener: (err: Error) => void): this;
198 prependOnceListener(event: "finish", listener: () => void): this;
199 prependOnceListener(event: "pipe", listener: (src: Readable) => void): this;
200 prependOnceListener(event: "unpipe", listener: (src: Readable) => void): this;
201 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
202
203 removeListener(event: "close", listener: () => void): this;
204 removeListener(event: "drain", listener: () => void): this;
205 removeListener(event: "error", listener: (err: Error) => void): this;
206 removeListener(event: "finish", listener: () => void): this;
207 removeListener(event: "pipe", listener: (src: Readable) => void): this;
208 removeListener(event: "unpipe", listener: (src: Readable) => void): this;
209 removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
210 }
211
212 interface DuplexOptions extends ReadableOptions, WritableOptions {
213 allowHalfOpen?: boolean | undefined;
214 readableObjectMode?: boolean | undefined;
215 writableObjectMode?: boolean | undefined;
216 readableHighWaterMark?: number | undefined;
217 writableHighWaterMark?: number | undefined;
218 read?(this: Duplex, size: number): void;
219 write?(this: Duplex, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
220 writev?(this: Duplex, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
221 final?(this: Duplex, callback: (error?: Error | null) => void): void;
222 destroy?(this: Duplex, error: Error | null, callback: (error: Error | null) => void): void;
223 }
224
225 // Note: Duplex extends both Readable and Writable.
226 class Duplex extends Readable implements Writable {
227 readonly writable: boolean;
228 readonly writableEnded: boolean;
229 readonly writableFinished: boolean;
230 readonly writableHighWaterMark: number;
231 readonly writableLength: number;
232 readonly writableObjectMode: boolean;
233 allowHalfOpen: boolean;
234 constructor(opts?: DuplexOptions);
235 _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
236 _writev?(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
237 _destroy(error: Error | null, callback: (error: Error | null) => void): void;
238 _final(callback: (error?: Error | null) => void): void;
239 write(chunk: any, encoding?: string, cb?: (error: Error | null | undefined) => void): boolean;
240 write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
241 setDefaultEncoding(encoding: string): this;
242 end(cb?: () => void): this;
243 end(chunk: any, cb?: () => void): this;
244 end(chunk: any, encoding?: string, cb?: () => void): this;
245 cork(): void;
246 uncork(): void;
247 }
248
249 type TransformCallback = (error?: Error | null, data?: any) => void;
250
251 interface TransformOptions extends DuplexOptions {
252 read?(this: Transform, size: number): void;
253 write?(this: Transform, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
254 writev?(this: Transform, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
255 final?(this: Transform, callback: (error?: Error | null) => void): void;
256 destroy?(this: Transform, error: Error | null, callback: (error: Error | null) => void): void;
257 transform?(this: Transform, chunk: any, encoding: string, callback: TransformCallback): void;
258 flush?(this: Transform, callback: TransformCallback): void;
259 }
260
261 class Transform extends Duplex {
262 constructor(opts?: TransformOptions);
263 _transform(chunk: any, encoding: string, callback: TransformCallback): void;
264 _flush(callback: TransformCallback): void;
265 }
266
267 class PassThrough extends Transform { }
268
269 interface FinishedOptions {
270 error?: boolean | undefined;
271 readable?: boolean | undefined;
272 writable?: boolean | undefined;
273 }
274 function finished(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, options: FinishedOptions, callback: (err?: NodeJS.ErrnoException | null) => void): () => void;
275 function finished(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, callback: (err?: NodeJS.ErrnoException | null) => void): () => void;
276 namespace finished {
277 function __promisify__(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, options?: FinishedOptions): Promise<void>;
278 }
279
280 function pipeline<T extends NodeJS.WritableStream>(stream1: NodeJS.ReadableStream, stream2: T, callback?: (err: NodeJS.ErrnoException | null) => void): T;
281 function pipeline<T extends NodeJS.WritableStream>(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: T, callback?: (err: NodeJS.ErrnoException | null) => void): T;
282 function pipeline<T extends NodeJS.WritableStream>(
283 stream1: NodeJS.ReadableStream,
284 stream2: NodeJS.ReadWriteStream,
285 stream3: NodeJS.ReadWriteStream,
286 stream4: T,
287 callback?: (err: NodeJS.ErrnoException | null) => void,
288 ): T;
289 function pipeline<T extends NodeJS.WritableStream>(
290 stream1: NodeJS.ReadableStream,
291 stream2: NodeJS.ReadWriteStream,
292 stream3: NodeJS.ReadWriteStream,
293 stream4: NodeJS.ReadWriteStream,
294 stream5: T,
295 callback?: (err: NodeJS.ErrnoException | null) => void,
296 ): T;
297 function pipeline(
298 streams: ReadonlyArray<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>,
299 callback?: (err: NodeJS.ErrnoException | null) => void,
300 ): NodeJS.WritableStream;
301 function pipeline(
302 stream1: NodeJS.ReadableStream,
303 stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
304 ...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream | ((err: NodeJS.ErrnoException | null) => void)>,
305 ): NodeJS.WritableStream;
306 namespace pipeline {
307 function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.WritableStream): Promise<void>;
308 function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: NodeJS.WritableStream): Promise<void>;
309 function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: NodeJS.ReadWriteStream, stream4: NodeJS.WritableStream): Promise<void>;
310 function __promisify__(
311 stream1: NodeJS.ReadableStream,
312 stream2: NodeJS.ReadWriteStream,
313 stream3: NodeJS.ReadWriteStream,
314 stream4: NodeJS.ReadWriteStream,
315 stream5: NodeJS.WritableStream,
316 ): Promise<void>;
317 function __promisify__(streams: ReadonlyArray<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>): Promise<void>;
318 function __promisify__(
319 stream1: NodeJS.ReadableStream,
320 stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
321 ...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream>,
322 ): Promise<void>;
323 }
324
325 interface Pipe {
326 close(): void;
327 hasRef(): boolean;
328 ref(): void;
329 unref(): void;
330 }
331 }
332
333 export = internal;
334}