96dc79cfe6
- 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>
104 lines
3.5 KiB
JavaScript
Executable File
104 lines
3.5 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
import constants from './src/constants.js';
|
|
import fs from 'node:fs';
|
|
import * as ldapServer from './src/ldapserver.js';
|
|
import net from 'node:net';
|
|
import * as oidcServer from './src/oidcserver.js';
|
|
import paths from './src/paths.js';
|
|
import * as proxyAuth from './src/proxyauth.js';
|
|
import safe from 'safetydance';
|
|
import * as server from './src/server.js';
|
|
import * as directoryServer from './src/directoryserver.js';
|
|
import debugModule from 'debug';
|
|
|
|
const debug = debugModule('box:box');
|
|
|
|
let logFd;
|
|
|
|
async function setupLogging() {
|
|
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) {
|
|
const ts = new Date().toISOString();
|
|
if (status.message) fs.write(logFd, `${ts} ${status.message}\n`, function () {});
|
|
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);
|
|
}
|
|
|
|
async function startServers() {
|
|
await setupLogging();
|
|
await setupNetworking();
|
|
await server.start(); // do this first since it also inits the database
|
|
await proxyAuth.start();
|
|
await ldapServer.start();
|
|
|
|
const conf = await directoryServer.getConfig();
|
|
if (conf.enabled) await directoryServer.start();
|
|
}
|
|
|
|
async function main() {
|
|
const [error] = await safe(startServers());
|
|
if (error) return exitSync({ error, code: 1, message: 'Error starting servers' });
|
|
|
|
// require this here so that logging handler is already setup
|
|
|
|
process.on('SIGHUP', async function () {
|
|
debug('Received SIGHUP. Re-reading configs.');
|
|
const conf = await directoryServer.getConfig();
|
|
if (conf.enabled) await directoryServer.checkCertificate();
|
|
});
|
|
|
|
process.on('SIGINT', async function () {
|
|
debug('Received SIGINT. Shutting down.');
|
|
|
|
await proxyAuth.stop();
|
|
await server.stop();
|
|
await directoryServer.stop();
|
|
await ldapServer.stop();
|
|
await oidcServer.stop();
|
|
|
|
setTimeout(() => {
|
|
debug('Shutdown complete');
|
|
process.exit();
|
|
}, 2000); // need to wait for the task processes to die
|
|
});
|
|
|
|
process.on('SIGTERM', async function () {
|
|
debug('Received SIGTERM. Shutting down.');
|
|
|
|
await proxyAuth.stop();
|
|
await server.stop();
|
|
await directoryServer.stop();
|
|
await ldapServer.stop();
|
|
await oidcServer.stop();
|
|
|
|
setTimeout(() => {
|
|
debug('Shutdown complete');
|
|
process.exit();
|
|
}, 2000); // need to wait for the task processes to die
|
|
});
|
|
|
|
process.on('uncaughtException', (error) => exitSync({ error, code: 1, message: 'From uncaughtException handler.' }));
|
|
}
|
|
|
|
main();
|