Skip to content

Commit

Permalink
[style] Minor style changes (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne authored Dec 21, 2016
1 parent 181acef commit ebf92a5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 57 deletions.
53 changes: 25 additions & 28 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,43 +385,43 @@ exports.decodePayload = function (data, binaryType, callback) {
return callback(err, 0, 1);
}

var length = ''
, n, msg;
var length = '', n, msg;

for (var i = 0, l = data.length; i < l; i++) {
var chr = data.charAt(i);

if (':' != chr) {
length += chr;
} else {
if ('' == length || (length != (n = Number(length)))) {
// parser error - ignoring payload
return callback(err, 0, 1);
}
continue;
}

msg = data.substr(i + 1, n);
if ('' == length || (length != (n = Number(length)))) {
// parser error - ignoring payload
return callback(err, 0, 1);
}

if (length != msg.length) {
// parser error - ignoring payload
return callback(err, 0, 1);
}
msg = data.substr(i + 1, n);

if (msg.length) {
packet = exports.decodePacket(msg, binaryType, false);
if (length != msg.length) {
// parser error - ignoring payload
return callback(err, 0, 1);
}

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
return callback(err, 0, 1);
}
if (msg.length) {
packet = exports.decodePacket(msg, binaryType, false);

var ret = callback(packet, i + n, l);
if (false === ret) return;
if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
return callback(err, 0, 1);
}

// advance cursor
i += n;
length = '';
var ret = callback(packet, i + n, l);
if (false === ret) return;
}

// advance cursor
i += n;
length = '';
}

if (length != '') {
Expand Down Expand Up @@ -562,7 +562,6 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
var bufferTail = data;
var buffers = [];

var numberTooLong = false;
while (bufferTail.byteLength > 0) {
var tailArray = new Uint8Array(bufferTail);
var isString = tailArray[0] === 0;
Expand All @@ -571,16 +570,14 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
for (var i = 1; ; i++) {
if (tailArray[i] == 255) break;

// 310 = char length of Number.MAX_VALUE
if (msgLength.length > 310) {
numberTooLong = true;
break;
return callback(err, 0, 1);
}

msgLength += tailArray[i];
}

if(numberTooLong) return callback(err, 0, 1);

bufferTail = sliceBuffer(bufferTail, 2 + msgLength.length);
msgLength = parseInt(msgLength);

Expand Down
55 changes: 26 additions & 29 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,43 +280,43 @@ exports.decodePayload = function (data, binaryType, callback) {
return callback(err, 0, 1);
}

var length = ''
, n, msg;
var length = '', n, msg;

for (var i = 0, l = data.length; i < l; i++) {
var chr = data.charAt(i);

if (':' != chr) {
length += chr;
} else {
if ('' == length || (length != (n = Number(length)))) {
// parser error - ignoring payload
return callback(err, 0, 1);
}
continue;
}

msg = data.substr(i + 1, n);
if ('' == length || (length != (n = Number(length)))) {
// parser error - ignoring payload
return callback(err, 0, 1);
}

if (length != msg.length) {
// parser error - ignoring payload
return callback(err, 0, 1);
}
msg = data.substr(i + 1, n);

if (msg.length) {
packet = exports.decodePacket(msg, binaryType, false);
if (length != msg.length) {
// parser error - ignoring payload
return callback(err, 0, 1);
}

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
return callback(err, 0, 1);
}
if (msg.length) {
packet = exports.decodePacket(msg, binaryType, false);

var ret = callback(packet, i + n, l);
if (false === ret) return;
if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
return callback(err, 0, 1);
}

// advance cursor
i += n;
length = '';
var ret = callback(packet, i + n, l);
if (false === ret) return;
}

// advance cursor
i += n;
length = '';
}

if (length != '') {
Expand All @@ -335,7 +335,7 @@ exports.decodePayload = function (data, binaryType, callback) {

function bufferToString(buffer) {
var str = '';
for (var i = 0; i < buffer.length; i++) {
for (var i = 0, l = buffer.length; i < l; i++) {
str += String.fromCharCode(buffer[i]);
}
return str;
Expand All @@ -350,7 +350,7 @@ function bufferToString(buffer) {

function stringToBuffer(string) {
var buf = new Buffer(string.length);
for (var i = 0; i < string.length; i++) {
for (var i = 0, l = string.length; i < l; i++) {
buf.writeUInt8(string.charCodeAt(i), i);
}
return buf;
Expand Down Expand Up @@ -446,17 +446,14 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
while (bufferTail.length > 0) {
var strLen = '';
var isString = bufferTail[0] === 0;
var numberTooLong = false;
for (var i = 1; ; i++) {
if (bufferTail[i] == 255) break;
// 310 = char length of Number.MAX_VALUE
if (strLen.length > 310) {
numberTooLong = true;
break;
return callback(err, 0, 1);
}
strLen += '' + bufferTail[i];
}
if(numberTooLong) return callback(err, 0, 1);
bufferTail = bufferTail.slice(strLen.length + 1);

var msgLength = parseInt(strLen, 10);
Expand Down

0 comments on commit ebf92a5

Please # to comment.