From ebf92a5097b04070ed30d8017b9da980a5865074 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 21 Dec 2016 09:31:14 +0100 Subject: [PATCH] [style] Minor style changes (#83) --- lib/browser.js | 53 +++++++++++++++++++++++------------------------- lib/index.js | 55 ++++++++++++++++++++++++-------------------------- 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index 6df90aadc1..0432ea431c 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -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 != '') { @@ -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; @@ -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); diff --git a/lib/index.js b/lib/index.js index 35d9100b6b..ef7647f1a1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 != '') { @@ -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; @@ -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; @@ -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);