What's the proper way to handle back-pressure in a node.js Transform stream?

前端 未结 6 652
天命终不由人
天命终不由人 2020-12-25 13:55

Intro

These are my first adventures in writing node.js server side. It\'s been fun so far but I\'m having some difficulty understanding the proper way to implement

6条回答
  •  暖寄归人
    2020-12-25 14:48

    Mike Lippert's answer is the closest to the truth, I think. It appears that waiting for a new _read() call to begin again from the reading stream is the only way that the Transform is actively notified that the reader is ready. I wanted to share a simple example of how I override _read() temporarily.

    _transform(buf, enc, callback) {
    
      // prepend any unused data from the prior chunk.
      if (this.prev) {
        buf = Buffer.concat([ this.prev, buf ]);
        this.prev = null;
      }
    
      // will keep transforming until buf runs low on data.
      if (buf.length < this.requiredData) {
        this.prev = buf;
        return callback();
      }
    
      var result = // do something with data...
      var nextbuf = buf.slice(this.requiredData);
    
      if (this.push(result)) {
        // Continue transforming this chunk
        this._transform(nextbuf, enc, callback);
      }
      else {
        // Node is warning us to slow down (applying "backpressure")
        // Temporarily override _read request to continue the transform
        this._read = function() {
            delete this._read;
            this._transform(nextbuf, enc, callback);
        };
      }
    }
    

提交回复
热议问题