🩹 Ensure stat finished before startSSLServer()

The two chained stat() promises may not have finished by the time
`enableSSL` is evaluated in case of a slow file system (e.g. on a
Raspberry Pi where the only block device is an SD card).
This commit is contained in:
Marcell Fülöp 2022-06-24 19:28:46 +00:00
parent a3a26ce063
commit 91d4fd55c0
1 changed files with 12 additions and 10 deletions

View File

@ -24,13 +24,14 @@ const printSuccess = () => {
// Check if the SSL certs are present and SSL should be enabled // Check if the SSL certs are present and SSL should be enabled
let enableSSL = false; let enableSSL = false;
stat(httpsCerts.public).then(() => { const checkCertificateFiles = stat(httpsCerts.public).then(() => {
stat(httpsCerts.private).then(() => { return stat(httpsCerts.private).then(() => {
enableSSL = true; enableSSL = true;
}).catch(() => { printNotSoGood('Private key not present'); }); }).catch(() => { printNotSoGood('Private key not present'); });
}).catch(() => { printNotSoGood('Public key not present'); }); }).catch(() => { printNotSoGood('Public key not present'); });
const startSSLServer = (app) => { const startSSLServer = (app) => {
checkCertificateFiles.then(() => {
// If SSL should be enabled, create a secured server and start it // If SSL should be enabled, create a secured server and start it
if (enableSSL) { if (enableSSL) {
const httpsServer = https.createServer({ const httpsServer = https.createServer({
@ -39,6 +40,7 @@ const startSSLServer = (app) => {
}, app); }, app);
httpsServer.listen(SSLPort, () => { printSuccess(); }); httpsServer.listen(SSLPort, () => { printSuccess(); });
} }
});
}; };
const middleware = (req, res, next) => { const middleware = (req, res, next) => {