Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle malformed output from child process #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions lib/Local.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,24 @@ function Local(){
const obj = childProcess.spawnSync(that.binaryPath, that.getBinaryArgs());
this.tunnel = {pid: obj.pid};
var data = {};
if(obj.stdout.length > 0)
data = JSON.parse(obj.stdout);
else if(obj.stderr.length > 0)
data = JSON.parse(obj.stderr);
else
if(obj.stdout.length > 0){
try{
data = JSON.parse(obj.stdout);
}catch(error){
data = obj.stdout;
}
} else if(obj.stderr.length > 0){
try{
data = JSON.parse(obj.stderr);
}catch(error){
data = obj.stderr;
}
} else {
return new LocalError('No output received');
}
if(typeof data === 'string'){
return new LocalError('Malformed output received');
}
if(data['state'] != 'connected'){
return new LocalError(data['message']['message']);
} else {
Expand Down Expand Up @@ -93,21 +105,31 @@ function Local(){
that.retriesLeft -= 1;
fs.unlinkSync(that.binaryPath);
delete(that.binaryPath);
that.start(options, callback);
return;
return that.start(options, callback);
} else {
callback(new LocalError(error.toString()));
return callback(new LocalError(error.toString()));
}
}

var data = {};
if(stdout)
data = JSON.parse(stdout);
else if(stderr)
data = JSON.parse(stderr);
else
callback(new LocalError('No output received'));

if(stdout){
try{
data = JSON.parse(stdout);
}catch(error){
data = stdout;
}
} else if(stderr){
try{
data = JSON.parse(stderr);
}catch(error){
data = stderr;
}
} else {
return callback(new LocalError('No output received'));
}
if(typeof data === 'string'){
return callback(new LocalError('Malformed output received'));
}
if(data['state'] != 'connected'){
callback(new LocalError(data['message']['message']));
} else {
Expand All @@ -126,7 +148,7 @@ function Local(){
this.stop = function (callback) {
if(!this.pid) return callback();
this.killAllProcesses(function(error){
if(error) callback(new LocalError(error.toString()));
if(error) return callback(new LocalError(error.toString()));
callback();
});
};
Expand Down Expand Up @@ -316,6 +338,7 @@ function Local(){

this.killAllProcesses = function(callback){
psTree(this.pid, (err, children) => {
if(err) return callback(err);
var childPids = children.map(val => val.PID);
var killChecker = setInterval(() => {
if(childPids.length === 0) {
Expand Down