Skip to content

Instantly share code, notes, and snippets.

@grimen
Last active April 9, 2019 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grimen/8ac7259d0f2ed4609f9cf3a09a59d4ab to your computer and use it in GitHub Desktop.
Save grimen/8ac7259d0f2ed4609f9cf3a09a59d4ab to your computer and use it in GitHub Desktop.
class CombinedStream extends stream.PassThrough {
constructor (...streams) {
super()
this._streams = streams
this._transformStream = undefined
this.on('pipe', this.onPipe)
}
onPipe (sourceStream) {
sourceStream.unpipe(this)
for (const stream of this._streams) {
sourceStream = sourceStream.pipe(stream)
}
const eventTypes = [
'data',
'end',
'close',
'drain',
'error',
'finish',
'pipe',
'unpipe',
]
for (const eventType of eventTypes) {
sourceStream.on(eventType, (...args) => {
this.emit(eventType, ...args)
})
}
this._transformStream = sourceStream
}
pipe (destinationStream, options) {
return this._transformStream.pipe(destinationStream, options)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment