Created
May 21, 2018 21:48
-
-
Save rhengles/9c598e21830cabd29a0664508cab814c to your computer and use it in GitHub Desktop.
Node Split Lines Transform Stream
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
var strDec = require('string_decoder'); | |
var StringDecoder = strDec.StringDecoder; | |
var stream = require('stream'); | |
var Transform = stream.Transform; | |
var util = require('util'); | |
function LineSplitter(options) { | |
if (!(this instanceof LineSplitter)) { | |
return new LineSplitter(options); | |
} | |
options || (options = {}); | |
options.readableObjectMode = true; | |
Transform.call(this, options); | |
var state = this._writableState; | |
this._decoder = new StringDecoder(state.defaultEncoding); | |
this._lineBuffer = ''; | |
this._eol = options.eol || '\n'; | |
} | |
util.inherits(LineSplitter, Transform); | |
LineSplitter.prototype._transform = function(chunk, encoding, callback) { | |
if (encoding === 'buffer') { | |
chunk = this._decoder.write(chunk); | |
} | |
this._readChunk(chunk); | |
callback(); | |
}; | |
LineSplitter.prototype._readChunk = function(chunk) { | |
var chunk = this._lineBuffer + chunk; | |
var eol = this._eol; | |
var eolSize = eol.length; | |
var eolIndex; | |
while (-1 !== (eolIndex = chunk.indexOf(eol))) { | |
var line = chunk.substr(0, eolIndex); | |
chunk = chunk.substr(eolIndex + eolSize); | |
this.push(line); | |
} | |
this._lineBuffer = chunk; | |
}; | |
LineSplitter.prototype._flush = function() { | |
this._readChunk(this._decoder.end()); | |
var lastLine = this._lineBuffer; | |
this._lineBuffer = null; | |
if (lastLine.length) { | |
this.push(lastLine); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment