Skip to content

Instantly share code, notes, and snippets.

@grimen
Last active April 9, 2019 03:29
Show Gist options
  • Save grimen/8ac7259d0f2ed4609f9cf3a09a59d4ab to your computer and use it in GitHub Desktop.
Save grimen/8ac7259d0f2ed4609f9cf3a09a59d4ab to your computer and use it in GitHub Desktop.

Revisions

  1. grimen revised this gist Apr 9, 2019. 1 changed file with 28 additions and 7 deletions.
    35 changes: 28 additions & 7 deletions combineStream.js
    Original file line number Diff line number Diff line change
    @@ -4,16 +4,37 @@ class CombinedStream extends stream.PassThrough {
    super()

    this._streams = streams
    this._transformStream = undefined

    this.on('pipe', function (sourceStream) {
    sourceStream.unpipe(this)
    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 stream of this._streams) {
    sourceStream = sourceStream.pipe(stream)
    }
    for (const eventType of eventTypes) {
    sourceStream.on(eventType, (...args) => {
    this.emit(eventType, ...args)
    })
    }

    this._transformStream = sourceStream
    })
    this._transformStream = sourceStream
    }

    pipe (destinationStream, options) {
  2. grimen created this gist Apr 6, 2019.
    23 changes: 23 additions & 0 deletions combineStream.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    class CombinedStream extends stream.PassThrough {

    constructor (...streams) {
    super()

    this._streams = streams

    this.on('pipe', function (sourceStream) {
    sourceStream.unpipe(this)

    for (const stream of this._streams) {
    sourceStream = sourceStream.pipe(stream)
    }

    this._transformStream = sourceStream
    })
    }

    pipe (destinationStream, options) {
    return this._transformStream.pipe(destinationStream, options)
    }

    }