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 assert from 'node:assert';
|
2026-02-14 15:43:24 +01:00
|
|
|
import database from './database.js';
|
2021-08-19 21:39:27 -07:00
|
|
|
|
|
|
|
|
|
2026-02-14 16:34:34 +01:00
|
|
|
async function unset(appId, addonId) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof addonId, 'string');
|
|
|
|
|
|
|
|
|
|
await database.query('DELETE FROM appAddonConfigs WHERE appId = ? AND addonId = ?', [ appId, addonId ]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 21:39:27 -07:00
|
|
|
async function set(appId, addonId, env) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof addonId, 'string');
|
|
|
|
|
assert(Array.isArray(env));
|
|
|
|
|
|
|
|
|
|
await unset(appId, addonId);
|
|
|
|
|
if (env.length === 0) return;
|
|
|
|
|
|
|
|
|
|
const query = 'INSERT INTO appAddonConfigs(appId, addonId, name, value) VALUES ';
|
|
|
|
|
const args = [ ], queryArgs = [ ];
|
|
|
|
|
for (let i = 0; i < env.length; i++) {
|
|
|
|
|
args.push(appId, addonId, env[i].name, env[i].value);
|
|
|
|
|
queryArgs.push('(?, ?, ?, ?)');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await database.query(query + queryArgs.join(','), args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function unsetByAppId(appId) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
|
|
|
|
|
await database.query('DELETE FROM appAddonConfigs WHERE appId = ?', [ appId ]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function get(appId, addonId) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof addonId, 'string');
|
|
|
|
|
|
|
|
|
|
const results = await database.query('SELECT name, value FROM appAddonConfigs WHERE appId = ? AND addonId = ?', [ appId, addonId ]);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getByAppId(appId) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
|
|
|
|
|
const results = await database.query('SELECT name, value FROM appAddonConfigs WHERE appId = ?', [ appId ]);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getAppIdByValue(addonId, namePattern, value) {
|
|
|
|
|
assert.strictEqual(typeof addonId, 'string');
|
|
|
|
|
assert.strictEqual(typeof namePattern, 'string');
|
|
|
|
|
assert.strictEqual(typeof value, 'string');
|
|
|
|
|
|
|
|
|
|
const results = await database.query('SELECT appId FROM appAddonConfigs WHERE addonId = ? AND name LIKE ? AND value = ?', [ addonId, namePattern, value ]);
|
|
|
|
|
if (results.length === 0) return null;
|
|
|
|
|
return results[0].appId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getByName(appId, addonId, namePattern) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof addonId, 'string');
|
|
|
|
|
assert.strictEqual(typeof namePattern, 'string');
|
|
|
|
|
|
|
|
|
|
const results = await database.query('SELECT value FROM appAddonConfigs WHERE appId = ? AND addonId = ? AND name LIKE ?', [ appId, addonId, namePattern ]);
|
|
|
|
|
if (results.length === 0) return null;
|
|
|
|
|
return results[0].value;
|
|
|
|
|
}
|
2026-02-14 15:43:24 +01:00
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
get,
|
|
|
|
|
set,
|
|
|
|
|
unset,
|
|
|
|
|
|
|
|
|
|
getByAppId,
|
|
|
|
|
getByName,
|
|
|
|
|
unsetByAppId,
|
|
|
|
|
getAppIdByValue,
|
|
|
|
|
};
|