import { utils } from '@start9labs/start-sdk' import { getJoinSecret } from '../actions/getJoinSecret' import { i18n } from '../i18n' import { sdk } from '../sdk' import { storeJson } from '../fileModels/store.json' /** * The two server-wide secrets, both minted here so they exist before main.ts's daemon ever starts. * * `JOIN_SECRET` is not optional — the server exits immediately without one (`src/server/index.ts` * in the game repo) — so it cannot be left for the user to generate via the action first. * `ADMIN_SECRET` is optional to the server, which switches its `/api/games` routes off rather * than open when it is missing; it is seeded anyway, because an administrator who cannot list or * end a game has no way to clear an abandoned one. */ export const seedSecrets = sdk.setupOnInit(async (effects, kind) => { if (kind === 'install') { await storeJson.merge(effects, { joinSecret: utils.getDefaultString({ charset: 'a-z,A-Z,0-9', len: 24 }), adminSecret: utils.getDefaultString({ charset: 'a-z,A-Z,0-9', len: 32 }), }) await sdk.action.createOwnTask(effects, getJoinSecret, 'critical', { reason: i18n('Get the join secret to share with players'), }) } else { // 'update' and 'restore' — repairs a corrupted store.json without touching secrets that are // already there. A restored volume carries both (they live inside the backed-up volume, see // backups.ts), so there is nothing to seed. An UPDATE from a version that predates // `adminSecret` is the one case that needs filling in, and `merge` cannot do it without // overwriting, so it is handled explicitly below. await storeJson.merge(effects, {}) // Backfill for a volume written before this package had an admin secret. Guarded on absence // rather than merged unconditionally: minting a new one on every start would invalidate the // running daemon's own secret on each restart. const existing = await storeJson.read((s) => s.adminSecret).once() if (!existing) { await storeJson.merge(effects, { adminSecret: utils.getDefaultString({ charset: 'a-z,A-Z,0-9', len: 32, }), }) } } })