feat(parsers): ByteLength is now more efficient (#1402)

And better documented
This commit is contained in:
Francis Gulotta 2017-11-16 12:50:48 -05:00 • committed by GitHub
parent dcd256d27a
commit f7eb2f0df4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,14 +5,16 @@ const Transform = require('stream').Transform;
/**
* A transform stream that emits data as a buffer after a specific number of bytes are received.
* @extends Transform
* @param {Object} options
* @param {Number} options.length the number of bytes on each data event
* @example
To use the `ByteLength` parser, provide the length of the number of bytes:
To use the `ByteLength` parser:
```js
const SerialPort = require('serialport');
const ByteLength = SerialPort.parsers.ByteLength
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(new ByteLength({length: 8}));
parser.on('data', console.log);
parser.on('data', console.log); // will have 8 bytes per data event
```
*/
class ByteLengthParser extends Transform {
@ -29,23 +31,28 @@ class ByteLengthParser extends Transform {
}
this.length = options.length;
this.buffer = Buffer.alloc(0);
this.position = 0;
this.buffer = Buffer.alloc(this.length);
}
_transform(chunk, encoding, cb) {
let data = Buffer.concat([this.buffer, chunk]);
while (data.length >= this.length) {
const out = data.slice(0, this.length);
this.push(out);
data = data.slice(this.length);
let cursor = 0;
while (cursor < chunk.length) {
this.buffer[this.position] = chunk[cursor];
cursor++;
this.position++;
if (this.position === this.length) {
this.push(this.buffer);
this.buffer = Buffer.alloc(this.length);
this.position = 0;
}
}
this.buffer = data;
cb();
}
_flush(cb) {
this.push(this.buffer);
this.buffer = Buffer.alloc(0);
this.push(this.buffer.slice(0, this.position));
this.buffer = Buffer.alloc(this.length);
cb();
}
};