AceDevHub
Intermediate Node.js Interview QuestionsIntermediateConcept

Node.js · Question 52

What are Duplex and Transform streams in Node.js?

Direct answer

Duplex streams are independent readable and writable sides (like a socket); Transform streams are Duplex variants that modify data in _transform — use for gzip, encryption, line parsing, or NDJSON filtering in a pipeline.

Duplex automatic pipe from write to read — TCP sends and receives independently. Transform links written input to readable output through your transform function.

  • Duplex examples — net.connect(), child_process stdin/stdout.
  • Transform examples — zlib, crypto ciphers, CSV line splitter.
  • _transform(chunk, enc, cb) — call cb(null, outputChunk) zero or more times per input.
  • _flush(cb) — emit trailing data when input ends.
transform-lines.mjs
import { Transform } from "node:stream";
import { pipeline } from "node:stream/promises";
import fs from "node:fs";

const ndjsonFilter = new Transform({
  objectMode: true,
  transform(obj, _enc, cb) {
    if (obj.topic === "Node.js") cb(null, obj);
    else cb();
  },
});

// Readable.from([...]) → ndjsonFilter → fs.createWriteStream
await pipeline(
  fs.createReadStream("export.ndjson", { encoding: "utf8" }),
  lineSplitter(), // hypothetical Transform
  fs.createWriteStream("node-only.ndjson")
);