| 14 | const moduleCacheControl = "public, max-age=60, s-maxage=300"; |
| 15 | |
| 16 | export async function handleRequest(request: Request, env: Env, context: ExecutionContext): Promise<Response> { |
| 17 | let url = new URL(request.url); |
| 18 | |
| 19 | if (request.method === "OPTIONS") { |
| 20 | return new Response(null, { |
| 21 | headers: { |
| 22 | Allow: "GET, HEAD, OPTIONS, POST", |
| 23 | "Access-Control-Allow-Headers": "*", |
| 24 | "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS, POST", |
| 25 | "Access-Control-Allow-Origin": "*", |
| 26 | }, |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | if (url.pathname === "/transform" && request.method === "POST") { |
| 31 | return handleInlineTransformRequest(request, env); |
| 32 | } |
| 33 | |
| 34 | if (request.method !== "GET" && request.method !== "HEAD") { |
| 35 | return new Response(`Invalid request method: ${request.method}`, { |
| 36 | status: 405, |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | if (url.pathname === "/_health") { |
| 41 | return new Response("OK"); |
| 42 | } |
| 43 | |
| 44 | if (url.pathname === "/index.html") { |
| 45 | return redirect("/", 301); |
| 46 | } |
| 47 | |
| 48 | if (url.pathname === "/") { |
| 49 | return new Response(createHomePage(env), { |
| 50 | headers: { |
| 51 | "Cache-Control": "public, max-age=60, s-maxage=300", |
| 52 | "Content-Type": "text/html; charset=utf-8", |
| 53 | }, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | let normalized = normalizeEsmRequestUrl(request.url); |
| 58 | if ("code" in normalized) { |
| 59 | return jsonError(normalized); |
| 60 | } |
| 61 | |
| 62 | let packagePath = normalized.packagePath; |
| 63 | let packageName = packagePath.package.toLowerCase(); |
| 64 | let packageInfo = await getPackageInfo(context, publicNpmRegistry, packageName); |
| 65 | if (packageInfo == null) { |
| 66 | return jsonError({ |
| 67 | code: "PACKAGE_NOT_FOUND", |
| 68 | message: `Package not found: ${packagePath.package}`, |
| 69 | status: 404, |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | let version = resolvePackageVersion(packageInfo, packagePath.version ?? "latest"); |