From 8ba4b88e0071290e71481151d66115af72a35094 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 3 Jun 2019 09:34:52 +0000 Subject: [PATCH] minor bugfixes for node v6 --- README.md | 1 + lib/compat.js | 37 +++++++++++++++++++++++++++++++++++++ package.json | 4 ++-- server.js | 31 ++++++++++++++----------------- 4 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 lib/compat.js diff --git a/README.md b/README.md index 51aad67..8b64e17 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ and **node.js middleware systems**. - [x] Let's Encrypt v2 - [x] Let's Encrypt v1 - [x] Full node.js support + - [x] node v6+ - [x] core https module - [x] Express.js - [x] [Koa](https://git.rootprojects.org/root/greenlock-koa.js) diff --git a/lib/compat.js b/lib/compat.js new file mode 100644 index 0000000..e80b911 --- /dev/null +++ b/lib/compat.js @@ -0,0 +1,37 @@ +'use strict'; + +function requireBluebird() { + try { + return require('bluebird'); + } catch(e) { + console.error(""); + console.error("DON'T PANIC. You're running an old version of node with incomplete Promise support."); + console.error("EASY FIX: `npm install --save bluebird`"); + console.error(""); + throw e; + } +} + +if ('undefined' === typeof Promise) { + global.Promise = requireBluebird(); +} + +if ('function' !== typeof require('util').promisify) { + require('util').promisify = requireBluebird().promisify; +} + +if (!console.debug) { + console.debug = console.log; +} + +var fs = require('fs'); +var fsAsync = {}; +Object.keys(fs).forEach(function (key) { + var fn = fs[key]; + if ('function' !== typeof fn || !/[a-z]/.test(key[0])) { + return; + } + fsAsync[key] = require('util').promisify(fn); +}); + +exports.fsAsync = fsAsync; diff --git a/package.json b/package.json index c47cc3a..9c7a2b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "greenlock-express", - "version": "2.7.8", + "version": "2.7.9", "description": "Free SSL and managed or automatic HTTPS for node.js with Express, Koa, Connect, Hapi, and all other middleware systems.", "main": "index.js", "homepage": "https://greenlock.domains", @@ -8,7 +8,7 @@ "example": "examples" }, "dependencies": { - "greenlock": "^2.7.17", + "greenlock": "^2.7.24", "redirect-https": "^1.1.5" }, "files": [ diff --git a/server.js b/server.js index 6eb0c48..9752d2a 100644 --- a/server.js +++ b/server.js @@ -21,7 +21,7 @@ var config = require(configpath); // For example: whatever.com may live in /srv/www/whatever.com, thus /srv/www is our path var path = require('path'); -var fs = require('fs').promises; +var fs = require('./lib/compat.js').fsAsync; var finalhandler = require('finalhandler'); var serveStatic = require('serve-static'); @@ -48,7 +48,7 @@ var glx = require('./').create({ //, communityMember: true // Join Greenlock to get important updates, no spam //, debug: true -, store: require('le-store-fs') +, store: require('greenlock-store-fs') }); @@ -58,7 +58,7 @@ server.on('listening', function () { }); function myApproveDomains(opts) { - console.log(opts.domain); + console.info("SNI:", opts.domain); // In this example the filesystem is our "database". // We check in /srv/www for whatever.com and if it exists, it's allowed // SECURITY Greenlock validates opts.domains ahead-of-time so you don't have to @@ -66,19 +66,13 @@ function myApproveDomains(opts) { var domains = []; var domain = opts.domain.replace(/^(www|api)\./, ''); return checkWwws(domain).then(function (hostname) { - // tried both permutations already (failed first, succeeded second) - if (hostname !== domain) { - domains.push(hostname); - return; - } - - // only tried the bare domain, let's try www too - domains.push(domain); - return checkWwws('www.' + domain).then(function (hostname) { - if (hostname === domain) { - domains.push(domain); + // this is either example.com or www.example.com + domains.push(hostname); + if ('api.' + domain !== opts.domain) { + if (!domains.includes(opts.domain)) { + domains.push(opts.domain) } - }); + } }).catch(function () { // ignore error return null; @@ -88,7 +82,8 @@ function myApproveDomains(opts) { if (domains.length) { apiname = 'api.' + domain; } - return checkApi(apiname).then(function () { + return checkApi(apiname).then(function (app) { + if (!app) { return null; } domains.push(apiname); }).catch(function () { return null; @@ -98,6 +93,8 @@ function myApproveDomains(opts) { return Promise.reject(new Error("no bare, www., or api. domain matching '" + opts.domain + "'")); } + console.info('Approved domains:', domains); + opts.domains = domains; //opts.email = email; opts.agreeTos = true; // pick the shortest (bare) or latest (www. instead of api.) to be the subject @@ -176,7 +173,7 @@ function checkWwws(_hostname) { function myVhostApp(req, res) { // SECURITY greenlock pre-sanitizes hostnames to prevent unauthorized fs access so you don't have to // (also: only domains approved above will get here) - console.info(req.method, (req.headers.host|'') + req.url); + console.info(req.method, (req.headers.host||'') + req.url); Object.keys(req.headers).forEach(function (key) { console.info(key, req.headers[key]) });