The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import path from "pathe"; |
| 11 | |
| 12 | export const Bucket = Cloudflare.R2.Bucket("ExternalHostBucket", { forceDestroy: true }); |
| 13 | export const Namespace = Cloudflare.KV.Namespace("ExternalHostNamespace"); |
| 14 | export const Queue = Cloudflare.Queues.Queue("ExternalHostQueue"); |
| 15 | export const Event = Schema.Struct({ key: Schema.String }); |
| 16 | export const K2Stream = Cloudflare.K2.Stream("ExternalHostStream", { http: true, schema: Event }); |
| 17 | |
| 18 | export class ExternalHostLambda extends Lambda.Function<Lambda.Function>()("ExternalHostLambda") {} |
| 19 | |
| 20 | /** |
| 21 | * An AWS Lambda using Cloudflare `*Http` bindings: a host outside |
| 22 | * Cloudflare, so each binding grants on the Lambda's minted |
| 23 | * `AccountApiToken`, delivered through its env. One route per binding; a |
| 24 | * failure answers 500 with the Cloudflare error. |
| 25 | */ |
| 26 | export default ExternalHostLambda.make( |
| 27 | { |
| 28 | main: path.resolve(import.meta.dirname, "handler.ts"), |
| 29 | functionUrl: true, |
| 30 | timeout: Duration.seconds(30), |
| 31 | }, |
| 32 | Effect.gen(function* () { |
| 33 | const r2 = yield* Cloudflare.R2.ReadWriteBucket(Bucket); |
| 34 | const kv = yield* Cloudflare.KV.ReadWriteNamespace(Namespace); |
| 35 | const queue = yield* Cloudflare.Queues.WriteQueue(Queue); |
| 36 | const stream = yield* Cloudflare.K2.WriteStream(yield* K2Stream); |
| 37 | |
| 38 | return { |
| 39 | fetch: Effect.gen(function* () { |
| 40 | const request = yield* HttpServerRequest; |
| 41 | const url = new URL(request.originalUrl, "http://lambda"); |
| 42 | const key = url.searchParams.get("key") ?? "external-host"; |
| 43 | switch (url.pathname) { |
| 44 | case "/r2": { |
| 45 | yield* r2.put(key, "from-lambda"); |
| 46 | const object = yield* r2.get(key); |
| 47 | return yield* HttpServerResponse.json({ |
| 48 | value: object ? yield* object.text() : null, |
| 49 | }); |
| 50 | } |
| 51 | case "/kv": { |
| 52 | yield* kv.put(key, "from-lambda"); |
| 53 | return yield* HttpServerResponse.json({ value: yield* kv.get(key) }); |
| 54 | } |
| 55 | case "/queue": { |
| 56 | yield* queue.send({ key }); |
| 57 | return yield* HttpServerResponse.json({ sent: true }); |
| 58 | } |
| 59 | case "/k2": { |
| 60 | yield* stream.send([{ key }]); |
| 61 | return yield* HttpServerResponse.json({ sent: true }); |
| 62 | } |
| 63 | default: |
| 64 | return yield* HttpServerResponse.json({ ok: true }); |
| 65 | } |
| 66 | }).pipe( |
| 67 | Effect.catchCause((cause) => |
| 68 | HttpServerResponse.json({ error: Cause.pretty(cause) }, { status: 500 }), |
| 69 | ), |
| 70 | ), |
| 71 | }; |
| 72 | }).pipe( |
| 73 | Effect.provide( |
| 74 | Layer.mergeAll( |
| 75 | Cloudflare.R2.ReadWriteBucketHttp, |
| 76 | Cloudflare.KV.ReadWriteNamespaceHttp, |
| 77 | Cloudflare.Queues.WriteQueueHttp, |
| 78 | Cloudflare.K2.WriteStreamHttp, |
| 79 | ), |
| 80 | ), |
| 81 | ), |
| 82 | ); |