Skip to content

Commit

Permalink
start develop multiple end/start tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychopoulet committed Feb 21, 2018
1 parent c2841a3 commit 583dc2c
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 10 deletions.
113 changes: 103 additions & 10 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@

"use strict";

// private

// methods

/**
* Test tag validity
* @param {number|Array} tag : tag checked
* @returns {bool} Tag alidity
*/
function _checkStartOrEndTag (tag) {

let result = true;

if (
"number" !== typeof tag && "object" !== typeof tag && !(tag instanceof Array || tag instanceof Buffer)
) {
result = false;
}
else if ("object" === typeof tag && tag instanceof Array) {

for (let i = 0; i < tag.length; ++i) {

if ("number" !== typeof tag[i]) {
result = false; break;
}

}

}

return result;

}

// module

module.exports = class SplitFrames extends require("stream").Transform {
Expand All @@ -23,6 +57,17 @@ module.exports = class SplitFrames extends require("stream").Transform {
this.escaped = [];

}

else if ("undefined" !== typeof options.start && !_checkStartOrEndTag(options.start)) {
throw new Error("start parameter sended is not a number, or an Array of numbers");
}
else if ("undefined" !== typeof options.end && !_checkStartOrEndTag(options.end)) {
throw new Error("end parameter sended is not a number, or an Array of numbers");
}
else if ("undefined" !== typeof options.escapeWith && "number" !== typeof options.escapeWith) {
throw new Error("escapeWith parameter sended is not a number");
}

else {

this.start = "undefined" !== typeof options.start ? options.start : null;
Expand All @@ -40,22 +85,70 @@ module.exports = class SplitFrames extends require("stream").Transform {
_searchTag (isThereEscape, tag, beginAt = 0) {

// if not concerned by escape
if (!isThereEscape || !this.escaped.includes(tag)) {
return this.frame.indexOf(tag, beginAt);
if (!isThereEscape) {

if ("object" === typeof tag && tag instanceof Array) {

// @TODO : different starters
// (0, console).log("not concerned by escape");
// (0, console).log(isThereEscape, tag, beginAt);

let foundAt = -1;

for (let i = 0; i < tag.length; ++i) {

foundAt = this._searchTag(isThereEscape, tag[i], beginAt);

if (-1 < foundAt) {
break;
}

}

return foundAt;

}
else {
return this.frame.indexOf(tag, beginAt);
}

}

// if concerned by escape
else {

let foundAt = this.frame.indexOf(tag, beginAt);
let foundAt = -1;

if ("number" === typeof tag) {

foundAt = this.frame.indexOf(tag, beginAt);

if (
// if not found, nothing to do
0 < foundAt &&
// if first one not escaped, nothing to do
this.frame[foundAt - 1] === this.escapeWith
) {
foundAt = this._searchTag(isThereEscape, tag, foundAt + 1);
}

}
else {

// @TODO : different starters
// (0, console).log("concerned by escape");
// (0, console).log(isThereEscape, tag, beginAt);

for (let i = 0; i < tag.length; ++i) {

foundAt = this._searchTag(isThereEscape, tag[i], beginAt);

if (-1 < foundAt) {
break;
}

}

if (
// if not found, nothing to do
0 < foundAt &&
// if first one not escaped, nothing to do
this.frame[foundAt - 1] === this.escapeWith
) {
foundAt = this._searchTag(isThereEscape, tag, foundAt + 1);
}

return foundAt;
Expand Down
10 changes: 10 additions & 0 deletions tests/0_withoutTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ describe("split", () => {

});

it("should split frame with wrong escap tag", () => {

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

});

it("should split frame with escaped data", () => {

return new Promise((resolve, reject) => {
Expand Down
112 changes: 112 additions & 0 deletions tests/1_startOnly.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
eslint no-new: 0
*/

"use strict";

// deps
Expand All @@ -8,6 +12,7 @@
// consts

const STX = 0x02;
// const STX2 = 0x82;
const DLE = 0x10;

// module
Expand All @@ -16,6 +21,26 @@ describe("split", () => {

describe("start only", () => {

it("should test wrong start", () => {

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

});

it("should test wrong start", () => {

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

});

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

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -210,6 +235,93 @@ describe("split", () => {

});

// @TODO : different starters

// it("should split frame with two start and different starters", () => {

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

// let dataCount = 0;

// new SplitFrames({
// "start": [ STX, STX2 ]
// }).on("error", reject).on("data", (chunk) => {

// ++dataCount;

// if (1 === dataCount) {

// 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([ 0x03, 0x04, 0x05, 0x06, 0x01 ]), "The chunk is not as expected");

// }
// else if (2 === dataCount) {

// 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([ 0x09, 0x10, 0x01, 0x02 ]), "The chunk is not as expected");

// resolve();

// }
// else {
// reject(new Error("Too much frames"));
// }

// // tested frame
// }).write(Buffer.from([ 0x01, STX, 0x03, 0x04, 0x05, 0x06, 0x01, STX2, 0x09, 0x10, 0x01, 0x02, STX ]));

// });

// });

// it("should split frame with two start and different starters and escaped data", () => {

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

// let dataCount = 0;

// new SplitFrames({
// "start": [ STX, STX2 ],
// "escapeWith": DLE,
// "escaped": [ DLE, STX, STX2 ]
// }).on("error", reject).on("data", (chunk) => {

// ++dataCount;

// if (1 === dataCount) {

// 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([ 0x01, STX, 0x04, 0x05, DLE, 0x06, 0x07, 0x08 ]), "The chunk is not as expected");

// }
// else if (2 === dataCount) {

// 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([ 0x01, STX, 0x04, 0x05, DLE, 0x06, 0x07, 0x08 ]), "The chunk is not as expected");

// resolve();

// }
// else {
// reject(new Error("Too much frames"));
// }

// // tested frame
// }).write(Buffer.from([
// 0x01,
// STX, 0x01, DLE, STX, 0x04, 0x05, DLE, DLE, 0x06, 0x07, 0x08,
// STX2, 0x01, DLE, STX, 0x04, 0x05, DLE, DLE, 0x06, 0x07, 0x08,
// STX
// ]));

// });

// });

});

});
24 changes: 24 additions & 0 deletions tests/2_endOnly.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
eslint no-new: 0
*/

"use strict";

// deps
Expand All @@ -16,6 +20,26 @@ describe("split", () => {

describe("end only", () => {

it("should test wrong end", () => {

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

});

it("should test wrong end", () => {

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

});

it("should split frame with no end", () => {

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

0 comments on commit 583dc2c

Please sign in to comment.