Node.js Reference ยท Streams and readline
Node.js stream.PassThrough class
Reference for stream.PassThrough in Node.js: a Transform that forwards chunks unchanged, used as a placeholder, tap, adapter or in-memory pipe between code.
stream.PassThrough is a Transform whose transform function simply forwards every chunk unchanged. Whatever you write to it appears, in order, on its readable side. It has the full stream interface (buffering, backpressure, events, pipe()), but it adds no behavior of its own.
That makes it a useful connector. You can hand one end to code that expects a writable and the other end to code that expects a readable, tap a stream to observe its data without altering it, or return a stream immediately from a function whose real source is not ready yet.
import { PassThrough } from 'node:stream'; const pass = new PassThrough();
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.PassThrough([options])
| Name | Description |
|---|---|
options |
The same options as Transform/Duplex: highWaterMark, objectMode, readableObjectMode, writableObjectMode, encoding, signal, and so on. |
Returns: a PassThrough instance (a Transform, therefore a Duplex, Readable and Writable). There are no extra methods or events.
Errors: same as any stream. destroy(err) emits 'error'; writing after end() emits ERR_STREAM_WRITE_AFTER_END.
Usage notes
- Its two sides share one buffer budget in practice: if nobody reads, writes eventually return
falseand the producer should wait for'drain'. APassThroughnobody reads from is a memory leak in waiting. - Attaching a
'data'listener starts flow. When thePassThroughsits in line in a pipeline, as in the second example, the listener and the next stage both receive every chunk, so it works as a tap. APassThroughwith only a'data'listener and no other consumer simply discards what it reads. - Typical uses: adapting APIs (give a library a writable and read from the other end), buffering between fast and slow stages, forwarding a stream whose source arrives later, and splitting a source into several consumers with
pipe()calls. - A
PassThroughdoes not copy chunks, so mutating aBufferafter writing it changes what readers see. - Since
stream.pipeline()accepts async functions and generators, many formerPassThroughuses can be written without one. - For text, call
setEncoding('utf8')on the readable side to receive strings instead of buffers.