Repository: teambition/merge2
Branch: master
Commit: bc86a3dbacc0
Files: 7
Total size: 18.1 KB
Directory structure:
gitextract_s115vdeb/
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── index.js
├── package.json
└── test/
└── index.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
# Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules
# Users Environment Variables
.lock-wscript
.esm-cache
================================================
FILE: .travis.yml
================================================
language: node_js
node_js:
- "8"
- "10"
- "12"
- "14"
sudo: false
cache:
directories:
- node_modules
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2014-2022 Teambition
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# merge2
Merge multiple streams into one stream in sequence or parallel.
[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Downloads][downloads-image]][downloads-url]
## Install
Install with [npm](https://npmjs.org/package/merge2)
```sh
npm install merge2
```
## Usage
```js
const gulp = require('gulp')
const merge2 = require('merge2')
const concat = require('gulp-concat')
const minifyHtml = require('gulp-minify-html')
const ngtemplate = require('gulp-ngtemplate')
gulp.task('app-js', function () {
return merge2(
gulp.src('static/src/tpl/*.html')
.pipe(minifyHtml({empty: true}))
.pipe(ngtemplate({
module: 'genTemplates',
standalone: true
})),
gulp.src([
'static/src/js/app.js',
'static/src/js/locale_zh-cn.js',
'static/src/js/router.js',
'static/src/js/tools.js',
'static/src/js/services.js',
'static/src/js/filters.js',
'static/src/js/directives.js',
'static/src/js/controllers.js'
])
)
.pipe(concat('app.js'))
.pipe(gulp.dest('static/dist/js/'))
})
```
```js
const stream = merge2([stream1, stream2], stream3, {end: false})
//...
stream.add(stream4, stream5)
//..
stream.end()
```
```js
// equal to merge2([stream1, stream2], stream3)
const stream = merge2()
stream.add([stream1, stream2])
stream.add(stream3)
```
```js
// merge order:
// 1. merge `stream1`;
// 2. merge `stream2` and `stream3` in parallel after `stream1` merged;
// 3. merge 'stream4' after `stream2` and `stream3` merged;
const stream = merge2(stream1, [stream2, stream3], stream4)
// merge order:
// 1. merge `stream5` and `stream6` in parallel after `stream4` merged;
// 2. merge 'stream7' after `stream5` and `stream6` merged;
stream.add([stream5, stream6], stream7)
```
```js
// nest merge
// equal to merge2(stream1, stream2, stream6, stream3, [stream4, stream5]);
const streamA = merge2(stream1, stream2)
const streamB = merge2(stream3, [stream4, stream5])
const stream = merge2(streamA, streamB)
streamA.add(stream6)
```
## API
```js
const merge2 = require('merge2')
```
### merge2()
### merge2(options)
### merge2(stream1, stream2, ..., streamN)
### merge2(stream1, stream2, ..., streamN, options)
### merge2(stream1, [stream2, stream3, ...], streamN, options)
return a duplex stream (mergedStream). streams in array will be merged in parallel.
### mergedStream.add(stream)
### mergedStream.add(stream1, [stream2, stream3, ...], ...)
return the mergedStream.
### mergedStream.on('queueDrain', function() {})
It 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.
#### stream
*option*
Type: `Readable` or `Duplex` or `Transform` stream.
#### options
*option*
Type: `Object`.
* **end** - `Boolean` - if `end === false` then mergedStream will not be auto ended, you should end by yourself. **Default:** `undefined`
* **pipeError** - `Boolean` - if `pipeError === true` then mergedStream will emit `error` event from source streams. **Default:** `undefined`
* **objectMode** - `Boolean` . **Default:** `true`
`objectMode` and other options(`highWaterMark`, `defaultEncoding` ...) is same as Node.js `Stream`.
## License
MIT © [Teambition](https://www.teambition.com)
[npm-url]: https://npmjs.org/package/merge2
[npm-image]: http://img.shields.io/npm/v/merge2.svg
[travis-url]: https://travis-ci.org/teambition/merge2
[travis-image]: http://img.shields.io/travis/teambition/merge2.svg
[downloads-url]: https://npmjs.org/package/merge2
[downloads-image]: http://img.shields.io/npm/dm/merge2.svg?style=flat-square
================================================
FILE: index.js
================================================
'use strict'
/*
* merge2
* https://github.com/teambition/merge2
*
* Copyright (c) 2014-2022 Teambition
* Licensed under the MIT license.
*/
const Stream = require('stream')
const PassThrough = Stream.PassThrough
const slice = Array.prototype.slice
module.exports = merge2
function merge2 () {
const streamsQueue = []
const args = slice.call(arguments)
let merging = false
let options = args[args.length - 1]
if (options && !Array.isArray(options) && options.pipe == null) {
args.pop()
} else {
options = {}
}
const doEnd = options.end !== false
const doPipeError = options.pipeError === true
if (options.objectMode == null) {
options.objectMode = true
}
if (options.highWaterMark == null) {
options.highWaterMark = 64 * 1024
}
const mergedStream = PassThrough(options)
function addStream () {
for (let i = 0, len = arguments.length; i < len; i++) {
streamsQueue.push(pauseStreams(arguments[i], options))
}
mergeStream()
return this
}
function mergeStream () {
if (merging) {
return
}
merging = true
let streams = streamsQueue.shift()
if (!streams) {
process.nextTick(endStream)
return
}
if (!Array.isArray(streams)) {
streams = [streams]
}
let pipesCount = streams.length + 1
function next () {
if (--pipesCount > 0) {
return
}
merging = false
mergeStream()
}
function pipe (stream) {
function onend () {
stream.removeListener('merge2UnpipeEnd', onend)
stream.removeListener('end', onend)
if (doPipeError) {
stream.removeListener('error', onerror)
}
next()
}
function onerror (err) {
mergedStream.emit('error', err)
}
// skip ended stream
if (stream._readableState.endEmitted) {
return next()
}
stream.on('merge2UnpipeEnd', onend)
stream.on('end', onend)
if (doPipeError) {
stream.on('error', onerror)
}
stream.pipe(mergedStream, { end: false })
// compatible for old stream
stream.resume()
}
for (let i = 0; i < streams.length; i++) {
pipe(streams[i])
}
next()
}
function endStream () {
merging = false
// emit 'queueDrain' when all streams merged.
mergedStream.emit('queueDrain')
if (doEnd) {
mergedStream.end()
}
}
mergedStream.setMaxListeners(0)
mergedStream.add = addStream
mergedStream.on('unpipe', function (stream) {
stream.emit('merge2UnpipeEnd')
})
if (args.length) {
addStream.apply(null, args)
}
return mergedStream
}
// check and pause streams for pipe.
function pauseStreams (streams, options) {
if (!Array.isArray(streams)) {
// Backwards-compat with old-style streams
if (!streams._readableState && streams.pipe) {
streams = streams.pipe(PassThrough(options))
}
if (!streams._readableState || !streams.pause || !streams.pipe) {
throw new Error('Only readable stream can be merged.')
}
streams.pause()
} else {
for (let i = 0, len = streams.length; i < len; i++) {
streams[i] = pauseStreams(streams[i], options)
}
}
return streams
}
================================================
FILE: package.json
================================================
{
"name": "merge2",
"description": "Merge multiple streams into one stream in sequence or parallel.",
"authors": [
"Yan Qing <admin@zensh.com>"
],
"license": "MIT",
"version": "1.4.1",
"main": "./index.js",
"repository": {
"type": "git",
"url": "git@github.com:teambition/merge2.git"
},
"homepage": "https://github.com/teambition/merge2",
"keywords": [
"merge2",
"multiple",
"sequence",
"parallel",
"merge",
"stream",
"merge stream",
"sync"
],
"engines": {
"node": ">= 8"
},
"dependencies": {},
"devDependencies": {
"standard": "^14.3.4",
"through2": "^3.0.1",
"thunks": "^4.9.6",
"tman": "^1.10.0",
"to-through": "^2.0.0"
},
"scripts": {
"test": "standard && tman"
},
"files": [
"README.md",
"index.js"
]
}
================================================
FILE: test/index.js
================================================
'use strict'
const tman = require('tman')
const assert = require('assert')
const Stream = require('stream')
const thunk = require('thunks').thunk
const through = require('through2')
const toThrough = require('to-through')
test(require('..'))
function test (merge2) {
tman.suite('merge2', function () {
tman.it('merge2(read1, read2, through3)', function (done) {
const options = { objectMode: true }
const result = []
const read1 = fakeReadStream(options)
const read2 = fakeReadStream(options)
const through3 = through.obj()
const mergeStream = merge2(read1, read2, through3)
read1.push(1)
thunk.delay(100)(function () {
read1.push(2)
read1.push(null)
})
read2.push(3)
thunk.delay(10)(function () {
read2.push(4)
read2.push(null)
})
through3.push(5)
thunk.delay(200)(function () {
through3.push(6)
through3.end()
})
mergeStream
.on('data', function (chunk) {
result.push(chunk)
})
.on('error', done)
.on('end', function () {
assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6])
done()
})
})
tman.it('merge2 - error handling', function (done) {
const ts = through.obj()
const mergeStream = merge2(toThrough(ts), { pipeError: true })
const expectedError = new Error('error')
thunk.delay(100)(function () {
ts.destroy(expectedError)
})
mergeStream
.on('error', function (error) {
assert.strictEqual(error, expectedError)
done()
})
.on('end', function () {
throw Error('error expected')
})
})
tman.it('merge2(TransformStream)', function (done) {
const result = []
const ts = through.obj()
const mergeStream = merge2(toThrough(ts))
ts.push(1)
thunk.delay(100)(function () {
ts.push(2)
ts.push(null)
})
mergeStream
.on('data', function (chunk) {
result.push(chunk)
})
.on('error', done)
.on('end', function () {
assert.deepStrictEqual(result, [1, 2])
done()
})
})
tman.it('merge2(read1, [read2, through3], through4, [through5, read6])', function (done) {
const options = { objectMode: true }
const result = []
const read1 = fakeReadStream(options)
const read2 = fakeReadStream(options)
const through3 = through.obj()
const through4 = through.obj()
const through5 = through.obj()
const read6 = fakeReadStream(options)
read1.push(1)
read1.push(null)
thunk.delay(100)(function () {
read2.push(2)
read2.push(null)
})
through3.push(3)
through3.end()
through4.push(4)
through4.push(null)
through5.push(5)
through5.push(null)
thunk.delay(200)(function () {
read6.push(6)
read6.push(null)
})
const mergeStream = merge2(read1, [read2, through3], through4, [through5, read6])
mergeStream
.on('data', function (chunk) {
result.push(chunk)
})
.on('error', done)
.on('end', function () {
assert.deepStrictEqual(result, [1, 3, 2, 4, 5, 6])
done()
})
})
tman.it('merge2().add(read1, [read2, through3], through4, [through5, read6])', function (done) {
const options = { objectMode: true }
const result = []
const read1 = fakeReadStream(options)
const read2 = fakeReadStream(options)
const through3 = through.obj()
const through4 = through.obj()
const through5 = through.obj()
const read6 = fakeReadStream(options)
const mergeStream = merge2()
read1.push(1)
read1.push(null)
thunk.delay(100)(function () {
read2.push(2)
read2.push(null)
})
through3.push(3)
through3.end()
through4.push(4)
through4.push(null)
through5.push(5)
through5.push(null)
thunk.delay(200)(function () {
read6.push(6)
read6.push(null)
})
mergeStream
.add(read1, [read2, through3], through4)
.on('data', function (chunk) {
result.push(chunk)
})
.add([through5, read6])
.on('error', done)
.on('end', function () {
assert.deepStrictEqual(result, [1, 3, 2, 4, 5, 6])
done()
})
})
tman.it('merge2(read1, read2, through3, {objectMode: false})', function (done) {
const options = { objectMode: false }
let result = ''
const read1 = fakeReadStream(options)
const read2 = fakeReadStream(options)
const through3 = through(options)
const mergeStream = merge2(read1, read2, through3, options)
read1.push('1')
thunk.delay(100)(function () {
read1.push('2')
read1.push(null)
})
read2.push('3')
thunk.delay(10)(function () {
read2.push('4')
read2.push(null)
})
through3.push('5')
thunk.delay(200)(function () {
through3.push('6')
through3.end()
})
mergeStream
.on('data', function (chunk) {
result += chunk.toString()
})
.on('error', done)
.on('end', function () {
assert.strictEqual(result, '123456')
done()
})
})
tman.it('merge2([read1, read2]) with classic style streams', function (done) {
const result = []
const read1 = fakeReadClassicStream()
const read2 = fakeReadClassicStream()
const mergeStream = merge2([read1, read2])
read1.push(1)
read1.push(null)
thunk.delay(100)(function () {
read2.push(2)
read2.push(null)
})
mergeStream
.on('data', function (chunk) {
result.push(chunk)
})
.on('error', done)
.on('end', function () {
assert.deepStrictEqual(result, [1, 2])
done()
})
})
tman.it('merge2(read1, read2, {end: false})', function (done) {
const options = { objectMode: true }
const result = []
const read1 = fakeReadStream(options)
const read2 = fakeReadStream(options)
const through3 = through.obj()
const mergeStream = merge2(read1, read2, { end: false })
read1.push(1)
read1.push(2)
read1.push(null)
read2.push(3)
read2.push(4)
read2.push(null)
through3.push(5)
through3.push(6)
through3.end()
thunk.delay(500)(function () {
assert.deepStrictEqual(result, [1, 2, 3, 4])
mergeStream.add(through3)
return thunk.delay(100)
})(function () {
mergeStream.end()
})
mergeStream
.on('data', function (chunk) {
result.push(chunk)
})
.on('error', done)
.on('end', function () {
assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6])
done()
})
})
tman.it('merge2(merge2(through4, [through5, read6]), read1, [read2, through3])', function (done) {
const options = { objectMode: true }
const result1 = []
const result2 = []
const read1 = fakeReadStream(options)
const read2 = fakeReadStream(options)
const through3 = through.obj()
const through4 = through.obj()
const through5 = through.obj()
const read6 = fakeReadStream(options)
read1.push(1)
read1.push(null)
thunk.delay(100)(function () {
read2.push(2)
read2.push(null)
})
through3.push(3)
through3.end()
through4.push(4)
through4.push(null)
through5.push(5)
through5.push(null)
thunk.delay(10)(function () {
read6.push(6)
read6.push(null)
})
const mergeStream1 = merge2(through4, [through5, read6])
mergeStream1.on('data', function (chunk) {
result1.push(chunk)
})
const mergeStream = merge2(mergeStream1, read1, [read2, through3])
mergeStream
.on('data', function (chunk) {
result2.push(chunk)
if (result2.length <= 3) assert.deepStrictEqual(result1, result2)
else assert.deepStrictEqual(result1, [4, 5, 6])
})
.on('error', done)
.on('end', function () {
assert.deepStrictEqual(result2, [4, 5, 6, 1, 3, 2])
done()
})
})
})
}
function fakeReadStream (options) {
const readStream = new Stream.Readable(options)
readStream._read = function () {}
return readStream
}
function fakeReadClassicStream () {
const readStream = new Stream()
readStream.readable = true
readStream.push = function (data) {
if (data === null) {
this.emit('end')
readStream.readable = false
}
this.emit('data', data)
}
return readStream
}
gitextract_s115vdeb/
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── index.js
├── package.json
└── test/
└── index.js
SYMBOL INDEX (5 symbols across 2 files)
FILE: index.js
function merge2 (line 15) | function merge2 () {
function pauseStreams (line 128) | function pauseStreams (streams, options) {
FILE: test/index.js
function test (line 12) | function test (merge2) {
function fakeReadStream (line 324) | function fakeReadStream (options) {
function fakeReadClassicStream (line 330) | function fakeReadClassicStream () {
Condensed preview — 7 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (20K chars).
[
{
"path": ".gitignore",
"chars": 597,
"preview": "# Logs\nlogs\n*.log\n\n# Runtime data\npids\n*.pid\n*.seed\n\n# Directory for instrumented libs generated by jscoverage/JSCover\nl"
},
{
"path": ".travis.yml",
"chars": 115,
"preview": "language: node_js\nnode_js:\n - \"8\"\n - \"10\"\n - \"12\"\n - \"14\"\nsudo: false\ncache:\n directories:\n - node_modules\n"
},
{
"path": "LICENSE",
"chars": 1082,
"preview": "The MIT License (MIT)\n\nCopyright (c) 2014-2022 Teambition\n\nPermission is hereby granted, free of charge, to any person o"
},
{
"path": "README.md",
"chars": 3742,
"preview": "# merge2\n\nMerge multiple streams into one stream in sequence or parallel.\n\n[![NPM version][npm-image]][npm-url]\n[![Build"
},
{
"path": "index.js",
"chars": 3241,
"preview": "'use strict'\n/*\n * merge2\n * https://github.com/teambition/merge2\n *\n * Copyright (c) 2014-2022 Teambition\n * Licensed u"
},
{
"path": "package.json",
"chars": 830,
"preview": "{\n \"name\": \"merge2\",\n \"description\": \"Merge multiple streams into one stream in sequence or parallel.\",\n \"authors\": ["
},
{
"path": "test/index.js",
"chars": 8939,
"preview": "'use strict'\n\nconst tman = require('tman')\nconst assert = require('assert')\nconst Stream = require('stream')\nconst thunk"
}
]
About this extraction
This page contains the full source code of the teambition/merge2 GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 7 files (18.1 KB), approximately 5.1k tokens, and a symbol index with 5 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.