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