From 268e892a41b71595bc3ffeb3f324857e9bdb3349 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 16 Jul 2014 11:57:51 -0600 Subject: [PATCH] fix #6 --- ca-store-generator.js | 90 ++++++++++++++++++++++++++++--------------- latest.js | 30 +++++++++++++++ package.json | 3 +- 3 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 latest.js diff --git a/ca-store-generator.js b/ca-store-generator.js index 1fb7c94..5047479 100644 --- a/ca-store-generator.js +++ b/ca-store-generator.js @@ -9,6 +9,7 @@ var fs = require('fs') , HEADER , outputFile , outputPemsDir + , Promise = require('es6-promise').Promise ; HEADER = @@ -143,6 +144,7 @@ function dumpCerts(certs, filename, pemsDir) { fs.writeFileSync(pemsFile, pem.value); }); + console.info("Wrote " + certs.length + " certificates in '" + path.join(__dirname, 'pems/').replace(/'/g, "\\'") + "'."); @@ -172,38 +174,62 @@ function dumpCerts(certs, filename, pemsDir) { console.info("Wrote '" + filename.replace(/'/g, "\\'") + "'."); } -if (process.argv[2] === null) { - console.error("Error: No file specified"); - console.info("Usage: %s ", process.argv[1]); - console.info(" where is the name of the file to write to, relative to %s", process.argv[1]); - console.info("Note that a 'pems/' directory will also be created at the same location as the , containing individual .pem files."); - process.exit(3); +function run(filename) { + return new Promise(function (resolve, reject) { + if (!filename) { + console.error("Error: No file specified"); + console.info("Usage: %s ", process.argv[1]); + console.info(" where is the name of the file to write to, relative to %s", process.argv[1]); + console.info("Note that a 'pems/' directory will also be created at the same location as the , containing individual .pem files."); + reject(3); + return; + } + + // main (combined) output file location, relative to this script's location + outputFile = path.resolve(__dirname, filename); + + // pems/ output directory, in the same directory as the outputFile + outputPemsDir = path.resolve(outputFile, '../pems'); + + + console.info("Loading latest certificates from " + CERTDB_URL); + request.get(CERTDB_URL, function (error, response, body) { + if (error) { + console.error(error); + console.error(error.stacktrace); + reject({ code: 1, error: error }); + return; + } + + if (response.statusCode !== 200) { + console.error("Fetching failed with status code %s", response.statusCode); + reject({ code: 2, error: "Fetching failed with status code " + response.statusCode }); + return; + } + + var lines = body.split("\n") + , certs = parseCertData(lines) + , pemsFile = path.join(outputPemsDir, 'mozilla-certdata.txt') + ; + + fs.writeFileSync(pemsFile, body); + dumpCerts(certs, outputFile, outputPemsDir); + + resolve(); + }); + }); } -// main (combined) output file location, relative to this script's location -outputFile = path.resolve(__dirname, process.argv[2]); +module.exports.generate = run; -// pems/ output directory, in the same directory as the outputFile -outputPemsDir = path.resolve(outputFile, '../pems'); - - -console.info("Loading latest certificates from " + CERTDB_URL); -request.get(CERTDB_URL, function (error, response, body) { - if (error) { - console.error(error.stacktrace); - process.exit(1); - } - - if (response.statusCode !== 200) { - console.error("Fetching failed with status code %s", response.statusCode); - process.exit(2); - } - - var lines = body.split("\n") - , certs = parseCertData(lines) - , pemsFile = path.join(outputPemsDir, 'mozilla-certdata.txt') - ; - - fs.writeFileSync(pemsFile, body); - dumpCerts(certs, outputFile, outputPemsDir); -}); +if (require.main === module) { + run(process.argv[2]) + .then + (function () { + // something + } + , function (errcode) { + process.exit(errcode); + } + ); +} diff --git a/latest.js b/latest.js new file mode 100644 index 0000000..048e301 --- /dev/null +++ b/latest.js @@ -0,0 +1,30 @@ +'use strict'; + +var fs = require('fs') + , path = require('path') + , generate = require('./ca-store-generator').generate + , latestFile = path.join(__dirname, 'ssl-root-cas-latest.js') + ; + +if (!fs.existsSync(latestFile)) { + console.log('needs latest', latestFile); + module.exports = require('./ssl-root-cas'); + generate(latestFile).then(function () { + console.info('\n'); + console.info('##########################################################################################'); + console.info('# #'); + console.info('# Downloaded the latest Root Certificate Authorities. Restart your server to use them. #'); + console.info('# #'); + console.info('##########################################################################################'); + console.info('\n'); + }, function (e) { + console.warn('\n\n'); + console.warn("Couldn't download the latest Root CAs, but it's not a big deal."); + console.warn(''); + console.warn('Use "require(\'ssl-root-cas\')" instead of "require(\'ssl-root-cas/latest\')"'); + console.warn(''); + }); +} else { + console.log('has latest'); + module.exports = require('./ssl-root-cas-latest'); +} diff --git a/package.json b/package.json index 4bb7502..c326b21 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "node ca-store-generator.js ssl-root-cas-test.js", "prepublish": "node ca-store-generator.js ssl-root-cas.js", - "postinstall": "node ca-store-generator.js latest.js" + "postinstall": "node latest.js" }, "repository": { "type": "git", @@ -29,6 +29,7 @@ }, "homepage": "https://github.com/coolaj86/node-ssl-root-cas", "dependencies": { + "es6-promise": "^1.0.0", "request": "~2.34.0" } }