import handler from "@tanstack/react-start/server-entry"; import { handleGitRequest, matchGitRoute } from "./server/git/proxy"; import { handleJobApi, handleRunnerApi, reapStaleRunnerJobs } from "./server/ci/runner-api"; import { handleQueueBatch } from "./server/events/consumer"; import type { QueueMessage } from "./server/events/emit"; import { HttpError } from "./server/errors"; import { finishGithubLogin, startGithubLogin } from "./server/github-oauth"; import { handleRawRoutes } from "./server/raw"; import { handleLfsRequest, matchLfsRoute } from "./server/lfs/api"; import { handlePagesRequest } from "./server/pages/serve"; import { handleSecurityApi } from "./server/security/api"; import { handleSecurityBatch, SECURITY_QUEUE } from "./server/security/queue"; export { Runner } from "./server/ci/runner-do"; export { CiRunWorkflow } from "./server/ci/run-workflow"; export { PushWorkflow } from "./server/events/push-workflow"; export { PagesDeployWorkflow } from "./server/pages/workflow"; const RUNNER_API_PREFIX = "/api/runner/"; const JOB_API_PREFIX = "/api/"; async function route(request: Request, ctx: ExecutionContext): Promise { const pages = await handlePagesRequest(request); if (pages) return pages; const url = new URL(request.url); const gitRoute = matchGitRoute(url); if (gitRoute) return handleGitRequest(request, gitRoute, ctx); const lfsRoute = matchLfsRoute(url); if (lfsRoute) return handleLfsRequest(request, lfsRoute); if (url.pathname.startsWith(RUNNER_API_PREFIX)) { return handleRunnerApi(request, url.pathname.slice(RUNNER_API_PREFIX.length)); } if (url.pathname.startsWith("/api/jobs/") || url.pathname.startsWith("/api/artifacts/")) { return handleJobApi(request, url.pathname.slice(JOB_API_PREFIX.length)); } const securityApi = await handleSecurityApi(request, url); if (securityApi) return securityApi; if (url.pathname === "/auth/github") return startGithubLogin(request); if (url.pathname === "/auth/github/callback") return finishGithubLogin(request); return handleRawRoutes(request, url); } export default { async fetch(request: Request, _env: Env, ctx: ExecutionContext): Promise { try { const response = await route(request, ctx); if (response) return response; } catch (error) { if (error instanceof HttpError) { return Response.json({ error: error.message }, { status: error.status }); } console.error("Unhandled API error", error); return Response.json({ error: "Internal error" }, { status: 500 }); } return handler.fetch(request); }, async queue(batch: MessageBatch): Promise { if (batch.queue === SECURITY_QUEUE) return handleSecurityBatch(batch); await handleQueueBatch(batch); }, async scheduled(_controller: ScheduledController, _env: Env, ctx: ExecutionContext): Promise { ctx.waitUntil(reapStaleRunnerJobs()); }, } satisfies ExportedHandler;