reorder functions for no-use-before-define

This commit is contained in:
Girish Ramakrishnan
2026-02-14 16:34:34 +01:00
parent 36aa641cb9
commit e9f96593c3
31 changed files with 2621 additions and 2648 deletions
+31 -31
View File
@@ -67,15 +67,6 @@ async function providerMatches(domainObject, cert) {
return !mismatch;
}
async function notifyCertChange() {
await mailServer.checkCertificate();
await shell.sudo([ RESTART_SERVICE_CMD, 'box' ], {}); // directory server
const allApps = (await apps.list()).filter(app => app.runState !== apps.RSTATE_STOPPED);
for (const app of allApps) {
if (app.manifest.addons?.tls) await setupTlsAddon(app);
}
}
async function reload() {
if (constants.TEST) return;
@@ -83,27 +74,6 @@ async function reload() {
if (error) throw new BoxError(BoxError.NGINX_ERROR, `Error reloading nginx: ${error.message}`);
}
async function setFallbackCertificate(domain, certificate) {
assert.strictEqual(typeof domain, 'string');
assert(certificate && typeof certificate === 'object');
debug(`setFallbackCertificate: setting certs for domain ${domain}`);
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain}.host.cert`), certificate.cert)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain}.host.key`), certificate.key)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
await reload();
await notifyCertChange(); // if domain uses fallback certs, propagate immediately
}
async function restoreFallbackCertificates() {
const result = await domains.list();
for (const domain of result) {
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain.domain}.host.cert`), domain.fallbackCertificate.cert)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain.domain}.host.key`), domain.fallbackCertificate.key)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
}
}
function getAppLocationsSync(app) {
assert.strictEqual(typeof app, 'object');
@@ -184,7 +154,6 @@ async function getDirectoryServerCertificate() {
return await getCertificate(dashboardLocation);
}
// write if contents mismatch (thus preserving mtime)
function writeFileSync(filePath, data) {
assert.strictEqual(typeof filePath, 'string');
assert.strictEqual(typeof data, 'string');
@@ -195,6 +164,15 @@ function writeFileSync(filePath, data) {
return true;
}
async function restoreFallbackCertificates() {
const result = await domains.list();
for (const domain of result) {
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain.domain}.host.cert`), domain.fallbackCertificate.cert)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain.domain}.host.key`), domain.fallbackCertificate.key)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
}
}
async function setupTlsAddon(app) {
assert.strictEqual(typeof app, 'object');
@@ -233,6 +211,28 @@ async function setupTlsAddon(app) {
if (changed || removed) await docker.restartContainer(app.id);
}
// write if contents mismatch (thus preserving mtime)
async function notifyCertChange() {
await mailServer.checkCertificate();
await shell.sudo([ RESTART_SERVICE_CMD, 'box' ], {}); // directory server
const allApps = (await apps.list()).filter(app => app.runState !== apps.RSTATE_STOPPED);
for (const app of allApps) {
if (app.manifest.addons?.tls) await setupTlsAddon(app);
}
}
async function setFallbackCertificate(domain, certificate) {
assert.strictEqual(typeof domain, 'string');
assert(certificate && typeof certificate === 'object');
debug(`setFallbackCertificate: setting certs for domain ${domain}`);
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain}.host.cert`), certificate.cert)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
if (!safe.fs.writeFileSync(path.join(paths.NGINX_CERT_DIR, `${domain}.host.key`), certificate.key)) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
await reload();
await notifyCertChange(); // if domain uses fallback certs, propagate immediately
}
// writes latest certificate to disk and returns the path
async function writeCertificate(location) {
assert.strictEqual(typeof location, 'object');