Skip to content

Commit

Permalink
Update to node 12 (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Oct 14, 2021
1 parent 4a776ff commit f662904
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 106 deletions.
25 changes: 1 addition & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,12 @@ name: ci
on: [push, pull_request]

jobs:
legacy:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [6.x, 8.x]

steps:
- uses: actions/checkout@v2

- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
npm install
- name: Run tests
run: |
npm run legacy
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 13.x, 14.x]
node-version: [12.x, 14.x, 16.x]

steps:
- uses: actions/checkout@v2
Expand Down
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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 Node.js core [`Transform`](https://nodejs.org/api/stream.html#stream_new_stream_transform_options) via [`readable-stream`](https://github.com/nodejs/readable-stream)
However, it is based on Node.js core [`Transform`](https://nodejs.org/api/stream.html#stream_new_stream_transform_options).

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

Expand Down Expand Up @@ -68,21 +68,9 @@ fs.createReadStream(file)
However, in [@dominictarr](https://github.com/dominictarr) [`split`](https://github.com/dominictarr/split) the mapper
is wrapped in a try-catch, while here it is not: if your parsing logic can throw, wrap it yourself. Otherwise, you can also use the stream error handling when mapper function throw.

# Benchmark

```bash
$ node bench.js
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-2018, Matteo Collina <hello@matteocollina.com>
Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com>

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
10 changes: 5 additions & 5 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

var split = require('./')
var bench = require('fastbench')
var binarySplit = require('binary-split')
var fs = require('fs')
const split = require('./')
const bench = require('fastbench')
const binarySplit = require('binary-split')
const fs = require('fs')

function benchSplit (cb) {
fs.createReadStream('package.json')
Expand All @@ -19,7 +19,7 @@ function benchBinarySplit (cb) {
.resume()
}

var run = bench([
const run = bench([
benchSplit,
benchBinarySplit
], 10000)
Expand Down
23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2014-2018, Matteo Collina <hello@matteocollina.com>
Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com>
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,15 +16,15 @@ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

'use strict'

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

function transform (chunk, enc, cb) {
var list
let list
if (this.overflow) { // Line buffer is full. Skip to start of next line.
var buf = this[kDecoder].write(chunk)
const buf = this[kDecoder].write(chunk)
list = buf.split(this.matcher)

if (list.length === 1) return cb() // Line ending not found. Discard entire chunk.
Expand All @@ -39,7 +39,7 @@ function transform (chunk, enc, cb) {

this[kLast] = list.pop()

for (var i = 0; i < list.length; i++) {
for (let i = 0; i < list.length; i++) {
try {
push(this, this.mapper(list[i]))
} catch (error) {
Expand All @@ -48,7 +48,10 @@ function transform (chunk, enc, cb) {
}

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

cb()
}
Expand Down Expand Up @@ -112,6 +115,7 @@ function split (matcher, mapper, options) {
}

options = Object.assign({}, options)
options.autoDestroy = true
options.transform = transform
options.flush = flush
options.readableObjectMode = true
Expand All @@ -123,8 +127,13 @@ function split (matcher, mapper, options) {
stream.matcher = matcher
stream.mapper = mapper
stream.maxLength = options.maxLength
stream.skipOverflow = options.skipOverflow
stream.skipOverflow = options.skipOverflow || false
stream.overflow = false
stream._destroy = function (err, cb) {
// Weird Node v12 bug that we need to work around
this._writableState.errorEmitted = false
cb(err)
}

return stream
}
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
"fastbench": "^1.0.0",
"nyc": "^15.0.1",
"pre-commit": "^1.1.2",
"safe-buffer": "^5.1.1",
"standard": "^16.0.1",
"tape": "^5.0.0"
},
"dependencies": {
"readable-stream": "^3.0.0"
}
}
Loading

0 comments on commit f662904

Please sign in to comment.