Asynchronous https support

This commit is contained in:
bartbutenaers 2020-05-11 23:33:54 +02:00 committed by GitHub
parent 15f97bbf26
commit bfa5f39b6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 256 additions and 225 deletions

View File

@ -142,6 +142,7 @@ if (process.env.NODE_RED_ENABLE_PROJECTS) {
settings.editorTheme.projects.enabled = !/^false$/i.test(process.env.NODE_RED_ENABLE_PROJECTS);
}
var httpsPromise;
if (settings.https) {
var startupHttps = settings.https;
@ -150,11 +151,26 @@ if (settings.https) {
startupHttps = startupHttps();
}
if (startupHttps.hasOwnProperty('then') && typeof startupHttps.then === 'function') {
// A promise was returned
httpsPromise = startupHttps;
} else {
// An object was returned - wrap in a promise
httpsPromise = Promise.resolve(startupHttps);
}
}
else {
// No https is enable - wrap null
httpsPromise = Promise.resolve(null);
}
httpsPromise.then(function(startupHttps) {
if (startupHttps) {
server = https.createServer(startupHttps,function(req,res) {app(req,res);});
// Refresh https settings at intervals for NodeJs version 11 and above
if (settings.httpsRefreshInterval) {
if (typeof startupHttps === "function") {
if (typeof settings.https === "function") {
if (server.setSecureContext) {
console.log("Refreshing https settings every " + parseInt(settings.httpsRefreshInterval) + " seconds.");
setInterval(function () {
@ -162,6 +178,17 @@ if (settings.https) {
// Get the result of the function, because createServer doesn't accept functions as input
var refreshedHttps = settings.https();
var httpsPromise;
if (refreshedHttps.hasOwnProperty('then') && typeof refreshedHttps.then === 'function') {
// A promise was returned
httpsPromise = refreshedHttps;
} else {
// An object was returned - wrap in a promise
httpsPromise = Promise.resolve(refreshedHttps);
}
httpsPromise.then(function(refreshedHttps) {
// Use the refreshed https settings
if (!refreshedHttps.key || !refreshedHttps.cert) {
console.log("Cannot refresh the https settings when the https property function doesn't return a 'key' and 'cert'.");
return;
@ -172,8 +199,11 @@ if (settings.https) {
server.setSecureContext(refreshedHttps);
console.log("The https settings have been refreshed.");
}
}).catch(function(err) {
console.log("Failed to apply the refreshed https settings: " + err);
});
} catch(err) {
console.log("Failed to refresh the https settings: " + err);
console.log("Failed to get the refreshed https settings: " + err);
}
}, parseInt(settings.httpsRefreshInterval)*1000);
} else {
@ -386,3 +416,4 @@ process.on('SIGINT', function () {
process.exit();
});
});
});