From 1967fb5e4870ae9e60e4b54aa92c4753605f18c7 Mon Sep 17 00:00:00 2001 From: Konstantin Chuvilyov Date: Wed, 3 Jun 2020 11:35:13 +0700 Subject: [PATCH] introduced pipeError option --- README.md | 2 ++ index.js | 11 ++++++++--- test/index.js | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f19d627..27f8eb9 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,8 @@ 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`. diff --git a/index.js b/index.js index 76da693..48d3f77 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,7 @@ function merge2 () { 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) @@ -64,9 +65,13 @@ function merge2 () { stream.on('merge2UnpipeEnd', onend) stream.on('end', onend) - stream.on('error', function (err) { - mergedStream.emit('error', err) - }) + + if (doPipeError) { + stream.on('error', function (err) { + mergedStream.emit('error', err) + }) + } + stream.pipe(mergedStream, { end: false }) // compatible for old stream stream.resume() diff --git a/test/index.js b/test/index.js index 3bd68f4..544ed70 100644 --- a/test/index.js +++ b/test/index.js @@ -50,7 +50,7 @@ function test (merge2) { tman.it('merge2 - error handling', function (done) { const ts = through.obj() - const mergeStream = merge2(toThrough(ts)) + const mergeStream = merge2(toThrough(ts), { pipeError: true }) const expectedError = new Error('error') thunk.delay(100)(function () {