node-serialport-bindings-cpp/lib/parser-byte-length.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

45 lines
1 KiB
JavaScript

'use strict';
var Transform = require('stream').Transform;
var inherits = require('util').inherits;
function ByteLengthParser(options) {
if (!(this instanceof ByteLengthParser)) {
return new ByteLengthParser(options);
}
Transform.call(this, options);
options = options || {};
if (typeof options.length !== 'number') {
throw new TypeError('"length" is not a number');
}
if (options.length < 1) {
throw new TypeError('"length" is not greater than 0');
}
this.length = options.length;
this.buffer = new Buffer(0);
}
inherits(ByteLengthParser, Transform);
ByteLengthParser.prototype._transform = function(chunk, encoding, cb) {
var data = Buffer.concat([this.buffer, chunk]);
while (data.length >= this.length) {
var out = data.slice(0, this.length);
this.push(out);
data = data.slice(this.length);
}
this.buffer = data;
cb();
};
ByteLengthParser.prototype._flush = function(cb) {
this.push(this.buffer);
this.buffer = new Buffer(0);
cb();
};
module.exports = ByteLengthParser;