Last active
April 9, 2019 03:29
-
-
Save grimen/8ac7259d0f2ed4609f9cf3a09a59d4ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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