index.js 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var duplexer = require('duplexer')
  2. var through = require('through')
  3. module.exports = function () {
  4. var streams
  5. if(arguments.length == 1 && Array.isArray(arguments[0])) {
  6. streams = arguments[0]
  7. } else {
  8. streams = [].slice.call(arguments)
  9. }
  10. if(streams.length == 0)
  11. return through()
  12. else if(streams.length == 1)
  13. return streams[0]
  14. var first = streams[0]
  15. , last = streams[streams.length - 1]
  16. , thepipe = duplexer(first, last)
  17. //pipe all the streams together
  18. function recurse (streams) {
  19. if(streams.length < 2)
  20. return
  21. streams[0].pipe(streams[1])
  22. recurse(streams.slice(1))
  23. }
  24. recurse(streams)
  25. function onerror () {
  26. var args = [].slice.call(arguments)
  27. args.unshift('error')
  28. thepipe.emit.apply(thepipe, args)
  29. }
  30. //es.duplex already reemits the error from the first and last stream.
  31. //add a listener for the inner streams in the pipeline.
  32. for(var i = 1; i < streams.length - 1; i ++)
  33. streams[i].on('error', onerror)
  34. return thepipe
  35. }