@types/node 20.4.10 → 20.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
node/ts4.8/stream.d.ts CHANGED
@@ -34,6 +34,744 @@ declare module 'stream' {
34
34
  ): T;
35
35
  compose<T extends NodeJS.ReadableStream>(stream: T | ComposeFnParam | Iterable<T> | AsyncIterable<T>, options?: { signal: AbortSignal }): T;
36
36
  }
37
+ import Stream = internal.Stream;
38
+ import Readable = internal.Readable;
39
+ import ReadableOptions = internal.ReadableOptions;
40
+ class ReadableBase extends Stream implements NodeJS.ReadableStream {
41
+ /**
42
+ * A utility method for creating Readable Streams out of iterators.
43
+ */
44
+ static from(iterable: Iterable<any> | AsyncIterable<any>, options?: ReadableOptions): Readable;
45
+ /**
46
+ * Returns whether the stream has been read from or cancelled.
47
+ * @since v16.8.0
48
+ */
49
+ static isDisturbed(stream: Readable | NodeJS.ReadableStream): boolean;
50
+ /**
51
+ * Returns whether the stream was destroyed or errored before emitting `'end'`.
52
+ * @since v16.8.0
53
+ * @experimental
54
+ */
55
+ readonly readableAborted: boolean;
56
+ /**
57
+ * Is `true` if it is safe to call `readable.read()`, which means
58
+ * the stream has not been destroyed or emitted `'error'` or `'end'`.
59
+ * @since v11.4.0
60
+ */
61
+ readable: boolean;
62
+ /**
63
+ * Returns whether `'data'` has been emitted.
64
+ * @since v16.7.0, v14.18.0
65
+ * @experimental
66
+ */
67
+ readonly readableDidRead: boolean;
68
+ /**
69
+ * Getter for the property `encoding` of a given `Readable` stream. The `encoding`property can be set using the `readable.setEncoding()` method.
70
+ * @since v12.7.0
71
+ */
72
+ readonly readableEncoding: BufferEncoding | null;
73
+ /**
74
+ * Becomes `true` when `'end'` event is emitted.
75
+ * @since v12.9.0
76
+ */
77
+ readonly readableEnded: boolean;
78
+ /**
79
+ * This property reflects the current state of a `Readable` stream as described
80
+ * in the `Three states` section.
81
+ * @since v9.4.0
82
+ */
83
+ readonly readableFlowing: boolean | null;
84
+ /**
85
+ * Returns the value of `highWaterMark` passed when creating this `Readable`.
86
+ * @since v9.3.0
87
+ */
88
+ readonly readableHighWaterMark: number;
89
+ /**
90
+ * This property contains the number of bytes (or objects) in the queue
91
+ * ready to be read. The value provides introspection data regarding
92
+ * the status of the `highWaterMark`.
93
+ * @since v9.4.0
94
+ */
95
+ readonly readableLength: number;
96
+ /**
97
+ * Getter for the property `objectMode` of a given `Readable` stream.
98
+ * @since v12.3.0
99
+ */
100
+ readonly readableObjectMode: boolean;
101
+ /**
102
+ * Is `true` after `readable.destroy()` has been called.
103
+ * @since v8.0.0
104
+ */
105
+ destroyed: boolean;
106
+ /**
107
+ * Is `true` after `'close'` has been emitted.
108
+ * @since v18.0.0
109
+ */
110
+ readonly closed: boolean;
111
+ /**
112
+ * Returns error if the stream has been destroyed with an error.
113
+ * @since v18.0.0
114
+ */
115
+ readonly errored: Error | null;
116
+ constructor(opts?: ReadableOptions);
117
+ _construct?(callback: (error?: Error | null) => void): void;
118
+ _read(size: number): void;
119
+ /**
120
+ * The `readable.read()` method reads data out of the internal buffer and
121
+ * returns it. If no data is available to be read, `null` is returned. By default,
122
+ * the data is returned as a `Buffer` object unless an encoding has been
123
+ * specified using the `readable.setEncoding()` method or the stream is operating
124
+ * in object mode.
125
+ *
126
+ * The optional `size` argument specifies a specific number of bytes to read. If`size` bytes are not available to be read, `null` will be returned _unless_the stream has ended, in which
127
+ * case all of the data remaining in the internal
128
+ * buffer will be returned.
129
+ *
130
+ * If the `size` argument is not specified, all of the data contained in the
131
+ * internal buffer will be returned.
132
+ *
133
+ * The `size` argument must be less than or equal to 1 GiB.
134
+ *
135
+ * The `readable.read()` method should only be called on `Readable` streams
136
+ * operating in paused mode. In flowing mode, `readable.read()` is called
137
+ * automatically until the internal buffer is fully drained.
138
+ *
139
+ * ```js
140
+ * const readable = getReadableStreamSomehow();
141
+ *
142
+ * // 'readable' may be triggered multiple times as data is buffered in
143
+ * readable.on('readable', () => {
144
+ * let chunk;
145
+ * console.log('Stream is readable (new data received in buffer)');
146
+ * // Use a loop to make sure we read all currently available data
147
+ * while (null !== (chunk = readable.read())) {
148
+ * console.log(`Read ${chunk.length} bytes of data...`);
149
+ * }
150
+ * });
151
+ *
152
+ * // 'end' will be triggered once when there is no more data available
153
+ * readable.on('end', () => {
154
+ * console.log('Reached end of stream.');
155
+ * });
156
+ * ```
157
+ *
158
+ * Each call to `readable.read()` returns a chunk of data, or `null`. The chunks
159
+ * are not concatenated. A `while` loop is necessary to consume all data
160
+ * currently in the buffer. When reading a large file `.read()` may return `null`,
161
+ * having consumed all buffered content so far, but there is still more data to
162
+ * come not yet buffered. In this case a new `'readable'` event will be emitted
163
+ * when there is more data in the buffer. Finally the `'end'` event will be
164
+ * emitted when there is no more data to come.
165
+ *
166
+ * Therefore to read a file's whole contents from a `readable`, it is necessary
167
+ * to collect chunks across multiple `'readable'` events:
168
+ *
169
+ * ```js
170
+ * const chunks = [];
171
+ *
172
+ * readable.on('readable', () => {
173
+ * let chunk;
174
+ * while (null !== (chunk = readable.read())) {
175
+ * chunks.push(chunk);
176
+ * }
177
+ * });
178
+ *
179
+ * readable.on('end', () => {
180
+ * const content = chunks.join('');
181
+ * });
182
+ * ```
183
+ *
184
+ * A `Readable` stream in object mode will always return a single item from
185
+ * a call to `readable.read(size)`, regardless of the value of the`size` argument.
186
+ *
187
+ * If the `readable.read()` method returns a chunk of data, a `'data'` event will
188
+ * also be emitted.
189
+ *
190
+ * Calling {@link read} after the `'end'` event has
191
+ * been emitted will return `null`. No runtime error will be raised.
192
+ * @since v0.9.4
193
+ * @param size Optional argument to specify how much data to read.
194
+ */
195
+ read(size?: number): any;
196
+ /**
197
+ * The `readable.setEncoding()` method sets the character encoding for
198
+ * data read from the `Readable` stream.
199
+ *
200
+ * By default, no encoding is assigned and stream data will be returned as`Buffer` objects. Setting an encoding causes the stream data
201
+ * to be returned as strings of the specified encoding rather than as `Buffer`objects. For instance, calling `readable.setEncoding('utf8')` will cause the
202
+ * output data to be interpreted as UTF-8 data, and passed as strings. Calling`readable.setEncoding('hex')` will cause the data to be encoded in hexadecimal
203
+ * string format.
204
+ *
205
+ * The `Readable` stream will properly handle multi-byte characters delivered
206
+ * through the stream that would otherwise become improperly decoded if simply
207
+ * pulled from the stream as `Buffer` objects.
208
+ *
209
+ * ```js
210
+ * const readable = getReadableStreamSomehow();
211
+ * readable.setEncoding('utf8');
212
+ * readable.on('data', (chunk) => {
213
+ * assert.equal(typeof chunk, 'string');
214
+ * console.log('Got %d characters of string data:', chunk.length);
215
+ * });
216
+ * ```
217
+ * @since v0.9.4
218
+ * @param encoding The encoding to use.
219
+ */
220
+ setEncoding(encoding: BufferEncoding): this;
221
+ /**
222
+ * The `readable.pause()` method will cause a stream in flowing mode to stop
223
+ * emitting `'data'` events, switching out of flowing mode. Any data that
224
+ * becomes available will remain in the internal buffer.
225
+ *
226
+ * ```js
227
+ * const readable = getReadableStreamSomehow();
228
+ * readable.on('data', (chunk) => {
229
+ * console.log(`Received ${chunk.length} bytes of data.`);
230
+ * readable.pause();
231
+ * console.log('There will be no additional data for 1 second.');
232
+ * setTimeout(() => {
233
+ * console.log('Now data will start flowing again.');
234
+ * readable.resume();
235
+ * }, 1000);
236
+ * });
237
+ * ```
238
+ *
239
+ * The `readable.pause()` method has no effect if there is a `'readable'`event listener.
240
+ * @since v0.9.4
241
+ */
242
+ pause(): this;
243
+ /**
244
+ * The `readable.resume()` method causes an explicitly paused `Readable` stream to
245
+ * resume emitting `'data'` events, switching the stream into flowing mode.
246
+ *
247
+ * The `readable.resume()` method can be used to fully consume the data from a
248
+ * stream without actually processing any of that data:
249
+ *
250
+ * ```js
251
+ * getReadableStreamSomehow()
252
+ * .resume()
253
+ * .on('end', () => {
254
+ * console.log('Reached the end, but did not read anything.');
255
+ * });
256
+ * ```
257
+ *
258
+ * The `readable.resume()` method has no effect if there is a `'readable'`event listener.
259
+ * @since v0.9.4
260
+ */
261
+ resume(): this;
262
+ /**
263
+ * The `readable.isPaused()` method returns the current operating state of the`Readable`. This is used primarily by the mechanism that underlies the`readable.pipe()` method. In most
264
+ * typical cases, there will be no reason to
265
+ * use this method directly.
266
+ *
267
+ * ```js
268
+ * const readable = new stream.Readable();
269
+ *
270
+ * readable.isPaused(); // === false
271
+ * readable.pause();
272
+ * readable.isPaused(); // === true
273
+ * readable.resume();
274
+ * readable.isPaused(); // === false
275
+ * ```
276
+ * @since v0.11.14
277
+ */
278
+ isPaused(): boolean;
279
+ /**
280
+ * The `readable.unpipe()` method detaches a `Writable` stream previously attached
281
+ * using the {@link pipe} method.
282
+ *
283
+ * If the `destination` is not specified, then _all_ pipes are detached.
284
+ *
285
+ * If the `destination` is specified, but no pipe is set up for it, then
286
+ * the method does nothing.
287
+ *
288
+ * ```js
289
+ * const fs = require('node:fs');
290
+ * const readable = getReadableStreamSomehow();
291
+ * const writable = fs.createWriteStream('file.txt');
292
+ * // All the data from readable goes into 'file.txt',
293
+ * // but only for the first second.
294
+ * readable.pipe(writable);
295
+ * setTimeout(() => {
296
+ * console.log('Stop writing to file.txt.');
297
+ * readable.unpipe(writable);
298
+ * console.log('Manually close the file stream.');
299
+ * writable.end();
300
+ * }, 1000);
301
+ * ```
302
+ * @since v0.9.4
303
+ * @param destination Optional specific stream to unpipe
304
+ */
305
+ unpipe(destination?: NodeJS.WritableStream): this;
306
+ /**
307
+ * Passing `chunk` as `null` signals the end of the stream (EOF) and behaves the
308
+ * same as `readable.push(null)`, after which no more data can be written. The EOF
309
+ * signal is put at the end of the buffer and any buffered data will still be
310
+ * flushed.
311
+ *
312
+ * The `readable.unshift()` method pushes a chunk of data back into the internal
313
+ * buffer. This is useful in certain situations where a stream is being consumed by
314
+ * code that needs to "un-consume" some amount of data that it has optimistically
315
+ * pulled out of the source, so that the data can be passed on to some other party.
316
+ *
317
+ * The `stream.unshift(chunk)` method cannot be called after the `'end'` event
318
+ * has been emitted or a runtime error will be thrown.
319
+ *
320
+ * Developers using `stream.unshift()` often should consider switching to
321
+ * use of a `Transform` stream instead. See the `API for stream implementers` section for more information.
322
+ *
323
+ * ```js
324
+ * // Pull off a header delimited by \n\n.
325
+ * // Use unshift() if we get too much.
326
+ * // Call the callback with (error, header, stream).
327
+ * const { StringDecoder } = require('node:string_decoder');
328
+ * function parseHeader(stream, callback) {
329
+ * stream.on('error', callback);
330
+ * stream.on('readable', onReadable);
331
+ * const decoder = new StringDecoder('utf8');
332
+ * let header = '';
333
+ * function onReadable() {
334
+ * let chunk;
335
+ * while (null !== (chunk = stream.read())) {
336
+ * const str = decoder.write(chunk);
337
+ * if (str.includes('\n\n')) {
338
+ * // Found the header boundary.
339
+ * const split = str.split(/\n\n/);
340
+ * header += split.shift();
341
+ * const remaining = split.join('\n\n');
342
+ * const buf = Buffer.from(remaining, 'utf8');
343
+ * stream.removeListener('error', callback);
344
+ * // Remove the 'readable' listener before unshifting.
345
+ * stream.removeListener('readable', onReadable);
346
+ * if (buf.length)
347
+ * stream.unshift(buf);
348
+ * // Now the body of the message can be read from the stream.
349
+ * callback(null, header, stream);
350
+ * return;
351
+ * }
352
+ * // Still reading the header.
353
+ * header += str;
354
+ * }
355
+ * }
356
+ * }
357
+ * ```
358
+ *
359
+ * Unlike {@link push}, `stream.unshift(chunk)` will not
360
+ * end the reading process by resetting the internal reading state of the stream.
361
+ * This can cause unexpected results if `readable.unshift()` is called during a
362
+ * read (i.e. from within a {@link _read} implementation on a
363
+ * custom stream). Following the call to `readable.unshift()` with an immediate {@link push} will reset the reading state appropriately,
364
+ * however it is best to simply avoid calling `readable.unshift()` while in the
365
+ * process of performing a read.
366
+ * @since v0.9.11
367
+ * @param chunk Chunk of data to unshift onto the read queue. For streams not operating in object mode, `chunk` must be a string, `Buffer`, `Uint8Array`, or `null`. For object mode
368
+ * streams, `chunk` may be any JavaScript value.
369
+ * @param encoding Encoding of string chunks. Must be a valid `Buffer` encoding, such as `'utf8'` or `'ascii'`.
370
+ */
371
+ unshift(chunk: any, encoding?: BufferEncoding): void;
372
+ /**
373
+ * Prior to Node.js 0.10, streams did not implement the entire `node:stream`module API as it is currently defined. (See `Compatibility` for more
374
+ * information.)
375
+ *
376
+ * When using an older Node.js library that emits `'data'` events and has a {@link pause} method that is advisory only, the`readable.wrap()` method can be used to create a `Readable`
377
+ * stream that uses
378
+ * the old stream as its data source.
379
+ *
380
+ * It will rarely be necessary to use `readable.wrap()` but the method has been
381
+ * provided as a convenience for interacting with older Node.js applications and
382
+ * libraries.
383
+ *
384
+ * ```js
385
+ * const { OldReader } = require('./old-api-module.js');
386
+ * const { Readable } = require('node:stream');
387
+ * const oreader = new OldReader();
388
+ * const myReader = new Readable().wrap(oreader);
389
+ *
390
+ * myReader.on('readable', () => {
391
+ * myReader.read(); // etc.
392
+ * });
393
+ * ```
394
+ * @since v0.9.4
395
+ * @param stream An "old style" readable stream
396
+ */
397
+ wrap(stream: NodeJS.ReadableStream): this;
398
+ push(chunk: any, encoding?: BufferEncoding): boolean;
399
+ _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
400
+ /**
401
+ * Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`event (unless `emitClose` is set to `false`). After this call, the readable
402
+ * stream will release any internal resources and subsequent calls to `push()`will be ignored.
403
+ *
404
+ * Once `destroy()` has been called any further calls will be a no-op and no
405
+ * further errors except from `_destroy()` may be emitted as `'error'`.
406
+ *
407
+ * Implementors should not override this method, but instead implement `readable._destroy()`.
408
+ * @since v8.0.0
409
+ * @param error Error which will be passed as payload in `'error'` event
410
+ */
411
+ destroy(error?: Error): this;
412
+ /**
413
+ * Event emitter
414
+ * The defined events on documents including:
415
+ * 1. close
416
+ * 2. data
417
+ * 3. end
418
+ * 4. error
419
+ * 5. pause
420
+ * 6. readable
421
+ * 7. resume
422
+ */
423
+ addListener(event: 'close', listener: () => void): this;
424
+ addListener(event: 'data', listener: (chunk: any) => void): this;
425
+ addListener(event: 'end', listener: () => void): this;
426
+ addListener(event: 'error', listener: (err: Error) => void): this;
427
+ addListener(event: 'pause', listener: () => void): this;
428
+ addListener(event: 'readable', listener: () => void): this;
429
+ addListener(event: 'resume', listener: () => void): this;
430
+ addListener(event: string | symbol, listener: (...args: any[]) => void): this;
431
+ emit(event: 'close'): boolean;
432
+ emit(event: 'data', chunk: any): boolean;
433
+ emit(event: 'end'): boolean;
434
+ emit(event: 'error', err: Error): boolean;
435
+ emit(event: 'pause'): boolean;
436
+ emit(event: 'readable'): boolean;
437
+ emit(event: 'resume'): boolean;
438
+ emit(event: string | symbol, ...args: any[]): boolean;
439
+ on(event: 'close', listener: () => void): this;
440
+ on(event: 'data', listener: (chunk: any) => void): this;
441
+ on(event: 'end', listener: () => void): this;
442
+ on(event: 'error', listener: (err: Error) => void): this;
443
+ on(event: 'pause', listener: () => void): this;
444
+ on(event: 'readable', listener: () => void): this;
445
+ on(event: 'resume', listener: () => void): this;
446
+ on(event: string | symbol, listener: (...args: any[]) => void): this;
447
+ once(event: 'close', listener: () => void): this;
448
+ once(event: 'data', listener: (chunk: any) => void): this;
449
+ once(event: 'end', listener: () => void): this;
450
+ once(event: 'error', listener: (err: Error) => void): this;
451
+ once(event: 'pause', listener: () => void): this;
452
+ once(event: 'readable', listener: () => void): this;
453
+ once(event: 'resume', listener: () => void): this;
454
+ once(event: string | symbol, listener: (...args: any[]) => void): this;
455
+ prependListener(event: 'close', listener: () => void): this;
456
+ prependListener(event: 'data', listener: (chunk: any) => void): this;
457
+ prependListener(event: 'end', listener: () => void): this;
458
+ prependListener(event: 'error', listener: (err: Error) => void): this;
459
+ prependListener(event: 'pause', listener: () => void): this;
460
+ prependListener(event: 'readable', listener: () => void): this;
461
+ prependListener(event: 'resume', listener: () => void): this;
462
+ prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
463
+ prependOnceListener(event: 'close', listener: () => void): this;
464
+ prependOnceListener(event: 'data', listener: (chunk: any) => void): this;
465
+ prependOnceListener(event: 'end', listener: () => void): this;
466
+ prependOnceListener(event: 'error', listener: (err: Error) => void): this;
467
+ prependOnceListener(event: 'pause', listener: () => void): this;
468
+ prependOnceListener(event: 'readable', listener: () => void): this;
469
+ prependOnceListener(event: 'resume', listener: () => void): this;
470
+ prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
471
+ removeListener(event: 'close', listener: () => void): this;
472
+ removeListener(event: 'data', listener: (chunk: any) => void): this;
473
+ removeListener(event: 'end', listener: () => void): this;
474
+ removeListener(event: 'error', listener: (err: Error) => void): this;
475
+ removeListener(event: 'pause', listener: () => void): this;
476
+ removeListener(event: 'readable', listener: () => void): this;
477
+ removeListener(event: 'resume', listener: () => void): this;
478
+ removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
479
+ [Symbol.asyncIterator](): AsyncIterableIterator<any>;
480
+ /**
481
+ * Calls `readable.destroy()` with an `AbortError` and returns a promise that fulfills when the stream is finished.
482
+ * @since v20.4.0
483
+ */
484
+ [Symbol.asyncDispose](): Promise<void>;
485
+ }
486
+ import WritableOptions = internal.WritableOptions;
487
+ class WritableBase extends Stream implements NodeJS.WritableStream {
488
+ /**
489
+ * Is `true` if it is safe to call `writable.write()`, which means
490
+ * the stream has not been destroyed, errored, or ended.
491
+ * @since v11.4.0
492
+ */
493
+ readonly writable: boolean;
494
+ /**
495
+ * Is `true` after `writable.end()` has been called. This property
496
+ * does not indicate whether the data has been flushed, for this use `writable.writableFinished` instead.
497
+ * @since v12.9.0
498
+ */
499
+ readonly writableEnded: boolean;
500
+ /**
501
+ * Is set to `true` immediately before the `'finish'` event is emitted.
502
+ * @since v12.6.0
503
+ */
504
+ readonly writableFinished: boolean;
505
+ /**
506
+ * Return the value of `highWaterMark` passed when creating this `Writable`.
507
+ * @since v9.3.0
508
+ */
509
+ readonly writableHighWaterMark: number;
510
+ /**
511
+ * This property contains the number of bytes (or objects) in the queue
512
+ * ready to be written. The value provides introspection data regarding
513
+ * the status of the `highWaterMark`.
514
+ * @since v9.4.0
515
+ */
516
+ readonly writableLength: number;
517
+ /**
518
+ * Getter for the property `objectMode` of a given `Writable` stream.
519
+ * @since v12.3.0
520
+ */
521
+ readonly writableObjectMode: boolean;
522
+ /**
523
+ * Number of times `writable.uncork()` needs to be
524
+ * called in order to fully uncork the stream.
525
+ * @since v13.2.0, v12.16.0
526
+ */
527
+ readonly writableCorked: number;
528
+ /**
529
+ * Is `true` after `writable.destroy()` has been called.
530
+ * @since v8.0.0
531
+ */
532
+ destroyed: boolean;
533
+ /**
534
+ * Is `true` after `'close'` has been emitted.
535
+ * @since v18.0.0
536
+ */
537
+ readonly closed: boolean;
538
+ /**
539
+ * Returns error if the stream has been destroyed with an error.
540
+ * @since v18.0.0
541
+ */
542
+ readonly errored: Error | null;
543
+ /**
544
+ * Is `true` if the stream's buffer has been full and stream will emit `'drain'`.
545
+ * @since v15.2.0, v14.17.0
546
+ */
547
+ readonly writableNeedDrain: boolean;
548
+ constructor(opts?: WritableOptions);
549
+ _write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
550
+ _writev?(
551
+ chunks: Array<{
552
+ chunk: any;
553
+ encoding: BufferEncoding;
554
+ }>,
555
+ callback: (error?: Error | null) => void
556
+ ): void;
557
+ _construct?(callback: (error?: Error | null) => void): void;
558
+ _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
559
+ _final(callback: (error?: Error | null) => void): void;
560
+ /**
561
+ * The `writable.write()` method writes some data to the stream, and calls the
562
+ * supplied `callback` once the data has been fully handled. If an error
563
+ * occurs, the `callback` will be called with the error as its
564
+ * first argument. The `callback` is called asynchronously and before `'error'` is
565
+ * emitted.
566
+ *
567
+ * The return value is `true` if the internal buffer is less than the`highWaterMark` configured when the stream was created after admitting `chunk`.
568
+ * If `false` is returned, further attempts to write data to the stream should
569
+ * stop until the `'drain'` event is emitted.
570
+ *
571
+ * While a stream is not draining, calls to `write()` will buffer `chunk`, and
572
+ * return false. Once all currently buffered chunks are drained (accepted for
573
+ * delivery by the operating system), the `'drain'` event will be emitted.
574
+ * Once `write()` returns false, do not write more chunks
575
+ * until the `'drain'` event is emitted. While calling `write()` on a stream that
576
+ * is not draining is allowed, Node.js will buffer all written chunks until
577
+ * maximum memory usage occurs, at which point it will abort unconditionally.
578
+ * Even before it aborts, high memory usage will cause poor garbage collector
579
+ * performance and high RSS (which is not typically released back to the system,
580
+ * even after the memory is no longer required). Since TCP sockets may never
581
+ * drain if the remote peer does not read the data, writing a socket that is
582
+ * not draining may lead to a remotely exploitable vulnerability.
583
+ *
584
+ * Writing data while the stream is not draining is particularly
585
+ * problematic for a `Transform`, because the `Transform` streams are paused
586
+ * by default until they are piped or a `'data'` or `'readable'` event handler
587
+ * is added.
588
+ *
589
+ * If the data to be written can be generated or fetched on demand, it is
590
+ * recommended to encapsulate the logic into a `Readable` and use {@link pipe}. However, if calling `write()` is preferred, it is
591
+ * possible to respect backpressure and avoid memory issues using the `'drain'` event:
592
+ *
593
+ * ```js
594
+ * function write(data, cb) {
595
+ * if (!stream.write(data)) {
596
+ * stream.once('drain', cb);
597
+ * } else {
598
+ * process.nextTick(cb);
599
+ * }
600
+ * }
601
+ *
602
+ * // Wait for cb to be called before doing any other write.
603
+ * write('hello', () => {
604
+ * console.log('Write completed, do more writes now.');
605
+ * });
606
+ * ```
607
+ *
608
+ * A `Writable` stream in object mode will always ignore the `encoding` argument.
609
+ * @since v0.9.4
610
+ * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a string, `Buffer` or `Uint8Array`. For object mode streams, `chunk` may be any
611
+ * JavaScript value other than `null`.
612
+ * @param [encoding='utf8'] The encoding, if `chunk` is a string.
613
+ * @param callback Callback for when this chunk of data is flushed.
614
+ * @return `false` if the stream wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
615
+ */
616
+ write(chunk: any, callback?: (error: Error | null | undefined) => void): boolean;
617
+ write(chunk: any, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): boolean;
618
+ /**
619
+ * The `writable.setDefaultEncoding()` method sets the default `encoding` for a `Writable` stream.
620
+ * @since v0.11.15
621
+ * @param encoding The new default encoding
622
+ */
623
+ setDefaultEncoding(encoding: BufferEncoding): this;
624
+ /**
625
+ * Calling the `writable.end()` method signals that no more data will be written
626
+ * to the `Writable`. The optional `chunk` and `encoding` arguments allow one
627
+ * final additional chunk of data to be written immediately before closing the
628
+ * stream.
629
+ *
630
+ * Calling the {@link write} method after calling {@link end} will raise an error.
631
+ *
632
+ * ```js
633
+ * // Write 'hello, ' and then end with 'world!'.
634
+ * const fs = require('node:fs');
635
+ * const file = fs.createWriteStream('example.txt');
636
+ * file.write('hello, ');
637
+ * file.end('world!');
638
+ * // Writing more now is not allowed!
639
+ * ```
640
+ * @since v0.9.4
641
+ * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a string, `Buffer` or `Uint8Array`. For object mode streams, `chunk` may be any
642
+ * JavaScript value other than `null`.
643
+ * @param encoding The encoding if `chunk` is a string
644
+ * @param callback Callback for when the stream is finished.
645
+ */
646
+ end(cb?: () => void): this;
647
+ end(chunk: any, cb?: () => void): this;
648
+ end(chunk: any, encoding: BufferEncoding, cb?: () => void): this;
649
+ /**
650
+ * The `writable.cork()` method forces all written data to be buffered in memory.
651
+ * The buffered data will be flushed when either the {@link uncork} or {@link end} methods are called.
652
+ *
653
+ * The primary intent of `writable.cork()` is to accommodate a situation in which
654
+ * several small chunks are written to the stream in rapid succession. Instead of
655
+ * immediately forwarding them to the underlying destination, `writable.cork()`buffers all the chunks until `writable.uncork()` is called, which will pass them
656
+ * all to `writable._writev()`, if present. This prevents a head-of-line blocking
657
+ * situation where data is being buffered while waiting for the first small chunk
658
+ * to be processed. However, use of `writable.cork()` without implementing`writable._writev()` may have an adverse effect on throughput.
659
+ *
660
+ * See also: `writable.uncork()`, `writable._writev()`.
661
+ * @since v0.11.2
662
+ */
663
+ cork(): void;
664
+ /**
665
+ * The `writable.uncork()` method flushes all data buffered since {@link cork} was called.
666
+ *
667
+ * When using `writable.cork()` and `writable.uncork()` to manage the buffering
668
+ * of writes to a stream, defer calls to `writable.uncork()` using`process.nextTick()`. Doing so allows batching of all`writable.write()` calls that occur within a given Node.js event
669
+ * loop phase.
670
+ *
671
+ * ```js
672
+ * stream.cork();
673
+ * stream.write('some ');
674
+ * stream.write('data ');
675
+ * process.nextTick(() => stream.uncork());
676
+ * ```
677
+ *
678
+ * If the `writable.cork()` method is called multiple times on a stream, the
679
+ * same number of calls to `writable.uncork()` must be called to flush the buffered
680
+ * data.
681
+ *
682
+ * ```js
683
+ * stream.cork();
684
+ * stream.write('some ');
685
+ * stream.cork();
686
+ * stream.write('data ');
687
+ * process.nextTick(() => {
688
+ * stream.uncork();
689
+ * // The data will not be flushed until uncork() is called a second time.
690
+ * stream.uncork();
691
+ * });
692
+ * ```
693
+ *
694
+ * See also: `writable.cork()`.
695
+ * @since v0.11.2
696
+ */
697
+ uncork(): void;
698
+ /**
699
+ * Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`event (unless `emitClose` is set to `false`). After this call, the writable
700
+ * stream has ended and subsequent calls to `write()` or `end()` will result in
701
+ * an `ERR_STREAM_DESTROYED` error.
702
+ * This is a destructive and immediate way to destroy a stream. Previous calls to`write()` may not have drained, and may trigger an `ERR_STREAM_DESTROYED` error.
703
+ * Use `end()` instead of destroy if data should flush before close, or wait for
704
+ * the `'drain'` event before destroying the stream.
705
+ *
706
+ * Once `destroy()` has been called any further calls will be a no-op and no
707
+ * further errors except from `_destroy()` may be emitted as `'error'`.
708
+ *
709
+ * Implementors should not override this method,
710
+ * but instead implement `writable._destroy()`.
711
+ * @since v8.0.0
712
+ * @param error Optional, an error to emit with `'error'` event.
713
+ */
714
+ destroy(error?: Error): this;
715
+ /**
716
+ * Event emitter
717
+ * The defined events on documents including:
718
+ * 1. close
719
+ * 2. drain
720
+ * 3. error
721
+ * 4. finish
722
+ * 5. pipe
723
+ * 6. unpipe
724
+ */
725
+ addListener(event: 'close', listener: () => void): this;
726
+ addListener(event: 'drain', listener: () => void): this;
727
+ addListener(event: 'error', listener: (err: Error) => void): this;
728
+ addListener(event: 'finish', listener: () => void): this;
729
+ addListener(event: 'pipe', listener: (src: Readable) => void): this;
730
+ addListener(event: 'unpipe', listener: (src: Readable) => void): this;
731
+ addListener(event: string | symbol, listener: (...args: any[]) => void): this;
732
+ emit(event: 'close'): boolean;
733
+ emit(event: 'drain'): boolean;
734
+ emit(event: 'error', err: Error): boolean;
735
+ emit(event: 'finish'): boolean;
736
+ emit(event: 'pipe', src: Readable): boolean;
737
+ emit(event: 'unpipe', src: Readable): boolean;
738
+ emit(event: string | symbol, ...args: any[]): boolean;
739
+ on(event: 'close', listener: () => void): this;
740
+ on(event: 'drain', listener: () => void): this;
741
+ on(event: 'error', listener: (err: Error) => void): this;
742
+ on(event: 'finish', listener: () => void): this;
743
+ on(event: 'pipe', listener: (src: Readable) => void): this;
744
+ on(event: 'unpipe', listener: (src: Readable) => void): this;
745
+ on(event: string | symbol, listener: (...args: any[]) => void): this;
746
+ once(event: 'close', listener: () => void): this;
747
+ once(event: 'drain', listener: () => void): this;
748
+ once(event: 'error', listener: (err: Error) => void): this;
749
+ once(event: 'finish', listener: () => void): this;
750
+ once(event: 'pipe', listener: (src: Readable) => void): this;
751
+ once(event: 'unpipe', listener: (src: Readable) => void): this;
752
+ once(event: string | symbol, listener: (...args: any[]) => void): this;
753
+ prependListener(event: 'close', listener: () => void): this;
754
+ prependListener(event: 'drain', listener: () => void): this;
755
+ prependListener(event: 'error', listener: (err: Error) => void): this;
756
+ prependListener(event: 'finish', listener: () => void): this;
757
+ prependListener(event: 'pipe', listener: (src: Readable) => void): this;
758
+ prependListener(event: 'unpipe', listener: (src: Readable) => void): this;
759
+ prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
760
+ prependOnceListener(event: 'close', listener: () => void): this;
761
+ prependOnceListener(event: 'drain', listener: () => void): this;
762
+ prependOnceListener(event: 'error', listener: (err: Error) => void): this;
763
+ prependOnceListener(event: 'finish', listener: () => void): this;
764
+ prependOnceListener(event: 'pipe', listener: (src: Readable) => void): this;
765
+ prependOnceListener(event: 'unpipe', listener: (src: Readable) => void): this;
766
+ prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
767
+ removeListener(event: 'close', listener: () => void): this;
768
+ removeListener(event: 'drain', listener: () => void): this;
769
+ removeListener(event: 'error', listener: (err: Error) => void): this;
770
+ removeListener(event: 'finish', listener: () => void): this;
771
+ removeListener(event: 'pipe', listener: (src: Readable) => void): this;
772
+ removeListener(event: 'unpipe', listener: (src: Readable) => void): this;
773
+ removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
774
+ }
37
775
  namespace internal {
38
776
  class Stream extends internal {
39
777
  constructor(opts?: ReadableOptions);
@@ -53,458 +791,19 @@ declare module 'stream' {
53
791
  /**
54
792
  * @since v0.9.4
55
793
  */
56
- class Readable extends Stream implements NodeJS.ReadableStream {
57
- /**
58
- * A utility method for creating Readable Streams out of iterators.
59
- */
60
- static from(iterable: Iterable<any> | AsyncIterable<any>, options?: ReadableOptions): Readable;
794
+ class Readable extends ReadableBase {
61
795
  /**
62
796
  * A utility method for creating a `Readable` from a web `ReadableStream`.
63
797
  * @since v17.0.0
64
798
  * @experimental
65
799
  */
66
800
  static fromWeb(readableStream: streamWeb.ReadableStream, options?: Pick<ReadableOptions, 'encoding' | 'highWaterMark' | 'objectMode' | 'signal'>): Readable;
67
- /**
68
- * Returns whether the stream has been read from or cancelled.
69
- * @since v16.8.0
70
- */
71
- static isDisturbed(stream: Readable | NodeJS.ReadableStream): boolean;
72
801
  /**
73
802
  * A utility method for creating a web `ReadableStream` from a `Readable`.
74
803
  * @since v17.0.0
75
804
  * @experimental
76
805
  */
77
806
  static toWeb(streamReadable: Readable): streamWeb.ReadableStream;
78
- /**
79
- * Returns whether the stream was destroyed or errored before emitting `'end'`.
80
- * @since v16.8.0
81
- * @experimental
82
- */
83
- readonly readableAborted: boolean;
84
- /**
85
- * Is `true` if it is safe to call `readable.read()`, which means
86
- * the stream has not been destroyed or emitted `'error'` or `'end'`.
87
- * @since v11.4.0
88
- */
89
- readable: boolean;
90
- /**
91
- * Returns whether `'data'` has been emitted.
92
- * @since v16.7.0, v14.18.0
93
- * @experimental
94
- */
95
- readonly readableDidRead: boolean;
96
- /**
97
- * Getter for the property `encoding` of a given `Readable` stream. The `encoding`property can be set using the `readable.setEncoding()` method.
98
- * @since v12.7.0
99
- */
100
- readonly readableEncoding: BufferEncoding | null;
101
- /**
102
- * Becomes `true` when `'end'` event is emitted.
103
- * @since v12.9.0
104
- */
105
- readonly readableEnded: boolean;
106
- /**
107
- * This property reflects the current state of a `Readable` stream as described
108
- * in the `Three states` section.
109
- * @since v9.4.0
110
- */
111
- readonly readableFlowing: boolean | null;
112
- /**
113
- * Returns the value of `highWaterMark` passed when creating this `Readable`.
114
- * @since v9.3.0
115
- */
116
- readonly readableHighWaterMark: number;
117
- /**
118
- * This property contains the number of bytes (or objects) in the queue
119
- * ready to be read. The value provides introspection data regarding
120
- * the status of the `highWaterMark`.
121
- * @since v9.4.0
122
- */
123
- readonly readableLength: number;
124
- /**
125
- * Getter for the property `objectMode` of a given `Readable` stream.
126
- * @since v12.3.0
127
- */
128
- readonly readableObjectMode: boolean;
129
- /**
130
- * Is `true` after `readable.destroy()` has been called.
131
- * @since v8.0.0
132
- */
133
- destroyed: boolean;
134
- /**
135
- * Is `true` after `'close'` has been emitted.
136
- * @since v18.0.0
137
- */
138
- readonly closed: boolean;
139
- /**
140
- * Returns error if the stream has been destroyed with an error.
141
- * @since v18.0.0
142
- */
143
- readonly errored: Error | null;
144
- constructor(opts?: ReadableOptions);
145
- _construct?(callback: (error?: Error | null) => void): void;
146
- _read(size: number): void;
147
- /**
148
- * The `readable.read()` method reads data out of the internal buffer and
149
- * returns it. If no data is available to be read, `null` is returned. By default,
150
- * the data is returned as a `Buffer` object unless an encoding has been
151
- * specified using the `readable.setEncoding()` method or the stream is operating
152
- * in object mode.
153
- *
154
- * The optional `size` argument specifies a specific number of bytes to read. If`size` bytes are not available to be read, `null` will be returned _unless_the stream has ended, in which
155
- * case all of the data remaining in the internal
156
- * buffer will be returned.
157
- *
158
- * If the `size` argument is not specified, all of the data contained in the
159
- * internal buffer will be returned.
160
- *
161
- * The `size` argument must be less than or equal to 1 GiB.
162
- *
163
- * The `readable.read()` method should only be called on `Readable` streams
164
- * operating in paused mode. In flowing mode, `readable.read()` is called
165
- * automatically until the internal buffer is fully drained.
166
- *
167
- * ```js
168
- * const readable = getReadableStreamSomehow();
169
- *
170
- * // 'readable' may be triggered multiple times as data is buffered in
171
- * readable.on('readable', () => {
172
- * let chunk;
173
- * console.log('Stream is readable (new data received in buffer)');
174
- * // Use a loop to make sure we read all currently available data
175
- * while (null !== (chunk = readable.read())) {
176
- * console.log(`Read ${chunk.length} bytes of data...`);
177
- * }
178
- * });
179
- *
180
- * // 'end' will be triggered once when there is no more data available
181
- * readable.on('end', () => {
182
- * console.log('Reached end of stream.');
183
- * });
184
- * ```
185
- *
186
- * Each call to `readable.read()` returns a chunk of data, or `null`. The chunks
187
- * are not concatenated. A `while` loop is necessary to consume all data
188
- * currently in the buffer. When reading a large file `.read()` may return `null`,
189
- * having consumed all buffered content so far, but there is still more data to
190
- * come not yet buffered. In this case a new `'readable'` event will be emitted
191
- * when there is more data in the buffer. Finally the `'end'` event will be
192
- * emitted when there is no more data to come.
193
- *
194
- * Therefore to read a file's whole contents from a `readable`, it is necessary
195
- * to collect chunks across multiple `'readable'` events:
196
- *
197
- * ```js
198
- * const chunks = [];
199
- *
200
- * readable.on('readable', () => {
201
- * let chunk;
202
- * while (null !== (chunk = readable.read())) {
203
- * chunks.push(chunk);
204
- * }
205
- * });
206
- *
207
- * readable.on('end', () => {
208
- * const content = chunks.join('');
209
- * });
210
- * ```
211
- *
212
- * A `Readable` stream in object mode will always return a single item from
213
- * a call to `readable.read(size)`, regardless of the value of the`size` argument.
214
- *
215
- * If the `readable.read()` method returns a chunk of data, a `'data'` event will
216
- * also be emitted.
217
- *
218
- * Calling {@link read} after the `'end'` event has
219
- * been emitted will return `null`. No runtime error will be raised.
220
- * @since v0.9.4
221
- * @param size Optional argument to specify how much data to read.
222
- */
223
- read(size?: number): any;
224
- /**
225
- * The `readable.setEncoding()` method sets the character encoding for
226
- * data read from the `Readable` stream.
227
- *
228
- * By default, no encoding is assigned and stream data will be returned as`Buffer` objects. Setting an encoding causes the stream data
229
- * to be returned as strings of the specified encoding rather than as `Buffer`objects. For instance, calling `readable.setEncoding('utf8')` will cause the
230
- * output data to be interpreted as UTF-8 data, and passed as strings. Calling`readable.setEncoding('hex')` will cause the data to be encoded in hexadecimal
231
- * string format.
232
- *
233
- * The `Readable` stream will properly handle multi-byte characters delivered
234
- * through the stream that would otherwise become improperly decoded if simply
235
- * pulled from the stream as `Buffer` objects.
236
- *
237
- * ```js
238
- * const readable = getReadableStreamSomehow();
239
- * readable.setEncoding('utf8');
240
- * readable.on('data', (chunk) => {
241
- * assert.equal(typeof chunk, 'string');
242
- * console.log('Got %d characters of string data:', chunk.length);
243
- * });
244
- * ```
245
- * @since v0.9.4
246
- * @param encoding The encoding to use.
247
- */
248
- setEncoding(encoding: BufferEncoding): this;
249
- /**
250
- * The `readable.pause()` method will cause a stream in flowing mode to stop
251
- * emitting `'data'` events, switching out of flowing mode. Any data that
252
- * becomes available will remain in the internal buffer.
253
- *
254
- * ```js
255
- * const readable = getReadableStreamSomehow();
256
- * readable.on('data', (chunk) => {
257
- * console.log(`Received ${chunk.length} bytes of data.`);
258
- * readable.pause();
259
- * console.log('There will be no additional data for 1 second.');
260
- * setTimeout(() => {
261
- * console.log('Now data will start flowing again.');
262
- * readable.resume();
263
- * }, 1000);
264
- * });
265
- * ```
266
- *
267
- * The `readable.pause()` method has no effect if there is a `'readable'`event listener.
268
- * @since v0.9.4
269
- */
270
- pause(): this;
271
- /**
272
- * The `readable.resume()` method causes an explicitly paused `Readable` stream to
273
- * resume emitting `'data'` events, switching the stream into flowing mode.
274
- *
275
- * The `readable.resume()` method can be used to fully consume the data from a
276
- * stream without actually processing any of that data:
277
- *
278
- * ```js
279
- * getReadableStreamSomehow()
280
- * .resume()
281
- * .on('end', () => {
282
- * console.log('Reached the end, but did not read anything.');
283
- * });
284
- * ```
285
- *
286
- * The `readable.resume()` method has no effect if there is a `'readable'`event listener.
287
- * @since v0.9.4
288
- */
289
- resume(): this;
290
- /**
291
- * The `readable.isPaused()` method returns the current operating state of the`Readable`. This is used primarily by the mechanism that underlies the`readable.pipe()` method. In most
292
- * typical cases, there will be no reason to
293
- * use this method directly.
294
- *
295
- * ```js
296
- * const readable = new stream.Readable();
297
- *
298
- * readable.isPaused(); // === false
299
- * readable.pause();
300
- * readable.isPaused(); // === true
301
- * readable.resume();
302
- * readable.isPaused(); // === false
303
- * ```
304
- * @since v0.11.14
305
- */
306
- isPaused(): boolean;
307
- /**
308
- * The `readable.unpipe()` method detaches a `Writable` stream previously attached
309
- * using the {@link pipe} method.
310
- *
311
- * If the `destination` is not specified, then _all_ pipes are detached.
312
- *
313
- * If the `destination` is specified, but no pipe is set up for it, then
314
- * the method does nothing.
315
- *
316
- * ```js
317
- * const fs = require('node:fs');
318
- * const readable = getReadableStreamSomehow();
319
- * const writable = fs.createWriteStream('file.txt');
320
- * // All the data from readable goes into 'file.txt',
321
- * // but only for the first second.
322
- * readable.pipe(writable);
323
- * setTimeout(() => {
324
- * console.log('Stop writing to file.txt.');
325
- * readable.unpipe(writable);
326
- * console.log('Manually close the file stream.');
327
- * writable.end();
328
- * }, 1000);
329
- * ```
330
- * @since v0.9.4
331
- * @param destination Optional specific stream to unpipe
332
- */
333
- unpipe(destination?: NodeJS.WritableStream): this;
334
- /**
335
- * Passing `chunk` as `null` signals the end of the stream (EOF) and behaves the
336
- * same as `readable.push(null)`, after which no more data can be written. The EOF
337
- * signal is put at the end of the buffer and any buffered data will still be
338
- * flushed.
339
- *
340
- * The `readable.unshift()` method pushes a chunk of data back into the internal
341
- * buffer. This is useful in certain situations where a stream is being consumed by
342
- * code that needs to "un-consume" some amount of data that it has optimistically
343
- * pulled out of the source, so that the data can be passed on to some other party.
344
- *
345
- * The `stream.unshift(chunk)` method cannot be called after the `'end'` event
346
- * has been emitted or a runtime error will be thrown.
347
- *
348
- * Developers using `stream.unshift()` often should consider switching to
349
- * use of a `Transform` stream instead. See the `API for stream implementers` section for more information.
350
- *
351
- * ```js
352
- * // Pull off a header delimited by \n\n.
353
- * // Use unshift() if we get too much.
354
- * // Call the callback with (error, header, stream).
355
- * const { StringDecoder } = require('node:string_decoder');
356
- * function parseHeader(stream, callback) {
357
- * stream.on('error', callback);
358
- * stream.on('readable', onReadable);
359
- * const decoder = new StringDecoder('utf8');
360
- * let header = '';
361
- * function onReadable() {
362
- * let chunk;
363
- * while (null !== (chunk = stream.read())) {
364
- * const str = decoder.write(chunk);
365
- * if (str.includes('\n\n')) {
366
- * // Found the header boundary.
367
- * const split = str.split(/\n\n/);
368
- * header += split.shift();
369
- * const remaining = split.join('\n\n');
370
- * const buf = Buffer.from(remaining, 'utf8');
371
- * stream.removeListener('error', callback);
372
- * // Remove the 'readable' listener before unshifting.
373
- * stream.removeListener('readable', onReadable);
374
- * if (buf.length)
375
- * stream.unshift(buf);
376
- * // Now the body of the message can be read from the stream.
377
- * callback(null, header, stream);
378
- * return;
379
- * }
380
- * // Still reading the header.
381
- * header += str;
382
- * }
383
- * }
384
- * }
385
- * ```
386
- *
387
- * Unlike {@link push}, `stream.unshift(chunk)` will not
388
- * end the reading process by resetting the internal reading state of the stream.
389
- * This can cause unexpected results if `readable.unshift()` is called during a
390
- * read (i.e. from within a {@link _read} implementation on a
391
- * custom stream). Following the call to `readable.unshift()` with an immediate {@link push} will reset the reading state appropriately,
392
- * however it is best to simply avoid calling `readable.unshift()` while in the
393
- * process of performing a read.
394
- * @since v0.9.11
395
- * @param chunk Chunk of data to unshift onto the read queue. For streams not operating in object mode, `chunk` must be a string, `Buffer`, `Uint8Array`, or `null`. For object mode
396
- * streams, `chunk` may be any JavaScript value.
397
- * @param encoding Encoding of string chunks. Must be a valid `Buffer` encoding, such as `'utf8'` or `'ascii'`.
398
- */
399
- unshift(chunk: any, encoding?: BufferEncoding): void;
400
- /**
401
- * Prior to Node.js 0.10, streams did not implement the entire `node:stream`module API as it is currently defined. (See `Compatibility` for more
402
- * information.)
403
- *
404
- * When using an older Node.js library that emits `'data'` events and has a {@link pause} method that is advisory only, the`readable.wrap()` method can be used to create a `Readable`
405
- * stream that uses
406
- * the old stream as its data source.
407
- *
408
- * It will rarely be necessary to use `readable.wrap()` but the method has been
409
- * provided as a convenience for interacting with older Node.js applications and
410
- * libraries.
411
- *
412
- * ```js
413
- * const { OldReader } = require('./old-api-module.js');
414
- * const { Readable } = require('node:stream');
415
- * const oreader = new OldReader();
416
- * const myReader = new Readable().wrap(oreader);
417
- *
418
- * myReader.on('readable', () => {
419
- * myReader.read(); // etc.
420
- * });
421
- * ```
422
- * @since v0.9.4
423
- * @param stream An "old style" readable stream
424
- */
425
- wrap(stream: NodeJS.ReadableStream): this;
426
- push(chunk: any, encoding?: BufferEncoding): boolean;
427
- _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
428
- /**
429
- * Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`event (unless `emitClose` is set to `false`). After this call, the readable
430
- * stream will release any internal resources and subsequent calls to `push()`will be ignored.
431
- *
432
- * Once `destroy()` has been called any further calls will be a no-op and no
433
- * further errors except from `_destroy()` may be emitted as `'error'`.
434
- *
435
- * Implementors should not override this method, but instead implement `readable._destroy()`.
436
- * @since v8.0.0
437
- * @param error Error which will be passed as payload in `'error'` event
438
- */
439
- destroy(error?: Error): this;
440
- /**
441
- * Event emitter
442
- * The defined events on documents including:
443
- * 1. close
444
- * 2. data
445
- * 3. end
446
- * 4. error
447
- * 5. pause
448
- * 6. readable
449
- * 7. resume
450
- */
451
- addListener(event: 'close', listener: () => void): this;
452
- addListener(event: 'data', listener: (chunk: any) => void): this;
453
- addListener(event: 'end', listener: () => void): this;
454
- addListener(event: 'error', listener: (err: Error) => void): this;
455
- addListener(event: 'pause', listener: () => void): this;
456
- addListener(event: 'readable', listener: () => void): this;
457
- addListener(event: 'resume', listener: () => void): this;
458
- addListener(event: string | symbol, listener: (...args: any[]) => void): this;
459
- emit(event: 'close'): boolean;
460
- emit(event: 'data', chunk: any): boolean;
461
- emit(event: 'end'): boolean;
462
- emit(event: 'error', err: Error): boolean;
463
- emit(event: 'pause'): boolean;
464
- emit(event: 'readable'): boolean;
465
- emit(event: 'resume'): boolean;
466
- emit(event: string | symbol, ...args: any[]): boolean;
467
- on(event: 'close', listener: () => void): this;
468
- on(event: 'data', listener: (chunk: any) => void): this;
469
- on(event: 'end', listener: () => void): this;
470
- on(event: 'error', listener: (err: Error) => void): this;
471
- on(event: 'pause', listener: () => void): this;
472
- on(event: 'readable', listener: () => void): this;
473
- on(event: 'resume', listener: () => void): this;
474
- on(event: string | symbol, listener: (...args: any[]) => void): this;
475
- once(event: 'close', listener: () => void): this;
476
- once(event: 'data', listener: (chunk: any) => void): this;
477
- once(event: 'end', listener: () => void): this;
478
- once(event: 'error', listener: (err: Error) => void): this;
479
- once(event: 'pause', listener: () => void): this;
480
- once(event: 'readable', listener: () => void): this;
481
- once(event: 'resume', listener: () => void): this;
482
- once(event: string | symbol, listener: (...args: any[]) => void): this;
483
- prependListener(event: 'close', listener: () => void): this;
484
- prependListener(event: 'data', listener: (chunk: any) => void): this;
485
- prependListener(event: 'end', listener: () => void): this;
486
- prependListener(event: 'error', listener: (err: Error) => void): this;
487
- prependListener(event: 'pause', listener: () => void): this;
488
- prependListener(event: 'readable', listener: () => void): this;
489
- prependListener(event: 'resume', listener: () => void): this;
490
- prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
491
- prependOnceListener(event: 'close', listener: () => void): this;
492
- prependOnceListener(event: 'data', listener: (chunk: any) => void): this;
493
- prependOnceListener(event: 'end', listener: () => void): this;
494
- prependOnceListener(event: 'error', listener: (err: Error) => void): this;
495
- prependOnceListener(event: 'pause', listener: () => void): this;
496
- prependOnceListener(event: 'readable', listener: () => void): this;
497
- prependOnceListener(event: 'resume', listener: () => void): this;
498
- prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
499
- removeListener(event: 'close', listener: () => void): this;
500
- removeListener(event: 'data', listener: (chunk: any) => void): this;
501
- removeListener(event: 'end', listener: () => void): this;
502
- removeListener(event: 'error', listener: (err: Error) => void): this;
503
- removeListener(event: 'pause', listener: () => void): this;
504
- removeListener(event: 'readable', listener: () => void): this;
505
- removeListener(event: 'resume', listener: () => void): this;
506
- removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
507
- [Symbol.asyncIterator](): AsyncIterableIterator<any>;
508
807
  }
509
808
  interface WritableOptions extends StreamOptions<Writable> {
510
809
  decodeStrings?: boolean | undefined;
@@ -523,7 +822,7 @@ declare module 'stream' {
523
822
  /**
524
823
  * @since v0.9.4
525
824
  */
526
- class Writable extends Stream implements NodeJS.WritableStream {
825
+ class Writable extends WritableBase {
527
826
  /**
528
827
  * A utility method for creating a `Writable` from a web `WritableStream`.
529
828
  * @since v17.0.0
@@ -536,292 +835,6 @@ declare module 'stream' {
536
835
  * @experimental
537
836
  */
538
837
  static toWeb(streamWritable: Writable): streamWeb.WritableStream;
539
- /**
540
- * Is `true` if it is safe to call `writable.write()`, which means
541
- * the stream has not been destroyed, errored, or ended.
542
- * @since v11.4.0
543
- */
544
- readonly writable: boolean;
545
- /**
546
- * Is `true` after `writable.end()` has been called. This property
547
- * does not indicate whether the data has been flushed, for this use `writable.writableFinished` instead.
548
- * @since v12.9.0
549
- */
550
- readonly writableEnded: boolean;
551
- /**
552
- * Is set to `true` immediately before the `'finish'` event is emitted.
553
- * @since v12.6.0
554
- */
555
- readonly writableFinished: boolean;
556
- /**
557
- * Return the value of `highWaterMark` passed when creating this `Writable`.
558
- * @since v9.3.0
559
- */
560
- readonly writableHighWaterMark: number;
561
- /**
562
- * This property contains the number of bytes (or objects) in the queue
563
- * ready to be written. The value provides introspection data regarding
564
- * the status of the `highWaterMark`.
565
- * @since v9.4.0
566
- */
567
- readonly writableLength: number;
568
- /**
569
- * Getter for the property `objectMode` of a given `Writable` stream.
570
- * @since v12.3.0
571
- */
572
- readonly writableObjectMode: boolean;
573
- /**
574
- * Number of times `writable.uncork()` needs to be
575
- * called in order to fully uncork the stream.
576
- * @since v13.2.0, v12.16.0
577
- */
578
- readonly writableCorked: number;
579
- /**
580
- * Is `true` after `writable.destroy()` has been called.
581
- * @since v8.0.0
582
- */
583
- destroyed: boolean;
584
- /**
585
- * Is `true` after `'close'` has been emitted.
586
- * @since v18.0.0
587
- */
588
- readonly closed: boolean;
589
- /**
590
- * Returns error if the stream has been destroyed with an error.
591
- * @since v18.0.0
592
- */
593
- readonly errored: Error | null;
594
- /**
595
- * Is `true` if the stream's buffer has been full and stream will emit `'drain'`.
596
- * @since v15.2.0, v14.17.0
597
- */
598
- readonly writableNeedDrain: boolean;
599
- constructor(opts?: WritableOptions);
600
- _write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
601
- _writev?(
602
- chunks: Array<{
603
- chunk: any;
604
- encoding: BufferEncoding;
605
- }>,
606
- callback: (error?: Error | null) => void
607
- ): void;
608
- _construct?(callback: (error?: Error | null) => void): void;
609
- _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
610
- _final(callback: (error?: Error | null) => void): void;
611
- /**
612
- * The `writable.write()` method writes some data to the stream, and calls the
613
- * supplied `callback` once the data has been fully handled. If an error
614
- * occurs, the `callback` will be called with the error as its
615
- * first argument. The `callback` is called asynchronously and before `'error'` is
616
- * emitted.
617
- *
618
- * The return value is `true` if the internal buffer is less than the`highWaterMark` configured when the stream was created after admitting `chunk`.
619
- * If `false` is returned, further attempts to write data to the stream should
620
- * stop until the `'drain'` event is emitted.
621
- *
622
- * While a stream is not draining, calls to `write()` will buffer `chunk`, and
623
- * return false. Once all currently buffered chunks are drained (accepted for
624
- * delivery by the operating system), the `'drain'` event will be emitted.
625
- * Once `write()` returns false, do not write more chunks
626
- * until the `'drain'` event is emitted. While calling `write()` on a stream that
627
- * is not draining is allowed, Node.js will buffer all written chunks until
628
- * maximum memory usage occurs, at which point it will abort unconditionally.
629
- * Even before it aborts, high memory usage will cause poor garbage collector
630
- * performance and high RSS (which is not typically released back to the system,
631
- * even after the memory is no longer required). Since TCP sockets may never
632
- * drain if the remote peer does not read the data, writing a socket that is
633
- * not draining may lead to a remotely exploitable vulnerability.
634
- *
635
- * Writing data while the stream is not draining is particularly
636
- * problematic for a `Transform`, because the `Transform` streams are paused
637
- * by default until they are piped or a `'data'` or `'readable'` event handler
638
- * is added.
639
- *
640
- * If the data to be written can be generated or fetched on demand, it is
641
- * recommended to encapsulate the logic into a `Readable` and use {@link pipe}. However, if calling `write()` is preferred, it is
642
- * possible to respect backpressure and avoid memory issues using the `'drain'` event:
643
- *
644
- * ```js
645
- * function write(data, cb) {
646
- * if (!stream.write(data)) {
647
- * stream.once('drain', cb);
648
- * } else {
649
- * process.nextTick(cb);
650
- * }
651
- * }
652
- *
653
- * // Wait for cb to be called before doing any other write.
654
- * write('hello', () => {
655
- * console.log('Write completed, do more writes now.');
656
- * });
657
- * ```
658
- *
659
- * A `Writable` stream in object mode will always ignore the `encoding` argument.
660
- * @since v0.9.4
661
- * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a string, `Buffer` or `Uint8Array`. For object mode streams, `chunk` may be any
662
- * JavaScript value other than `null`.
663
- * @param [encoding='utf8'] The encoding, if `chunk` is a string.
664
- * @param callback Callback for when this chunk of data is flushed.
665
- * @return `false` if the stream wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
666
- */
667
- write(chunk: any, callback?: (error: Error | null | undefined) => void): boolean;
668
- write(chunk: any, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): boolean;
669
- /**
670
- * The `writable.setDefaultEncoding()` method sets the default `encoding` for a `Writable` stream.
671
- * @since v0.11.15
672
- * @param encoding The new default encoding
673
- */
674
- setDefaultEncoding(encoding: BufferEncoding): this;
675
- /**
676
- * Calling the `writable.end()` method signals that no more data will be written
677
- * to the `Writable`. The optional `chunk` and `encoding` arguments allow one
678
- * final additional chunk of data to be written immediately before closing the
679
- * stream.
680
- *
681
- * Calling the {@link write} method after calling {@link end} will raise an error.
682
- *
683
- * ```js
684
- * // Write 'hello, ' and then end with 'world!'.
685
- * const fs = require('node:fs');
686
- * const file = fs.createWriteStream('example.txt');
687
- * file.write('hello, ');
688
- * file.end('world!');
689
- * // Writing more now is not allowed!
690
- * ```
691
- * @since v0.9.4
692
- * @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a string, `Buffer` or `Uint8Array`. For object mode streams, `chunk` may be any
693
- * JavaScript value other than `null`.
694
- * @param encoding The encoding if `chunk` is a string
695
- * @param callback Callback for when the stream is finished.
696
- */
697
- end(cb?: () => void): this;
698
- end(chunk: any, cb?: () => void): this;
699
- end(chunk: any, encoding: BufferEncoding, cb?: () => void): this;
700
- /**
701
- * The `writable.cork()` method forces all written data to be buffered in memory.
702
- * The buffered data will be flushed when either the {@link uncork} or {@link end} methods are called.
703
- *
704
- * The primary intent of `writable.cork()` is to accommodate a situation in which
705
- * several small chunks are written to the stream in rapid succession. Instead of
706
- * immediately forwarding them to the underlying destination, `writable.cork()`buffers all the chunks until `writable.uncork()` is called, which will pass them
707
- * all to `writable._writev()`, if present. This prevents a head-of-line blocking
708
- * situation where data is being buffered while waiting for the first small chunk
709
- * to be processed. However, use of `writable.cork()` without implementing`writable._writev()` may have an adverse effect on throughput.
710
- *
711
- * See also: `writable.uncork()`, `writable._writev()`.
712
- * @since v0.11.2
713
- */
714
- cork(): void;
715
- /**
716
- * The `writable.uncork()` method flushes all data buffered since {@link cork} was called.
717
- *
718
- * When using `writable.cork()` and `writable.uncork()` to manage the buffering
719
- * of writes to a stream, defer calls to `writable.uncork()` using`process.nextTick()`. Doing so allows batching of all`writable.write()` calls that occur within a given Node.js event
720
- * loop phase.
721
- *
722
- * ```js
723
- * stream.cork();
724
- * stream.write('some ');
725
- * stream.write('data ');
726
- * process.nextTick(() => stream.uncork());
727
- * ```
728
- *
729
- * If the `writable.cork()` method is called multiple times on a stream, the
730
- * same number of calls to `writable.uncork()` must be called to flush the buffered
731
- * data.
732
- *
733
- * ```js
734
- * stream.cork();
735
- * stream.write('some ');
736
- * stream.cork();
737
- * stream.write('data ');
738
- * process.nextTick(() => {
739
- * stream.uncork();
740
- * // The data will not be flushed until uncork() is called a second time.
741
- * stream.uncork();
742
- * });
743
- * ```
744
- *
745
- * See also: `writable.cork()`.
746
- * @since v0.11.2
747
- */
748
- uncork(): void;
749
- /**
750
- * Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`event (unless `emitClose` is set to `false`). After this call, the writable
751
- * stream has ended and subsequent calls to `write()` or `end()` will result in
752
- * an `ERR_STREAM_DESTROYED` error.
753
- * This is a destructive and immediate way to destroy a stream. Previous calls to`write()` may not have drained, and may trigger an `ERR_STREAM_DESTROYED` error.
754
- * Use `end()` instead of destroy if data should flush before close, or wait for
755
- * the `'drain'` event before destroying the stream.
756
- *
757
- * Once `destroy()` has been called any further calls will be a no-op and no
758
- * further errors except from `_destroy()` may be emitted as `'error'`.
759
- *
760
- * Implementors should not override this method,
761
- * but instead implement `writable._destroy()`.
762
- * @since v8.0.0
763
- * @param error Optional, an error to emit with `'error'` event.
764
- */
765
- destroy(error?: Error): this;
766
- /**
767
- * Event emitter
768
- * The defined events on documents including:
769
- * 1. close
770
- * 2. drain
771
- * 3. error
772
- * 4. finish
773
- * 5. pipe
774
- * 6. unpipe
775
- */
776
- addListener(event: 'close', listener: () => void): this;
777
- addListener(event: 'drain', listener: () => void): this;
778
- addListener(event: 'error', listener: (err: Error) => void): this;
779
- addListener(event: 'finish', listener: () => void): this;
780
- addListener(event: 'pipe', listener: (src: Readable) => void): this;
781
- addListener(event: 'unpipe', listener: (src: Readable) => void): this;
782
- addListener(event: string | symbol, listener: (...args: any[]) => void): this;
783
- emit(event: 'close'): boolean;
784
- emit(event: 'drain'): boolean;
785
- emit(event: 'error', err: Error): boolean;
786
- emit(event: 'finish'): boolean;
787
- emit(event: 'pipe', src: Readable): boolean;
788
- emit(event: 'unpipe', src: Readable): boolean;
789
- emit(event: string | symbol, ...args: any[]): boolean;
790
- on(event: 'close', listener: () => void): this;
791
- on(event: 'drain', listener: () => void): this;
792
- on(event: 'error', listener: (err: Error) => void): this;
793
- on(event: 'finish', listener: () => void): this;
794
- on(event: 'pipe', listener: (src: Readable) => void): this;
795
- on(event: 'unpipe', listener: (src: Readable) => void): this;
796
- on(event: string | symbol, listener: (...args: any[]) => void): this;
797
- once(event: 'close', listener: () => void): this;
798
- once(event: 'drain', listener: () => void): this;
799
- once(event: 'error', listener: (err: Error) => void): this;
800
- once(event: 'finish', listener: () => void): this;
801
- once(event: 'pipe', listener: (src: Readable) => void): this;
802
- once(event: 'unpipe', listener: (src: Readable) => void): this;
803
- once(event: string | symbol, listener: (...args: any[]) => void): this;
804
- prependListener(event: 'close', listener: () => void): this;
805
- prependListener(event: 'drain', listener: () => void): this;
806
- prependListener(event: 'error', listener: (err: Error) => void): this;
807
- prependListener(event: 'finish', listener: () => void): this;
808
- prependListener(event: 'pipe', listener: (src: Readable) => void): this;
809
- prependListener(event: 'unpipe', listener: (src: Readable) => void): this;
810
- prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
811
- prependOnceListener(event: 'close', listener: () => void): this;
812
- prependOnceListener(event: 'drain', listener: () => void): this;
813
- prependOnceListener(event: 'error', listener: (err: Error) => void): this;
814
- prependOnceListener(event: 'finish', listener: () => void): this;
815
- prependOnceListener(event: 'pipe', listener: (src: Readable) => void): this;
816
- prependOnceListener(event: 'unpipe', listener: (src: Readable) => void): this;
817
- prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
818
- removeListener(event: 'close', listener: () => void): this;
819
- removeListener(event: 'drain', listener: () => void): this;
820
- removeListener(event: 'error', listener: (err: Error) => void): this;
821
- removeListener(event: 'finish', listener: () => void): this;
822
- removeListener(event: 'pipe', listener: (src: Readable) => void): this;
823
- removeListener(event: 'unpipe', listener: (src: Readable) => void): this;
824
- removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
825
838
  }
826
839
  interface DuplexOptions extends ReadableOptions, WritableOptions {
827
840
  allowHalfOpen?: boolean | undefined;
@@ -854,7 +867,7 @@ declare module 'stream' {
854
867
  * * `crypto streams`
855
868
  * @since v0.9.4
856
869
  */
857
- class Duplex extends Readable implements Writable {
870
+ class Duplex extends ReadableBase implements WritableBase {
858
871
  readonly writable: boolean;
859
872
  readonly writableEnded: boolean;
860
873
  readonly writableFinished: boolean;
@@ -916,6 +929,27 @@ declare module 'stream' {
916
929
  end(chunk: any, encoding?: BufferEncoding, cb?: () => void): this;
917
930
  cork(): void;
918
931
  uncork(): void;
932
+ /**
933
+ * A utility method for creating a web `ReadableStream` and `WritableStream` from a `Duplex`.
934
+ * @since v17.0.0
935
+ * @experimental
936
+ */
937
+ static toWeb(streamDuplex: Duplex): {
938
+ readable: streamWeb.ReadableStream;
939
+ writable: streamWeb.WritableStream;
940
+ };
941
+ /**
942
+ * A utility method for creating a `Duplex` from a web `ReadableStream` and `WritableStream`.
943
+ * @since v17.0.0
944
+ * @experimental
945
+ */
946
+ static fromWeb(
947
+ duplexStream: {
948
+ readable: streamWeb.ReadableStream;
949
+ writable: streamWeb.WritableStream;
950
+ },
951
+ options?: Pick<DuplexOptions, 'allowHalfOpen' | 'decodeStrings' | 'encoding' | 'highWaterMark' | 'objectMode' | 'signal'>
952
+ ): Duplex;
919
953
  /**
920
954
  * Event emitter
921
955
  * The defined events on documents including: