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";Typorithm · medium
Step 1 of 8 · intro card
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).
A Transform is a duplex stream (readable + writable).
transform(chunk, encoding, callback) processes each chunk.
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);Connect each part of the code to the concept before you type.
Transform is the base class for a duplex stream that reads input and writes a modified output.
import { Transform } from "node:stream";Constructing Transform with a transform option defines how each chunk is processed.
const upper = new Transform({The transform(chunk, encoding, callback) hook runs once per chunk that flows through.
transform(chunk, encoding, callback) {callback(err, data) signals completion; chunk is a Buffer, so toString() converts it first.
callback(null, chunk.toString().toUpperCase());Chaining pipes builds a pipeline: stdin → transform → stdout.
process.stdin.pipe(upper).pipe(process.stdout);Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.