mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
44 lines
1.1 KiB
JavaScript
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;
|