Skip to content

Commit

Permalink
feat(parsers): ByteLength is now more efficient (#1402)
Browse files Browse the repository at this point in the history
And better documented
  • Loading branch information
reconbot committed Nov 16, 2017
1 parent dcd256d commit f7eb2f0
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/parsers/byte-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
};
Expand Down

0 comments on commit f7eb2f0

Please sign in to comment.