UNPKG

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