From 6df7bdd954efb817b50dafad7f90a1285c59c0c9 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 18 Dec 2011 01:26:00 +0100 Subject: [PATCH] child_process: make .send() throw if message is undefined JSON.stringify(undefined) returns "undefined" but JSON.parse() doesn't know how to parse that. --- lib/child_process.js | 6 +++++- test/simple/test-child-process-fork.js | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/child_process.js b/lib/child_process.js index a34313f61ed2..55412b949499 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -111,7 +111,11 @@ function setupChannel(target, channel) { }; target.send = function(message, sendHandle) { - if (!target._channel) throw new Error('channel closed'); + if (typeof message === 'undefined') { + throw new TypeError('message cannot be undefined'); + } + + if (!target._channel) throw new Error("channel closed"); // For overflow protection don't write if channel queue is too deep. if (channel.writeQueueSize > 1024 * 1024) { diff --git a/test/simple/test-child-process-fork.js b/test/simple/test-child-process-fork.js index 4860732b71f2..41cc28c72e40 100644 --- a/test/simple/test-child-process-fork.js +++ b/test/simple/test-child-process-fork.js @@ -35,6 +35,11 @@ n.on('message', function(m) { messageCount++; }); +// https://github.com/joyent/node/issues/2355 - JSON.stringify(undefined) +// returns "undefined" but JSON.parse() cannot parse that... +assert.throws(function() { n.send(undefined); }, TypeError); +assert.throws(function() { n.send(); }, TypeError); + n.send({ hello: 'world' }); var childExitCode = -1;