A stream that emits multiple other streams one after another (streams2)
JavaScript
Latest commit f8695f1 Mar 22, 2017 @feross feross standard
Permalink
Failed to load latest commit information.
test Handle stream errors on all streams Jul 18, 2016
.npmignore npmignore img.jpg Mar 11, 2016
.travis.yml travis: test latest node Feb 11, 2016
.zuul.yml zuul: remove flaky ios tests Feb 28, 2016
LICENSE license Aug 10, 2014
README.md standard Mar 22, 2017
img.jpg initial commit Jul 6, 2014
index.js Handle stream errors on all streams Jul 18, 2016
package.json build Aug 26, 2016

README.md

multistream travis npm downloads javascript style guide

A stream that emits multiple other streams one after another (streams2)

Sauce Test Status

cat

Simple, robust streams2 version of combined-stream. Allows you to combine multiple streams into a single stream. When the first stream ends, the next one starts, and so on, until all streams are consumed.

This module is used by WebTorrent, specifically create-torrent.

install

npm install multistream

usage

Use multistream like this:

var MultiStream = require('multistream')
var fs = require('fs')

var streams = [
  fs.createReadStream(__dirname + '/numbers/1.txt'),
  fs.createReadStream(__dirname + '/numbers/2.txt'),
  fs.createReadStream(__dirname + '/numbers/3.txt')
]

MultiStream(streams).pipe(process.stdout) // => 123

You can also create an object-mode stream with MultiStream.obj(streams).

To lazily create the streams, wrap them in a function:

var streams = [
  fs.createReadStream(__dirname + '/numbers/1.txt'),
  function () { // will be executed when the stream is active
    return fs.createReadStream(__dirname + '/numbers/2.txt')
  },
  function () { // same
    return fs.createReadStream(__dirname + '/numbers/3.txt')
  }
]

MultiStream(streams).pipe(process.stdout) // => 123

Alternatively, streams may be created by an asynchronous "factory" function:

var count = 0;
function factory (cb) {
  if (count > 3) return cb(null, null)
  count++
  setTimeout(function () {
    cb(null, fs.createReadStream(__dirname + '/numbers/' + count + '.txt'))
  }, 100)
}

MultiStream(factory).pipe(process.stdout) // => 123

contributors

license

MIT. Copyright (c) Feross Aboukhadijeh.