The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Http from "alchemy/Http"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 5 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 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 Stream from "effect/Stream"; |
| 10 | import { Clicks, type ClickEvent } from "./Clicks.ts"; |
| 11 | import LinkRoom from "./LinkRoom.ts"; |
| 12 | import { Links, LinksSql } from "./Links.ts"; |
| 13 | import { ShortyApi } from "./ShortyApi.ts"; |
| 14 | import { NeonStorage } from "./Storage.ts"; |
| 15 | |
| 16 | export default class Api extends Cloudflare.Worker<Api>()( |
| 17 | "Api", |
| 18 | { main: import.meta.url }, |
| 19 | Effect.gen(function* () { |
| 20 | const links = yield* Links; |
| 21 | const rooms = yield* LinkRoom; |
| 22 | const clicksQueue = yield* Clicks; |
| 23 | const clicks = yield* Cloudflare.Queues.WriteQueue(clicksQueue); |
| 24 | |
| 25 | // Count clicks in batches: one Durable Object call per link per batch. |
| 26 | yield* Cloudflare.Queues.consumeQueueMessages<ClickEvent>( |
| 27 | clicksQueue, |
| 28 | { batchSize: 100, maxWaitTime: "1 second", maxRetries: 10, retryDelay: "3 seconds" }, |
| 29 | (events) => |
| 30 | events.pipe( |
| 31 | Stream.runFold( |
| 32 | () => new Map<string, number>(), |
| 33 | (counts, { body }) => counts.set(body.code, (counts.get(body.code) ?? 0) + 1), |
| 34 | ), |
| 35 | Effect.flatMap((counts) => |
| 36 | Effect.forEach(counts, ([code, n]) => rooms.getByName(code).record(n), { |
| 37 | concurrency: "unbounded", |
| 38 | discard: true, |
| 39 | }), |
| 40 | ), |
| 41 | ), |
| 42 | ); |
| 43 | |
| 44 | const handlers = HttpApiBuilder.group(ShortyApi, "links", (handlers) => |
| 45 | handlers |
| 46 | .handle("create", ({ payload }) => links.create(payload.url).pipe(Effect.orDie)) |
| 47 | .handle("list", () => links.list().pipe(Effect.orDie)) |
| 48 | .handle("get", ({ params }) => |
| 49 | links.get(params.code).pipe(Effect.catchTag("LinkStoreError", Effect.die)), |
| 50 | ), |
| 51 | ); |
| 52 | |
| 53 | const api = yield* HttpRouter.toHttpEffect( |
| 54 | HttpApiBuilder.layer(ShortyApi).pipe( |
| 55 | Layer.provide(handlers), |
| 56 | Layer.provide(Http.Platform), |
| 57 | Layer.provide(HttpRouter.cors()), |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | return { |
| 62 | fetch: Effect.gen(function* () { |
| 63 | const request = yield* HttpServerRequest; |
| 64 | const [, first, code, action] = new URL(request.url, "http://localhost").pathname.split( |
| 65 | "/", |
| 66 | ); |
| 67 | |
| 68 | // GET /links/:code/live → a WebSocket to the link's room |
| 69 | if (first === "links" && code && action === "live") { |
| 70 | return yield* rooms.getByName(code).fetch(request); |
| 71 | } |
| 72 | |
| 73 | // GET /:code → queue the click, redirect straight away |
| 74 | if (request.method === "GET" && first && first !== "links" && code === undefined) { |
| 75 | const link = yield* links.get(first); |
| 76 | yield* clicks.send({ |
| 77 | code: link.code, |
| 78 | at: new Date().toISOString(), |
| 79 | } satisfies ClickEvent); |
| 80 | return HttpServerResponse.redirect(link.url, { status: 302 }); |
| 81 | } |
| 82 | |
| 83 | return yield* api; |
| 84 | }).pipe( |
| 85 | Effect.catchTags({ |
| 86 | LinkNotFound: ({ code }) => |
| 87 | HttpServerResponse.json({ _tag: "LinkNotFound", code }, { status: 404 }), |
| 88 | LinkStoreError: () => |
| 89 | HttpServerResponse.json({ error: "storage unavailable" }, { status: 503 }), |
| 90 | SendError: () => HttpServerResponse.json({ error: "queue unavailable" }, { status: 503 }), |
| 91 | }), |
| 92 | ), |
| 93 | }; |
| 94 | }).pipe( |
| 95 | Effect.provide( |
| 96 | Layer.mergeAll( |
| 97 | LinksSql.pipe(Layer.provide(NeonStorage)), |
| 98 | Cloudflare.Queues.WriteQueueBinding, |
| 99 | Cloudflare.Queues.EventSourceLive, |
| 100 | ), |
| 101 | ), |
| 102 | ), |
| 103 | ) {} |