import { fileURLToPath } from 'node:url' import { defineConfig, mergeConfig, type Plugin } from 'vite' import base from '../vite.config' import { checkGatewayIdentity, targetPortGuard, trialTarget, } from './trial-targets' /** * Dev server of `pnpm db:sandbox trial` (docs/development/V1-2-6_OWNER_TRIAL.md) and of * `pnpm db:trial app` (docs/development/SUPABASE.md ยง5.2). * The browser talks to this origin only: the three Supabase routes are * forwarded to the stack's gateway on loopback, so the app itself needs no CORS (the proxied * routes keep the stack's own CORS headers). * The server binds the one host the calling script validated (loopback or a private address of * this machine) and refuses any other Host header. It serves exactly one of the two stacks of * ./trial-targets.ts: gateway, app port, app URL and anon key issuer must all name the same one, * and the db:trial gateway must answer its identity; anything else stops it before it listens. */ const host = process.env.FAUNOSTEO_TRIAL_HOST ?? '' const PRIVATE_IPV4 = /^(127\.0\.0\.1|10(\.\d{1,3}){3}|172\.(1[6-9]|2\d|3[01])(\.\d{1,3}){2}|192\.168(\.\d{1,3}){2})$/ if (!PRIVATE_IPV4.test(host)) throw new Error( 'FAUNOSTEO_TRIAL_HOST must be 127.0.0.1 or a private IPv4 address', ) const target = trialTarget(process.env) const SUPABASE_ROUTES = ['/auth/v1', '/rest/v1', '/storage/v1'] const REPO = fileURLToPath(new URL('../../../', import.meta.url)) // The CLI's --port and --strictPort are part of the resolved config: checked there. const targetPortOnly = targetPortGuard(target) /** * `allowedHosts` still lets every IP literal and localhost through (Vite 7): any HTTP request whose * Host is not the trial host is refused (403); the editor-launch route is closed (404). * The trial serves no WebSocket: the proxy's upgrade listener would hold such a socket open, so * every upgrade is closed at once. */ const trialHostOnly: Plugin = { name: 'faunosteo-trial-host-only', configureServer(server) { server.httpServer?.on('upgrade', (_req, socket) => socket.destroy()) server.middlewares.use('/__open-in-editor', (_req, res) => { res.statusCode = 404 res.end() }) server.middlewares.use((req, res, next) => { const name = (req.headers.host ?? '').replace(/:\d+$/, '') const launch = (req.url ?? '').toLowerCase().includes('/__open-in-editor') if (name === host && !launch) return next() res.statusCode = name === host ? 404 : 403 res.end() }) }, } export default defineConfig(async (env) => { await checkGatewayIdentity(target) return mergeConfig(base(env), { plugins: [trialHostOnly, targetPortOnly], // One validated host, never 0.0.0.0: the proxy carries the trial account's session. server: { host, strictPort: true, allowedHosts: [host], // Same origin through the proxy: Vite adds no CORS header, and no hot-reload socket // (its upgrade skips the middlewares above) on a manual trial. cors: false, ws: false, // The LAN sees the app's own sources only: never the repository's notes, seed or builds. fs: { allow: ['apps/app', 'packages', 'node_modules'].map( (dir) => REPO + dir, ), }, proxy: Object.fromEntries( SUPABASE_ROUTES.map((route) => [route, { target: target.gateway }]), ), }, }) })