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

前端 未结 6 664
天命终不由人
天命终不由人 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:35

    Ran into a similar problem lately, needing to handle backpressure in an inflating transform stream - the secret to handling push() returning false is to register and handle the 'drain' event on the stream

    _transform(data, enc, callback) {
      const continueTransforming = () => {
        ... do some work / parse the data, keep state of where we're at etc
        if(!this.push(event)) 
             this._readableState.pipes.once('drain', continueTransforming); // will get called again when the reader can consume more data
        if(allDone)
           callback();
      }
      continueTransforming()
    }
    

    NOTE this is a bit hacky as we're reaching into the internals and pipes can even be an array of Readables but it does work in the common case of ....pipe(transform).pipe(...

    Would be great if someone from the Node community can suggest a "correct" method for handling .push() returning false

提交回复
热议问题