Skip to content

Commit

Permalink
Add skipOverflow option
Browse files Browse the repository at this point in the history
Also addresses surprising maxLength behavior described in #23
  • Loading branch information
IronSavior committed Dec 12, 2018
1 parent 50c92f6 commit 7036986
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ is directly passed as a
[Transform](https://nodejs.org/api/stream.html#stream_new_stream_transform_options)
option.

Additionally, the `.maxLength` option is implemented, which will make the split stream throw an error
if the buffer size exceeds `.maxLength`.
Additionally, the `.maxLength` and `.skipOverflow` options are implemented, which set limits on the internal
buffer size and the stream's behavior when the limit is exceeded. There is no limit unless `maxLength` is set. When
the internal buffer size exceeds `maxLength`, the stream emits an error by default. You may also set `skipOverflow` to
true to suppress the error and instead skip past any lines that cause the internal buffer to exceed `maxLength`.

Calling `.destroy` will make the stream emit `close`. Use this to perform cleanup logic

Expand Down
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@ const kLast = Symbol('last')
const kDecoder = Symbol('decoder')

function transform (chunk, enc, cb) {
this[kLast] += this[kDecoder].write(chunk)
if (this[kLast].length > this.maxLength) {
return cb(new Error('maximum buffer reached'))
var list
if (this.overflow) {
var buf = this[kDecoder].write(chunk)
list = buf.split(this.matcher)
if (list.length === 1) return cb()

list.shift()
this.overflow = false
} else {
this[kLast] += this[kDecoder].write(chunk)
list = this[kLast].split(this.matcher)
}

var list = this[kLast].split(this.matcher)

this[kLast] = list.pop()

for (var i = 0; i < list.length; i++) {
push(this, this.mapper(list[i]))
}

this.overflow = this[kLast].length > this.maxLength
if (this.overflow && !this.skipOverflow) return cb(new Error('maximum buffer reached'))

cb()
}

Expand Down Expand Up @@ -103,6 +112,7 @@ function split (matcher, mapper, options) {
stream.matcher = matcher
stream.mapper = mapper
stream.maxLength = options.maxLength
stream.skipOverflow = options.skipOverflow

return stream
}
Expand Down
29 changes: 29 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,32 @@ test('readable highWaterMark', function (t) {
t.equal(input._readableState.highWaterMark, 16)
t.end()
})

test('maxLength < chunk size', function (t) {
t.plan(2)

var input = split({ maxLength: 2 })

input.pipe(strcb(function (err, list) {
t.error(err)
t.deepEqual(list, ['a', 'b'])
}))

input.end('a\nb')
})

test('maximum buffer limit w/skip', function (t) {
t.plan(2)

var input = split({ maxLength: 2, skipOverflow: true })

input.pipe(strcb(function (err, list) {
t.error(err)
t.deepEqual(list, ['a', 'b', 'c'])
}))

input.write('a\n123')
input.write('456')
input.write('789\nb\nc')
input.end()
})

0 comments on commit 7036986

Please sign in to comment.