/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/serverless/handler * * `fetch`-style adapter over the single BXC HTTP app (`src/server/app.ts`), * built without GraphQL, Swagger, request logging or installer downloads for * a fast cold start. The short root paths (`/scrape`, `/search`, `/detect`, * `/autocomplete`) are kept as aliases of their `/api/v1` routes; `/scrape` * only allows spawn-free profiles. */ import { type BxcApp, createBxcApp } from "../server/app.ts"; const SERVERLESS_PROFILES = new Set(["static", "fast", "http"]); let appPromise: Promise | null = null; function app(): Promise { appPromise ??= createBxcApp({ graphql: false, swagger: false, requestLogger: false, downloads: false, }); return appPromise; } function withHeaders(res: Response): Response { const headers = new Headers(res.headers); headers.set("cache-control", "no-store"); return new Response(res.body, { status: res.status, statusText: res.statusText, headers }); } function badRequest(error: string): Response { return Response.json({ ok: false, error }, { status: 400 }); } /** Rewrite legacy root aliases onto `/api/v1`, validating like the old routes. */ function rewrite(req: Request): Request | Response { const url = new URL(req.url); const required = (name: string) => url.searchParams.get(name); switch (url.pathname) { case "/search": case "/autocomplete": if (!required("q")) return badRequest("missing 'q'"); break; case "/detect": if (!required("url")) return badRequest("missing 'url'"); break; case "/scrape": { const target = required("url"); if (!target) return badRequest("missing 'url'"); const profile = url.searchParams.get("profile") ?? "static"; if (!SERVERLESS_PROFILES.has(profile)) return badRequest("disallowed profile"); return new Request(new URL("/api/v1/scrape", url), { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ url: target, profile }), }); } default: return req; } url.pathname = `/api/v1${url.pathname}`; return new Request(url, req); } /** * Elysia locates the path with `url.indexOf("/", 11)`, which misroutes very * short origins such as `http://x/`. Serverless platforms may hand us any * host, so route on a fixed origin. */ function normalizeOrigin(req: Request): Request { const url = new URL(req.url); if (url.origin === "http://localhost") return req; return new Request(`http://localhost${url.pathname}${url.search}`, req); } export async function handler(req: Request): Promise { const rewritten = rewrite(normalizeOrigin(req)); if (rewritten instanceof Response) return withHeaders(rewritten); try { return withHeaders(await (await app()).handle(rewritten)); } catch (e) { return withHeaders( Response.json({ ok: false, error: (e as Error).message }, { status: 500 }), ); } }