sprinkle in some async/await

This commit is contained in:
AJ ONeal 2019-11-06 14:29:50 -07:00
parent 297b932db2
commit 71746ca759
1 changed files with 175 additions and 197 deletions

View File

@ -113,8 +113,8 @@ G.create = function(gconf) {
request: request request: request
//punycode: require('punycode') //punycode: require('punycode')
}) })
.then(function() { .then(async function() {
return greenlock.manager._defaults().then(function(MCONF) { var MCONF = await greenlock.manager._defaults();
mergeDefaults(MCONF, gconf); mergeDefaults(MCONF, gconf);
if (true === MCONF.agreeToTerms) { if (true === MCONF.agreeToTerms) {
gdefaults.agreeToTerms = function(tos) { gdefaults.agreeToTerms = function(tos) {
@ -123,7 +123,6 @@ G.create = function(gconf) {
} }
return greenlock.manager._defaults(MCONF); return greenlock.manager._defaults(MCONF);
});
}) })
.catch(function(err) { .catch(function(err) {
if ('load_plugin' !== err.context) { if ('load_plugin' !== err.context) {
@ -196,14 +195,10 @@ G.create = function(gconf) {
}; };
// certs.get // certs.get
greenlock.get = function(args) { greenlock.get = async function(args) {
return greenlock greenlock._single(args);
._single(args)
.then(function() {
args._includePems = true; args._includePems = true;
return greenlock.renew(args); var results = await greenlock.renew(args);
})
.then(function(results) {
if (!results || !results.length) { if (!results || !results.length) {
// TODO throw an error here? // TODO throw an error here?
return null; return null;
@ -235,12 +230,12 @@ G.create = function(gconf) {
// site for plugin options, such as http-01 challenge // site for plugin options, such as http-01 challenge
// pems for the obvious reasons // pems for the obvious reasons
return result; return result;
});
}; };
greenlock._single = function(args) { // TODO remove async here, it doesn't matter
greenlock._single = async function(args) {
if ('string' !== typeof args.servername) { if ('string' !== typeof args.servername) {
return Promise.reject(new Error('no `servername` given')); throw new Error('no `servername` given');
} }
// www.example.com => *.example.com // www.example.com => *.example.com
args.wildname = args.wildname =
@ -262,34 +257,31 @@ G.create = function(gconf) {
args.issueBefore || args.issueBefore ||
args.expiresBefore args.expiresBefore
) { ) {
return Promise.reject( throw new Error(
new Error(
'bad arguments, did you mean to call greenlock.renew()?' 'bad arguments, did you mean to call greenlock.renew()?'
)
); );
} }
// duplicate, force, and others still allowed // duplicate, force, and others still allowed
return Promise.resolve(args); return args;
}; };
greenlock._config = function(args) { greenlock._config = async function(args) {
return greenlock._single(args).then(function() { greenlock._single(args);
return greenlock._configAll(args).then(function(sites) { var sites = await greenlock._configAll(args);
return sites[0]; return sites[0];
});
});
}; };
greenlock._configAll = function(args) { greenlock._configAll = async function(args) {
return greenlock._find(args).then(function(sites) { var sites = await greenlock._find(args);
if (!sites || !sites.length) { if (!sites || !sites.length) {
return []; return [];
} }
sites = JSON.parse(JSON.stringify(sites)); sites = JSON.parse(JSON.stringify(sites));
return greenlock.manager._defaults().then(function(mconf) { var mconf = await greenlock.manager._defaults();
return sites.map(function(site) { return sites.map(function(site) {
if (site.store && site.challenges) { if (site.store && site.challenges) {
return site; return site;
} }
var dconf = site; var dconf = site;
// TODO make cli and api mode the same // TODO make cli and api mode the same
if (gconf._bin_mode) { if (gconf._bin_mode) {
@ -303,26 +295,22 @@ G.create = function(gconf) {
} }
return site; return site;
}); });
});
});
}; };
// needs to get info about the renewal, such as which store and challenge(s) to use // needs to get info about the renewal, such as which store and challenge(s) to use
greenlock.renew = function(args) { greenlock.renew = async function(args) {
return greenlock._init().then(function() { await greenlock._init();
return greenlock.manager._defaults().then(function(mconf) { var mconf = await greenlock.manager._defaults();
return greenlock._renew(mconf, args); return greenlock._renew(mconf, args);
});
});
}; };
greenlock._renew = function(mconf, args) { greenlock._renew = async function(mconf, args) {
if (!args) { if (!args) {
args = {}; args = {};
} }
var renewedOrFailed = []; var renewedOrFailed = [];
//console.log('greenlock._renew find', args); //console.log('greenlock._renew find', args);
return greenlock._find(args).then(function(sites) { var sites = await greenlock._find(args);
// Note: the manager must guaranteed that these are mutable copies // Note: the manager must guaranteed that these are mutable copies
//console.log('greenlock._renew found', sites);; //console.log('greenlock._renew found', sites);;
@ -332,10 +320,11 @@ G.create = function(gconf) {
JSON.stringify(sites) JSON.stringify(sites)
); );
} }
function next() {
await (async function next() {
var site = sites.shift(); var site = sites.shift();
if (!site) { if (!site) {
return Promise.resolve(null); return null;
} }
var order = { site: site }; var order = { site: site };
@ -366,12 +355,9 @@ G.create = function(gconf) {
.then(function() { .then(function() {
return next(); return next();
}); });
} })();
return next().then(function() {
return renewedOrFailed; return renewedOrFailed;
});
});
}; };
greenlock._acme = async function(mconf, args) { greenlock._acme = async function(mconf, args) {
@ -412,16 +398,14 @@ G.create = function(gconf) {
return acme; return acme;
}; };
greenlock.order = function(siteConf) { greenlock.order = async function(siteConf) {
return greenlock._init().then(function() { await greenlock._init();
return greenlock.manager._defaults().then(function(mconf) { var mconf = await greenlock.manager._defaults();
return greenlock._order(mconf, siteConf); return greenlock._order(mconf, siteConf);
});
});
}; };
greenlock._order = function(mconf, siteConf) { greenlock._order = async function(mconf, siteConf) {
// packageAgent, maintainerEmail // packageAgent, maintainerEmail
return greenlock._acme(mconf, siteConf).then(function(acme) { var acme = await greenlock._acme(mconf, siteConf);
var storeConf = siteConf.store || mconf.store; var storeConf = siteConf.store || mconf.store;
storeConf = JSON.parse(JSON.stringify(storeConf)); storeConf = JSON.parse(JSON.stringify(storeConf));
storeConf.packageRoot = gconf.packageRoot; storeConf.packageRoot = gconf.packageRoot;
@ -434,26 +418,26 @@ G.create = function(gconf) {
gconf.packageRoot || process.cwd(), gconf.packageRoot || process.cwd(),
storeConf.basePath storeConf.basePath
); );
return P._loadStore(storeConf).then(function(store) { var store = await P._loadStore(storeConf);
return A._getOrCreate( var account = await A._getOrCreate(
greenlock, greenlock,
mconf, mconf,
store.accounts, store.accounts,
acme, acme,
siteConf siteConf
).then(function(account) { );
var challengeConfs = var challengeConfs = siteConf.challenges || mconf.challenges;
siteConf.challenges || mconf.challenges; var challenges = {};
return Promise.all( var arr = await Promise.all(
Object.keys(challengeConfs).map(function(typ01) { Object.keys(challengeConfs).map(function(typ01) {
return P._loadChallenge(challengeConfs, typ01); return P._loadChallenge(challengeConfs, typ01);
}) })
).then(function(arr) { );
var challenges = {};
arr.forEach(function(el) { arr.forEach(function(el) {
challenges[el._type] = el; challenges[el._type] = el;
}); });
return C._getOrOrder(
var pems = await C._getOrOrder(
greenlock, greenlock,
mconf, mconf,
store.certificates, store.certificates,
@ -461,21 +445,15 @@ G.create = function(gconf) {
challenges, challenges,
account, account,
siteConf siteConf
).then(function(pems) { );
if (!pems) { if (!pems) {
throw new Error('no order result'); throw new Error('no order result');
} }
if (!pems.privkey) { if (!pems.privkey) {
throw new Error( throw new Error('missing private key, which is kinda important');
'missing private key, which is kinda important'
);
} }
return pems; return pems;
});
});
});
});
});
}; };
greenlock._create(); greenlock._create();