From f7eb2f0df4317191afa3a95a5ac8bbed4ec49344 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Thu, 16 Nov 2017 12:50:48 -0500 Subject: [PATCH] feat(parsers): ByteLength is now more efficient (#1402) And better documented --- lib/parsers/byte-length.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/parsers/byte-length.js b/lib/parsers/byte-length.js index 63d4b6091..4cd0ced21 100644 --- a/lib/parsers/byte-length.js +++ b/lib/parsers/byte-length.js @@ -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(); } };