2012-02-27 11:18:10 -08:00
# Stream
2010-10-28 23:18:16 +11:00
2017-01-22 19:16:21 -08:00
<!-- introduced_in=v0.10.0 -->
2016-07-16 00:35:38 +02:00
> Stability: 2 - Stable
2012-03-02 15:14:03 -08:00
2020-06-22 13:56:08 -04:00
<!-- source_link=lib/stream.js -->
2016-05-23 22:30:41 -07:00
A stream is an abstract interface for working with streaming data in Node.js.
2022-04-20 10:23:41 +02:00
The `node:stream` module provides an API for implementing the stream interface.
2010-10-28 23:18:16 +11:00
2016-05-23 22:30:41 -07:00
There are many stream objects provided by Node.js. For instance, a
[request to an HTTP server][http-incoming-message] and [`process.stdout` ][]
are both stream instances.
2012-02-27 11:18:10 -08:00
2016-05-23 22:30:41 -07:00
Streams can be readable, writable, or both. All streams are instances of
[`EventEmitter` ][].
2015-12-10 18:32:13 -03:00
2022-04-20 10:23:41 +02:00
To access the `node:stream` module:
2012-12-13 11:15:49 -08:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const stream = require('node:stream');
2016-05-23 22:30:41 -07:00
```
2012-12-13 11:15:49 -08:00
2022-04-20 10:23:41 +02:00
The `node:stream` module is useful for creating new types of stream instances.
It is usually not necessary to use the `node:stream` module to consume streams.
2016-05-23 22:30:41 -07:00
2020-06-14 14:49:34 -07:00
## Organization of this document
2016-05-23 22:30:41 -07:00
2019-07-08 14:58:24 -07:00
This document contains two primary sections and a third section for notes. The
first section explains how to use existing streams within an application. The
second section explains how to create new types of streams.
2016-05-23 22:30:41 -07:00
2020-06-14 14:49:34 -07:00
## Types of streams
2016-05-23 22:30:41 -07:00
There are four fundamental stream types within Node.js:
2019-10-23 21:28:42 -07:00
* [`Writable` ][]: streams to which data can be written (for example,
2016-06-19 00:19:41 +03:00
[`fs.createWriteStream()` ][]).
2019-10-23 21:28:42 -07:00
* [`Readable` ][]: streams from which data can be read (for example,
2018-06-14 15:09:49 -05:00
[`fs.createReadStream()` ][]).
2019-10-23 21:28:42 -07:00
* [`Duplex` ][]: streams that are both `Readable` and `Writable` (for example,
2016-05-23 22:30:41 -07:00
[`net.Socket` ][]).
2019-10-23 21:28:42 -07:00
* [`Transform` ][]: `Duplex` streams that can modify or transform the data as it
2018-08-26 19:02:27 +03:00
is written and read (for example, [`zlib.createDeflate()` ][]).
2016-05-23 22:30:41 -07:00
2019-06-22 14:57:29 +03:00
Additionally, this module includes the utility functions
2024-07-26 01:09:23 -07:00
[`stream.duplexPair()` ][],
[`stream.pipeline()` ][],
[`stream.finished()` ][]
[`stream.Readable.from()` ][], and
[`stream.addAbortSignal()` ][].
2018-04-04 16:52:19 +02:00
2020-06-28 16:29:01 +08:00
### Streams Promises API
2021-10-10 21:55:04 -07:00
2020-06-21 15:20:00 +02:00
<!-- YAML
added: v15.0.0
-->
2020-06-28 16:29:01 +08:00
The `stream/promises` API provides an alternative set of asynchronous utility
functions for streams that return `Promise` objects rather than using
2022-04-20 10:23:41 +02:00
callbacks. The API is accessible via `require('node:stream/promises')`
or `require('node:stream').promises` .
2020-06-28 16:29:01 +08:00
2022-12-15 16:34:23 +01:00
### `stream.pipeline(source[, ...transforms], destination[, options])`
### `stream.pipeline(streams[, options])`
<!-- YAML
added: v15.0.0
2023-07-30 17:12:50 +01:00
changes:
- version:
- v18.0.0
- v17.2.0
- v16.14.0
pr-url: https://github.com/nodejs/node/pull/40886
description: Add the `end` option, which can be set to `false` to prevent
automatically closing the destination stream when the source
ends.
2022-12-15 16:34:23 +01:00
-->
* `streams` {Stream\[]|Iterable\[]|AsyncIterable\[]|Function\[]}
* `source` {Stream|Iterable|AsyncIterable|Function}
* Returns: {Promise|AsyncIterable}
* `...transforms` {Stream|Function}
* `source` {AsyncIterable}
* Returns: {Promise|AsyncIterable}
* `destination` {Stream|Function}
* `source` {AsyncIterable}
* Returns: {Promise|AsyncIterable}
2023-07-30 17:12:50 +01:00
* `options` {Object} Pipeline options
2022-12-15 16:34:23 +01:00
* `signal` {AbortSignal}
2023-07-30 17:12:50 +01:00
* `end` {boolean} End the destination stream when the source stream ends.
Transform streams are always ended, even if this value is `false` .
**Default:** `true` .
2022-12-15 16:34:23 +01:00
* Returns: {Promise} Fulfills when the pipeline is complete.
```cjs
const { pipeline } = require('node:stream/promises');
const fs = require('node:fs');
const zlib = require('node:zlib');
async function run() {
await pipeline(
fs.createReadStream('archive.tar'),
zlib.createGzip(),
fs.createWriteStream('archive.tar.gz'),
);
console.log('Pipeline succeeded.');
}
run().catch(console.error);
```
```mjs
import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
await pipeline(
createReadStream('archive.tar'),
createGzip(),
createWriteStream('archive.tar.gz'),
);
console.log('Pipeline succeeded.');
```
To use an `AbortSignal` , pass it inside an options object, as the last argument.
When the signal is aborted, `destroy` will be called on the underlying pipeline,
with an `AbortError` .
```cjs
const { pipeline } = require('node:stream/promises');
const fs = require('node:fs');
const zlib = require('node:zlib');
async function run() {
const ac = new AbortController();
const signal = ac.signal;
setImmediate(() => ac.abort());
await pipeline(
fs.createReadStream('archive.tar'),
zlib.createGzip(),
fs.createWriteStream('archive.tar.gz'),
{ signal },
);
}
run().catch(console.error); // AbortError
```
```mjs
import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
const ac = new AbortController();
const { signal } = ac;
setImmediate(() => ac.abort());
try {
await pipeline(
createReadStream('archive.tar'),
createGzip(),
createWriteStream('archive.tar.gz'),
{ signal },
);
} catch (err) {
console.error(err); // AbortError
}
```
The `pipeline` API also supports async generators:
```cjs
const { pipeline } = require('node:stream/promises');
const fs = require('node:fs');
async function run() {
await pipeline(
fs.createReadStream('lowercase.txt'),
async function* (source, { signal }) {
source.setEncoding('utf8'); // Work with strings rather than `Buffer` s.
for await (const chunk of source) {
yield await processChunk(chunk, { signal });
}
},
fs.createWriteStream('uppercase.txt'),
);
console.log('Pipeline succeeded.');
}
run().catch(console.error);
```
```mjs
import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
await pipeline(
createReadStream('lowercase.txt'),
async function* (source, { signal }) {
source.setEncoding('utf8'); // Work with strings rather than `Buffer` s.
for await (const chunk of source) {
yield await processChunk(chunk, { signal });
}
},
createWriteStream('uppercase.txt'),
);
console.log('Pipeline succeeded.');
```
Remember to handle the `signal` argument passed into the async generator.
Especially in the case where the async generator is the source for the
pipeline (i.e. first argument) or the pipeline will never complete.
```cjs
const { pipeline } = require('node:stream/promises');
const fs = require('node:fs');
async function run() {
await pipeline(
async function* ({ signal }) {
await someLongRunningfn({ signal });
yield 'asd';
},
fs.createWriteStream('uppercase.txt'),
);
console.log('Pipeline succeeded.');
}
run().catch(console.error);
```
```mjs
import { pipeline } from 'node:stream/promises';
import fs from 'node:fs';
await pipeline(
async function* ({ signal }) {
await someLongRunningfn({ signal });
yield 'asd';
},
fs.createWriteStream('uppercase.txt'),
);
console.log('Pipeline succeeded.');
```
The `pipeline` API provides [callback version][stream-pipeline]:
### `stream.finished(stream[, options])`
<!-- YAML
added: v15.0.0
2024-04-21 02:37:01 +08:00
changes:
- version:
- v19.5.0
- v18.14.0
pr-url: https://github.com/nodejs/node/pull/46205
description: Added support for `ReadableStream` and `WritableStream` .
2024-09-25 15:35:08 +01:00
- version:
- v19.1.0
- v18.13.0
pr-url: https://github.com/nodejs/node/pull/44862
description: The `cleanup` option was added.
2022-12-15 16:34:23 +01:00
-->
2024-04-21 02:37:01 +08:00
* `stream` {Stream|ReadableStream|WritableStream} A readable and/or writable
stream/webstream.
2022-12-15 16:34:23 +01:00
* `options` {Object}
* `error` {boolean|undefined}
* `readable` {boolean|undefined}
* `writable` {boolean|undefined}
2024-09-25 15:35:08 +01:00
* `signal` {AbortSignal|undefined}
* `cleanup` {boolean|undefined} If `true` , removes the listeners registered by
this function before the promise is fulfilled. **Default:** `false` .
2022-12-15 16:34:23 +01:00
* Returns: {Promise} Fulfills when the stream is no
longer readable or writable.
```cjs
const { finished } = require('node:stream/promises');
const fs = require('node:fs');
const rs = fs.createReadStream('archive.tar');
async function run() {
await finished(rs);
console.log('Stream is done reading.');
}
run().catch(console.error);
rs.resume(); // Drain the stream.
```
```mjs
import { finished } from 'node:stream/promises';
import { createReadStream } from 'node:fs';
const rs = createReadStream('archive.tar');
async function run() {
await finished(rs);
console.log('Stream is done reading.');
}
run().catch(console.error);
rs.resume(); // Drain the stream.
```
2023-08-23 11:17:31 +02:00
The `finished` API also provides a [callback version][stream-finished].
2022-12-15 16:34:23 +01:00
2024-09-25 15:35:08 +01:00
`stream.finished()` leaves dangling event listeners (in particular
`'error'` , `'end'` , `'finish'` and `'close'` ) after the returned promise is
resolved or rejected. The reason for this is so that unexpected `'error'`
events (due to incorrect stream implementations) do not cause unexpected
crashes. If this is unwanted behavior then `options.cleanup` should be set to
`true` :
2024-11-20 19:10:38 +09:00
```mjs
2024-09-25 15:35:08 +01:00
await finished(rs, { cleanup: true });
```
2020-06-14 14:49:34 -07:00
### Object mode
2016-05-23 22:30:41 -07:00
2024-03-20 18:27:29 +01:00
All streams created by Node.js APIs operate exclusively on strings, {Buffer},
{TypedArray} and {DataView} objects:
* `Strings` and `Buffers` are the most common types used with streams.
* `TypedArray` and `DataView` lets you handle binary data with types like
`Int32Array` or `Uint8Array` . When you write a TypedArray or DataView to a
stream, Node.js processes
the raw bytes.
It is possible, however, for stream
implementations to work with other types of JavaScript values (with the
exception of `null` , which serves a special purpose within streams).
Such streams are considered to operate in "object mode".
2016-05-23 22:30:41 -07:00
Stream instances are switched into object mode using the `objectMode` option
when the stream is created. Attempting to switch an existing stream into
object mode is not safe.
### Buffering
2012-12-13 11:15:49 -08:00
2013-07-15 16:56:02 -07:00
<!-- type=misc -->
2012-12-13 11:15:49 -08:00
2018-04-29 20:46:41 +03:00
Both [`Writable` ][] and [`Readable` ][] streams will store data in an internal
2020-12-31 15:02:48 -08:00
buffer.
2016-05-23 22:30:41 -07:00
The amount of data potentially buffered depends on the `highWaterMark` option
2018-08-31 14:06:57 -04:00
passed into the stream's constructor. For normal streams, the `highWaterMark`
2017-06-03 16:11:32 -04:00
option specifies a [total number of bytes][hwm-gotcha]. For streams operating
2024-05-10 20:58:40 +03:00
in object mode, the `highWaterMark` specifies a total number of objects. For
streams operating on (but not decoding) strings, the `highWaterMark` specifies
a total number of UTF-16 code units.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
Data is buffered in `Readable` streams when the implementation calls
2016-05-23 22:30:41 -07:00
[`stream.push(chunk)` ][stream-push]. If the consumer of the Stream does not
call [`stream.read()` ][stream-read], the data will sit in the internal
queue until it is consumed.
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
Once the total size of the internal read buffer reaches the threshold specified
by `highWaterMark` , the stream will temporarily stop reading data from the
underlying resource until the data currently buffered can be consumed (that is,
2020-06-06 11:20:14 +05:30
the stream will stop calling the internal [`readable._read()` ][] method that is
2016-05-23 22:30:41 -07:00
used to fill the read buffer).
2018-04-29 20:46:41 +03:00
Data is buffered in `Writable` streams when the
2016-05-23 22:30:41 -07:00
[`writable.write(chunk)` ][stream-write] method is called repeatedly. While the
total size of the internal write buffer is below the threshold set by
2016-08-29 20:53:10 +02:00
`highWaterMark` , calls to `writable.write()` will return `true` . Once
2016-05-23 22:30:41 -07:00
the size of the internal buffer reaches or exceeds the `highWaterMark` , `false`
will be returned.
2019-10-02 00:31:57 -04:00
A key goal of the `stream` API, particularly the [`stream.pipe()` ][] method,
2016-05-23 22:30:41 -07:00
is to limit the buffering of data to acceptable levels such that sources and
destinations of differing speeds will not overwhelm the available memory.
2020-05-16 14:53:36 +03:00
The `highWaterMark` option is a threshold, not a limit: it dictates the amount
of data that a stream buffers before it stops asking for more data. It does not
enforce a strict memory limitation in general. Specific stream implementations
may choose to enforce stricter limits but doing so is optional.
2018-04-29 20:46:41 +03:00
Because [`Duplex` ][] and [`Transform` ][] streams are both `Readable` and
2021-10-10 21:55:04 -07:00
`Writable` , each maintains _two_ separate internal buffers used for reading and
2018-04-29 20:46:41 +03:00
writing, allowing each side to operate independently of the other while
maintaining an appropriate and efficient flow of data. For example,
[`net.Socket` ][] instances are [`Duplex` ][] streams whose `Readable` side allows
2021-10-10 21:55:04 -07:00
consumption of data received _from_ the socket and whose `Writable` side allows
writing data _to_ the socket. Because data may be written to the socket at a
2019-10-24 15:19:07 -07:00
faster or slower rate than data is received, each side should
2018-04-29 20:46:41 +03:00
operate (and buffer) independently of the other.
2013-07-15 16:56:02 -07:00
2020-12-31 15:02:48 -08:00
The mechanics of the internal buffering are an internal implementation detail
and may be changed at any time. However, for certain advanced implementations,
the internal buffers can be retrieved using `writable.writableBuffer` or
`readable.readableBuffer` . Use of these undocumented properties is discouraged.
2020-06-14 14:49:34 -07:00
## API for stream consumers
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
<!-- type=misc -->
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
Almost all Node.js applications, no matter how simple, use streams in some
manner. The following is an example of using streams in a Node.js application
that implements an HTTP server:
2012-12-13 11:15:49 -08:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const http = require('node:http');
2013-07-15 16:56:02 -07:00
2017-04-21 17:38:31 +03:00
const server = http.createServer((req, res) => {
2020-06-14 14:49:34 -07:00
// `req` is an http.IncomingMessage, which is a readable stream.
// `res` is an http.ServerResponse, which is a writable stream.
2013-07-15 16:56:02 -07:00
2016-06-13 10:32:44 -04:00
let body = '';
2016-05-23 22:30:41 -07:00
// Get the data as utf8 strings.
// If an encoding is not set, Buffer objects will be received.
2013-07-15 16:56:02 -07:00
req.setEncoding('utf8');
2019-07-07 20:56:12 +03:00
// Readable streams emit 'data' events once a listener is added.
2015-12-14 15:20:25 -08:00
req.on('data', (chunk) => {
2013-07-15 16:56:02 -07:00
body += chunk;
2014-10-03 15:53:15 +10:00
});
2013-07-15 16:56:02 -07:00
2019-07-07 20:56:12 +03:00
// The 'end' event indicates that the entire body has been received.
2015-12-14 15:20:25 -08:00
req.on('end', () => {
2013-07-15 16:56:02 -07:00
try {
2016-05-23 22:30:41 -07:00
const data = JSON.parse(body);
2019-01-21 01:22:27 +01:00
// Write back something interesting to the user:
2016-08-16 17:56:52 +01:00
res.write(typeof data);
res.end();
2013-07-15 16:56:02 -07:00
} catch (er) {
2017-05-21 13:18:16 +03:00
// uh oh! bad json!
2013-07-15 16:56:02 -07:00
res.statusCode = 400;
2015-12-14 15:20:25 -08:00
return res.end(`error: ${er.message}` );
2013-07-15 16:56:02 -07:00
}
2014-10-03 15:53:15 +10:00
});
});
2012-12-13 11:15:49 -08:00
2013-07-15 16:56:02 -07:00
server.listen(1337);
2017-05-21 13:18:16 +03:00
// $ curl localhost:1337 -d "{}"
2013-07-15 16:56:02 -07:00
// object
2017-05-21 13:18:16 +03:00
// $ curl localhost:1337 -d "\"foo\""
2013-07-15 16:56:02 -07:00
// string
2017-05-21 13:18:16 +03:00
// $ curl localhost:1337 -d "not json"
2022-11-29 19:25:41 +09:00
// error: Unexpected token 'o', "not json" is not valid JSON
2012-12-13 11:15:49 -08:00
```
2018-04-29 20:46:41 +03:00
[`Writable` ][] streams (such as `res` in the example) expose methods such as
2016-05-23 22:30:41 -07:00
`write()` and `end()` that are used to write data onto the stream.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
[`Readable` ][] streams use the [`EventEmitter` ][] API for notifying application
2016-05-23 22:30:41 -07:00
code when data is available to be read off the stream. That available data can
be read from the stream in multiple ways.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
Both [`Writable` ][] and [`Readable` ][] streams use the [`EventEmitter` ][] API in
2016-05-23 22:30:41 -07:00
various ways to communicate the current state of the stream.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
[`Duplex` ][] and [`Transform` ][] streams are both [`Writable` ][] and
[`Readable` ][].
2016-05-23 22:30:41 -07:00
Applications that are either writing data to or consuming data from a stream
are not required to implement the stream interfaces directly and will generally
2022-04-20 10:23:41 +02:00
have no reason to call `require('node:stream')` .
2016-05-23 22:30:41 -07:00
Developers wishing to implement new types of streams should refer to the
2020-06-14 14:49:34 -07:00
section [API for stream implementers][].
2016-05-23 22:30:41 -07:00
2020-06-14 14:49:34 -07:00
### Writable streams
2016-05-23 22:30:41 -07:00
2021-10-10 21:55:04 -07:00
Writable streams are an abstraction for a _destination_ to which data is
2016-05-23 22:30:41 -07:00
written.
2018-04-29 20:46:41 +03:00
Examples of [`Writable` ][] streams include:
2016-05-23 22:30:41 -07:00
* [HTTP requests, on the client][]
* [HTTP responses, on the server][]
* [fs write streams][]
2016-02-02 20:34:29 +03:00
* [zlib streams][zlib]
* [crypto streams][crypto]
2016-05-23 22:30:41 -07:00
* [TCP sockets][]
* [child process stdin][]
* [`process.stdout` ][], [`process.stderr` ][]
2018-04-29 20:46:41 +03:00
Some of these examples are actually [`Duplex` ][] streams that implement the
[`Writable` ][] interface.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
All [`Writable` ][] streams implement the interface defined by the
2016-05-23 22:30:41 -07:00
`stream.Writable` class.
2018-04-29 20:46:41 +03:00
While specific instances of [`Writable` ][] streams may differ in various ways,
all `Writable` streams follow the same fundamental usage pattern as illustrated
2016-05-23 22:30:41 -07:00
in the example below:
```js
const myStream = getWritableStreamSomehow();
myStream.write('some data');
myStream.write('some more data');
myStream.end('done writing data');
```
2015-11-05 14:54:10 -05:00
2019-12-24 15:09:29 -08:00
#### Class: `stream.Writable`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2012-12-13 11:15:49 -08:00
2013-07-15 16:56:02 -07:00
<!-- type=class -->
2019-12-24 15:09:29 -08:00
##### Event: `'close'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
2019-01-09 13:07:59 +01:00
changes:
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18438
description: Add `emitClose` option to specify if `'close'` is emitted on
destroy.
2016-06-13 10:32:44 -04:00
-->
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
The `'close'` event is emitted when the stream and any of its underlying
resources (a file descriptor, for example) have been closed. The event indicates
that no more events will be emitted, and no further computation will occur.
2013-07-15 16:56:02 -07:00
2019-01-09 13:07:59 +01:00
A [`Writable` ][] stream will always emit the `'close'` event if it is
created with the `emitClose` option.
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-17 18:24:02 -07:00
2019-12-24 15:09:29 -08:00
##### Event: `'drain'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-17 18:24:02 -07:00
2016-05-23 22:30:41 -07:00
If a call to [`stream.write(chunk)` ][stream-write] returns `false` , the
`'drain'` event will be emitted when it is appropriate to resume writing data
to the stream.
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-17 18:24:02 -07:00
2016-05-23 22:30:41 -07:00
```js
// Write the data to the supplied writable stream one million times.
// Be attentive to back-pressure.
function writeOneMillionTimes(writer, data, encoding, callback) {
2016-06-13 10:32:44 -04:00
let i = 1000000;
2016-05-23 22:30:41 -07:00
write();
function write() {
2017-04-22 15:22:40 +03:00
let ok = true;
2016-05-23 22:30:41 -07:00
do {
i--;
if (i === 0) {
2019-07-07 20:56:12 +03:00
// Last time!
2016-05-23 22:30:41 -07:00
writer.write(data, encoding, callback);
} else {
2019-03-07 01:03:53 +01:00
// See if we should continue, or wait.
// Don't pass the callback, because we're not done yet.
2016-05-23 22:30:41 -07:00
ok = writer.write(data, encoding);
}
} while (i > 0 & & ok);
if (i > 0) {
2019-07-07 20:56:12 +03:00
// Had to stop early!
// Write some more once it drains.
2016-05-23 22:30:41 -07:00
writer.once('drain', write);
}
}
}
```
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-17 18:24:02 -07:00
2019-12-24 15:09:29 -08:00
##### Event: `'error'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-17 18:24:02 -07:00
2016-05-23 22:30:41 -07:00
* {Error}
The `'error'` event is emitted if an error occurred while writing or piping
data. The listener callback is passed a single `Error` argument when called.
2019-11-24 13:17:56 +01:00
The stream is closed when the `'error'` event is emitted unless the
[`autoDestroy` ][writable-new] option was set to `false` when creating the
2019-08-11 11:23:46 +02:00
stream.
2016-05-23 22:30:41 -07:00
2021-10-10 21:55:04 -07:00
After `'error'` , no further events other than `'close'` _should_ be emitted
2019-07-16 00:03:23 +02:00
(including `'error'` events).
2019-12-24 15:09:29 -08:00
##### Event: `'finish'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2016-05-23 22:30:41 -07:00
The `'finish'` event is emitted after the [`stream.end()` ][stream-end] method
has been called, and all data has been flushed to the underlying system.
```js
const writer = getWritableStreamSomehow();
2017-04-21 22:55:51 +03:00
for (let i = 0; i < 100 ; i + + ) {
2017-04-04 14:17:12 -07:00
writer.write(`hello, #${i}!\n` );
2016-05-23 22:30:41 -07:00
}
writer.on('finish', () => {
2018-11-06 08:40:22 +10:00
console.log('All writes are now complete.');
2016-05-23 22:30:41 -07:00
});
2019-12-10 23:17:39 +05:30
writer.end('This is the end\n');
2016-05-23 22:30:41 -07:00
```
2019-12-24 15:09:29 -08:00
##### Event: `'pipe'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2016-05-23 22:30:41 -07:00
* `src` {stream.Readable} source stream that is piping to this writable
The `'pipe'` event is emitted when the [`stream.pipe()` ][] method is called on
a readable stream, adding this writable to its set of destinations.
```js
const writer = getWritableStreamSomehow();
const reader = getReadableStreamSomehow();
writer.on('pipe', (src) => {
2018-11-06 08:40:22 +10:00
console.log('Something is piping into the writer.');
2016-05-23 22:30:41 -07:00
assert.equal(src, reader);
});
reader.pipe(writer);
```
2019-12-24 15:09:29 -08:00
##### Event: `'unpipe'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2016-05-23 22:30:41 -07:00
2017-11-06 12:14:23 +05:30
* `src` {stream.Readable} The source stream that
2016-05-23 22:30:41 -07:00
[unpiped][`stream.unpipe()` ] this writable
The `'unpipe'` event is emitted when the [`stream.unpipe()` ][] method is called
2018-04-29 20:46:41 +03:00
on a [`Readable` ][] stream, removing this [`Writable` ][] from its set of
2016-05-23 22:30:41 -07:00
destinations.
2018-04-29 20:46:41 +03:00
This is also emitted in case this [`Writable` ][] stream emits an error when a
[`Readable` ][] stream pipes into it.
2018-02-08 09:22:02 +01:00
2016-05-23 22:30:41 -07:00
```js
const writer = getWritableStreamSomehow();
const reader = getReadableStreamSomehow();
writer.on('unpipe', (src) => {
2018-11-06 08:40:22 +10:00
console.log('Something has stopped piping into the writer.');
2016-05-23 22:30:41 -07:00
assert.equal(src, reader);
});
reader.pipe(writer);
reader.unpipe(writer);
```
2019-12-24 15:09:29 -08:00
##### `writable.cork()`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.11.2
-->
2016-05-23 22:30:41 -07:00
The `writable.cork()` method forces all written data to be buffered in memory.
The buffered data will be flushed when either the [`stream.uncork()` ][] or
[`stream.end()` ][stream-end] methods are called.
2019-11-12 10:29:39 -06:00
The primary intent of `writable.cork()` is to accommodate a situation in which
several small chunks are written to the stream in rapid succession. Instead of
immediately forwarding them to the underlying destination, `writable.cork()`
buffers all the chunks until `writable.uncork()` is called, which will pass them
all to `writable._writev()` , if present. This prevents a head-of-line blocking
situation where data is being buffered while waiting for the first small chunk
to be processed. However, use of `writable.cork()` without implementing
`writable._writev()` may have an adverse effect on throughput.
See also: [`writable.uncork()` ][], [`writable._writev()` ][stream-_writev].
2017-02-07 19:10:03 +01:00
2019-12-24 15:09:29 -08:00
##### `writable.destroy([error])`
2021-10-10 21:55:04 -07:00
2018-03-21 04:12:32 +02:00
<!-- YAML
added: v8.0.0
2020-09-24 12:27:06 +02:00
changes:
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/29197
2020-09-27 07:50:41 -07:00
description: Work as a no-op on a stream that has already been destroyed.
2018-03-21 04:12:32 +02:00
-->
2019-02-06 15:37:40 -08:00
* `error` {Error} Optional, an error to emit with `'error'` event.
2018-03-21 04:12:32 +02:00
* Returns: {this}
2019-03-11 19:06:12 +01:00
Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`
2019-08-05 12:01:33 +02:00
event (unless `emitClose` is set to `false` ). After this call, the writable
2019-03-11 19:06:12 +01:00
stream has ended and subsequent calls to `write()` or `end()` will result in
an `ERR_STREAM_DESTROYED` error.
2019-02-06 15:37:40 -08:00
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.
Use `end()` instead of destroy if data should flush before close, or wait for
the `'drain'` event before destroying the stream.
2019-08-18 23:38:35 +02:00
2021-07-22 16:16:50 -05:00
```cjs
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2021-07-22 16:16:50 -05:00
const myStream = new Writable();
const fooErr = new Error('foo error');
myStream.destroy(fooErr);
myStream.on('error', (fooErr) => console.error(fooErr.message)); // foo error
```
```cjs
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2021-07-22 16:16:50 -05:00
const myStream = new Writable();
myStream.destroy();
myStream.on('error', function wontHappen() {});
```
```cjs
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2021-07-22 16:16:50 -05:00
const myStream = new Writable();
myStream.destroy();
myStream.write('foo', (error) => console.error(error.code));
// ERR_STREAM_DESTROYED
```
2020-09-27 07:50:41 -07:00
Once `destroy()` has been called any further calls will be a no-op and no
further errors except from `_destroy()` may be emitted as `'error'` .
2019-08-18 23:38:35 +02:00
2018-03-21 04:12:32 +02:00
Implementors should not override this method,
2018-04-09 19:30:22 +03:00
but instead implement [`writable._destroy()` ][writable-_destroy].
2018-03-21 04:12:32 +02:00
2021-11-02 12:01:48 +02:00
##### `writable.closed`
<!-- YAML
2022-04-19, Version 18.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) stream: remove thenable support (Robert Nagy)
(https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
fetch (experimental):
An experimental fetch API is available on the global scope by default.
The implementation is based upon https://undici.nodejs.org/#/,
an HTTP/1.1 client written for Node.js by contributors to the project.
Through this addition, the following globals are made available: `fetch`
, `FormData`, `Headers`, `Request`, `Response`.
Disable this API with the `--no-experimental-fetch` command-line flag.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/41811.
HTTP Timeouts:
`server.headersTimeout`, which limits the amount of time the parser will
wait to receive the complete HTTP headers, is now set to `60000` (60
seconds) by default.
`server.requestTimeout`, which sets the timeout value in milliseconds
for receiving the entire request from the client, is now set to `300000`
(5 minutes) by default.
If these timeouts expire, the server responds with status 408 without
forwarding the request to the request listener and then closes the
connection.
Both timeouts must be set to a non-zero value to protect against
potential Denial-of-Service attacks in case the server is deployed
without a reverse proxy in front.
Contributed by Paolo Insogna in https://github.com/nodejs/node/pull/41263.
Test Runner module (experimental):
The `node:test` module facilitates the creation of JavaScript tests that
report results in TAP format. This module is only available under the
`node:` scheme.
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/42325.
Toolchain and Compiler Upgrades:
- Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux
(RHEL) 8 and are compatible with Linux distributions based on glibc
2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
- Prebuilt binaries for macOS now require macOS 10.15 or later.
- For AIX the minimum supported architecture has been raised from Power
7 to Power 8.
Prebuilt binaries for 32-bit Windows will initially not be available due
to issues building the V8 dependency in Node.js. We hope to restore
32-bit Windows binaries for Node.js 18 with a future V8 update.
Node.js does not support running on operating systems that are no longer
supported by their vendor. For operating systems where their vendor has
planned to end support earlier than April 2025, such as Windows 8.1
(January 2023) and Windows Server 2012 R2 (October 2023), support for
Node.js 18 will end at the earlier date.
Full details about the supported toolchains and compilers are documented
in the Node.js `BUILDING.md` file.
Contributed by Richard Lau in https://github.com/nodejs/node/pull/42292,
https://github.com/nodejs/node/pull/42604 and https://github.com/nodejs/node/pull/42659
, and Michaël Zasso in https://github.com/nodejs/node/pull/42105 and
https://github.com/nodejs/node/pull/42666.
V8 10.1:
The V8 engine is updated to version 10.1, which is part of Chromium 101.
Compared to the version included in Node.js 17.9.0, the following new
features are included:
- The `findLast` and `findLastIndex` array methods.
- Improvements to the `Intl.Locale` API.
- The `Intl.supportedValuesOf` function.
- Improved performance of class fields and private class methods (the
initialization of them is now as fast as ordinary property stores).
The data format returned by the serialization API (`v8.serialize(value)`)
has changed, and cannot be deserialized by earlier versions of Node.js.
On the other hand, it is still possible to deserialize the previous
format, as the API is backwards-compatible.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/42657.
Web Streams API (experimental):
Node.js now exposes the experimental implementation of the Web Streams
API on the global scope. This means the following APIs are now globally
available:
- `ReadableStream`, `ReadableStreamDefaultReader`,
`ReadableStreamBYOBReader`, `ReadableStreamBYOBRequest`,
`ReadableByteStreamController`, `ReadableStreamDefaultController`,
`TransformStream`, `TransformStreamDefaultController`, `WritableStream`,
`WritableStreamDefaultWriter`, `WritableStreamDefaultController`,
`ByteLengthQueuingStrategy`, `CountQueuingStrategy`, `TextEncoderStream`,
`TextDecoderStream`, `CompressionStream`, `DecompressionStream`.
Contributed James Snell in https://github.com/nodejs/node/pull/39062,
and Antoine du Hamel in https://github.com/nodejs/node/pull/42225.
Other Notable Changes:
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- doc: add RafaelGSS to collaborators
(RafaelGSS) (https://github.com/nodejs/node/pull/42718)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
Semver-Major Commits:
- (SEMVER-MAJOR) assert,util: compare RegExp.lastIndex while using deep
equal checks
(Ruben Bridgewater) (https://github.com/nodejs/node/pull/41020)
- (SEMVER-MAJOR) buffer: refactor `byteLength` to remove outdated
optimizations
(Rongjian Zhang) (https://github.com/nodejs/node/pull/38545)
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) buffer: graduate Blob from experimental
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) build: make x86 Windows support temporarily
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42666)
- (SEMVER-MAJOR) build: bump macOS deployment target to 10.15
(Richard Lau) (https://github.com/nodejs/node/pull/42292)
- (SEMVER-MAJOR) build: downgrade Windows 8.1 and server 2012 R2 to
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42105)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- (SEMVER-MAJOR) cluster: make `kill` to be just `process.kill`
(Bar Admoni) (https://github.com/nodejs/node/pull/34312)
- (SEMVER-MAJOR) crypto: cleanup validation
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/39841)
- (SEMVER-MAJOR) crypto: prettify othername in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42123)
- (SEMVER-MAJOR) crypto: fix X509Certificate toLegacyObject
(Tobias Nießen) (https://github.com/nodejs/node/pull/42124)
- (SEMVER-MAJOR) crypto: use RFC2253 format in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42002)
- (SEMVER-MAJOR) crypto: change default check(Host|Email) behavior
(Tobias Nießen) (https://github.com/nodejs/node/pull/41600)
- (SEMVER-MAJOR) deps: V8: cherry-pick semver-major commits from 10.2
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 10.1.124.6
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 9.8.177.9
(Michaël Zasso) (https://github.com/nodejs/node/pull/41610)
- (SEMVER-MAJOR) deps: update V8 to 9.7.106.18
(Michaël Zasso) (https://github.com/nodejs/node/pull/40907)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) doc: update minimum glibc requirements for Linux
(Richard Lau) (https://github.com/nodejs/node/pull/42659)
- (SEMVER-MAJOR) doc: update AIX minimum supported arch
(Richard Lau) (https://github.com/nodejs/node/pull/42604)
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) http: refactor headersTimeout and requestTimeout logic
(Paolo Insogna) (https://github.com/nodejs/node/pull/41263)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) lib: enable fetch by default
(Michaël Zasso) (https://github.com/nodejs/node/pull/41811)
- (SEMVER-MAJOR) lib: replace validator and error
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/41678)
- (SEMVER-MAJOR) module,repl: support 'node:'-only core modules
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: disallow some uses of Object.defineProperty()
on process.env
(Himself65) (https://github.com/nodejs/node/pull/28006)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) readline: fix question still called after closed
(Xuguang Mei) (https://github.com/nodejs/node/pull/42464)
- (SEMVER-MAJOR) stream: remove thenable support
(Robert Nagy) (https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) stream: expose web streams globals, remove runtime
experimental warning
(Antoine du Hamel) (https://github.com/nodejs/node/pull/42225)
- (SEMVER-MAJOR) stream: need to cleanup event listeners if last stream
is readable
(Xuguang Mei) (https://github.com/nodejs/node/pull/41954)
- (SEMVER-MAJOR) stream: revert revert `map` spec compliance
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41933)
- (SEMVER-MAJOR) stream: throw invalid arg type from End Of Stream
(Jithil P Ponnan) (https://github.com/nodejs/node/pull/41766)
- (SEMVER-MAJOR) stream: don't emit finish after destroy
(Robert Nagy) (https://github.com/nodejs/node/pull/40852)
- (SEMVER-MAJOR) stream: add errored and closed props
(Robert Nagy) (https://github.com/nodejs/node/pull/40696)
- (SEMVER-MAJOR) test: add initial test module
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) timers: refactor internal classes to ES2015 syntax
(Rabbit) (https://github.com/nodejs/node/pull/37408)
- (SEMVER-MAJOR) tls: represent registeredID numerically always
(Tobias Nießen) (https://github.com/nodejs/node/pull/41561)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
- (SEMVER-MAJOR) url: throw on NULL in IPv6 hostname
(Rich Trott) (https://github.com/nodejs/node/pull/42313)
- (SEMVER-MAJOR) v8: make v8.writeHeapSnapshot() error codes consistent
(Darshan Sen) (https://github.com/nodejs/node/pull/42577)
- (SEMVER-MAJOR) v8: make writeHeapSnapshot throw if fopen fails
(Antonio Román) (https://github.com/nodejs/node/pull/41373)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
PR-URL: https://github.com/nodejs/node/pull/42262
2022-03-08 01:39:47 +00:00
added: v18.0.0
2021-11-02 12:01:48 +02:00
-->
* {boolean}
Is `true` after `'close'` has been emitted.
2019-12-24 15:09:29 -08:00
##### `writable.destroyed`
2021-10-10 21:55:04 -07:00
2019-07-23 09:45:20 +02:00
<!-- YAML
added: v8.0.0
-->
* {boolean}
Is `true` after [`writable.destroy()` ][writable-destroy] has been called.
2021-07-22 16:16:50 -05:00
```cjs
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2021-07-22 16:16:50 -05:00
const myStream = new Writable();
console.log(myStream.destroyed); // false
myStream.destroy();
console.log(myStream.destroyed); // true
```
2019-12-24 15:09:29 -08:00
##### `writable.end([chunk[, encoding]][, callback])`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
2017-01-09 19:05:06 +01:00
changes:
2024-05-02 11:31:36 +02:00
- version:
- v22.0.0
- v20.13.0
2024-03-20 18:27:29 +01:00
pr-url: https://github.com/nodejs/node/pull/51866
description: The `chunk` argument can now be a `TypedArray` or `DataView` instance.
2020-10-20, Version 15.0.0 (Current)
Notable changes:
Deprecations and Removals:
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
npm 7 (https://github.com/nodejs/node/pull/35631):
Node.js 15 comes with a new major release of npm, npm 7. npm 7 comes
with many new features - including npm workspaces and a new
package-lock.json format. npm 7 also includes yarn.lock file support.
One of the big changes in npm 7 is that peer dependencies are now
installed by default.
Throw On Unhandled Rejections
(https://github.com/nodejs/node/pull/33021):
As of Node.js 15, the default mode for `unhandledRejection` is changed
to `throw` (from `warn`). In `throw` mode, if an `unhandledRejection`
hook is not set, the `unhandledRejection` is raised as an uncaught
exception. Users that have an `unhandledRejection` hook should see no
change in behavior, and it’s still possible to switch modes using the
`--unhandled-rejections=mode` process flag.
QUIC (https://github.com/nodejs/node/pull/32379):
Node.js 15 comes with experimental support QUIC, which can be enabled
by compiling Node.js with the `--experimental-quic` configuration flag.
The Node.js QUIC implementation is exposed by the core `net` module.
V8 8.6 (https://github.com/nodejs/node/pull/35415):
The V8 JavaScript engine has been updated to V8 8.6 (V8 8.4 is the
latest available in Node.js 14). Along with performance tweaks and
improvements the V8 update also brings the following language features:
* `Promise.any()` (from V8 8.5)
* `AggregateError` (from V8 8.5)
* `String.prototype.replaceAll()` (from V8 8.5)
* Logical assignment operators `&&=`, `||=`, and `??=` (from V8 8.5)
Other Notable Changes:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
Semver-Major Commits:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **build**: reset embedder string to "-node.0" (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **crypto**: refactoring internals, add WebCrypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **crypto**: move node\_crypto files to src/crypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **deps**: V8: cherry-pick d76abfed3512 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 717543bbf0ef (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 6be2f6e26e8d (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix V8 build issue with inline methods (Jiawen Geng)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix platform-embedded-file-writer-win for ARM64 (Michaël
Zasso) (https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 postmortem metadata script (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 to 8.6.395 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: upgrade npm to 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **deps**: update npm to 7.0.0-rc.3 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **deps**: V8: cherry-pick 0d6debcc5f08 (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **doc**: update support macos version for 15.x (Ash Cripps)
(https://github.com/nodejs/node/pull/35022)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http**: fixed socket.setEncoding fatal error (iskore)
(https://github.com/nodejs/node/pull/33405)
- **http**: emit 'error' on aborted server request (Robert Nagy)
(https://github.com/nodejs/node/pull/33172)
- **http**: cleanup end argument handling (Robert Nagy)
(https://github.com/nodejs/node/pull/31818)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **http2**: add `invalidheaders` test (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33161)
- **http2**: refactor state code validation for the http2Stream class
(rickyes) (https://github.com/nodejs/node/pull/33535)
- **http2**: header field valid checks (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33193)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: remove ERR\_INVALID\_OPT\_VALUE and
ERR\_INVALID\_OPT\_VALUE\_ENCODING (Denys Otrishko)
(https://github.com/nodejs/node/pull/34682)
- **lib**: handle one of args case in ERR\_MISSING\_ARGS (Denys
Otrishko) (https://github.com/nodejs/node/pull/34022)
- **lib**: remove NodeError from the prototype of errors with code
(Michaël Zasso) (https://github.com/nodejs/node/pull/33857)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: check args in net.connect() and socket.connect() calls
(Denys Otrishko) (https://github.com/nodejs/node/pull/34022)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **process**: update v8 fast api calls usage (Maya Lekova)
(https://github.com/nodejs/node/pull/35415)
- **process**: change default --unhandled-rejections=throw (Dan
Fabulich) (https://github.com/nodejs/node/pull/33021)
- **process**: use v8 fast api calls for hrtime (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **process**: delay throwing an error using `throwDeprecation` (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/32312)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: implement NodePlatform::PostJob (Clemens Backes)
(https://github.com/nodejs/node/pull/35415)
- **src**: update NODE\_MODULE\_VERSION to 88 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **src**: error reporting on CPUUsage (Yash Ladha)
(https://github.com/nodejs/node/pull/34762)
- **src**: use node:moduleName as builtin module filename (Michaël
Zasso) (https://github.com/nodejs/node/pull/35498)
- **src**: enable wasm trap handler on windows (Gus Caplan)
(https://github.com/nodejs/node/pull/35033)
- **src**: update NODE\_MODULE\_VERSION to 86 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33579)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **src**: remove \_third\_party\_main support (Anna Henningsen)
(https://github.com/nodejs/node/pull/33971)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **src**: remove unused CancelPendingDelayedTasks (Anna Henningsen)
(https://github.com/nodejs/node/pull/32859)
- **stream**: try to wait for flush to complete before 'finish' (Robert
Nagy) (https://github.com/nodejs/node/pull/34314)
- **stream**: cleanup and fix Readable.wrap (Robert Nagy)
(https://github.com/nodejs/node/pull/34204)
- **stream**: add promises version to utility functions (rickyes)
(https://github.com/nodejs/node/pull/33991)
- **stream**: fix writable.end callback behavior (Robert Nagy)
(https://github.com/nodejs/node/pull/34101)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **stream**: write should throw on unknown encoding (Robert Nagy)
(https://github.com/nodejs/node/pull/33075)
- **stream**: fix \_final and 'prefinish' timing (Robert Nagy)
(https://github.com/nodejs/node/pull/32780)
- **stream**: simplify Transform stream implementation (Robert Nagy)
(https://github.com/nodejs/node/pull/32763)
- **stream**: use callback to properly propagate error (Robert Nagy)
(https://github.com/nodejs/node/pull/29179)
- **test**: update tests after increasing typed array size to 4GB
(Kim-Anh Tran) (https://github.com/nodejs/node/pull/35415)
- **test**: fix tests for npm 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **test**: fix test suite to work with npm 7 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **test**: update WPT harness and tests (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **tools**: disable x86 safe exception handlers in V8 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.6 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.5 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **url**: file URL path normalization (Daijiro Wachi)
(https://github.com/nodejs/node/pull/35477)
- **url**: verify domain is not empty after "ToASCII" (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove U+0000 case in the fragment state (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove gopher from special schemes (Michaël Zasso)
(https://github.com/nodejs/node/pull/33325)
- **url**: forbid lt and gt in url host code point (Yash Ladha)
(https://github.com/nodejs/node/pull/33328)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **win, child_process**: sanitize env variables (Bartosz Sosnowski)
(https://github.com/nodejs/node/pull/35210)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
- **worker**: set trackUnmanagedFds to true by default (Anna
Henningsen) (https://github.com/nodejs/node/pull/34394)
- **worker**: rename error code to be more accurate (Anna Henningsen)
(https://github.com/nodejs/node/pull/33872)
PR-URL: https://github.com/nodejs/node/pull/35014
2020-09-01 21:16:46 +01:00
- version: v15.0.0
2020-06-28 17:44:07 +02:00
pr-url: https://github.com/nodejs/node/pull/34101
description: The `callback` is invoked before 'finish' or on error.
2020-03-10 17:16:08 +00:00
- version: v14.0.0
2020-03-13 03:31:10 +05:30
pr-url: https://github.com/nodejs/node/pull/29747
description: The `callback` is invoked if 'finish' or 'error' is emitted.
2018-03-02 09:53:46 -08:00
- version: v10.0.0
2018-02-14 12:29:17 +00:00
pr-url: https://github.com/nodejs/node/pull/18780
description: This method now returns a reference to `writable` .
2017-03-15 20:26:14 -07:00
- version: v8.0.0
2017-01-09 19:05:06 +01:00
pr-url: https://github.com/nodejs/node/pull/11608
description: The `chunk` argument can now be a `Uint8Array` instance.
2016-06-13 10:32:44 -04:00
-->
2016-05-23 22:30:41 -07:00
2024-03-20 18:27:29 +01:00
* `chunk` {string|Buffer|TypedArray|DataView|any} Optional data to write. For
streams not operating in object mode, `chunk` must be a {string}, {Buffer},
{TypedArray} or {DataView}. For object mode streams, `chunk` may be any
JavaScript value other than `null` .
2018-11-05 20:40:07 -08:00
* `encoding` {string} The encoding if `chunk` is a string
2020-06-28 17:44:07 +02:00
* `callback` {Function} Callback for when the stream is finished.
2018-02-14 12:29:17 +00:00
* Returns: {this}
2016-05-23 22:30:41 -07:00
Calling the `writable.end()` method signals that no more data will be written
2018-04-29 20:46:41 +03:00
to the [`Writable` ][]. The optional `chunk` and `encoding` arguments allow one
2016-05-23 22:30:41 -07:00
final additional chunk of data to be written immediately before closing the
2020-06-28 17:44:07 +02:00
stream.
2016-05-23 22:30:41 -07:00
Calling the [`stream.write()` ][stream-write] method after calling
[`stream.end()` ][stream-end] will raise an error.
```js
2019-07-07 20:56:12 +03:00
// Write 'hello, ' and then end with 'world!'.
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
2016-05-23 22:30:41 -07:00
const file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
2019-03-07 01:03:53 +01:00
// Writing more now is not allowed!
2016-05-23 22:30:41 -07:00
```
2019-12-24 15:09:29 -08:00
##### `writable.setDefaultEncoding(encoding)`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.11.15
2017-02-21 23:38:48 +01:00
changes:
- version: v6.1.0
pr-url: https://github.com/nodejs/node/pull/5040
description: This method now returns a reference to `writable` .
2016-06-13 10:32:44 -04:00
-->
2016-05-23 22:30:41 -07:00
2017-02-04 16:15:33 +01:00
* `encoding` {string} The new default encoding
2018-01-30 00:15:53 +02:00
* Returns: {this}
2016-05-23 22:30:41 -07:00
The `writable.setDefaultEncoding()` method sets the default `encoding` for a
2018-04-29 20:46:41 +03:00
[`Writable` ][] stream.
2016-05-23 22:30:41 -07:00
2019-12-24 15:09:29 -08:00
##### `writable.uncork()`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.11.2
-->
2016-05-23 22:30:41 -07:00
The `writable.uncork()` method flushes all data buffered since
[`stream.cork()` ][] was called.
2017-02-07 19:10:03 +01:00
When using [`writable.cork()` ][] and `writable.uncork()` to manage the buffering
2022-03-31 20:12:30 -07:00
of writes to a stream, defer calls to `writable.uncork()` using
`process.nextTick()` . Doing so allows batching of all
2016-05-23 22:30:41 -07:00
`writable.write()` calls that occur within a given Node.js event loop phase.
```js
stream.cork();
stream.write('some ');
stream.write('data ');
process.nextTick(() => stream.uncork());
```
2018-02-12 02:31:55 -05:00
If the [`writable.cork()` ][] method is called multiple times on a stream, the
same number of calls to `writable.uncork()` must be called to flush the buffered
2016-05-23 22:30:41 -07:00
data.
2016-07-09 08:13:09 +03:00
```js
2016-05-23 22:30:41 -07:00
stream.cork();
stream.write('some ');
stream.cork();
stream.write('data ');
process.nextTick(() => {
stream.uncork();
// The data will not be flushed until uncork() is called a second time.
stream.uncork();
});
```
2017-02-07 19:10:03 +01:00
See also: [`writable.cork()` ][].
2019-12-24 15:09:29 -08:00
##### `writable.writable`
2021-10-10 21:55:04 -07:00
2018-10-28 11:55:22 +08:00
<!-- YAML
2018-12-05 21:29:36 +01:00
added: v11.4.0
2018-10-28 11:55:22 +08:00
-->
* {boolean}
2020-01-05 18:41:31 +01:00
Is `true` if it is safe to call [`writable.write()` ][stream-write], which means
2022-09-15 02:16:10 +09:00
the stream has not been destroyed, errored, or ended.
2018-10-28 11:55:22 +08:00
2021-11-13 15:08:14 +02:00
##### `writable.writableAborted`
<!-- YAML
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
added:
- v18.0.0
- v16.17.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-11-13 15:08:14 +02:00
-->
* {boolean}
Returns whether the stream was destroyed or errored before emitting `'finish'` .
2019-12-24 15:09:29 -08:00
##### `writable.writableEnded`
2021-10-10 21:55:04 -07:00
2019-08-02 08:09:06 +02:00
<!-- YAML
2019-08-19 21:14:22 +02:00
added: v12.9.0
2019-08-02 08:09:06 +02:00
-->
* {boolean}
Is `true` after [`writable.end()` ][] has been called. This property
does not indicate whether the data has been flushed, for this use
[`writable.writableFinished` ][] instead.
2019-12-24 15:09:29 -08:00
##### `writable.writableCorked`
2021-10-10 21:55:04 -07:00
2019-08-06 13:41:12 +02:00
<!-- YAML
2020-04-24 18:43:06 +02:00
added:
- v13.2.0
- v12.16.0
2019-08-06 13:41:12 +02:00
-->
* {integer}
Number of times [`writable.uncork()` ][stream-uncork] needs to be
called in order to fully uncork the stream.
2021-11-13 14:42:30 +02:00
##### `writable.errored`
2021-11-02 12:01:48 +02:00
<!-- YAML
added:
2022-04-19, Version 18.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) stream: remove thenable support (Robert Nagy)
(https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
fetch (experimental):
An experimental fetch API is available on the global scope by default.
The implementation is based upon https://undici.nodejs.org/#/,
an HTTP/1.1 client written for Node.js by contributors to the project.
Through this addition, the following globals are made available: `fetch`
, `FormData`, `Headers`, `Request`, `Response`.
Disable this API with the `--no-experimental-fetch` command-line flag.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/41811.
HTTP Timeouts:
`server.headersTimeout`, which limits the amount of time the parser will
wait to receive the complete HTTP headers, is now set to `60000` (60
seconds) by default.
`server.requestTimeout`, which sets the timeout value in milliseconds
for receiving the entire request from the client, is now set to `300000`
(5 minutes) by default.
If these timeouts expire, the server responds with status 408 without
forwarding the request to the request listener and then closes the
connection.
Both timeouts must be set to a non-zero value to protect against
potential Denial-of-Service attacks in case the server is deployed
without a reverse proxy in front.
Contributed by Paolo Insogna in https://github.com/nodejs/node/pull/41263.
Test Runner module (experimental):
The `node:test` module facilitates the creation of JavaScript tests that
report results in TAP format. This module is only available under the
`node:` scheme.
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/42325.
Toolchain and Compiler Upgrades:
- Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux
(RHEL) 8 and are compatible with Linux distributions based on glibc
2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
- Prebuilt binaries for macOS now require macOS 10.15 or later.
- For AIX the minimum supported architecture has been raised from Power
7 to Power 8.
Prebuilt binaries for 32-bit Windows will initially not be available due
to issues building the V8 dependency in Node.js. We hope to restore
32-bit Windows binaries for Node.js 18 with a future V8 update.
Node.js does not support running on operating systems that are no longer
supported by their vendor. For operating systems where their vendor has
planned to end support earlier than April 2025, such as Windows 8.1
(January 2023) and Windows Server 2012 R2 (October 2023), support for
Node.js 18 will end at the earlier date.
Full details about the supported toolchains and compilers are documented
in the Node.js `BUILDING.md` file.
Contributed by Richard Lau in https://github.com/nodejs/node/pull/42292,
https://github.com/nodejs/node/pull/42604 and https://github.com/nodejs/node/pull/42659
, and Michaël Zasso in https://github.com/nodejs/node/pull/42105 and
https://github.com/nodejs/node/pull/42666.
V8 10.1:
The V8 engine is updated to version 10.1, which is part of Chromium 101.
Compared to the version included in Node.js 17.9.0, the following new
features are included:
- The `findLast` and `findLastIndex` array methods.
- Improvements to the `Intl.Locale` API.
- The `Intl.supportedValuesOf` function.
- Improved performance of class fields and private class methods (the
initialization of them is now as fast as ordinary property stores).
The data format returned by the serialization API (`v8.serialize(value)`)
has changed, and cannot be deserialized by earlier versions of Node.js.
On the other hand, it is still possible to deserialize the previous
format, as the API is backwards-compatible.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/42657.
Web Streams API (experimental):
Node.js now exposes the experimental implementation of the Web Streams
API on the global scope. This means the following APIs are now globally
available:
- `ReadableStream`, `ReadableStreamDefaultReader`,
`ReadableStreamBYOBReader`, `ReadableStreamBYOBRequest`,
`ReadableByteStreamController`, `ReadableStreamDefaultController`,
`TransformStream`, `TransformStreamDefaultController`, `WritableStream`,
`WritableStreamDefaultWriter`, `WritableStreamDefaultController`,
`ByteLengthQueuingStrategy`, `CountQueuingStrategy`, `TextEncoderStream`,
`TextDecoderStream`, `CompressionStream`, `DecompressionStream`.
Contributed James Snell in https://github.com/nodejs/node/pull/39062,
and Antoine du Hamel in https://github.com/nodejs/node/pull/42225.
Other Notable Changes:
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- doc: add RafaelGSS to collaborators
(RafaelGSS) (https://github.com/nodejs/node/pull/42718)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
Semver-Major Commits:
- (SEMVER-MAJOR) assert,util: compare RegExp.lastIndex while using deep
equal checks
(Ruben Bridgewater) (https://github.com/nodejs/node/pull/41020)
- (SEMVER-MAJOR) buffer: refactor `byteLength` to remove outdated
optimizations
(Rongjian Zhang) (https://github.com/nodejs/node/pull/38545)
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) buffer: graduate Blob from experimental
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) build: make x86 Windows support temporarily
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42666)
- (SEMVER-MAJOR) build: bump macOS deployment target to 10.15
(Richard Lau) (https://github.com/nodejs/node/pull/42292)
- (SEMVER-MAJOR) build: downgrade Windows 8.1 and server 2012 R2 to
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42105)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- (SEMVER-MAJOR) cluster: make `kill` to be just `process.kill`
(Bar Admoni) (https://github.com/nodejs/node/pull/34312)
- (SEMVER-MAJOR) crypto: cleanup validation
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/39841)
- (SEMVER-MAJOR) crypto: prettify othername in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42123)
- (SEMVER-MAJOR) crypto: fix X509Certificate toLegacyObject
(Tobias Nießen) (https://github.com/nodejs/node/pull/42124)
- (SEMVER-MAJOR) crypto: use RFC2253 format in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42002)
- (SEMVER-MAJOR) crypto: change default check(Host|Email) behavior
(Tobias Nießen) (https://github.com/nodejs/node/pull/41600)
- (SEMVER-MAJOR) deps: V8: cherry-pick semver-major commits from 10.2
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 10.1.124.6
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 9.8.177.9
(Michaël Zasso) (https://github.com/nodejs/node/pull/41610)
- (SEMVER-MAJOR) deps: update V8 to 9.7.106.18
(Michaël Zasso) (https://github.com/nodejs/node/pull/40907)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) doc: update minimum glibc requirements for Linux
(Richard Lau) (https://github.com/nodejs/node/pull/42659)
- (SEMVER-MAJOR) doc: update AIX minimum supported arch
(Richard Lau) (https://github.com/nodejs/node/pull/42604)
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) http: refactor headersTimeout and requestTimeout logic
(Paolo Insogna) (https://github.com/nodejs/node/pull/41263)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) lib: enable fetch by default
(Michaël Zasso) (https://github.com/nodejs/node/pull/41811)
- (SEMVER-MAJOR) lib: replace validator and error
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/41678)
- (SEMVER-MAJOR) module,repl: support 'node:'-only core modules
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: disallow some uses of Object.defineProperty()
on process.env
(Himself65) (https://github.com/nodejs/node/pull/28006)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) readline: fix question still called after closed
(Xuguang Mei) (https://github.com/nodejs/node/pull/42464)
- (SEMVER-MAJOR) stream: remove thenable support
(Robert Nagy) (https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) stream: expose web streams globals, remove runtime
experimental warning
(Antoine du Hamel) (https://github.com/nodejs/node/pull/42225)
- (SEMVER-MAJOR) stream: need to cleanup event listeners if last stream
is readable
(Xuguang Mei) (https://github.com/nodejs/node/pull/41954)
- (SEMVER-MAJOR) stream: revert revert `map` spec compliance
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41933)
- (SEMVER-MAJOR) stream: throw invalid arg type from End Of Stream
(Jithil P Ponnan) (https://github.com/nodejs/node/pull/41766)
- (SEMVER-MAJOR) stream: don't emit finish after destroy
(Robert Nagy) (https://github.com/nodejs/node/pull/40852)
- (SEMVER-MAJOR) stream: add errored and closed props
(Robert Nagy) (https://github.com/nodejs/node/pull/40696)
- (SEMVER-MAJOR) test: add initial test module
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) timers: refactor internal classes to ES2015 syntax
(Rabbit) (https://github.com/nodejs/node/pull/37408)
- (SEMVER-MAJOR) tls: represent registeredID numerically always
(Tobias Nießen) (https://github.com/nodejs/node/pull/41561)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
- (SEMVER-MAJOR) url: throw on NULL in IPv6 hostname
(Rich Trott) (https://github.com/nodejs/node/pull/42313)
- (SEMVER-MAJOR) v8: make v8.writeHeapSnapshot() error codes consistent
(Darshan Sen) (https://github.com/nodejs/node/pull/42577)
- (SEMVER-MAJOR) v8: make writeHeapSnapshot throw if fopen fails
(Antonio Román) (https://github.com/nodejs/node/pull/41373)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
PR-URL: https://github.com/nodejs/node/pull/42262
2022-03-08 01:39:47 +00:00
v18.0.0
2021-11-02 12:01:48 +02:00
-->
* {Error}
Returns error if the stream has been destroyed with an error.
2019-12-24 15:09:29 -08:00
##### `writable.writableFinished`
2021-10-10 21:55:04 -07:00
2019-07-07 20:56:12 +03:00
<!-- YAML
added: v12.6.0
-->
* {boolean}
2019-07-23 07:21:49 +02:00
Is set to `true` immediately before the [`'finish'` ][] event is emitted.
2019-07-07 20:56:12 +03:00
2019-12-24 15:09:29 -08:00
##### `writable.writableHighWaterMark`
2021-10-10 21:55:04 -07:00
2017-05-05 15:57:57 +02:00
<!-- YAML
2017-12-12 03:09:37 -05:00
added: v9.3.0
2017-05-05 15:57:57 +02:00
-->
2018-04-11 21:07:14 +03:00
* {number}
2019-08-25 18:13:27 +02:00
Return the value of `highWaterMark` passed when creating this `Writable` .
2017-05-05 15:57:57 +02:00
2019-12-24 15:09:29 -08:00
##### `writable.writableLength`
2021-10-10 21:55:04 -07:00
2017-05-05 14:42:21 +02:00
<!-- YAML
2018-01-09 19:23:55 -05:00
added: v9.4.0
2017-05-05 14:42:21 +02:00
-->
2019-07-10 16:04:45 +02:00
* {number}
2017-05-05 14:42:21 +02:00
This property contains the number of bytes (or objects) in the queue
ready to be written. The value provides introspection data regarding
the status of the `highWaterMark` .
2020-09-25 18:40:01 +02:00
##### `writable.writableNeedDrain`
2021-10-10 21:55:04 -07:00
2020-09-25 18:40:01 +02:00
<!-- YAML
2021-05-11, Version 14.17.0 'Fermium' (LTS)
Notable Changes:
Diagnostics channel (experimental module):
`diagnostics_channel` is a new experimental module that provides an API
to create named channels to report arbitrary message data for
diagnostics purposes.
The module was initially introduced in Node.js v15.1.0 and is
backported to v14.17.0 to enable testing it at a larger scale.
With `diagnostics_channel`, Node.js core and module authors can publish
contextual data about what they are doing at a given time. This could
be the hostname and query string of a mysql query, for example. Just
create a named channel with `dc.channel(name)` and call
`channel.publish(data)` to send the data to any listeners to that
channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
MySQL.prototype.query = function query(queryString, values, callback) {
// Broadcast query information whenever a query is made
channel.publish({
query: queryString,
host: this.hostname,
});
this.doQuery(queryString, values, callback);
};
```
Channels are like one big global event emitter but are split into
separate objects to ensure they get the best performance. If nothing is
listening to the channel, the publishing overhead should be as close to
zero as possible. Consuming channel data is as easy as using
`channel.subscribe(listener)` to run a function whenever a message is
published to that channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
channel.subscribe(({ query, host }) => {
console.log(`mysql query to ${host}: ${query}`);
});
```
The data captured can be used to provide context for what an app is
doing at a given time. This can be used for things like augmenting
tracing data, tracking network and filesystem activity, logging
queries, and many other things. It's also a very useful data source
for diagnostics tools to provide a clearer picture of exactly what the
application is doing at a given point in the data they are presenting.
Contributed by Stephen Belanger (https://github.com/nodejs/node/pull/34895).
UUID support in the crypto module:
The new `crypto.randomUUID()` method now allows to generate random
[RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4
UUID strings:
```js
const { randomUUID } = require('crypto');
console.log(randomUUID());
// 'aa7c91a1-f8fc-4339-b9db-f93fc7233429'
```
Contributed by James M Snell (https://github.com/nodejs/node/pull/36729).
Experimental support for `AbortController` and `AbortSignal`:
Node.js 14.17.0 adds experimental partial support for `AbortController`
and `AbortSignal`.
Both constructors can be enabled globally using the
`--experimental-abortcontroller` flag.
Additionally, several Node.js APIs have been updated to support
`AbortSignal` for cancellation.
It is not mandatory to use the built-in constructors with them. Any
spec-compliant third-party alternatives should be compatible.
`AbortSignal` support was added to the following methods:
* `child_process.exec`
* `child_process.execFile`
* `child_process.fork`
* `child_process.spawn`
* `dgram.createSocket`
* `events.on`
* `events.once`
* `fs.readFile`
* `fs.watch`
* `fs.writeFile`
* `http.request`
* `https.request`
* `http2Session.request`
* The promisified variants of `setImmediate` and `setTimeout`
Other notable changes:
* doc:
* revoke deprecation of legacy url, change status to legacy (James M Snell) (https://github.com/nodejs/node/pull/37784)
* add legacy status to stability index (James M Snell) (https://github.com/nodejs/node/pull/37784)
* upgrade stability status of report API (Gireesh Punathil) (https://github.com/nodejs/node/pull/35654)
* deps:
* V8: Backport various patches for Apple Silicon support (BoHong Li) (https://github.com/nodejs/node/pull/38051)
* update ICU to 68.1 (Michaël Zasso) (https://github.com/nodejs/node/pull/36187)
* upgrade to libuv 1.41.0 (Colin Ihrig) (https://github.com/nodejs/node/pull/37360)
* http:
* add http.ClientRequest.getRawHeaderNames() (simov) (https://github.com/nodejs/node/pull/37660)
* report request start and end with diagnostics\_channel (Stephen Belanger) (https://github.com/nodejs/node/pull/34895)
* util:
* add getSystemErrorMap() impl (eladkeyshawn) (https://github.com/nodejs/node/pull/38101)
PR-URL: https://github.com/nodejs/node/pull/38507
2021-05-02 23:12:18 -04:00
added:
- v15.2.0
- v14.17.0
2020-09-25 18:40:01 +02:00
-->
* {boolean}
Is `true` if the stream's buffer has been full and stream will emit `'drain'` .
2019-12-24 15:09:29 -08:00
##### `writable.writableObjectMode`
2021-10-10 21:55:04 -07:00
2019-05-19 17:24:07 +05:30
<!-- YAML
2019-05-21 13:49:35 +02:00
added: v12.3.0
2019-05-19 17:24:07 +05:30
-->
2019-07-10 16:04:45 +02:00
* {boolean}
2019-05-19 17:24:07 +05:30
Getter for the property `objectMode` of a given `Writable` stream.
2024-06-16 02:41:59 +03:00
##### `writable[Symbol.asyncDispose]()`
<!-- YAML
2024-07-19 15:32:30 +02:00
added:
- v22.4.0
- v20.16.0
2024-06-16 02:41:59 +03:00
-->
> Stability: 1 - Experimental
Calls [`writable.destroy()` ][writable-destroy] with an `AbortError` and returns
a promise that fulfills when the stream is finished.
2019-12-24 15:09:29 -08:00
##### `writable.write(chunk[, encoding][, callback])`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
2017-02-21 23:38:48 +01:00
changes:
2024-05-02 11:31:36 +02:00
- version:
- v22.0.0
- v20.13.0
2024-03-20 18:27:29 +01:00
pr-url: https://github.com/nodejs/node/pull/51866
description: The `chunk` argument can now be a `TypedArray` or `DataView` instance.
2017-03-15 20:26:14 -07:00
- version: v8.0.0
2017-01-09 19:05:06 +01:00
pr-url: https://github.com/nodejs/node/pull/11608
description: The `chunk` argument can now be a `Uint8Array` instance.
2017-02-21 23:38:48 +01:00
- version: v6.0.0
pr-url: https://github.com/nodejs/node/pull/6170
description: Passing `null` as the `chunk` parameter will always be
considered invalid now, even in object mode.
2016-06-13 10:32:44 -04:00
-->
2016-05-23 22:30:41 -07:00
2024-03-20 18:27:29 +01:00
* `chunk` {string|Buffer|TypedArray|DataView|any} Optional data to write. For
streams not operating in object mode, `chunk` must be a {string}, {Buffer},
{TypedArray} or {DataView}. For object mode streams, `chunk` may be any
JavaScript value other than `null` .
2020-09-27 12:58:42 +05:30
* `encoding` {string|null} The encoding, if `chunk` is a string. **Default:** `'utf8'`
2020-06-28 17:44:07 +02:00
* `callback` {Function} Callback for when this chunk of data is flushed.
2017-03-05 18:03:39 +01:00
* Returns: {boolean} `false` if the stream wishes for the calling code to
2016-05-23 22:30:41 -07:00
wait for the `'drain'` event to be emitted before continuing to write
additional data; otherwise `true` .
The `writable.write()` method writes some data to the stream, and calls the
supplied `callback` once the data has been fully handled. If an error
2021-06-07 16:01:49 +02:00
occurs, the `callback` will be called with the error as its
first argument. The `callback` is called asynchronously and before `'error'` is
2020-02-15 14:13:29 +01:00
emitted.
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-17 18:24:02 -07:00
2017-01-03 01:44:49 +00:00
The return value is `true` if the internal buffer is less than the
2016-11-04 21:11:10 +05:30
`highWaterMark` configured when the stream was created after admitting `chunk` .
If `false` is returned, further attempts to write data to the stream should
2017-01-05 11:28:46 +01:00
stop until the [`'drain'` ][] event is emitted.
While a stream is not draining, calls to `write()` will buffer `chunk` , and
return false. Once all currently buffered chunks are drained (accepted for
delivery by the operating system), the `'drain'` event will be emitted.
2022-03-31 20:12:30 -07:00
Once `write()` returns false, do not write more chunks
2017-01-05 11:28:46 +01:00
until the `'drain'` event is emitted. While calling `write()` on a stream that
is not draining is allowed, Node.js will buffer all written chunks until
maximum memory usage occurs, at which point it will abort unconditionally.
Even before it aborts, high memory usage will cause poor garbage collector
performance and high RSS (which is not typically released back to the system,
even after the memory is no longer required). Since TCP sockets may never
drain if the remote peer does not read the data, writing a socket that is
not draining may lead to a remotely exploitable vulnerability.
Writing data while the stream is not draining is particularly
2018-04-29 20:46:41 +03:00
problematic for a [`Transform` ][], because the `Transform` streams are paused
2018-11-05 20:40:07 -08:00
by default until they are piped or a `'data'` or `'readable'` event handler
2017-01-05 11:28:46 +01:00
is added.
If the data to be written can be generated or fetched on demand, it is
2018-04-29 20:46:41 +03:00
recommended to encapsulate the logic into a [`Readable` ][] and use
2017-01-05 11:28:46 +01:00
[`stream.pipe()` ][]. However, if calling `write()` is preferred, it is
possible to respect backpressure and avoid memory issues using the
2017-03-06 11:32:45 +01:00
[`'drain'` ][] event:
2017-01-05 11:28:46 +01:00
```js
2017-04-21 17:38:31 +03:00
function write(data, cb) {
2017-01-05 11:28:46 +01:00
if (!stream.write(data)) {
2017-04-21 17:38:31 +03:00
stream.once('drain', cb);
2017-01-05 11:28:46 +01:00
} else {
2017-04-21 17:38:31 +03:00
process.nextTick(cb);
2017-01-05 11:28:46 +01:00
}
}
// Wait for cb to be called before doing any other write.
write('hello', () => {
2018-11-06 08:40:22 +10:00
console.log('Write completed, do more writes now.');
2017-04-21 17:38:31 +03:00
});
2017-01-05 11:28:46 +01:00
```
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
A `Writable` stream in object mode will always ignore the `encoding` argument.
2016-05-23 22:30:41 -07:00
2020-06-14 14:49:34 -07:00
### Readable streams
2016-05-23 22:30:41 -07:00
2021-10-10 21:55:04 -07:00
Readable streams are an abstraction for a _source_ from which data is
2016-05-23 22:30:41 -07:00
consumed.
2018-04-29 20:46:41 +03:00
Examples of `Readable` streams include:
2013-07-15 16:56:02 -07:00
2016-02-02 20:34:29 +03:00
* [HTTP responses, on the client][http-incoming-message]
* [HTTP requests, on the server][http-incoming-message]
2015-11-13 19:21:49 -08:00
* [fs read streams][]
2016-02-02 20:34:29 +03:00
* [zlib streams][zlib]
* [crypto streams][crypto]
* [TCP sockets][]
2013-07-15 16:56:02 -07:00
* [child process stdout and stderr][]
2015-11-27 18:30:32 -05:00
* [`process.stdin` ][]
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
All [`Readable` ][] streams implement the interface defined by the
2016-05-23 22:30:41 -07:00
`stream.Readable` class.
2020-06-14 14:49:34 -07:00
#### Two reading modes
2016-05-23 22:30:41 -07:00
2018-08-31 14:06:57 -04:00
`Readable` streams effectively operate in one of two modes: flowing and
paused. These modes are separate from [object mode][object-mode].
A [`Readable` ][] stream can be in object mode or not, regardless of whether
it is in flowing mode or paused mode.
2016-05-23 22:30:41 -07:00
2018-08-31 14:06:57 -04:00
* In flowing mode, data is read from the underlying system automatically
2020-11-09 05:44:32 -08:00
and provided to an application as quickly as possible using events via the
[`EventEmitter` ][] interface.
2016-05-23 22:30:41 -07:00
2018-08-31 14:06:57 -04:00
* In paused mode, the [`stream.read()` ][stream-read] method must be called
2020-11-09 05:44:32 -08:00
explicitly to read chunks of data from the stream.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
All [`Readable` ][] streams begin in paused mode but can be switched to flowing
2016-05-23 22:30:41 -07:00
mode in one of the following ways:
2015-06-23 20:42:49 -07:00
2016-05-23 22:30:41 -07:00
* Adding a [`'data'` ][] event handler.
* Calling the [`stream.resume()` ][stream-resume] method.
2018-04-29 20:46:41 +03:00
* Calling the [`stream.pipe()` ][] method to send the data to a [`Writable` ][].
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
The `Readable` can switch back to paused mode using one of the following:
2016-05-23 22:30:41 -07:00
* If there are no pipe destinations, by calling the
[`stream.pause()` ][stream-pause] method.
2018-01-25 23:45:17 +08:00
* If there are pipe destinations, by removing all pipe destinations.
Multiple pipe destinations may be removed by calling the
2016-05-23 22:30:41 -07:00
[`stream.unpipe()` ][] method.
2018-04-29 20:46:41 +03:00
The important concept to remember is that a `Readable` will not generate data
2016-05-23 22:30:41 -07:00
until a mechanism for either consuming or ignoring that data is provided. If
2021-10-10 21:55:04 -07:00
the consuming mechanism is disabled or taken away, the `Readable` will _attempt_
2016-05-23 22:30:41 -07:00
to stop generating the data.
2018-11-05 20:40:07 -08:00
For backward compatibility reasons, removing [`'data'` ][] event handlers will
2018-02-05 21:55:16 -08:00
**not** automatically pause the stream. Also, if there are piped destinations,
then calling [`stream.pause()` ][stream-pause] will not guarantee that the
2021-10-10 21:55:04 -07:00
stream will _remain_ paused once those destinations drain and ask for more data.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
If a [`Readable` ][] is switched into flowing mode and there are no consumers
2018-02-05 21:55:16 -08:00
available to handle the data, that data will be lost. This can occur, for
instance, when the `readable.resume()` method is called without a listener
2016-05-23 22:30:41 -07:00
attached to the `'data'` event, or when a `'data'` event handler is removed
from the stream.
2020-06-29 15:46:04 +02:00
Adding a [`'readable'` ][] event handler automatically makes the stream
stop flowing, and the data has to be consumed via
2019-10-02 00:31:57 -04:00
[`readable.read()` ][stream-read]. If the [`'readable'` ][] event handler is
2018-08-09 14:02:33 +02:00
removed, then the stream will start flowing again if there is a
[`'data'` ][] event handler.
2020-06-14 14:49:34 -07:00
#### Three states
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
The "two modes" of operation for a `Readable` stream are a simplified
abstraction for the more complicated internal state management that is happening
within the `Readable` stream implementation.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
Specifically, at any given point in time, every `Readable` is in one of three
2016-05-23 22:30:41 -07:00
possible states:
2018-08-31 14:06:57 -04:00
* `readable.readableFlowing === null`
* `readable.readableFlowing === false`
* `readable.readableFlowing === true`
2016-05-23 22:30:41 -07:00
2017-05-16 11:08:49 -04:00
When `readable.readableFlowing` is `null` , no mechanism for consuming the
2018-08-31 14:06:57 -04:00
stream's data is provided. Therefore, the stream will not generate data.
While in this state, attaching a listener for the `'data'` event, calling the
2018-02-12 02:31:55 -05:00
`readable.pipe()` method, or calling the `readable.resume()` method will switch
2018-08-31 14:06:57 -04:00
`readable.readableFlowing` to `true` , causing the `Readable` to begin actively
emitting events as data is generated.
2015-06-23 20:42:49 -07:00
2018-07-03 10:56:29 +02:00
Calling `readable.pause()` , `readable.unpipe()` , or receiving backpressure
2017-05-16 11:08:49 -04:00
will cause the `readable.readableFlowing` to be set as `false` ,
2021-10-10 21:55:04 -07:00
temporarily halting the flowing of events but _not_ halting the generation of
2017-05-31 08:51:50 +02:00
data. While in this state, attaching a listener for the `'data'` event
2018-08-31 14:06:57 -04:00
will not switch `readable.readableFlowing` to `true` .
2017-05-31 08:51:50 +02:00
```js
2022-04-20 10:23:41 +02:00
const { PassThrough, Writable } = require('node:stream');
2017-05-31 08:51:50 +02:00
const pass = new PassThrough();
const writable = new Writable();
pass.pipe(writable);
pass.unpipe(writable);
2019-07-07 20:56:12 +03:00
// readableFlowing is now false.
2017-05-31 08:51:50 +02:00
pass.on('data', (chunk) => { console.log(chunk.toString()); });
2022-11-23 22:34:46 +09:00
// readableFlowing is still false.
2019-07-07 20:56:12 +03:00
pass.write('ok'); // Will not emit 'data'.
pass.resume(); // Must be called to make stream emit 'data'.
2022-11-23 22:34:46 +09:00
// readableFlowing is now true.
2017-05-31 08:51:50 +02:00
```
2012-12-13 11:15:49 -08:00
2017-05-16 11:08:49 -04:00
While `readable.readableFlowing` is `false` , data may be accumulating
2018-08-31 14:06:57 -04:00
within the stream's internal buffer.
2013-07-15 16:56:02 -07:00
2020-06-14 14:49:34 -07:00
#### Choose one API style
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
The `Readable` stream API evolved across multiple Node.js versions and provides
2016-05-23 22:30:41 -07:00
multiple methods of consuming stream data. In general, developers should choose
2021-10-10 21:55:04 -07:00
_one_ of the methods of consuming data and _should never_ use multiple methods
2018-08-09 14:02:33 +02:00
to consume data from a single stream. Specifically, using a combination
2018-08-31 14:06:57 -04:00
of `on('data')` , `on('readable')` , `pipe()` , or async iterators could
2018-08-09 14:02:33 +02:00
lead to unintuitive behavior.
2016-05-23 22:30:41 -07:00
2019-12-24 15:09:29 -08:00
#### Class: `stream.Readable`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2016-05-23 22:30:41 -07:00
<!-- type=class -->
2019-12-24 15:09:29 -08:00
##### Event: `'close'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
2019-01-09 13:07:59 +01:00
changes:
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18438
description: Add `emitClose` option to specify if `'close'` is emitted on
destroy.
2016-06-13 10:32:44 -04:00
-->
2016-05-23 22:30:41 -07:00
The `'close'` event is emitted when the stream and any of its underlying
resources (a file descriptor, for example) have been closed. The event indicates
that no more events will be emitted, and no further computation will occur.
2019-01-09 13:07:59 +01:00
A [`Readable` ][] stream will always emit the `'close'` event if it is
created with the `emitClose` option.
2016-05-23 22:30:41 -07:00
2019-12-24 15:09:29 -08:00
##### Event: `'data'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2016-05-23 22:30:41 -07:00
2017-02-04 16:15:33 +01:00
* `chunk` {Buffer|string|any} The chunk of data. For streams that are not
2016-05-23 22:30:41 -07:00
operating in object mode, the chunk will be either a string or `Buffer` .
For streams that are in object mode, the chunk can be any JavaScript value
other than `null` .
The `'data'` event is emitted whenever the stream is relinquishing ownership of
a chunk of data to a consumer. This may occur whenever the stream is switched
in flowing mode by calling `readable.pipe()` , `readable.resume()` , or by
attaching a listener callback to the `'data'` event. The `'data'` event will
also be emitted whenever the `readable.read()` method is called and a chunk of
data is available to be returned.
Attaching a `'data'` event listener to a stream that has not been explicitly
paused will switch the stream into flowing mode. Data will then be passed as
soon as it is available.
The listener callback will be passed the chunk of data as a string if a default
encoding has been specified for the stream using the
`readable.setEncoding()` method; otherwise the data will be passed as a
`Buffer` .
2013-07-15 16:56:02 -07:00
2016-01-17 18:39:07 +01:00
```js
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
2015-12-14 15:20:25 -08:00
readable.on('data', (chunk) => {
2016-05-23 22:30:41 -07:00
console.log(`Received ${chunk.length} bytes of data.` );
2014-10-03 15:53:15 +10:00
});
2012-12-13 11:15:49 -08:00
```
2019-12-24 15:09:29 -08:00
##### Event: `'end'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2012-12-13 11:15:49 -08:00
2016-05-23 22:30:41 -07:00
The `'end'` event is emitted when there is no more data to be consumed from
the stream.
2010-10-28 23:18:16 +11:00
2018-02-05 21:55:16 -08:00
The `'end'` event **will not be emitted** unless the data is completely
consumed. This can be accomplished by switching the stream into flowing mode,
or by calling [`stream.read()` ][stream-read] repeatedly until all data has been
consumed.
2012-02-27 11:09:34 -08:00
2016-01-17 18:39:07 +01:00
```js
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
2015-12-14 15:20:25 -08:00
readable.on('data', (chunk) => {
2016-05-23 22:30:41 -07:00
console.log(`Received ${chunk.length} bytes of data.` );
2014-10-03 15:53:15 +10:00
});
2015-12-14 15:20:25 -08:00
readable.on('end', () => {
2016-05-23 22:30:41 -07:00
console.log('There will be no more data.');
2013-07-15 16:56:02 -07:00
});
```
2010-10-28 23:18:16 +11:00
2019-12-24 15:09:29 -08:00
##### Event: `'error'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2010-10-28 23:18:16 +11:00
2016-04-01 22:41:14 -07:00
* {Error}
2014-08-07 20:56:34 +02:00
2018-04-29 20:46:41 +03:00
The `'error'` event may be emitted by a `Readable` implementation at any time.
2017-05-09 13:48:45 -03:00
Typically, this may occur if the underlying stream is unable to generate data
2016-05-23 22:30:41 -07:00
due to an underlying internal failure, or when a stream implementation attempts
to push an invalid chunk of data.
2012-02-05 19:11:54 +09:00
2016-05-23 22:30:41 -07:00
The listener callback will be passed a single `Error` object.
2010-10-28 23:18:16 +11:00
2019-12-24 15:09:29 -08:00
##### Event: `'pause'`
2021-10-10 21:55:04 -07:00
2019-03-29 21:42:02 +01:00
<!-- YAML
added: v0.9.4
-->
The `'pause'` event is emitted when [`stream.pause()` ][stream-pause] is called
and `readableFlowing` is not `false` .
2019-12-24 15:09:29 -08:00
##### Event: `'readable'`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
2018-01-04 18:06:56 +01:00
changes:
2018-03-02 09:53:46 -08:00
- version: v10.0.0
2018-01-04 18:06:56 +01:00
pr-url: https://github.com/nodejs/node/pull/17979
2019-01-09 09:32:08 -08:00
description: The `'readable'` is always emitted in the next tick after
`.push()` is called.
2018-03-02 09:53:46 -08:00
- version: v10.0.0
2018-02-26 09:24:30 +01:00
pr-url: https://github.com/nodejs/node/pull/18994
2018-04-09 19:30:22 +03:00
description: Using `'readable'` requires calling `.read()` .
2016-06-13 10:32:44 -04:00
-->
2012-12-13 11:15:49 -08:00
2016-05-23 22:30:41 -07:00
The `'readable'` event is emitted when there is data available to be read from
2024-06-08 11:52:46 -04:00
the stream, up to the configured high water mark (`state.highWaterMark` ). Effectively,
it indicates that the stream has new information within the buffer. If data is available
within this buffer, [`stream.read()` ][stream-read] can be called to retrieve that data.
Additionally, the `'readable'` event may also be emitted when the end of the stream has been
reached.
2013-02-28 15:42:55 -08:00
2020-05-23 19:34:40 -04:00
```js
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
2018-02-26 09:24:30 +01:00
readable.on('readable', function() {
2019-07-07 20:56:12 +03:00
// There is some data to read now.
2018-02-26 09:24:30 +01:00
let data;
2022-01-17 08:54:01 -08:00
while ((data = this.read()) !== null) {
2018-02-26 09:24:30 +01:00
console.log(data);
}
2013-07-15 16:56:02 -07:00
});
```
2018-02-26 09:24:30 +01:00
2021-09-25 10:07:15 +02:00
If the end of the stream has been reached, calling
[`stream.read()` ][stream-read] will return `null` and trigger the `'end'`
event. This is also true if there never was any data to be read. For instance,
in the following example, `foo.txt` is an empty file:
2013-01-07 18:07:17 -08:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
2016-05-23 22:30:41 -07:00
const rr = fs.createReadStream('foo.txt');
2015-12-14 15:20:25 -08:00
rr.on('readable', () => {
2017-11-26 17:54:26 +09:00
console.log(`readable: ${rr.read()}` );
2015-11-05 14:54:10 -05:00
});
2015-12-14 15:20:25 -08:00
rr.on('end', () => {
2015-11-05 14:54:10 -05:00
console.log('end');
2014-10-03 15:53:15 +10:00
});
2013-07-15 16:56:02 -07:00
```
2015-11-05 14:54:10 -05:00
The output of running this script is:
2013-07-15 16:56:02 -07:00
2019-09-01 11:07:24 +08:00
```console
2016-01-17 18:39:07 +01:00
$ node test.js
2015-11-05 14:54:10 -05:00
readable: null
end
```
2013-08-27 18:59:58 -07:00
2021-09-25 10:07:15 +02:00
In some cases, attaching a listener for the `'readable'` event will cause some
amount of data to be read into an internal buffer.
2018-02-05 21:55:16 -08:00
In general, the `readable.pipe()` and `'data'` event mechanisms are easier to
understand than the `'readable'` event. However, handling `'readable'` might
result in increased throughput.
2016-05-23 22:30:41 -07:00
2018-08-31 14:06:57 -04:00
If both `'readable'` and [`'data'` ][] are used at the same time, `'readable'`
2018-02-26 09:24:30 +01:00
takes precedence in controlling the flow, i.e. `'data'` will be emitted
2018-08-09 14:02:33 +02:00
only when [`stream.read()` ][stream-read] is called. The
`readableFlowing` property would become `false` .
If there are `'data'` listeners when `'readable'` is removed, the stream
will start flowing, i.e. `'data'` events will be emitted without calling
`.resume()` .
2018-02-26 09:24:30 +01:00
2019-12-24 15:09:29 -08:00
##### Event: `'resume'`
2021-10-10 21:55:04 -07:00
2019-03-29 21:42:02 +01:00
<!-- YAML
added: v0.9.4
-->
The `'resume'` event is emitted when [`stream.resume()` ][stream-resume] is
called and `readableFlowing` is not `true` .
2019-12-24 15:09:29 -08:00
##### `readable.destroy([error])`
2021-10-10 21:55:04 -07:00
2018-03-21 04:12:32 +02:00
<!-- YAML
added: v8.0.0
2020-09-24 12:27:06 +02:00
changes:
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/29197
2020-09-27 07:50:41 -07:00
description: Work as a no-op on a stream that has already been destroyed.
2018-03-21 04:12:32 +02:00
-->
* `error` {Error} Error which will be passed as payload in `'error'` event
* Returns: {this}
2019-03-11 19:06:12 +01:00
Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`
2019-08-05 12:01:33 +02:00
event (unless `emitClose` is set to `false` ). After this call, the readable
2019-03-11 19:06:12 +01:00
stream will release any internal resources and subsequent calls to `push()`
will be ignored.
2019-08-18 23:38:35 +02:00
2020-09-27 07:50:41 -07:00
Once `destroy()` has been called any further calls will be a no-op and no
further errors except from `_destroy()` may be emitted as `'error'` .
2019-08-18 23:38:35 +02:00
2018-03-21 04:12:32 +02:00
Implementors should not override this method, but instead implement
2018-04-09 19:30:22 +03:00
[`readable._destroy()` ][readable-_destroy].
2018-03-21 04:12:32 +02:00
2021-11-02 12:01:48 +02:00
##### `readable.closed`
2021-10-10 21:55:04 -07:00
2019-07-23 09:45:20 +02:00
<!-- YAML
2022-07-28 21:02:11 -04:00
added: v18.0.0
2019-07-23 09:45:20 +02:00
-->
* {boolean}
2021-11-02 12:01:48 +02:00
Is `true` after `'close'` has been emitted.
##### `readable.destroyed`
<!-- YAML
2022-07-28 21:02:11 -04:00
added: v8.0.0
2021-11-02 12:01:48 +02:00
-->
* {boolean}
2019-07-23 09:45:20 +02:00
Is `true` after [`readable.destroy()` ][readable-destroy] has been called.
2019-12-24 15:09:29 -08:00
##### `readable.isPaused()`
2021-10-10 21:55:04 -07:00
2017-03-03 12:44:28 -05:00
<!-- YAML
2016-06-13 10:32:44 -04:00
added: v0.11.14
-->
2013-07-15 16:56:02 -07:00
2017-03-05 18:03:39 +01:00
* Returns: {boolean}
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
The `readable.isPaused()` method returns the current operating state of the
2018-04-29 20:46:41 +03:00
`Readable` . This is used primarily by the mechanism that underlies the
2016-05-23 22:30:41 -07:00
`readable.pipe()` method. In most typical cases, there will be no reason to
use this method directly.
2013-01-07 18:07:17 -08:00
2016-01-17 18:39:07 +01:00
```js
2017-04-21 17:38:31 +03:00
const readable = new stream.Readable();
2015-11-05 14:54:10 -05:00
2017-04-21 17:38:31 +03:00
readable.isPaused(); // === false
readable.pause();
readable.isPaused(); // === true
readable.resume();
readable.isPaused(); // === false
2013-07-15 16:56:02 -07:00
```
2019-12-24 15:09:29 -08:00
##### `readable.pause()`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2013-07-15 16:56:02 -07:00
2018-01-30 00:15:53 +02:00
* Returns: {this}
2013-08-27 18:59:58 -07:00
2016-05-23 22:30:41 -07:00
The `readable.pause()` method will cause a stream in flowing mode to stop
emitting [`'data'` ][] events, switching out of flowing mode. Any data that
becomes available will remain in the internal buffer.
2013-01-07 18:07:17 -08:00
2016-01-17 18:39:07 +01:00
```js
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
2015-12-14 15:20:25 -08:00
readable.on('data', (chunk) => {
2016-05-23 22:30:41 -07:00
console.log(`Received ${chunk.length} bytes of data.` );
2013-07-15 16:56:02 -07:00
readable.pause();
2016-05-23 22:30:41 -07:00
console.log('There will be no additional data for 1 second.');
2015-12-14 15:20:25 -08:00
setTimeout(() => {
2016-05-23 22:30:41 -07:00
console.log('Now data will start flowing again.');
2013-07-15 16:56:02 -07:00
readable.resume();
}, 1000);
2014-10-03 15:53:15 +10:00
});
2013-07-15 16:56:02 -07:00
```
2013-01-07 18:07:17 -08:00
2018-08-09 14:02:33 +02:00
The `readable.pause()` method has no effect if there is a `'readable'`
event listener.
2019-12-24 15:09:29 -08:00
##### `readable.pipe(destination[, options])`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2013-01-07 18:07:17 -08:00
2016-01-19 13:03:15 -03:00
* `destination` {stream.Writable} The destination for writing data
2013-07-15 16:56:02 -07:00
* `options` {Object} Pipe options
2018-04-02 04:44:32 +03:00
* `end` {boolean} End the writer when the reader ends. **Default:** `true` .
2021-10-10 21:55:04 -07:00
* Returns: {stream.Writable} The _destination_ , allowing for a chain of pipes if
2018-08-15 23:03:15 -07:00
it is a [`Duplex` ][] or a [`Transform` ][] stream
2013-01-07 18:07:17 -08:00
2018-04-29 20:46:41 +03:00
The `readable.pipe()` method attaches a [`Writable` ][] stream to the `readable` ,
2016-05-23 22:30:41 -07:00
causing it to switch automatically into flowing mode and push all of its data
2018-04-29 20:46:41 +03:00
to the attached [`Writable` ][]. The flow of data will be automatically managed
so that the destination `Writable` stream is not overwhelmed by a faster
`Readable` stream.
2013-01-07 18:07:17 -08:00
2016-05-23 22:30:41 -07:00
The following example pipes all of the data from the `readable` into a file
named `file.txt` :
2013-07-15 16:56:02 -07:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
const writable = fs.createWriteStream('file.txt');
2019-07-07 20:56:12 +03:00
// All the data from readable goes into 'file.txt'.
2013-07-15 16:56:02 -07:00
readable.pipe(writable);
2013-01-07 18:07:17 -08:00
```
2019-08-29 09:28:03 -04:00
2018-04-29 20:46:41 +03:00
It is possible to attach multiple `Writable` streams to a single `Readable`
stream.
2012-12-13 11:15:49 -08:00
2021-10-10 21:55:04 -07:00
The `readable.pipe()` method returns a reference to the _destination_ stream
2016-05-23 22:30:41 -07:00
making it possible to set up chains of piped streams:
2013-02-28 15:42:55 -08:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
2022-06-06 03:56:21 +08:00
const zlib = require('node:zlib');
2016-05-23 22:30:41 -07:00
const r = fs.createReadStream('file.txt');
const z = zlib.createGzip();
const w = fs.createWriteStream('file.txt.gz');
2013-07-15 16:56:02 -07:00
r.pipe(z).pipe(w);
```
2018-04-29 20:46:41 +03:00
By default, [`stream.end()` ][stream-end] is called on the destination `Writable`
stream when the source `Readable` stream emits [`'end'` ][], so that the
2016-05-23 22:30:41 -07:00
destination is no longer writable. To disable this default behavior, the `end`
2018-08-26 19:02:27 +03:00
option can be passed as `false` , causing the destination stream to remain open:
2013-07-15 16:56:02 -07:00
2016-01-17 18:39:07 +01:00
```js
2013-07-15 16:56:02 -07:00
reader.pipe(writer, { end: false });
2015-12-14 15:20:25 -08:00
reader.on('end', () => {
2013-07-15 16:56:02 -07:00
writer.end('Goodbye\n');
});
```
2018-04-29 20:46:41 +03:00
One important caveat is that if the `Readable` stream emits an error during
2021-10-10 21:55:04 -07:00
processing, the `Writable` destination _is not closed_ automatically. If an
error occurs, it will be necessary to _manually_ close each stream in order
2016-05-23 22:30:41 -07:00
to prevent memory leaks.
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
The [`process.stderr` ][] and [`process.stdout` ][] `Writable` streams are never
2018-02-05 21:55:16 -08:00
closed until the Node.js process exits, regardless of the specified options.
2016-05-23 22:30:41 -07:00
2019-12-24 15:09:29 -08:00
##### `readable.read([size])`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2013-07-15 16:56:02 -07:00
2017-02-04 16:15:33 +01:00
* `size` {number} Optional argument to specify how much data to read.
2018-06-07 23:45:35 +03:00
* Returns: {string|Buffer|null|any}
2013-07-15 16:56:02 -07:00
2022-01-14 09:42:38 -08:00
The `readable.read()` method reads data out of the internal buffer and
returns it. If no data is available to be read, `null` is returned. By default,
the data is returned as a `Buffer` object unless an encoding has been
2016-05-23 22:30:41 -07:00
specified using the `readable.setEncoding()` method or the stream is operating
in object mode.
2013-02-28 15:42:55 -08:00
2016-05-23 22:30:41 -07:00
The optional `size` argument specifies a specific number of bytes to read. If
2021-10-10 21:55:04 -07:00
`size` bytes are not available to be read, `null` will be returned _unless_
2016-05-23 22:30:41 -07:00
the stream has ended, in which case all of the data remaining in the internal
2017-08-24 16:45:19 +02:00
buffer will be returned.
stream: Fix unshift() race conditions
Fix #5272
The consumption of a readable stream is a dance with 3 partners.
1. The specific stream Author (A)
2. The Stream Base class (B), and
3. The Consumer of the stream (C)
When B calls the _read() method that A implements, it sets a 'reading'
flag, so that parallel calls to _read() can be avoided. When A calls
stream.push(), B knows that it's safe to start calling _read() again.
If the consumer C is some kind of parser that wants in some cases to
pass the source stream off to some other party, but not before "putting
back" some bit of previously consumed data (as in the case of Node's
websocket http upgrade implementation). So, stream.unshift() will
generally *never* be called by A, but *only* called by C.
Prior to this patch, stream.unshift() *also* unset the state.reading
flag, meaning that C could indicate the end of a read, and B would
dutifully fire off another _read() call to A. This is inappropriate.
In the case of fs streams, and other variably-laggy streams that don't
tolerate overlapped _read() calls, this causes big problems.
Also, calling stream.shift() after the 'end' event did not raise any
kind of error, but would cause very strange behavior indeed. Calling it
after the EOF chunk was seen, but before the 'end' event was fired would
also cause weird behavior, and could lead to data being lost, since it
would not emit another 'readable' event.
This change makes it so that:
1. stream.unshift() does *not* set state.reading = false
2. stream.unshift() is allowed up until the 'end' event.
3. unshifting onto a EOF-encountered and zero-length (but not yet
end-emitted) stream will defer the 'end' event until the new data is
consumed.
4. pushing onto a EOF-encountered stream is now an error.
So, if you read(), you have that single tick to safely unshift() data
back into the stream, even if the null chunk was pushed, and the length
was 0.
2013-04-11 15:01:26 -07:00
2016-05-23 22:30:41 -07:00
If the `size` argument is not specified, all of the data contained in the
internal buffer will be returned.
2015-11-05 14:54:10 -05:00
2020-09-04 11:53:16 +02:00
The `size` argument must be less than or equal to 1 GiB.
2019-10-20 10:13:57 +02:00
2018-04-29 20:46:41 +03:00
The `readable.read()` method should only be called on `Readable` streams
operating in paused mode. In flowing mode, `readable.read()` is called
automatically until the internal buffer is fully drained.
2013-07-15 16:56:02 -07:00
2016-01-17 18:39:07 +01:00
```js
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
2019-04-22 20:37:48 +01:00
// 'readable' may be triggered multiple times as data is buffered in
2015-12-14 15:20:25 -08:00
readable.on('readable', () => {
2017-04-22 15:22:40 +03:00
let chunk;
2019-04-22 20:37:48 +01:00
console.log('Stream is readable (new data received in buffer)');
// Use a loop to make sure we read all currently available data
2015-11-05 14:54:10 -05:00
while (null !== (chunk = readable.read())) {
2019-04-22 20:37:48 +01:00
console.log(`Read ${chunk.length} bytes of data...` );
2015-11-05 14:54:10 -05:00
}
});
2019-04-22 20:37:48 +01:00
// 'end' will be triggered once when there is no more data available
readable.on('end', () => {
console.log('Reached end of stream.');
});
2013-07-15 16:56:02 -07:00
```
2024-06-08 11:52:46 -04:00
Each call to `readable.read()` returns a chunk of data or `null` , signifying
that there's no more data to read at that moment. These chunks aren't automatically
concatenated. Because a single `read()` call does not return all the data, using
a while loop may be necessary to continuously read chunks until all data is retrieved.
When reading a large file, `.read()` might return `null` temporarily, indicating
that it has consumed all buffered content but there may be more data yet to be
buffered. In such cases, a new `'readable'` event is emitted once there's more
data in the buffer, and the `'end'` event signifies the end of data transmission.
2019-04-22 20:37:48 +01:00
Therefore to read a file's whole contents from a `readable` , it is necessary
to collect chunks across multiple `'readable'` events:
```js
const chunks = [];
readable.on('readable', () => {
let chunk;
while (null !== (chunk = readable.read())) {
chunks.push(chunk);
}
});
readable.on('end', () => {
const content = chunks.join('');
});
```
2019-01-07 15:37:34 +01:00
2018-04-29 20:46:41 +03:00
A `Readable` stream in object mode will always return a single item from
2016-05-23 22:30:41 -07:00
a call to [`readable.read(size)` ][stream-read], regardless of the value of the
`size` argument.
2018-02-05 21:55:16 -08:00
If the `readable.read()` method returns a chunk of data, a `'data'` event will
also be emitted.
2013-07-15 16:56:02 -07:00
2018-02-05 21:55:16 -08:00
Calling [`stream.read([size])` ][stream-read] after the [`'end'` ][] event has
been emitted will return `null` . No runtime error will be raised.
2013-02-28 15:42:55 -08:00
2019-12-24 15:09:29 -08:00
##### `readable.readable`
2021-10-10 21:55:04 -07:00
2018-10-28 11:55:22 +08:00
<!-- YAML
2018-12-05 21:29:36 +01:00
added: v11.4.0
2018-10-28 11:55:22 +08:00
-->
* {boolean}
2020-01-05 18:41:31 +01:00
Is `true` if it is safe to call [`readable.read()` ][stream-read], which means
the stream has not been destroyed or emitted `'error'` or `'end'` .
2018-10-28 11:55:22 +08:00
2021-08-02 13:08:32 +02:00
##### `readable.readableAborted`
2021-10-10 21:55:04 -07:00
2021-08-02 13:08:32 +02:00
<!-- YAML
2021-08-25 09:01:17 +02:00
added: v16.8.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-08-02 13:08:32 +02:00
-->
* {boolean}
Returns whether the stream was destroyed or errored before emitting `'end'` .
2021-07-30 14:18:38 +02:00
##### `readable.readableDidRead`
2021-10-10 21:55:04 -07:00
2021-07-30 14:18:38 +02:00
<!-- YAML
2021-09-04 15:29:35 +02:00
added:
- v16.7.0
- v14.18.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-07-30 14:18:38 +02:00
-->
* {boolean}
2021-08-02 13:08:32 +02:00
Returns whether `'data'` has been emitted.
2021-07-30 14:18:38 +02:00
2019-12-24 15:09:29 -08:00
##### `readable.readableEncoding`
2021-10-10 21:55:04 -07:00
2019-07-05 15:54:34 +08:00
<!-- YAML
2019-07-23 10:29:14 +02:00
added: v12.7.0
2019-07-05 15:54:34 +08:00
-->
* {null|string}
Getter for the property `encoding` of a given `Readable` stream. The `encoding`
property can be set using the [`readable.setEncoding()` ][] method.
2019-12-24 15:09:29 -08:00
##### `readable.readableEnded`
2021-10-10 21:55:04 -07:00
2019-07-23 09:40:13 +02:00
<!-- YAML
2019-08-19 21:14:22 +02:00
added: v12.9.0
2019-07-23 09:40:13 +02:00
-->
* {boolean}
Becomes `true` when [`'end'` ][] event is emitted.
2021-11-13 14:42:30 +02:00
##### `readable.errored`
2021-11-02 12:01:48 +02:00
<!-- YAML
added:
2022-04-19, Version 18.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) stream: remove thenable support (Robert Nagy)
(https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
fetch (experimental):
An experimental fetch API is available on the global scope by default.
The implementation is based upon https://undici.nodejs.org/#/,
an HTTP/1.1 client written for Node.js by contributors to the project.
Through this addition, the following globals are made available: `fetch`
, `FormData`, `Headers`, `Request`, `Response`.
Disable this API with the `--no-experimental-fetch` command-line flag.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/41811.
HTTP Timeouts:
`server.headersTimeout`, which limits the amount of time the parser will
wait to receive the complete HTTP headers, is now set to `60000` (60
seconds) by default.
`server.requestTimeout`, which sets the timeout value in milliseconds
for receiving the entire request from the client, is now set to `300000`
(5 minutes) by default.
If these timeouts expire, the server responds with status 408 without
forwarding the request to the request listener and then closes the
connection.
Both timeouts must be set to a non-zero value to protect against
potential Denial-of-Service attacks in case the server is deployed
without a reverse proxy in front.
Contributed by Paolo Insogna in https://github.com/nodejs/node/pull/41263.
Test Runner module (experimental):
The `node:test` module facilitates the creation of JavaScript tests that
report results in TAP format. This module is only available under the
`node:` scheme.
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/42325.
Toolchain and Compiler Upgrades:
- Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux
(RHEL) 8 and are compatible with Linux distributions based on glibc
2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
- Prebuilt binaries for macOS now require macOS 10.15 or later.
- For AIX the minimum supported architecture has been raised from Power
7 to Power 8.
Prebuilt binaries for 32-bit Windows will initially not be available due
to issues building the V8 dependency in Node.js. We hope to restore
32-bit Windows binaries for Node.js 18 with a future V8 update.
Node.js does not support running on operating systems that are no longer
supported by their vendor. For operating systems where their vendor has
planned to end support earlier than April 2025, such as Windows 8.1
(January 2023) and Windows Server 2012 R2 (October 2023), support for
Node.js 18 will end at the earlier date.
Full details about the supported toolchains and compilers are documented
in the Node.js `BUILDING.md` file.
Contributed by Richard Lau in https://github.com/nodejs/node/pull/42292,
https://github.com/nodejs/node/pull/42604 and https://github.com/nodejs/node/pull/42659
, and Michaël Zasso in https://github.com/nodejs/node/pull/42105 and
https://github.com/nodejs/node/pull/42666.
V8 10.1:
The V8 engine is updated to version 10.1, which is part of Chromium 101.
Compared to the version included in Node.js 17.9.0, the following new
features are included:
- The `findLast` and `findLastIndex` array methods.
- Improvements to the `Intl.Locale` API.
- The `Intl.supportedValuesOf` function.
- Improved performance of class fields and private class methods (the
initialization of them is now as fast as ordinary property stores).
The data format returned by the serialization API (`v8.serialize(value)`)
has changed, and cannot be deserialized by earlier versions of Node.js.
On the other hand, it is still possible to deserialize the previous
format, as the API is backwards-compatible.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/42657.
Web Streams API (experimental):
Node.js now exposes the experimental implementation of the Web Streams
API on the global scope. This means the following APIs are now globally
available:
- `ReadableStream`, `ReadableStreamDefaultReader`,
`ReadableStreamBYOBReader`, `ReadableStreamBYOBRequest`,
`ReadableByteStreamController`, `ReadableStreamDefaultController`,
`TransformStream`, `TransformStreamDefaultController`, `WritableStream`,
`WritableStreamDefaultWriter`, `WritableStreamDefaultController`,
`ByteLengthQueuingStrategy`, `CountQueuingStrategy`, `TextEncoderStream`,
`TextDecoderStream`, `CompressionStream`, `DecompressionStream`.
Contributed James Snell in https://github.com/nodejs/node/pull/39062,
and Antoine du Hamel in https://github.com/nodejs/node/pull/42225.
Other Notable Changes:
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- doc: add RafaelGSS to collaborators
(RafaelGSS) (https://github.com/nodejs/node/pull/42718)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
Semver-Major Commits:
- (SEMVER-MAJOR) assert,util: compare RegExp.lastIndex while using deep
equal checks
(Ruben Bridgewater) (https://github.com/nodejs/node/pull/41020)
- (SEMVER-MAJOR) buffer: refactor `byteLength` to remove outdated
optimizations
(Rongjian Zhang) (https://github.com/nodejs/node/pull/38545)
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) buffer: graduate Blob from experimental
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) build: make x86 Windows support temporarily
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42666)
- (SEMVER-MAJOR) build: bump macOS deployment target to 10.15
(Richard Lau) (https://github.com/nodejs/node/pull/42292)
- (SEMVER-MAJOR) build: downgrade Windows 8.1 and server 2012 R2 to
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42105)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- (SEMVER-MAJOR) cluster: make `kill` to be just `process.kill`
(Bar Admoni) (https://github.com/nodejs/node/pull/34312)
- (SEMVER-MAJOR) crypto: cleanup validation
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/39841)
- (SEMVER-MAJOR) crypto: prettify othername in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42123)
- (SEMVER-MAJOR) crypto: fix X509Certificate toLegacyObject
(Tobias Nießen) (https://github.com/nodejs/node/pull/42124)
- (SEMVER-MAJOR) crypto: use RFC2253 format in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42002)
- (SEMVER-MAJOR) crypto: change default check(Host|Email) behavior
(Tobias Nießen) (https://github.com/nodejs/node/pull/41600)
- (SEMVER-MAJOR) deps: V8: cherry-pick semver-major commits from 10.2
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 10.1.124.6
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 9.8.177.9
(Michaël Zasso) (https://github.com/nodejs/node/pull/41610)
- (SEMVER-MAJOR) deps: update V8 to 9.7.106.18
(Michaël Zasso) (https://github.com/nodejs/node/pull/40907)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) doc: update minimum glibc requirements for Linux
(Richard Lau) (https://github.com/nodejs/node/pull/42659)
- (SEMVER-MAJOR) doc: update AIX minimum supported arch
(Richard Lau) (https://github.com/nodejs/node/pull/42604)
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) http: refactor headersTimeout and requestTimeout logic
(Paolo Insogna) (https://github.com/nodejs/node/pull/41263)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) lib: enable fetch by default
(Michaël Zasso) (https://github.com/nodejs/node/pull/41811)
- (SEMVER-MAJOR) lib: replace validator and error
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/41678)
- (SEMVER-MAJOR) module,repl: support 'node:'-only core modules
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: disallow some uses of Object.defineProperty()
on process.env
(Himself65) (https://github.com/nodejs/node/pull/28006)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) readline: fix question still called after closed
(Xuguang Mei) (https://github.com/nodejs/node/pull/42464)
- (SEMVER-MAJOR) stream: remove thenable support
(Robert Nagy) (https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) stream: expose web streams globals, remove runtime
experimental warning
(Antoine du Hamel) (https://github.com/nodejs/node/pull/42225)
- (SEMVER-MAJOR) stream: need to cleanup event listeners if last stream
is readable
(Xuguang Mei) (https://github.com/nodejs/node/pull/41954)
- (SEMVER-MAJOR) stream: revert revert `map` spec compliance
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41933)
- (SEMVER-MAJOR) stream: throw invalid arg type from End Of Stream
(Jithil P Ponnan) (https://github.com/nodejs/node/pull/41766)
- (SEMVER-MAJOR) stream: don't emit finish after destroy
(Robert Nagy) (https://github.com/nodejs/node/pull/40852)
- (SEMVER-MAJOR) stream: add errored and closed props
(Robert Nagy) (https://github.com/nodejs/node/pull/40696)
- (SEMVER-MAJOR) test: add initial test module
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) timers: refactor internal classes to ES2015 syntax
(Rabbit) (https://github.com/nodejs/node/pull/37408)
- (SEMVER-MAJOR) tls: represent registeredID numerically always
(Tobias Nießen) (https://github.com/nodejs/node/pull/41561)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
- (SEMVER-MAJOR) url: throw on NULL in IPv6 hostname
(Rich Trott) (https://github.com/nodejs/node/pull/42313)
- (SEMVER-MAJOR) v8: make v8.writeHeapSnapshot() error codes consistent
(Darshan Sen) (https://github.com/nodejs/node/pull/42577)
- (SEMVER-MAJOR) v8: make writeHeapSnapshot throw if fopen fails
(Antonio Román) (https://github.com/nodejs/node/pull/41373)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
PR-URL: https://github.com/nodejs/node/pull/42262
2022-03-08 01:39:47 +00:00
v18.0.0
2021-11-02 12:01:48 +02:00
-->
* {Error}
Returns error if the stream has been destroyed with an error.
2019-12-24 15:09:29 -08:00
##### `readable.readableFlowing`
2021-10-10 21:55:04 -07:00
2019-09-09 11:57:14 -04:00
<!-- YAML
added: v9.4.0
-->
* {boolean}
This property reflects the current state of a `Readable` stream as described
2020-06-14 14:49:34 -07:00
in the [Three states][] section.
2019-09-09 11:57:14 -04:00
2019-12-24 15:09:29 -08:00
##### `readable.readableHighWaterMark`
2021-10-10 21:55:04 -07:00
2018-03-21 04:12:32 +02:00
<!-- YAML
added: v9.3.0
-->
2018-10-28 11:55:22 +08:00
* {number}
2018-03-21 04:12:32 +02:00
2019-08-25 18:13:27 +02:00
Returns the value of `highWaterMark` passed when creating this `Readable` .
2018-03-21 04:12:32 +02:00
2019-12-24 15:09:29 -08:00
##### `readable.readableLength`
2021-10-10 21:55:04 -07:00
2017-05-05 14:42:21 +02:00
<!-- YAML
2018-01-09 19:23:55 -05:00
added: v9.4.0
2017-05-05 14:42:21 +02:00
-->
2018-10-28 11:55:22 +08:00
* {number}
2018-03-15 03:28:34 +03:00
2017-05-05 14:42:21 +02:00
This property contains the number of bytes (or objects) in the queue
ready to be read. The value provides introspection data regarding
the status of the `highWaterMark` .
2019-12-24 15:09:29 -08:00
##### `readable.readableObjectMode`
2021-10-10 21:55:04 -07:00
2019-05-19 17:24:07 +05:30
<!-- YAML
2019-05-21 13:49:35 +02:00
added: v12.3.0
2019-05-19 17:24:07 +05:30
-->
2019-07-10 16:04:45 +02:00
* {boolean}
2019-05-19 17:24:07 +05:30
Getter for the property `objectMode` of a given `Readable` stream.
2019-12-24 15:09:29 -08:00
##### `readable.resume()`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
2018-02-26 09:24:30 +01:00
changes:
2018-03-02 09:53:46 -08:00
- version: v10.0.0
2018-02-26 09:24:30 +01:00
pr-url: https://github.com/nodejs/node/pull/18994
2018-04-09 19:30:22 +03:00
description: The `resume()` has no effect if there is a `'readable'` event
listening.
2016-06-13 10:32:44 -04:00
-->
2013-02-28 15:42:55 -08:00
2018-01-30 00:15:53 +02:00
* Returns: {this}
2015-06-23 20:42:49 -07:00
2018-04-29 20:46:41 +03:00
The `readable.resume()` method causes an explicitly paused `Readable` stream to
2016-05-23 22:30:41 -07:00
resume emitting [`'data'` ][] events, switching the stream into flowing mode.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
The `readable.resume()` method can be used to fully consume the data from a
2018-08-26 19:02:27 +03:00
stream without actually processing any of that data:
2013-07-15 16:56:02 -07:00
2016-01-17 18:39:07 +01:00
```js
2016-05-23 22:30:41 -07:00
getReadableStreamSomehow()
2016-06-19 00:19:41 +03:00
.resume()
2016-05-23 22:30:41 -07:00
.on('end', () => {
console.log('Reached the end, but did not read anything.');
});
2015-11-05 14:54:10 -05:00
```
2018-02-26 09:24:30 +01:00
The `readable.resume()` method has no effect if there is a `'readable'`
event listener.
2019-12-24 15:09:29 -08:00
##### `readable.setEncoding(encoding)`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2015-11-05 14:54:10 -05:00
2017-02-04 16:15:33 +01:00
* `encoding` {string} The encoding to use.
2018-01-30 00:15:53 +02:00
* Returns: {this}
2015-11-05 14:54:10 -05:00
2017-05-19 12:59:25 +01:00
The `readable.setEncoding()` method sets the character encoding for
2018-04-29 20:46:41 +03:00
data read from the `Readable` stream.
2016-05-23 22:30:41 -07:00
2017-05-19 12:59:25 +01:00
By default, no encoding is assigned and stream data will be returned as
`Buffer` objects. Setting an encoding causes the stream data
to be returned as strings of the specified encoding rather than as `Buffer`
2016-05-23 22:30:41 -07:00
objects. For instance, calling `readable.setEncoding('utf8')` will cause the
2017-05-19 12:59:25 +01:00
output data to be interpreted as UTF-8 data, and passed as strings. Calling
2016-05-23 22:30:41 -07:00
`readable.setEncoding('hex')` will cause the data to be encoded in hexadecimal
string format.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
The `Readable` stream will properly handle multi-byte characters delivered
through the stream that would otherwise become improperly decoded if simply
pulled from the stream as `Buffer` objects.
2015-11-05 14:54:10 -05:00
2016-01-17 18:39:07 +01:00
```js
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
2015-11-05 14:54:10 -05:00
readable.setEncoding('utf8');
2015-12-14 15:20:25 -08:00
readable.on('data', (chunk) => {
2015-11-05 14:54:10 -05:00
assert.equal(typeof chunk, 'string');
2018-11-06 08:40:22 +10:00
console.log('Got %d characters of string data:', chunk.length);
2015-11-05 14:54:10 -05:00
});
```
2019-12-24 15:09:29 -08:00
##### `readable.unpipe([destination])`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2015-11-05 14:54:10 -05:00
2016-01-19 13:03:15 -03:00
* `destination` {stream.Writable} Optional specific stream to unpipe
2018-03-15 03:28:34 +03:00
* Returns: {this}
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
The `readable.unpipe()` method detaches a `Writable` stream previously attached
2016-05-23 22:30:41 -07:00
using the [`stream.pipe()` ][] method.
2015-11-05 14:54:10 -05:00
2021-10-10 21:55:04 -07:00
If the `destination` is not specified, then _all_ pipes are detached.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
If the `destination` is specified, but no pipe is set up for it, then
the method does nothing.
2015-11-05 14:54:10 -05:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
2016-05-23 22:30:41 -07:00
const readable = getReadableStreamSomehow();
const writable = fs.createWriteStream('file.txt');
2015-11-05 14:54:10 -05:00
// All the data from readable goes into 'file.txt',
2019-07-07 20:56:12 +03:00
// but only for the first second.
2015-11-05 14:54:10 -05:00
readable.pipe(writable);
2015-12-14 15:20:25 -08:00
setTimeout(() => {
2018-11-06 08:40:22 +10:00
console.log('Stop writing to file.txt.');
2015-11-05 14:54:10 -05:00
readable.unpipe(writable);
2018-11-06 08:40:22 +10:00
console.log('Manually close the file stream.');
2015-11-05 14:54:10 -05:00
writable.end();
}, 1000);
```
2019-12-24 15:09:29 -08:00
##### `readable.unshift(chunk[, encoding])`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.11
2017-01-09 19:05:06 +01:00
changes:
2024-05-02 11:31:36 +02:00
- version:
- v22.0.0
- v20.13.0
2024-03-20 18:27:29 +01:00
pr-url: https://github.com/nodejs/node/pull/51866
description: The `chunk` argument can now be a `TypedArray` or `DataView` instance.
2017-03-15 20:26:14 -07:00
- version: v8.0.0
2017-01-09 19:05:06 +01:00
pr-url: https://github.com/nodejs/node/pull/11608
description: The `chunk` argument can now be a `Uint8Array` instance.
2016-06-13 10:32:44 -04:00
-->
2015-11-05 14:54:10 -05:00
2024-03-20 18:27:29 +01:00
* `chunk` {Buffer|TypedArray|DataView|string|null|any} Chunk of data to unshift
onto the read queue. For streams not operating in object mode, `chunk` must
be a {string}, {Buffer}, {TypedArray}, {DataView} or `null` .
For object mode streams, `chunk` may be any JavaScript value.
2019-04-11 19:16:34 -03:00
* `encoding` {string} Encoding of string chunks. Must be a valid
`Buffer` encoding, such as `'utf8'` or `'ascii'` .
2015-11-05 14:54:10 -05:00
2019-10-16 12:55:15 +02:00
Passing `chunk` as `null` signals the end of the stream (EOF) and behaves the
same as `readable.push(null)` , after which no more data can be written. The EOF
signal is put at the end of the buffer and any buffered data will still be
flushed.
2019-08-04 12:54:48 -03:00
2016-05-23 22:30:41 -07:00
The `readable.unshift()` method pushes a chunk of data back into the internal
buffer. This is useful in certain situations where a stream is being consumed by
code that needs to "un-consume" some amount of data that it has optimistically
pulled out of the source, so that the data can be passed on to some other party.
2015-11-05 14:54:10 -05:00
2018-02-05 21:55:16 -08:00
The `stream.unshift(chunk)` method cannot be called after the [`'end'` ][] event
has been emitted or a runtime error will be thrown.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
Developers using `stream.unshift()` often should consider switching to
2020-06-14 14:49:34 -07:00
use of a [`Transform` ][] stream instead. See the [API for stream implementers][]
2016-05-23 22:30:41 -07:00
section for more information.
2015-11-05 14:54:10 -05:00
2016-01-17 18:39:07 +01:00
```js
2019-07-07 20:56:12 +03:00
// Pull off a header delimited by \n\n.
// Use unshift() if we get too much.
// Call the callback with (error, header, stream).
2022-04-20 10:23:41 +02:00
const { StringDecoder } = require('node:string_decoder');
2015-11-05 14:54:10 -05:00
function parseHeader(stream, callback) {
stream.on('error', callback);
2013-07-15 16:56:02 -07:00
stream.on('readable', onReadable);
2016-05-23 22:30:41 -07:00
const decoder = new StringDecoder('utf8');
2017-04-22 15:22:40 +03:00
let header = '';
2013-07-15 16:56:02 -07:00
function onReadable() {
2017-04-22 15:22:40 +03:00
let chunk;
2013-07-15 16:56:02 -07:00
while (null !== (chunk = stream.read())) {
2017-04-22 15:22:40 +03:00
const str = decoder.write(chunk);
2022-02-15 19:01:36 +08:00
if (str.includes('\n\n')) {
2019-07-07 20:56:12 +03:00
// Found the header boundary.
2017-04-22 15:22:40 +03:00
const split = str.split(/\n\n/);
2013-07-15 16:56:02 -07:00
header += split.shift();
2016-05-23 22:30:41 -07:00
const remaining = split.join('\n\n');
const buf = Buffer.from(remaining, 'utf8');
2013-07-15 16:56:02 -07:00
stream.removeListener('error', callback);
2019-07-07 20:56:12 +03:00
// Remove the 'readable' listener before unshifting.
2013-07-15 16:56:02 -07:00
stream.removeListener('readable', onReadable);
2016-08-21 10:07:10 +08:00
if (buf.length)
stream.unshift(buf);
2018-12-10 13:27:32 +01:00
// Now the body of the message can be read from the stream.
2013-07-15 16:56:02 -07:00
callback(null, header, stream);
2022-02-15 19:01:36 +08:00
return;
2013-07-15 16:56:02 -07:00
}
2022-02-15 19:01:36 +08:00
// Still reading the header.
header += str;
2013-07-15 16:56:02 -07:00
}
}
}
```
2016-01-17 18:39:07 +01:00
2018-02-05 21:55:16 -08:00
Unlike [`stream.push(chunk)` ][stream-push], `stream.unshift(chunk)` will not
end the reading process by resetting the internal reading state of the stream.
This can cause unexpected results if `readable.unshift()` is called during a
read (i.e. from within a [`stream._read()` ][stream-_read] implementation on a
custom stream). Following the call to `readable.unshift()` with an immediate
[`stream.push('')` ][stream-push] will reset the reading state appropriately,
however it is best to simply avoid calling `readable.unshift()` while in the
process of performing a read.
2013-07-15 16:56:02 -07:00
2019-12-24 15:09:29 -08:00
##### `readable.wrap(stream)`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2013-07-15 16:56:02 -07:00
* `stream` {Stream} An "old style" readable stream
2018-03-15 03:28:34 +03:00
* Returns: {this}
2013-07-15 16:56:02 -07:00
2022-04-20 10:23:41 +02:00
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
information.)
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
When using an older Node.js library that emits [`'data'` ][] events and has a
[`stream.pause()` ][stream-pause] method that is advisory only, the
2018-04-29 20:46:41 +03:00
`readable.wrap()` method can be used to create a [`Readable` ][] stream that uses
2016-05-23 22:30:41 -07:00
the old stream as its data source.
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
It will rarely be necessary to use `readable.wrap()` but the method has been
provided as a convenience for interacting with older Node.js applications and
libraries.
2013-07-15 16:56:02 -07:00
2016-01-17 18:39:07 +01:00
```js
2017-06-01 01:21:22 +03:00
const { OldReader } = require('./old-api-module.js');
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2017-04-21 17:38:31 +03:00
const oreader = new OldReader();
2015-12-14 15:20:25 -08:00
const myReader = new Readable().wrap(oreader);
2013-07-15 16:56:02 -07:00
2015-12-14 15:20:25 -08:00
myReader.on('readable', () => {
2013-07-15 16:56:02 -07:00
myReader.read(); // etc.
});
```
2019-12-24 15:09:29 -08:00
##### `readable[Symbol.asyncIterator]()`
2021-10-10 21:55:04 -07:00
2017-12-19 13:33:31 +01:00
<!-- YAML
2018-03-02 09:53:46 -08:00
added: v10.0.0
2019-03-29 17:14:48 +01:00
changes:
2019-04-09 23:55:02 +01:00
- version: v11.14.0
2019-03-29 17:14:48 +01:00
pr-url: https://github.com/nodejs/node/pull/26989
description: Symbol.asyncIterator support is no longer experimental.
2017-12-19 13:33:31 +01:00
-->
2018-04-14 23:02:30 +03:00
* Returns: {AsyncIterator} to fully consume the stream.
2017-12-19 13:33:31 +01:00
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
2018-02-15 23:53:13 +03:00
2017-12-19 13:33:31 +01:00
async function print(readable) {
readable.setEncoding('utf8');
let data = '';
2019-07-07 20:56:12 +03:00
for await (const chunk of readable) {
data += chunk;
2017-12-19 13:33:31 +01:00
}
console.log(data);
}
2019-07-07 20:56:12 +03:00
print(fs.createReadStream('file')).catch(console.error);
2017-12-19 13:33:31 +01:00
```
2021-04-17 21:16:46 +03:00
If the loop terminates with a `break` , `return` , or a `throw` , the stream will
be destroyed. In other terms, iterating over a stream will consume the stream
2018-03-13 23:45:51 +03:00
fully. The stream will be read in chunks of size equal to the `highWaterMark`
option. In the code example above, data will be in a single chunk if the file
2022-04-20 00:46:37 +02:00
has less then 64 KiB of data because no `highWaterMark` option is provided to
2018-03-13 23:45:51 +03:00
[`fs.createReadStream()` ][].
2017-12-19 13:33:31 +01:00
2023-06-25 14:18:54 +03:00
##### `readable[Symbol.asyncDispose]()`
<!-- YAML
2023-09-16 22:51:24 -04:00
added:
- v20.4.0
- v18.18.0
2023-06-25 14:18:54 +03:00
-->
> Stability: 1 - Experimental
Calls [`readable.destroy()` ][readable-destroy] with an `AbortError` and returns
a promise that fulfills when the stream is finished.
2022-10-31 15:57:02 +02:00
##### `readable.compose(stream[, options])`
<!-- YAML
2023-01-05, Version 18.13.0 'Hydrogen' (LTS)
Notable changes:
Add support for externally shared js builtins:
By default Node.js is built so that all dependencies are bundled into the
Node.js binary itself. Some Node.js distributions prefer to manage dependencies
externally. There are existing build options that allow dependencies with
native code to be externalized. This commit adds additional options so that
dependencies with JavaScript code (including WASM) can also be externalized.
This addition does not affect binaries shipped by the Node.js project but
will allow other distributions to externalize additional dependencies when
needed.
Contributed by Michael Dawson in https://github.com/nodejs/node/pull/44376
Introduce `File`:
The File class is part of the [FileAPI](https://w3c.github.io/FileAPI/).
It can be used anywhere a Blob can, for example in `URL.createObjectURL`
and `FormData`. It contains two properties that Blobs do not have: `lastModified`,
the last time the file was modified in ms, and `name`, the name of the file.
Contributed by Khafra in https://github.com/nodejs/node/pull/45139
Support function mocking on Node.js test runner:
The `node:test` module supports mocking during testing via a top-level `mock`
object.
```js
test('spies on an object method', (t) => {
const number = {
value: 5,
add(a) {
return this.value + a;
},
};
t.mock.method(number, 'add');
assert.strictEqual(number.add(3), 8);
assert.strictEqual(number.add.mock.calls.length, 1);
});
```
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/45326
Other notable changes:
build:
* disable v8 snapshot compression by default (Joyee Cheung) https://github.com/nodejs/node/pull/45716
crypto:
* update root certificates (Luigi Pinca) https://github.com/nodejs/node/pull/45490
deps:
* update ICU to 72.1 (Michaël Zasso) https://github.com/nodejs/node/pull/45068
doc:
* add doc-only deprecation for headers/trailers setters (Rich Trott) https://github.com/nodejs/node/pull/45697
* add Rafael to the tsc (Michael Dawson) https://github.com/nodejs/node/pull/45691
* deprecate use of invalid ports in `url.parse` (Antoine du Hamel) https://github.com/nodejs/node/pull/45576
* add lukekarrys to collaborators (Luke Karrys) https://github.com/nodejs/node/pull/45180
* add anonrig to collaborators (Yagiz Nizipli) https://github.com/nodejs/node/pull/45002
* deprecate url.parse() (Rich Trott) https://github.com/nodejs/node/pull/44919
lib:
* drop fetch experimental warning (Matteo Collina) https://github.com/nodejs/node/pull/45287
net:
* (SEMVER-MINOR) add autoSelectFamily and autoSelectFamilyAttemptTimeout options (Paolo Insogna) https://github.com/nodejs/node/pull/44731
* src:
* (SEMVER-MINOR) add uvwasi version (Jithil P Ponnan) https://github.com/nodejs/node/pull/45639
* (SEMVER-MINOR) add initial shadow realm support (Chengzhong Wu) https://github.com/nodejs/node/pull/42869
test_runner:
* (SEMVER-MINOR) add t.after() hook (Colin Ihrig) https://github.com/nodejs/node/pull/45792
* (SEMVER-MINOR) don't use a symbol for runHook() (Colin Ihrig) https://github.com/nodejs/node/pull/45792
tls:
* (SEMVER-MINOR) add "ca" property to certificate object (Ben Noordhuis) https://github.com/nodejs/node/pull/44935
* remove trustcor root ca certificates (Ben Noordhuis) https://github.com/nodejs/node/pull/45776
tools:
* update certdata.txt (Luigi Pinca) https://github.com/nodejs/node/pull/45490
util:
* add fast path for utf8 encoding (Yagiz Nizipli) https://github.com/nodejs/node/pull/45412
* improve textdecoder decode performance (Yagiz Nizipli) https://github.com/nodejs/node/pull/45294
* (SEMVER-MINOR) add MIME utilities (#21128) (Bradley Farias) https://github.com/nodejs/node/pull/21128
PR-URL: https://github.com/nodejs/node/pull/46025
2022-12-30 15:18:44 -05:00
added:
- v19.1.0
- v18.13.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2022-10-31 15:57:02 +02:00
-->
* `stream` {Stream|Iterable|AsyncIterable|Function}
* `options` {Object}
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Duplex} a stream composed with the stream `stream` .
```mjs
import { Readable } from 'node:stream';
async function* splitToWords(source) {
for await (const chunk of source) {
const words = String(chunk).split(' ');
for (const word of words) {
yield word;
}
}
}
const wordsStream = Readable.from(['this is', 'compose as operator']).compose(splitToWords);
const words = await wordsStream.toArray();
console.log(words); // prints ['this', 'is', 'compose', 'as', 'operator']
```
See [`stream.compose` ][] for more information.
2021-04-17 21:16:46 +03:00
##### `readable.iterator([options])`
2021-10-10 21:55:04 -07:00
2021-04-17 21:16:46 +03:00
<!-- YAML
2021-05-31 15:50:35 -04:00
added: v16.3.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-04-17 21:16:46 +03:00
-->
* `options` {Object}
* `destroyOnReturn` {boolean} When set to `false` , calling `return` on the
async iterator, or exiting a `for await...of` iteration using a `break` ,
`return` , or `throw` will not destroy the stream. **Default:** `true` .
* Returns: {AsyncIterator} to consume the stream.
The iterator created by this method gives users the option to cancel the
destruction of the stream if the `for await...of` loop is exited by `return` ,
`break` , or `throw` , or if the iterator should destroy the stream if the stream
emitted an error during iteration.
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2021-04-17 21:16:46 +03:00
async function printIterator(readable) {
for await (const chunk of readable.iterator({ destroyOnReturn: false })) {
console.log(chunk); // 1
break;
}
console.log(readable.destroyed); // false
for await (const chunk of readable.iterator({ destroyOnReturn: false })) {
console.log(chunk); // Will print 2 and then 3
}
console.log(readable.destroyed); // True, stream was totally consumed
}
async function printSymbolAsyncIterator(readable) {
for await (const chunk of readable) {
console.log(chunk); // 1
break;
}
console.log(readable.destroyed); // true
}
async function showBoth() {
await printIterator(Readable.from([1, 2, 3]));
await printSymbolAsyncIterator(Readable.from([1, 2, 3]));
}
showBoth();
```
2022-02-13 01:57:43 +08:00
##### `readable.map(fn[, options])`
2021-11-15 15:39:05 +02:00
<!-- YAML
2022-02-01 00:34:51 -05:00
added:
- v17.4.0
- v16.14.0
2023-08-24 15:11:21 +03:00
changes:
2023-11-27 12:19:49 +01:00
- version:
- v20.7.0
- v18.19.0
2023-08-24 15:11:21 +03:00
pr-url: https://github.com/nodejs/node/pull/49249
description: added `highWaterMark` in options.
2021-11-15 15:39:05 +02:00
-->
> Stability: 1 - Experimental
2022-02-07 09:24:17 +02:00
* `fn` {Function|AsyncFunction} a function to map over every chunk in the
stream.
2021-11-15 15:39:05 +02:00
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `options` {Object}
2022-01-08 12:46:07 +02:00
* `concurrency` {number} the maximum concurrent invocation of `fn` to call
2021-11-15 15:39:05 +02:00
on the stream at once. **Default:** `1` .
2023-08-24 15:11:21 +03:00
* `highWaterMark` {number} how many items to buffer while waiting for user
consumption of the mapped items. **Default:** `concurrency * 2 - 1` .
2021-11-15 15:39:05 +02:00
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Readable} a stream mapped with the function `fn` .
This method allows mapping over the stream. The `fn` function will be called
2022-02-07 09:24:17 +02:00
for every chunk in the stream. If the `fn` function returns a promise - that
2021-11-15 15:39:05 +02:00
promise will be `await` ed before being passed to the result stream.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { Resolver } from 'node:dns/promises';
2021-11-15 15:39:05 +02:00
// With a synchronous mapper.
2022-02-07 09:24:17 +02:00
for await (const chunk of Readable.from([1, 2, 3, 4]).map((x) => x * 2)) {
console.log(chunk); // 2, 4, 6, 8
2021-11-15 15:39:05 +02:00
}
// With an asynchronous mapper, making at most 2 queries at a time.
const resolver = new Resolver();
2022-01-19 16:22:43 +02:00
const dnsResults = Readable.from([
2021-11-15 15:39:05 +02:00
'nodejs.org',
'openjsf.org',
'www.linuxfoundation.org',
]).map((domain) => resolver.resolve4(domain), { concurrency: 2 });
for await (const result of dnsResults) {
console.log(result); // Logs the DNS result of resolver.resolve4.
}
```
2022-02-13 01:57:43 +08:00
##### `readable.filter(fn[, options])`
2021-12-30 12:14:03 +02:00
<!-- YAML
2022-02-01 00:34:51 -05:00
added:
- v17.4.0
- v16.14.0
2023-08-24 15:11:21 +03:00
changes:
2023-11-27 12:19:49 +01:00
- version:
- v20.7.0
- v18.19.0
2023-08-24 15:11:21 +03:00
pr-url: https://github.com/nodejs/node/pull/49249
description: added `highWaterMark` in options.
2021-12-30 12:14:03 +02:00
-->
> Stability: 1 - Experimental
2022-02-07 09:24:17 +02:00
* `fn` {Function|AsyncFunction} a function to filter chunks from the stream.
2021-12-30 12:14:03 +02:00
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `options` {Object}
2022-01-08 12:46:07 +02:00
* `concurrency` {number} the maximum concurrent invocation of `fn` to call
2021-12-30 12:14:03 +02:00
on the stream at once. **Default:** `1` .
2023-08-24 15:11:21 +03:00
* `highWaterMark` {number} how many items to buffer while waiting for user
consumption of the filtered items. **Default:** `concurrency * 2 - 1` .
2021-12-30 12:14:03 +02:00
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Readable} a stream filtered with the predicate `fn` .
2022-02-07 09:24:17 +02:00
This method allows filtering the stream. For each chunk in the stream the `fn`
function will be called and if it returns a truthy value, the chunk will be
2021-12-30 12:14:03 +02:00
passed to the result stream. If the `fn` function returns a promise - that
promise will be `await` ed.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { Resolver } from 'node:dns/promises';
2021-12-30 12:14:03 +02:00
// With a synchronous predicate.
2022-02-07 09:24:17 +02:00
for await (const chunk of Readable.from([1, 2, 3, 4]).filter((x) => x > 2)) {
console.log(chunk); // 3, 4
2021-12-30 12:14:03 +02:00
}
// With an asynchronous predicate, making at most 2 queries at a time.
const resolver = new Resolver();
2022-01-19 16:22:43 +02:00
const dnsResults = Readable.from([
2021-12-30 12:14:03 +02:00
'nodejs.org',
'openjsf.org',
'www.linuxfoundation.org',
]).filter(async (domain) => {
const { address } = await resolver.resolve4(domain, { ttl: true });
return address.ttl > 60;
}, { concurrency: 2 });
for await (const result of dnsResults) {
// Logs domains with more than 60 seconds on the resolved dns record.
console.log(result);
}
```
2022-02-13 01:57:43 +08:00
##### `readable.forEach(fn[, options])`
2022-01-08 12:46:07 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-08 12:46:07 +02:00
-->
> Stability: 1 - Experimental
2022-02-07 09:24:17 +02:00
* `fn` {Function|AsyncFunction} a function to call on each chunk of the stream.
2022-01-08 12:46:07 +02:00
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `options` {Object}
* `concurrency` {number} the maximum concurrent invocation of `fn` to call
on the stream at once. **Default:** `1` .
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Promise} a promise for when the stream has finished.
2022-02-07 09:24:17 +02:00
This method allows iterating a stream. For each chunk in the stream the
2022-01-08 12:46:07 +02:00
`fn` function will be called. If the `fn` function returns a promise - that
promise will be `await` ed.
This method is different from `for await...of` loops in that it can optionally
2022-02-07 09:24:17 +02:00
process chunks concurrently. In addition, a `forEach` iteration can only be
2022-01-08 12:46:07 +02:00
stopped by having passed a `signal` option and aborting the related
`AbortController` while `for await...of` can be stopped with `break` or
`return` . In either case the stream will be destroyed.
This method is different from listening to the [`'data'` ][] event in that it
2024-09-29 23:15:15 +10:00
uses the [`readable` ][] event in the underlying machinery and can limit the
2022-01-08 12:46:07 +02:00
number of concurrent `fn` calls.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { Resolver } from 'node:dns/promises';
2022-01-08 12:46:07 +02:00
// With a synchronous predicate.
2022-02-07 09:24:17 +02:00
for await (const chunk of Readable.from([1, 2, 3, 4]).filter((x) => x > 2)) {
console.log(chunk); // 3, 4
2022-01-08 12:46:07 +02:00
}
// With an asynchronous predicate, making at most 2 queries at a time.
const resolver = new Resolver();
2022-01-19 16:22:43 +02:00
const dnsResults = Readable.from([
2022-01-08 12:46:07 +02:00
'nodejs.org',
'openjsf.org',
'www.linuxfoundation.org',
]).map(async (domain) => {
const { address } = await resolver.resolve4(domain, { ttl: true });
return address;
}, { concurrency: 2 });
await dnsResults.forEach((result) => {
// Logs result, similar to `for await (const result of dnsResults)`
console.log(result);
});
console.log('done'); // Stream has finished
```
2022-02-13 01:57:43 +08:00
##### `readable.toArray([options])`
2022-01-16 14:09:27 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-16 14:09:27 +02:00
-->
> Stability: 1 - Experimental
* `options` {Object}
* `signal` {AbortSignal} allows cancelling the toArray operation if the
signal is aborted.
2022-01-23 10:19:53 +02:00
* Returns: {Promise} a promise containing an array with the contents of the
stream.
2022-01-16 14:09:27 +02:00
2022-01-23 10:19:53 +02:00
This method allows easily obtaining the contents of a stream.
2022-01-16 14:09:27 +02:00
As this method reads the entire stream into memory, it negates the benefits of
streams. It's intended for interoperability and convenience, not as the primary
way to consume streams.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { Resolver } from 'node:dns/promises';
2022-01-16 14:09:27 +02:00
await Readable.from([1, 2, 3, 4]).toArray(); // [1, 2, 3, 4]
// Make dns queries concurrently using .map and collect
2022-01-17 20:21:51 +02:00
// the results into an array using toArray
2022-01-16 14:09:27 +02:00
const dnsResults = await Readable.from([
'nodejs.org',
'openjsf.org',
'www.linuxfoundation.org',
]).map(async (domain) => {
const { address } = await resolver.resolve4(domain, { ttl: true });
return address;
}, { concurrency: 2 }).toArray();
```
2022-02-13 01:57:43 +08:00
##### `readable.some(fn[, options])`
2022-01-17 20:21:51 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-17 20:21:51 +02:00
-->
> Stability: 1 - Experimental
2022-02-07 09:24:17 +02:00
* `fn` {Function|AsyncFunction} a function to call on each chunk of the stream.
2022-01-17 20:21:51 +02:00
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `options` {Object}
* `concurrency` {number} the maximum concurrent invocation of `fn` to call
on the stream at once. **Default:** `1` .
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Promise} a promise evaluating to `true` if `fn` returned a truthy
value for at least one of the chunks.
This method is similar to `Array.prototype.some` and calls `fn` on each chunk
in the stream until the awaited return value is `true` (or any truthy value).
Once an `fn` call on a chunk awaited return value is truthy, the stream is
destroyed and the promise is fulfilled with `true` . If none of the `fn`
calls on the chunks return a truthy value, the promise is fulfilled with
`false` .
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { stat } from 'node:fs/promises';
2022-01-17 20:21:51 +02:00
// With a synchronous predicate.
await Readable.from([1, 2, 3, 4]).some((x) => x > 2); // true
await Readable.from([1, 2, 3, 4]).some((x) => x < 0 ) ; / / false
// With an asynchronous predicate, making at most 2 file checks at a time.
const anyBigFile = await Readable.from([
'file1',
'file2',
'file3',
]).some(async (fileName) => {
const stats = await stat(fileName);
2023-03-01 18:13:29 +01:00
return stats.size > 1024 * 1024;
2022-01-17 20:21:51 +02:00
}, { concurrency: 2 });
console.log(anyBigFile); // `true` if any file in the list is bigger than 1MB
console.log('done'); // Stream has finished
```
2022-02-13 01:57:43 +08:00
##### `readable.find(fn[, options])`
2022-02-07 09:24:17 +02:00
<!-- YAML
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
added:
- v17.5.0
- v16.17.0
2022-02-07 09:24:17 +02:00
-->
> Stability: 1 - Experimental
* `fn` {Function|AsyncFunction} a function to call on each chunk of the stream.
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `options` {Object}
* `concurrency` {number} the maximum concurrent invocation of `fn` to call
on the stream at once. **Default:** `1` .
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Promise} a promise evaluating to the first chunk for which `fn`
evaluated with a truthy value, or `undefined` if no element was found.
This method is similar to `Array.prototype.find` and calls `fn` on each chunk
in the stream to find a chunk with a truthy value for `fn` . Once an `fn` call's
awaited return value is truthy, the stream is destroyed and the promise is
fulfilled with value for which `fn` returned a truthy value. If all of the
`fn` calls on the chunks return a falsy value, the promise is fulfilled with
`undefined` .
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { stat } from 'node:fs/promises';
2022-02-07 09:24:17 +02:00
// With a synchronous predicate.
await Readable.from([1, 2, 3, 4]).find((x) => x > 2); // 3
await Readable.from([1, 2, 3, 4]).find((x) => x > 0); // 1
await Readable.from([1, 2, 3, 4]).find((x) => x > 10); // undefined
// With an asynchronous predicate, making at most 2 file checks at a time.
const foundBigFile = await Readable.from([
'file1',
'file2',
'file3',
]).find(async (fileName) => {
const stats = await stat(fileName);
2023-03-01 18:13:29 +01:00
return stats.size > 1024 * 1024;
2022-02-07 09:24:17 +02:00
}, { concurrency: 2 });
console.log(foundBigFile); // File name of large file, if any file in the list is bigger than 1MB
console.log('done'); // Stream has finished
```
2022-02-13 01:57:43 +08:00
##### `readable.every(fn[, options])`
2022-01-17 20:21:51 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-17 20:21:51 +02:00
-->
> Stability: 1 - Experimental
2022-02-07 09:24:17 +02:00
* `fn` {Function|AsyncFunction} a function to call on each chunk of the stream.
2022-01-17 20:21:51 +02:00
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `options` {Object}
* `concurrency` {number} the maximum concurrent invocation of `fn` to call
on the stream at once. **Default:** `1` .
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Promise} a promise evaluating to `true` if `fn` returned a truthy
value for all of the chunks.
This method is similar to `Array.prototype.every` and calls `fn` on each chunk
in the stream to check if all awaited return values are truthy value for `fn` .
Once an `fn` call on a chunk awaited return value is falsy, the stream is
destroyed and the promise is fulfilled with `false` . If all of the `fn` calls
on the chunks return a truthy value, the promise is fulfilled with `true` .
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { stat } from 'node:fs/promises';
2022-01-17 20:21:51 +02:00
// With a synchronous predicate.
await Readable.from([1, 2, 3, 4]).every((x) => x > 2); // false
await Readable.from([1, 2, 3, 4]).every((x) => x > 0); // true
// With an asynchronous predicate, making at most 2 file checks at a time.
const allBigFiles = await Readable.from([
'file1',
'file2',
'file3',
]).every(async (fileName) => {
const stats = await stat(fileName);
2023-03-01 18:13:29 +01:00
return stats.size > 1024 * 1024;
2022-01-17 20:21:51 +02:00
}, { concurrency: 2 });
// `true` if all files in the list are bigger than 1MiB
console.log(allBigFiles);
console.log('done'); // Stream has finished
```
2022-02-13 01:57:43 +08:00
##### `readable.flatMap(fn[, options])`
2022-01-20 14:01:43 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-20 14:01:43 +02:00
-->
> Stability: 1 - Experimental
* `fn` {Function|AsyncGeneratorFunction|AsyncFunction} a function to map over
2022-02-07 09:24:17 +02:00
every chunk in the stream.
2022-01-20 14:01:43 +02:00
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `options` {Object}
* `concurrency` {number} the maximum concurrent invocation of `fn` to call
on the stream at once. **Default:** `1` .
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Readable} a stream flat-mapped with the function `fn` .
This method returns a new stream by applying the given callback to each
chunk of the stream and then flattening the result.
It is possible to return a stream or another iterable or async iterable from
`fn` and the result streams will be merged (flattened) into the returned
stream.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
import { createReadStream } from 'node:fs';
2022-01-20 14:01:43 +02:00
// With a synchronous mapper.
2022-02-07 09:24:17 +02:00
for await (const chunk of Readable.from([1, 2, 3, 4]).flatMap((x) => [x, x])) {
console.log(chunk); // 1, 1, 2, 2, 3, 3, 4, 4
2022-01-20 14:01:43 +02:00
}
// With an asynchronous mapper, combine the contents of 4 files
const concatResult = Readable.from([
'./1.mjs',
'./2.mjs',
'./3.mjs',
'./4.mjs',
]).flatMap((fileName) => createReadStream(fileName));
for await (const result of concatResult) {
// This will contain the contents (all chunks) of all 4 files
console.log(result);
}
```
2022-02-13 01:57:43 +08:00
##### `readable.drop(limit[, options])`
2022-01-21 18:42:21 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-21 18:42:21 +02:00
-->
> Stability: 1 - Experimental
* `limit` {number} the number of chunks to drop from the readable.
* `options` {Object}
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Readable} a stream with `limit` chunks dropped.
This method returns a new stream with the first `limit` chunks dropped.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
2022-01-21 18:42:21 +02:00
await Readable.from([1, 2, 3, 4]).drop(2).toArray(); // [3, 4]
```
2022-02-13 01:57:43 +08:00
##### `readable.take(limit[, options])`
2022-01-21 18:42:21 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-21 18:42:21 +02:00
-->
> Stability: 1 - Experimental
* `limit` {number} the number of chunks to take from the readable.
* `options` {Object}
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Readable} a stream with `limit` chunks taken.
This method returns a new stream with the first `limit` chunks.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
2022-01-21 18:42:21 +02:00
await Readable.from([1, 2, 3, 4]).take(2).toArray(); // [1, 2]
```
2022-02-13 01:57:43 +08:00
##### `readable.reduce(fn[, initial[, options]])`
2022-01-30 16:07:32 +02:00
<!-- YAML
2022-04-23 21:03:46 -04:00
added:
- v17.5.0
- v16.15.0
2022-01-30 16:07:32 +02:00
-->
> Stability: 1 - Experimental
* `fn` {Function|AsyncFunction} a reducer function to call over every chunk
in the stream.
* `previous` {any} the value obtained from the last call to `fn` or the
`initial` value if specified or the first chunk of the stream otherwise.
* `data` {any} a chunk of data from the stream.
* `options` {Object}
* `signal` {AbortSignal} aborted if the stream is destroyed allowing to
abort the `fn` call early.
* `initial` {any} the initial value to use in the reduction.
* `options` {Object}
* `signal` {AbortSignal} allows destroying the stream if the signal is
aborted.
* Returns: {Promise} a promise for the final value of the reduction.
This method calls `fn` on each chunk of the stream in order, passing it the
result from the calculation on the previous element. It returns a promise for
the final value of the reduction.
If no `initial` value is supplied the first chunk of the stream is used as the
initial value. If the stream is empty, the promise is rejected with a
`TypeError` with the `ERR_INVALID_ARGS` code property.
```mjs
2022-04-20 10:23:41 +02:00
import { Readable } from 'node:stream';
2023-03-24 14:27:55 +03:00
import { readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';
2022-01-30 16:07:32 +02:00
2023-03-24 14:27:55 +03:00
const directoryPath = './src';
const filesInDir = await readdir(directoryPath);
const folderSize = await Readable.from(filesInDir)
.reduce(async (totalSize, file) => {
const { size } = await stat(join(directoryPath, file));
return totalSize + size;
}, 0);
console.log(folderSize);
```
The reducer function iterates the stream element-by-element which means that
there is no `concurrency` parameter or parallelism. To perform a `reduce`
concurrently, you can extract the async function to [`readable.map` ][] method.
```mjs
import { Readable } from 'node:stream';
import { readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';
const directoryPath = './src';
const filesInDir = await readdir(directoryPath);
const folderSize = await Readable.from(filesInDir)
.map((file) => stat(join(directoryPath, file)), { concurrency: 2 })
.reduce((totalSize, { size }) => totalSize + size, 0);
console.log(folderSize);
2022-01-30 16:07:32 +02:00
```
2020-06-14 14:49:34 -07:00
### Duplex and transform streams
2015-11-05 14:54:10 -05:00
2019-12-24 15:09:29 -08:00
#### Class: `stream.Duplex`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
2017-02-21 23:38:48 +01:00
changes:
- version: v6.8.0
pr-url: https://github.com/nodejs/node/pull/8834
description: Instances of `Duplex` now return `true` when
checking `instanceof stream.Writable` .
2016-06-13 10:32:44 -04:00
-->
2016-05-23 22:30:41 -07:00
<!-- type=class -->
2018-04-29 20:46:41 +03:00
Duplex streams are streams that implement both the [`Readable` ][] and
[`Writable` ][] interfaces.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
Examples of `Duplex` streams include:
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
* [TCP sockets][]
2016-02-02 20:34:29 +03:00
* [zlib streams][zlib]
* [crypto streams][crypto]
2013-07-15 16:56:02 -07:00
2021-06-23 16:20:08 +02:00
##### `duplex.allowHalfOpen`
2021-10-10 21:55:04 -07:00
2021-06-23 16:20:08 +02:00
<!-- YAML
added: v0.9.4
-->
* {boolean}
If `false` then the stream will automatically end the writable side when the
readable side ends. Set initially by the `allowHalfOpen` constructor option,
2022-07-05 20:44:07 +02:00
which defaults to `true` .
2021-06-23 16:20:08 +02:00
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
emitted.
2019-12-24 15:09:29 -08:00
#### Class: `stream.Transform`
2021-10-10 21:55:04 -07:00
2016-06-13 10:32:44 -04:00
<!-- YAML
added: v0.9.4
-->
2013-07-15 16:56:02 -07:00
<!-- type=class -->
2018-04-29 20:46:41 +03:00
Transform streams are [`Duplex` ][] streams where the output is in some way
related to the input. Like all [`Duplex` ][] streams, `Transform` streams
implement both the [`Readable` ][] and [`Writable` ][] interfaces.
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
Examples of `Transform` streams include:
2013-07-15 16:56:02 -07:00
2016-02-02 20:34:29 +03:00
* [zlib streams][zlib]
* [crypto streams][crypto]
2013-07-15 16:56:02 -07:00
2019-12-24 15:09:29 -08:00
##### `transform.destroy([error])`
2021-10-10 21:55:04 -07:00
2017-05-06 14:20:52 +02:00
<!-- YAML
2017-03-15 20:26:14 -07:00
added: v8.0.0
2020-09-24 12:27:06 +02:00
changes:
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/29197
2020-09-27 07:50:41 -07:00
description: Work as a no-op on a stream that has already been destroyed.
2017-05-06 14:20:52 +02:00
-->
2019-09-06 01:42:22 -04:00
2018-07-12 13:48:11 -04:00
* `error` {Error}
2020-04-11 15:18:43 -04:00
* Returns: {this}
2017-05-06 14:20:52 +02:00
2019-03-11 19:06:12 +01:00
Destroy the stream, and optionally emit an `'error'` event. After this call, the
2017-05-06 14:20:52 +02:00
transform stream would release any internal resources.
2018-09-29 21:21:21 +02:00
Implementors should not override this method, but instead implement
2018-04-09 19:30:22 +03:00
[`readable._destroy()` ][readable-_destroy].
2019-03-11 19:06:12 +01:00
The default implementation of `_destroy()` for `Transform` also emit `'close'`
unless `emitClose` is set in false.
2016-05-01 00:58:16 -07:00
2020-09-27 07:50:41 -07:00
Once `destroy()` has been called, any further calls will be a no-op and no
further errors except from `_destroy()` may be emitted as `'error'` .
2019-08-18 23:38:35 +02:00
2024-07-26 01:09:23 -07:00
#### `stream.duplexPair([options])`
<!-- YAML
2024-08-19 10:01:30 +02:00
added:
- v22.6.0
- v20.17.0
2024-07-26 01:09:23 -07:00
-->
* `options` {Object} A value to pass to both [`Duplex` ][] constructors,
to set options such as buffering.
* Returns: {Array} of two [`Duplex` ][] instances.
The utility function `duplexPair` returns an Array with two items,
each being a `Duplex` stream connected to the other side:
```js
const [ sideA, sideB ] = duplexPair();
```
Whatever is written to one stream is made readable on the other. It provides
behavior analogous to a network connection, where the data written by the client
becomes readable by the server, and vice-versa.
The Duplex streams are symmetrical; one or the other may be used without any
difference in behavior.
2019-12-24 15:09:29 -08:00
### `stream.finished(stream[, options], callback)`
2021-10-10 21:55:04 -07:00
2018-04-04 16:52:19 +02:00
<!-- YAML
2018-03-02 09:53:46 -08:00
added: v10.0.0
2020-04-25 21:16:51 +02:00
changes:
2023-02-01 00:21:14 +05:30
- version: v19.5.0
pr-url: https://github.com/nodejs/node/pull/46205
description: Added support for `ReadableStream` and `WritableStream` .
2021-03-02 14:22:58 +01:00
- version: v15.11.0
2021-02-13 14:05:04 +02:00
pr-url: https://github.com/nodejs/node/pull/37354
description: The `signal` option was added.
2020-04-25 21:16:51 +02:00
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/32158
description: The `finished(stream, cb)` will wait for the `'close'` event
before invoking the callback. The implementation tries to
detect legacy streams and only apply this behavior to streams
which are expected to emit `'close'` .
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/31545
description: Emitting `'close'` before `'end'` on a `Readable` stream
will cause an `ERR_STREAM_PREMATURE_CLOSE` error.
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/31509
description: Callback will be invoked on streams which have already
finished before the call to `finished(stream, cb)` .
2018-04-04 16:52:19 +02:00
-->
2024-02-14 00:37:42 +03:00
* `stream` {Stream|ReadableStream|WritableStream} A readable and/or writable
stream/webstream.
2018-05-31 16:00:24 +02:00
* `options` {Object}
* `error` {boolean} If set to `false` , then a call to `emit('error', err)` is
2021-02-15 13:21:50 -05:00
not treated as finished. **Default:** `true` .
2018-05-31 16:00:24 +02:00
* `readable` {boolean} When set to `false` , the callback will be called when
the stream ends even though the stream might still be readable.
2021-02-15 13:21:50 -05:00
**Default:** `true` .
2018-05-31 16:00:24 +02:00
* `writable` {boolean} When set to `false` , the callback will be called when
the stream ends even though the stream might still be writable.
2021-02-15 13:21:50 -05:00
**Default:** `true` .
2021-02-13 14:05:04 +02:00
* `signal` {AbortSignal} allows aborting the wait for the stream finish. The
2021-10-10 21:55:04 -07:00
underlying stream will _not_ be aborted if the signal is aborted. The
2021-02-13 14:05:04 +02:00
callback will get called with an `AbortError` . All registered
listeners added by this function will also be removed.
2018-04-04 16:52:19 +02:00
* `callback` {Function} A callback function that takes an optional error
argument.
2019-08-02 08:59:44 +02:00
* Returns: {Function} A cleanup function which removes all registered
listeners.
2018-04-04 16:52:19 +02:00
A function to get notified when a stream is no longer readable, writable
or has experienced an error or a premature close event.
```js
2022-04-20 10:23:41 +02:00
const { finished } = require('node:stream');
2022-06-06 03:56:21 +08:00
const fs = require('node:fs');
2018-04-04 16:52:19 +02:00
const rs = fs.createReadStream('archive.tar');
finished(rs, (err) => {
if (err) {
2018-11-06 08:40:22 +10:00
console.error('Stream failed.', err);
2018-04-04 16:52:19 +02:00
} else {
2018-11-06 08:40:22 +10:00
console.log('Stream is done reading.');
2018-04-04 16:52:19 +02:00
}
});
2019-07-07 20:56:12 +03:00
rs.resume(); // Drain the stream.
2018-04-04 16:52:19 +02:00
```
Especially useful in error handling scenarios where a stream is destroyed
prematurely (like an aborted HTTP request), and will not emit `'end'`
or `'finish'` .
2022-12-15 16:34:23 +01:00
The `finished` API provides [promise version][stream-finished-promise].
2018-04-04 16:52:19 +02:00
2019-08-02 08:59:44 +02:00
`stream.finished()` leaves dangling event listeners (in particular
`'error'` , `'end'` , `'finish'` and `'close'` ) after `callback` has been
invoked. The reason for this is so that unexpected `'error'` events (due to
incorrect stream implementations) do not cause unexpected crashes.
If this is unwanted behavior then the returned cleanup function needs to be
invoked in the callback:
```js
2019-10-15 22:21:46 +08:00
const cleanup = finished(rs, (err) => {
2019-08-02 08:59:44 +02:00
cleanup();
// ...
});
```
2020-02-14 14:25:32 +02:00
### `stream.pipeline(source[, ...transforms], destination, callback)`
2021-10-10 21:55:04 -07:00
2020-07-01 18:22:53 +08:00
### `stream.pipeline(streams, callback)`
2021-10-10 21:55:04 -07:00
2018-04-04 16:52:19 +02:00
<!-- YAML
2018-03-02 09:53:46 -08:00
added: v10.0.0
2020-01-06 15:03:33 +01:00
changes:
2023-04-10 23:02:28 -04:00
- version:
- v19.7.0
- v18.16.0
2023-02-03 01:15:42 +05:30
pr-url: https://github.com/nodejs/node/pull/46307
description: Added support for webstreams.
2022-04-19, Version 18.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) stream: remove thenable support (Robert Nagy)
(https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
fetch (experimental):
An experimental fetch API is available on the global scope by default.
The implementation is based upon https://undici.nodejs.org/#/,
an HTTP/1.1 client written for Node.js by contributors to the project.
Through this addition, the following globals are made available: `fetch`
, `FormData`, `Headers`, `Request`, `Response`.
Disable this API with the `--no-experimental-fetch` command-line flag.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/41811.
HTTP Timeouts:
`server.headersTimeout`, which limits the amount of time the parser will
wait to receive the complete HTTP headers, is now set to `60000` (60
seconds) by default.
`server.requestTimeout`, which sets the timeout value in milliseconds
for receiving the entire request from the client, is now set to `300000`
(5 minutes) by default.
If these timeouts expire, the server responds with status 408 without
forwarding the request to the request listener and then closes the
connection.
Both timeouts must be set to a non-zero value to protect against
potential Denial-of-Service attacks in case the server is deployed
without a reverse proxy in front.
Contributed by Paolo Insogna in https://github.com/nodejs/node/pull/41263.
Test Runner module (experimental):
The `node:test` module facilitates the creation of JavaScript tests that
report results in TAP format. This module is only available under the
`node:` scheme.
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/42325.
Toolchain and Compiler Upgrades:
- Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux
(RHEL) 8 and are compatible with Linux distributions based on glibc
2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
- Prebuilt binaries for macOS now require macOS 10.15 or later.
- For AIX the minimum supported architecture has been raised from Power
7 to Power 8.
Prebuilt binaries for 32-bit Windows will initially not be available due
to issues building the V8 dependency in Node.js. We hope to restore
32-bit Windows binaries for Node.js 18 with a future V8 update.
Node.js does not support running on operating systems that are no longer
supported by their vendor. For operating systems where their vendor has
planned to end support earlier than April 2025, such as Windows 8.1
(January 2023) and Windows Server 2012 R2 (October 2023), support for
Node.js 18 will end at the earlier date.
Full details about the supported toolchains and compilers are documented
in the Node.js `BUILDING.md` file.
Contributed by Richard Lau in https://github.com/nodejs/node/pull/42292,
https://github.com/nodejs/node/pull/42604 and https://github.com/nodejs/node/pull/42659
, and Michaël Zasso in https://github.com/nodejs/node/pull/42105 and
https://github.com/nodejs/node/pull/42666.
V8 10.1:
The V8 engine is updated to version 10.1, which is part of Chromium 101.
Compared to the version included in Node.js 17.9.0, the following new
features are included:
- The `findLast` and `findLastIndex` array methods.
- Improvements to the `Intl.Locale` API.
- The `Intl.supportedValuesOf` function.
- Improved performance of class fields and private class methods (the
initialization of them is now as fast as ordinary property stores).
The data format returned by the serialization API (`v8.serialize(value)`)
has changed, and cannot be deserialized by earlier versions of Node.js.
On the other hand, it is still possible to deserialize the previous
format, as the API is backwards-compatible.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/42657.
Web Streams API (experimental):
Node.js now exposes the experimental implementation of the Web Streams
API on the global scope. This means the following APIs are now globally
available:
- `ReadableStream`, `ReadableStreamDefaultReader`,
`ReadableStreamBYOBReader`, `ReadableStreamBYOBRequest`,
`ReadableByteStreamController`, `ReadableStreamDefaultController`,
`TransformStream`, `TransformStreamDefaultController`, `WritableStream`,
`WritableStreamDefaultWriter`, `WritableStreamDefaultController`,
`ByteLengthQueuingStrategy`, `CountQueuingStrategy`, `TextEncoderStream`,
`TextDecoderStream`, `CompressionStream`, `DecompressionStream`.
Contributed James Snell in https://github.com/nodejs/node/pull/39062,
and Antoine du Hamel in https://github.com/nodejs/node/pull/42225.
Other Notable Changes:
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- doc: add RafaelGSS to collaborators
(RafaelGSS) (https://github.com/nodejs/node/pull/42718)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
Semver-Major Commits:
- (SEMVER-MAJOR) assert,util: compare RegExp.lastIndex while using deep
equal checks
(Ruben Bridgewater) (https://github.com/nodejs/node/pull/41020)
- (SEMVER-MAJOR) buffer: refactor `byteLength` to remove outdated
optimizations
(Rongjian Zhang) (https://github.com/nodejs/node/pull/38545)
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) buffer: graduate Blob from experimental
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) build: make x86 Windows support temporarily
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42666)
- (SEMVER-MAJOR) build: bump macOS deployment target to 10.15
(Richard Lau) (https://github.com/nodejs/node/pull/42292)
- (SEMVER-MAJOR) build: downgrade Windows 8.1 and server 2012 R2 to
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42105)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- (SEMVER-MAJOR) cluster: make `kill` to be just `process.kill`
(Bar Admoni) (https://github.com/nodejs/node/pull/34312)
- (SEMVER-MAJOR) crypto: cleanup validation
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/39841)
- (SEMVER-MAJOR) crypto: prettify othername in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42123)
- (SEMVER-MAJOR) crypto: fix X509Certificate toLegacyObject
(Tobias Nießen) (https://github.com/nodejs/node/pull/42124)
- (SEMVER-MAJOR) crypto: use RFC2253 format in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42002)
- (SEMVER-MAJOR) crypto: change default check(Host|Email) behavior
(Tobias Nießen) (https://github.com/nodejs/node/pull/41600)
- (SEMVER-MAJOR) deps: V8: cherry-pick semver-major commits from 10.2
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 10.1.124.6
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 9.8.177.9
(Michaël Zasso) (https://github.com/nodejs/node/pull/41610)
- (SEMVER-MAJOR) deps: update V8 to 9.7.106.18
(Michaël Zasso) (https://github.com/nodejs/node/pull/40907)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) doc: update minimum glibc requirements for Linux
(Richard Lau) (https://github.com/nodejs/node/pull/42659)
- (SEMVER-MAJOR) doc: update AIX minimum supported arch
(Richard Lau) (https://github.com/nodejs/node/pull/42604)
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) http: refactor headersTimeout and requestTimeout logic
(Paolo Insogna) (https://github.com/nodejs/node/pull/41263)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) lib: enable fetch by default
(Michaël Zasso) (https://github.com/nodejs/node/pull/41811)
- (SEMVER-MAJOR) lib: replace validator and error
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/41678)
- (SEMVER-MAJOR) module,repl: support 'node:'-only core modules
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: disallow some uses of Object.defineProperty()
on process.env
(Himself65) (https://github.com/nodejs/node/pull/28006)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) readline: fix question still called after closed
(Xuguang Mei) (https://github.com/nodejs/node/pull/42464)
- (SEMVER-MAJOR) stream: remove thenable support
(Robert Nagy) (https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) stream: expose web streams globals, remove runtime
experimental warning
(Antoine du Hamel) (https://github.com/nodejs/node/pull/42225)
- (SEMVER-MAJOR) stream: need to cleanup event listeners if last stream
is readable
(Xuguang Mei) (https://github.com/nodejs/node/pull/41954)
- (SEMVER-MAJOR) stream: revert revert `map` spec compliance
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41933)
- (SEMVER-MAJOR) stream: throw invalid arg type from End Of Stream
(Jithil P Ponnan) (https://github.com/nodejs/node/pull/41766)
- (SEMVER-MAJOR) stream: don't emit finish after destroy
(Robert Nagy) (https://github.com/nodejs/node/pull/40852)
- (SEMVER-MAJOR) stream: add errored and closed props
(Robert Nagy) (https://github.com/nodejs/node/pull/40696)
- (SEMVER-MAJOR) test: add initial test module
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) timers: refactor internal classes to ES2015 syntax
(Rabbit) (https://github.com/nodejs/node/pull/37408)
- (SEMVER-MAJOR) tls: represent registeredID numerically always
(Tobias Nießen) (https://github.com/nodejs/node/pull/41561)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
- (SEMVER-MAJOR) url: throw on NULL in IPv6 hostname
(Rich Trott) (https://github.com/nodejs/node/pull/42313)
- (SEMVER-MAJOR) v8: make v8.writeHeapSnapshot() error codes consistent
(Darshan Sen) (https://github.com/nodejs/node/pull/42577)
- (SEMVER-MAJOR) v8: make writeHeapSnapshot throw if fopen fails
(Antonio Román) (https://github.com/nodejs/node/pull/41373)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
PR-URL: https://github.com/nodejs/node/pull/42262
2022-03-08 01:39:47 +00:00
- version: v18.0.0
2022-01-24 19:39:16 +03:30
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the `callback` argument
now throws `ERR_INVALID_ARG_TYPE` instead of
`ERR_INVALID_CALLBACK` .
2020-04-25 21:16:51 +02:00
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/32158
description: The `pipeline(..., cb)` will wait for the `'close'` event
before invoking the callback. The implementation tries to
detect legacy streams and only apply this behavior to streams
which are expected to emit `'close'` .
2020-10-01 20:49:03 +02:00
- version: v13.10.0
pr-url: https://github.com/nodejs/node/pull/31223
description: Add support for async generators.
2020-01-06 15:03:33 +01:00
-->
2023-02-03 01:15:42 +05:30
* `streams` {Stream\[]|Iterable\[]|AsyncIterable\[]|Function\[]|
ReadableStream\[]|WritableStream\[]|TransformStream\[]}
* `source` {Stream|Iterable|AsyncIterable|Function|ReadableStream}
2020-01-06 15:03:33 +01:00
* Returns: {Iterable|AsyncIterable}
2023-02-03 01:15:42 +05:30
* `...transforms` {Stream|Function|TransformStream}
2020-01-06 15:03:33 +01:00
* `source` {AsyncIterable}
* Returns: {AsyncIterable}
2023-02-03 01:15:42 +05:30
* `destination` {Stream|Function|WritableStream}
2020-01-06 15:03:33 +01:00
* `source` {AsyncIterable}
* Returns: {AsyncIterable|Promise}
2018-05-31 12:11:22 +02:00
* `callback` {Function} Called when the pipeline is fully done.
* `err` {Error}
2020-01-06 15:03:33 +01:00
* `val` Resolved value of `Promise` returned by `destination` .
* Returns: {Stream}
2018-04-04 16:52:19 +02:00
2020-01-06 15:03:33 +01:00
A module method to pipe between streams and generators forwarding errors and
properly cleaning up and provide a callback when the pipeline is complete.
2018-04-04 16:52:19 +02:00
```js
2022-04-20 10:23:41 +02:00
const { pipeline } = require('node:stream');
const fs = require('node:fs');
const zlib = require('node:zlib');
2018-04-04 16:52:19 +02:00
// Use the pipeline API to easily pipe a series of streams
// together and get notified when the pipeline is fully done.
// A pipeline to gzip a potentially huge tar file efficiently:
pipeline(
fs.createReadStream('archive.tar'),
zlib.createGzip(),
fs.createWriteStream('archive.tar.gz'),
(err) => {
if (err) {
2018-11-06 08:40:22 +10:00
console.error('Pipeline failed.', err);
2018-04-04 16:52:19 +02:00
} else {
2018-11-06 08:40:22 +10:00
console.log('Pipeline succeeded.');
2018-04-04 16:52:19 +02:00
}
2022-11-17 08:19:12 -05:00
},
2018-04-04 16:52:19 +02:00
);
```
2022-12-15 16:34:23 +01:00
The `pipeline` API provides a [promise version][stream-pipeline-promise].
2021-06-17 22:25:34 +02:00
2019-09-27 22:32:54 +02:00
`stream.pipeline()` will call `stream.destroy(err)` on all streams except:
2021-10-10 21:55:04 -07:00
2019-09-27 22:32:54 +02:00
* `Readable` streams which have emitted `'end'` or `'close'` .
* `Writable` streams which have emitted `'finish'` or `'close'` .
2019-08-02 08:59:44 +02:00
`stream.pipeline()` leaves dangling event listeners on the streams
after the `callback` has been invoked. In the case of reuse of streams after
2022-02-15 18:15:14 +08:00
failure, this can cause event listener leaks and swallowed errors. If the last
stream is readable, dangling event listeners will be removed so that the last
stream can be consumed later.
2019-08-02 08:59:44 +02:00
2022-02-06 06:28:55 -03:00
`stream.pipeline()` closes all the streams when an error is raised.
The `IncomingRequest` usage with `pipeline` could lead to an unexpected behavior
once it would destroy the socket without sending the expected response.
See the example below:
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
const http = require('node:http');
const { pipeline } = require('node:stream');
2022-02-06 06:28:55 -03:00
const server = http.createServer((req, res) => {
const fileStream = fs.createReadStream('./fileNotExist.txt');
pipeline(fileStream, res, (err) => {
if (err) {
console.log(err); // No such file
// this message can't be sent once `pipeline` already destroyed the socket
return res.end('error!!!');
}
});
});
```
2021-06-18 08:08:50 +02:00
### `stream.compose(...streams)`
2021-10-10 21:55:04 -07:00
2021-06-18 08:08:50 +02:00
<!-- YAML
2021-09-06 09:44:13 +02:00
added: v16.9.0
2023-02-27 14:20:39 +05:30
changes:
2023-11-12 08:52:51 +01:00
- version:
- v21.1.0
- v20.10.0
2023-10-18 20:00:05 -05:00
pr-url: https://github.com/nodejs/node/pull/50187
description: Added support for stream class.
2023-04-10 23:02:28 -04:00
- version:
- v19.8.0
- v18.16.0
2023-02-27 14:20:39 +05:30
pr-url: https://github.com/nodejs/node/pull/46675
description: Added support for webstreams.
2021-06-18 08:08:50 +02:00
-->
2021-07-23 07:46:28 +02:00
> Stability: 1 - `stream.compose` is experimental.
2023-02-27 14:20:39 +05:30
* `streams` {Stream\[]|Iterable\[]|AsyncIterable\[]|Function\[]|
2023-10-18 20:00:05 -05:00
ReadableStream\[]|WritableStream\[]|TransformStream\[]|Duplex\[]|Function}
2021-06-18 08:08:50 +02:00
* Returns: {stream.Duplex}
Combines two or more streams into a `Duplex` stream that writes to the
first stream and reads from the last. Each provided stream is piped into
the next, using `stream.pipeline` . If any of the streams error then all
are destroyed, including the outer `Duplex` stream.
Because `stream.compose` returns a new stream that in turn can (and
should) be piped into other streams, it enables composition. In contrast,
when passing streams to `stream.pipeline` , typically the first stream is
a readable stream and the last a writable stream, forming a closed
circuit.
2021-07-19 08:19:03 +02:00
If passed a `Function` it must be a factory method taking a `source`
`Iterable` .
2021-06-18 08:08:50 +02:00
```mjs
2022-04-20 10:23:41 +02:00
import { compose, Transform } from 'node:stream';
2021-06-18 08:08:50 +02:00
const removeSpaces = new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).replace(' ', ''));
2022-11-17 08:19:12 -05:00
},
2021-06-18 08:08:50 +02:00
});
2021-07-19 08:19:03 +02:00
async function* toUpper(source) {
for await (const chunk of source) {
yield String(chunk).toUpperCase();
2021-06-18 08:08:50 +02:00
}
2021-07-19 08:19:03 +02:00
}
2021-06-18 08:08:50 +02:00
let res = '';
for await (const buf of compose(removeSpaces, toUpper).end('hello world')) {
res += buf;
}
console.log(res); // prints 'HELLOWORLD'
```
2021-07-19 08:19:03 +02:00
`stream.compose` can be used to convert async iterables, generators and
functions into streams.
* `AsyncIterable` converts into a readable `Duplex` . Cannot yield
`null` .
* `AsyncGeneratorFunction` converts into a readable/writable transform `Duplex` .
Must take a source `AsyncIterable` as first parameter. Cannot yield
`null` .
* `AsyncFunction` converts into a writable `Duplex` . Must return
either `null` or `undefined` .
```mjs
2022-04-20 10:23:41 +02:00
import { compose } from 'node:stream';
import { finished } from 'node:stream/promises';
2021-07-19 08:19:03 +02:00
// Convert AsyncIterable into readable Duplex.
const s1 = compose(async function*() {
yield 'Hello';
yield 'World';
}());
// Convert AsyncGenerator into transform Duplex.
const s2 = compose(async function*(source) {
for await (const chunk of source) {
yield String(chunk).toUpperCase();
}
});
let res = '';
// Convert AsyncFunction into writable Duplex.
const s3 = compose(async function(source) {
for await (const chunk of source) {
res += chunk;
}
});
await finished(compose(s1, s2, s3));
console.log(res); // prints 'HELLOWORLD'
```
2022-10-31 15:57:02 +02:00
See [`readable.compose(stream)` ][] for `stream.compose` as operator.
2021-09-04 12:06:59 +05:30
### `stream.Readable.from(iterable[, options])`
2021-10-10 21:55:04 -07:00
2019-07-15 12:15:20 +02:00
<!-- YAML
2020-04-03 12:09:45 +01:00
added:
- v12.3.0
- v10.17.0
2019-07-15 12:15:20 +02:00
-->
2019-05-12 19:00:53 +02:00
* `iterable` {Iterable} Object implementing the `Symbol.asyncIterator` or
2020-04-16 06:07:47 +05:30
`Symbol.iterator` iterable protocol. Emits an 'error' event if a null
2021-10-10 21:55:04 -07:00
value is passed.
2019-05-12 19:00:53 +02:00
* `options` {Object} Options provided to `new stream.Readable([options])` .
By default, `Readable.from()` will set `options.objectMode` to `true` , unless
this is explicitly opted out by setting `options.objectMode` to `false` .
2019-07-07 20:56:12 +03:00
* Returns: {stream.Readable}
2019-05-12 19:00:53 +02:00
2020-06-14 14:49:34 -07:00
A utility method for creating readable streams out of iterators.
2019-05-12 19:00:53 +02:00
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2019-05-12 19:00:53 +02:00
async function * generate() {
yield 'hello';
yield 'streams';
}
const readable = Readable.from(generate());
readable.on('data', (chunk) => {
console.log(chunk);
});
```
2019-12-12 14:40:50 +01:00
Calling `Readable.from(string)` or `Readable.from(buffer)` will not have
the strings or buffers be iterated to match the other streams semantics
for performance reasons.
2023-01-11 09:16:27 +01:00
If an `Iterable` object containing promises is passed as an argument,
it might result in unhandled rejection.
```js
const { Readable } = require('node:stream');
Readable.from([
new Promise((resolve) => setTimeout(resolve('1'), 1500)),
new Promise((_, reject) => setTimeout(reject(new Error('2')), 1000)), // Unhandled rejection
]);
```
2021-06-23 22:24:19 -07:00
### `stream.Readable.fromWeb(readableStream[, options])`
2021-10-10 21:55:04 -07:00
2021-06-23 22:24:19 -07:00
<!-- YAML
2021-11-15 20:47:52 +01:00
added: v17.0.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-06-23 22:24:19 -07:00
-->
* `readableStream` {ReadableStream}
* `options` {Object}
* `encoding` {string}
* `highWaterMark` {number}
2021-10-05 14:06:19 -05:00
* `objectMode` {boolean}
2021-06-23 22:24:19 -07:00
* `signal` {AbortSignal}
* Returns: {stream.Readable}
2021-08-02 13:08:32 +02:00
### `stream.Readable.isDisturbed(stream)`
2021-10-10 21:55:04 -07:00
2021-08-02 13:08:32 +02:00
<!-- YAML
2021-08-25 09:01:17 +02:00
added: v16.8.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-08-02 13:08:32 +02:00
-->
* `stream` {stream.Readable|ReadableStream}
* Returns: `boolean`
Returns whether the stream has been read from or cancelled.
2021-12-09 09:15:12 +01:00
### `stream.isErrored(stream)`
<!-- YAML
2022-02-01 00:34:51 -05:00
added:
- v17.3.0
- v16.14.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-12-09 09:15:12 +01:00
-->
* `stream` {Readable|Writable|Duplex|WritableStream|ReadableStream}
* Returns: {boolean}
Returns whether the stream has encountered an error.
2021-12-16 14:32:02 +01:00
### `stream.isReadable(stream)`
<!-- YAML
2022-02-01 00:34:51 -05:00
added:
- v17.4.0
- v16.14.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-12-16 14:32:02 +01:00
-->
* `stream` {Readable|Duplex|ReadableStream}
* Returns: {boolean}
Returns whether the stream is readable.
2022-07-13 07:14:38 -03:00
### `stream.Readable.toWeb(streamReadable[, options])`
2021-10-10 21:55:04 -07:00
2021-06-23 22:24:19 -07:00
<!-- YAML
2021-10-19, Version 17.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup`
options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- doc: deprecate (doc-only) http abort related
(dr-js) [https://github.com/nodejs/node/pull/36670]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
OpenSSL 3.0:
Node.js now includes OpenSSL 3.0, specifically https://github.com/quictls/openssl
which provides QUIC support.
While OpenSSL 3.0 APIs should be mostly compatible with those provided
by OpenSSL 1.1.1, we do anticipate some ecosystem impact due to
tightened restrictions on the allowed algorithms and key sizes.
If you hit an `ERR_OSSL_EVP_UNSUPPORTED` error in your application with
Node.js 17, it’s likely that your application or a module you’re using
is attempting to use an algorithm or key size which is no longer allowed
by default with OpenSSL 3.0. A command-line option,
`--openssl-legacy-provider`, has been added to revert to the legacy
provider as a temporary workaround for these tightened restrictions.
For details about all the features in
OpenSSL 3.0 please see https://www.openssl.org/blog/blog/2021/09/07/OpenSSL3.Final.
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
Contributed in https://github.com/nodejs/node/pull/38512, https://github.com/nodejs/node/pull/40478
V8 9.5:
The V8 JavaScript engine is updated to V8 9.5. This release comes with
additional supported types for the `Intl.DisplayNames` API and Extended
`timeZoneName` options in the `Intl.DateTimeFormat` API. You can read
more details in the V8 9.5 release post https://v8.dev/blog/v8-release-95.
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
Readline Promise API:
The `readline` module provides an interface for reading data from a
Readable stream (such as `process.stdin`) one line at a time.
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
Other Notable Changes:
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) errors: print Node.js version on fatal exceptions that
cause exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- deps: upgrade npm to 8.1.0
(npm team) [https://github.com/nodejs/node/pull/40463]
- (SEMVER-MINOR) fs: add FileHandle.prototype.readableWebStream()
(James M Snell) [https://github.com/nodejs/node/pull/39331]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
Semver-Major Commits:
- (SEMVER-MAJOR) build: compile with C++17 (MSVC)
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) build: compile with --gnu++17
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) deps: update V8 to 9.5.172.19
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
- (SEMVER-MAJOR) deps,test,src,doc,tools: update to OpenSSL 3.0
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
- (SEMVER-MAJOR) dgram: tighten `address` validation in `socket.send`
(Voltrex) [https://github.com/nodejs/node/pull/39190]
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup` options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) doc: update minimum supported FreeBSD to 12.2
(Michaël Zasso) [https://github.com/nodejs/node/pull/40179]
- (SEMVER-MAJOR) errors: disp ver on fatal except that causes exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- (SEMVER-MAJOR) fs: fix rmsync error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38684]
- (SEMVER-MAJOR) fs: aggregate errors in fsPromises to avoid error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38259]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
- (SEMVER-MAJOR) readline: validate `AbortSignal`s and remove unused event listeners
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: introduce promise-based API
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: refactor `Interface` to ES2015 class
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) src: allow CAP\_NET\_BIND\_SERVICE in SafeGetenv
(Daniel Bevenius) [https://github.com/nodejs/node/pull/37727]
- (SEMVER-MAJOR) src: return Maybe from a couple of functions
(Darshan Sen) [https://github.com/nodejs/node/pull/39603]
- (SEMVER-MAJOR) src: allow custom PageAllocator in NodePlatform
(Shelley Vohr) [https://github.com/nodejs/node/pull/38362]
- (SEMVER-MAJOR) stream: fix highwatermark threshold and add the missing error
(Rongjian Zhang) [https://github.com/nodejs/node/pull/38700]
- (SEMVER-MAJOR) stream: don't emit 'data' after 'error' or 'close'
(Robert Nagy) [https://github.com/nodejs/node/pull/39639]
- (SEMVER-MAJOR) stream: do not emit `end` on readable error
(Szymon Marczak) [https://github.com/nodejs/node/pull/39607]
- (SEMVER-MAJOR) stream: forward errored to callback
(Robert Nagy) [https://github.com/nodejs/node/pull/39364]
- (SEMVER-MAJOR) stream: destroy readable on read error
(Robert Nagy) [https://github.com/nodejs/node/pull/39342]
- (SEMVER-MAJOR) stream: validate abort signal
(Robert Nagy) [https://github.com/nodejs/node/pull/39346]
- (SEMVER-MAJOR) stream: unify stream utils
(Robert Nagy) [https://github.com/nodejs/node/pull/39294]
- (SEMVER-MAJOR) stream: throw on premature close in Readable\
(Darshan Sen) [https://github.com/nodejs/node/pull/39117]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
- (SEMVER-MAJOR) stream: error Duplex write/read if not writable/readable
(Robert Nagy) [https://github.com/nodejs/node/pull/34385]
- (SEMVER-MAJOR) stream: bypass legacy destroy for pipeline and async iteration
(Robert Nagy) [https://github.com/nodejs/node/pull/38505]
- (SEMVER-MAJOR) url: throw invalid this on detached accessors
(James M Snell) [https://github.com/nodejs/node/pull/39752]
- (SEMVER-MAJOR) url: forbid certain confusable changes from being introduced by toASCII
(Timothy Gu) [https://github.com/nodejs/node/pull/38631]
PR-URL: https://github.com/nodejs/node/pull/40119
2021-09-15 01:55:37 +01:00
added: v17.0.0
2025-02-08 03:05:04 +08:00
changes:
2025-03-16 17:27:47 -07:00
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2025-02-08 03:05:04 +08:00
- version:
- v18.7.0
pr-url: https://github.com/nodejs/node/pull/43515
description: include strategy options on Readable.
2021-06-23 22:24:19 -07:00
-->
* `streamReadable` {stream.Readable}
2022-07-13 07:14:38 -03:00
* `options` {Object}
* `strategy` {Object}
2023-02-23 00:26:50 +05:30
* `highWaterMark` {number} The maximum internal queue size (of the created
`ReadableStream` ) before backpressure is applied in reading from the given
`stream.Readable` . If no value is provided, it will be taken from the
given `stream.Readable` .
* `size` {Function} A function that size of the given chunk of data.
If no value is provided, the size will be `1` for all the chunks.
* `chunk` {any}
* Returns: {number}
2021-06-23 22:24:19 -07:00
* Returns: {ReadableStream}
### `stream.Writable.fromWeb(writableStream[, options])`
2021-10-10 21:55:04 -07:00
2021-06-23 22:24:19 -07:00
<!-- YAML
2021-10-19, Version 17.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup`
options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- doc: deprecate (doc-only) http abort related
(dr-js) [https://github.com/nodejs/node/pull/36670]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
OpenSSL 3.0:
Node.js now includes OpenSSL 3.0, specifically https://github.com/quictls/openssl
which provides QUIC support.
While OpenSSL 3.0 APIs should be mostly compatible with those provided
by OpenSSL 1.1.1, we do anticipate some ecosystem impact due to
tightened restrictions on the allowed algorithms and key sizes.
If you hit an `ERR_OSSL_EVP_UNSUPPORTED` error in your application with
Node.js 17, it’s likely that your application or a module you’re using
is attempting to use an algorithm or key size which is no longer allowed
by default with OpenSSL 3.0. A command-line option,
`--openssl-legacy-provider`, has been added to revert to the legacy
provider as a temporary workaround for these tightened restrictions.
For details about all the features in
OpenSSL 3.0 please see https://www.openssl.org/blog/blog/2021/09/07/OpenSSL3.Final.
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
Contributed in https://github.com/nodejs/node/pull/38512, https://github.com/nodejs/node/pull/40478
V8 9.5:
The V8 JavaScript engine is updated to V8 9.5. This release comes with
additional supported types for the `Intl.DisplayNames` API and Extended
`timeZoneName` options in the `Intl.DateTimeFormat` API. You can read
more details in the V8 9.5 release post https://v8.dev/blog/v8-release-95.
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
Readline Promise API:
The `readline` module provides an interface for reading data from a
Readable stream (such as `process.stdin`) one line at a time.
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
Other Notable Changes:
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) errors: print Node.js version on fatal exceptions that
cause exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- deps: upgrade npm to 8.1.0
(npm team) [https://github.com/nodejs/node/pull/40463]
- (SEMVER-MINOR) fs: add FileHandle.prototype.readableWebStream()
(James M Snell) [https://github.com/nodejs/node/pull/39331]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
Semver-Major Commits:
- (SEMVER-MAJOR) build: compile with C++17 (MSVC)
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) build: compile with --gnu++17
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) deps: update V8 to 9.5.172.19
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
- (SEMVER-MAJOR) deps,test,src,doc,tools: update to OpenSSL 3.0
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
- (SEMVER-MAJOR) dgram: tighten `address` validation in `socket.send`
(Voltrex) [https://github.com/nodejs/node/pull/39190]
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup` options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) doc: update minimum supported FreeBSD to 12.2
(Michaël Zasso) [https://github.com/nodejs/node/pull/40179]
- (SEMVER-MAJOR) errors: disp ver on fatal except that causes exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- (SEMVER-MAJOR) fs: fix rmsync error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38684]
- (SEMVER-MAJOR) fs: aggregate errors in fsPromises to avoid error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38259]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
- (SEMVER-MAJOR) readline: validate `AbortSignal`s and remove unused event listeners
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: introduce promise-based API
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: refactor `Interface` to ES2015 class
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) src: allow CAP\_NET\_BIND\_SERVICE in SafeGetenv
(Daniel Bevenius) [https://github.com/nodejs/node/pull/37727]
- (SEMVER-MAJOR) src: return Maybe from a couple of functions
(Darshan Sen) [https://github.com/nodejs/node/pull/39603]
- (SEMVER-MAJOR) src: allow custom PageAllocator in NodePlatform
(Shelley Vohr) [https://github.com/nodejs/node/pull/38362]
- (SEMVER-MAJOR) stream: fix highwatermark threshold and add the missing error
(Rongjian Zhang) [https://github.com/nodejs/node/pull/38700]
- (SEMVER-MAJOR) stream: don't emit 'data' after 'error' or 'close'
(Robert Nagy) [https://github.com/nodejs/node/pull/39639]
- (SEMVER-MAJOR) stream: do not emit `end` on readable error
(Szymon Marczak) [https://github.com/nodejs/node/pull/39607]
- (SEMVER-MAJOR) stream: forward errored to callback
(Robert Nagy) [https://github.com/nodejs/node/pull/39364]
- (SEMVER-MAJOR) stream: destroy readable on read error
(Robert Nagy) [https://github.com/nodejs/node/pull/39342]
- (SEMVER-MAJOR) stream: validate abort signal
(Robert Nagy) [https://github.com/nodejs/node/pull/39346]
- (SEMVER-MAJOR) stream: unify stream utils
(Robert Nagy) [https://github.com/nodejs/node/pull/39294]
- (SEMVER-MAJOR) stream: throw on premature close in Readable\
(Darshan Sen) [https://github.com/nodejs/node/pull/39117]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
- (SEMVER-MAJOR) stream: error Duplex write/read if not writable/readable
(Robert Nagy) [https://github.com/nodejs/node/pull/34385]
- (SEMVER-MAJOR) stream: bypass legacy destroy for pipeline and async iteration
(Robert Nagy) [https://github.com/nodejs/node/pull/38505]
- (SEMVER-MAJOR) url: throw invalid this on detached accessors
(James M Snell) [https://github.com/nodejs/node/pull/39752]
- (SEMVER-MAJOR) url: forbid certain confusable changes from being introduced by toASCII
(Timothy Gu) [https://github.com/nodejs/node/pull/38631]
PR-URL: https://github.com/nodejs/node/pull/40119
2021-09-15 01:55:37 +01:00
added: v17.0.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-06-23 22:24:19 -07:00
-->
* `writableStream` {WritableStream}
* `options` {Object}
* `decodeStrings` {boolean}
* `highWaterMark` {number}
* `objectMode` {boolean}
* `signal` {AbortSignal}
* Returns: {stream.Writable}
### `stream.Writable.toWeb(streamWritable)`
2021-10-10 21:55:04 -07:00
2021-06-23 22:24:19 -07:00
<!-- YAML
2021-10-19, Version 17.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup`
options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- doc: deprecate (doc-only) http abort related
(dr-js) [https://github.com/nodejs/node/pull/36670]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
OpenSSL 3.0:
Node.js now includes OpenSSL 3.0, specifically https://github.com/quictls/openssl
which provides QUIC support.
While OpenSSL 3.0 APIs should be mostly compatible with those provided
by OpenSSL 1.1.1, we do anticipate some ecosystem impact due to
tightened restrictions on the allowed algorithms and key sizes.
If you hit an `ERR_OSSL_EVP_UNSUPPORTED` error in your application with
Node.js 17, it’s likely that your application or a module you’re using
is attempting to use an algorithm or key size which is no longer allowed
by default with OpenSSL 3.0. A command-line option,
`--openssl-legacy-provider`, has been added to revert to the legacy
provider as a temporary workaround for these tightened restrictions.
For details about all the features in
OpenSSL 3.0 please see https://www.openssl.org/blog/blog/2021/09/07/OpenSSL3.Final.
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
Contributed in https://github.com/nodejs/node/pull/38512, https://github.com/nodejs/node/pull/40478
V8 9.5:
The V8 JavaScript engine is updated to V8 9.5. This release comes with
additional supported types for the `Intl.DisplayNames` API and Extended
`timeZoneName` options in the `Intl.DateTimeFormat` API. You can read
more details in the V8 9.5 release post https://v8.dev/blog/v8-release-95.
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
Readline Promise API:
The `readline` module provides an interface for reading data from a
Readable stream (such as `process.stdin`) one line at a time.
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
Other Notable Changes:
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) errors: print Node.js version on fatal exceptions that
cause exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- deps: upgrade npm to 8.1.0
(npm team) [https://github.com/nodejs/node/pull/40463]
- (SEMVER-MINOR) fs: add FileHandle.prototype.readableWebStream()
(James M Snell) [https://github.com/nodejs/node/pull/39331]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
Semver-Major Commits:
- (SEMVER-MAJOR) build: compile with C++17 (MSVC)
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) build: compile with --gnu++17
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) deps: update V8 to 9.5.172.19
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
- (SEMVER-MAJOR) deps,test,src,doc,tools: update to OpenSSL 3.0
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
- (SEMVER-MAJOR) dgram: tighten `address` validation in `socket.send`
(Voltrex) [https://github.com/nodejs/node/pull/39190]
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup` options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) doc: update minimum supported FreeBSD to 12.2
(Michaël Zasso) [https://github.com/nodejs/node/pull/40179]
- (SEMVER-MAJOR) errors: disp ver on fatal except that causes exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- (SEMVER-MAJOR) fs: fix rmsync error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38684]
- (SEMVER-MAJOR) fs: aggregate errors in fsPromises to avoid error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38259]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
- (SEMVER-MAJOR) readline: validate `AbortSignal`s and remove unused event listeners
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: introduce promise-based API
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: refactor `Interface` to ES2015 class
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) src: allow CAP\_NET\_BIND\_SERVICE in SafeGetenv
(Daniel Bevenius) [https://github.com/nodejs/node/pull/37727]
- (SEMVER-MAJOR) src: return Maybe from a couple of functions
(Darshan Sen) [https://github.com/nodejs/node/pull/39603]
- (SEMVER-MAJOR) src: allow custom PageAllocator in NodePlatform
(Shelley Vohr) [https://github.com/nodejs/node/pull/38362]
- (SEMVER-MAJOR) stream: fix highwatermark threshold and add the missing error
(Rongjian Zhang) [https://github.com/nodejs/node/pull/38700]
- (SEMVER-MAJOR) stream: don't emit 'data' after 'error' or 'close'
(Robert Nagy) [https://github.com/nodejs/node/pull/39639]
- (SEMVER-MAJOR) stream: do not emit `end` on readable error
(Szymon Marczak) [https://github.com/nodejs/node/pull/39607]
- (SEMVER-MAJOR) stream: forward errored to callback
(Robert Nagy) [https://github.com/nodejs/node/pull/39364]
- (SEMVER-MAJOR) stream: destroy readable on read error
(Robert Nagy) [https://github.com/nodejs/node/pull/39342]
- (SEMVER-MAJOR) stream: validate abort signal
(Robert Nagy) [https://github.com/nodejs/node/pull/39346]
- (SEMVER-MAJOR) stream: unify stream utils
(Robert Nagy) [https://github.com/nodejs/node/pull/39294]
- (SEMVER-MAJOR) stream: throw on premature close in Readable\
(Darshan Sen) [https://github.com/nodejs/node/pull/39117]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
- (SEMVER-MAJOR) stream: error Duplex write/read if not writable/readable
(Robert Nagy) [https://github.com/nodejs/node/pull/34385]
- (SEMVER-MAJOR) stream: bypass legacy destroy for pipeline and async iteration
(Robert Nagy) [https://github.com/nodejs/node/pull/38505]
- (SEMVER-MAJOR) url: throw invalid this on detached accessors
(James M Snell) [https://github.com/nodejs/node/pull/39752]
- (SEMVER-MAJOR) url: forbid certain confusable changes from being introduced by toASCII
(Timothy Gu) [https://github.com/nodejs/node/pull/38631]
PR-URL: https://github.com/nodejs/node/pull/40119
2021-09-15 01:55:37 +01:00
added: v17.0.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-06-23 22:24:19 -07:00
-->
* `streamWritable` {stream.Writable}
* Returns: {WritableStream}
2021-07-25 20:01:43 +02:00
### `stream.Duplex.from(src)`
2021-10-10 21:55:04 -07:00
2021-07-25 20:01:43 +02:00
<!-- YAML
2021-08-25 09:01:17 +02:00
added: v16.8.0
2023-02-01 00:21:14 +05:30
changes:
2023-07-10 08:12:52 -04:00
- version:
- v19.5.0
- v18.17.0
2023-02-01 00:21:14 +05:30
pr-url: https://github.com/nodejs/node/pull/46190
description: The `src` argument can now be a `ReadableStream` or
`WritableStream` .
2021-07-25 20:01:43 +02:00
-->
* `src` {Stream|Blob|ArrayBuffer|string|Iterable|AsyncIterable|
2023-02-01 00:21:14 +05:30
AsyncGeneratorFunction|AsyncFunction|Promise|Object|
ReadableStream|WritableStream}
2021-07-25 20:01:43 +02:00
A utility method for creating duplex streams.
* `Stream` converts writable stream into writable `Duplex` and readable stream
to `Duplex` .
* `Blob` converts into readable `Duplex` .
* `string` converts into readable `Duplex` .
* `ArrayBuffer` converts into readable `Duplex` .
* `AsyncIterable` converts into a readable `Duplex` . Cannot yield
`null` .
* `AsyncGeneratorFunction` converts into a readable/writable transform
`Duplex` . Must take a source `AsyncIterable` as first parameter. Cannot yield
`null` .
* `AsyncFunction` converts into a writable `Duplex` . Must return
either `null` or `undefined`
* `Object ({ writable, readable })` converts `readable` and
`writable` into `Stream` and then combines them into `Duplex` where the
`Duplex` will write to the `writable` and read from the `readable` .
* `Promise` converts into readable `Duplex` . Value `null` is ignored.
2023-02-01 00:21:14 +05:30
* `ReadableStream` converts into readable `Duplex` .
* `WritableStream` converts into writable `Duplex` .
2021-07-25 20:01:43 +02:00
* Returns: {stream.Duplex}
2023-01-11 09:16:27 +01:00
If an `Iterable` object containing promises is passed as an argument,
it might result in unhandled rejection.
```js
const { Duplex } = require('node:stream');
Duplex.from([
new Promise((resolve) => setTimeout(resolve('1'), 1500)),
new Promise((_, reject) => setTimeout(reject(new Error('2')), 1000)), // Unhandled rejection
]);
```
2021-06-23 22:24:19 -07:00
### `stream.Duplex.fromWeb(pair[, options])`
2021-10-10 21:55:04 -07:00
2021-06-23 22:24:19 -07:00
<!-- YAML
2021-10-19, Version 17.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup`
options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- doc: deprecate (doc-only) http abort related
(dr-js) [https://github.com/nodejs/node/pull/36670]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
OpenSSL 3.0:
Node.js now includes OpenSSL 3.0, specifically https://github.com/quictls/openssl
which provides QUIC support.
While OpenSSL 3.0 APIs should be mostly compatible with those provided
by OpenSSL 1.1.1, we do anticipate some ecosystem impact due to
tightened restrictions on the allowed algorithms and key sizes.
If you hit an `ERR_OSSL_EVP_UNSUPPORTED` error in your application with
Node.js 17, it’s likely that your application or a module you’re using
is attempting to use an algorithm or key size which is no longer allowed
by default with OpenSSL 3.0. A command-line option,
`--openssl-legacy-provider`, has been added to revert to the legacy
provider as a temporary workaround for these tightened restrictions.
For details about all the features in
OpenSSL 3.0 please see https://www.openssl.org/blog/blog/2021/09/07/OpenSSL3.Final.
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
Contributed in https://github.com/nodejs/node/pull/38512, https://github.com/nodejs/node/pull/40478
V8 9.5:
The V8 JavaScript engine is updated to V8 9.5. This release comes with
additional supported types for the `Intl.DisplayNames` API and Extended
`timeZoneName` options in the `Intl.DateTimeFormat` API. You can read
more details in the V8 9.5 release post https://v8.dev/blog/v8-release-95.
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
Readline Promise API:
The `readline` module provides an interface for reading data from a
Readable stream (such as `process.stdin`) one line at a time.
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
Other Notable Changes:
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) errors: print Node.js version on fatal exceptions that
cause exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- deps: upgrade npm to 8.1.0
(npm team) [https://github.com/nodejs/node/pull/40463]
- (SEMVER-MINOR) fs: add FileHandle.prototype.readableWebStream()
(James M Snell) [https://github.com/nodejs/node/pull/39331]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
Semver-Major Commits:
- (SEMVER-MAJOR) build: compile with C++17 (MSVC)
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) build: compile with --gnu++17
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) deps: update V8 to 9.5.172.19
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
- (SEMVER-MAJOR) deps,test,src,doc,tools: update to OpenSSL 3.0
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
- (SEMVER-MAJOR) dgram: tighten `address` validation in `socket.send`
(Voltrex) [https://github.com/nodejs/node/pull/39190]
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup` options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) doc: update minimum supported FreeBSD to 12.2
(Michaël Zasso) [https://github.com/nodejs/node/pull/40179]
- (SEMVER-MAJOR) errors: disp ver on fatal except that causes exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- (SEMVER-MAJOR) fs: fix rmsync error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38684]
- (SEMVER-MAJOR) fs: aggregate errors in fsPromises to avoid error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38259]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
- (SEMVER-MAJOR) readline: validate `AbortSignal`s and remove unused event listeners
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: introduce promise-based API
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: refactor `Interface` to ES2015 class
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) src: allow CAP\_NET\_BIND\_SERVICE in SafeGetenv
(Daniel Bevenius) [https://github.com/nodejs/node/pull/37727]
- (SEMVER-MAJOR) src: return Maybe from a couple of functions
(Darshan Sen) [https://github.com/nodejs/node/pull/39603]
- (SEMVER-MAJOR) src: allow custom PageAllocator in NodePlatform
(Shelley Vohr) [https://github.com/nodejs/node/pull/38362]
- (SEMVER-MAJOR) stream: fix highwatermark threshold and add the missing error
(Rongjian Zhang) [https://github.com/nodejs/node/pull/38700]
- (SEMVER-MAJOR) stream: don't emit 'data' after 'error' or 'close'
(Robert Nagy) [https://github.com/nodejs/node/pull/39639]
- (SEMVER-MAJOR) stream: do not emit `end` on readable error
(Szymon Marczak) [https://github.com/nodejs/node/pull/39607]
- (SEMVER-MAJOR) stream: forward errored to callback
(Robert Nagy) [https://github.com/nodejs/node/pull/39364]
- (SEMVER-MAJOR) stream: destroy readable on read error
(Robert Nagy) [https://github.com/nodejs/node/pull/39342]
- (SEMVER-MAJOR) stream: validate abort signal
(Robert Nagy) [https://github.com/nodejs/node/pull/39346]
- (SEMVER-MAJOR) stream: unify stream utils
(Robert Nagy) [https://github.com/nodejs/node/pull/39294]
- (SEMVER-MAJOR) stream: throw on premature close in Readable\
(Darshan Sen) [https://github.com/nodejs/node/pull/39117]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
- (SEMVER-MAJOR) stream: error Duplex write/read if not writable/readable
(Robert Nagy) [https://github.com/nodejs/node/pull/34385]
- (SEMVER-MAJOR) stream: bypass legacy destroy for pipeline and async iteration
(Robert Nagy) [https://github.com/nodejs/node/pull/38505]
- (SEMVER-MAJOR) url: throw invalid this on detached accessors
(James M Snell) [https://github.com/nodejs/node/pull/39752]
- (SEMVER-MAJOR) url: forbid certain confusable changes from being introduced by toASCII
(Timothy Gu) [https://github.com/nodejs/node/pull/38631]
PR-URL: https://github.com/nodejs/node/pull/40119
2021-09-15 01:55:37 +01:00
added: v17.0.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-06-23 22:24:19 -07:00
-->
* `pair` {Object}
* `readable` {ReadableStream}
* `writable` {WritableStream}
* `options` {Object}
* `allowHalfOpen` {boolean}
* `decodeStrings` {boolean}
* `encoding` {string}
* `highWaterMark` {number}
* `objectMode` {boolean}
* `signal` {AbortSignal}
* Returns: {stream.Duplex}
2022-04-28 04:53:52 -03:00
```mjs
import { Duplex } from 'node:stream';
import {
ReadableStream,
2022-11-17 08:19:12 -05:00
WritableStream,
2022-04-28 04:53:52 -03:00
} from 'node:stream/web';
const readable = new ReadableStream({
start(controller) {
controller.enqueue('world');
},
});
const writable = new WritableStream({
write(chunk) {
console.log('writable', chunk);
2022-11-17 08:19:12 -05:00
},
2022-04-28 04:53:52 -03:00
});
const pair = {
readable,
2022-11-17 08:19:12 -05:00
writable,
2022-04-28 04:53:52 -03:00
};
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
duplex.write('hello');
for await (const chunk of duplex) {
console.log('readable', chunk);
}
```
```cjs
const { Duplex } = require('node:stream');
const {
ReadableStream,
2022-11-17 08:19:12 -05:00
WritableStream,
2022-04-28 04:53:52 -03:00
} = require('node:stream/web');
const readable = new ReadableStream({
start(controller) {
controller.enqueue('world');
},
});
const writable = new WritableStream({
write(chunk) {
console.log('writable', chunk);
2022-11-17 08:19:12 -05:00
},
2022-04-28 04:53:52 -03:00
});
const pair = {
readable,
2022-11-17 08:19:12 -05:00
writable,
2022-04-28 04:53:52 -03:00
};
const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
duplex.write('hello');
duplex.once('readable', () => console.log('readable', duplex.read()));
```
2021-06-23 22:24:19 -07:00
### `stream.Duplex.toWeb(streamDuplex)`
2021-10-10 21:55:04 -07:00
2021-06-23 22:24:19 -07:00
<!-- YAML
2021-10-19, Version 17.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup`
options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- doc: deprecate (doc-only) http abort related
(dr-js) [https://github.com/nodejs/node/pull/36670]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
OpenSSL 3.0:
Node.js now includes OpenSSL 3.0, specifically https://github.com/quictls/openssl
which provides QUIC support.
While OpenSSL 3.0 APIs should be mostly compatible with those provided
by OpenSSL 1.1.1, we do anticipate some ecosystem impact due to
tightened restrictions on the allowed algorithms and key sizes.
If you hit an `ERR_OSSL_EVP_UNSUPPORTED` error in your application with
Node.js 17, it’s likely that your application or a module you’re using
is attempting to use an algorithm or key size which is no longer allowed
by default with OpenSSL 3.0. A command-line option,
`--openssl-legacy-provider`, has been added to revert to the legacy
provider as a temporary workaround for these tightened restrictions.
For details about all the features in
OpenSSL 3.0 please see https://www.openssl.org/blog/blog/2021/09/07/OpenSSL3.Final.
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
Contributed in https://github.com/nodejs/node/pull/38512, https://github.com/nodejs/node/pull/40478
V8 9.5:
The V8 JavaScript engine is updated to V8 9.5. This release comes with
additional supported types for the `Intl.DisplayNames` API and Extended
`timeZoneName` options in the `Intl.DateTimeFormat` API. You can read
more details in the V8 9.5 release post https://v8.dev/blog/v8-release-95.
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
Readline Promise API:
The `readline` module provides an interface for reading data from a
Readable stream (such as `process.stdin`) one line at a time.
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
Other Notable Changes:
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) errors: print Node.js version on fatal exceptions that
cause exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- deps: upgrade npm to 8.1.0
(npm team) [https://github.com/nodejs/node/pull/40463]
- (SEMVER-MINOR) fs: add FileHandle.prototype.readableWebStream()
(James M Snell) [https://github.com/nodejs/node/pull/39331]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
Semver-Major Commits:
- (SEMVER-MAJOR) build: compile with C++17 (MSVC)
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) build: compile with --gnu++17
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) deps: update V8 to 9.5.172.19
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
- (SEMVER-MAJOR) deps,test,src,doc,tools: update to OpenSSL 3.0
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
- (SEMVER-MAJOR) dgram: tighten `address` validation in `socket.send`
(Voltrex) [https://github.com/nodejs/node/pull/39190]
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup` options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) doc: update minimum supported FreeBSD to 12.2
(Michaël Zasso) [https://github.com/nodejs/node/pull/40179]
- (SEMVER-MAJOR) errors: disp ver on fatal except that causes exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- (SEMVER-MAJOR) fs: fix rmsync error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38684]
- (SEMVER-MAJOR) fs: aggregate errors in fsPromises to avoid error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38259]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
- (SEMVER-MAJOR) readline: validate `AbortSignal`s and remove unused event listeners
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: introduce promise-based API
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: refactor `Interface` to ES2015 class
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) src: allow CAP\_NET\_BIND\_SERVICE in SafeGetenv
(Daniel Bevenius) [https://github.com/nodejs/node/pull/37727]
- (SEMVER-MAJOR) src: return Maybe from a couple of functions
(Darshan Sen) [https://github.com/nodejs/node/pull/39603]
- (SEMVER-MAJOR) src: allow custom PageAllocator in NodePlatform
(Shelley Vohr) [https://github.com/nodejs/node/pull/38362]
- (SEMVER-MAJOR) stream: fix highwatermark threshold and add the missing error
(Rongjian Zhang) [https://github.com/nodejs/node/pull/38700]
- (SEMVER-MAJOR) stream: don't emit 'data' after 'error' or 'close'
(Robert Nagy) [https://github.com/nodejs/node/pull/39639]
- (SEMVER-MAJOR) stream: do not emit `end` on readable error
(Szymon Marczak) [https://github.com/nodejs/node/pull/39607]
- (SEMVER-MAJOR) stream: forward errored to callback
(Robert Nagy) [https://github.com/nodejs/node/pull/39364]
- (SEMVER-MAJOR) stream: destroy readable on read error
(Robert Nagy) [https://github.com/nodejs/node/pull/39342]
- (SEMVER-MAJOR) stream: validate abort signal
(Robert Nagy) [https://github.com/nodejs/node/pull/39346]
- (SEMVER-MAJOR) stream: unify stream utils
(Robert Nagy) [https://github.com/nodejs/node/pull/39294]
- (SEMVER-MAJOR) stream: throw on premature close in Readable\
(Darshan Sen) [https://github.com/nodejs/node/pull/39117]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
- (SEMVER-MAJOR) stream: error Duplex write/read if not writable/readable
(Robert Nagy) [https://github.com/nodejs/node/pull/34385]
- (SEMVER-MAJOR) stream: bypass legacy destroy for pipeline and async iteration
(Robert Nagy) [https://github.com/nodejs/node/pull/38505]
- (SEMVER-MAJOR) url: throw invalid this on detached accessors
(James M Snell) [https://github.com/nodejs/node/pull/39752]
- (SEMVER-MAJOR) url: forbid certain confusable changes from being introduced by toASCII
(Timothy Gu) [https://github.com/nodejs/node/pull/38631]
PR-URL: https://github.com/nodejs/node/pull/40119
2021-09-15 01:55:37 +01:00
added: v17.0.0
2025-03-16 17:27:47 -07:00
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57513
description: Marking the API stable.
2021-06-23 22:24:19 -07:00
-->
* `streamDuplex` {stream.Duplex}
* Returns: {Object}
* `readable` {ReadableStream}
* `writable` {WritableStream}
2022-04-28 04:53:52 -03:00
```mjs
import { Duplex } from 'node:stream';
const duplex = Duplex({
objectMode: true,
read() {
this.push('world');
this.push(null);
},
write(chunk, encoding, callback) {
console.log('writable', chunk);
callback();
2022-11-17 08:19:12 -05:00
},
2022-04-28 04:53:52 -03:00
});
const { readable, writable } = Duplex.toWeb(duplex);
writable.getWriter().write('hello');
const { value } = await readable.getReader().read();
console.log('readable', value);
```
```cjs
const { Duplex } = require('node:stream');
const duplex = Duplex({
objectMode: true,
read() {
this.push('world');
this.push(null);
},
write(chunk, encoding, callback) {
console.log('writable', chunk);
callback();
2022-11-17 08:19:12 -05:00
},
2022-04-28 04:53:52 -03:00
});
const { readable, writable } = Duplex.toWeb(duplex);
writable.getWriter().write('hello');
readable.getReader().read().then((result) => {
console.log('readable', result.value);
});
```
2020-11-09 23:25:30 +02:00
### `stream.addAbortSignal(signal, stream)`
2021-10-10 21:55:04 -07:00
2020-11-09 23:25:30 +02:00
<!-- YAML
2020-12-07 15:40:58 -05:00
added: v15.4.0
2023-02-17 16:39:08 +05:30
changes:
2023-04-10 23:02:28 -04:00
- version:
- v19.7.0
- v18.16.0
2023-02-17 16:39:08 +05:30
pr-url: https://github.com/nodejs/node/pull/46273
description: Added support for `ReadableStream` and
`WritableStream` .
2020-11-09 23:25:30 +02:00
-->
2021-10-10 21:55:04 -07:00
2020-11-09 23:25:30 +02:00
* `signal` {AbortSignal} A signal representing possible cancellation
2024-02-14 00:37:42 +03:00
* `stream` {Stream|ReadableStream|WritableStream} A stream to attach a signal
to.
2020-11-09 23:25:30 +02:00
Attaches an AbortSignal to a readable or writeable stream. This lets code
control stream destruction using an `AbortController` .
Calling `abort` on the `AbortController` corresponding to the passed
`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`
2023-02-17 16:39:08 +05:30
on the stream, and `controller.error(new AbortError())` for webstreams.
2020-11-09 23:25:30 +02:00
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
2020-11-09 23:25:30 +02:00
const controller = new AbortController();
const read = addAbortSignal(
controller.signal,
2022-11-17 08:19:12 -05:00
fs.createReadStream(('object.json')),
2020-11-09 23:25:30 +02:00
);
// Later, abort the operation closing the stream
controller.abort();
```
Or using an `AbortSignal` with a readable stream as an async iterable:
```js
const controller = new AbortController();
setTimeout(() => controller.abort(), 10_000); // set a timeout
const stream = addAbortSignal(
controller.signal,
2022-11-17 08:19:12 -05:00
fs.createReadStream(('object.json')),
2020-11-09 23:25:30 +02:00
);
(async () => {
try {
for await (const chunk of stream) {
await process(chunk);
}
} catch (e) {
if (e.name === 'AbortError') {
// The operation was cancelled
} else {
throw e;
}
}
})();
```
2021-10-10 21:55:04 -07:00
2023-02-17 16:39:08 +05:30
Or using an `AbortSignal` with a ReadableStream:
```js
const controller = new AbortController();
const rs = new ReadableStream({
start(controller) {
controller.enqueue('hello');
controller.enqueue('world');
controller.close();
},
});
addAbortSignal(controller.signal, rs);
finished(rs, (err) => {
if (err) {
if (err.name === 'AbortError') {
// The operation was cancelled
}
}
});
const reader = rs.getReader();
reader.read().then(({ value, done }) => {
console.log(value); // hello
console.log(done); // false
controller.abort();
});
```
2023-03-29 20:02:10 +02:00
### `stream.getDefaultHighWaterMark(objectMode)`
<!-- YAML
2023-07-10 08:12:52 -04:00
added:
- v19.9.0
- v18.17.0
2023-03-29 20:02:10 +02:00
-->
2023-05-06 22:41:58 +03:00
* `objectMode` {boolean}
2023-03-29 20:02:10 +02:00
* Returns: {integer}
Returns the default highWaterMark used by streams.
2024-06-20 20:59:14 +12:00
Defaults to `65536` (64 KiB), or `16` for `objectMode` .
2023-03-29 20:02:10 +02:00
### `stream.setDefaultHighWaterMark(objectMode, value)`
<!-- YAML
2023-07-10 08:12:52 -04:00
added:
- v19.9.0
- v18.17.0
2023-03-29 20:02:10 +02:00
-->
2023-05-06 22:41:58 +03:00
* `objectMode` {boolean}
* `value` {integer} highWaterMark value
2023-03-29 20:02:10 +02:00
Sets the default highWaterMark used by streams.
2020-06-14 14:49:34 -07:00
## API for stream implementers
2016-05-01 00:58:16 -07:00
2016-05-23 22:30:41 -07:00
<!-- type=misc -->
2016-05-01 00:58:16 -07:00
2022-04-20 10:23:41 +02:00
The `node:stream` module API has been designed to make it possible to easily
2016-10-14 12:27:14 -07:00
implement streams using JavaScript's prototypal inheritance model.
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
First, a stream developer would declare a new JavaScript class that extends one
of the four basic stream classes (`stream.Writable` , `stream.Readable` ,
2017-04-15 12:47:50 -04:00
`stream.Duplex` , or `stream.Transform` ), making sure they call the appropriate
2016-05-23 22:30:41 -07:00
parent class constructor:
2013-07-15 16:56:02 -07:00
2018-12-14 22:22:40 -05:00
<!-- eslint - disable no - useless - constructor -->
2021-10-10 21:55:04 -07:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2016-05-23 22:30:41 -07:00
class MyWritable extends Writable {
2019-10-06 12:07:51 +02:00
constructor({ highWaterMark, ...options }) {
2019-11-24 13:17:56 +01:00
super({ highWaterMark });
2017-05-21 21:53:57 +03:00
// ...
2013-07-15 16:56:02 -07:00
}
}
```
2019-10-24 15:19:07 -07:00
When extending streams, keep in mind what options the user
2019-10-06 12:07:51 +02:00
can and should provide before forwarding these to the base constructor. For
2019-10-24 15:19:07 -07:00
example, if the implementation makes assumptions in regard to the
`autoDestroy` and `emitClose` options, do not allow the
user to override these. Be explicit about what
2019-10-06 12:07:51 +02:00
options are forwarded instead of implicitly forwarding all options.
2016-05-23 22:30:41 -07:00
The new stream class must then implement one or more specific methods, depending
on the type of stream being created, as detailed in the chart below:
2013-07-15 16:56:02 -07:00
2021-10-10 21:55:04 -07:00
| Use-case | Class | Method(s) to implement |
| --------------------------------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------ |
| Reading only | [`Readable` ][] | [`_read()` ][stream-_read] |
| Writing only | [`Writable` ][] | [`_write()` ][stream-_write], [`_writev()` ][stream-_writev], [`_final()` ][stream-_final] |
| Reading and writing | [`Duplex` ][] | [`_read()` ][stream-_read], [`_write()` ][stream-_write], [`_writev()` ][stream-_writev], [`_final()` ][stream-_final] |
| Operate on written data, then read the result | [`Transform` ][] | [`_transform()` ][stream-_transform], [`_flush()` ][stream-_flush], [`_final()` ][stream-_final] |
2013-07-15 16:56:02 -07:00
2021-10-10 21:55:04 -07:00
The implementation code for a stream should _never_ call the "public" methods
2018-02-05 21:55:16 -08:00
of a stream that are intended for use by consumers (as described in the
2020-06-14 14:49:34 -07:00
[API for stream consumers][] section). Doing so may lead to adverse side effects
2018-02-05 21:55:16 -08:00
in application code consuming the stream.
2013-07-15 16:56:02 -07:00
2019-10-09 08:45:00 +02:00
Avoid overriding public methods such as `write()` , `end()` , `cork()` ,
`uncork()` , `read()` and `destroy()` , or emitting internal events such
as `'error'` , `'data'` , `'end'` , `'finish'` and `'close'` through `.emit()` .
Doing so can break current and future stream invariants leading to behavior
and/or compatibility issues with other streams, stream utilities, and user
expectations.
2020-06-14 14:49:34 -07:00
### Simplified construction
2021-10-10 21:55:04 -07:00
2017-02-21 23:38:48 +01:00
<!-- YAML
added: v1.2.0
-->
2015-11-05 14:54:10 -05:00
2019-08-25 18:13:27 +02:00
For many simple cases, it is possible to create a stream without relying on
2016-05-23 22:30:41 -07:00
inheritance. This can be accomplished by directly creating instances of the
2022-09-15 02:16:10 +09:00
`stream.Writable` , `stream.Readable` , `stream.Duplex` , or `stream.Transform`
2016-05-23 22:30:41 -07:00
objects and passing appropriate methods as constructor options.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
const myWritable = new Writable({
2019-08-25 18:13:27 +02:00
construct(callback) {
// Initialize state and load resources...
},
2016-05-23 22:30:41 -07:00
write(chunk, encoding, callback) {
// ...
2019-08-25 18:13:27 +02:00
},
destroy() {
// Free resources...
2022-11-17 08:19:12 -05:00
},
2016-05-23 22:30:41 -07:00
});
```
2015-11-05 14:54:10 -05:00
2020-06-14 14:49:34 -07:00
### Implementing a writable stream
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
The `stream.Writable` class is extended to implement a [`Writable` ][] stream.
2015-11-05 14:54:10 -05:00
2021-10-10 21:55:04 -07:00
Custom `Writable` streams _must_ call the `new stream.Writable([options])`
2019-09-21 01:29:41 +02:00
constructor and implement the `writable._write()` and/or `writable._writev()`
method.
2015-11-05 14:54:10 -05:00
2020-06-06 22:06:34 -07:00
#### `new stream.Writable([options])`
2021-10-10 21:55:04 -07:00
2018-01-29 19:32:34 +01:00
<!-- YAML
changes:
2024-04-12 15:30:46 -03:00
- version: v22.0.0
2024-03-13 20:02:14 +01:00
pr-url: https://github.com/nodejs/node/pull/52037
description: bump default highWaterMark.
2020-12-21 21:14:19 +01:00
- version: v15.5.0
2020-12-07 18:42:46 +02:00
pr-url: https://github.com/nodejs/node/pull/36431
description: support passing in an AbortSignal.
2020-04-24 18:43:06 +02:00
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/30623
description: Change `autoDestroy` option default to `true` .
- version:
- v11.2.0
- v10.16.0
pr-url: https://github.com/nodejs/node/pull/22795
description: Add `autoDestroy` option to automatically `destroy()` the
stream when it emits `'finish'` or errors.
2018-03-02 09:53:46 -08:00
- version: v10.0.0
2018-01-29 19:32:34 +01:00
pr-url: https://github.com/nodejs/node/pull/18438
2019-01-09 09:32:08 -08:00
description: Add `emitClose` option to specify if `'close'` is emitted on
destroy.
2018-01-29 19:32:34 +01:00
-->
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
* `options` {Object}
2017-02-04 16:15:33 +01:00
* `highWaterMark` {number} Buffer level when
2018-04-02 04:44:32 +03:00
[`stream.write()` ][stream-write] starts returning `false` . **Default:**
2024-03-13 20:02:14 +01:00
`65536` (64 KiB), or `16` for `objectMode` streams.
2019-01-12 22:35:34 +00:00
* `decodeStrings` {boolean} Whether to encode `string` s passed to
[`stream.write()` ][stream-write] to `Buffer` s (with the encoding
specified in the [`stream.write()` ][stream-write] call) before passing
them to [`stream._write()` ][stream-_write]. Other types of data are not
converted (i.e. `Buffer` s are not decoded into `string` s). Setting to
2020-06-20 16:46:33 -07:00
false will prevent `string` s from being converted. **Default:** `true` .
2018-10-03 13:32:26 -07:00
* `defaultEncoding` {string} The default encoding that is used when no
encoding is specified as an argument to [`stream.write()` ][stream-write].
**Default:** `'utf8'` .
2017-02-04 16:15:33 +01:00
* `objectMode` {boolean} Whether or not the
2016-05-23 22:30:41 -07:00
[`stream.write(anyObj)` ][stream-write] is a valid operation. When set,
2024-03-20 18:27:29 +01:00
it becomes possible to write JavaScript values other than string, {Buffer},
{TypedArray} or {DataView} if supported by the stream implementation.
2018-04-02 04:44:32 +03:00
**Default:** `false` .
2018-04-09 19:30:22 +03:00
* `emitClose` {boolean} Whether or not the stream should emit `'close'`
2018-04-02 04:44:32 +03:00
after it has been destroyed. **Default:** `true` .
2016-05-23 22:30:41 -07:00
* `write` {Function} Implementation for the
[`stream._write()` ][stream-_write] method.
* `writev` {Function} Implementation for the
[`stream._writev()` ][stream-_writev] method.
2017-05-06 14:20:52 +02:00
* `destroy` {Function} Implementation for the
[`stream._destroy()` ][writable-_destroy] method.
2017-05-04 15:33:14 +02:00
* `final` {Function} Implementation for the
[`stream._final()` ][stream-_final] method.
2019-08-25 18:13:27 +02:00
* `construct` {Function} Implementation for the
[`stream._construct()` ][writable-_construct] method.
2018-08-21 20:05:12 +02:00
* `autoDestroy` {boolean} Whether this stream should automatically call
2019-11-24 13:17:56 +01:00
`.destroy()` on itself after ending. **Default:** `true` .
2020-12-07 18:42:46 +02:00
* `signal` {AbortSignal} A signal representing possible cancellation.
2015-11-05 14:54:10 -05:00
2018-12-14 22:22:40 -05:00
<!-- eslint - disable no - useless - constructor -->
2021-10-10 21:55:04 -07:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2016-05-23 22:30:41 -07:00
class MyWritable extends Writable {
constructor(options) {
2019-07-07 20:56:12 +03:00
// Calls the stream.Writable() constructor.
2016-05-23 22:30:41 -07:00
super(options);
2017-05-21 21:53:57 +03:00
// ...
2016-05-23 22:30:41 -07:00
}
}
```
Or, when using pre-ES6 style constructors:
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
const util = require('node:util');
2016-05-23 22:30:41 -07:00
function MyWritable(options) {
if (!(this instanceof MyWritable))
return new MyWritable(options);
Writable.call(this, options);
}
util.inherits(MyWritable, Writable);
```
2020-06-14 14:49:34 -07:00
Or, using the simplified constructor approach:
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2016-05-23 22:30:41 -07:00
const myWritable = new Writable({
write(chunk, encoding, callback) {
// ...
},
writev(chunks, callback) {
// ...
2022-11-17 08:19:12 -05:00
},
2016-05-23 22:30:41 -07:00
});
```
2020-12-07 18:42:46 +02:00
Calling `abort` on the `AbortController` corresponding to the passed
`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`
on the writeable stream.
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2020-12-07 18:42:46 +02:00
const controller = new AbortController();
const myWritable = new Writable({
write(chunk, encoding, callback) {
// ...
},
writev(chunks, callback) {
// ...
},
2022-11-17 08:19:12 -05:00
signal: controller.signal,
2020-12-07 18:42:46 +02:00
});
// Later, abort the operation closing the stream
controller.abort();
```
2021-10-10 21:55:04 -07:00
2019-08-25 18:13:27 +02:00
#### `writable._construct(callback)`
2021-10-10 21:55:04 -07:00
2019-08-25 18:13:27 +02:00
<!-- YAML
2020-11-10 15:47:27 +01:00
added: v15.0.0
2019-08-25 18:13:27 +02:00
-->
* `callback` {Function} Call this function (optionally with an error
argument) when the stream has finished initializing.
The `_construct()` method MUST NOT be called directly. It may be implemented
by child classes, and if so, will be called by the internal `Writable`
class methods only.
This optional function will be called in a tick after the stream constructor
2020-09-27 07:50:41 -07:00
has returned, delaying any `_write()` , `_final()` and `_destroy()` calls until
2019-08-25 18:13:27 +02:00
`callback` is called. This is useful to initialize state or asynchronously
initialize resources before the stream can be used.
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
const fs = require('node:fs');
2019-08-25 18:13:27 +02:00
class WriteStream extends Writable {
constructor(filename) {
super();
this.filename = filename;
2021-11-02 21:52:43 +03:00
this.fd = null;
2019-08-25 18:13:27 +02:00
}
_construct(callback) {
2024-11-03 00:55:53 +08:00
fs.open(this.filename, 'w', (err, fd) => {
2019-08-25 18:13:27 +02:00
if (err) {
callback(err);
} else {
this.fd = fd;
callback();
}
});
}
_write(chunk, encoding, callback) {
fs.write(this.fd, chunk, callback);
}
_destroy(err, callback) {
if (this.fd) {
fs.close(this.fd, (er) => callback(er || err));
} else {
callback(err);
}
}
}
```
2019-12-24 15:09:29 -08:00
#### `writable._write(chunk, encoding, callback)`
2021-10-10 21:55:04 -07:00
2019-09-21 01:29:41 +02:00
<!-- YAML
changes:
2019-09-25 00:45:45 +02:00
- version: v12.11.0
2019-09-21 01:29:41 +02:00
pr-url: https://github.com/nodejs/node/pull/29639
description: _write() is optional when providing _writev().
-->
2016-05-23 22:30:41 -07:00
2019-01-12 22:35:34 +00:00
* `chunk` {Buffer|string|any} The `Buffer` to be written, converted from the
`string` passed to [`stream.write()` ][stream-write]. If the stream's
`decodeStrings` option is `false` or the stream is operating in object mode,
the chunk will not be converted & will be whatever was passed to
[`stream.write()` ][stream-write].
2017-02-04 16:15:33 +01:00
* `encoding` {string} If the chunk is a string, then `encoding` is the
2016-05-23 22:30:41 -07:00
character encoding of that string. If chunk is a `Buffer` , or if the
stream is operating in object mode, `encoding` may be ignored.
* `callback` {Function} Call this function (optionally with an error
argument) when processing is complete for the supplied chunk.
2018-04-29 20:46:41 +03:00
All `Writable` stream implementations must provide a
2019-09-21 01:29:41 +02:00
[`writable._write()` ][stream-_write] and/or
[`writable._writev()` ][stream-_writev] method to send data to the underlying
2016-05-23 22:30:41 -07:00
resource.
2018-04-29 20:46:41 +03:00
[`Transform` ][] streams provide their own implementation of the
2016-06-19 00:19:41 +03:00
[`writable._write()` ][stream-_write].
2016-05-23 22:30:41 -07:00
2018-02-05 21:55:16 -08:00
This function MUST NOT be called by application code directly. It should be
2018-04-29 20:46:41 +03:00
implemented by child classes, and called by the internal `Writable` class
methods only.
2016-05-23 22:30:41 -07:00
2020-02-19 17:06:29 +01:00
The `callback` function must be called synchronously inside of
`writable._write()` or asynchronously (i.e. different tick) to signal either
that the write completed successfully or failed with an error.
The first argument passed to the `callback` must be the `Error` object if the
call failed or `null` if the write succeeded.
2016-05-23 22:30:41 -07:00
2017-12-27 19:21:06 -08:00
All calls to `writable.write()` that occur between the time `writable._write()`
is called and the `callback` is called will cause the written data to be
2018-01-25 23:45:17 +08:00
buffered. When the `callback` is invoked, the stream might emit a [`'drain'` ][]
2017-12-27 19:21:06 -08:00
event. If a stream implementation is capable of processing multiple chunks of
data at once, the `writable._writev()` method should be implemented.
2016-05-23 22:30:41 -07:00
2018-04-02 23:08:48 +09:00
If the `decodeStrings` property is explicitly set to `false` in the constructor
options, then `chunk` will remain the same object that is passed to `.write()` ,
and may be a string rather than a `Buffer` . This is to support implementations
that have an optimized handling for certain string data encodings. In that case,
the `encoding` argument will indicate the character encoding of the string.
Otherwise, the `encoding` argument can be safely ignored.
2016-05-23 22:30:41 -07:00
2016-06-19 00:19:41 +03:00
The `writable._write()` method is prefixed with an underscore because it is
2016-05-23 22:30:41 -07:00
internal to the class that defines it, and should never be called directly by
user programs.
2019-12-24 15:09:29 -08:00
#### `writable._writev(chunks, callback)`
2016-05-23 22:30:41 -07:00
2021-10-10 21:55:04 -07:00
* `chunks` {Object\[]} The data to be written. The value is an array of {Object}
2021-03-11 14:20:38 -05:00
that each represent a discrete chunk of data to write. The properties of
2021-01-06 13:59:32 -08:00
these objects are:
* `chunk` {Buffer|string} A buffer instance or string containing the data to
be written. The `chunk` will be a string if the `Writable` was created with
the `decodeStrings` option set to `false` and a string was passed to `write()` .
* `encoding` {string} The character encoding of the `chunk` . If `chunk` is
2021-02-08 14:26:31 +01:00
a `Buffer` , the `encoding` will be `'buffer'` .
2016-05-23 22:30:41 -07:00
* `callback` {Function} A callback function (optionally with an error
argument) to be invoked when processing is complete for the supplied chunks.
2018-02-05 21:55:16 -08:00
This function MUST NOT be called by application code directly. It should be
2018-04-29 20:46:41 +03:00
implemented by child classes, and called by the internal `Writable` class
methods only.
2016-05-23 22:30:41 -07:00
2019-09-21 01:29:41 +02:00
The `writable._writev()` method may be implemented in addition or alternatively
to `writable._write()` in stream implementations that are capable of processing
2020-01-14 09:46:08 -05:00
multiple chunks of data at once. If implemented and if there is buffered data
from previous writes, `_writev()` will be called instead of `_write()` .
2016-05-23 22:30:41 -07:00
2016-06-19 00:19:41 +03:00
The `writable._writev()` method is prefixed with an underscore because it is
2016-05-23 22:30:41 -07:00
internal to the class that defines it, and should never be called directly by
user programs.
2019-12-24 15:09:29 -08:00
#### `writable._destroy(err, callback)`
2021-10-10 21:55:04 -07:00
2017-05-06 14:20:52 +02:00
<!-- YAML
2017-03-15 20:26:14 -07:00
added: v8.0.0
2017-05-06 14:20:52 +02:00
-->
2017-10-19 09:37:36 +02:00
* `err` {Error} A possible error.
* `callback` {Function} A callback function that takes an optional error
argument.
The `_destroy()` method is called by [`writable.destroy()` ][writable-destroy].
2018-01-14 22:08:46 +01:00
It can be overridden by child classes but it **must not** be called directly.
2017-05-06 14:20:52 +02:00
2019-12-24 15:09:29 -08:00
#### `writable._final(callback)`
2021-10-10 21:55:04 -07:00
2017-05-04 15:33:14 +02:00
<!-- YAML
2017-03-15 20:26:14 -07:00
added: v8.0.0
2017-05-04 15:33:14 +02:00
-->
* `callback` {Function} Call this function (optionally with an error
2017-08-18 23:09:14 -07:00
argument) when finished writing any remaining data.
2017-05-04 15:33:14 +02:00
2017-08-18 23:09:14 -07:00
The `_final()` method **must not** be called directly. It may be implemented
2018-04-29 20:46:41 +03:00
by child classes, and if so, will be called by the internal `Writable`
2017-05-04 15:33:14 +02:00
class methods only.
This optional function will be called before the stream closes, delaying the
2018-04-09 19:30:22 +03:00
`'finish'` event until `callback` is called. This is useful to close resources
2017-05-04 15:33:14 +02:00
or write buffered data before a stream ends.
2020-06-14 14:49:34 -07:00
#### Errors while writing
2016-05-23 22:30:41 -07:00
2019-09-22 16:10:35 +02:00
Errors occurring during the processing of the [`writable._write()` ][],
2019-10-02 00:31:57 -04:00
[`writable._writev()` ][] and [`writable._final()` ][] methods must be propagated
2019-09-22 16:10:35 +02:00
by invoking the callback and passing the error as the first argument.
Throwing an `Error` from within these methods or manually emitting an `'error'`
event results in undefined behavior.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
If a `Readable` stream pipes into a `Writable` stream when `Writable` emits an
error, the `Readable` stream will be unpiped.
2018-02-08 09:22:02 +01:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2016-05-23 22:30:41 -07:00
const myWritable = new Writable({
write(chunk, encoding, callback) {
if (chunk.toString().indexOf('a') >= 0) {
2016-07-14 22:41:29 -07:00
callback(new Error('chunk is invalid'));
2016-05-23 22:30:41 -07:00
} else {
2016-07-14 22:41:29 -07:00
callback();
2016-05-23 22:30:41 -07:00
}
2022-11-17 08:19:12 -05:00
},
2016-05-23 22:30:41 -07:00
});
```
2020-06-14 14:49:34 -07:00
#### An example writable stream
2016-05-23 22:30:41 -07:00
The following illustrates a rather simplistic (and somewhat pointless) custom
2018-04-29 20:46:41 +03:00
`Writable` stream implementation. While this specific `Writable` stream instance
2016-05-23 22:30:41 -07:00
is not of any real particular usefulness, the example illustrates each of the
2018-04-29 20:46:41 +03:00
required elements of a custom [`Writable` ][] stream instance:
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
2016-05-23 22:30:41 -07:00
class MyWritable extends Writable {
_write(chunk, encoding, callback) {
if (chunk.toString().indexOf('a') >= 0) {
2016-07-14 22:41:29 -07:00
callback(new Error('chunk is invalid'));
2016-05-23 22:30:41 -07:00
} else {
2016-07-14 22:41:29 -07:00
callback();
2016-05-23 22:30:41 -07:00
}
}
}
```
2020-06-14 14:49:34 -07:00
#### Decoding buffers in a writable stream
2017-10-23 09:28:52 +02:00
Decoding buffers is a common task, for instance, when using transformers whose
input is a string. This is not a trivial process when using multi-byte
characters encoding, such as UTF-8. The following example shows how to decode
2018-04-29 20:46:41 +03:00
multi-byte strings using `StringDecoder` and [`Writable` ][].
2017-10-23 09:28:52 +02:00
```js
2022-04-20 10:23:41 +02:00
const { Writable } = require('node:stream');
const { StringDecoder } = require('node:string_decoder');
2017-10-23 09:28:52 +02:00
class StringWritable extends Writable {
constructor(options) {
super(options);
2024-11-20 19:10:38 +09:00
this._decoder = new StringDecoder(options?.defaultEncoding);
2017-10-23 09:28:52 +02:00
this.data = '';
}
_write(chunk, encoding, callback) {
if (encoding === 'buffer') {
chunk = this._decoder.write(chunk);
}
this.data += chunk;
callback();
}
_final(callback) {
this.data += this._decoder.end();
callback();
}
}
const euro = [[0xE2, 0x82], [0xAC]].map(Buffer.from);
const w = new StringWritable();
w.write('currency: ');
w.write(euro[0]);
w.end(euro[1]);
console.log(w.data); // currency: €
```
2020-06-14 14:49:34 -07:00
### Implementing a readable stream
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
The `stream.Readable` class is extended to implement a [`Readable` ][] stream.
2013-07-15 16:56:02 -07:00
2021-10-10 21:55:04 -07:00
Custom `Readable` streams _must_ call the `new stream.Readable([options])`
2020-06-06 11:20:14 +05:30
constructor and implement the [`readable._read()` ][] method.
2013-07-15 16:56:02 -07:00
2019-12-24 15:09:29 -08:00
#### `new stream.Readable([options])`
2021-10-10 21:55:04 -07:00
2018-08-21 20:05:12 +02:00
<!-- YAML
changes:
2024-04-12 15:30:46 -03:00
- version: v22.0.0
2024-03-13 20:02:14 +01:00
pr-url: https://github.com/nodejs/node/pull/52037
description: bump default highWaterMark.
2020-12-21 21:14:19 +01:00
- version: v15.5.0
2020-12-07 18:42:46 +02:00
pr-url: https://github.com/nodejs/node/pull/36431
description: support passing in an AbortSignal.
2020-10-01 20:49:03 +02:00
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/30623
description: Change `autoDestroy` option default to `true` .
2020-04-24 18:43:06 +02:00
- version:
- v11.2.0
- v10.16.0
2018-08-21 20:05:12 +02:00
pr-url: https://github.com/nodejs/node/pull/22795
2019-01-09 09:32:08 -08:00
description: Add `autoDestroy` option to automatically `destroy()` the
stream when it emits `'end'` or errors.
2018-08-21 20:05:12 +02:00
-->
2013-07-15 16:56:02 -07:00
2015-11-05 14:54:10 -05:00
* `options` {Object}
2017-06-03 16:11:32 -04:00
* `highWaterMark` {number} The maximum [number of bytes][hwm-gotcha] to store
in the internal buffer before ceasing to read from the underlying resource.
2024-03-13 20:02:14 +01:00
**Default:** `65536` (64 KiB), or `16` for `objectMode` streams.
2017-02-04 16:15:33 +01:00
* `encoding` {string} If specified, then buffers will be decoded to
2018-04-02 04:44:32 +03:00
strings using the specified encoding. **Default:** `null` .
2017-02-04 16:15:33 +01:00
* `objectMode` {boolean} Whether this stream should behave
2016-02-02 20:34:29 +03:00
as a stream of objects. Meaning that [`stream.read(n)` ][stream-read] returns
2018-04-29 20:46:41 +03:00
a single value instead of a `Buffer` of size `n` . **Default:** `false` .
2019-07-07 20:56:12 +03:00
* `emitClose` {boolean} Whether or not the stream should emit `'close'`
after it has been destroyed. **Default:** `true` .
2016-02-02 20:34:29 +03:00
* `read` {Function} Implementation for the [`stream._read()` ][stream-_read]
method.
2018-02-12 02:31:55 -05:00
* `destroy` {Function} Implementation for the
[`stream._destroy()` ][readable-_destroy] method.
2019-08-25 18:13:27 +02:00
* `construct` {Function} Implementation for the
[`stream._construct()` ][readable-_construct] method.
2018-08-21 20:05:12 +02:00
* `autoDestroy` {boolean} Whether this stream should automatically call
2019-11-24 13:17:56 +01:00
`.destroy()` on itself after ending. **Default:** `true` .
2020-12-07 18:42:46 +02:00
* `signal` {AbortSignal} A signal representing possible cancellation.
2015-11-05 14:54:10 -05:00
2018-12-14 22:22:40 -05:00
<!-- eslint - disable no - useless - constructor -->
2021-10-10 21:55:04 -07:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2016-05-23 22:30:41 -07:00
class MyReadable extends Readable {
constructor(options) {
2019-07-07 20:56:12 +03:00
// Calls the stream.Readable(options) constructor.
2016-05-23 22:30:41 -07:00
super(options);
2017-05-21 21:53:57 +03:00
// ...
2016-05-23 22:30:41 -07:00
}
}
```
Or, when using pre-ES6 style constructors:
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
const util = require('node:util');
2016-05-23 22:30:41 -07:00
function MyReadable(options) {
if (!(this instanceof MyReadable))
return new MyReadable(options);
Readable.call(this, options);
}
util.inherits(MyReadable, Readable);
```
2020-06-14 14:49:34 -07:00
Or, using the simplified constructor approach:
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2016-05-23 22:30:41 -07:00
const myReadable = new Readable({
read(size) {
// ...
2022-11-17 08:19:12 -05:00
},
2016-05-23 22:30:41 -07:00
});
```
2015-11-05 14:54:10 -05:00
2020-12-07 18:42:46 +02:00
Calling `abort` on the `AbortController` corresponding to the passed
`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`
on the readable created.
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2020-12-07 18:42:46 +02:00
const controller = new AbortController();
const read = new Readable({
read(size) {
// ...
},
2022-11-17 08:19:12 -05:00
signal: controller.signal,
2020-12-07 18:42:46 +02:00
});
// Later, abort the operation closing the stream
controller.abort();
```
2019-08-25 18:13:27 +02:00
#### `readable._construct(callback)`
2021-10-10 21:55:04 -07:00
2019-08-25 18:13:27 +02:00
<!-- YAML
2020-11-10 15:47:27 +01:00
added: v15.0.0
2019-08-25 18:13:27 +02:00
-->
* `callback` {Function} Call this function (optionally with an error
argument) when the stream has finished initializing.
The `_construct()` method MUST NOT be called directly. It may be implemented
by child classes, and if so, will be called by the internal `Readable`
class methods only.
2020-03-15 15:20:46 +01:00
This optional function will be scheduled in the next tick by the stream
2020-09-27 07:50:41 -07:00
constructor, delaying any `_read()` and `_destroy()` calls until `callback` is
2020-03-15 15:20:46 +01:00
called. This is useful to initialize state or asynchronously initialize
resources before the stream can be used.
2019-08-25 18:13:27 +02:00
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
const fs = require('node:fs');
2019-08-25 18:13:27 +02:00
class ReadStream extends Readable {
constructor(filename) {
super();
this.filename = filename;
this.fd = null;
}
_construct(callback) {
2020-10-24 16:33:50 -03:00
fs.open(this.filename, (err, fd) => {
2019-08-25 18:13:27 +02:00
if (err) {
callback(err);
} else {
this.fd = fd;
callback();
}
});
}
_read(n) {
const buf = Buffer.alloc(n);
fs.read(this.fd, buf, 0, n, null, (err, bytesRead) => {
if (err) {
this.destroy(err);
} else {
this.push(bytesRead > 0 ? buf.slice(0, bytesRead) : null);
}
});
}
_destroy(err, callback) {
if (this.fd) {
fs.close(this.fd, (er) => callback(er || err));
} else {
callback(err);
}
}
}
```
2019-12-24 15:09:29 -08:00
#### `readable._read(size)`
2021-10-10 21:55:04 -07:00
2018-01-04 18:06:56 +01:00
<!-- YAML
added: v0.9.4
-->
2015-11-05 14:54:10 -05:00
2017-02-04 16:15:33 +01:00
* `size` {number} Number of bytes to read asynchronously
2015-11-05 14:54:10 -05:00
2018-02-05 21:55:16 -08:00
This function MUST NOT be called by application code directly. It should be
2018-04-29 20:46:41 +03:00
implemented by child classes, and called by the internal `Readable` class
methods only.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
All `Readable` stream implementations must provide an implementation of the
2020-06-06 11:20:14 +05:30
[`readable._read()` ][] method to fetch data from the underlying resource.
2016-05-23 22:30:41 -07:00
2020-06-06 11:20:14 +05:30
When [`readable._read()` ][] is called, if data is available from the resource,
the implementation should begin pushing that data into the read queue using the
2021-05-18 13:33:32 -06:00
[`this.push(dataChunk)` ][stream-push] method. `_read()` will be called again
after each call to [`this.push(dataChunk)` ][stream-push] once the stream is
ready to accept more data. `_read()` may continue reading from the resource and
pushing data until `readable.push()` returns `false` . Only when `_read()` is
called again after it has stopped should it resume pushing additional data into
the queue.
2016-05-23 22:30:41 -07:00
2020-06-06 11:20:14 +05:30
Once the [`readable._read()` ][] method has been called, it will not be called
again until more data is pushed through the [`readable.push()` ][stream-push]
method. Empty data such as empty buffers and strings will not cause
[`readable._read()` ][] to be called.
2016-05-23 22:30:41 -07:00
The `size` argument is advisory. For implementations where a "read" is a
single operation that returns data can use the `size` argument to determine how
much data to fetch. Other implementations may ignore this argument and simply
provide data whenever it becomes available. There is no need to "wait" until
2016-02-02 20:34:29 +03:00
`size` bytes are available before calling [`stream.push(chunk)` ][stream-push].
2015-11-05 14:54:10 -05:00
2020-06-06 11:20:14 +05:30
The [`readable._read()` ][] method is prefixed with an underscore because it is
2016-05-23 22:30:41 -07:00
internal to the class that defines it, and should never be called directly by
user programs.
2015-11-05 14:54:10 -05:00
2019-12-24 15:09:29 -08:00
#### `readable._destroy(err, callback)`
2021-10-10 21:55:04 -07:00
2017-09-10 15:53:57 +02:00
<!-- YAML
added: v8.0.0
-->
2017-10-19 09:37:36 +02:00
* `err` {Error} A possible error.
2017-09-10 15:53:57 +02:00
* `callback` {Function} A callback function that takes an optional error
2017-10-19 09:37:36 +02:00
argument.
The `_destroy()` method is called by [`readable.destroy()` ][readable-destroy].
2018-01-14 22:08:46 +01:00
It can be overridden by child classes but it **must not** be called directly.
2017-09-10 15:53:57 +02:00
2019-12-24 15:09:29 -08:00
#### `readable.push(chunk[, encoding])`
2021-10-10 21:55:04 -07:00
2017-01-09 19:05:06 +01:00
<!-- YAML
changes:
2024-05-02 11:31:36 +02:00
- version:
- v22.0.0
- v20.13.0
2024-03-20 18:27:29 +01:00
pr-url: https://github.com/nodejs/node/pull/51866
description: The `chunk` argument can now be a `TypedArray` or `DataView` instance.
2017-03-15 20:26:14 -07:00
- version: v8.0.0
2017-01-09 19:05:06 +01:00
pr-url: https://github.com/nodejs/node/pull/11608
description: The `chunk` argument can now be a `Uint8Array` instance.
-->
2016-01-19 13:03:15 -03:00
2024-03-20 18:27:29 +01:00
* `chunk` {Buffer|TypedArray|DataView|string|null|any} Chunk of data to push
into the read queue. For streams not operating in object mode, `chunk` must
be a {string}, {Buffer}, {TypedArray} or {DataView}. For object mode streams,
`chunk` may be any JavaScript value.
2018-04-02 08:38:48 +03:00
* `encoding` {string} Encoding of string chunks. Must be a valid
2018-04-29 20:46:41 +03:00
`Buffer` encoding, such as `'utf8'` or `'ascii'` .
2018-11-05 20:40:07 -08:00
* Returns: {boolean} `true` if additional chunks of data may continue to be
2016-05-23 22:30:41 -07:00
pushed; `false` otherwise.
2015-11-05 14:54:10 -05:00
2024-03-20 18:27:29 +01:00
When `chunk` is a {Buffer}, {TypedArray}, {DataView} or {string}, the `chunk`
of data will be added to the internal queue for users of the stream to consume.
2017-01-09 19:05:06 +01:00
Passing `chunk` as `null` signals the end of the stream (EOF), after which no
more data can be written.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
When the `Readable` is operating in paused mode, the data added with
2016-05-23 22:30:41 -07:00
`readable.push()` can be read out by calling the
[`readable.read()` ][stream-read] method when the [`'readable'` ][] event is
emitted.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
When the `Readable` is operating in flowing mode, the data added with
2016-05-23 22:30:41 -07:00
`readable.push()` will be delivered by emitting a `'data'` event.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
The `readable.push()` method is designed to be as flexible as possible. For
example, when wrapping a lower-level source that provides some form of
pause/resume mechanism, and a data callback, the low-level source can be wrapped
2018-08-26 19:02:27 +03:00
by the custom `Readable` instance:
2015-11-05 14:54:10 -05:00
2016-01-17 18:39:07 +01:00
```js
2018-12-10 13:27:32 +01:00
// `_source` is an object with readStop() and readStart() methods,
2015-11-05 14:54:10 -05:00
// and an `ondata` member that gets called when it has data, and
// an `onend` member that gets called when the data is over.
2016-05-02 07:03:23 +02:00
class SourceWrapper extends Readable {
constructor(options) {
super(options);
2015-11-05 14:54:10 -05:00
2018-12-10 13:27:32 +01:00
this._source = getLowLevelSourceObject();
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
// Every time there's data, push it into the internal buffer.
2016-05-02 07:03:23 +02:00
this._source.ondata = (chunk) => {
2019-07-07 20:56:12 +03:00
// If push() returns false, then stop reading from source.
2016-05-02 07:03:23 +02:00
if (!this.push(chunk))
this._source.readStop();
};
2015-11-05 14:54:10 -05:00
2019-07-07 20:56:12 +03:00
// When the source ends, push the EOF-signaling `null` chunk.
2016-05-02 07:03:23 +02:00
this._source.onend = () => {
this.push(null);
};
}
2019-07-07 20:56:12 +03:00
// _read() will be called when the stream wants to pull more data in.
// The advisory size argument is ignored in this case.
2016-05-02 07:03:23 +02:00
_read(size) {
this._source.readStart();
}
2015-11-05 14:54:10 -05:00
}
```
2018-02-05 21:55:16 -08:00
2019-09-25 14:02:21 +08:00
The `readable.push()` method is used to push the content
2020-06-06 11:20:14 +05:30
into the internal buffer. It can be driven by the [`readable._read()` ][] method.
2016-05-23 22:30:41 -07:00
2018-01-22 00:58:35 +08:00
For streams not operating in object mode, if the `chunk` parameter of
`readable.push()` is `undefined` , it will be treated as empty string or
buffer. See [`readable.push('')` ][] for more information.
2020-06-14 14:49:34 -07:00
#### Errors while reading
2016-05-23 22:30:41 -07:00
2019-09-22 16:10:35 +02:00
Errors occurring during processing of the [`readable._read()` ][] must be
propagated through the [`readable.destroy(err)` ][readable-_destroy] method.
Throwing an `Error` from within [`readable._read()` ][] or manually emitting an
`'error'` event results in undefined behavior.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2016-05-23 22:30:41 -07:00
const myReadable = new Readable({
read(size) {
2019-09-22 16:10:35 +02:00
const err = checkSomeErrorCondition();
if (err) {
this.destroy(err);
} else {
// Do some work.
2016-05-23 22:30:41 -07:00
}
2022-11-17 08:19:12 -05:00
},
2016-05-23 22:30:41 -07:00
});
```
2020-06-14 14:49:34 -07:00
#### An example counting stream
2015-11-05 14:54:10 -05:00
<!-- type=example -->
2018-04-29 20:46:41 +03:00
The following is a basic example of a `Readable` stream that emits the numerals
2015-11-05 14:54:10 -05:00
from 1 to 1,000,000 in ascending order, and then ends.
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2013-07-15 16:56:02 -07:00
2016-05-02 07:03:23 +02:00
class Counter extends Readable {
constructor(opt) {
super(opt);
this._max = 1000000;
this._index = 1;
}
2013-07-15 16:56:02 -07:00
2016-05-02 07:03:23 +02:00
_read() {
2017-04-22 15:22:40 +03:00
const i = this._index++;
2016-05-02 07:03:23 +02:00
if (i > this._max)
this.push(null);
else {
2018-02-17 03:58:50 +01:00
const str = String(i);
2017-04-22 15:22:40 +03:00
const buf = Buffer.from(str, 'ascii');
2016-05-02 07:03:23 +02:00
this.push(buf);
}
2013-07-15 16:56:02 -07:00
}
2016-05-02 07:03:23 +02:00
}
2013-07-15 16:56:02 -07:00
```
2020-06-14 14:49:34 -07:00
### Implementing a duplex stream
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
A [`Duplex` ][] stream is one that implements both [`Readable` ][] and
[`Writable` ][], such as a TCP socket connection.
2013-07-15 16:56:02 -07:00
2016-07-16 09:36:00 -07:00
Because JavaScript does not have support for multiple inheritance, the
2018-04-29 20:46:41 +03:00
`stream.Duplex` class is extended to implement a [`Duplex` ][] stream (as opposed
2021-10-10 21:55:04 -07:00
to extending the `stream.Readable` _and_ `stream.Writable` classes).
2013-02-28 15:42:55 -08:00
2018-02-05 21:55:16 -08:00
The `stream.Duplex` class prototypically inherits from `stream.Readable` and
parasitically from `stream.Writable` , but `instanceof` will work properly for
both base classes due to overriding [`Symbol.hasInstance` ][] on
`stream.Writable` .
2013-02-28 15:42:55 -08:00
2021-10-10 21:55:04 -07:00
Custom `Duplex` streams _must_ call the `new stream.Duplex([options])`
constructor and implement _both_ the [`readable._read()` ][] and
2016-06-19 00:19:41 +03:00
`writable._write()` methods.
2013-02-28 15:42:55 -08:00
2019-12-24 15:09:29 -08:00
#### `new stream.Duplex(options)`
2021-10-10 21:55:04 -07:00
2017-08-13 22:05:13 +02:00
<!-- YAML
changes:
2017-08-13 22:33:49 +02:00
- version: v8.4.0
2017-08-13 22:05:13 +02:00
pr-url: https://github.com/nodejs/node/pull/14636
description: The `readableHighWaterMark` and `writableHighWaterMark` options
are supported now.
-->
2013-02-28 15:42:55 -08:00
2018-04-29 20:46:41 +03:00
* `options` {Object} Passed to both `Writable` and `Readable`
2016-05-23 22:30:41 -07:00
constructors. Also has the following fields:
2018-04-02 04:44:32 +03:00
* `allowHalfOpen` {boolean} If set to `false` , then the stream will
automatically end the writable side when the readable side ends.
**Default:** `true` .
2020-07-15 21:26:34 +05:30
* `readable` {boolean} Sets whether the `Duplex` should be readable.
**Default:** `true` .
* `writable` {boolean} Sets whether the `Duplex` should be writable.
**Default:** `true` .
2018-04-02 04:44:32 +03:00
* `readableObjectMode` {boolean} Sets `objectMode` for readable side of the
stream. Has no effect if `objectMode` is `true` . **Default:** `false` .
* `writableObjectMode` {boolean} Sets `objectMode` for writable side of the
stream. Has no effect if `objectMode` is `true` . **Default:** `false` .
2017-08-05 02:12:24 +03:00
* `readableHighWaterMark` {number} Sets `highWaterMark` for the readable side
of the stream. Has no effect if `highWaterMark` is provided.
* `writableHighWaterMark` {number} Sets `highWaterMark` for the writable side
of the stream. Has no effect if `highWaterMark` is provided.
2013-02-28 15:42:55 -08:00
2018-12-14 22:22:40 -05:00
<!-- eslint - disable no - useless - constructor -->
2021-10-10 21:55:04 -07:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Duplex } = require('node:stream');
2013-02-28 15:42:55 -08:00
2016-05-23 22:30:41 -07:00
class MyDuplex extends Duplex {
constructor(options) {
super(options);
2017-05-21 21:53:57 +03:00
// ...
2013-02-28 15:42:55 -08:00
}
2016-05-02 07:03:23 +02:00
}
2013-02-28 15:42:55 -08:00
```
2016-05-23 22:30:41 -07:00
Or, when using pre-ES6 style constructors:
2010-10-28 23:18:16 +11:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Duplex } = require('node:stream');
const util = require('node:util');
2010-10-28 23:18:16 +11:00
2016-05-23 22:30:41 -07:00
function MyDuplex(options) {
if (!(this instanceof MyDuplex))
return new MyDuplex(options);
Duplex.call(this, options);
}
util.inherits(MyDuplex, Duplex);
```
2013-03-03 19:05:44 -08:00
2020-06-14 14:49:34 -07:00
Or, using the simplified constructor approach:
2014-09-03 09:01:15 -04:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const { Duplex } = require('node:stream');
2014-09-03 09:01:15 -04:00
2016-05-23 22:30:41 -07:00
const myDuplex = new Duplex({
read(size) {
// ...
},
write(chunk, encoding, callback) {
// ...
2022-11-17 08:19:12 -05:00
},
2016-05-23 22:30:41 -07:00
});
2014-09-03 09:01:15 -04:00
```
2010-10-28 23:18:16 +11:00
2019-08-25 18:13:27 +02:00
When using pipeline:
```js
2022-04-20 10:23:41 +02:00
const { Transform, pipeline } = require('node:stream');
const fs = require('node:fs');
2019-08-25 18:13:27 +02:00
pipeline(
fs.createReadStream('object.json')
2021-03-27 14:26:39 +01:00
.setEncoding('utf8'),
2019-08-25 18:13:27 +02:00
new Transform({
decodeStrings: false, // Accept string input rather than Buffers
construct(callback) {
this.data = '';
callback();
},
transform(chunk, encoding, callback) {
this.data += chunk;
callback();
},
flush(callback) {
try {
// Make sure is valid json.
JSON.parse(this.data);
this.push(this.data);
2021-11-10 10:42:17 -06:00
callback();
2019-08-25 18:13:27 +02:00
} catch (err) {
callback(err);
}
2022-11-17 08:19:12 -05:00
},
2019-08-25 18:13:27 +02:00
}),
fs.createWriteStream('valid-object.json'),
(err) => {
if (err) {
console.error('failed', err);
} else {
console.log('completed');
}
2022-11-17 08:19:12 -05:00
},
2019-08-25 18:13:27 +02:00
);
```
2020-06-14 14:49:34 -07:00
#### An example duplex stream
2013-02-28 15:42:55 -08:00
2018-04-29 20:46:41 +03:00
The following illustrates a simple example of a `Duplex` stream that wraps a
2016-05-23 22:30:41 -07:00
hypothetical lower-level source object to which data can be written, and
from which data can be read, albeit using an API that is not compatible with
Node.js streams.
2018-04-29 20:46:41 +03:00
The following illustrates a simple example of a `Duplex` stream that buffers
incoming written data via the [`Writable` ][] interface that is read back out
via the [`Readable` ][] interface.
2013-02-28 15:42:55 -08:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Duplex } = require('node:stream');
2016-05-23 22:30:41 -07:00
const kSource = Symbol('source');
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
class MyDuplex extends Duplex {
constructor(source, options) {
2016-05-02 07:03:23 +02:00
super(options);
2016-05-23 22:30:41 -07:00
this[kSource] = source;
2016-05-02 07:03:23 +02:00
}
2013-02-28 15:42:55 -08:00
2016-05-23 22:30:41 -07:00
_write(chunk, encoding, callback) {
2019-07-07 20:56:12 +03:00
// The underlying source only deals with strings.
2016-05-23 22:30:41 -07:00
if (Buffer.isBuffer(chunk))
2016-09-02 11:47:27 +02:00
chunk = chunk.toString();
this[kSource].writeSomeData(chunk);
2016-05-23 22:30:41 -07:00
callback();
}
2013-02-28 15:42:55 -08:00
2016-05-23 22:30:41 -07:00
_read(size) {
this[kSource].fetchSomeData(size, (data, encoding) => {
this.push(Buffer.from(data, encoding));
});
2013-02-28 15:42:55 -08:00
}
2016-05-02 07:03:23 +02:00
}
2013-02-28 15:42:55 -08:00
```
2018-04-29 20:46:41 +03:00
The most important aspect of a `Duplex` stream is that the `Readable` and
`Writable` sides operate independently of one another despite co-existing within
a single object instance.
2010-10-28 23:18:16 +11:00
2020-06-14 14:49:34 -07:00
#### Object mode duplex streams
2011-02-18 18:30:15 -05:00
2018-04-29 20:46:41 +03:00
For `Duplex` streams, `objectMode` can be set exclusively for either the
`Readable` or `Writable` side using the `readableObjectMode` and
`writableObjectMode` options respectively.
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
In the following example, for instance, a new `Transform` stream (which is a
type of [`Duplex` ][] stream) is created that has an object mode `Writable` side
2016-09-01 15:55:42 +01:00
that accepts JavaScript numbers that are converted to hexadecimal strings on
2018-04-29 20:46:41 +03:00
the `Readable` side.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Transform } = require('node:stream');
2015-11-05 14:54:10 -05:00
2019-07-07 20:56:12 +03:00
// All Transform streams are also Duplex Streams.
2016-05-23 22:30:41 -07:00
const myTransform = new Transform({
writableObjectMode: true,
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
transform(chunk, encoding, callback) {
2019-07-07 20:56:12 +03:00
// Coerce the chunk to a number if necessary.
2016-05-23 22:30:41 -07:00
chunk |= 0;
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
// Transform the chunk into something else.
const data = chunk.toString(16);
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
// Push the data onto the readable queue.
callback(null, '0'.repeat(data.length % 2) + data);
2022-11-17 08:19:12 -05:00
},
2016-05-23 22:30:41 -07:00
});
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
myTransform.setEncoding('ascii');
myTransform.on('data', (chunk) => console.log(chunk));
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
myTransform.write(1);
2016-11-08 21:04:57 +01:00
// Prints: 01
2016-05-23 22:30:41 -07:00
myTransform.write(10);
2016-11-08 21:04:57 +01:00
// Prints: 0a
2016-05-23 22:30:41 -07:00
myTransform.write(100);
2016-11-08 21:04:57 +01:00
// Prints: 64
2016-05-23 22:30:41 -07:00
```
2015-11-05 14:54:10 -05:00
2020-06-14 14:49:34 -07:00
### Implementing a transform stream
2015-11-05 14:54:10 -05:00
2018-04-29 20:46:41 +03:00
A [`Transform` ][] stream is a [`Duplex` ][] stream where the output is computed
2016-05-23 22:30:41 -07:00
in some way from the input. Examples include [zlib][] streams or [crypto][]
streams that compress, encrypt, or decrypt data.
2013-07-15 16:56:02 -07:00
2018-02-05 21:55:16 -08:00
There is no requirement that the output be the same size as the input, the same
2018-04-29 20:46:41 +03:00
number of chunks, or arrive at the same time. For example, a `Hash` stream will
2018-02-05 21:55:16 -08:00
only ever have a single chunk of output which is provided when the input is
ended. A `zlib` stream will produce output that is either much smaller or much
larger than its input.
2013-07-15 16:56:02 -07:00
2018-04-29 20:46:41 +03:00
The `stream.Transform` class is extended to implement a [`Transform` ][] stream.
2015-02-03 01:12:41 +00:00
2016-05-23 22:30:41 -07:00
The `stream.Transform` class prototypically inherits from `stream.Duplex` and
2020-06-06 11:20:14 +05:30
implements its own versions of the `writable._write()` and
2021-10-10 21:55:04 -07:00
[`readable._read()` ][] methods. Custom `Transform` implementations _must_
implement the [`transform._transform()` ][stream-_transform] method and _may_
2020-06-06 11:20:14 +05:30
also implement the [`transform._flush()` ][stream-_flush] method.
2015-02-03 01:12:41 +00:00
2018-04-29 20:46:41 +03:00
Care must be taken when using `Transform` streams in that data written to the
stream can cause the `Writable` side of the stream to become paused if the
output on the `Readable` side is not consumed.
2015-02-03 01:12:41 +00:00
2019-12-24 15:09:29 -08:00
#### `new stream.Transform([options])`
2015-02-03 01:12:41 +00:00
2018-04-29 20:46:41 +03:00
* `options` {Object} Passed to both `Writable` and `Readable`
2016-05-23 22:30:41 -07:00
constructors. Also has the following fields:
* `transform` {Function} Implementation for the
[`stream._transform()` ][stream-_transform] method.
* `flush` {Function} Implementation for the [`stream._flush()` ][stream-_flush]
method.
2015-02-03 01:12:41 +00:00
2018-12-14 22:22:40 -05:00
<!-- eslint - disable no - useless - constructor -->
2021-10-10 21:55:04 -07:00
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const { Transform } = require('node:stream');
2015-10-30 06:59:21 -04:00
2016-05-23 22:30:41 -07:00
class MyTransform extends Transform {
constructor(options) {
super(options);
2017-05-21 21:53:57 +03:00
// ...
2015-02-03 01:12:41 +00:00
}
2016-05-23 22:30:41 -07:00
}
```
2015-02-03 01:12:41 +00:00
2016-05-23 22:30:41 -07:00
Or, when using pre-ES6 style constructors:
2015-10-30 06:59:21 -04:00
2016-05-23 22:30:41 -07:00
```js
2022-04-20 10:23:41 +02:00
const { Transform } = require('node:stream');
const util = require('node:util');
2015-10-30 06:59:21 -04:00
2016-05-23 22:30:41 -07:00
function MyTransform(options) {
if (!(this instanceof MyTransform))
return new MyTransform(options);
Transform.call(this, options);
}
util.inherits(MyTransform, Transform);
2015-02-03 01:12:41 +00:00
```
2020-06-14 14:49:34 -07:00
Or, using the simplified constructor approach:
2016-01-17 18:39:07 +01:00
```js
2022-04-20 10:23:41 +02:00
const { Transform } = require('node:stream');
2015-10-30 06:59:21 -04:00
2016-05-23 22:30:41 -07:00
const myTransform = new Transform({
transform(chunk, encoding, callback) {
// ...
2022-11-17 08:19:12 -05:00
},
2015-11-05 14:54:10 -05:00
});
```
2020-06-14 13:56:18 -07:00
#### Event: `'end'`
2016-01-17 18:39:07 +01:00
2020-06-14 13:56:18 -07:00
The [`'end'` ][] event is from the `stream.Readable` class. The `'end'` event is
emitted after all data has been output, which occurs after the callback in
2019-08-05 16:07:49 +02:00
[`transform._flush()` ][stream-_flush] has been called. In the case of an error,
2020-06-14 13:56:18 -07:00
`'end'` should not be emitted.
#### Event: `'finish'`
The [`'finish'` ][] event is from the `stream.Writable` class. The `'finish'`
event is emitted after [`stream.end()` ][stream-end] is called and all chunks
have been processed by [`stream._transform()` ][stream-_transform]. In the case
of an error, `'finish'` should not be emitted.
2015-10-30 06:59:21 -04:00
2019-12-24 15:09:29 -08:00
#### `transform._flush(callback)`
2015-10-30 06:59:21 -04:00
2016-05-23 22:30:41 -07:00
* `callback` {Function} A callback function (optionally with an error
2016-06-07 22:54:51 +02:00
argument and data) to be called when remaining data has been flushed.
2015-10-30 06:59:21 -04:00
2018-02-05 21:55:16 -08:00
This function MUST NOT be called by application code directly. It should be
2018-04-29 20:46:41 +03:00
implemented by child classes, and called by the internal `Readable` class
methods only.
2015-10-30 06:59:21 -04:00
2016-05-23 22:30:41 -07:00
In some cases, a transform operation may need to emit an additional bit of
data at the end of the stream. For example, a `zlib` compression stream will
store an amount of internal state used to optimally compress the output. When
the stream ends, however, that additional data needs to be flushed so that the
compressed data will be complete.
2015-02-03 01:12:41 +00:00
2021-10-10 21:55:04 -07:00
Custom [`Transform` ][] implementations _may_ implement the `transform._flush()`
2016-05-23 22:30:41 -07:00
method. This will be called when there is no more written data to be consumed,
but before the [`'end'` ][] event is emitted signaling the end of the
2018-04-29 20:46:41 +03:00
[`Readable` ][] stream.
2016-01-17 18:39:07 +01:00
2019-11-16 18:13:18 -05:00
Within the `transform._flush()` implementation, the `transform.push()` method
2016-05-23 22:30:41 -07:00
may be called zero or more times, as appropriate. The `callback` function must
be called when the flush operation is complete.
2015-10-30 06:59:21 -04:00
2016-06-19 00:19:41 +03:00
The `transform._flush()` method is prefixed with an underscore because it is
2016-05-23 22:30:41 -07:00
internal to the class that defines it, and should never be called directly by
user programs.
2015-11-05 14:54:10 -05:00
2019-12-24 15:09:29 -08:00
#### `transform._transform(chunk, encoding, callback)`
2015-11-05 14:54:10 -05:00
2019-01-12 22:35:34 +00:00
* `chunk` {Buffer|string|any} The `Buffer` to be transformed, converted from
the `string` passed to [`stream.write()` ][stream-write]. If the stream's
`decodeStrings` option is `false` or the stream is operating in object mode,
the chunk will not be converted & will be whatever was passed to
[`stream.write()` ][stream-write].
2017-02-04 16:15:33 +01:00
* `encoding` {string} If the chunk is a string, then this is the
2016-05-23 22:30:41 -07:00
encoding type. If chunk is a buffer, then this is the special
2019-10-23 21:28:42 -07:00
value `'buffer'` . Ignore it in that case.
2016-05-23 22:30:41 -07:00
* `callback` {Function} A callback function (optionally with an error
argument and data) to be called after the supplied `chunk` has been
processed.
2015-10-30 06:59:21 -04:00
2018-02-05 21:55:16 -08:00
This function MUST NOT be called by application code directly. It should be
2018-04-29 20:46:41 +03:00
implemented by child classes, and called by the internal `Readable` class
methods only.
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
All `Transform` stream implementations must provide a `_transform()`
2016-06-19 00:19:41 +03:00
method to accept input and produce output. The `transform._transform()`
2016-05-23 22:30:41 -07:00
implementation handles the bytes being written, computes an output, then passes
2019-11-16 18:13:18 -05:00
that output off to the readable portion using the `transform.push()` method.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
The `transform.push()` method may be called zero or more times to generate
output from a single input chunk, depending on how much is to be output
as a result of the chunk.
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
It is possible that no output is generated from any given chunk of input data.
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
The `callback` function must be called only when the current chunk is completely
consumed. The first argument passed to the `callback` must be an `Error` object
if an error occurred while processing the input or `null` otherwise. If a second
argument is passed to the `callback` , it will be forwarded on to the
2023-07-09 00:11:30 +04:00
`transform.push()` method, but only if the first argument is falsy. In other
words, the following are equivalent:
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
```js
2017-04-21 17:38:31 +03:00
transform.prototype._transform = function(data, encoding, callback) {
2016-05-23 22:30:41 -07:00
this.push(data);
callback();
};
2013-07-15 16:56:02 -07:00
2017-04-21 17:38:31 +03:00
transform.prototype._transform = function(data, encoding, callback) {
2016-05-23 22:30:41 -07:00
callback(null, data);
};
```
2013-07-15 16:56:02 -07:00
2016-06-19 00:19:41 +03:00
The `transform._transform()` method is prefixed with an underscore because it
2016-05-23 22:30:41 -07:00
is internal to the class that defines it, and should never be called directly by
user programs.
2013-07-15 16:56:02 -07:00
2018-04-02 08:38:48 +03:00
`transform._transform()` is never called in parallel; streams implement a
2017-07-17 10:02:02 +02:00
queue mechanism, and to receive the next chunk, `callback` must be
2017-07-19 13:49:26 +02:00
called, either synchronously or asynchronously.
2017-07-17 10:02:02 +02:00
2019-12-24 15:09:29 -08:00
#### Class: `stream.PassThrough`
2016-05-23 22:30:41 -07:00
2018-04-29 20:46:41 +03:00
The `stream.PassThrough` class is a trivial implementation of a [`Transform` ][]
2016-05-23 22:30:41 -07:00
stream that simply passes the input bytes across to the output. Its purpose is
primarily for examples and testing, but there are some use cases where
`stream.PassThrough` is useful as a building block for novel sorts of streams.
2013-07-15 16:56:02 -07:00
2020-06-14 14:49:34 -07:00
## Additional notes
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
<!-- type=misc -->
2013-07-15 16:56:02 -07:00
2020-06-14 14:49:34 -07:00
### Streams compatibility with async generators and async iterators
2019-05-12 19:00:53 +02:00
With the support of async generators and iterators in JavaScript, async
generators are effectively a first-class language-level stream construct at
this point.
Some common interop cases of using Node.js streams with async generators
and async iterators are provided below.
2020-06-14 14:49:34 -07:00
#### Consuming readable streams with async iterators
2019-05-12 19:00:53 +02:00
```js
(async function() {
for await (const chunk of readable) {
console.log(chunk);
}
})();
```
2019-08-06 11:34:51 +02:00
Async iterators register a permanent error handler on the stream to prevent any
unhandled post-destroy errors.
2020-06-14 14:49:34 -07:00
#### Creating readable streams with async generators
2019-05-12 19:00:53 +02:00
2020-06-14 14:49:34 -07:00
A Node.js readable stream can be created from an asynchronous generator using
2019-08-25 18:13:27 +02:00
the `Readable.from()` utility method:
2019-05-12 19:00:53 +02:00
```js
2022-04-20 10:23:41 +02:00
const { Readable } = require('node:stream');
2019-05-12 19:00:53 +02:00
2021-06-17 22:25:34 +02:00
const ac = new AbortController();
const signal = ac.signal;
2019-05-12 19:00:53 +02:00
async function * generate() {
yield 'a';
2021-06-17 22:25:34 +02:00
await someLongRunningFn({ signal });
2019-05-12 19:00:53 +02:00
yield 'b';
yield 'c';
}
const readable = Readable.from(generate());
2021-06-17 22:25:34 +02:00
readable.on('close', () => {
ac.abort();
});
2019-05-12 19:00:53 +02:00
readable.on('data', (chunk) => {
console.log(chunk);
});
```
2020-06-14 14:49:34 -07:00
#### Piping to writable streams from async iterators
2019-05-12 19:00:53 +02:00
2020-06-21 12:37:18 -05:00
When writing to a writable stream from an async iterator, ensure correct
handling of backpressure and errors. [`stream.pipeline()` ][] abstracts away
the handling of backpressure and backpressure-related errors:
2019-05-12 19:00:53 +02:00
```js
2022-04-20 10:23:41 +02:00
const fs = require('node:fs');
const { pipeline } = require('node:stream');
const { pipeline: pipelinePromise } = require('node:stream/promises');
2019-05-12 19:00:53 +02:00
2019-07-24 01:28:09 +08:00
const writable = fs.createWriteStream('./file');
2019-05-12 19:00:53 +02:00
2021-06-17 22:25:34 +02:00
const ac = new AbortController();
const signal = ac.signal;
const iterator = createIterator({ signal });
2020-06-21 12:37:18 -05:00
// Callback Pattern
pipeline(iterator, writable, (err, value) => {
if (err) {
console.error(err);
} else {
console.log(value, 'value returned');
2019-05-12 19:00:53 +02:00
}
2021-06-17 22:25:34 +02:00
}).on('close', () => {
ac.abort();
2020-06-21 12:37:18 -05:00
});
2019-09-03 13:34:36 -04:00
2020-06-21 12:37:18 -05:00
// Promise Pattern
pipelinePromise(iterator, writable)
.then((value) => {
console.log(value, 'value returned');
})
2021-06-17 22:25:34 +02:00
.catch((err) => {
console.error(err);
ac.abort();
});
2019-05-12 19:00:53 +02:00
```
<!-- type=misc -->
2020-06-14 14:49:34 -07:00
### Compatibility with older Node.js versions
2013-07-15 16:56:02 -07:00
<!-- type=misc -->
2018-10-06 17:01:25 -07:00
Prior to Node.js 0.10, the `Readable` stream interface was simpler, but also
less powerful and less useful.
2013-07-15 16:56:02 -07:00
2018-10-06 17:03:49 -07:00
* Rather than waiting for calls to the [`stream.read()` ][stream-read] method,
2016-05-23 22:30:41 -07:00
[`'data'` ][] events would begin emitting immediately. Applications that
would need to perform some amount of work to decide how to handle data
were required to store read data into buffers so the data would not be lost.
2016-02-02 20:34:29 +03:00
* The [`stream.pause()` ][stream-pause] method was advisory, rather than
2016-05-23 22:30:41 -07:00
guaranteed. This meant that it was still necessary to be prepared to receive
2021-10-10 21:55:04 -07:00
[`'data'` ][] events _even when the stream was in a paused state_ .
2016-05-23 22:30:41 -07:00
2018-11-05 20:40:07 -08:00
In Node.js 0.10, the [`Readable` ][] class was added. For backward
2018-04-29 20:46:41 +03:00
compatibility with older Node.js programs, `Readable` streams switch into
"flowing mode" when a [`'data'` ][] event handler is added, or when the
2016-05-23 22:30:41 -07:00
[`stream.resume()` ][stream-resume] method is called. The effect is that, even
when not using the new [`stream.read()` ][stream-read] method and
[`'readable'` ][] event, it is no longer necessary to worry about losing
2016-02-02 20:34:29 +03:00
[`'data'` ][] chunks.
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
While most applications will continue to function normally, this introduces an
edge case in the following conditions:
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
* No [`'data'` ][] event listener is added.
2016-02-02 20:34:29 +03:00
* The [`stream.resume()` ][stream-resume] method is never called.
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-17 18:24:02 -07:00
* The stream is not piped to any writable destination.
2013-07-15 16:56:02 -07:00
For example, consider the following code:
2016-01-17 18:39:07 +01:00
```js
2013-07-15 16:56:02 -07:00
// WARNING! BROKEN!
2015-12-14 15:20:25 -08:00
net.createServer((socket) => {
2013-07-15 16:56:02 -07:00
2019-07-07 20:56:12 +03:00
// We add an 'end' listener, but never consume the data.
2015-12-14 15:20:25 -08:00
socket.on('end', () => {
2013-07-15 16:56:02 -07:00
// It will never get here.
2016-05-23 22:30:41 -07:00
socket.end('The message was received but was not processed.\n');
2013-07-15 16:56:02 -07:00
});
}).listen(1337);
```
2018-10-06 17:01:25 -07:00
Prior to Node.js 0.10, the incoming message data would be simply discarded.
However, in Node.js 0.10 and beyond, the socket remains paused forever.
2013-07-15 16:56:02 -07:00
2016-02-02 20:34:29 +03:00
The workaround in this situation is to call the
2016-05-23 22:30:41 -07:00
[`stream.resume()` ][stream-resume] method to begin the flow of data:
2013-07-15 16:56:02 -07:00
2016-01-17 18:39:07 +01:00
```js
2019-07-07 20:56:12 +03:00
// Workaround.
2015-12-14 15:20:25 -08:00
net.createServer((socket) => {
socket.on('end', () => {
2016-05-23 22:30:41 -07:00
socket.end('The message was received but was not processed.\n');
2013-07-15 16:56:02 -07:00
});
2019-03-07 01:03:53 +01:00
// Start the flow of data, discarding it.
2013-07-15 16:56:02 -07:00
socket.resume();
}).listen(1337);
```
2018-04-29 20:46:41 +03:00
In addition to new `Readable` streams switching into flowing mode,
2018-10-06 17:01:25 -07:00
pre-0.10 style streams can be wrapped in a `Readable` class using the
2016-06-19 00:19:41 +03:00
[`readable.wrap()` ][`stream.wrap()` ] method.
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
### `readable.read(0)`
2013-07-15 16:56:02 -07:00
2016-05-23 22:30:41 -07:00
There are some cases where it is necessary to trigger a refresh of the
2015-11-05 14:54:10 -05:00
underlying readable stream mechanisms, without actually consuming any
2016-05-23 22:30:41 -07:00
data. In such cases, it is possible to call `readable.read(0)` , which will
2016-06-06 16:31:49 -07:00
always return `null` .
2015-11-05 14:54:10 -05:00
If the internal read buffer is below the `highWaterMark` , and the
2016-02-02 20:34:29 +03:00
stream is not currently reading, then calling `stream.read(0)` will trigger
a low-level [`stream._read()` ][stream-_read] call.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
While most applications will almost never need to do this, there are
situations within Node.js where this is done, particularly in the
2018-04-29 20:46:41 +03:00
`Readable` stream class internals.
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
### `readable.push('')`
2015-11-05 14:54:10 -05:00
2016-05-23 22:30:41 -07:00
Use of `readable.push('')` is not recommended.
2015-11-05 14:54:10 -05:00
2024-03-20 18:27:29 +01:00
Pushing a zero-byte {string}, {Buffer}, {TypedArray} or {DataView} to a stream
that is not in object mode has an interesting side effect.
Because it _is_ a call to
2016-05-23 22:30:41 -07:00
[`readable.push()` ][stream-push], the call will end the reading process.
However, because the argument is an empty string, no data is added to the
readable buffer so there is nothing for a user to consume.
2012-06-06 15:05:18 -04:00
2017-11-27 15:33:36 -05:00
### `highWaterMark` discrepancy after calling `readable.setEncoding()`
2017-06-03 16:11:32 -04:00
The use of `readable.setEncoding()` will change the behavior of how the
`highWaterMark` operates in non-object mode.
Typically, the size of the current buffer is measured against the
`highWaterMark` in _bytes_ . However, after `setEncoding()` is called, the
comparison function will begin to measure the buffer's size in _characters_ .
This is not a problem in common cases with `latin1` or `ascii` . But it is
advised to be mindful about this behavior when working with strings that could
contain multi-byte characters.
2021-07-04 20:39:17 -07:00
[API for stream consumers]: #api -for-stream-consumers
[API for stream implementers]: #api -for-stream-implementers
[Compatibility]: #compatibility -with-older-nodejs-versions
[HTTP requests, on the client]: http.md#class -httpclientrequest
[HTTP responses, on the server]: http.md#class -httpserverresponse
[TCP sockets]: net.md#class -netsocket
[Three states]: #three -states
[`'data'` ]: #event -data
[`'drain'` ]: #event -drain
[`'end'` ]: #event -end
[`'finish'` ]: #event -finish
[`'readable'` ]: #event -readable
[`Duplex` ]: #class -streamduplex
[`EventEmitter` ]: events.md#class -eventemitter
[`Readable` ]: #class -streamreadable
2017-05-08 09:30:13 -07:00
[`Symbol.hasInstance` ]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance
2021-07-04 20:39:17 -07:00
[`Transform` ]: #class -streamtransform
[`Writable` ]: #class -streamwritable
[`fs.createReadStream()` ]: fs.md#fscreatereadstreampath -options
[`fs.createWriteStream()` ]: fs.md#fscreatewritestreampath -options
[`net.Socket` ]: net.md#class -netsocket
[`process.stderr` ]: process.md#processstderr
[`process.stdin` ]: process.md#processstdin
[`process.stdout` ]: process.md#processstdout
[`readable._read()` ]: #readable_readsize
2022-10-31 15:57:02 +02:00
[`readable.compose(stream)` ]: #readablecomposestream -options
2022-01-30 16:07:32 +02:00
[`readable.map` ]: #readablemapfn -options
2021-07-04 20:39:17 -07:00
[`readable.push('')` ]: #readablepush
[`readable.setEncoding()` ]: #readablesetencodingencoding
[`stream.Readable.from()` ]: #streamreadablefromiterable -options
[`stream.addAbortSignal()` ]: #streamaddabortsignalsignal -stream
2022-10-31 15:57:02 +02:00
[`stream.compose` ]: #streamcomposestreams
2021-07-04 20:39:17 -07:00
[`stream.cork()` ]: #writablecork
2024-07-26 01:09:23 -07:00
[`stream.duplexPair()` ]: #streamduplexpairoptions
2021-07-04 20:39:17 -07:00
[`stream.finished()` ]: #streamfinishedstream -options-callback
[`stream.pipe()` ]: #readablepipedestination -options
[`stream.pipeline()` ]: #streampipelinesource -transforms-destination-callback
[`stream.uncork()` ]: #writableuncork
[`stream.unpipe()` ]: #readableunpipedestination
[`stream.wrap()` ]: #readablewrapstream
[`writable._final()` ]: #writable_finalcallback
[`writable._write()` ]: #writable_writechunk -encoding-callback
[`writable._writev()` ]: #writable_writevchunks -callback
[`writable.cork()` ]: #writablecork
[`writable.end()` ]: #writableendchunk -encoding-callback
[`writable.uncork()` ]: #writableuncork
[`writable.writableFinished` ]: #writablewritablefinished
[`zlib.createDeflate()` ]: zlib.md#zlibcreatedeflateoptions
[child process stdin]: child_process.md#subprocessstdin
[child process stdout and stderr]: child_process.md#subprocessstdout
2020-09-14 17:09:13 +02:00
[crypto]: crypto.md
2021-07-04 20:39:17 -07:00
[fs read streams]: fs.md#class -fsreadstream
[fs write streams]: fs.md#class -fswritestream
[http-incoming-message]: http.md#class -httpincomingmessage
[hwm-gotcha]: #highwatermark -discrepancy-after-calling-readablesetencoding
[object-mode]: #object -mode
[readable-_construct]: #readable_constructcallback
[readable-_destroy]: #readable_destroyerr -callback
[readable-destroy]: #readabledestroyerror
[stream-_final]: #writable_finalcallback
[stream-_flush]: #transform_flushcallback
[stream-_read]: #readable_readsize
[stream-_transform]: #transform_transformchunk -encoding-callback
[stream-_write]: #writable_writechunk -encoding-callback
[stream-_writev]: #writable_writevchunks -callback
[stream-end]: #writableendchunk -encoding-callback
2022-12-15 16:34:23 +01:00
[stream-finished]: #streamfinishedstream -options-callback
[stream-finished-promise]: #streamfinishedstream -options
2021-07-04 20:39:17 -07:00
[stream-pause]: #readablepause
2022-12-15 16:34:23 +01:00
[stream-pipeline]: #streampipelinesource -transforms-destination-callback
[stream-pipeline-promise]: #streampipelinesource -transforms-destination-options
2021-07-04 20:39:17 -07:00
[stream-push]: #readablepushchunk -encoding
[stream-read]: #readablereadsize
[stream-resume]: #readableresume
[stream-uncork]: #writableuncork
[stream-write]: #writablewritechunk -encoding-callback
[writable-_construct]: #writable_constructcallback
[writable-_destroy]: #writable_destroyerr -callback
[writable-destroy]: #writabledestroyerror
[writable-new]: #new -streamwritableoptions
2020-09-14 17:09:13 +02:00
[zlib]: zlib.md