Skip to content

Commit

Permalink
Merge pull request #6 from Psychopoulet/develop
Browse files Browse the repository at this point in the history
remove debug
  • Loading branch information
Psychopoulet committed Feb 21, 2018
2 parents 6a984f8 + 045c54b commit ce24601
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 76 deletions.
75 changes: 0 additions & 75 deletions lib/main.js
Expand Up @@ -4,21 +4,6 @@

"use strict";

// consts

const DEBUG = false;

// private

/**
* Print debug data in console if debug mode
* @param {Array} msg : data to print
* @returns {void}
*/
function _debug (...msg) {
return DEBUG ? (0, console).log(...msg) : null;
}

// module

module.exports = class SplitFrames extends require("stream").Transform {
Expand Down Expand Up @@ -50,14 +35,10 @@ module.exports = class SplitFrames extends require("stream").Transform {
this.frame = Buffer.from([]);
this.digesting = false;

_debug("constructor", "start", this.start, "end", this.end, "escapeWith", this.escapeWith, "escaped", this.escaped);

}

_searchTag (isThereEscape, tag, beginAt = 0) {

_debug("_searchTag", isThereEscape, tag, beginAt);

// if not concerned by escape
if (!isThereEscape || !this.escaped.includes(tag)) {
return this.frame.indexOf(tag, beginAt);
Expand All @@ -68,11 +49,6 @@ module.exports = class SplitFrames extends require("stream").Transform {

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

_debug(
foundAt, 0 < foundAt,
this.frame[foundAt - 1] === this.escapeWith
);

if (
// if not found, nothing to do
0 < foundAt &&
Expand All @@ -82,8 +58,6 @@ module.exports = class SplitFrames extends require("stream").Transform {
foundAt = this._searchTag(isThereEscape, tag, foundAt + 1);
}

_debug("result", foundAt);

return foundAt;

}
Expand All @@ -92,8 +66,6 @@ module.exports = class SplitFrames extends require("stream").Transform {

_removeEscapeTag (msg) {

_debug("_removeEscapeTag");

const content = [];

for (let i = 0; i < msg.length; ++i) {
Expand All @@ -115,16 +87,12 @@ module.exports = class SplitFrames extends require("stream").Transform {

}

_debug(Buffer.from(content));

return Buffer.from(content);

}

_digestNoStartNoEnd (isThereEscape) {

_debug("neither start or end", isThereEscape);

this.push(
isThereEscape ?
this._removeEscapeTag(this.frame.slice(0, this.frame.length)) :
Expand All @@ -139,15 +107,11 @@ module.exports = class SplitFrames extends require("stream").Transform {

_digestStartOnly (isThereEscape) {

_debug("only start", isThereEscape);

const firstStartAt = this._searchTag(isThereEscape, this.start);

// no start tag detected
if (-1 >= firstStartAt) {

_debug("no start tag detected");

this.frame = Buffer.from([]);
this.digesting = false;

Expand All @@ -156,15 +120,11 @@ module.exports = class SplitFrames extends require("stream").Transform {
// first start tag detected
else {

_debug("first start tag detected at", firstStartAt);

let nextStartAt = this._searchTag(isThereEscape, this.start, firstStartAt + 1);

// second start tag not detected
if (-1 >= nextStartAt) {

_debug("second start tag not detected");

// remove useless bits
if (0 < firstStartAt) {
this.frame = Buffer.from(this.frame.slice(firstStartAt, this.frame.length));
Expand All @@ -177,8 +137,6 @@ module.exports = class SplitFrames extends require("stream").Transform {
// second start tag detected
else {

_debug("second start tag detected at", nextStartAt);

const startSize = "object" === typeof this.start && this.start instanceof Buffer ? this.start.length : 1;

this.push(
Expand All @@ -193,11 +151,7 @@ module.exports = class SplitFrames extends require("stream").Transform {

// second full frame detected
if (-1 < nextStartAt) {

_debug("second full frame detected");

this._digest();

}
else {
this.digesting = false;
Expand All @@ -211,23 +165,16 @@ module.exports = class SplitFrames extends require("stream").Transform {

_digestEndOnly (isThereEscape) {

_debug("only end", isThereEscape);

const firstEndAt = this._searchTag(isThereEscape, this.end);

// no end tag detected
if (-1 >= firstEndAt) {

_debug("no end tag detected");
this.digesting = false;

}

// first end tag detected
else {

_debug("first end tag detected at", firstEndAt);

const endSize = "object" === typeof this.end && this.end instanceof Buffer ? this.end.length : 1;

this.push(
Expand All @@ -242,11 +189,7 @@ module.exports = class SplitFrames extends require("stream").Transform {

// second full frame detected
if (-1 < nextEndAt) {

_debug("second full frame detected");

this._digest();

}
else {
this.digesting = false;
Expand All @@ -258,15 +201,11 @@ module.exports = class SplitFrames extends require("stream").Transform {

_digestStartAndEndOnly (isThereEscape) {

_debug("start & end", isThereEscape);

const startAt = this._searchTag(isThereEscape, this.start);

// no start tag detected
if (-1 >= startAt) {

_debug("no start tag detected");

this.frame = Buffer.from([]);
this.digesting = false;

Expand All @@ -275,15 +214,11 @@ module.exports = class SplitFrames extends require("stream").Transform {
// start tag detected
else {

_debug("start tag detected at", startAt);

const endAt = this._searchTag(isThereEscape, this.end, startAt + 1);

// end tag not detected
if (-1 >= endAt) {

_debug("end tag not detected");

// remove useless bits
if (0 < startAt) {
this.frame = Buffer.from(this.frame.slice(startAt, this.frame.length));
Expand All @@ -296,8 +231,6 @@ module.exports = class SplitFrames extends require("stream").Transform {
// end tag detected
else {

_debug("end tag detected at", endAt);

const startSize = "object" === typeof this.start && this.start instanceof Buffer ? this.start.length : 1;
const endSize = "object" === typeof this.end && this.end instanceof Buffer ? this.end.length : 1;

Expand All @@ -313,11 +246,7 @@ module.exports = class SplitFrames extends require("stream").Transform {

// start frame detected
if (-1 < nextStartAt) {

_debug("second full frame detected");

this._digest();

}
else {
this.digesting = false;
Expand All @@ -340,8 +269,6 @@ module.exports = class SplitFrames extends require("stream").Transform {

try {

_debug("_digest", this.frame);

if (null === this.start && null === this.end) {
this._digestNoStartNoEnd("undefined" !== typeof this.escapeWith && null !== this.escapeWith);
}
Expand All @@ -366,8 +293,6 @@ module.exports = class SplitFrames extends require("stream").Transform {

_transform (chunk, enc, cb) {

_debug("_transform", chunk);

this.frame = chunk.length ? Buffer.concat([ this.frame, chunk ]) : this.frame;

if (!this.digesting) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "split-frames",
"version": "0.2.1",
"version": "0.2.2",
"description": "Split Buffer frames from streams",
"main": "lib/main.js",
"scripts": {
Expand Down

0 comments on commit ce24601

Please sign in to comment.