Skip to content

Catch JSON.stringfy exceptions instead of silent failing #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
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
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,31 @@ function Encoder() {}
*/

Encoder.prototype.encode = function(obj, callback){
if ((obj.type === exports.EVENT || obj.type === exports.ACK) && hasBin(obj.data)) {
obj.type = obj.type === exports.EVENT ? exports.BINARY_EVENT : exports.BINARY_ACK;
}

debug('encoding packet %j', obj);
if (obj.type === exports.EVENT || obj.type === exports.ACK) {
/**
* For object data with circular references, hasBin will throw
* exceptions, which must be handled or it could hang the channel.
* And the circular references only happen to non-Binary data,
* so we will invoke callback with an array.
*/
try {
if (hasBin(obj.data)) {
obj.type = obj.type === exports.EVENT ? exports.BINARY_EVENT : exports.BINARY_ACK;
}
} catch (e) {
callback([encodeError()]);
}
}

debug('encoding packet %j', obj);

if (exports.BINARY_EVENT === obj.type || exports.BINARY_ACK === obj.type) {
encodeAsBinary(obj, callback);
}
else {
} else {
var encoding = encodeAsString(obj);
callback([encoding]);
}
}
};

/**
Expand Down Expand Up @@ -170,7 +182,11 @@ function encodeAsString(obj) {

// json data
if (null != obj.data) {
str += JSON.stringify(obj.data);
try {
str += JSON.stringify(obj.data);
} catch (e) {
return encodeError();
}
}

debug('encoded %j as %s', obj, str);
Expand Down Expand Up @@ -398,3 +414,7 @@ function error() {
data: 'parser error'
};
}

function encodeError() {
return exports.ERROR + JSON.stringify('encode error');
}
24 changes: 22 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 @@ -51,6 +49,28 @@ describe('parser', function(){
});
});

it('encodes a circular object (return error)', function() {
var john = new Object();
var mary = new Object();

john.sister = mary;
mary.brother = john;

var data = {
type: parser.EVENT,
data: john,
id: 1,
nsp: '/'
}

var error = parser.ERROR + JSON.stringify('encode error');
var encoder = new parser.Encoder();

encoder.encode(data, function(encodedPackets) {
expect(encodedPackets[0]).to.be(error);
});
});

it('decodes a bad binary packet', function(){
try {
var decoder = new parser.Decoder();
Expand Down