From f77c14230bf0860f581398425149f3345e70057f Mon Sep 17 00:00:00 2001 From: Eli Skeggs Date: Fri, 28 Mar 2014 09:58:07 -0700 Subject: [PATCH] Add typechecking to Socket#pack to avoid parsing undefined Fixes #124 provided we can make the tests work. Signed-off-by: Eli Skeggs --- lib/sockets/sock.js | 12 ++++++++++++ test/test.arg-types.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/sockets/sock.js b/lib/sockets/sock.js index b48982b..6ed9e53 100644 --- a/lib/sockets/sock.js +++ b/lib/sockets/sock.js @@ -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(); }; diff --git a/test/test.arg-types.js b/test/test.arg-types.js index 500fb1e..b11b4c5 100644 --- a/test/test.arg-types.js +++ b/test/test.arg-types.js @@ -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); +}, /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]); @@ -29,4 +40,4 @@ pull.on('message', function(a, b, c, d){ process.on('exit', function(){ assert(done); -}); \ No newline at end of file +});