-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from mcollina/next
Updated to readable-stream@3
- Loading branch information
Showing
4 changed files
with
29 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ... | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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])) | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters