75 lines
2.9 KiB
TypeScript
75 lines
2.9 KiB
TypeScript
import { fileURLToPath } from 'node:url'
|
|
import { defineConfig, mergeConfig, type Plugin } from 'vite'
|
|
import base from '../vite.config'
|
|
|
|
/**
|
|
* Dev server of `pnpm db:sandbox trial` (docs/development/V1-2-6_OWNER_TRIAL.md).
|
|
* The browser talks to this origin only: the three Supabase routes are
|
|
* forwarded to the trial 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 that scripts/db-sandbox.sh validated (loopback or a private
|
|
* address of this machine) and refuses any other Host header.
|
|
*/
|
|
const target = process.env.FAUNOSTEO_TRIAL_GATEWAY
|
|
const url = target ? new URL(target) : null
|
|
if (!url || url.protocol !== 'http:' || url.hostname !== '127.0.0.1')
|
|
throw new Error(
|
|
'FAUNOSTEO_TRIAL_GATEWAY must be an http://127.0.0.1:<port> URL',
|
|
)
|
|
|
|
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 SUPABASE_ROUTES = ['/auth/v1', '/rest/v1', '/storage/v1']
|
|
const REPO = fileURLToPath(new URL('../../../', import.meta.url))
|
|
|
|
/**
|
|
* `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((env) =>
|
|
mergeConfig(base(env), {
|
|
plugins: [trialHostOnly],
|
|
// 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 }]),
|
|
),
|
|
},
|
|
}),
|
|
)
|