-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parsers): Add cctalk parsers (#1342)
* Added Working CCTalk Parser * add basic tests * compiled docs for cctalk parser
- Loading branch information
1 parent
a3b8d35
commit bcb492f
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
const Transform = require('stream').Transform; | ||
const Buffer = require('safe-buffer').Buffer; | ||
|
||
module.exports = 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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'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])); |