import handler from "vinext/server/app-router-entry"; type AssetFetcher = { fetch(request: Request): Promise; }; interface Env { ASSETS: AssetFetcher; DB?: unknown; } interface ExecutionContext { waitUntil(promise: Promise): void; passThroughOnException(): void; } function traceId(request: Request) { const incoming = request.headers.get("x-request-id")?.trim(); if (incoming && /^[A-Za-z0-9._:-]{1,128}$/.test(incoming)) return incoming; return `req_${crypto.randomUUID()}`; } const contentSecurityPolicy = [ "default-src 'self'", "base-uri 'self'", "frame-ancestors 'none'", "form-action 'self'", "object-src 'none'", "img-src 'self' data:", "font-src 'self' data:", "style-src 'self' 'unsafe-inline'", "script-src 'self' 'unsafe-inline' 'unsafe-eval'", "connect-src 'self' ws: wss:", ].join("; "); function attachWorkerHeaders(response: Response, requestId: string) { response.headers.set("x-request-id", requestId); response.headers.set("x-content-type-options", "nosniff"); response.headers.set("x-frame-options", "DENY"); response.headers.set("referrer-policy", "strict-origin-when-cross-origin"); response.headers.set("permissions-policy", "camera=(), microphone=(), geolocation=()"); response.headers.set("cross-origin-opener-policy", "same-origin"); response.headers.set("cross-origin-resource-policy", "same-origin"); response.headers.set("content-security-policy", contentSecurityPolicy); return response; } const worker = { async fetch(request: Request, env: Env, ctx: ExecutionContext) { const requestId = traceId(request); const headers = new Headers(request.headers); headers.set("x-request-id", requestId); const tracedRequest = new Request(request, { headers }); const response = await handler.fetch(tracedRequest, env, ctx); const tracedResponse = new Response(response.body, { status: response.status, statusText: response.statusText, headers: response.headers, }); return attachWorkerHeaders(tracedResponse, requestId); }, }; export default worker;