use tpl file, add .create(), minor cleanup

This commit is contained in:
AJ ONeal 2016-10-20 11:57:26 -06:00
parent aa7384b2f2
commit 78ad49a688
2 changed files with 52 additions and 28 deletions

View File

@ -6,18 +6,10 @@ var fs = require('fs')
, path = require('path')
, request = require('request')
, CERTDB_URL = 'https://mxr.mozilla.org/nss/source/lib/ckfw/builtins/certdata.txt?raw=1'
, HEADER
, outputFile
, outputPemsDir
;
HEADER =
"/**\n" +
" * Mozilla's root CA store\n" +
" *\n" +
" * generated from " + CERTDB_URL + "\n" +
" */\n\n";
function Certificate() {
this.name = null;
this.body = '';
@ -156,26 +148,9 @@ function dumpCerts(certs, filename, pemsDir) {
fs.writeFileSync(
filename
, HEADER
+ 'var cas = module.exports = [\n'
+ certs.map(function (cert) { return cert.quasiPEM().value; }).join(',\n\n')
+ '\n];\n'
+ "module.exports.rootCas = cas;\n"
+ "module.exports.inject = function () {\n"
+ " var opts = require('https').globalAgent.options;\n"
+ " if (!opts.ca || !opts.ca.__injected) { opts.ca = (opts.ca||[]).concat(cas); }\n"
+ " opts.ca.__injected = true;\n"
+ " return module.exports;\n"
+ "};\n"
+ "module.exports.addFile = function (filepath) {\n"
+ " var opts = require('https').globalAgent.options;\n"
+ " var root = filepath[0] === '/' ? '/' : '';\n"
+ " var filepaths = filepath.split(/\\//g);\n"
+ " if (root) { filepaths.unshift(root); }\n"
+ " opts.ca = opts.ca || [];\n"
+ " opts.ca.push(require('fs').readFileSync(require('path').join.apply(null, filepaths)));\n"
+ " return module.exports;\n"
+ "};\n"
, fs.readFileSync(path.join(__dirname, 'ssl-root-cas.tpl.js'), 'utf8')
.replace(/\/\*TPL\*\//, certs.map(function (cert) { return cert.quasiPEM().value; }).join(',\n\n'))
, 'utf8'
);
console.info("Wrote '" + filename.replace(/'/g, "\\'") + "'.");
}

49
ssl-root-cas.tpl.js Normal file
View File

@ -0,0 +1,49 @@
/**
* Mozilla's root CA store
*
* generated from https://mxr.mozilla.org/nss/source/lib/ckfw/builtins/certdata.txt?raw=1
*/
'use strict';
var originalCas = [
/*TPL*/
];
module.exports.rootCas = module.exports = originalCas.slice(0);
module.exports.rootCas.inject = function (/*context*/) {
var rootCas = this || module.exports.rootCas;
var opts = /*context ||*/ require('https').globalAgent.options;
if (!opts.ca || !opts.ca.__injected) { opts.ca = (opts.ca||[]).concat(rootCas); }
opts.ca.__injected = true;
return module.exports;
};
module.exports.rootCas.addFile = function (filepath) {
// BEGIN TODO
// What is this filepath stuff all about?
// (maybe be a leftover MS Windows hack ??)
// Can we get rid of it?
var path = require('path');
var root = (filepath[0] === '/' ? '/' : '');
var filepaths = filepath.split(/\//g);
if (root) { filepaths.unshift(root); }
filepath = path.join.apply(null, filepaths);
// END TODO
var httpsOpts = require('https').globalAgent.options;
var rootCas = this || module.exports.rootCas;
var buf = require('fs').readFileSync(filepath);
rootCas.push(buf);
// backwards compat
if (rootCas !== httpsOpts.ca) {
httpsOpts.ca = httpsOpts.ca || [];
httpsOpts.ca.push(buf);
}
return module.exports;
};
module.exports.create = function () {
var rootCas = originalCas.slice(0);
rootCas.inject = module.exports.inject;
rootCas.addFile = module.exports.addFile;
return rootCas;
};