The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 2 | import * as Namespace from "alchemy/Namespace"; |
| 3 | import { ProviderModePolicy } from "alchemy/ProviderMode"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import type { Bucket } from "./Bucket.ts"; |
| 8 | import { |
| 9 | BucketEventSource, |
| 10 | type BucketEventSourceProps, |
| 11 | type BucketEventSourceService, |
| 12 | } from "./BucketEventSource.ts"; |
| 13 | import { Function } from "./Function.ts"; |
| 14 | import { FunctionRequest } from "./FunctionEnvironment.ts"; |
| 15 | import { FunctionTrigger } from "./FunctionTrigger.ts"; |
| 16 | import { decodeFunctionTriggerEvent, type BucketEvent } from "./FunctionTriggerEvent.ts"; |
| 17 | |
| 18 | /** |
| 19 | * Neon Function HTTP object-event dispatch and deployment wiring. |
| 20 | * |
| 21 | * ### Subscribe to uploads |
| 22 | * **Example:** Provide the subscription implementation |
| 23 | * ```typescript |
| 24 | * const application = Effect.gen(function* () { |
| 25 | * yield* Neon.BucketEventSource(uploads, { name: "Uploads" }, event => |
| 26 | * Effect.log(event.objectKey), |
| 27 | * ); |
| 28 | * return { fetch: Effect.succeed(HttpServerResponse.text("ok")) }; |
| 29 | * }).pipe(Effect.provide(Neon.BucketEventSourceHttp)); |
| 30 | * ``` |
| 31 | * |
| 32 | * @layer |
| 33 | * @product Bucket |
| 34 | * @provides Neon.BucketEventSource |
| 35 | */ |
| 36 | export const BucketEventSourceHttp = Layer.effect( |
| 37 | BucketEventSource, |
| 38 | Effect.gen(function* () { |
| 39 | const host = yield* Function; |
| 40 | const Trigger = yield* FunctionTrigger; |
| 41 | return Effect.fn(function* ( |
| 42 | bucket: Bucket, |
| 43 | props: BucketEventSourceProps, |
| 44 | handler: (event: BucketEvent) => Effect.Effect<void, unknown>, |
| 45 | ) { |
| 46 | const path = `/__alchemy/neon/bucket/${encodeURIComponent(props.name)}`; |
| 47 | const triggerName = `${host.FQN}:${props.name}`; |
| 48 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 49 | const context = yield* AlchemyContext; |
| 50 | const remote = yield* ProviderModePolicy; |
| 51 | if (!context.dev || remote) |
| 52 | yield* Namespace.push( |
| 53 | host.LogicalId, |
| 54 | Trigger(props.name, { |
| 55 | function: host, |
| 56 | type: "storage_object_created", |
| 57 | name: triggerName, |
| 58 | storageObjectCreated: { bucket, prefix: props.prefix }, |
| 59 | path, |
| 60 | enabled: props.enabled, |
| 61 | }), |
| 62 | ); |
| 63 | } |
| 64 | const bucketName = yield* bucket.bucketName; |
| 65 | yield* host.route( |
| 66 | path, |
| 67 | Effect.gen(function* () { |
| 68 | const event = yield* decodeFunctionTriggerEvent(yield* FunctionRequest); |
| 69 | if ( |
| 70 | event.trigger.type !== "storage_object_created" || |
| 71 | event.trigger.name !== triggerName || |
| 72 | !("bucket_name" in event.data) || |
| 73 | event.data.bucket_name !== (yield* bucketName) || |
| 74 | (props.prefix && !event.data.object_key.startsWith(props.prefix)) |
| 75 | ) |
| 76 | return HttpServerResponse.empty({ status: 400 }); |
| 77 | yield* handler({ |
| 78 | invocationId: event.invocation_id, |
| 79 | triggerId: event.trigger.id, |
| 80 | name: props.name, |
| 81 | bucketName: event.data.bucket_name, |
| 82 | objectKey: event.data.object_key, |
| 83 | }).pipe(Effect.orDie); |
| 84 | return HttpServerResponse.empty({ status: 204 }); |
| 85 | }).pipe( |
| 86 | Effect.catchTag("FunctionTriggerEventError", (error) => |
| 87 | Effect.succeed(HttpServerResponse.empty({ status: error.status })), |
| 88 | ), |
| 89 | ), |
| 90 | ); |
| 91 | }) as BucketEventSourceService; |
| 92 | }), |
| 93 | ); |