stream

The stream module provides APIs for handling streaming data. Streams allow processing data piece by piece instead of loading it all at once.


Types of Streams

  • Readable: can be read from (fs.createReadStream).
  • Writable: can be written to (fs.createWriteStream).
  • Duplex: both readable and writable (net.Socket).
  • Transform: duplex streams that modify data (zlib.createGzip).

Core API

  • stream.Readable: implement custom readable streams.
  • stream.Writable: implement custom writable streams.
  • stream.Duplex: implement both.
  • stream.Transform: implement transform.
  • stream.pipeline(src, ...transforms, dest, cb) — pipe with error handling.
  • stream.finished(stream, cb) — notify when done or errored.

Examples (English only)

const fs = require("fs");
const { Transform, pipeline } = require("stream");

// Transform to uppercase
const upper = new Transform({
  transform(chunk, enc, cb) {
    cb(null, chunk.toString().toUpperCase());
  }
});

// Pipe file through transform and out
pipeline(
  fs.createReadStream("input.txt"),
  upper,
  fs.createWriteStream("output.txt"),
  (err) => {
    if (err) console.error("Pipeline failed:", err);
    else console.log("Pipeline succeeded");
  }
);

Notes

  • Streams handle backpressure, preventing memory overload.
  • Always use pipeline instead of chaining .pipe() for better error handling.
  • Use finished to know when a stream is fully consumed.
  • Core modules like fs, net, http use streams extensively.