juliangruber / multipipe

A better `Stream.pipe` that creates duplex streams and lets you handle errors in one place.
294 stars 23 forks source link

Flow problem with node >= 10.10 #48

Closed arantes555 closed 5 years ago

arantes555 commented 5 years ago

Hi,

I'm running into an issue with all node versions >= 10.10.

The problem is that, sometimes (I cannot pinpoint exactly under which conditions), putting streams into multipipe seems to stop the flow, so streams are piped but no data is actually flowing.

As a test, you can run the following code:

const multipipe = require('multipipe')

const { PassThrough } = require('stream')

const stringToStream = (string) => {
  const myStream = new PassThrough()
  myStream.end(Buffer.from(string, 'binary'))
  return myStream
}

const str = 'a'.repeat(1e6)

const returnStream = new PassThrough()

let tmp = ''
returnStream
  .on('data', (chunk) => {
    tmp += chunk
  })
  .on('end', () => {
    console.log(tmp.length)
  })

const input = stringToStream(str)
const outputStream = multipipe(new PassThrough(), returnStream)
multipipe(input, outputStream)
// outputStream.resume()

On node < 10.10, you get a log of 1000000, as is expected.

On node >= 10.10, you get nothing, except if you uncomment the last line.

This unexpected behaviour is easy to workaround (just add a .resume()), but quite annoying... It seems linked to https://github.com/nodejs/node/pull/22209 and https://github.com/nodejs/node/pull/18994 .

juliangruber commented 5 years ago

This is by design, the multipipe itself needs to be consumed for data to flow internally. You can also fix your example by ending it with:

const m = multipipe(input, outputStream)
m.resume()

If you don't care about the return value of multipipe and just want to use it for internal flow control, Stream#pipeline() from node core is the right method.