Skip to content

Commit

Permalink
add start timeout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychopoulet committed Sep 21, 2018
1 parent 1160ccb commit 560c4ab
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 24 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ type ControlBits = "none" | "end+1" | "end+2";
type Tag: number | Buffer | Array<number | Buffer>
```

* "startWith": Tag
* "endWith": Tag
* "escapeWith": Tag
* "escaped": Array<Tag>
* "specifics": object
* "controlBits": ControlBits (default: "none")
* ```javascript "startWith": Tag ```
* ```javascript "startTimeout": integer ``` (default: 200)
* ```javascript "endWith": Tag ```
* ```javascript "escapeWith": Tag ```
* ```javascript "escaped": Array<Tag> ```
* ```javascript "specifics": object ```
* ```javascript "controlBits": ControlBits ``` (default: "none")

> "specifics" is a [ key: string => value: Tag ] object which fire a "key" event when a "value" tag is found out of the message and not escaped
> "startTimeout" is a timeout (in milliseconds) which end frame in "start only" mode, if no second "start" bit is encountered after the first one

> "specifics" is a [ key: string => value: Tag ] object which fire a "key" event when a "value" tag is found out of the message and not escaped
> ex : { "specifics": { "nak": 0x25 } } will fire an "nak" event when 0x25 bit is encountered

## Examples
Expand Down
7 changes: 6 additions & 1 deletion lib/checkers/initData.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// consts

const ALLOWED_OPTIONS = [ "startWith", "endWith", "escapeWith", "escaped", "specifics", "controlBits" ];
const ALLOWED_OPTIONS = [ "startWith", "startTimeout", "endWith", "escapeWith", "escaped", "specifics", "controlBits" ];
const ALLOWED_CONTROL_BITS_OPTIONS = [ "none", "end+1", "end+2" ];

// module
Expand All @@ -22,6 +22,7 @@ module.exports = (options) => {

const result = {
"startWith": null,
"startTimeout": null,
"endWith": null,
"escapeWith": null,
"escaped": [],
Expand All @@ -47,6 +48,9 @@ module.exports = (options) => {
if ("undefined" !== typeof options.startWith && !_checkTagsValidity(options.startWith)) {
throw new TypeError("startWith parameter sended is not a number, Buffer, or an Array of numbers");
}
else if ("undefined" !== typeof options.startTimeout && "number" !== typeof options.startTimeout) {
throw new TypeError("startTimeout parameter sended is not a number");
}
else if ("undefined" !== typeof options.endWith && !_checkTagsValidity(options.endWith)) {
throw new TypeError("endWith parameter sended is not a number, Buffer, or an Array of numbers");
}
Expand All @@ -70,6 +74,7 @@ module.exports = (options) => {
else {

result.startWith = "undefined" !== typeof options.startWith ? options.startWith : null;
result.startTimeout = "undefined" !== typeof options.startTimeout ? options.startTimeout : 200;
result.endWith = "undefined" !== typeof options.endWith ? options.endWith : null;

result.escapeWith = "undefined" !== typeof options.escapeWith ? options.escapeWith : null;
Expand Down
17 changes: 17 additions & 0 deletions lib/digesters/digestStartOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

module.exports = function digestStartOnly () {

if (this._timeoutDigest) {
clearTimeout(this._timeoutDigest);
this._timeoutDigest = null;
}

const firstStart = this._searchFirstStart();

// no start tag detected
Expand All @@ -28,6 +33,18 @@ module.exports = function digestStartOnly () {
this._frame = 0 < firstStart.start ? Buffer.from(this._frame.slice(firstStart.start, this._frame.length)) : this._frame;
this._digesting = false;

this._timeoutDigest = setTimeout(() => {

clearTimeout(this._timeoutDigest);
this._timeoutDigest = null;

this.push(Buffer.from(this._frame.slice(firstStart.start, this._frame.length)));

this._frame = Buffer.from([]);
this._digesting = false;

});

}

// second start tag detected
Expand Down
15 changes: 4 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,10 @@ module.exports = class SplitFrames extends require("stream").Transform {
const endAt = startAt + ("object" === typeof this._startWith && this._startWith instanceof Buffer ? this._startWith.length : 1);

// start tag detected but without valid start bit length
if (endAt > this._frame.length) {
return null;
}
else {

return {
"start": startAt,
"end": endAt
};

}
return endAt > this._frame.length ? null : {
"start": startAt,
"end": endAt
};

}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "split-frames",
"version": "2.0.2",
"version": "2.1.0",
"description": "Split Buffer frames from streams",
"main": "lib/main.js",
"typings": "lib/index.d.ts",
Expand Down
18 changes: 14 additions & 4 deletions tests/0_parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ describe("parameters", () => {

});

it("should check with wrong start timeout tag", () => {

assert.throws(() => {
new SplitFrames({
"startTimeout": "test"
});
}, TypeError);

});

it("should check with wrong end tag", () => {

assert.throws(() => {
Expand All @@ -79,13 +89,13 @@ describe("parameters", () => {
new SplitFrames({
"escapeWith": "test"
});
}, Error);
}, TypeError);

assert.throws(() => {
new SplitFrames({
"escapeWith": [ "test" ]
});
}, Error);
}, TypeError);

});

Expand All @@ -95,7 +105,7 @@ describe("parameters", () => {
new SplitFrames({
"specifics": "test"
});
}, Error);
}, TypeError);

assert.throws(() => {
new SplitFrames({
Expand Down Expand Up @@ -199,7 +209,7 @@ describe("parameters", () => {
new SplitFrames({
"controlBits": false
});
}, Error);
}, TypeError);

});

Expand Down
24 changes: 24 additions & 0 deletions tests/2_startOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ describe("start only", () => {

});

it("should split frame with no second start bit", () => {

return new Promise((resolve, reject) => {

const splitter = new SplitFrames({
"startWith": STX,
"startTimeout": 200
}).once("error", reject).once("data", (chunk) => {

assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24 ]), "The chunk is not as expected");

resolve();

});

// tested frame
splitter.write(Buffer.from([ STX, 0x24 ]));

});

});

it("should split frame with one start", () => {

return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 560c4ab

Please sign in to comment.