Course contentsShow
← JavaScript
Lesson 1569 of 2,72334. Streams and BuffersFree lesson

Chaining Multiple Transforms

Piping transform streams together to build complex data processing pipelines.

Chaining Multiple Transforms

You'll learn how to connect multiple transform streams together to build powerful, modular data processing pipelines.

The Power of Piping

Just like in Unix command-line tools where you pipe output from one command into another (cat file.txt | grep "error" | sort), Node.js streams let you pipe the output of one transform stream into another. Each transform performs one specific task, and by chaining them, you create complex processing workflows that remain clean and maintainable.

How Stream Chaining Works

When you pipe streams together, data flows through each transform in sequence. The output of the first transform becomes the input of the second, and so on. This is done using the .pipe() method:

const { Transform } = require('stream');

// Transform 1: Convert to uppercase
const uppercase = new Transform({
  transform(chunk, encoding, callback) {
    callback(null, chunk.toString().toUpperCase());
  }
});

// Transform 2: Add prefix
const addPrefix = new Transform({
  transform(chunk, encoding, callback) {
    callback(null, `[PROCESSED] ${chunk}`);
  }
});

// Chain them together
process.stdin
  .pipe(uppercase)      // First transformation
  .pipe(addPrefix)      // Second transformation
  .pipe(process.stdout); // Output

Each transform is a separate, reusable component. You can add, remove, or reorder transforms without touching the others—perfect for building flexible data processors like compression→encryption→upload or parse→validate→transform→output pipelines.

Why Chain?

Chaining keeps each transform focused on one responsibility, making your code easier to test, debug, and reuse. It's also memory-efficient since data flows through piece by piece rather than loading everything into memory.

Key Takeaway: Chaining transforms with .pipe() creates modular, composable pipelines where each stream does one job well, and complex processing emerges from their combination.