Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
€ý5i�����:q!

€ý5i€kd€kd€kd€kr€kr  €kb€kb€kb€kbuse €kb€kb€kb€kb€kbuse files i€kb€kb instead of /dev/stdin�ZZ:q!
  • Loading branch information
andris9 committed Apr 17, 2013
1 parent 3cd272a commit 52fdc25
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
language: node_js
node_js:
- 0.4
- 0.6
- 0.7
- 0.8
- 0.10

notifications:
email:
recipients:
- andris@node.ee
on_success: change
on_failure: change
on_failure: change
85 changes: 64 additions & 21 deletions lib/pem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
var spawn = require("child_process").spawn;
var spawn = require("child_process").spawn,
os = require("os"),
pathlib = require("path"),
fs = require("fs"),
crypto = require("crypto"),
tempDir = (os.tmpdir || os.tmpDir)();

module.exports.createPrivateKey = createPrivateKey;
module.exports.createCSR =createCSR;
Expand Down Expand Up @@ -431,28 +436,66 @@ function execOpenSSL(params, searchStr, stdin, callback){
stdin = false;
}

spawnOpenSSL(params, stdin, function(err, code, stdout, stderr) {
var start, end;
if (err) {
return callback(err);
}
var files = [];

if((start = stdout.match(new RegExp("\\-+BEGIN "+searchStr+"\\-+$", "m")))){
start = start.index;
}else{
start = -1;
}
if(stdin){
stdin = [].concat(stdin || []);
params.forEach(function(value, i){
var fname;
if(value == "/dev/stdin"){
fpath = pathlib.join(tempDir, crypto.randomBytes(20).toString("hex"));
files.push({
path: fpath,
contents: stdin.shift()
});
params[i] = fpath;
}
})
}

if((end = stdout.match(new RegExp("^\\-+END "+searchStr+"\\-+", "m")))){
end = end.index + (end[0] || "").length;
}else{
end = -1;
}
var processFiles = function(){
var file = files.shift();

if(start >= 0 && end >=0){
return callback(null, stdout.substring(start, end));
}else{
return callback(new Error(searchStr + " not found from openssl output:\n---stdout---\n" + stdout + "\n---stderr---\n" + stderr + "\ncode: " + code + "\nsignal: " + signal));
if(!file){
return spawnSSL();
}
});

fs.writeFile(file.path, file.contents, function(err, bytes){
processFiles();
});
}

var spawnSSL = function(){
spawnOpenSSL(params, false, function(err, code, stdout, stderr) {
var start, end;

files.forEach(function(file){
fs.unlink(file.path);
})

if (err) {
return callback(err);
}

if((start = stdout.match(new RegExp("\\-+BEGIN "+searchStr+"\\-+$", "m")))){
start = start.index;
}else{
start = -1;
}

if((end = stdout.match(new RegExp("^\\-+END "+searchStr+"\\-+", "m")))){
end = end.index + (end[0] || "").length;
}else{
end = -1;
}

if(start >= 0 && end >=0){
return callback(null, stdout.substring(start, end));
}else{
return callback(new Error(searchStr + " not found from openssl output:\n---stdout---\n" + stdout + "\n---stderr---\n" + stderr + "\ncode: " + code + "\nsignal: " + signal));
}
});
}

processFiles();
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Andris Reinman <andris@node.ee>",
"name": "pem",
"description": "Create private keys and certificates with node.js",
"version": "0.1.0",
"version": "0.2.0",
"repository": {
"type": "git",
"url": "git://github.com/andris9/pem.git"
Expand Down

0 comments on commit 52fdc25

Please sign in to comment.