Skip to content

Commit 6741900

Browse files
[fix] Handle undefined case properly when decoding packet (#74)
1 parent 5aecaa9 commit 6741900

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

lib/browser.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,11 @@ exports.encodeBase64Packet = function(packet, callback) {
222222
*/
223223

224224
exports.decodePacket = function (data, binaryType, utf8decode) {
225+
if (data === undefined) {
226+
return err;
227+
}
225228
// String data
226-
if (typeof data == 'string' || data === undefined) {
229+
if (typeof data == 'string') {
227230
if (data.charAt(0) == 'b') {
228231
return exports.decodeBase64Packet(data.substr(1), binaryType);
229232
}

lib/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ exports.encodeBase64Packet = function(packet, callback){
118118
*/
119119

120120
exports.decodePacket = function (data, binaryType, utf8decode) {
121+
if (data === undefined) {
122+
return err;
123+
}
121124
// String data
122-
if (typeof data == 'string' || data === undefined) {
125+
if (typeof data == 'string') {
123126
if (data.charAt(0) == 'b') {
124127
return exports.decodeBase64Packet(data.substr(1), binaryType);
125128
}

test/parser.js

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ module.exports = function(parser) {
129129
describe('decoding error handing', function () {
130130
var err = { type: 'error', data: 'parser error' };
131131

132+
it('should disallow empty payload', function () {
133+
expect(decode(undefined)).to.eql(err);
134+
});
135+
132136
it('should disallow bad format', function () {
133137
expect(decode(':::')).to.eql(err);
134138
});

0 commit comments

Comments
 (0)