pem
Advanced tools
Comparing version 1.4.6 to 1.5.0
106
lib/pem.js
@@ -9,2 +9,3 @@ 'use strict'; | ||
crypto = require('crypto'), | ||
which = require('which'), | ||
pathOpenSSL, | ||
@@ -74,4 +75,2 @@ tempDir = process.env.PEMJS_TMPDIR || (os.tmpdir || os.tmpDir) && (os.tmpdir || os.tmpDir)() || '/tmp'; | ||
*/ | ||
function createCSR(options, callback) { | ||
@@ -541,51 +540,58 @@ if (!callback && typeof options === 'function') { | ||
var pathBin = pathOpenSSL || process.env.OPENSSL_BIN || 'openssl'; | ||
var openssl = spawn(pathBin, params), | ||
stdout = '', | ||
stderr = ''; | ||
openssl.stdout.on('data', function(data) { | ||
stdout += (data || '').toString('binary'); | ||
}); | ||
openssl.stderr.on('data', function(data) { | ||
stderr += (data || '').toString('binary'); | ||
}); | ||
// We need both the return code and access to all of stdout. Stdout isn't | ||
// *really* available until the close event fires; the timing nuance was | ||
// making this fail periodically. | ||
var needed = 2; // wait for both exit and close. | ||
var code = -1; | ||
var finished = false; | ||
var done = function(err) { | ||
if (finished) { | ||
return; | ||
} | ||
testOpenSSLPath(pathBin, function(err) { | ||
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); | ||
var openssl = spawn(pathBin, params), | ||
stdout = '', | ||
stderr = ''; | ||
openssl.stdout.on('data', function(data) { | ||
stdout += (data || '').toString('binary'); | ||
}); | ||
openssl.stderr.on('data', function(data) { | ||
stderr += (data || '').toString('binary'); | ||
}); | ||
// We need both the return code and access to all of stdout. Stdout isn't | ||
// *really* available until the close event fires; the timing nuance was | ||
// making this fail periodically. | ||
var needed = 2; // wait for both exit and close. | ||
var code = -1; | ||
var finished = false; | ||
var done = function(err) { | ||
if (finished) { | ||
return; | ||
} | ||
} | ||
}; | ||
openssl.on('error', done); | ||
if (err) { | ||
finished = true; | ||
return callback(err); | ||
} | ||
openssl.on('exit', function(ret) { | ||
code = ret; | ||
done(); | ||
}); | ||
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('close', function() { | ||
stdout = new Buffer(stdout, 'binary').toString('utf-8'); | ||
stderr = new Buffer(stderr, 'binary').toString('utf-8'); | ||
done(); | ||
openssl.on('error', done); | ||
openssl.on('exit', function(ret) { | ||
code = ret; | ||
done(); | ||
}); | ||
openssl.on('close', function() { | ||
stdout = new Buffer(stdout, 'binary').toString('utf-8'); | ||
stderr = new Buffer(stderr, 'binary').toString('utf-8'); | ||
done(); | ||
}); | ||
}); | ||
@@ -673,2 +679,18 @@ } | ||
}); | ||
} | ||
/** | ||
* Validates the pathBin for the openssl command. | ||
* | ||
* @param {String} pathBin The path to OpenSSL Bin | ||
* @param {Function} callback Callback function with an error object | ||
*/ | ||
function testOpenSSLPath(pathBin, callback) { | ||
which(pathBin, function(error) { | ||
if (error) { | ||
return callback(new Error('Could not find openssl on your system on this path: ' + pathBin)); | ||
} | ||
callback(); | ||
}); | ||
} |
{ | ||
"author": "Andris Reinman <andris@node.ee>", | ||
"author": "Andris Reinman <andris@kreata.ee>", | ||
"name": "pem", | ||
"description": "Create private keys and certificates with node.js", | ||
"version": "1.4.6", | ||
"version": "1.5.0", | ||
"repository": { | ||
@@ -14,3 +14,5 @@ "type": "git", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"which": "~1.0.8" | ||
}, | ||
"devDependencies": { | ||
@@ -17,0 +19,0 @@ "nodeunit": "*" |
@@ -25,3 +25,3 @@ pem | ||
https.createServer({key: keys.serviceKey, cert: keys.certificate}, function(req, res){ | ||
res.end("o hai!") | ||
res.end('o hai!') | ||
}).listen(443); | ||
@@ -41,3 +41,3 @@ }); | ||
app.get('/', requireAuth, function(req, res){ | ||
res.send("o hai!"); | ||
res.send('o hai!'); | ||
}); | ||
@@ -156,4 +156,17 @@ | ||
### Setting openssl location | ||
In some systems the `openssl` executable might not be available by the default name or it is not included in $PATH. In this case you can define the location of the executable yourself as a one time action after you have loaded the pem module: | ||
```javascript | ||
var pem = require('pem'); | ||
pem.config({ | ||
pathOpenSSL: '/usr/local/bin/openssl' | ||
}); | ||
... | ||
// do something with the pem module | ||
``` | ||
## License | ||
**MIT** |
@@ -341,3 +341,19 @@ 'use strict'; | ||
}); | ||
}, | ||
'Return an error if openssl was not found': function(test) { | ||
pem.config({ | ||
pathOpenSSL: 'zzzzzzzzzzz' | ||
}); | ||
pem.createPrivateKey(function(error) { | ||
test.ok(error); | ||
pem.config({ | ||
pathOpenSSL: 'openssl' | ||
}); | ||
pem.createPrivateKey(function(error) { | ||
test.ifError(error); | ||
test.done(); | ||
}); | ||
}); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
41681
915
169
1
+ Addedwhich@~1.0.8
+ Addedwhich@1.0.9(transitive)