From 72be1dbc4024516f6cb93bac2989118d19da862a Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 20 Oct 2016 14:32:51 +0200 Subject: [PATCH] [fix] Handle undefined case properly when decoding packet --- lib/browser.js | 5 ++++- lib/index.js | 5 ++++- test/parser.js | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index e7debfb..652dc0c 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -222,8 +222,11 @@ exports.encodeBase64Packet = function(packet, callback) { */ exports.decodePacket = function (data, binaryType, utf8decode) { + if (data === undefined) { + return err; + } // String data - if (typeof data == 'string' || data === undefined) { + if (typeof data == 'string') { if (data.charAt(0) == 'b') { return exports.decodeBase64Packet(data.substr(1), binaryType); } diff --git a/lib/index.js b/lib/index.js index e9dafd9..716b1d5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -118,8 +118,11 @@ exports.encodeBase64Packet = function(packet, callback){ */ exports.decodePacket = function (data, binaryType, utf8decode) { + if (data === undefined) { + return err; + } // String data - if (typeof data == 'string' || data === undefined) { + if (typeof data == 'string') { if (data.charAt(0) == 'b') { return exports.decodeBase64Packet(data.substr(1), binaryType); } diff --git a/test/parser.js b/test/parser.js index da852d8..9a19394 100644 --- a/test/parser.js +++ b/test/parser.js @@ -129,6 +129,10 @@ module.exports = function(parser) { describe('decoding error handing', function () { var err = { type: 'error', data: 'parser error' }; + it('should disallow empty payload', function () { + expect(decode(undefined)).to.eql(err); + }); + it('should disallow bad format', function () { expect(decode(':::')).to.eql(err); });