From 8176f9fb514c5abb69d3eeb44cfb5aee460323c8 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 20 Oct 2011 15:33:37 +0200 Subject: [PATCH] Make sure all data is streamed before we try to parse it. It may happen that JSON string is split into more than one packet, in such case script was trying to parse just portion of received data and failed with parse error. --- lib/forever.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/forever.js b/lib/forever.js index a8d34f1d..6a368edb 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -93,7 +93,8 @@ function getAllProcesses(callback) { function getProcess(name, next) { var fullPath = path.join(sockPath, name), - socket = new net.Socket({ type: 'unix' }); + socket = new net.Socket({ type: 'unix' }), + data = ''; socket.on('error', function (err) { if (err.code === 'ECONNREFUSED') { @@ -113,8 +114,12 @@ function getAllProcesses(callback) { next(); }); - socket.on('data', function (data) { - var monitors = JSON.parse(data.toString()); + socket.on('data', function (msg) { + data += msg.toString(); + }); + + socket.on('close', function () { + var monitors = JSON.parse(data); results.push.apply(results, monitors.monitors); next(); });