Skip to content

Commit

Permalink
Merge pull request #19 from mcollina/next
Browse files Browse the repository at this point in the history
Updated to readable-stream@3
  • Loading branch information
mcollina authored Aug 12, 2018
2 parents f8f353b + 4c7cb70 commit be6dad6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "6"
- "7"
- "8"
- "10"
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Break up a stream and reassemble it so that each line is a chunk.
`split2` is inspired by [@dominictarr](https://github.com/dominictarr) [`split`](https://github.com/dominictarr/split) module,
and it is totally API compatible with it.
However, it is based on [`through2`](https://github.com/rvagg/through2) by [@rvagg](https://github.com/rvagg) and it is fully based on Stream3.
However, it is based on Node.js core [`Transform`](https://nodejs.org/api/stream.html#stream_new_stream_transform_options) via [`readable-stream`](https://github.com/nodejs/readable-stream)

`matcher` may be a `String`, or a `RegExp`. Example, read every line in a file ...

Expand All @@ -25,7 +25,7 @@ However, it is based on [`through2`](https://github.com/rvagg/through2) by [@rva

`split` takes an optional options object on it's third argument, which
is directly passed as a
[Transform](http://nodejs.org/api/stream.html#stream_class_stream_transform_1)
[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
Expand Down Expand Up @@ -69,16 +69,17 @@ is wrapped in a try-catch, while here it is not: if your parsing logic can throw

```bash
$ node bench.js

benchSplit*10000: 4241.612ms
benchBinarySplit*10000: 2372.667ms
benchSplit*10000: 2276.079ms
benchBinarySplit*10000: 2332.015ms
benchSplit*10000: 1484.983ms
benchBinarySplit*10000: 1484.080ms
benchSplit*10000: 1407.334ms
benchBinarySplit*10000: 1500.281ms
```

Benchmark taken on Node 8.11.3, on a Macbook i5 2018.

# License

Copyright (c) 2014-2017, Matteo Collina <[email protected]>
Copyright (c) 2014-2018, Matteo Collina <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
38 changes: 18 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2014-2016, Matteo Collina <[email protected]>
Copyright (c) 2014-2018, Matteo Collina <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,18 +16,20 @@ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

'use strict'

var through = require('through2')
var StringDecoder = require('string_decoder').StringDecoder
const { Transform } = require('readable-stream')
const { StringDecoder } = require('string_decoder')
const kLast = Symbol('last')
const kDecoder = Symbol('decoder')

function transform (chunk, enc, cb) {
this._last += this._decoder.write(chunk)
if (this._last.length > this.maxLength) {
this[kLast] += this[kDecoder].write(chunk)
if (this[kLast].length > this.maxLength) {
return cb(new Error('maximum buffer reached'))
}

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

this._last = list.pop()
this[kLast] = list.pop()

for (var i = 0; i < list.length; i++) {
push(this, this.mapper(list[i]))
Expand All @@ -38,10 +40,10 @@ function transform (chunk, enc, cb) {

function flush (cb) {
// forward any gibberish left in there
this._last += this._decoder.end()
this[kLast] += this[kDecoder].end()

if (this._last) {
push(this, this.mapper(this._last))
if (this[kLast]) {
push(this, this.mapper(this[kLast]))
}

cb()
Expand Down Expand Up @@ -90,18 +92,14 @@ function split (matcher, mapper, options) {
}
}

var stream = through(options, transform, flush)
options.transform = transform
options.flush = flush
options.readableObjectMode = true

// this stream is in objectMode only in the readable part
stream._readableState.objectMode = true
const stream = new Transform(options)

// objectMode default hwm is 16 and not 16384
if (stream._readableState.highWaterMark && !options.highWaterMark) {
stream._readableState.highWaterMark = 16
}

stream._last = ''
stream._decoder = new StringDecoder('utf8')
stream[kLast] = ''
stream[kDecoder] = new StringDecoder('utf8')
stream.matcher = matcher
stream.mapper = mapper
stream.maxLength = options.maxLength
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"pre-commit": "^1.1.2",
"safe-buffer": "^5.1.1",
"standard": "^11.0.0",
"tap": "^10.0.0"
"tap": "^12.0.0"
},
"dependencies": {
"through2": "^2.0.2"
"readable-stream": "^3.0.0"
},
"greenkeeper": {
"ignore": [
Expand Down

0 comments on commit be6dad6

Please sign in to comment.