Full answer
Two stream families
- Node streams (node:stream): older, event-based, used by fs, net, http, zlib. Backpressure through write() returning false and 'drain', or through pipeline.
- Web Streams (WHATWG): promise-based readers and writers, pipeTo and pipeThrough, used by fetch, Response, Blob.stream(), Deno and browsers. Backpressure through the desiredSize and the pull model.
In Node
- Globals: ReadableStream, WritableStream, TransformStream, TextEncoderStream, TextDecoderStream, CompressionStream and DecompressionStream.
- fetch(url).then(r => r.body) is a ReadableStream. It is async iterable in Node, so for await works.
- Conversions: Readable.fromWeb(webReadable), Readable.toWeb(nodeReadable), Writable.fromWeb, Writable.toWeb, Duplex.fromWeb and Duplex.toWeb. stream.pipeline accepts Web Streams as well.
- new Response(readableStream) can send a stream as a response body in frameworks that use the fetch Response model (and undici).
Which to use
- Shared code between browser and server, or code built around fetch and Response: Web Streams.
- fs.createReadStream, sockets, process.stdin, existing libraries: Node streams. They are mature and often faster in Node.
- Mixed: convert at the boundary, once.
Details
- A TransformStream is simple: new TransformStream({ transform(chunk, controller) { controller.enqueue(...) } }).
- A reader can be locked: a stream can have only one reader at a time. Use tee() to split.
- CompressionStream('gzip') and 'deflate' are available (and 'brotli' in newer versions). They work on Uint8Array chunks.
- Errors: cancel() and abort() propagate, and pipeTo rejects if a stage fails.
javascript
const { Readable, Writable } = require('node:stream');
const { pipeline } = require('node:stream/promises');
const zlib = require('node:zlib');
async function collect(webStream) {
const chunks = [];
for await (const chunk of webStream) chunks.push(chunk);
return Buffer.concat(chunks);
}
async function main() {
// 1. A web ReadableStream built by hand, with a TransformStream in between.
const source = new ReadableStream({
start(controller) { ['node', 'web', 'streams'].forEach((w) => controller.enqueue(w)); controller.close(); },
});
const upper = new TransformStream({ transform(chunk, controller) { controller.enqueue(chunk.toUpperCase()); } });
const words = [];
for await (const word of source.pipeThrough(upper)) words.push(word);
console.log('1. pipeThrough:', words.join(' '));
// 2. Node Readable to Web ReadableStream and back
const nodeReadable = Readable.from(['a', 'b', 'c']);
const web = Readable.toWeb(nodeReadable);
console.log('2. toWeb gives a ReadableStream:', web instanceof ReadableStream);
const back = Readable.fromWeb(new ReadableStream({ start(c) { c.enqueue('x'); c.enqueue('y'); c.close(); } }), { objectMode: true });
const pieces = [];
await pipeline(back, new Writable({ objectMode: true, write(chunk, enc, cb) { pieces.push(chunk); cb(); } }));
console.log(' fromWeb pieces:', pieces.join(','));
// 3. CompressionStream and DecompressionStream, compared with zlib
const text = 'hello hello hello hello';
const gz = await collect(new Blob([text]).stream().pipeThrough(new CompressionStream('gzip')));
console.log('3. zlib can read what CompressionStream wrote:', zlib.gunzipSync(gz).toString() === text);
const unzipped = await collect(new Blob([zlib.gzipSync(text)]).stream().pipeThrough(new DecompressionStream('gzip')));
console.log(' DecompressionStream can read what zlib wrote:', unzipped.toString() === text);
// 4. A Response body is a web stream, and tee() makes two readers
const response = new Response('shared body');
const [one, two] = response.body.tee();
console.log('4. tee gives both readers the same data:', (await collect(one)).toString() === (await collect(two)).toString());
// 5. Only one reader at a time
const locked = new ReadableStream({ start(c) { c.close(); } });
locked.getReader();
console.log('5. second reader ->', (() => { try { locked.getReader(); } catch (err) { return err.name; } })());
}
main();The script builds web streams, converts between Web and Node streams, and checks that CompressionStream and zlib read each other's output. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): 1. pipeThrough: NODE WEB STREAMS 2. toWeb gives a ReadableStream: true fromWeb pieces: x,y 3. zlib can read what CompressionStream wrote: true DecompressionStream can read what zlib wrote: true 4. tee gives both readers the same data: true 5. second reader -> TypeError
Key points
- Web Streams are the standard API behind fetch and Response.
- Node has the globals and async iteration over them.
- Readable.fromWeb and toWeb convert between the two families.
- Use Web Streams for shared browser and server code.
- A web stream has one reader at a time; tee() splits it.
Common mistakes
- Mixing both APIs in one pipeline without converting at the boundary.
- Trying to read a web stream twice without tee().
- Assuming chunks are always Buffers (they can be strings or Uint8Arrays).
- Forgetting that a locked stream cannot get a second reader.
- Using Web Streams for fs-heavy code without measuring.
Follow-ups
- How does backpressure work in a ReadableStream?
- How do you stream a fetch response to a file?
- What is the difference between pipeTo and pipeThrough?
Tips
- Say that fetch uses Web Streams.
- Name fromWeb and toWeb.
- Give a rule for which family to choose.