Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ function decodeString(str) {
type: Number(str.charAt(0))
};

if (null == exports.types[p.type]) return error();
if (exports.types[p.type] === null) {
throw new Error('unknown packet type');
}

// look up attachments if type binary
if (exports.BINARY_EVENT === p.type || exports.BINARY_ACK === p.type) {
Expand Down Expand Up @@ -318,22 +320,13 @@ function decodeString(str) {

// look up json data
if (str.charAt(++i)) {
p = tryParse(p, str.substr(i));
p.data = JSON.parse(str.substr(i));
}

debug('decoded %s as %j', str, p);
return p;
}

function tryParse(p, str) {
try {
p.data = JSON.parse(str);
} catch(e){
return error();
}
return p;
}

/**
* Deallocates a parser's resources
*
Expand Down Expand Up @@ -391,10 +384,3 @@ BinaryReconstructor.prototype.finishedReconstruction = function() {
this.reconPack = null;
this.buffers = [];
};

function error() {
return {
type: exports.ERROR,
data: 'parser error'
};
}
11 changes: 9 additions & 2 deletions test/parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var parser = require('../index.js');
var expect = require('expect.js');
var helpers = require('./helpers.js');
var encode = parser.encode;
var decode = parser.decode;

describe('parser', function(){

Expand Down Expand Up @@ -59,4 +57,13 @@ describe('parser', function(){
expect(e.message).to.match(/Illegal/);
}
});

it('should throw when decoding an invalid packet', function(){
try {
var decoder = new parser.Decoder();
decoder.add('442["some","data","that","breaks","stuff"]');
} catch (e) {
expect(e.message).to.match(/Illegal/);
}
});
});