Typorithm · medium

Readable → Writable (pipe)

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

Step 1 of 8 · intro card

Learn Readable → Writable (pipe)

0%

Streams

Streams process data in chunks instead of loading a whole file into memory. createReadStream is a Readable, createWriteStream a Writable; .pipe() wires source to destination and handles backpressure automatically, keeping memory constant for any file size.

A stream processes data in chunks rather than reading an entire file into memory at once, so memory stays constant no matter how large the file is.

createReadStream produces a Readable (the source of chunks) and createWriteStream produces a Writable (the sink). Calling src.pipe(dst) wires the two together.

.pipe() pulls chunks from the source and pushes them into the destination, automatically applying backpressure so a slow Writable never overwhelms memory. The "end" event fires when the source is exhausted.

01

createReadStream is a Readable; createWriteStream is a Writable.

02

.pipe() wires source → destination and handles backpressure.

03

The "end" event fires when the source is exhausted.

import { createReadStream, createWriteStream } from "node:fs";

const src = createReadStream("in.txt");
const dst = createWriteStream("out.txt");
src.pipe(dst);
src.on("end", () => console.log("done"));

Read the implementation

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

1. Readable source

createReadStream returns a Readable that emits the file's contents as a sequence of chunks.

const src = createReadStream("in.txt");

2. Writable destination

createWriteStream returns a Writable that consumes chunks and writes them to the file.

const dst = createWriteStream("out.txt");

3. Wire source to dest

.pipe() connects the Readable to the Writable and manages backpressure automatically.

src.pipe(dst);

4. Source exhausted

The "end" event fires once the Readable has emitted all of its data.

src.on("end", () => console.log("done"));

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.

Readable → Writable (pipe): Lesson & Practice · Typorithm | ApplyDjinn