Piping Streams with pipeThrough
You'll learn how to transform streaming data by routing it through processing pipelines without loading everything into memory.
What is pipeThrough()?
When working with a ReadableStream, you often want to transform the data as it flows through—like compressing it, encrypting it, or converting formats. Instead of manually reading chunks, transforming them, and writing them out, pipeThrough() lets you connect a ReadableStream to a TransformStream, creating a new transformed ReadableStream automatically.
Think of it like plumbing: you're connecting pipes together so water (data) flows through one pipe, gets filtered or modified, and comes out the other end—all without storing the water in buckets.
How It Works
The pipeThrough() method takes a TransformStream object (which has both a readable and writable side) and returns a new ReadableStream. The original stream's data flows into the transform's writable side, gets processed, and emerges from the readable side.
const response = await fetch('/large-data.json');
const stream = response.body;
// Create a transform stream (example: text decoding)
const textDecoderStream = new TextDecoderStream();
// Pipe the response stream through the transformer
const decodedStream = stream.pipeThrough(textDecoderStream);
// Now read from the decoded stream
const reader = decodedStream.getReader();
You can chain multiple pipeThrough() calls to create multi-stage processing pipelines—each transformation happens as data flows through, keeping memory usage low even with huge datasets.
Key Takeaway: pipeThrough() connects a ReadableStream to a TransformStream, creating efficient data processing pipelines that transform streaming data on-the-fly without buffering everything in memory.