Node.js Reference ยท Streams and readline
Node.js stream.Duplex class
Reference for stream.Duplex in Node.js: a stream with independent readable and writable sides, allowHalfOpen, per-side options and Duplex.from().
A Duplex stream is both a Readable and a Writable. The two sides are independent: what you write does not have to be what you read. A TCP socket is the classic example. Bytes you write travel to the peer, and bytes you read come from the peer; the two directions are separate flows that happen to share one object.
You create a duplex by supplying both a read() and a write() implementation. If the output is computed from the input, you probably want a Transform instead, which is a duplex whose two sides are linked. Use Duplex directly for sockets, protocol wrappers, or test doubles where input and output are unrelated.
import { Duplex } from 'node:stream'; const d = new Duplex({ read() {}, write(chunk, enc, cb) { cb(); } });
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
new stream.Duplex([options]) Duplex.from(src): Duplex
| Option | Description |
|---|---|
read(size) |
Source implementation. Call this.push(). |
write(chunk, encoding, cb) |
Sink implementation. |
allowHalfOpen |
Default true. If false, ending the readable side also ends the writable side automatically. |
readableObjectMode, writableObjectMode |
Object mode for each side separately. objectMode sets both. |
readableHighWaterMark, writableHighWaterMark |
Buffer limits per side. highWaterMark sets both. |
decodeStrings, final, writev, destroy, signal |
As for Writable and Readable. |
Duplex.from(src) builds a duplex from a stream, an async iterable, an async generator function (as above), a promise, a { readable, writable } pair or a Web ReadableWritablePair. Related converters: Duplex.toWeb() and Duplex.fromWeb().
Errors: an error or destroy(err) destroys both sides and emits 'error' once.
Usage notes
- Options for one side use prefixes (
readable...,writable...). A plainhighWaterMarkorobjectModeapplies to both, which is rarely what you want when one side is binary and the other is objects. push()feeds the readable side,write()feeds the writable side. Mixing them up is the commonest bug: inside awrite()implementation, callingthis.push()is how you answer, but it does not complete the write.- With the default
allowHalfOpen: trueyou must end each direction yourself. A server that only callsend()on its own writable side while the client keeps sending is a legitimate half-open connection. net.Socket,tls.TLSSocket,zlibstreams andstream.Transformare all duplexes.stream.pipeline(a, duplex, b)works with duplexes in the middle; a duplex used as a pipeline's first or last element only has one of its sides used.- Since Node.js 16.8,
Duplex.from()andstream.compose()make it easy to combine streams without writing a class.