Files
faunosteo/apps/app/vite/trial-targets.ts
T
bchanot 9f60d841eb fix(app): trial dev server serves one of two known stacks only (R13)
trial.config.ts no longer accepts any loopback gateway. A literal
table (vite/trial-targets.ts) holds the db:sandbox trial target
(5193, gateway 57801, issuer db-sandbox) and the db:trial target
(5194, gateway 58801, issuer faunosteo-trial, gateway identity
probe); the gateway picks the entry by exact match and the app
port, app URL and anon key issuer must match it, with no default
and no fallback. Tests cover both mix-ups, the probe and the
values db-sandbox.sh passes today.
2026-11-10 13:40:42 +01:00

141 lines
4.6 KiB
TypeScript

/**
* The two stacks the trial dev server (vite/trial.config.ts) may proxy to, and nothing else
* (docs/development/TRIAL_STACK.md §3.6, R13): `pnpm db:sandbox trial` (app 5193, sandbox
* gateway 57801, keys of the issuer db-sandbox) and `pnpm db:trial app` (app 5194, trial gateway
* 58801, keys of the issuer faunosteo-trial, a gateway that names itself). The exact gateway
* string picks the target; every other value it implies must match, or the server does not
* start. No default, no fallback to another stack.
*/
export interface TrialTarget {
readonly name: 'sandbox' | 'trial'
readonly appPort: number
readonly gateway: string
readonly issuer: string
/**
* The exact body of the gateway's GET /__faunosteo_stack; absent: no probe
* (the sandbox gateway, A12).
*/
readonly identity?: string
}
export const TRIAL_TARGETS: readonly TrialTarget[] = [
{
name: 'sandbox',
appPort: 5193,
gateway: 'http://127.0.0.1:57801',
issuer: 'db-sandbox',
},
{
name: 'trial',
appPort: 5194,
gateway: 'http://127.0.0.1:58801',
issuer: 'faunosteo-trial',
identity: 'faunosteo_trial\n',
},
]
type Env = Readonly<Record<string, string | undefined>>
/**
* The claims of a JWT payload, or null. Identity only: no secret here, so no signature check
* (scripts/db-trial.sh checks it with the stack's secret before it hands a key to Vite).
*/
export function jwtClaims(
token: string | undefined,
): Record<string, unknown> | null {
const payload = token?.split('.')[1]
if (!payload || !/^[A-Za-z0-9_-]+$/.test(payload)) return null
try {
const base64 = payload.replace(/-/g, '+').replace(/_/g, '/')
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, '=')
const claims: unknown = JSON.parse(atob(padded))
return typeof claims === 'object' &&
claims !== null &&
!Array.isArray(claims)
? (claims as Record<string, unknown>)
: null
} catch {
return null
}
}
/** The one target `env` names, every value it implies checked; throws otherwise. */
export function trialTarget(env: Env): TrialTarget {
const target = TRIAL_TARGETS.find(
(entry) => entry.gateway === env.FAUNOSTEO_TRIAL_GATEWAY,
)
if (!target)
throw new Error(
'FAUNOSTEO_TRIAL_GATEWAY is neither the gateway of pnpm db:sandbox trial ' +
'(http://127.0.0.1:57801) nor that of pnpm db:trial app (http://127.0.0.1:58801)',
)
const claims = jwtClaims(env.VITE_SUPABASE_ANON_KEY)
if (claims?.role !== 'anon' || claims.iss !== target.issuer)
throw new Error(
`VITE_SUPABASE_ANON_KEY is not an anon key of the issuer ${target.issuer} ` +
`(${target.name} stack)`,
)
const host = env.FAUNOSTEO_TRIAL_HOST ?? ''
if (!host || env.VITE_SUPABASE_URL !== `http://${host}:${target.appPort}`)
throw new Error(
`VITE_SUPABASE_URL must be http://<FAUNOSTEO_TRIAL_HOST>:${target.appPort} ` +
`(${target.name} stack)`,
)
return target
}
/** Why Vite's resolved port does not fit the target, or null (the CLI's --port is resolved in). */
export function appPortProblem(
target: TrialTarget,
port: number | undefined,
strictPort: boolean | undefined,
): string | null {
if (port === target.appPort && strictPort === true) return null
return `the ${target.name} trial app runs on port ${target.appPort} with --strictPort only`
}
/**
* The part of Vite's resolved config the guard reads (structural: no import of Vite's types
* here).
*/
export interface ResolvedServerPort {
readonly server: { readonly port?: number; readonly strictPort?: boolean }
}
/** A Vite plugin that refuses, once the CLI is resolved in, any port but the target's. */
export function targetPortGuard(target: TrialTarget): {
name: string
configResolved(config: ResolvedServerPort): void
} {
return {
name: 'faunosteo-trial-target-port',
configResolved(config) {
const problem = appPortProblem(
target,
config.server.port,
config.server.strictPort,
)
if (problem) throw new Error(`Refused: ${problem}`)
},
}
}
/** Throws unless the target's gateway answers its identity body exactly, within 3 s. */
export async function checkGatewayIdentity(target: TrialTarget): Promise<void> {
if (target.identity === undefined) return
let body = ''
try {
const res = await fetch(`${target.gateway}/__faunosteo_stack`, {
signal: AbortSignal.timeout(3000),
redirect: 'error',
})
body = res.status === 200 ? await res.text() : ''
} catch {
body = ''
}
if (body !== target.identity)
throw new Error(
`${target.gateway} does not answer as the ${target.name} stack's gateway: refused`,
)
}