CodeBlanks Logo Icon CodeBlanks

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.

Practice 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().

Starter code

index.js
// Import Transform and create an upper instance
// write here

// Listen for the "data" event
// write here

// Write "hello" and call end()
// write here

Sample solution

index.js
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();

Expected output

Dönüştü: MERHABA

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the transform body

Question
cb(null, chunk.toString().to____Case());
Answer
cb(null, chunk.toString().toUpperCase());

Explanation: toUpperCase uppercases letters.

2 What is the core job of a Transform stream?

index.js
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 Correct answer
  • Throws an error

Explanation: Transform takes input, transforms it and passes output.

3 Match each piece to its role

Term Meaning
transform(chunk, ...) Processes the input chunk
cb(null, output) Passes the transformed data
toUpperCase() Uppercases letters
.pipe(transform) Adds a transformer to the chain

All lessons in Intermediate