@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 102 lines 3.45 KB
Raw
1 import * as Data from "effect/Data";
2 import * as Effect from "effect/Effect";
3 import * as Schema from "effect/Schema";
4
5 const ScheduleEnvelope = Schema.Struct({
6 version: Schema.Literal(1),
7 invocation_id: Schema.String,
8 trigger: Schema.Struct({
9 type: Schema.Literal("schedule"),
10 id: Schema.String,
11 name: Schema.String,
12 }),
13 data: Schema.Struct({ scheduled_at: Schema.String }),
14 });
15 const BucketEnvelope = Schema.Struct({
16 version: Schema.Literal(1),
17 invocation_id: Schema.String,
18 trigger: Schema.Struct({
19 type: Schema.Literal("storage_object_created"),
20 id: Schema.String,
21 name: Schema.String,
22 }),
23 data: Schema.Struct({
24 bucket_name: Schema.String,
25 object_key: Schema.String,
26 }),
27 });
28 /** Validated wire envelope shared by native and Effect handlers. */
29 export const FunctionTriggerEnvelope = Schema.Union([ScheduleEnvelope, BucketEnvelope]);
30 export type FunctionTriggerEnvelope = typeof FunctionTriggerEnvelope.Type;
31
32 export interface CronEvent {
33 /** Stable occurrence ID for application idempotency. */ invocationId: string;
34 /** Project-wide trigger identifier. */ triggerId: string;
35 /** Trigger name. */ name: string;
36 /** UTC scheduled time. */ scheduledAt: string;
37 }
38 export interface BucketEvent {
39 /** Stable occurrence ID for application idempotency. */ invocationId: string;
40 /** Project-wide trigger identifier. */ triggerId: string;
41 /** Trigger name. */ name: string;
42 /** Exact bucket name. */ bucketName: string;
43 /** Exact uploaded key, without path normalization. */ objectKey: string;
44 }
45 export class FunctionTriggerEventError extends Data.TaggedError("FunctionTriggerEventError")<{
46 status: 400 | 403 | 405;
47 message: string;
48 }> {}
49
50 /**
51 * Decode a Neon POST envelope and compare the edge-attested invocation header.
52 * Trust this header only behind Neon's edge, which strips client-supplied
53 * X-Neon-* headers. It is not authentication for an arbitrary local HTTP server.
54 */
55 export const decodeFunctionTriggerEvent = Effect.fn(function* (request: Request) {
56 if (request.method !== "POST")
57 return yield* new FunctionTriggerEventError({
58 status: 405,
59 message: "Trigger delivery requires POST",
60 });
61 const invocation = request.headers.get("x-neon-trigger-invocation-id")?.trim();
62 if (!invocation)
63 return yield* new FunctionTriggerEventError({
64 status: 403,
65 message: "Missing Neon trigger attestation",
66 });
67 const json = yield* Effect.tryPromise({
68 try: () => request.json(),
69 catch: () =>
70 new FunctionTriggerEventError({
71 status: 400,
72 message: "Invalid trigger JSON",
73 }),
74 });
75 const event = yield* Schema.decodeUnknownEffect(FunctionTriggerEnvelope)(json).pipe(
76 Effect.mapError(
77 () =>
78 new FunctionTriggerEventError({
79 status: 400,
80 message: "Invalid trigger discriminator or schema version",
81 }),
82 ),
83 );
84 if (
85 !event.trigger.id ||
86 !event.trigger.name ||
87 (event.trigger.type === "schedule" &&
88 "scheduled_at" in event.data &&
89 !event.data.scheduled_at) ||
90 ("bucket_name" in event.data && (!event.data.bucket_name || !event.data.object_key))
91 )
92 return yield* new FunctionTriggerEventError({
93 status: 400,
94 message: "Empty trigger identity or event data",
95 });
96 if (event.invocation_id !== invocation || event.invocation_id.length === 0)
97 return yield* new FunctionTriggerEventError({
98 status: 403,
99 message: "Trigger invocation ID mismatch",
100 });
101 return event;
102 });