Files
events-hub/backend/src/utils/envTemplates.js
T
joshuaandClaude Opus 5.5 94230d0ba9 Hub 2.3.3: refuse server settings the apps can't start with
TRUST_PROXY=TRUE made an Events install build for minutes, then crash at
startup. Saving a server's configuration now refuses it, and installs and
updates are refused (CONFIG_INVALID) while an older saved configuration has
one. Contracts 1.6.0 vendored (agent.checkEnvValue / invalidValues).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 10:25:57 +02:00

53 lines
2.4 KiB
JavaScript

/**
* The env template of the release being installed (crosscode.env-template/1,
* apps/<product>/env.template.json at its tag), read from git like the release notes. The Hub
* refuses to make a job when a required server or defaults value is missing (AGENT-1.0-PLAN §5).
* Releases from before templates (events@2.2.0) use the copy kept here, the same one the
* crosscode script falls back to.
*/
const { agent } = require('@crosscode/contracts');
const { getSettings, releaseRepoToken } = require('./settings');
const { PLATFORM_REPO, parseRepoUrl, gitGet } = require('./releaseSync');
const FALLBACK = { events: require('./envTemplates/events.json'), accounts: require('./envTemplates/accounts.json') };
const cache = new Map();
async function envTemplate(product, version) {
const key = `${product}@${version}`;
if (cache.has(key)) return cache.get(key);
let template = null;
try {
const repo = parseRepoUrl(PLATFORM_REPO);
const token = await releaseRepoToken().catch(() => '');
const text = await gitGet(`${repo.api}/raw/${encodeURIComponent(`apps/${product}`)}/env.template.json?ref=${encodeURIComponent(key)}`, token, { raw: true });
if (text) {
const parsed = agent.envTemplateSchema.safeParse(JSON.parse(text));
if (parsed.success) template = parsed.data;
}
} catch (e) {
console.warn(`[env template] ${key}: ${e?.message || e}`);
}
template = template || (FALLBACK[product] ? agent.envTemplateSchema.parse(FALLBACK[product]) : null);
if (template) cache.set(key, template);
return template;
}
/** What's missing for a release on a server: { server: [...], defaults: [...] }. */
async function missingFor(product, version, serverValues, defaults) {
const template = await envTemplate(product, version);
if (!template) return { server: [], defaults: [] };
return { server: agent.missingRequired(template, 'server', serverValues), defaults: agent.missingRequired(template, 'defaults', defaults) };
}
/**
* Values the apps can't start with, as sentences (e.g. TRUST_PROXY=TRUE, which crashed the Events
* backend after an install's build). Saving a configuration checks them too, but configurations
* saved before Hub 2.3.3 weren't checked.
*/
function invalidFor(serverValues, defaults) {
return [...Object.values(agent.invalidValues(serverValues || {})), ...Object.values(agent.invalidValues(defaults || {}))];
}
module.exports = { envTemplate, missingFor, invalidFor, _cache: cache, getSettings };