Browse files

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.
  • Loading branch information...
1 parent 4ca2862 commit 8176f9fb514c5abb69d3eeb44cfb5aee460323c8 @medikoo medikoo committed with Oct 20, 2011
Showing with 8 additions and 3 deletions.
  1. +8 −3 lib/forever.js
View
11 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();
});

0 comments on commit 8176f9f

Please sign in to comment.