node-serialport-bindings-cpp/lib/parser-delimiter.js
Francis Gulotta 223d39ff3f Remove readable-stream
Streams3 are stable in node 4+ so we don't need readable-stream anymore
2016-12-13 23:55:42 -05:00

44 lines
1.1 KiB
JavaScript

'use strict';
var Transform = require('stream').Transform;
var inherits = require('util').inherits;
function DelimiterParser(options) {
if (!(this instanceof DelimiterParser)) {
return new DelimiterParser(options);
}
Transform.call(this, options);
options = options || {};
if (options.delimiter === undefined) {
throw new TypeError('"delimiter" is not a bufferable object');
}
if (options.delimiter.length === 0) {
throw new TypeError('"delimiter" has a 0 or undefined length');
}
this.delimiter = new Buffer(options.delimiter);
this.buffer = new Buffer(0);
}
inherits(DelimiterParser, Transform);
DelimiterParser.prototype._transform = function(chunk, encoding, cb) {
var data = Buffer.concat([this.buffer, chunk]);
var position;
while ((position = data.indexOf(this.delimiter)) !== -1) {
this.push(data.slice(0, position));
data = data.slice(position + this.delimiter.length);
}
this.buffer = data;
cb();
};
DelimiterParser.prototype._flush = function(cb) {
this.push(this.buffer);
this.buffer = new Buffer(0);
cb();
};
module.exports = DelimiterParser;