diff --git a/docs/parsers_byte-length.js.html b/docs/parsers_byte-length.js.html
deleted file mode 100644
index b58164c..0000000
--- a/docs/parsers_byte-length.js.html
+++ /dev/null
@@ -1,116 +0,0 @@
-
-
-
-
-
-
-
-
-
diff --git a/docs/parsers_cctalk.js.html b/docs/parsers_cctalk.js.html
deleted file mode 100644
index 82a7a36..0000000
--- a/docs/parsers_cctalk.js.html
+++ /dev/null
@@ -1,101 +0,0 @@
-
-
-
-
-
-
-
-
-
diff --git a/docs/parsers_delimiter.js.html b/docs/parsers_delimiter.js.html
deleted file mode 100644
index c889782..0000000
--- a/docs/parsers_delimiter.js.html
+++ /dev/null
@@ -1,109 +0,0 @@
-
-
-
-
-
-
-
-
-
diff --git a/docs/parsers_readline.js.html b/docs/parsers_readline.js.html
deleted file mode 100644
index 6cbc3db..0000000
--- a/docs/parsers_readline.js.html
+++ /dev/null
@@ -1,89 +0,0 @@
-
-
-
-
-
-
-
-
-
diff --git a/docs/parsers_ready.js.html b/docs/parsers_ready.js.html
deleted file mode 100644
index 773240f..0000000
--- a/docs/parsers_ready.js.html
+++ /dev/null
@@ -1,124 +0,0 @@
-
-
-
-
-
-
-
-
-
diff --git a/docs/parsers_regex.js.html b/docs/parsers_regex.js.html
deleted file mode 100644
index f6454ae..0000000
--- a/docs/parsers_regex.js.html
+++ /dev/null
@@ -1,110 +0,0 @@
-
-
-
-
-
-
-
-
-
diff --git a/docs/serialport.js.html b/docs/serialport.js.html
index 32065b7..2598b52 100644
--- a/docs/serialport.js.html
+++ b/docs/serialport.js.html
@@ -22,7 +22,7 @@
@@ -373,7 +373,7 @@ SerialPort.prototype._writev = function(data, callback) {
*/
/**
- * Listening for the `data` event puts the port in flowing mode. Data is emitted as soon as it's received. Data is a `Buffer` object with a varying amount of data in it. The `readLine` parser converts the data into string lines. See the [parsers](#module_serialport--SerialPort.parsers) section for more information on parsers, and the [Node.js stream documentation](https://nodejs.org/api/stream.html#stream_event_data) for more information on the data event.
+ * Listening for the `data` event puts the port in flowing mode. Data is emitted as soon as it's received. Data is a `Buffer` object with a varying amount of data in it. The `readLine` parser converts the data into string lines. See the [parsers](https://node-serialport.github.io/node-serialport/global.html#Parsers) section for more information on parsers, and the [Node.js stream documentation](https://nodejs.org/api/stream.html#stream_event_data) for more information on the data event.
* @event data
*/
@@ -684,7 +684,7 @@ module.exports = SerialPort;
- Documentation generated by JSDoc 3.5.5 on Tue Feb 27 2018 23:06:42 GMT-0500 (EST) using the docdash theme.
+ Documentation generated by JSDoc 3.5.5 on Sat Mar 10 2018 23:49:02 GMT-0500 (EST) using the docdash theme.
diff --git a/lib/bindings/linux-list.js b/lib/bindings/linux-list.js
index 3d79244..cd7764d 100644
--- a/lib/bindings/linux-list.js
+++ b/lib/bindings/linux-list.js
@@ -1,7 +1,7 @@
'use strict';
const childProcess = require('child_process');
-const Readline = require('../parsers/readline');
+const Readline = require('parser-readline');
// get only serial port names
function checkPathOfDevice(path) {
diff --git a/lib/parsers.js b/lib/parsers.js
new file mode 100644
index 0000000..941fad8
--- /dev/null
+++ b/lib/parsers.js
@@ -0,0 +1,36 @@
+'use strict';
+/**
+ * Parsers are collection of transform streams to processes incoming data
+ * @summary The default `Parsers` are [Transform streams](https://nodejs.org/api/stream.html#stream_class_stream_transform) that process incoming data. To use the parsers, you must create them and then pipe the Serialport to the parser. Be careful to only write to the SerialPort object and not the parser. Full documentation for parsers can be found in [their api docs](https://node-serialport.github.io/parsers/).
+ * @typedef {Object} Parsers
+ * @property {Transform} ByteLength
+ * @property {Transform} CCtalk
+ * @property {Transform} Delimiter
+ * @property {Transform} Readline
+ * @property {Transform} Ready
+ * @property {Transform} Regex
+
+ * @since 5.0.0
+ * @example
+```js
+const SerialPort = require('serialport');
+const Readline = SerialPort.parsers.Readline;
+const port = new SerialPort('/dev/tty-usbserial1');
+const parser = new Readline();
+port.pipe(parser);
+parser.on('data', console.log);
+port.write('ROBOT PLEASE RESPOND\n');
+
+// Creating the parser and piping can be shortened to
+// const parser = port.pipe(new Readline());
+```
+ */
+
+module.exports = {
+ ByteLength: require('parser-byte-length'),
+ CCTalk: require('parser-cctalk'),
+ Delimiter: require('parser-delimiter'),
+ Readline: require('parser-readline'),
+ Ready: require('parser-ready'),
+ Regex: require('parser-regex')
+};
diff --git a/lib/parsers/byte-length.js b/lib/parsers/byte-length.js
deleted file mode 100644
index 91271be..0000000
--- a/lib/parsers/byte-length.js
+++ /dev/null
@@ -1,58 +0,0 @@
-'use strict';
-const Buffer = require('safe-buffer').Buffer;
-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 parser options object
- * @param {Number} options.length the number of bytes on each data event
- * @example
-// To use the `ByteLength` parser:
-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); // will have 8 bytes per data event
- */
-class ByteLengthParser extends Transform {
- constructor(options) {
- super(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.position = 0;
- this.buffer = Buffer.alloc(this.length);
- }
-
- _transform(chunk, encoding, cb) {
- 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;
- }
- }
- cb();
- }
-
- _flush(cb) {
- this.push(this.buffer.slice(0, this.position));
- this.buffer = Buffer.alloc(this.length);
- cb();
- }
-};
-
-module.exports = ByteLengthParser;
diff --git a/lib/parsers/cctalk.js b/lib/parsers/cctalk.js
deleted file mode 100644
index de03a6c..0000000
--- a/lib/parsers/cctalk.js
+++ /dev/null
@@ -1,43 +0,0 @@
-'use strict';
-const Transform = require('stream').Transform;
-const Buffer = require('safe-buffer').Buffer;
-/**
- * Parses the CCTalk protocol
- * @extends Transform
- * @example
-// CCTalk Messages are emitted as buffers.
-const SerialPort = require('serialport');
-const CCTalk = SerialPort.parsers.CCTalk;
-const port = new SerialPort('/dev/ttyUSB0');
-const parser = port.pipe(new CCtalk());
-parser.on('data', console.log);
- */
-class CCTalkParser extends Transform {
- constructor() {
- super();
- this.array = [];
- this.cursor = 0;
- }
- _transform(buffer, _, cb) {
- this.cursor += buffer.length;
- // TODO: Better Faster es7 no supported by node 4
- // ES7 allows directly push [...buffer]
- // this.array = this.array.concat(Array.from(buffer)); //Slower ?!?
- Array.from(buffer)
- .map((byte) => this.array.push(byte));
- while (this.cursor > 1 && this.cursor >= this.array[1] + 5) {
- // full frame accumulated
- // copy command from the array
- const FullMsgLength = this.array[1] + 5;
-
- const frame = Buffer.from(this.array.slice(0, FullMsgLength));
- // Preserve Extra Data
- this.array = this.array.slice(frame.length, this.array.length);
- this.cursor -= FullMsgLength;
- this.push(frame);
- }
- cb();
- }
-};
-
-module.exports = CCTalkParser;
diff --git a/lib/parsers/delimiter.js b/lib/parsers/delimiter.js
deleted file mode 100644
index 5653e7e..0000000
--- a/lib/parsers/delimiter.js
+++ /dev/null
@@ -1,51 +0,0 @@
-'use strict';
-const Buffer = require('safe-buffer').Buffer;
-const Transform = require('stream').Transform;
-/**
- * A transform stream that emits data each time a byte sequence is received.
- * @extends Transform
- * @example
-// To use the `Delimiter` parser, provide a delimiter as a string, buffer, or array of bytes:
-const SerialPort = require('serialport');
-const Delimiter = SerialPort.parsers.Delimiter;
-const port = new SerialPort('/dev/tty-usbserial1');
-const parser = port.pipe(new Delimiter({ delimiter: '\n' }));
-parser.on('data', console.log);
- */
-class DelimiterParser extends Transform {
- constructor(options) {
- options = options || {};
- super(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.includeDelimiter = options.includeDelimiter !== undefined ? options.includeDelimiter : false;
- this.delimiter = Buffer.from(options.delimiter);
- this.buffer = Buffer.alloc(0);
- }
-
- _transform(chunk, encoding, cb) {
- let data = Buffer.concat([this.buffer, chunk]);
- let position;
- while ((position = data.indexOf(this.delimiter)) !== -1) {
- this.push(data.slice(0, position + (this.includeDelimiter ? this.delimiter.length : 0)));
- data = data.slice(position + this.delimiter.length);
- }
- this.buffer = data;
- cb();
- }
-
- _flush(cb) {
- this.push(this.buffer);
- this.buffer = Buffer.alloc(0);
- cb();
- }
-};
-
-module.exports = DelimiterParser;
diff --git a/lib/parsers/index.js b/lib/parsers/index.js
deleted file mode 100644
index 6e18d42..0000000
--- a/lib/parsers/index.js
+++ /dev/null
@@ -1,37 +0,0 @@
-'use strict';
-/**
- * The default `Parsers` are [Transform streams](https://nodejs.org/api/stream.html#stream_class_stream_transform) that parse data in different ways to transform incoming data.
-
- To use the parsers, you must create them and then pipe the Serialport to the parser. Be careful to only write to the SerialPort object and not the parser.
- * @typedef {Object} Parsers
- * @property {Transform} ByteLength
- * @property {Transform} CCtalk
- * @property {Transform} Delimiter
- * @property {Transform} Readline
- * @property {Transform} Ready
- * @property {Transform} Regex
-
- * @since 5.0.0
- * @example
-```js
-const SerialPort = require('serialport');
-const Readline = SerialPort.parsers.Readline;
-const port = new SerialPort('/dev/tty-usbserial1');
-const parser = new Readline();
-port.pipe(parser);
-parser.on('data', console.log);
-port.write('ROBOT PLEASE RESPOND\n');
-
-// Creating the parser and piping can be shortened to
-// const parser = port.pipe(new Readline());
-```
- */
-
-module.exports = {
- ByteLength: require('./byte-length'),
- CCTalk: require('./cctalk'),
- Delimiter: require('./delimiter'),
- Readline: require('./readline'),
- Ready: require('./ready'),
- Regex: require('./regex')
-};
diff --git a/lib/parsers/readline.js b/lib/parsers/readline.js
deleted file mode 100644
index c463f4b..0000000
--- a/lib/parsers/readline.js
+++ /dev/null
@@ -1,31 +0,0 @@
-'use strict';
-const Buffer = require('safe-buffer').Buffer;
-const DelimiterParser = require('./delimiter');
-/**
- * A transform stream that emits data after a newline delimiter is received.
- *
- * To use the `Readline` parser, provide a delimiter (defaults to `\n`). Data is emitted as string controllable by the `encoding` option (defaults to `utf8`).
- * @extends DelimiterParser
- * @example
-const SerialPort = require('serialport');
-const Readline = SerialPort.parsers.Readline;
-const port = new SerialPort('/dev/tty-usbserial1');
-const parser = port.pipe(new Readline({ delimiter: '\r\n' }));
-parser.on('data', console.log);
-*/
-class ReadLineParser extends DelimiterParser {
- constructor(options) {
- const opts = Object.assign({
- delimiter: Buffer.from('\n', 'utf8'),
- encoding: 'utf8'
- }, options);
-
- if (typeof opts.delimiter === 'string') {
- opts.delimiter = Buffer.from(opts.delimiter, opts.encoding);
- }
-
- super(opts);
- }
-};
-
-module.exports = ReadLineParser;
diff --git a/lib/parsers/ready.js b/lib/parsers/ready.js
deleted file mode 100644
index e349554..0000000
--- a/lib/parsers/ready.js
+++ /dev/null
@@ -1,66 +0,0 @@
-'use strict';
-const Buffer = require('safe-buffer').Buffer;
-const Transform = require('stream').Transform;
-/**
- * A transform stream that waits for a sequence of "ready" bytes before emitting a ready event and emitting data events
- *
- * To use the `Ready` parser provide a byte start sequence. After the bytes have been received a ready event is fired and data events are passed through.
- * @extends Transform
- * @example
-const SerialPort = require('serialport');
-const Ready = SerialPort.parsers.Ready;
-const port = new SerialPort('/dev/tty-usbserial1');
-const parser = port.pipe(new Ready({ delimiter: 'READY' }));
-parser.on('ready', () => console.log('the ready byte sequence has been received'))
-parser.on('data', console.log); // all data after READY is received
- */
-class ReadyParser extends Transform {
- /**
- *
- * @param {object} options options for the parser
- * @param {string|Buffer|array} options.delimiter data to look for before emitted "ready"
- */
- constructor(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');
- }
-
- super(options);
- this.delimiter = Buffer.from(options.delimiter);
- this.readOffset = 0;
- this.ready = false;
- }
-
- _transform(chunk, encoding, cb) {
- if (this.ready) {
- this.push(chunk);
- return cb();
- }
- const delimiter = this.delimiter;
- let chunkOffset = 0;
- while (this.readOffset < delimiter.length && chunkOffset < chunk.length) {
- if (delimiter[this.readOffset] === chunk[chunkOffset]) {
- this.readOffset++;
- } else {
- this.readOffset = 0;
- }
- chunkOffset++;
- }
- if (this.readOffset === delimiter.length) {
- this.ready = true;
- this.emit('ready');
- const chunkRest = chunk.slice(chunkOffset);
- if (chunkRest.length > 0) {
- this.push(chunkRest);
- }
- }
- cb();
- }
-};
-
-module.exports = ReadyParser;
diff --git a/lib/parsers/regex.js b/lib/parsers/regex.js
deleted file mode 100644
index 977d04b..0000000
--- a/lib/parsers/regex.js
+++ /dev/null
@@ -1,52 +0,0 @@
-'use strict';
-const Transform = require('stream').Transform;
-/**
- * A transform stream that uses a regular expression to split the incoming text upon.
- *
- * To use the `Regex` parser provide a regular expression to split the incoming text upon. Data is emitted as string controllable by the `encoding` option (defaults to `utf8`).
- * @extends Transform
- * @example
-const SerialPort = require('serialport');
-const Regex = SerialPort.parsers.Regex;
-const port = new SerialPort('/dev/tty-usbserial1');
-const parser = port.pipe(new Regex({ regex: /[\r\n]+/ }));
-parser.on('data', console.log);
- */
-class RegexParser extends Transform {
- constructor(options) {
- const opts = Object.assign({
- encoding: 'utf8'
- }, options);
-
- if (opts.regex === undefined) {
- throw new TypeError('"options.regex" must be a regular expression pattern or object');
- }
-
- if (!(opts.regex instanceof RegExp)) {
- opts.regex = new RegExp(opts.regex);
- }
- super(opts);
-
- this.regex = opts.regex;
- this.buffer = '';
- }
-
- _transform(chunk, encoding, cb) {
- const data = this.buffer + chunk;
- const parts = data.split(this.regex);
- this.buffer = parts.pop();
-
- parts.forEach((part) => {
- this.push(part);
- });
- cb();
- }
-
- _flush(cb) {
- this.push(this.buffer);
- this.buffer = '';
- cb();
- }
-};
-
-module.exports = RegexParser;
diff --git a/lib/serialport.js b/lib/serialport.js
index acffaa9..fe176aa 100644
--- a/lib/serialport.js
+++ b/lib/serialport.js
@@ -334,7 +334,7 @@ SerialPort.prototype._writev = function(data, callback) {
*/
/**
- * Listening for the `data` event puts the port in flowing mode. Data is emitted as soon as it's received. Data is a `Buffer` object with a varying amount of data in it. The `readLine` parser converts the data into string lines. See the [parsers](#module_serialport--SerialPort.parsers) section for more information on parsers, and the [Node.js stream documentation](https://nodejs.org/api/stream.html#stream_event_data) for more information on the data event.
+ * Listening for the `data` event puts the port in flowing mode. Data is emitted as soon as it's received. Data is a `Buffer` object with a varying amount of data in it. The `readLine` parser converts the data into string lines. See the [parsers](https://node-serialport.github.io/node-serialport/global.html#Parsers) section for more information on parsers, and the [Node.js stream documentation](https://nodejs.org/api/stream.html#stream_event_data) for more information on the data event.
* @event data
*/
diff --git a/package.json b/package.json
index 72e2ccb..3dc179c 100644
--- a/package.json
+++ b/package.json
@@ -60,6 +60,12 @@
"commander": "^2.13.0",
"debug": "^3.1.0",
"nan": "^2.9.2",
+ "parser-byte-length": "^1.0.2",
+ "parser-cctalk": "^1.0.2",
+ "parser-delimiter": "^1.0.2",
+ "parser-readline": "^1.0.2",
+ "parser-ready": "^1.0.2",
+ "parser-regex": "^1.0.2",
"prebuild-install": "^2.4.1",
"promirepl": "^1.0.1",
"prompt-list": "^3.1.2",
diff --git a/test/parser-byte-length.js b/test/parser-byte-length.js
deleted file mode 100644
index dde382f..0000000
--- a/test/parser-byte-length.js
+++ /dev/null
@@ -1,63 +0,0 @@
-'use strict';
-/* eslint-disable no-new */
-
-const Buffer = require('safe-buffer').Buffer;
-const sinon = require('sinon');
-const ByteLengthParser = require('../lib/parsers/byte-length');
-
-describe('ByteLengthParser', () => {
- it('emits data events every 8 bytes', () => {
- const data = Buffer.from('Robots are so freaking cool!');
- const spy = sinon.spy();
- const parser = new ByteLengthParser({ length: 8 });
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 3);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('Robots a'));
- assert.deepEqual(spy.getCall(1).args[0], Buffer.from('re so fr'));
- assert.deepEqual(spy.getCall(2).args[0], Buffer.from('eaking c'));
- });
-
- it('throws when not provided with a length', () => {
- assert.throws(() => {
- new ByteLengthParser({});
- });
- });
-
- it('throws when length is zero', () => {
- assert.throws(() => {
- new ByteLengthParser({
- length: 0
- });
- });
- });
-
- it('throws when called with a non numeric length', () => {
- assert.throws(() => {
- new ByteLengthParser({
- length: 'foop'
- });
- });
- });
-
- it('continues looking for bytes in additional writes', () => {
- const parser = new ByteLengthParser({ length: 4 });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from('ab'));
- parser.write(Buffer.from('cd'));
- assert.equal(spy.callCount, 1);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('abcd'));
- });
-
- it('flushes remaining data when the stream ends', () => {
- const parser = new ByteLengthParser({ length: 4 });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from('12'));
- assert.equal(spy.callCount, 0);
- parser.end();
- assert.equal(spy.callCount, 1);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('12'));
- });
-});
diff --git a/test/parser-cctalk.js b/test/parser-cctalk.js
deleted file mode 100644
index 7c757a8..0000000
--- a/test/parser-cctalk.js
+++ /dev/null
@@ -1,41 +0,0 @@
-'use strict';
-/* eslint-disable no-new */
-
-const Buffer = require('safe-buffer').Buffer;
-const sinon = require('sinon');
-const CCTalkParser = require('../lib/parsers/cctalk');
-
-describe('CCTalkParser', () => {
- it('emits data for a default length message', () => {
- const data = Buffer.from([2, 0, 1, 254, 217]);
- const spy = sinon.spy();
- const parser = new CCTalkParser();
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 1);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 0, 1, 254, 217]));
- });
-
- it('emits data for a 7 byte length message', () => {
- const parser = new CCTalkParser();
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217]));
- assert.equal(spy.callCount, 1);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]));
- });
-
- it('emits 2 times data first length 7 secund length 5', () => {
- const parser = new CCTalkParser();
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from([2, 2, 1]));
- parser.write(Buffer.from([254, 1, 1]));
- parser.write(Buffer.from([217, 2]));
- parser.write(Buffer.from([0, 1, 254, 217]));
- assert.equal(spy.callCount, 2);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]));
- assert.deepEqual(spy.getCall(1).args[0], Buffer.from([2, 0, 1, 254, 217]));
- });
-});
-// TODO: parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217, 2, 0, 1, 254, 217, 2, 2, 1, 251, 1, 1, 217, 2, 2, 1, 252, 1, 1, 217, 2, 0, 1, 253, 217]));
diff --git a/test/parser-delimiter.js b/test/parser-delimiter.js
deleted file mode 100644
index efecd54..0000000
--- a/test/parser-delimiter.js
+++ /dev/null
@@ -1,143 +0,0 @@
-'use strict';
-/* eslint-disable no-new */
-
-const Buffer = require('safe-buffer').Buffer;
-const sinon = require('sinon');
-
-const DelimiterParser = require('../lib/parsers/delimiter');
-
-describe('DelimiterParser', () => {
- it('transforms data to strings split on a delimiter', () => {
- const spy = sinon.spy();
- const parser = new DelimiterParser({
- delimiter: Buffer.from('\n')
- });
- parser.on('data', spy);
- parser.write(Buffer.from('I love robots\nEach '));
- parser.write(Buffer.from('and Every One\n'));
- parser.write(Buffer.from('\n'));
- parser.write(Buffer.from('even you!'));
-
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('I love robots'));
- assert.deepEqual(spy.getCall(1).args[0], Buffer.from('Each and Every One'));
- assert(spy.calledTwice);
- });
-
- it('includes delimiter when includeDelimiter is true', () => {
- const spy = sinon.spy();
- const parser = new DelimiterParser({
- delimiter: Buffer.from('\n'),
- includeDelimiter: true
- });
- parser.on('data', spy);
- parser.write(Buffer.from('I love robots\nEach '));
- parser.write(Buffer.from('and Every One\n'));
- parser.write(Buffer.from('\n'));
- parser.write(Buffer.from('even you!'));
-
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('I love robots\n'));
- assert.deepEqual(spy.getCall(1).args[0], Buffer.from('Each and Every One\n'));
- assert.deepEqual(spy.getCall(2).args[0], Buffer.from('\n'));
- assert.equal(spy.callCount, 3);
- });
-
- it('flushes remaining data when the stream ends', () => {
- const parser = new DelimiterParser({ delimiter: Buffer.from([0]) });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from([1]));
- assert.equal(spy.callCount, 0);
- parser.end();
- assert.equal(spy.callCount, 1);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1]));
- });
-
- it('throws when not provided with a delimiter', () => {
- assert.throws(() => {
- new DelimiterParser({});
- });
- });
-
- it('throws when called with a 0 length delimiter', () => {
- assert.throws(() => {
- new DelimiterParser({
- delimiter: Buffer.alloc(0)
- });
- });
-
- assert.throws(() => {
- new DelimiterParser({
- delimiter: ''
- });
- });
-
- assert.throws(() => {
- new DelimiterParser({
- delimiter: []
- });
- });
- });
-
- it('allows setting of the delimiter with a string', () => {
- new DelimiterParser({ delimiter: 'string' });
- });
-
- it('allows setting of the delimiter with a buffer', () => {
- new DelimiterParser({ delimiter: Buffer.from([1]) });
- });
-
- it('allows setting of the delimiter with an array of bytes', () => {
- new DelimiterParser({ delimiter: [1] });
- });
-
- it('emits data events every time it meets 00x 00x', () => {
- const data = Buffer.from('This could be\0\0binary data\0\0sent from a Moteino\0\0');
- const parser = new DelimiterParser({ delimiter: [0, 0] });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 3);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('This could be'));
- assert.deepEqual(spy.getCall(1).args[0], Buffer.from('binary data'));
- assert.deepEqual(spy.getCall(2).args[0], Buffer.from('sent from a Moteino'));
- });
-
- it('accepts single byte delimiter', () => {
- const data = Buffer.from('This could be\0binary data\0sent from a Moteino\0');
- const parser = new DelimiterParser({ delimiter: [0] });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 3);
- });
-
- it('Works when buffer starts with delimiter', () => {
- const data = Buffer.from('\0Hello\0World\0');
- const parser = new DelimiterParser({ delimiter: Buffer.from([0]) });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 2);
- });
-
- it('should only emit if delimiters are strictly in row', () => {
- const data = Buffer.from('\0Hello\u0001World\0\0\u0001');
- const parser = new DelimiterParser({ delimiter: [0, 1] });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 1);
- });
-
- it('continues looking for delimiters in the next buffers', () => {
- const parser = new DelimiterParser({ delimiter: [0, 0] });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from('This could be\0\0binary '));
- parser.write(Buffer.from('data\0\0sent from a Moteino\0\0'));
- assert.equal(spy.callCount, 3);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('This could be'));
- assert.deepEqual(spy.getCall(1).args[0], Buffer.from('binary data'));
- assert.deepEqual(spy.getCall(2).args[0], Buffer.from('sent from a Moteino'));
- });
-});
diff --git a/test/parser-readline.js b/test/parser-readline.js
deleted file mode 100644
index b37aca5..0000000
--- a/test/parser-readline.js
+++ /dev/null
@@ -1,115 +0,0 @@
-'use strict';
-/* eslint-disable no-new */
-
-const Buffer = require('safe-buffer').Buffer;
-const sinon = require('sinon');
-
-const ReadlineParser = require('../lib/parsers/readline');
-
-describe('ReadlineParser', () => {
- it('transforms data to strings split on a delimiter', () => {
- const spy = sinon.spy();
- const parser = new ReadlineParser();
- parser.on('data', spy);
- parser.write(Buffer.from('I love robots\nEach '));
- parser.write(Buffer.from('and Every One\n'));
- parser.write(Buffer.from('even you!'));
- assert(spy.calledWith('I love robots'));
- assert(spy.calledWith('Each and Every One'));
- assert(spy.calledTwice);
- parser.end();
- assert(spy.calledWith('even you!'));
- assert(spy.calledThrice);
- });
-
- it('allows setting of the delimiter with a string', () => {
- const spy = sinon.spy();
- const parser = new ReadlineParser({ delimiter: 'a' });
- parser.on('data', spy);
- parser.write(Buffer.from('how are youa'));
- assert(spy.calledWith('how '));
- assert(spy.calledWith('re you'));
- });
-
- it('allows setting of the delimiter with a buffer', () => {
- const spy = sinon.spy();
- const parser = new ReadlineParser({ delimiter: Buffer.from('a') });
- parser.on('data', spy);
- parser.write(Buffer.from('how are youa'));
- assert(spy.calledWith('how '));
- assert(spy.calledWith('re you'));
- });
-
- it('allows setting of the delimiter with an array of bytes', () => {
- const spy = sinon.spy();
- const parser = new ReadlineParser({ delimiter: [97] });
- parser.on('data', spy);
- parser.write(Buffer.from('how are youa'));
- assert(spy.calledWith('how '));
- assert(spy.calledWith('re you'));
- });
-
- it('allows setting of encoding', () => {
- const spy = sinon.spy();
- const parser = new ReadlineParser({
- encoding: 'hex'
- });
- parser.on('data', spy);
- parser.write(Buffer.from('a\nb\n'));
- assert.equal(spy.getCall(0).args[0], '61');
- assert.equal(spy.getCall(1).args[0], '62');
- });
-
- it('encoding should be reflected in a string delimiter', () => {
- const spy = sinon.spy();
- const parser = new ReadlineParser({
- delimiter: 'FF',
- encoding: 'hex'
- });
- parser.on('data', spy);
- parser.write(Buffer.from([0, 255, 1, 255]));
- assert.equal(spy.getCall(0).args[0], '00');
- assert.equal(spy.getCall(1).args[0], '01');
- });
-
- it('throws when called with a 0 length delimiter', () => {
- assert.throws(() => {
- new ReadlineParser({
- delimiter: Buffer.alloc(0)
- });
- });
-
- assert.throws(() => {
- new ReadlineParser({
- delimiter: ''
- });
- });
-
- assert.throws(() => {
- new ReadlineParser({
- delimiter: []
- });
- });
- });
-
- it('allows setting of the delimiter with a string', () => {
- new ReadlineParser({ delimiter: 'string' });
- });
-
- it('allows setting of the delimiter with a buffer', () => {
- new ReadlineParser({ delimiter: Buffer.from([1]) });
- });
-
- it('allows setting of the delimiter with an array of bytes', () => {
- new ReadlineParser({ delimiter: [1] });
- });
-
- it('doesn\'t emits empty data events', () => {
- const spy = sinon.spy();
- const parser = new ReadlineParser({ delimiter: 'a' });
- parser.on('data', spy);
- parser.write(Buffer.from('aFa'));
- assert(spy.calledOnce);
- assert(spy.calledWith('F'));
- });
-});
diff --git a/test/parser-ready.js b/test/parser-ready.js
deleted file mode 100644
index fcdf920..0000000
--- a/test/parser-ready.js
+++ /dev/null
@@ -1,99 +0,0 @@
-'use strict';
-/* eslint-disable no-new */
-
-const Buffer = require('safe-buffer').Buffer;
-const sinon = require('sinon');
-
-const ReadyParser = require('../lib/parsers/ready');
-
-describe('ReadyParser', () => {
- it('emits data received after the ready data', () => {
- const spy = sinon.spy();
- const parser = new ReadyParser({
- delimiter: Buffer.from('\n')
- });
- parser.on('data', spy);
- parser.write(Buffer.from('which will you get?'));
- parser.write(Buffer.from('garbage\ngold'));
- parser.write(Buffer.from('just for you'));
-
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('gold'));
- assert.deepEqual(spy.getCall(1).args[0], Buffer.from('just for you'));
- assert(spy.calledTwice);
- });
-
- it('emits the ready event before the data event', () => {
- const spy = sinon.spy();
- const parser = new ReadyParser({ delimiter: '!' });
- parser.on('ready', () => {
- parser.on('data', spy);
- });
- parser.write(Buffer.from('!hi'));
- assert(spy.calledOnce);
- });
-
- it('has a ready property', () => {
- const parser = new ReadyParser({
- delimiter: Buffer.from('\n')
- });
- parser.resume();
- assert.isFalse(parser.ready);
- parser.write(Buffer.from('not the new line'));
- assert.isFalse(parser.ready);
- parser.write(Buffer.from('this is the \n'));
- assert.isTrue(parser.ready);
- });
-
- it('throws when not provided with a delimiter', () => {
- assert.throws(() => {
- new ReadyParser({});
- });
- });
-
- it('throws when called with a 0 length delimiter', () => {
- assert.throws(() => {
- new ReadyParser({
- delimiter: Buffer.alloc(0)
- });
- });
-
- assert.throws(() => {
- new ReadyParser({
- delimiter: ''
- });
- });
-
- assert.throws(() => {
- new ReadyParser({
- delimiter: []
- });
- });
- });
-
- it('allows setting of the delimiter with a string', () => {
- new ReadyParser({ delimiter: 'string' });
- });
-
- it('allows setting of the delimiter with a buffer', () => {
- new ReadyParser({ delimiter: Buffer.from([1]) });
- });
-
- it('allows setting of the delimiter with an array of bytes', () => {
- new ReadyParser({ delimiter: [1] });
- });
-
- it('allows receiving the delimiter over small writes', () => {
- const spy = sinon.spy();
- const parser = new ReadyParser({
- delimiter: Buffer.from('READY')
- });
- parser.on('data', spy);
- parser.write(Buffer.from('bad data then REA'));
- parser.write(Buffer.from('D'));
- parser.write(Buffer.from('Y'));
- parser.write(Buffer.from('!!!!!!'));
-
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from('!!!!!!'));
- assert(spy.calledOnce);
- });
-});
diff --git a/test/parser-regex.js b/test/parser-regex.js
deleted file mode 100644
index 621fcb8..0000000
--- a/test/parser-regex.js
+++ /dev/null
@@ -1,120 +0,0 @@
-'use strict';
-/* eslint-disable no-new */
-
-const Buffer = require('safe-buffer').Buffer;
-const sinon = require('sinon');
-
-const RegexParser = require('../lib/parsers/regex');
-
-describe('RegexParser', () => {
- it('transforms data to strings split on either carriage return or new line', () => {
- const spy = sinon.spy();
- const parser = new RegexParser({
- regex: /[\r\n]+/
- });
- parser.on('data', spy);
- parser.write(Buffer.from('I love robots\r\nEach '));
- parser.write(Buffer.from('and Every One\r'));
- parser.write(Buffer.from('even you!'));
- parser.write(Buffer.from('\nThe angry red robot'));
-
- assert(spy.calledThrice, 'expecting 3 data events');
- assert.deepEqual(spy.getCall(0).args[0], 'I love robots');
- assert.deepEqual(spy.getCall(1).args[0], 'Each and Every One');
- assert.deepEqual(spy.getCall(2).args[0], 'even you!');
- });
-
- it('flushes remaining data when the stream ends', () => {
- const parser = new RegexParser({ regex: /\n/ });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from([1]));
- assert.equal(spy.callCount, 0);
- parser.end();
- assert.equal(spy.callCount, 1);
- assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1]).toString());
- });
-
- it('throws when not provided with a regex', () => {
- assert.throws(() => {
- new RegexParser({});
- });
- });
-
- it('throws when called with an invalid regex expression', () => {
- assert.throws(() => {
- new RegexParser({
- regex: '\\'
- });
- });
- });
-
- it('allows setting of the regex with a regex string', () => {
- const spy = sinon.spy();
- const parser = new RegexParser({ regex: 'a|b' });
- parser.on('data', spy);
- parser.write('bhow are youa');
- assert(spy.calledWith('how '));
- assert(spy.calledWith('re you'));
- });
-
- it('allows setting of the regex with a buffer', () => {
- const parser = new RegexParser({ regex: Buffer.from('a|b') });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write('bhow are youa');
- assert(spy.calledWith('how '));
- assert(spy.calledWith('re you'));
- });
-
- it('allows setting of encoding', () => {
- const spy = sinon.spy();
- const parser = new RegexParser({
- regex: /\r/,
- encoding: 'hex'
- });
- parser.on('data', spy);
- parser.write(Buffer.from('a\rb\r'));
- assert.equal(spy.getCall(0).args[0], '61');
- assert.equal(spy.getCall(1).args[0], '62');
- });
-
- it('Works when buffer starts with regex regex', () => {
- const data = Buffer.from('\rHello\rWorld\r');
- const parser = new RegexParser({ regex: /\r/ });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 2);
- });
-
- it('should match unicode in buffer string', () => {
- const data = Buffer.from('\u000aHello\u000aWorld\u000d\u000a!');
- const parser = new RegexParser({ regex: /\r\n|\n/ });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(data);
- assert.equal(spy.callCount, 2);
- });
-
- it('continues looking for regexs in the next buffers', () => {
- const parser = new RegexParser({ regex: /\r\n|\n/ });
- const spy = sinon.spy();
- parser.on('data', spy);
- parser.write(Buffer.from('This could be\na poem '));
- parser.write(Buffer.from('or prose\r\nsent from a robot\r\n'));
- assert.equal(spy.callCount, 3);
- assert.deepEqual(spy.getCall(0).args[0], 'This could be');
- assert.deepEqual(spy.getCall(1).args[0], 'a poem or prose');
- assert.deepEqual(spy.getCall(2).args[0], 'sent from a robot');
- });
-
- it('doesn\'t emits empty data events', () => {
- const spy = sinon.spy();
- const parser = new RegexParser({ regex: /a|b/ });
- parser.on('data', spy);
- parser.write(Buffer.from('abaFab'));
- assert(spy.calledOnce);
- assert(spy.calledWith('F'));
- });
-});