The best way to control Claude Code, Codex and more from any computer or phone

main
ts 113 lines 4.29 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
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 export const Bucket = AWS.S3.Bucket("ExternalHostBucket", { forceDestroy: true });
10 export const Table = AWS.DynamoDB.Table("ExternalHostTable", {
11 partitionKey: "pk",
12 attributes: { pk: "S" },
13 });
14 export const Queue = AWS.SQS.Queue("ExternalHostQueue");
15
16 /**
17 * A Cloudflare Worker using AWS bindings: an external host, so each binding
18 * grants on the IAM role Alchemy mints for the Worker and signs with its
19 * assumed credentials. One route per binding; each answers
20 * `{ ok, result | error }` so a failure surfaces the AWS error in the test.
21 */
22 export default class ExternalHostWorker extends Cloudflare.Worker<ExternalHostWorker>()(
23 "ExternalHostWorker",
24 { main: import.meta.url },
25 Effect.gen(function* () {
26 const bucket = yield* Bucket;
27 const table = yield* Table;
28 const queue = yield* Queue;
29
30 const putObject = yield* AWS.S3.PutObject(bucket);
31 const getObject = yield* AWS.S3.GetObject(bucket);
32 const presignGetObject = yield* AWS.S3.PresignGetObject(bucket);
33 const putItem = yield* AWS.DynamoDB.PutItem(table);
34 const getItem = yield* AWS.DynamoDB.GetItem(table);
35 const sendMessage = yield* AWS.SQS.SendMessage(queue);
36 const receiveMessage = yield* AWS.SQS.ReceiveMessage(queue);
37 const validatePolicy = yield* AWS.AccessAnalyzer.ValidatePolicy();
38
39 const respond = (effect: Effect.Effect<unknown, unknown>) =>
40 effect.pipe(
41 Effect.flatMap((result) => HttpServerResponse.json({ ok: true, result })),
42 Effect.catchCause((cause) =>
43 HttpServerResponse.json({ ok: false, error: Cause.pretty(cause) }, { status: 500 }),
44 ),
45 );
46
47 return {
48 fetch: Effect.gen(function* () {
49 const request = yield* HttpServerRequest;
50 const url = new URL(request.url, "http://worker");
51 const key = url.searchParams.get("key") ?? "external-host";
52 switch (url.pathname) {
53 case "/s3":
54 return yield* respond(
55 Effect.gen(function* () {
56 yield* putObject({ Key: key, Body: "from-worker" });
57 const object = yield* getObject({ Key: key });
58 return { contentLength: object.ContentLength };
59 }),
60 );
61 case "/s3/presign":
62 return yield* respond(
63 Effect.gen(function* () {
64 yield* putObject({ Key: key, Body: "presigned" });
65 return { url: yield* presignGetObject({ key }) };
66 }),
67 );
68 case "/dynamodb":
69 return yield* respond(
70 Effect.gen(function* () {
71 yield* putItem({ Item: { pk: { S: key } } });
72 const item = yield* getItem({ Key: { pk: { S: key } }, ConsistentRead: true });
73 return { found: item.Item !== undefined };
74 }),
75 );
76 case "/sqs":
77 return yield* respond(
78 Effect.gen(function* () {
79 yield* sendMessage({ MessageBody: key });
80 const received = yield* receiveMessage({ WaitTimeSeconds: 5 });
81 return { bodies: (received.Messages ?? []).map((message) => message.Body) };
82 }),
83 );
84 case "/access-analyzer":
85 return yield* respond(
86 validatePolicy({
87 policyDocument: JSON.stringify({
88 Version: "2012-10-17",
89 Statement: [{ Effect: "Allow", Action: "s3:GetObject", Resource: "*" }],
90 }),
91 policyType: "IDENTITY_POLICY",
92 }),
93 );
94 default:
95 return HttpServerResponse.text("ok");
96 }
97 }),
98 };
99 }).pipe(
100 Effect.provide(
101 Layer.mergeAll(
102 AWS.S3.PutObjectHttp,
103 AWS.S3.GetObjectHttp,
104 AWS.S3.PresignGetObjectHttp,
105 AWS.DynamoDB.PutItemHttp,
106 AWS.DynamoDB.GetItemHttp,
107 AWS.SQS.SendMessageHttp,
108 AWS.SQS.ReceiveMessageHttp,
109 AWS.AccessAnalyzer.ValidatePolicyHttp,
110 ),
111 ),
112 ),
113 ) {}