UNPKG

17.7 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 constructor(opts?: ReadableOptions);
11 }
12
13 interface ReadableOptions {
14 highWaterMark?: number;
15 encoding?: string;
16 objectMode?: boolean;
17 read?(this: Readable, size: number): void;
18 destroy?(this: Readable, error: Error | null, callback: (error: Error | null) => void): void;
19 autoDestroy?: boolean;
20 }
21
22 class Readable extends Stream implements NodeJS.ReadableStream {
23 /**
24 * A utility method for creating Readable Streams out of iterators.
25 */
26 static from(iterable: Iterable<any> | AsyncIterable<any>, options?: ReadableOptions): Readable;
27
28 readable: boolean;
29 readonly readableHighWaterMark: number;
30 readonly readableLength: number;
31 readonly readableObjectMode: boolean;
32 destroyed: boolean;
33 constructor(opts?: ReadableOptions);
34 _read(size: number): void;
35 read(size?: number): any;
36 setEncoding(encoding: string): this;
37 pause(): this;
38 resume(): this;
39 isPaused(): boolean;
40 unpipe(destination?: NodeJS.WritableStream): this;
41 unshift(chunk: any, encoding?: BufferEncoding): void;
42 wrap(oldStream: NodeJS.ReadableStream): this;
43 push(chunk: any, encoding?: string): boolean;
44 _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
45 destroy(error?: Error): void;
46
47 /**
48 * Event emitter
49 * The defined events on documents including:
50 * 1. close
51 * 2. data
52 * 3. end
53 * 4. readable
54 * 5. error
55 */
56 addListener(event: "close", listener: () => void): this;
57 addListener(event: "data", listener: (chunk: any) => void): this;
58 addListener(event: "end", listener: () => void): this;
59 addListener(event: "readable", listener: () => void): this;
60 addListener(event: "error", listener: (err: Error) => void): this;
61 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
62
63 emit(event: "close"): boolean;
64 emit(event: "data", chunk: any): boolean;
65 emit(event: "end"): boolean;
66 emit(event: "readable"): boolean;
67 emit(event: "error", err: Error): boolean;
68 emit(event: string | symbol, ...args: any[]): boolean;
69
70 on(event: "close", listener: () => void): this;
71 on(event: "data", listener: (chunk: any) => void): this;
72 on(event: "end", listener: () => void): this;
73 on(event: "readable", listener: () => void): this;
74 on(event: "error", listener: (err: Error) => void): this;
75 on(event: string | symbol, listener: (...args: any[]) => void): this;
76
77 once(event: "close", listener: () => void): this;
78 once(event: "data", listener: (chunk: any) => void): this;
79 once(event: "end", listener: () => void): this;
80 once(event: "readable", listener: () => void): this;
81 once(event: "error", listener: (err: Error) => void): this;
82 once(event: string | symbol, listener: (...args: any[]) => void): this;
83
84 prependListener(event: "close", listener: () => void): this;
85 prependListener(event: "data", listener: (chunk: any) => void): this;
86 prependListener(event: "end", listener: () => void): this;
87 prependListener(event: "readable", listener: () => void): this;
88 prependListener(event: "error", listener: (err: Error) => void): this;
89 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
90
91 prependOnceListener(event: "close", listener: () => void): this;
92 prependOnceListener(event: "data", listener: (chunk: any) => void): this;
93 prependOnceListener(event: "end", listener: () => void): this;
94 prependOnceListener(event: "readable", listener: () => void): this;
95 prependOnceListener(event: "error", listener: (err: Error) => void): this;
96 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
97
98 removeListener(event: "close", listener: () => void): this;
99 removeListener(event: "data", listener: (chunk: any) => void): this;
100 removeListener(event: "end", listener: () => void): this;
101 removeListener(event: "readable", listener: () => void): this;
102 removeListener(event: "error", listener: (err: Error) => void): this;
103 removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
104
105 [Symbol.asyncIterator](): AsyncIterableIterator<any>;
106 }
107
108 interface WritableOptions {
109 highWaterMark?: number;
110 decodeStrings?: boolean;
111 defaultEncoding?: string;
112 objectMode?: boolean;
113 emitClose?: boolean;
114 write?(this: Writable, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
115 writev?(this: Writable, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
116 destroy?(this: Writable, error: Error | null, callback: (error: Error | null) => void): void;
117 final?(this: Writable, callback: (error?: Error | null) => void): void;
118 autoDestroy?: boolean;
119 }
120
121 class Writable extends Stream implements NodeJS.WritableStream {
122 readonly writable: boolean;
123 readonly writableEnded: boolean;
124 readonly writableFinished: boolean;
125 readonly writableHighWaterMark: number;
126 readonly writableLength: number;
127 readonly writableObjectMode: boolean;
128 readonly writableCorked: number;
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): void;
139 end(chunk: any, cb?: () => void): void;
140 end(chunk: any, encoding: string, cb?: () => void): void;
141 cork(): void;
142 uncork(): void;
143 destroy(error?: Error): void;
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;
214 readableObjectMode?: boolean;
215 writableObjectMode?: boolean;
216 readableHighWaterMark?: number;
217 writableHighWaterMark?: number;
218 writableCorked?: number;
219 read?(this: Duplex, size: number): void;
220 write?(this: Duplex, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
221 writev?(this: Duplex, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
222 final?(this: Duplex, callback: (error?: Error | null) => void): void;
223 destroy?(this: Duplex, error: Error | null, callback: (error: Error | null) => void): void;
224 }
225
226 // Note: Duplex extends both Readable and Writable.
227 class Duplex extends Readable implements Writable {
228 readonly writable: boolean;
229 readonly writableEnded: boolean;
230 readonly writableFinished: boolean;
231 readonly writableHighWaterMark: number;
232 readonly writableLength: number;
233 readonly writableObjectMode: boolean;
234 readonly writableCorked: number;
235 constructor(opts?: DuplexOptions);
236 _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
237 _writev?(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
238 _destroy(error: Error | null, callback: (error: Error | null) => void): void;
239 _final(callback: (error?: Error | null) => void): void;
240 write(chunk: any, encoding?: string, cb?: (error: Error | null | undefined) => void): boolean;
241 write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
242 setDefaultEncoding(encoding: string): this;
243 end(cb?: () => void): void;
244 end(chunk: any, cb?: () => void): void;
245 end(chunk: any, encoding?: string, cb?: () => void): void;
246 cork(): void;
247 uncork(): void;
248 }
249
250 type TransformCallback = (error?: Error | null, data?: any) => void;
251
252 interface TransformOptions extends DuplexOptions {
253 read?(this: Transform, size: number): void;
254 write?(this: Transform, chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
255 writev?(this: Transform, chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
256 final?(this: Transform, callback: (error?: Error | null) => void): void;
257 destroy?(this: Transform, error: Error | null, callback: (error: Error | null) => void): void;
258 transform?(this: Transform, chunk: any, encoding: string, callback: TransformCallback): void;
259 flush?(this: Transform, callback: TransformCallback): void;
260 }
261
262 class Transform extends Duplex {
263 constructor(opts?: TransformOptions);
264 _transform(chunk: any, encoding: string, callback: TransformCallback): void;
265 _flush(callback: TransformCallback): void;
266 }
267
268 class PassThrough extends Transform { }
269
270 function finished(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, callback: (err?: NodeJS.ErrnoException | null) => void): () => void;
271 namespace finished {
272 function __promisify__(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream): Promise<void>;
273 }
274
275 function pipeline<T extends NodeJS.WritableStream>(stream1: NodeJS.ReadableStream, stream2: T, callback?: (err: NodeJS.ErrnoException | null) => void): T;
276 function pipeline<T extends NodeJS.WritableStream>(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: T, callback?: (err: NodeJS.ErrnoException | null) => void): T;
277 function pipeline<T extends NodeJS.WritableStream>(
278 stream1: NodeJS.ReadableStream,
279 stream2: NodeJS.ReadWriteStream,
280 stream3: NodeJS.ReadWriteStream,
281 stream4: T,
282 callback?: (err: NodeJS.ErrnoException | null) => void,
283 ): T;
284 function pipeline<T extends NodeJS.WritableStream>(
285 stream1: NodeJS.ReadableStream,
286 stream2: NodeJS.ReadWriteStream,
287 stream3: NodeJS.ReadWriteStream,
288 stream4: NodeJS.ReadWriteStream,
289 stream5: T,
290 callback?: (err: NodeJS.ErrnoException | null) => void,
291 ): T;
292 function pipeline(streams: Array<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>, callback?: (err: NodeJS.ErrnoException | null) => void): NodeJS.WritableStream;
293 function pipeline(
294 stream1: NodeJS.ReadableStream,
295 stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
296 ...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream | ((err: NodeJS.ErrnoException | null) => void)>,
297 ): NodeJS.WritableStream;
298 namespace pipeline {
299 function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.WritableStream): Promise<void>;
300 function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: NodeJS.WritableStream): Promise<void>;
301 function __promisify__(stream1: NodeJS.ReadableStream, stream2: NodeJS.ReadWriteStream, stream3: NodeJS.ReadWriteStream, stream4: NodeJS.WritableStream): Promise<void>;
302 function __promisify__(
303 stream1: NodeJS.ReadableStream,
304 stream2: NodeJS.ReadWriteStream,
305 stream3: NodeJS.ReadWriteStream,
306 stream4: NodeJS.ReadWriteStream,
307 stream5: NodeJS.WritableStream,
308 ): Promise<void>;
309 function __promisify__(streams: Array<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>): Promise<void>;
310 function __promisify__(
311 stream1: NodeJS.ReadableStream,
312 stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
313 ...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream>,
314 ): Promise<void>;
315 }
316
317 interface Pipe {
318 close(): void;
319 hasRef(): boolean;
320 ref(): void;
321 unref(): void;
322 }
323 }
324
325 export = internal;
326}