A
A
alex4answ2020-08-28 12:52:22
Node.js
alex4answ, 2020-08-28 12:52:22

Why does pipe keep reading from a Readable Stream if the buffer is full?

Good afternoon,
there

Readable Stream

class MyReader extends Readable {
  current = 0;
  max = 100;
  _read(size) {
    this.current++;
    if (this.current >= this.max) {
      this.push(null);
    } else {
        const buffer = Buffer.from(`${this.current}`, 'utf8');
        const couldBeAdded= this.push(buffer);
        console.log(`push: ${this.current}, done: ${couldBeAdded}`);
      }
    }
}

There is also
Writable Stream
class MyWriter extends Writable {
  _write(chunk, encoding, done) {
    setTimeout(() => {
      console.log(`chunk: ${chunk.toString()}`);
      done(); // торможу запись
    }, 100);
  }
}


I pass it a Writable Stream:
const read = new MyReader({ highWaterMark: 4 });
const write = new MyWriter({ highWaterMark: 4 });
read.pipe(write);


As a result, in the console at iteration 10 (when I try to push stream 10) read.push() returns false, which means that the buffer is full, and further pieces will fill the memory.

When read.push() is false, the stream's Readable buffer is full , but pipe doesn't pause reading and wait for the buffer to be freed, why?

In the opposite situation, when the Writable Buffer is full, pipe pauses reading, everything is clear here

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question