mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
Parsers are now transform streams! I'm not 100% convinced this is easier to use but it is more clear about the mechanics. I think that might make it worthwhile. It also opens up a clear example to how to make your own parser.
45 lines
1 KiB
JavaScript
45 lines
1 KiB
JavaScript
'use strict';
|
|
var Transform = require('readable-stream').Transform;
|
|
var inherits = require('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;
|