UNPKG

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