UNPKG

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