Skip to content
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

Add typechecking to Socket#pack to avoid parsing undefined #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions lib/sockets/sock.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,24 @@ Socket.prototype.use = function(plugin){
/**
* Creates a new `Message` and write the `args`.
*
* Will throw if any of the provided `args` are `undefined`, given that
* JSON.stringify's result will be coerced to `'undefined'`, which JSON.parse
* cannot handle.
*
* @param {Array} args
* @return {Buffer}
* @api private
*/

Socket.prototype.pack = function(args){
var len = args.length;

for (var i = 0; i < len; i++) {
if (args[i] === undefined) {
throw new Error('cannot pack undefined');
}
}

var msg = new Message(args);
return msg.toBuffer();
};
Expand Down
13 changes: 12 additions & 1 deletion test/test.arg-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ var done;
push.bind(4000);
push.send('foo', { bar: 'baz' }, ['some', 1], new Buffer('hello'));

assert.throws(function() {
push.send(undefined);
}, /undefined/, 'send should reject unserializable types');
assert.throws(function() {
push.send(null, undefined);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this test, should probably ensure that push.send(null) still works.

}, /undefined/, 'send should reject unserializable types');
assert.throws(function() {
push.send({ bar: 'baz' }, undefined);
}, /undefined/, 'send should reject unserializable types');

pull.connect(4000);
pull.on('message', function(a, b, c, d){
assert(!done, 'message already received');
assert('string' == typeof a);
b.should.eql({ bar: 'baz' });
c.should.eql(['some', 1]);
Expand All @@ -29,4 +40,4 @@ pull.on('message', function(a, b, c, d){

process.on('exit', function(){
assert(done);
});
});