1import { FunctionTriggerEnvelope } from "@/Neon/FunctionTriggerEvent";
2import * as Schema from "effect/Schema";
3
4const events = new Map<string, FunctionTriggerEnvelope>();
5export default {
6 async fetch(request: Request) {
7 if (new URL(request.url).pathname === "/events")
8 return Response.json([...events.values()]);
9 if (request.method !== "POST") return new Response(null, { status: 405 });
10 const invocation = request.headers.get("x-neon-trigger-invocation-id");
11 if (!invocation) return new Response(null, { status: 403 });
12 let event: FunctionTriggerEnvelope;
13 try {
14 event = Schema.decodeUnknownSync(FunctionTriggerEnvelope)(
15 await request.json(),
16 );
17 } catch {
18 return new Response(null, { status: 400 });
19 }
20 if (event.invocation_id !== invocation)
21 return new Response(null, { status: 400 });
22 if (!events.has(invocation)) events.set(invocation, event);
23 return new Response(null, { status: 204 });
24 },
25};