PassThrough Streams
Learn about PassThrough streams, observability, and how to implement late piping and lazy stream patterns.
We'll cover the following...
There’s a fifth type of stream that’s worth mentioning and it’s called a PassThrough
stream. This type of stream is a special type of Transform
stream that outputs every data chunk without applying any transformation.
PassThrough
is possibly the most underrated type of stream, but there are actually several circumstances in which it can be a very valuable tool in our toolkit. For instance, PassThrough
streams can be useful for observability or for implementing late piping and lazy stream patterns.
Observability
If we want to observe how much data is flowing through one or more streams, we can do so by attaching a data
event listener to a PassThrough
instance and then piping this instance in a given point of a stream pipeline. Let’s look at a simplified example to be able to appreciate this concept.
import { PassThrough } from 'stream'let bytesWritten = 0const monitor = new PassThrough()monitor.on('data', (chunk) => {bytesWritten += chunk.length})monitor.on('finish', () => {console.log(`${bytesWritten} bytes written`)})monitor.write('Hello!')monitor.end()
In this example, we’re creating a new instance of PassThrough
and using the data
event to count how many bytes are flowing through the stream. We also use the finish
event to dump the total amount to the console. Finally, we write some data directly into the stream using the write()
and end()
methods. This is just an illustrative example; in a more realistic scenario, we would be piping our monitor
instance in a given point of a ...