Files
cloudron-box/box.js

98 lines
3.3 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
Migrate codebase from CommonJS to ES Modules - Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
import constants from './src/constants.js';
import fs from 'node:fs';
import ldapServer from './src/ldapserver.js';
Migrate codebase from CommonJS to ES Modules - Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
import net from 'node:net';
import oidcServer from './src/oidcserver.js';
Migrate codebase from CommonJS to ES Modules - Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
import paths from './src/paths.js';
import proxyAuth from './src/proxyauth.js';
2026-04-01 09:40:28 +02:00
import safe from '@cloudron/safetydance';
import server from './src/server.js';
import directoryServer from './src/directoryserver.js';
import logger from './src/logger.js';
2026-03-12 23:23:23 +05:30
const { log } = logger('box');
let logFd;
async function setupLogging() {
2023-10-01 13:52:19 +05:30
if (constants.TEST) return;
logFd = fs.openSync(paths.BOX_LOG_FILE, 'a');
// we used to write using a stream before but it caches internally and there is no way to flush it when things crash
process.stdout.write = process.stderr.write = function (...args) {
const callback = typeof args[args.length-1] === 'function' ? args.pop() : function () {}; // callback is required for fs.write
fs.write.apply(fs, [logFd, ...args, callback]);
};
}
// happy eyeballs workaround. when there is no ipv6, nodejs timesout prematurely since the default for ipv4 is just 250ms
// https://github.com/nodejs/node/issues/54359
async function setupNetworking() {
net.setDefaultAutoSelectFamilyAttemptTimeout(2500);
}
// this is also used as the 'uncaughtException' handler which can only have synchronous functions
function exitSync(status) {
2024-10-14 16:21:25 +02:00
const ts = new Date().toISOString();
if (status.message) fs.write(logFd, `${ts} ${status.message}\n`, function () {});
2024-10-14 16:21:25 +02:00
const msg = status.error.stack.replace(/\n/g, `\n${ts} `); // prefix each line with ts
if (status.error) fs.write(logFd, `${ts} ${msg}\n`, function () {});
fs.fsyncSync(logFd);
fs.closeSync(logFd);
process.exit(status.code);
2021-09-07 09:57:49 -07:00
}
2021-09-07 09:57:49 -07:00
async function startServers() {
await setupLogging();
await setupNetworking();
2021-09-07 09:57:49 -07:00
await server.start(); // do this first since it also inits the database
await proxyAuth.start();
await ldapServer.start();
2023-08-03 06:34:55 +05:30
const conf = await directoryServer.getConfig();
if (conf.enabled) await directoryServer.start();
}
2026-02-14 16:52:16 +01:00
const [error] = await safe(startServers());
if (error) exitSync({ error, code: 1, message: 'Error starting servers' });
2021-09-07 09:57:49 -07:00
2026-02-14 16:52:16 +01:00
process.on('SIGHUP', async function () {
log('Received SIGHUP. Re-reading configs.');
2026-02-14 16:52:16 +01:00
const conf = await directoryServer.getConfig();
if (conf.enabled) await directoryServer.checkCertificate();
});
process.on('SIGINT', async function () {
log('Received SIGINT. Shutting down.');
2026-02-14 16:52:16 +01:00
await proxyAuth.stop();
await server.stop();
await directoryServer.stop();
await ldapServer.stop();
await oidcServer.stop();
setTimeout(() => {
log('Shutdown complete');
2026-02-14 16:52:16 +01:00
process.exit();
}, 2000); // need to wait for the task processes to die
});
process.on('SIGTERM', async function () {
log('Received SIGTERM. Shutting down.');
2026-02-14 16:52:16 +01:00
await proxyAuth.stop();
await server.stop();
await directoryServer.stop();
await ldapServer.stop();
await oidcServer.stop();
setTimeout(() => {
log('Shutdown complete');
2026-02-14 16:52:16 +01:00
process.exit();
}, 2000); // need to wait for the task processes to die
});
2026-03-12 23:23:23 +05:30
process.on('uncaughtException', (uncaughtError) => exitSync({ error: uncaughtError, code: 1, message: 'From uncaughtException handler.' }));