CodeBlanks Logo Icon CodeBlanks

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.

Practice 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()).

Starter code

index.js
// Write the streamReport function
// Join four info strings with ' | ' and return
// Call and print
// write here

Sample solution

index.js
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());

Expected output

backpressure: true | highWaterMark: 16 | object mode | pipeline: src -> sink

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which one triggers backpressure?

  • queueSize >= highWaterMark Correct answer
  • queueSize < highWaterMark
  • currentSize === 0
  • mode === 'object'

Explanation: queueSize >= highWaterMark triggers backpressure.

2 Match each concept to its role

Term Meaning
backpressure Pauses the producer
highWaterMark Buffer ceiling
object mode Carries objects
pipeline Builds the chain

All lessons in Expert