@types/node 20.2.2 → 20.2.4

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