Skip to content

Commit

Permalink
Make sure all data is streamed before we try to parse it.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
medikoo authored and indexzero committed Oct 22, 2011
1 parent 4ca2862 commit 8176f9f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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();
});
Expand Down

0 comments on commit 8176f9f

Please sign in to comment.