streams

Reading a two gigabyte log into a string is a choice you will regret Streams process chunks instead

pipeline

import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import { createGzip } from "node:zlib";
import { createWriteStream } from "node:fs";

await pipeline(
  Readable.from(["payload\n"]),
  createGzip(),
  createWriteStream("out.gz")
);

That ran and produced a valid gzip file

Why pipeline over pipe

pipe moves data but does not forward errors the way you expect pipeline handles errors and cleanup on every stage Use pipeline in new code

Backpressure

A readable stream can pause when the writable side is behind Ignoring that signal is how memory grows until the process dies pipeline respects it for you

Async iteration

for await (const chunk of stream) {
  processChunk(chunk);
}

Good when each chunk needs custom handling Still respect backpressure by awaiting your work

Next

Events in 06 events

Posted