/** * Vercel Routing Middleware. Local Vite does not load this file. * On Vercel (VERCEL=1) pages and product APIs stay shut until the unlock * cookie or x-allowlatch-unlock header matches the SHA-256 digest. * /api/gate and agent discovery manifests stay open. */ import { lockPageHtml, requestDenied } from './src/auth/preview-lock.js' export const config = { runtime: 'nodejs', } const OPEN_PREFIXES = [ '/api/gate', '/api/identity/8004', '/api/mcp', '/api/v2/mcp', '/api/v2/escrow/status', '/api/v2/escrow/terms', '/api/v2/escrow/verdict', '/api/v2/escrow/accept', '/api/v2/catalog', '/api/v2/guard', '/api/v2/check', '/api/v2/reputation', '/api/v2/procure', '/api/v2/openapi.json', '/api/v2/registry', '/api/v2/testbed/402', '/api/v2/receipt/verify', '/api/v2/identity/8004', '/api/v2/shard', '/api/v2/benchmark', '/api/x402', '/api/kite', '/.well-known', '/agent.json', '/llms.txt', '/llms-full.txt', '/openapi.json', '/mcp', '/mcp.json', '/AGENTS.md', '/skills', '/robots.txt', '/agent-card.json', '/server.json', '/examples', '/api/v2/passport', '/api/v2/kite', '/api/v2/explain', '/api/v2/x402', '/api/v2/llms.txt', '/api/v2/llms-full.txt', '/api/v2/agent.json', '/api/v2/agent-card.json', '/api/v2/server.json', '/api/v2/AGENTS.md', '/api/v2/skills', '/api/v2/examples', '/api/v2/mcp.json', '/assets', '/favicon.svg', '/favicon.ico', '/favicon-192.png', '/logo.svg', '/logo-light.svg', ] function isOpen(pathname: string): boolean { return OPEN_PREFIXES.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`)) } function continueNext(): Response { return new Response(null, { headers: { 'Cache-Control': 'private, no-store', Vary: 'Cookie', 'x-middleware-next': '1', }, }) } export default function middleware(request: Request): Response { const pass = continueNext() if (process.env.VERCEL !== '1') return pass if (request.method === 'OPTIONS') return pass const url = new URL(request.url) if (isOpen(url.pathname)) return pass if (!requestDenied(request)) return pass if (url.pathname.startsWith('/api/')) { return new Response(JSON.stringify({ ok: false, error: 'Private preview. Unlock required.' }), { status: 401, headers: { 'Content-Type': 'application/json', 'Cache-Control': 'private, no-store', Vary: 'Cookie', }, }) } return new Response(lockPageHtml(), { status: 401, headers: { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'private, no-store', Vary: 'Cookie', }, }) }