What is Transform Stream in Node.js: Explanation and Example
transform stream in Node.js is a type of stream that can read data, modify or transform it, and then output the changed data. It acts like a middleman that processes data as it passes through, useful for tasks like compression, encryption, or data format changes.How It Works
Think of a transform stream as a factory conveyor belt where raw materials come in one end, get changed or improved in the middle, and the finished product comes out the other end. In Node.js, data flows through streams in chunks, and a transform stream lets you change those chunks as they pass.
Under the hood, a transform stream is both readable and writable. You write data into it, it processes that data (like converting text to uppercase), and then you read the transformed data out. This happens efficiently without waiting for all data to arrive first, making it great for handling large or continuous data flows.
Example
import { Transform } from 'stream'; class UpperCaseTransform extends Transform { _transform(chunk, encoding, callback) { const upperChunk = chunk.toString().toUpperCase(); this.push(upperChunk); callback(); } } const upperCaseStream = new UpperCaseTransform(); upperCaseStream.on('data', (chunk) => { console.log(chunk.toString()); }); upperCaseStream.write('hello '); upperCaseStream.write('world!'); upperCaseStream.end();
When to Use
Use transform streams when you need to change data while it moves through your application without loading it all into memory. This is helpful for:
- Compressing or decompressing files on the fly
- Encrypting or decrypting data streams
- Changing data formats, like converting CSV to JSON
- Filtering or modifying data in real-time
They are perfect for building efficient, scalable data processing pipelines.
Key Points
- A transform stream reads input, modifies it, and outputs the changed data.
- It is both readable and writable, working as a middle step in data flow.
- Transforms data chunk-by-chunk without waiting for all data.
- Commonly used for compression, encryption, and data format changes.