Courses / Node.js: From First Script to Production / Chapter 14
Module 6 · Inside the runtime · Chapter 14 of 27
Streams in Depth: Custom Streams, Backpressure and Pipelines
Builds on Asynchronous Programming: Callbacks, Promises and async/await, Event-Driven Programming with EventEmitter, Core Modules: Files, Paths, Buffers, Crypto and util and Building a Basic Web Server with node:http. Next up: V8 Memory, Garbage Collection and Leaks.
Part 1 · Four stream types
Data in pieces
A bank export of 10 lakh rows, a video upload and a log feed have one thing in common: they are too big, or too slow, to hold in memory as a single value. A stream processes such data in pieces, so the first piece can be used while the rest is still on its way.
A stream is an object that moves a sequence of chunks from one place to another. In the usual byte streams a chunk is a Buffer or a string. In object mode a chunk can be any JavaScript value except null. Every stream is also an EventEmitter, which is how it announces data, end, error and the other events you meet in this chapter.
The core stream module defines four kinds. You rarely create the base classes directly, but you will use objects built on them every day.
| Type | Direction | Typical examples |
|---|