0

I'm trying to pipe one Stream Axios Response into multiple files. It's not working, and I can reproduce it with the simple code below:

Will work:

const { PassThrough } = require('stream')
const inputStream = new PassThrough()
inputStream.write('foo')
// Now I have a stream with content

inputStream.pipe(process.stdout)
inputStream.pipe(process.stderr)
// will print 'foofoo', for both stdout and stderr

Will not work:

const { PassThrough } = require('stream')
const inputStream = new PassThrough()
inputStream.write('foo')

inputStream.pipe(process.stdout)
setImmediate(() => {
    inputStream.pipe(process.stderr)
})
// Will print only 'foo'

The question is, Can I say that the existed content in the stream will be piped only if the two pipe commands will execute in the same Event-Loop iteration?

Doesn't that make the situation non-deterministic?

baruchiro
  • 5,088
  • 5
  • 44
  • 66

1 Answers1

2

By the time the callback scheduled with setImmediate is executed, the stream data is already flushed. This can checked by .readableLength stream property.

You can use cork and uncork in order to control when the buffered stream data is flushed.

const { PassThrough } = require('stream')
const inputStream = new PassThrough()
inputStream.cork()
inputStream.write('foo')
inputStream.pipe(process.stdout)
setImmediate(() => {
  inputStream.pipe(process.stderr)
  inputStream.uncork()
})
antonku
  • 7,377
  • 2
  • 15
  • 21