Node.js
Summary - Stream internals
Combine backpressure, highWaterMark and pipeline to write a custom stream.
Summary - Stream internals
In this group we saw the internals of streams: backpressure, highWaterMark, object mode, pipeline and custom stream. Each shapes Node.js's data flow logic.
Backpressure pauses the producer when the queue crosses the threshold. highWaterMark is the buffer ceiling that drives 'flowing' and 'paused' states. Object mode carries each chunk as an object; binary carries Buffers. stream.pipeline builds a src, transform and sink chain with error safety. Custom stream is a flow customized through _read and _write methods. In the final lesson we combine them in a report function.
The report function
The final task combines all stream concepts into one report. The streamReport() function joins four info strings with ' | '. Output: backpressure: true | highWaterMark: 16 | object mode | pipeline: src -> sink.
This report summarizes the four core stream concepts in one string: the backpressure decision (true), the threshold (16), the mode (object mode) and the chain (src -> sink). Inside the function you place each piece in a variable, then join them with +. This lets you see what you learned on the console; you model the stream internals without running real streams.
Exercise Write the stream report
Write function streamReport(). Define const bp = 'backpressure: true', const hwm = 'highWaterMark: 16', const mode = 'object mode' and const pipe = 'pipeline: src -> sink'. Return bp + ' | ' + hwm + ' | ' + mode + ' | ' + pipe. Print console.log(streamReport()).
Show example solution
function streamReport() {
const bp = 'backpressure: true';
const hwm = 'highWaterMark: 16';
const mode = 'object mode';
const pipe = 'pipeline: src -> sink';
return bp + ' | ' + hwm + ' | ' + mode + ' | ' + pipe;
}
console.log(streamReport());
Test yourself
-
Which one triggers backpressure?
- queueSize >= highWaterMark
- queueSize < highWaterMark
- currentSize === 0
- mode === 'object'
Show answer
queueSize >= highWaterMark — queueSize >= highWaterMark triggers backpressure.
-
Match each concept to its role
- backpressure
- Pauses the producer
- highWaterMark
- Buffer ceiling
- object mode
- Carries objects
- pipeline
- Builds the chain
Next lesson: CAP theorem