Typorithm · medium

Transform Streams

stream-transformnodejstransformchunkduplex
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Transform Streams

0%

Streams

A Transform is a duplex stream that reads input and writes a modified output. The transform(chunk, encoding, callback) hook processes each chunk and calls callback(err, data); chunks arrive as Buffers (call toString), and you chain pipes to build a pipeline like stdin → transform → stdout.

A Transform stream is a duplex stream: it is both readable and writable, taking input chunks and producing modified output chunks.

You define behavior with the transform(chunk, encoding, callback) hook, which runs once per chunk. Inside, you call callback(err, data) — passing the processed data as the second argument and null as the error on success.

Chunks arrive as Buffers, so call toString() before string operations. Because a Transform is both ends of a pipe, you can chain: process.stdin.pipe(upper).pipe(process.stdout).

01

A Transform is a duplex stream (readable + writable).

02

transform(chunk, encoding, callback) processes each chunk.

03

Chunks are Buffers — call toString() before string ops.

import { Transform } from "node:stream";

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

Read the implementation

Connect each part of the code to the concept before you type.

1. Import Transform

Transform is the base class for a duplex stream that reads input and writes a modified output.

import { Transform } from "node:stream";

2. Create a Transform

Constructing Transform with a transform option defines how each chunk is processed.

const upper = new Transform({

3. The transform hook

The transform(chunk, encoding, callback) hook runs once per chunk that flows through.

transform(chunk, encoding, callback) {

4. Emit the result

callback(err, data) signals completion; chunk is a Buffer, so toString() converts it first.

callback(null, chunk.toString().toUpperCase());

5. Chain the pipes

Chaining pipes builds a pipeline: stdin → transform → stdout.

process.stdin.pipe(upper).pipe(process.stdout);

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Transform Streams: Lesson & Practice · Typorithm | ApplyDjinn