Node.js
Transform stream
Learn to transform data with a Transform stream.
Transform stream
A Transform stream both reads and writes: it takes an input chunk, transforms it and passes the output chunk. It is created with new Transform({ transform(chunk, encoding, callback) { ... } }).
The transform method is called for each input chunk. You pass the transformed data with callback(null, output); the first argument is the error (null = no error). Example: const upper = new Transform({ transform(chunk, enc, cb) { cb(null, chunk.toString().toUpperCase()); } });. The call upper.write("hello"); upper.end(); produces HELLO. You catch the output with upper.on("data", (c) => console.log(c.toString())). Transform is the middle link of a pipe chain.
Transform and pipe
Transform streams are the middle link of a pipe chain. With source.pipe(transformer).pipe(target) data flows from the source, is transformed and written to the target. This is the idiomatic way to process data fluently.
Example: const upper = new Transform({ transform(c, e, cb) { cb(null, c.toString().toUpperCase()); } }); source.pipe(upper).pipe(target);. Each chunk is uppercased as it flows to the target. It can also be used alone: upper.write("a"); upper.on("data", (c) => console.log(c.toString())); upper.end();. This prints A. Transform is a powerful tool combining Readable and Writable.
Exercise Write a Transform
Create a Transform named upper: its transform(chunk, enc, cb) body is cb(null, chunk.toString().toUpperCase()). Listen for the "data" event: for each c print "Transformed: " + c.toString(). Call upper.write("hello") and upper.end().
Show example solution
const { Transform } = require("stream");
const upper = new Transform({
transform(chunk, enc, cb) { cb(null, chunk.toString().toUpperCase()); }
});
upper.on("data", (c) => console.log("Transformed: " + c.toString()));
upper.write("hello");
upper.end();
Test yourself
-
Complete the transform body
cb(null, chunk.toString().to____Case());
Show answer
cb(null, chunk.toString().toUpperCase());
-
What is the core job of a Transform stream?
new Transform({ transform(c, e, cb) { cb(null, c.toString().toUpperCase()); } })
- Only reads data
- Writes data to memory
- Transforms the incoming chunk and passes on the outgoing one
- Throws an error
Show answer
Transforms the incoming chunk and passes on the outgoing one — Transform takes input, transforms it and passes output.
-
Match each piece to its role
- transform(chunk, ...)
- Processes the input chunk
- cb(null, output)
- Passes the transformed data
- toUpperCase()
- Uppercases letters
- .pipe(transform)
- Adds a transformer to the chain
Next lesson: Summary - Streams