From 990714631b0d5cd9a4b082fd1966fcfe4a1e601b Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Tue, 23 Oct 2018 14:31:47 +0200 Subject: [PATCH] turn on object mode for the readable side --- index.js | 8 +++++--- test.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2d93e2d..455769b 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ function BinarySplit (splitOn) { splitOn = splitOn || os.EOL const matcher = Buffer.from(splitOn) let buffered - return through(write, end) + return through({ readableObjectMode: true }, write, end) function write (buf, enc, done) { let offset = 0 @@ -22,7 +22,9 @@ function BinarySplit (splitOn) { while (true) { const idx = firstMatch(buf, offset - matcher.length + 1) if (idx !== -1 && idx < buf.length) { - this.push(buf.slice(lastMatch, idx)) + if (lastMatch !== idx) { + this.push(buf.slice(lastMatch, idx)) + } offset = idx + matcher.length lastMatch = offset } else { @@ -35,7 +37,7 @@ function BinarySplit (splitOn) { } function end (done) { - if (buffered) this.push(buffered) + if (buffered && buffered.length > 0) this.push(buffered) done() } diff --git a/test.js b/test.js index 3a8ae19..c6bc11c 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,6 @@ const test = require('tape') const fs = require('fs') +const PassThrough = require('stream').PassThrough const split = require('./') function splitTest (matcher, cb) { @@ -109,3 +110,16 @@ test('lookbehind in multi character matcher', function (t) { splitStream.write('\rb') splitStream.end() }) + +test('should not combine outputs', function (t) { + const pt = new PassThrough() + const stream = pt.pipe(split('.')) + pt.write('a.b') + pt.end('c.d') + setImmediate(function () { + t.equal(stream.read().toString(), 'a') + t.equal(stream.read().toString(), 'bc') + t.equal(stream.read().toString(), 'd') + t.end() + }) +})