UNPKG

581 BJavaScriptView Raw
1'use strict'
2
3let stream = require('stream')
4
5// this splits a stream into lines
6let transform = new stream.Transform({decodeStrings: false})
7transform._transform = function (chunk, encoding, next) {
8 let data = chunk
9 if (this._lastLineData) data = this._lastLineData + data
10
11 let lines = data.split('\n')
12 this._lastLineData = lines.splice(lines.length - 1, 1)[0]
13
14 lines.forEach(this.push.bind(this))
15 next()
16}
17
18transform._flush = function (done) {
19 if (this._lastLineData) this.push(this._lastLineData)
20 this._lastLineData = null
21 done()
22}
23
24module.exports = transform