(request: Request, env: Env, ctx: ExecutionContext)
| 89 | const pathname = url.pathname; |
| 90 | |
| 91 | const subdomainAppId = extractAppIdFromSubdomain(url.hostname, env.BUILDER_HOSTNAME); |
| 92 | const pathAppId = extractAppIdFromPath(pathname); |
| 93 | const appId = subdomainAppId ?? pathAppId; |
| 94 | |
| 95 | if (appId) { |
| 96 | logger.setTags({ appId }); |
| 97 | } |
| 98 | |
| 99 | logger.info('Request received', { |
| 100 | method: request.method, |
| 101 | pathname, |
| 102 | }); |
| 103 | |
| 104 | if (subdomainAppId) { |
| 105 | const allowedOrigin = getAllowedOrigin(request, env); |
| 106 | |
| 107 | // Handle CORS preflight for cross-origin requests (e.g. keep-alive pings from app.kilo.ai) |
| 108 | if (allowedOrigin && request.method === 'OPTIONS') { |
| 109 | const preflight = withCorsHeaders(new Response(null, { status: 204 }), allowedOrigin); |
| 110 | preflight.headers.set('Access-Control-Max-Age', '86400'); |
| 111 | return preflight; |
| 112 | } |
| 113 | |
| 114 | // All requests to app-id.* subdomain are proxied to the preview sandbox |
| 115 | const response = await handlePreviewProxy(request, env, subdomainAppId); |
| 116 | return allowedOrigin ? withCorsHeaders(response, allowedOrigin) : response; |
| 117 | } |
| 118 | |
| 119 | const initMatch = pathname.match(INIT_PATTERN); |
| 120 | if (initMatch && request.method === 'POST') { |
| 121 | return handleInit(request, env, initMatch[1]); |
| 122 | } |
| 123 | |
| 124 | const tokenMatch = pathname.match(TOKEN_PATTERN); |
| 125 | if (tokenMatch && request.method === 'POST') { |
| 126 | return handleGenerateToken(request, env, tokenMatch[1]); |
| 127 | } |
| 128 | |
| 129 | const commitMatch = pathname.match(COMMIT_PATTERN); |
| 130 | if (commitMatch && request.method === 'GET') { |
| 131 | return handleGetCommit(request, env, commitMatch[1]); |
| 132 | } |
| 133 | |
| 134 | const previewStatusMatch = pathname.match(PREVIEW_STATUS_PATTERN); |
| 135 | if (previewStatusMatch && request.method === 'GET') { |
| 136 | const matchedAppId = previewStatusMatch[1]; |
| 137 | if (env.DEV_MODE) { |
| 138 | previewAppId = matchedAppId; |
| 139 | } |
| 140 | return handleGetPreviewStatus(request, env, matchedAppId); |
| 141 | } |
| 142 | |
| 143 | const buildTriggerMatch = pathname.match(BUILD_TRIGGER_PATTERN); |
| 144 | if (buildTriggerMatch && request.method === 'POST') { |
| 145 | return handleTriggerBuild(request, env, buildTriggerMatch[1]); |
| 146 | } |
| 147 | |
| 148 | const buildLogsMatch = pathname.match(BUILD_LOGS_PATTERN); |
no test coverage detected