diff --git a/.travis.yml b/.travis.yml index d3ffbc4..a8955e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,6 @@ language: node_js sudo: false node_js: - - "0.10" - - "0.12" - - "4" - - "5" - "6" - - "7" - "8" - "10" diff --git a/README.md b/README.md index 04bca60..acb72a5 100644 --- a/README.md +++ b/README.md @@ -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 +Copyright (c) 2014-2018, Matteo Collina Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/index.js b/index.js index 70a8320..57d76a4 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2014-2016, Matteo Collina +Copyright (c) 2014-2018, Matteo Collina 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 diff --git a/package.json b/package.json index 98de24f..36b18e7 100644 --- a/package.json +++ b/package.json @@ -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": [