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, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as DomainCloudflare from "@alchemy.run/cloudflare/Domain"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import { Account, Billing, Open } from "./isolation.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Hosts Billing twice, each provided on its own: `POST /open/<id>/<owner>` |
| 11 | * writes through the first, `GET /state/<id>` reads through the second. |
| 12 | */ |
| 13 | export default class DomainTwiceWorker extends Cloudflare.Worker<DomainTwiceWorker>()( |
| 14 | "DomainTwiceWorker", |
| 15 | { main: import.meta.url }, |
| 16 | Effect.gen(function* () { |
| 17 | const accounts = Effect.map(Effect.all({ accounts: Account }), ({ accounts }) => accounts); |
| 18 | const BillingLive = Billing.layer(Layer.empty).pipe( |
| 19 | Layer.provide(DomainCloudflare.layerDurableObjects()), |
| 20 | ); |
| 21 | const writer = yield* Effect.provide(accounts, BillingLive); |
| 22 | const reader = yield* Effect.provide(accounts, BillingLive); |
| 23 | return { |
| 24 | fetch: Effect.gen(function* () { |
| 25 | const request = yield* HttpServerRequest; |
| 26 | const [, route, id, owner] = new URL(request.url, "http://localhost").pathname.split("/"); |
| 27 | if (route === "open") { |
| 28 | yield* writer.send(id!, new Open({ owner: owner! })); |
| 29 | return yield* HttpServerResponse.json({ ok: true }); |
| 30 | } |
| 31 | return yield* HttpServerResponse.json(yield* reader.state(id!)); |
| 32 | }).pipe(Effect.orDie), |
| 33 | }; |
| 34 | }), |
| 35 | ) {} |