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

Fix to catch errors on spawn #38

Merged
merged 1 commit into from
Jan 18, 2015
Merged
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
added the code necessary to catch the error event from the spawn and …
…send it to the callback
  • Loading branch information
noamokman committed Jan 16, 2015
commit b2941ab0aa19058ae4d708de01e7389d1c4ee6f8
34 changes: 22 additions & 12 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,28 +534,38 @@ function spawnOpenSSL(params, callback) {
// making this fail periodically.
var needed = 2; // wait for both exit and close.
var code = -1;
var bothDone = function() {
if (code) {
callback(new Error("Invalid openssl exit code: " + code + "\n% openssl " + params.join(" ") + "\n" + stderr), code);
} else {
callback(null, code, stdout, stderr);
var finished = false;
var done = function (err) {
if (finished) {
return;
}

if (err) {
finished = true;
return callback(err);
}

if (--needed < 1) {
finished = true;
if (code) {
callback(new Error("Invalid openssl exit code: " + code + "\n% openssl " + params.join(" ") + "\n" + stderr), code);
} else {
callback(null, code, stdout, stderr);
}
}
};

openssl.on('error', done);

openssl.on('exit', function (ret) {
code = ret;
if (--needed < 1) {
bothDone();
}
done();
});

openssl.on('close', function () {
stdout = new Buffer(stdout, "binary").toString("utf-8");
stderr = new Buffer(stderr, "binary").toString("utf-8");

if (--needed < 1) {
bothDone();
}
done();
});
}

Expand Down