Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Class

Transform

The stream.Transform class is a Duplex stream whose readable output is computed from writable input. Custom transforms implement _transform() and may implement _flush() for final processing.

How it works

Transform is a Duplex stream that accepts writable input and produces readable output derived from that input. Subclasses or configured instances process each chunk through _transform, optionally perform final work in _flush, and signal completion through the callback.

Syntax

new stream.Transform([options])

Parameters

NameTypeDescription
optionsObjectOptions passed to the Duplex constructor; the page documents Transform construction through the inherited stream options.

Returns

A stream.Transform instance.

Key points

  • Use Transform when readable output must be derived from writable input.
  • A Transform can receive data through writable methods and expose results through readable methods.
  • Create specialized transforms by extending the stream.Transform class.
  • The base class coordinates the writable and readable sides; custom implementations focus on transformation logic.
  • Implement _flush when finalization requires emitting data or completing buffered work after input ends.

Common mistakes

  • A custom Transform must implement _transform; omitting it leaves the stream without its required chunk-processing operation.

Example: Uppercase transform

import { Transform } from 'node:stream';

const upper = new Transform({
  transform(chunk, encoding, callback) {
    callback(null, chunk.toString().toUpperCase());
  }
});

upper.on('data', chunk => process.stdout.write(chunk));
upper.end('hello, transform!');

Creates a transform stream that converts each input chunk to uppercase and emits the result.

When to use it

Streaming data conversion

Convert, filter, or enrich data while it flows through a pipeline.

Encoding and compression

Compress, encrypt, decrypt, or otherwise encode chunks incrementally.

Finalization of buffered data

Collect buffered state and emit a final result when input ends.

Interview questions

QWhat is a Transform stream?

Transform extends Duplex, so it implements both writable and readable sides. Its output is computed from the input rather than merely forwarding it.

Follow-up: How would a transform emit several outputs for one input? Call the callback without output, then use push() for additional readable chunks before completing the operation.

QHow do you implement custom Transform behavior?

Implement _transform(chunk, encoding, callback). Process the supplied chunk, then invoke callback with an optional error and optional output data.

Follow-up: What happens if processing is asynchronous? Delay the callback until the asynchronous work completes, and pass any failure as its error argument.

QWhat is the purpose of _flush()?

_flush() is optional and is used for work that must occur after all input has been processed. It can produce final output before the stream finishes.

Follow-up: When is _flush useful? Use it for buffered or aggregated state, such as emitting a final remainder after the last input chunk.

  • stream.Duplex
  • stream.Readable
  • stream.Writable
  • stream.pipeline()
  • stream.Transform._transform()
  • stream.Transform._flush()

Source: Stream | Node.js v24.15.0 Documentation. Checked 2026-10-15.