@types/node 18.11.5 → 20.2.5
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/README.md +2 -2
- node/assert.d.ts +86 -75
- node/async_hooks.d.ts +66 -37
- node/buffer.d.ts +193 -100
- node/child_process.d.ts +82 -56
- node/cluster.d.ts +12 -12
- node/console.d.ts +5 -5
- node/crypto.d.ts +233 -235
- node/dgram.d.ts +15 -15
- node/diagnostics_channel.d.ts +59 -21
- node/dns/promises.d.ts +50 -6
- node/dns.d.ts +26 -17
- node/domain.d.ts +4 -4
- node/events.d.ts +111 -65
- node/fs/promises.d.ts +99 -47
- node/fs.d.ts +244 -73
- node/globals.d.ts +6 -3
- node/http.d.ts +265 -148
- node/http2.d.ts +42 -47
- node/https.d.ts +53 -153
- node/index.d.ts +2 -1
- node/inspector.d.ts +10 -3
- node/module.d.ts +6 -4
- node/net.d.ts +32 -15
- node/os.d.ts +27 -16
- node/package.json +9 -4
- node/path.d.ts +4 -4
- node/perf_hooks.d.ts +28 -15
- node/process.d.ts +49 -46
- node/punycode.d.ts +1 -1
- node/querystring.d.ts +5 -5
- node/readline/promises.d.ts +65 -63
- node/readline.d.ts +32 -159
- node/repl.d.ts +20 -20
- node/stream/consumers.d.ts +1 -1
- node/stream.d.ts +950 -869
- node/string_decoder.d.ts +6 -6
- node/test.d.ts +877 -139
- node/timers/promises.d.ts +26 -1
- node/timers.d.ts +125 -4
- node/tls.d.ts +120 -29
- node/trace_events.d.ts +20 -9
- node/ts4.8/assert.d.ts +86 -75
- node/ts4.8/async_hooks.d.ts +66 -37
- node/ts4.8/buffer.d.ts +193 -101
- node/ts4.8/child_process.d.ts +82 -56
- node/ts4.8/cluster.d.ts +12 -12
- node/ts4.8/console.d.ts +5 -5
- node/ts4.8/crypto.d.ts +230 -233
- node/ts4.8/dgram.d.ts +15 -15
- node/ts4.8/diagnostics_channel.d.ts +59 -21
- node/ts4.8/dns/promises.d.ts +50 -6
- node/ts4.8/dns.d.ts +26 -17
- node/ts4.8/domain.d.ts +4 -4
- node/ts4.8/events.d.ts +111 -65
- node/ts4.8/fs/promises.d.ts +97 -46
- node/ts4.8/fs.d.ts +244 -73
- node/ts4.8/globals.d.ts +21 -12
- node/ts4.8/http.d.ts +265 -148
- node/ts4.8/http2.d.ts +42 -47
- node/ts4.8/https.d.ts +53 -153
- node/ts4.8/inspector.d.ts +10 -3
- node/ts4.8/module.d.ts +6 -4
- node/ts4.8/net.d.ts +33 -16
- node/ts4.8/os.d.ts +27 -16
- node/ts4.8/path.d.ts +4 -4
- node/ts4.8/perf_hooks.d.ts +28 -15
- node/ts4.8/process.d.ts +49 -46
- node/ts4.8/punycode.d.ts +1 -1
- node/ts4.8/querystring.d.ts +5 -5
- node/ts4.8/readline/promises.d.ts +65 -63
- node/ts4.8/readline.d.ts +32 -159
- node/ts4.8/repl.d.ts +20 -20
- node/ts4.8/stream/consumers.d.ts +1 -1
- node/ts4.8/stream.d.ts +192 -140
- node/ts4.8/string_decoder.d.ts +6 -6
- node/ts4.8/test.d.ts +877 -139
- node/ts4.8/timers/promises.d.ts +26 -1
- node/ts4.8/timers.d.ts +125 -4
- node/ts4.8/tls.d.ts +120 -29
- node/ts4.8/trace_events.d.ts +20 -9
- node/ts4.8/tty.d.ts +4 -5
- node/ts4.8/url.d.ts +46 -42
- node/ts4.8/util.d.ts +325 -123
- node/ts4.8/v8.d.ts +253 -14
- node/ts4.8/vm.d.ts +417 -32
- node/ts4.8/wasi.d.ts +13 -19
- node/ts4.8/worker_threads.d.ts +38 -34
- node/ts4.8/zlib.d.ts +11 -11
- node/tty.d.ts +4 -5
- node/url.d.ts +46 -42
- node/util.d.ts +325 -123
- node/v8.d.ts +253 -14
- node/vm.d.ts +417 -32
- node/wasi.d.ts +13 -19
- node/worker_threads.d.ts +38 -34
- node/zlib.d.ts +11 -11
node/stream.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A stream is an abstract interface for working with streaming data in Node.js.
|
|
3
|
-
* The `stream` module provides an API for implementing the stream interface.
|
|
3
|
+
* The `node:stream` module provides an API for implementing the stream interface.
|
|
4
4
|
*
|
|
5
5
|
* There are many stream objects provided by Node.js. For instance, a `request to an HTTP server` and `process.stdout` are both stream instances.
|
|
6
6
|
*
|
|
7
7
|
* Streams can be readable, writable, or both. All streams are instances of `EventEmitter`.
|
|
8
8
|
*
|
|
9
|
-
* To access the `stream` module:
|
|
9
|
+
* To access the `node:stream` module:
|
|
10
10
|
*
|
|
11
11
|
* ```js
|
|
12
|
-
* const stream = require('stream');
|
|
12
|
+
* const stream = require('node:stream');
|
|
13
13
|
* ```
|
|
14
14
|
*
|
|
15
|
-
* The `stream` module is useful for creating new types of stream instances.
|
|
16
|
-
* usually not necessary to use the `stream` module to consume streams.
|
|
17
|
-
* @see [source](https://github.com/nodejs/node/blob/
|
|
15
|
+
* The `node:stream` module is useful for creating new types of stream instances.
|
|
16
|
+
* It is usually not necessary to use the `node:stream` module to consume streams.
|
|
17
|
+
* @see [source](https://github.com/nodejs/node/blob/v20.2.0/lib/stream.js)
|
|
18
18
|
*/
|
|
19
19
|
declare module 'stream' {
|
|
20
20
|
import { EventEmitter, Abortable } from 'node:events';
|
|
21
|
-
import { Blob as NodeBlob } from
|
|
21
|
+
import { Blob as NodeBlob } from 'node:buffer';
|
|
22
22
|
import * as streamPromises from 'node:stream/promises';
|
|
23
23
|
import * as streamConsumers from 'node:stream/consumers';
|
|
24
24
|
import * as streamWeb from 'node:stream/web';
|
|
@@ -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,775 +782,50 @@ declare module 'stream' {
|
|
|
49
782
|
/**
|
|
50
783
|
* @since v0.9.4
|
|
51
784
|
*/
|
|
52
|
-
class Readable extends
|
|
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('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('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 `stream` module API
|
|
398
|
-
* as it is currently defined. (See `Compatibility` for more 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('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;
|
|
507
801
|
defaultEncoding?: BufferEncoding | undefined;
|
|
508
802
|
write?(this: Writable, chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
|
|
509
|
-
writev?(
|
|
510
|
-
this: Writable,
|
|
511
|
-
chunks: Array<{
|
|
512
|
-
chunk: any;
|
|
513
|
-
encoding: BufferEncoding;
|
|
514
|
-
}>,
|
|
515
|
-
callback: (error?: Error | null) => void
|
|
516
|
-
): void;
|
|
517
|
-
final?(this: Writable, callback: (error?: Error | null) => void): void;
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* @since v0.9.4
|
|
521
|
-
*/
|
|
522
|
-
class Writable extends Stream implements NodeJS.WritableStream {
|
|
523
|
-
/**
|
|
524
|
-
* A utility method for creating a `Writable` from a web `WritableStream`.
|
|
525
|
-
* @since v17.0.0
|
|
526
|
-
* @experimental
|
|
527
|
-
*/
|
|
528
|
-
static fromWeb(writableStream: streamWeb.WritableStream, options?: Pick<WritableOptions, 'decodeStrings' | 'highWaterMark' | 'objectMode' | 'signal'>): Writable;
|
|
529
|
-
/**
|
|
530
|
-
* A utility method for creating a web `WritableStream` from a `Writable`.
|
|
531
|
-
* @since v17.0.0
|
|
532
|
-
* @experimental
|
|
533
|
-
*/
|
|
534
|
-
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?(
|
|
803
|
+
writev?(
|
|
804
|
+
this: Writable,
|
|
598
805
|
chunks: Array<{
|
|
599
806
|
chunk: any;
|
|
600
807
|
encoding: BufferEncoding;
|
|
601
808
|
}>,
|
|
602
809
|
callback: (error?: Error | null) => void
|
|
603
810
|
): void;
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
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('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;
|
|
811
|
+
final?(this: Writable, callback: (error?: Error | null) => void): void;
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* @since v0.9.4
|
|
815
|
+
*/
|
|
816
|
+
class Writable extends WritableBase {
|
|
745
817
|
/**
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
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.
|
|
818
|
+
* A utility method for creating a `Writable` from a web `WritableStream`.
|
|
819
|
+
* @since v17.0.0
|
|
820
|
+
* @experimental
|
|
760
821
|
*/
|
|
761
|
-
|
|
822
|
+
static fromWeb(writableStream: streamWeb.WritableStream, options?: Pick<WritableOptions, 'decodeStrings' | 'highWaterMark' | 'objectMode' | 'signal'>): Writable;
|
|
762
823
|
/**
|
|
763
|
-
*
|
|
764
|
-
*
|
|
765
|
-
*
|
|
766
|
-
* 2. drain
|
|
767
|
-
* 3. error
|
|
768
|
-
* 4. finish
|
|
769
|
-
* 5. pipe
|
|
770
|
-
* 6. unpipe
|
|
824
|
+
* A utility method for creating a web `WritableStream` from a `Writable`.
|
|
825
|
+
* @since v17.0.0
|
|
826
|
+
* @experimental
|
|
771
827
|
*/
|
|
772
|
-
|
|
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;
|
|
828
|
+
static toWeb(streamWritable: Writable): streamWeb.WritableStream;
|
|
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
|
|
861
|
+
class Duplex extends ReadableBase implements WritableBase {
|
|
854
862
|
readonly writable: boolean;
|
|
855
863
|
readonly writableEnded: boolean;
|
|
856
864
|
readonly writableFinished: boolean;
|
|
@@ -864,7 +872,7 @@ declare module 'stream' {
|
|
|
864
872
|
/**
|
|
865
873
|
* If `false` then the stream will automatically end the writable side when the
|
|
866
874
|
* readable side ends. Set initially by the `allowHalfOpen` constructor option,
|
|
867
|
-
* which defaults to `
|
|
875
|
+
* which defaults to `true`.
|
|
868
876
|
*
|
|
869
877
|
* This can be changed manually to change the half-open behavior of an existing`Duplex` stream instance, but must be changed before the `'end'` event is
|
|
870
878
|
* emitted.
|
|
@@ -912,6 +920,126 @@ 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;
|
|
944
|
+
/**
|
|
945
|
+
* Event emitter
|
|
946
|
+
* The defined events on documents including:
|
|
947
|
+
* 1. close
|
|
948
|
+
* 2. data
|
|
949
|
+
* 3. drain
|
|
950
|
+
* 4. end
|
|
951
|
+
* 5. error
|
|
952
|
+
* 6. finish
|
|
953
|
+
* 7. pause
|
|
954
|
+
* 8. pipe
|
|
955
|
+
* 9. readable
|
|
956
|
+
* 10. resume
|
|
957
|
+
* 11. unpipe
|
|
958
|
+
*/
|
|
959
|
+
addListener(event: 'close', listener: () => void): this;
|
|
960
|
+
addListener(event: 'data', listener: (chunk: any) => void): this;
|
|
961
|
+
addListener(event: 'drain', listener: () => void): this;
|
|
962
|
+
addListener(event: 'end', listener: () => void): this;
|
|
963
|
+
addListener(event: 'error', listener: (err: Error) => void): this;
|
|
964
|
+
addListener(event: 'finish', listener: () => void): this;
|
|
965
|
+
addListener(event: 'pause', listener: () => void): this;
|
|
966
|
+
addListener(event: 'pipe', listener: (src: Readable) => void): this;
|
|
967
|
+
addListener(event: 'readable', listener: () => void): this;
|
|
968
|
+
addListener(event: 'resume', listener: () => void): this;
|
|
969
|
+
addListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
|
970
|
+
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
971
|
+
emit(event: 'close'): boolean;
|
|
972
|
+
emit(event: 'data', chunk: any): boolean;
|
|
973
|
+
emit(event: 'drain'): boolean;
|
|
974
|
+
emit(event: 'end'): boolean;
|
|
975
|
+
emit(event: 'error', err: Error): boolean;
|
|
976
|
+
emit(event: 'finish'): boolean;
|
|
977
|
+
emit(event: 'pause'): boolean;
|
|
978
|
+
emit(event: 'pipe', src: Readable): boolean;
|
|
979
|
+
emit(event: 'readable'): boolean;
|
|
980
|
+
emit(event: 'resume'): boolean;
|
|
981
|
+
emit(event: 'unpipe', src: Readable): boolean;
|
|
982
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
|
983
|
+
on(event: 'close', listener: () => void): this;
|
|
984
|
+
on(event: 'data', listener: (chunk: any) => void): this;
|
|
985
|
+
on(event: 'drain', listener: () => void): this;
|
|
986
|
+
on(event: 'end', listener: () => void): this;
|
|
987
|
+
on(event: 'error', listener: (err: Error) => void): this;
|
|
988
|
+
on(event: 'finish', listener: () => void): this;
|
|
989
|
+
on(event: 'pause', listener: () => void): this;
|
|
990
|
+
on(event: 'pipe', listener: (src: Readable) => void): this;
|
|
991
|
+
on(event: 'readable', listener: () => void): this;
|
|
992
|
+
on(event: 'resume', listener: () => void): this;
|
|
993
|
+
on(event: 'unpipe', listener: (src: Readable) => void): this;
|
|
994
|
+
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
995
|
+
once(event: 'close', listener: () => void): this;
|
|
996
|
+
once(event: 'data', listener: (chunk: any) => void): this;
|
|
997
|
+
once(event: 'drain', listener: () => void): this;
|
|
998
|
+
once(event: 'end', listener: () => void): this;
|
|
999
|
+
once(event: 'error', listener: (err: Error) => void): this;
|
|
1000
|
+
once(event: 'finish', listener: () => void): this;
|
|
1001
|
+
once(event: 'pause', listener: () => void): this;
|
|
1002
|
+
once(event: 'pipe', listener: (src: Readable) => void): this;
|
|
1003
|
+
once(event: 'readable', listener: () => void): this;
|
|
1004
|
+
once(event: 'resume', listener: () => void): this;
|
|
1005
|
+
once(event: 'unpipe', listener: (src: Readable) => void): this;
|
|
1006
|
+
once(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
1007
|
+
prependListener(event: 'close', listener: () => void): this;
|
|
1008
|
+
prependListener(event: 'data', listener: (chunk: any) => void): this;
|
|
1009
|
+
prependListener(event: 'drain', listener: () => void): this;
|
|
1010
|
+
prependListener(event: 'end', listener: () => void): this;
|
|
1011
|
+
prependListener(event: 'error', listener: (err: Error) => void): this;
|
|
1012
|
+
prependListener(event: 'finish', listener: () => void): this;
|
|
1013
|
+
prependListener(event: 'pause', listener: () => void): this;
|
|
1014
|
+
prependListener(event: 'pipe', listener: (src: Readable) => void): this;
|
|
1015
|
+
prependListener(event: 'readable', listener: () => void): this;
|
|
1016
|
+
prependListener(event: 'resume', listener: () => void): this;
|
|
1017
|
+
prependListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
|
1018
|
+
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
1019
|
+
prependOnceListener(event: 'close', listener: () => void): this;
|
|
1020
|
+
prependOnceListener(event: 'data', listener: (chunk: any) => void): this;
|
|
1021
|
+
prependOnceListener(event: 'drain', listener: () => void): this;
|
|
1022
|
+
prependOnceListener(event: 'end', listener: () => void): this;
|
|
1023
|
+
prependOnceListener(event: 'error', listener: (err: Error) => void): this;
|
|
1024
|
+
prependOnceListener(event: 'finish', listener: () => void): this;
|
|
1025
|
+
prependOnceListener(event: 'pause', listener: () => void): this;
|
|
1026
|
+
prependOnceListener(event: 'pipe', listener: (src: Readable) => void): this;
|
|
1027
|
+
prependOnceListener(event: 'readable', listener: () => void): this;
|
|
1028
|
+
prependOnceListener(event: 'resume', listener: () => void): this;
|
|
1029
|
+
prependOnceListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
|
1030
|
+
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
1031
|
+
removeListener(event: 'close', listener: () => void): this;
|
|
1032
|
+
removeListener(event: 'data', listener: (chunk: any) => void): this;
|
|
1033
|
+
removeListener(event: 'drain', listener: () => void): this;
|
|
1034
|
+
removeListener(event: 'end', listener: () => void): this;
|
|
1035
|
+
removeListener(event: 'error', listener: (err: Error) => void): this;
|
|
1036
|
+
removeListener(event: 'finish', listener: () => void): this;
|
|
1037
|
+
removeListener(event: 'pause', listener: () => void): this;
|
|
1038
|
+
removeListener(event: 'pipe', listener: (src: Readable) => void): this;
|
|
1039
|
+
removeListener(event: 'readable', listener: () => void): this;
|
|
1040
|
+
removeListener(event: 'resume', listener: () => void): this;
|
|
1041
|
+
removeListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
|
1042
|
+
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
915
1043
|
}
|
|
916
1044
|
type TransformCallback = (error?: Error | null, data?: any) => void;
|
|
917
1045
|
interface TransformOptions extends DuplexOptions {
|
|
@@ -953,18 +1081,21 @@ declare module 'stream' {
|
|
|
953
1081
|
*/
|
|
954
1082
|
class PassThrough extends Transform {}
|
|
955
1083
|
/**
|
|
1084
|
+
* A stream to attach a signal to.
|
|
1085
|
+
*
|
|
956
1086
|
* Attaches an AbortSignal to a readable or writeable stream. This lets code
|
|
957
1087
|
* control stream destruction using an `AbortController`.
|
|
958
1088
|
*
|
|
959
|
-
* Calling `abort` on the `AbortController` corresponding to the passed`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`on the stream.
|
|
1089
|
+
* Calling `abort` on the `AbortController` corresponding to the passed`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`on the stream, and `controller.error(new
|
|
1090
|
+
* AbortError())` for webstreams.
|
|
960
1091
|
*
|
|
961
1092
|
* ```js
|
|
962
|
-
* const fs = require('fs');
|
|
1093
|
+
* const fs = require('node:fs');
|
|
963
1094
|
*
|
|
964
1095
|
* const controller = new AbortController();
|
|
965
1096
|
* const read = addAbortSignal(
|
|
966
1097
|
* controller.signal,
|
|
967
|
-
* fs.createReadStream(('object.json'))
|
|
1098
|
+
* fs.createReadStream(('object.json')),
|
|
968
1099
|
* );
|
|
969
1100
|
* // Later, abort the operation closing the stream
|
|
970
1101
|
* controller.abort();
|
|
@@ -977,7 +1108,7 @@ declare module 'stream' {
|
|
|
977
1108
|
* setTimeout(() => controller.abort(), 10_000); // set a timeout
|
|
978
1109
|
* const stream = addAbortSignal(
|
|
979
1110
|
* controller.signal,
|
|
980
|
-
* fs.createReadStream(('object.json'))
|
|
1111
|
+
* fs.createReadStream(('object.json')),
|
|
981
1112
|
* );
|
|
982
1113
|
* (async () => {
|
|
983
1114
|
* try {
|
|
@@ -993,22 +1124,70 @@ declare module 'stream' {
|
|
|
993
1124
|
* }
|
|
994
1125
|
* })();
|
|
995
1126
|
* ```
|
|
1127
|
+
*
|
|
1128
|
+
* Or using an `AbortSignal` with a ReadableStream:
|
|
1129
|
+
*
|
|
1130
|
+
* ```js
|
|
1131
|
+
* const controller = new AbortController();
|
|
1132
|
+
* const rs = new ReadableStream({
|
|
1133
|
+
* start(controller) {
|
|
1134
|
+
* controller.enqueue('hello');
|
|
1135
|
+
* controller.enqueue('world');
|
|
1136
|
+
* controller.close();
|
|
1137
|
+
* },
|
|
1138
|
+
* });
|
|
1139
|
+
*
|
|
1140
|
+
* addAbortSignal(controller.signal, rs);
|
|
1141
|
+
*
|
|
1142
|
+
* finished(rs, (err) => {
|
|
1143
|
+
* if (err) {
|
|
1144
|
+
* if (err.name === 'AbortError') {
|
|
1145
|
+
* // The operation was cancelled
|
|
1146
|
+
* }
|
|
1147
|
+
* }
|
|
1148
|
+
* });
|
|
1149
|
+
*
|
|
1150
|
+
* const reader = rs.getReader();
|
|
1151
|
+
*
|
|
1152
|
+
* reader.read().then(({ value, done }) => {
|
|
1153
|
+
* console.log(value); // hello
|
|
1154
|
+
* console.log(done); // false
|
|
1155
|
+
* controller.abort();
|
|
1156
|
+
* });
|
|
1157
|
+
* ```
|
|
996
1158
|
* @since v15.4.0
|
|
997
1159
|
* @param signal A signal representing possible cancellation
|
|
998
1160
|
* @param stream a stream to attach a signal to
|
|
999
1161
|
*/
|
|
1000
1162
|
function addAbortSignal<T extends Stream>(signal: AbortSignal, stream: T): T;
|
|
1163
|
+
/**
|
|
1164
|
+
* Returns the default highWaterMark used by streams.
|
|
1165
|
+
* Defaults to `16384` (16 KiB), or `16` for `objectMode`.
|
|
1166
|
+
* @since v19.9.0
|
|
1167
|
+
* @param objectMode
|
|
1168
|
+
*/
|
|
1169
|
+
function getDefaultHighWaterMark(objectMode: boolean): number;
|
|
1170
|
+
/**
|
|
1171
|
+
* Sets the default highWaterMark used by streams.
|
|
1172
|
+
* @since v19.9.0
|
|
1173
|
+
* @param objectMode
|
|
1174
|
+
* @param value highWaterMark value
|
|
1175
|
+
*/
|
|
1176
|
+
function setDefaultHighWaterMark(objectMode: boolean, value: number): void;
|
|
1001
1177
|
interface FinishedOptions extends Abortable {
|
|
1002
1178
|
error?: boolean | undefined;
|
|
1003
1179
|
readable?: boolean | undefined;
|
|
1004
1180
|
writable?: boolean | undefined;
|
|
1005
1181
|
}
|
|
1006
1182
|
/**
|
|
1183
|
+
* A readable and/or writable stream/webstream.
|
|
1184
|
+
*
|
|
1007
1185
|
* A function to get notified when a stream is no longer readable, writable
|
|
1008
1186
|
* or has experienced an error or a premature close event.
|
|
1009
1187
|
*
|
|
1010
1188
|
* ```js
|
|
1011
|
-
* const { finished } = require('stream');
|
|
1189
|
+
* const { finished } = require('node:stream');
|
|
1190
|
+
* const fs = require('node:fs');
|
|
1012
1191
|
*
|
|
1013
1192
|
* const rs = fs.createReadStream('archive.tar');
|
|
1014
1193
|
*
|
|
@@ -1026,21 +1205,7 @@ declare module 'stream' {
|
|
|
1026
1205
|
* Especially useful in error handling scenarios where a stream is destroyed
|
|
1027
1206
|
* prematurely (like an aborted HTTP request), and will not emit `'end'`or `'finish'`.
|
|
1028
1207
|
*
|
|
1029
|
-
* The `finished` API provides promise version
|
|
1030
|
-
*
|
|
1031
|
-
* ```js
|
|
1032
|
-
* const { finished } = require('stream/promises');
|
|
1033
|
-
*
|
|
1034
|
-
* const rs = fs.createReadStream('archive.tar');
|
|
1035
|
-
*
|
|
1036
|
-
* async function run() {
|
|
1037
|
-
* await finished(rs);
|
|
1038
|
-
* console.log('Stream is done reading.');
|
|
1039
|
-
* }
|
|
1040
|
-
*
|
|
1041
|
-
* run().catch(console.error);
|
|
1042
|
-
* rs.resume(); // Drain the stream.
|
|
1043
|
-
* ```
|
|
1208
|
+
* The `finished` API provides `promise version`.
|
|
1044
1209
|
*
|
|
1045
1210
|
* `stream.finished()` leaves dangling event listeners (in particular`'error'`, `'end'`, `'finish'` and `'close'`) after `callback` has been
|
|
1046
1211
|
* invoked. The reason for this is so that unexpected `'error'` events (due to
|
|
@@ -1080,16 +1245,17 @@ declare module 'stream' {
|
|
|
1080
1245
|
: (err: NodeJS.ErrnoException | null) => void;
|
|
1081
1246
|
type PipelinePromise<S extends PipelineDestination<any, any>> = S extends PipelineDestinationPromiseFunction<any, infer P> ? Promise<P> : Promise<void>;
|
|
1082
1247
|
interface PipelineOptions {
|
|
1083
|
-
signal
|
|
1248
|
+
signal?: AbortSignal | undefined;
|
|
1249
|
+
end?: boolean | undefined;
|
|
1084
1250
|
}
|
|
1085
1251
|
/**
|
|
1086
1252
|
* A module method to pipe between streams and generators forwarding errors and
|
|
1087
1253
|
* properly cleaning up and provide a callback when the pipeline is complete.
|
|
1088
1254
|
*
|
|
1089
1255
|
* ```js
|
|
1090
|
-
* const { pipeline } = require('stream');
|
|
1091
|
-
* const fs = require('fs');
|
|
1092
|
-
* const zlib = require('zlib');
|
|
1256
|
+
* const { pipeline } = require('node:stream');
|
|
1257
|
+
* const fs = require('node:fs');
|
|
1258
|
+
* const zlib = require('node:zlib');
|
|
1093
1259
|
*
|
|
1094
1260
|
* // Use the pipeline API to easily pipe a series of streams
|
|
1095
1261
|
* // together and get notified when the pipeline is fully done.
|
|
@@ -1106,95 +1272,11 @@ declare module 'stream' {
|
|
|
1106
1272
|
* } else {
|
|
1107
1273
|
* console.log('Pipeline succeeded.');
|
|
1108
1274
|
* }
|
|
1109
|
-
* }
|
|
1275
|
+
* },
|
|
1110
1276
|
* );
|
|
1111
1277
|
* ```
|
|
1112
1278
|
*
|
|
1113
|
-
* The `pipeline` API provides a promise version
|
|
1114
|
-
* receive an options argument as the last parameter with a`signal` `AbortSignal` property. When the signal is aborted,`destroy` will be called on the underlying pipeline, with
|
|
1115
|
-
* an`AbortError`.
|
|
1116
|
-
*
|
|
1117
|
-
* ```js
|
|
1118
|
-
* const { pipeline } = require('stream/promises');
|
|
1119
|
-
*
|
|
1120
|
-
* async function run() {
|
|
1121
|
-
* await pipeline(
|
|
1122
|
-
* fs.createReadStream('archive.tar'),
|
|
1123
|
-
* zlib.createGzip(),
|
|
1124
|
-
* fs.createWriteStream('archive.tar.gz')
|
|
1125
|
-
* );
|
|
1126
|
-
* console.log('Pipeline succeeded.');
|
|
1127
|
-
* }
|
|
1128
|
-
*
|
|
1129
|
-
* run().catch(console.error);
|
|
1130
|
-
* ```
|
|
1131
|
-
*
|
|
1132
|
-
* To use an `AbortSignal`, pass it inside an options object,
|
|
1133
|
-
* as the last argument:
|
|
1134
|
-
*
|
|
1135
|
-
* ```js
|
|
1136
|
-
* const { pipeline } = require('stream/promises');
|
|
1137
|
-
*
|
|
1138
|
-
* async function run() {
|
|
1139
|
-
* const ac = new AbortController();
|
|
1140
|
-
* const signal = ac.signal;
|
|
1141
|
-
*
|
|
1142
|
-
* setTimeout(() => ac.abort(), 1);
|
|
1143
|
-
* await pipeline(
|
|
1144
|
-
* fs.createReadStream('archive.tar'),
|
|
1145
|
-
* zlib.createGzip(),
|
|
1146
|
-
* fs.createWriteStream('archive.tar.gz'),
|
|
1147
|
-
* { signal },
|
|
1148
|
-
* );
|
|
1149
|
-
* }
|
|
1150
|
-
*
|
|
1151
|
-
* run().catch(console.error); // AbortError
|
|
1152
|
-
* ```
|
|
1153
|
-
*
|
|
1154
|
-
* The `pipeline` API also supports async generators:
|
|
1155
|
-
*
|
|
1156
|
-
* ```js
|
|
1157
|
-
* const { pipeline } = require('stream/promises');
|
|
1158
|
-
* const fs = require('fs');
|
|
1159
|
-
*
|
|
1160
|
-
* async function run() {
|
|
1161
|
-
* await pipeline(
|
|
1162
|
-
* fs.createReadStream('lowercase.txt'),
|
|
1163
|
-
* async function* (source, { signal }) {
|
|
1164
|
-
* source.setEncoding('utf8'); // Work with strings rather than `Buffer`s.
|
|
1165
|
-
* for await (const chunk of source) {
|
|
1166
|
-
* yield await processChunk(chunk, { signal });
|
|
1167
|
-
* }
|
|
1168
|
-
* },
|
|
1169
|
-
* fs.createWriteStream('uppercase.txt')
|
|
1170
|
-
* );
|
|
1171
|
-
* console.log('Pipeline succeeded.');
|
|
1172
|
-
* }
|
|
1173
|
-
*
|
|
1174
|
-
* run().catch(console.error);
|
|
1175
|
-
* ```
|
|
1176
|
-
*
|
|
1177
|
-
* Remember to handle the `signal` argument passed into the async generator.
|
|
1178
|
-
* Especially in the case where the async generator is the source for the
|
|
1179
|
-
* pipeline (i.e. first argument) or the pipeline will never complete.
|
|
1180
|
-
*
|
|
1181
|
-
* ```js
|
|
1182
|
-
* const { pipeline } = require('stream/promises');
|
|
1183
|
-
* const fs = require('fs');
|
|
1184
|
-
*
|
|
1185
|
-
* async function run() {
|
|
1186
|
-
* await pipeline(
|
|
1187
|
-
* async function* ({ signal }) {
|
|
1188
|
-
* await someLongRunningfn({ signal });
|
|
1189
|
-
* yield 'asd';
|
|
1190
|
-
* },
|
|
1191
|
-
* fs.createWriteStream('uppercase.txt')
|
|
1192
|
-
* );
|
|
1193
|
-
* console.log('Pipeline succeeded.');
|
|
1194
|
-
* }
|
|
1195
|
-
*
|
|
1196
|
-
* run().catch(console.error);
|
|
1197
|
-
* ```
|
|
1279
|
+
* The `pipeline` API provides a `promise version`.
|
|
1198
1280
|
*
|
|
1199
1281
|
* `stream.pipeline()` will call `stream.destroy(err)` on all streams except:
|
|
1200
1282
|
*
|
|
@@ -1213,9 +1295,9 @@ declare module 'stream' {
|
|
|
1213
1295
|
* See the example below:
|
|
1214
1296
|
*
|
|
1215
1297
|
* ```js
|
|
1216
|
-
* const fs = require('fs');
|
|
1217
|
-
* const http = require('http');
|
|
1218
|
-
* const { pipeline } = require('stream');
|
|
1298
|
+
* const fs = require('node:fs');
|
|
1299
|
+
* const http = require('node:http');
|
|
1300
|
+
* const { pipeline } = require('node:stream');
|
|
1219
1301
|
*
|
|
1220
1302
|
* const server = http.createServer((req, res) => {
|
|
1221
1303
|
* const fileStream = fs.createReadStream('./fileNotExist.txt');
|
|
@@ -1316,19 +1398,18 @@ declare module 'stream' {
|
|
|
1316
1398
|
ref(): void;
|
|
1317
1399
|
unref(): void;
|
|
1318
1400
|
}
|
|
1319
|
-
|
|
1320
1401
|
/**
|
|
1321
1402
|
* Returns whether the stream has encountered an error.
|
|
1322
|
-
* @since v17.3.0
|
|
1403
|
+
* @since v17.3.0, v16.14.0
|
|
1404
|
+
* @experimental
|
|
1323
1405
|
*/
|
|
1324
1406
|
function isErrored(stream: Readable | Writable | NodeJS.ReadableStream | NodeJS.WritableStream): boolean;
|
|
1325
|
-
|
|
1326
1407
|
/**
|
|
1327
1408
|
* Returns whether the stream is readable.
|
|
1328
|
-
* @since v17.4.0
|
|
1409
|
+
* @since v17.4.0, v16.14.0
|
|
1410
|
+
* @experimental
|
|
1329
1411
|
*/
|
|
1330
1412
|
function isReadable(stream: Readable | NodeJS.ReadableStream): boolean;
|
|
1331
|
-
|
|
1332
1413
|
const promises: typeof streamPromises;
|
|
1333
1414
|
const consumers: typeof streamConsumers;
|
|
1334
1415
|
}
|