use tpl file, add .create(), minor cleanup
This commit is contained in:
parent
aa7384b2f2
commit
78ad49a688
|
@ -6,18 +6,10 @@ var fs = require('fs')
|
||||||
, path = require('path')
|
, path = require('path')
|
||||||
, request = require('request')
|
, request = require('request')
|
||||||
, CERTDB_URL = 'https://mxr.mozilla.org/nss/source/lib/ckfw/builtins/certdata.txt?raw=1'
|
, CERTDB_URL = 'https://mxr.mozilla.org/nss/source/lib/ckfw/builtins/certdata.txt?raw=1'
|
||||||
, HEADER
|
|
||||||
, outputFile
|
, outputFile
|
||||||
, outputPemsDir
|
, outputPemsDir
|
||||||
;
|
;
|
||||||
|
|
||||||
HEADER =
|
|
||||||
"/**\n" +
|
|
||||||
" * Mozilla's root CA store\n" +
|
|
||||||
" *\n" +
|
|
||||||
" * generated from " + CERTDB_URL + "\n" +
|
|
||||||
" */\n\n";
|
|
||||||
|
|
||||||
function Certificate() {
|
function Certificate() {
|
||||||
this.name = null;
|
this.name = null;
|
||||||
this.body = '';
|
this.body = '';
|
||||||
|
@ -156,26 +148,9 @@ function dumpCerts(certs, filename, pemsDir) {
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
filename
|
filename
|
||||||
, HEADER
|
, fs.readFileSync(path.join(__dirname, 'ssl-root-cas.tpl.js'), 'utf8')
|
||||||
+ 'var cas = module.exports = [\n'
|
.replace(/\/\*TPL\*\//, certs.map(function (cert) { return cert.quasiPEM().value; }).join(',\n\n'))
|
||||||
+ certs.map(function (cert) { return cert.quasiPEM().value; }).join(',\n\n')
|
, 'utf8'
|
||||||
+ '\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"
|
|
||||||
);
|
);
|
||||||
console.info("Wrote '" + filename.replace(/'/g, "\\'") + "'.");
|
console.info("Wrote '" + filename.replace(/'/g, "\\'") + "'.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
};
|
Loading…
Reference in New Issue