// src/lib/server-status.ts // Public, unauthenticated Lobby status. Answers one question the CLI cannot // answer locally: does this server gate registration behind an invite code? // // The endpoint is not deployed on every environment yet (production currently // answers 404), so every failure path resolves to "assume an invite code is // required" — the conservative answer preserves today's behaviour instead of // silently dropping the gate. import { parseServerServiceStatus, type ServerServiceStatus } from './service-status.js'; export const SERVER_STATUS_PATH = '/api/v1/server/status'; const DEFAULT_TIMEOUT_MS = 3_000; export interface ServerStatusSnapshot { /** Undefined when the server did not give a usable boolean. */ registration_invite_required?: boolean; service_status?: ServerServiceStatus; /** `fallback` means the invite-code answer came from the local default. */ source: 'server' | 'fallback'; } export interface FetchServerStatusOptions { lobbyUrl: string; timeoutMs?: number; } function objectValue(value: unknown): Record | undefined { return value !== null && typeof value === 'object' && !Array.isArray(value) ? value as Record : undefined; } function inviteRequiredValue(root: Record | undefined): boolean | undefined { const value = root?.registration_invite_required; return typeof value === 'boolean' ? value : undefined; } /** * Parse the status payload. The observed shape nests everything under `data`; * the top level is also accepted so a future envelope change does not silently * turn into a fallback. */ export function parseServerStatus(payload: unknown): ServerStatusSnapshot { const root = objectValue(payload); const data = objectValue(root?.data); const inviteRequired = inviteRequiredValue(data) ?? inviteRequiredValue(root); const serviceStatus = parseServerServiceStatus(data) ?? parseServerServiceStatus(root); return { ...(inviteRequired === undefined ? {} : { registration_invite_required: inviteRequired }), ...(serviceStatus ? { service_status: serviceStatus } : {}), source: inviteRequired === undefined ? 'fallback' : 'server', }; } /** An invite code is required unless the server explicitly said it is not. */ export function resolveInviteRequired(snapshot: ServerStatusSnapshot): boolean { return snapshot.registration_invite_required !== false; } /** Probe the Lobby status endpoint. Never throws; degrades to `fallback`. */ export async function fetchServerStatus( options: FetchServerStatusOptions, ): Promise { const base = options.lobbyUrl.replace(/\/$/, ''); try { const response = await fetch(`${base}${SERVER_STATUS_PATH}`, { method: 'GET', signal: AbortSignal.timeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS), }); if (!response.ok) return { source: 'fallback' }; const text = await response.text(); return parseServerStatus(JSON.parse(text)); } catch { // 404 / 401 / HTML from a frontend catch-all / network error / timeout all // mean the same thing here: we did not learn the server's policy. return { source: 'fallback' }; } }