[
  {
    "path": ".gitignore",
    "content": "# Logs\nlogs\n*.log\n\n# Runtime data\npids\n*.pid\n*.seed\n\n# Directory for instrumented libs generated by jscoverage/JSCover\nlib-cov\n\n# Coverage directory used by tools like istanbul\ncoverage\n\n# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)\n.grunt\n\n# Compiled binary addons (http://nodejs.org/api/addons.html)\nbuild/Release\n\n# Dependency directory\n# Commenting this out is preferred by some people, see\n# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-\nnode_modules\n\n# Users Environment Variables\n.lock-wscript\n.esm-cache"
  },
  {
    "path": ".travis.yml",
    "content": "language: node_js\nnode_js:\n  - \"8\"\n  - \"10\"\n  - \"12\"\n  - \"14\"\nsudo: false\ncache:\n  directories:\n    - node_modules\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2014-2022 Teambition\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# merge2\n\nMerge multiple streams into one stream in sequence or parallel.\n\n[![NPM version][npm-image]][npm-url]\n[![Build Status][travis-image]][travis-url]\n[![Downloads][downloads-image]][downloads-url]\n\n## Install\n\nInstall with [npm](https://npmjs.org/package/merge2)\n\n```sh\nnpm install merge2\n```\n\n## Usage\n\n```js\nconst gulp = require('gulp')\nconst merge2 = require('merge2')\nconst concat = require('gulp-concat')\nconst minifyHtml = require('gulp-minify-html')\nconst ngtemplate = require('gulp-ngtemplate')\n\ngulp.task('app-js', function () {\n  return merge2(\n      gulp.src('static/src/tpl/*.html')\n        .pipe(minifyHtml({empty: true}))\n        .pipe(ngtemplate({\n          module: 'genTemplates',\n          standalone: true\n        })),\n      gulp.src([\n        'static/src/js/app.js',\n        'static/src/js/locale_zh-cn.js',\n        'static/src/js/router.js',\n        'static/src/js/tools.js',\n        'static/src/js/services.js',\n        'static/src/js/filters.js',\n        'static/src/js/directives.js',\n        'static/src/js/controllers.js'\n      ])\n    )\n    .pipe(concat('app.js'))\n    .pipe(gulp.dest('static/dist/js/'))\n})\n```\n\n```js\nconst stream = merge2([stream1, stream2], stream3, {end: false})\n//...\nstream.add(stream4, stream5)\n//..\nstream.end()\n```\n\n```js\n// equal to merge2([stream1, stream2], stream3)\nconst stream = merge2()\nstream.add([stream1, stream2])\nstream.add(stream3)\n```\n\n```js\n// merge order:\n//   1. merge `stream1`;\n//   2. merge `stream2` and `stream3` in parallel after `stream1` merged;\n//   3. merge 'stream4' after `stream2` and `stream3` merged;\nconst stream = merge2(stream1, [stream2, stream3], stream4)\n\n// merge order:\n//   1. merge `stream5` and `stream6` in parallel after `stream4` merged;\n//   2. merge 'stream7' after `stream5` and `stream6` merged;\nstream.add([stream5, stream6], stream7)\n```\n\n```js\n// nest merge\n// equal to merge2(stream1, stream2, stream6, stream3, [stream4, stream5]);\nconst streamA = merge2(stream1, stream2)\nconst streamB = merge2(stream3, [stream4, stream5])\nconst stream = merge2(streamA, streamB)\nstreamA.add(stream6)\n```\n\n## API\n\n```js\nconst merge2 = require('merge2')\n```\n\n### merge2()\n\n### merge2(options)\n\n### merge2(stream1, stream2, ..., streamN)\n\n### merge2(stream1, stream2, ..., streamN, options)\n\n### merge2(stream1, [stream2, stream3, ...], streamN, options)\n\nreturn a duplex stream (mergedStream). streams in array will be merged in parallel.\n\n### mergedStream.add(stream)\n\n### mergedStream.add(stream1, [stream2, stream3, ...], ...)\n\nreturn the mergedStream.\n\n### mergedStream.on('queueDrain', function() {})\n\nIt will emit 'queueDrain' when all streams merged. If you set `end === false` in options, this event give you a notice that should add more streams to merge or end the mergedStream.\n\n#### stream\n\n*option*\nType: `Readable` or `Duplex` or `Transform` stream.\n\n#### options\n\n*option*\nType: `Object`.\n\n* **end** - `Boolean` - if `end === false` then mergedStream will not be auto ended, you should end by yourself. **Default:** `undefined`\n\n* **pipeError** - `Boolean` - if `pipeError === true` then mergedStream will emit `error` event from source streams. **Default:** `undefined`\n\n* **objectMode** - `Boolean` . **Default:** `true`\n\n`objectMode` and other options(`highWaterMark`, `defaultEncoding` ...) is same as Node.js `Stream`.\n\n## License\n\nMIT © [Teambition](https://www.teambition.com)\n\n[npm-url]: https://npmjs.org/package/merge2\n[npm-image]: http://img.shields.io/npm/v/merge2.svg\n\n[travis-url]: https://travis-ci.org/teambition/merge2\n[travis-image]: http://img.shields.io/travis/teambition/merge2.svg\n\n[downloads-url]: https://npmjs.org/package/merge2\n[downloads-image]: http://img.shields.io/npm/dm/merge2.svg?style=flat-square\n"
  },
  {
    "path": "index.js",
    "content": "'use strict'\n/*\n * merge2\n * https://github.com/teambition/merge2\n *\n * Copyright (c) 2014-2022 Teambition\n * Licensed under the MIT license.\n */\nconst Stream = require('stream')\nconst PassThrough = Stream.PassThrough\nconst slice = Array.prototype.slice\n\nmodule.exports = merge2\n\nfunction merge2 () {\n  const streamsQueue = []\n  const args = slice.call(arguments)\n  let merging = false\n  let options = args[args.length - 1]\n\n  if (options && !Array.isArray(options) && options.pipe == null) {\n    args.pop()\n  } else {\n    options = {}\n  }\n\n  const doEnd = options.end !== false\n  const doPipeError = options.pipeError === true\n  if (options.objectMode == null) {\n    options.objectMode = true\n  }\n  if (options.highWaterMark == null) {\n    options.highWaterMark = 64 * 1024\n  }\n  const mergedStream = PassThrough(options)\n\n  function addStream () {\n    for (let i = 0, len = arguments.length; i < len; i++) {\n      streamsQueue.push(pauseStreams(arguments[i], options))\n    }\n    mergeStream()\n    return this\n  }\n\n  function mergeStream () {\n    if (merging) {\n      return\n    }\n    merging = true\n\n    let streams = streamsQueue.shift()\n    if (!streams) {\n      process.nextTick(endStream)\n      return\n    }\n    if (!Array.isArray(streams)) {\n      streams = [streams]\n    }\n\n    let pipesCount = streams.length + 1\n\n    function next () {\n      if (--pipesCount > 0) {\n        return\n      }\n      merging = false\n      mergeStream()\n    }\n\n    function pipe (stream) {\n      function onend () {\n        stream.removeListener('merge2UnpipeEnd', onend)\n        stream.removeListener('end', onend)\n        if (doPipeError) {\n          stream.removeListener('error', onerror)\n        }\n        next()\n      }\n      function onerror (err) {\n        mergedStream.emit('error', err)\n      }\n      // skip ended stream\n      if (stream._readableState.endEmitted) {\n        return next()\n      }\n\n      stream.on('merge2UnpipeEnd', onend)\n      stream.on('end', onend)\n\n      if (doPipeError) {\n        stream.on('error', onerror)\n      }\n\n      stream.pipe(mergedStream, { end: false })\n      // compatible for old stream\n      stream.resume()\n    }\n\n    for (let i = 0; i < streams.length; i++) {\n      pipe(streams[i])\n    }\n\n    next()\n  }\n\n  function endStream () {\n    merging = false\n    // emit 'queueDrain' when all streams merged.\n    mergedStream.emit('queueDrain')\n    if (doEnd) {\n      mergedStream.end()\n    }\n  }\n\n  mergedStream.setMaxListeners(0)\n  mergedStream.add = addStream\n  mergedStream.on('unpipe', function (stream) {\n    stream.emit('merge2UnpipeEnd')\n  })\n\n  if (args.length) {\n    addStream.apply(null, args)\n  }\n  return mergedStream\n}\n\n// check and pause streams for pipe.\nfunction pauseStreams (streams, options) {\n  if (!Array.isArray(streams)) {\n    // Backwards-compat with old-style streams\n    if (!streams._readableState && streams.pipe) {\n      streams = streams.pipe(PassThrough(options))\n    }\n    if (!streams._readableState || !streams.pause || !streams.pipe) {\n      throw new Error('Only readable stream can be merged.')\n    }\n    streams.pause()\n  } else {\n    for (let i = 0, len = streams.length; i < len; i++) {\n      streams[i] = pauseStreams(streams[i], options)\n    }\n  }\n  return streams\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"merge2\",\n  \"description\": \"Merge multiple streams into one stream in sequence or parallel.\",\n  \"authors\": [\n    \"Yan Qing <admin@zensh.com>\"\n  ],\n  \"license\": \"MIT\",\n  \"version\": \"1.4.1\",\n  \"main\": \"./index.js\",\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git@github.com:teambition/merge2.git\"\n  },\n  \"homepage\": \"https://github.com/teambition/merge2\",\n  \"keywords\": [\n    \"merge2\",\n    \"multiple\",\n    \"sequence\",\n    \"parallel\",\n    \"merge\",\n    \"stream\",\n    \"merge stream\",\n    \"sync\"\n  ],\n  \"engines\": {\n    \"node\": \">= 8\"\n  },\n  \"dependencies\": {},\n  \"devDependencies\": {\n    \"standard\": \"^14.3.4\",\n    \"through2\": \"^3.0.1\",\n    \"thunks\": \"^4.9.6\",\n    \"tman\": \"^1.10.0\",\n    \"to-through\": \"^2.0.0\"\n  },\n  \"scripts\": {\n    \"test\": \"standard && tman\"\n  },\n  \"files\": [\n    \"README.md\",\n    \"index.js\"\n  ]\n}\n"
  },
  {
    "path": "test/index.js",
    "content": "'use strict'\n\nconst tman = require('tman')\nconst assert = require('assert')\nconst Stream = require('stream')\nconst thunk = require('thunks').thunk\nconst through = require('through2')\nconst toThrough = require('to-through')\n\ntest(require('..'))\n\nfunction test (merge2) {\n  tman.suite('merge2', function () {\n    tman.it('merge2(read1, read2, through3)', function (done) {\n      const options = { objectMode: true }\n      const result = []\n      const read1 = fakeReadStream(options)\n      const read2 = fakeReadStream(options)\n      const through3 = through.obj()\n\n      const mergeStream = merge2(read1, read2, through3)\n\n      read1.push(1)\n      thunk.delay(100)(function () {\n        read1.push(2)\n        read1.push(null)\n      })\n      read2.push(3)\n      thunk.delay(10)(function () {\n        read2.push(4)\n        read2.push(null)\n      })\n      through3.push(5)\n      thunk.delay(200)(function () {\n        through3.push(6)\n        through3.end()\n      })\n\n      mergeStream\n        .on('data', function (chunk) {\n          result.push(chunk)\n        })\n        .on('error', done)\n        .on('end', function () {\n          assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6])\n          done()\n        })\n    })\n\n    tman.it('merge2 - error handling', function (done) {\n      const ts = through.obj()\n\n      const mergeStream = merge2(toThrough(ts), { pipeError: true })\n\n      const expectedError = new Error('error')\n      thunk.delay(100)(function () {\n        ts.destroy(expectedError)\n      })\n\n      mergeStream\n        .on('error', function (error) {\n          assert.strictEqual(error, expectedError)\n          done()\n        })\n        .on('end', function () {\n          throw Error('error expected')\n        })\n    })\n\n    tman.it('merge2(TransformStream)', function (done) {\n      const result = []\n      const ts = through.obj()\n\n      const mergeStream = merge2(toThrough(ts))\n\n      ts.push(1)\n      thunk.delay(100)(function () {\n        ts.push(2)\n        ts.push(null)\n      })\n\n      mergeStream\n        .on('data', function (chunk) {\n          result.push(chunk)\n        })\n        .on('error', done)\n        .on('end', function () {\n          assert.deepStrictEqual(result, [1, 2])\n          done()\n        })\n    })\n\n    tman.it('merge2(read1, [read2, through3], through4, [through5, read6])', function (done) {\n      const options = { objectMode: true }\n      const result = []\n      const read1 = fakeReadStream(options)\n      const read2 = fakeReadStream(options)\n      const through3 = through.obj()\n      const through4 = through.obj()\n      const through5 = through.obj()\n      const read6 = fakeReadStream(options)\n\n      read1.push(1)\n      read1.push(null)\n      thunk.delay(100)(function () {\n        read2.push(2)\n        read2.push(null)\n      })\n      through3.push(3)\n      through3.end()\n      through4.push(4)\n      through4.push(null)\n      through5.push(5)\n      through5.push(null)\n      thunk.delay(200)(function () {\n        read6.push(6)\n        read6.push(null)\n      })\n\n      const mergeStream = merge2(read1, [read2, through3], through4, [through5, read6])\n\n      mergeStream\n        .on('data', function (chunk) {\n          result.push(chunk)\n        })\n        .on('error', done)\n        .on('end', function () {\n          assert.deepStrictEqual(result, [1, 3, 2, 4, 5, 6])\n          done()\n        })\n    })\n\n    tman.it('merge2().add(read1, [read2, through3], through4, [through5, read6])', function (done) {\n      const options = { objectMode: true }\n      const result = []\n      const read1 = fakeReadStream(options)\n      const read2 = fakeReadStream(options)\n      const through3 = through.obj()\n      const through4 = through.obj()\n      const through5 = through.obj()\n      const read6 = fakeReadStream(options)\n      const mergeStream = merge2()\n\n      read1.push(1)\n      read1.push(null)\n      thunk.delay(100)(function () {\n        read2.push(2)\n        read2.push(null)\n      })\n      through3.push(3)\n      through3.end()\n      through4.push(4)\n      through4.push(null)\n      through5.push(5)\n      through5.push(null)\n      thunk.delay(200)(function () {\n        read6.push(6)\n        read6.push(null)\n      })\n\n      mergeStream\n        .add(read1, [read2, through3], through4)\n        .on('data', function (chunk) {\n          result.push(chunk)\n        })\n        .add([through5, read6])\n        .on('error', done)\n        .on('end', function () {\n          assert.deepStrictEqual(result, [1, 3, 2, 4, 5, 6])\n          done()\n        })\n    })\n\n    tman.it('merge2(read1, read2, through3, {objectMode: false})', function (done) {\n      const options = { objectMode: false }\n      let result = ''\n      const read1 = fakeReadStream(options)\n      const read2 = fakeReadStream(options)\n      const through3 = through(options)\n\n      const mergeStream = merge2(read1, read2, through3, options)\n\n      read1.push('1')\n      thunk.delay(100)(function () {\n        read1.push('2')\n        read1.push(null)\n      })\n      read2.push('3')\n      thunk.delay(10)(function () {\n        read2.push('4')\n        read2.push(null)\n      })\n      through3.push('5')\n      thunk.delay(200)(function () {\n        through3.push('6')\n        through3.end()\n      })\n\n      mergeStream\n        .on('data', function (chunk) {\n          result += chunk.toString()\n        })\n        .on('error', done)\n        .on('end', function () {\n          assert.strictEqual(result, '123456')\n          done()\n        })\n    })\n\n    tman.it('merge2([read1, read2]) with classic style streams', function (done) {\n      const result = []\n      const read1 = fakeReadClassicStream()\n      const read2 = fakeReadClassicStream()\n\n      const mergeStream = merge2([read1, read2])\n\n      read1.push(1)\n      read1.push(null)\n      thunk.delay(100)(function () {\n        read2.push(2)\n        read2.push(null)\n      })\n\n      mergeStream\n        .on('data', function (chunk) {\n          result.push(chunk)\n        })\n        .on('error', done)\n        .on('end', function () {\n          assert.deepStrictEqual(result, [1, 2])\n          done()\n        })\n    })\n\n    tman.it('merge2(read1, read2, {end: false})', function (done) {\n      const options = { objectMode: true }\n      const result = []\n      const read1 = fakeReadStream(options)\n      const read2 = fakeReadStream(options)\n      const through3 = through.obj()\n\n      const mergeStream = merge2(read1, read2, { end: false })\n\n      read1.push(1)\n      read1.push(2)\n      read1.push(null)\n      read2.push(3)\n      read2.push(4)\n      read2.push(null)\n      through3.push(5)\n      through3.push(6)\n      through3.end()\n\n      thunk.delay(500)(function () {\n        assert.deepStrictEqual(result, [1, 2, 3, 4])\n        mergeStream.add(through3)\n        return thunk.delay(100)\n      })(function () {\n        mergeStream.end()\n      })\n\n      mergeStream\n        .on('data', function (chunk) {\n          result.push(chunk)\n        })\n        .on('error', done)\n        .on('end', function () {\n          assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6])\n          done()\n        })\n    })\n\n    tman.it('merge2(merge2(through4, [through5, read6]), read1, [read2, through3])', function (done) {\n      const options = { objectMode: true }\n      const result1 = []\n      const result2 = []\n      const read1 = fakeReadStream(options)\n      const read2 = fakeReadStream(options)\n      const through3 = through.obj()\n      const through4 = through.obj()\n      const through5 = through.obj()\n      const read6 = fakeReadStream(options)\n\n      read1.push(1)\n      read1.push(null)\n      thunk.delay(100)(function () {\n        read2.push(2)\n        read2.push(null)\n      })\n      through3.push(3)\n      through3.end()\n      through4.push(4)\n      through4.push(null)\n      through5.push(5)\n      through5.push(null)\n      thunk.delay(10)(function () {\n        read6.push(6)\n        read6.push(null)\n      })\n\n      const mergeStream1 = merge2(through4, [through5, read6])\n\n      mergeStream1.on('data', function (chunk) {\n        result1.push(chunk)\n      })\n\n      const mergeStream = merge2(mergeStream1, read1, [read2, through3])\n\n      mergeStream\n        .on('data', function (chunk) {\n          result2.push(chunk)\n          if (result2.length <= 3) assert.deepStrictEqual(result1, result2)\n          else assert.deepStrictEqual(result1, [4, 5, 6])\n        })\n        .on('error', done)\n        .on('end', function () {\n          assert.deepStrictEqual(result2, [4, 5, 6, 1, 3, 2])\n          done()\n        })\n    })\n  })\n}\n\nfunction fakeReadStream (options) {\n  const readStream = new Stream.Readable(options)\n  readStream._read = function () {}\n  return readStream\n}\n\nfunction fakeReadClassicStream () {\n  const readStream = new Stream()\n  readStream.readable = true\n  readStream.push = function (data) {\n    if (data === null) {\n      this.emit('end')\n      readStream.readable = false\n    }\n    this.emit('data', data)\n  }\n  return readStream\n}\n"
  }
]