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 DynamoDB from "@alchemy.run/aws/DynamoDB"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | |
| 9 | /** |
| 10 | * A link shortener's storage on DynamoDB, served from a Cloudflare Worker. |
| 11 | * The Worker reaches AWS through the DynamoDB `*Http` bindings, which mint |
| 12 | * an IAM identity for it and sign each request with the assumed role. |
| 13 | */ |
| 14 | export const LinksTable = DynamoDB.Table("LinksTable", { |
| 15 | partitionKey: "id", |
| 16 | attributes: { id: "S" }, |
| 17 | }); |
| 18 | |
| 19 | export default class LinksWorker extends Cloudflare.Worker<LinksWorker>()( |
| 20 | "DynamoDBLinksWorker", |
| 21 | { main: import.meta.url }, |
| 22 | Effect.gen(function* () { |
| 23 | const table = yield* LinksTable; |
| 24 | const getItem = yield* DynamoDB.GetItem(table); |
| 25 | const putItem = yield* DynamoDB.PutItem(table); |
| 26 | const scan = yield* DynamoDB.Scan(table); |
| 27 | const batchGetItem = yield* DynamoDB.BatchGetItem(table); |
| 28 | |
| 29 | return { |
| 30 | fetch: Effect.gen(function* () { |
| 31 | const request = yield* HttpServerRequest; |
| 32 | const url = new URL(request.url, "http://x"); |
| 33 | const id = url.pathname.match(/^\/links\/([^/]+)$/)?.[1]; |
| 34 | |
| 35 | // PUT /links/:id body: the target URL |
| 36 | if (request.method === "PUT" && id) { |
| 37 | const target = yield* request.text; |
| 38 | yield* putItem({ |
| 39 | Item: { id: { S: id }, url: { S: target } }, |
| 40 | }); |
| 41 | return yield* HttpServerResponse.json({ id, url: target }); |
| 42 | } |
| 43 | |
| 44 | // GET /links/:id |
| 45 | if (request.method === "GET" && id) { |
| 46 | const { Item } = yield* getItem({ |
| 47 | Key: { id: { S: id } }, |
| 48 | ConsistentRead: true, |
| 49 | }); |
| 50 | return Item |
| 51 | ? yield* HttpServerResponse.json({ id, url: Item.url?.S }) |
| 52 | : HttpServerResponse.text("Not Found", { status: 404 }); |
| 53 | } |
| 54 | |
| 55 | // GET /links?ids=a,b — one BatchGetItem round trip |
| 56 | const ids = url.searchParams.get("ids"); |
| 57 | if (request.method === "GET" && url.pathname === "/links" && ids) { |
| 58 | const { Responses } = yield* batchGetItem({ |
| 59 | // Keyed by logical ID; the binding maps it to the table's physical name. |
| 60 | RequestItems: { |
| 61 | LinksTable: { |
| 62 | Keys: ids.split(",").map((id) => ({ id: { S: id } })), |
| 63 | ConsistentRead: true, |
| 64 | }, |
| 65 | }, |
| 66 | }); |
| 67 | return yield* HttpServerResponse.json({ |
| 68 | urls: Object.values(Responses ?? {}) |
| 69 | .flat() |
| 70 | .map((item) => item?.url?.S) |
| 71 | .sort(), |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | // GET /links |
| 76 | if (request.method === "GET" && url.pathname === "/links") { |
| 77 | const { Items } = yield* scan({ ConsistentRead: true }); |
| 78 | return yield* HttpServerResponse.json({ |
| 79 | ids: (Items ?? []).map((item) => item.id?.S).sort(), |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | return HttpServerResponse.text("Not Found", { status: 404 }); |
| 84 | }).pipe( |
| 85 | // Surface the failure (e.g. IAM not yet propagated) to the test. |
| 86 | Effect.catchCause((cause) => |
| 87 | HttpServerResponse.json({ error: Cause.pretty(cause) }, { status: 500 }), |
| 88 | ), |
| 89 | ), |
| 90 | }; |
| 91 | }).pipe( |
| 92 | Effect.provide( |
| 93 | Layer.mergeAll( |
| 94 | DynamoDB.GetItemHttp, |
| 95 | DynamoDB.PutItemHttp, |
| 96 | DynamoDB.ScanHttp, |
| 97 | DynamoDB.BatchGetItemHttp, |
| 98 | ), |
| 99 | ), |
| 100 | ), |
| 101 | ) {} |