Skip to content

Commit 6920a87

Browse files
committed
Merge pull request #13 from DullReferenceException/allow-non-paths
Accept certs/keys/cas as contents
2 parents 897ad3b + 86c62ac commit 6920a87

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,9 @@ module.exports = function createServers(options, listening) {
145145
//
146146
// Load default SSL key, cert and ca(s).
147147
//
148-
key: fs.readFileSync(path.resolve(ssl.root, ssl.key)),
149-
cert: fs.readFileSync(path.resolve(ssl.root, ssl.cert)),
150-
ca: ca && ca.map(
151-
function (file) {
152-
return fs.readFileSync(path.resolve(ssl.root, file));
153-
}
154-
),
148+
key: normalizeCertFile(ssl.root, ssl.key),
149+
cert: normalizeCertFile(ssl.root, ssl.cert),
150+
ca: ca && ca.map(normalizeCertFile.bind(null, ssl.root)),
155151
//
156152
// Properly expose ciphers for an A+ SSL rating:
157153
// https://certsimple.com/blog/a-plus-node-js-ssl
@@ -181,3 +177,13 @@ module.exports = function createServers(options, listening) {
181177
[createHttp, createHttps]
182178
.forEach(function (fn) { fn(); });
183179
};
180+
181+
var pemFormat = /-----BEGIN/;
182+
183+
function normalizeCertFile(root, file) {
184+
if (typeof(file) !== 'string' || pemFormat.test(file)) {
185+
return file; // Assumption that this is a Buffer, a PEM file, or something broken
186+
}
187+
188+
return fs.readFileSync(path.resolve(root, file));
189+
}

test/create-servers-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
var path = require('path'),
9+
fs = require('fs'),
910
http = require('http'),
1011
https = require('https'),
1112
test = require('tape'),
@@ -176,6 +177,26 @@ test('http && https with different handlers', function (t) {
176177
});
177178
});
178179

180+
test('supports cert contents instead of cert paths', function (t) {
181+
t.plan(3);
182+
var root = path.join(__dirname, 'fixtures');
183+
createServers({
184+
log: console.log,
185+
https: {
186+
port: 3456,
187+
root: root,
188+
cert: fs.readFileSync(path.resolve(root, 'agent2-cert.pem')),
189+
key: fs.readFileSync(path.resolve(root, 'agent2-key.pem'))
190+
},
191+
handler: fend
192+
}, function (err, servers) {
193+
t.error(err);
194+
t.equals(typeof servers, 'object');
195+
t.equals(typeof servers.https, 'object');
196+
servers.https.close();
197+
});
198+
});
199+
179200
test('supports requestCert https option', function (t) {
180201
t.plan(2);
181202
var spy = sinon.spy(https, 'createServer');

0 commit comments

Comments
 (0)