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

main
ts 168 lines 5.91 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as MQ from "@alchemy.run/aws/MQ";
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 * as Redacted from "effect/Redacted";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
11
12 export class MQBindingsFunction extends Lambda.Function<Lambda.Function>()("MQBindingsFunction") {}
13
14 export default MQBindingsFunction.make(
15 {
16 main,
17 functionUrl: true,
18 },
19 Effect.gen(function* () {
20 // A single-instance mq.t3.micro ActiveMQ broker is the cheapest topology
21 // and the only engine whose user APIs (CreateUser/UpdateUser/...) are
22 // supported. Omitting engineVersion lets AWS pick the current default.
23 const broker = yield* MQ.Broker("BindingsBroker", {
24 engineType: "ACTIVEMQ",
25 hostInstanceType: "mq.t3.micro",
26 deploymentMode: "SINGLE_INSTANCE",
27 publiclyAccessible: true,
28 users: [
29 {
30 username: "alchemyadmin",
31 password: Redacted.make("SuperSecretPassw0rd!"),
32 },
33 ],
34 });
35
36 const describeBroker = yield* MQ.DescribeBroker(broker);
37 const rebootBroker = yield* MQ.RebootBroker(broker);
38 const promote = yield* MQ.Promote(broker);
39 const createUser = yield* MQ.CreateUser(broker);
40 const updateUser = yield* MQ.UpdateUser(broker);
41 const deleteUser = yield* MQ.DeleteUser(broker);
42 const describeUser = yield* MQ.DescribeUser(broker);
43 const listUsers = yield* MQ.ListUsers(broker);
44 const listBrokers = yield* MQ.ListBrokers();
45
46 const bound = {
47 describeBroker,
48 rebootBroker,
49 promote,
50 createUser,
51 updateUser,
52 deleteUser,
53 describeUser,
54 listUsers,
55 listBrokers,
56 };
57
58 return {
59 fetch: Effect.gen(function* () {
60 const request = yield* HttpServerRequest;
61 const url = new URL(request.originalUrl);
62 const pathname = url.pathname;
63
64 if (request.method === "GET" && pathname === "/bindings") {
65 return yield* HttpServerResponse.json({
66 bound: Object.keys(bound),
67 });
68 }
69
70 if (request.method === "GET" && pathname === "/broker") {
71 // BrokerId injection scopes the call to the bound broker.
72 const response = yield* describeBroker();
73 return yield* HttpServerResponse.json({
74 brokerName: response.BrokerName,
75 brokerState: response.BrokerState,
76 });
77 }
78
79 if (request.method === "GET" && pathname === "/brokers") {
80 const response = yield* listBrokers();
81 return yield* HttpServerResponse.json({
82 count: (response.BrokerSummaries ?? []).length,
83 });
84 }
85
86 if (request.method === "GET" && pathname === "/users") {
87 const response = yield* listUsers();
88 return yield* HttpServerResponse.json({
89 count: (response.Users ?? []).length,
90 });
91 }
92
93 if (request.method === "POST" && pathname === "/users") {
94 // Full staged-user round-trip: create stages a CREATE pending
95 // change, describe reads it back, delete stages the removal — the
96 // broker never needs a reboot, so the test stays fast and the
97 // stack destroys cleanly.
98 yield* createUser({
99 Username: "alchemytenant",
100 Password: Redacted.make("AnotherSecretPassw0rd!"),
101 });
102 const described = yield* describeUser({
103 Username: "alchemytenant",
104 });
105 yield* deleteUser({ Username: "alchemytenant" });
106 return yield* HttpServerResponse.json({
107 created: true,
108 describedUsername: described.Username,
109 pendingChange: described.Pending?.PendingChange,
110 deleted: true,
111 });
112 }
113
114 if (request.method === "POST" && pathname === "/users/typed-not-found") {
115 // UpdateUser on a nonexistent user round-trips the typed
116 // NotFoundException — an IAM gap would surface AccessDenied (500),
117 // so the typed tag proves grant + injection end-to-end.
118 const typed = yield* updateUser({
119 Username: "alchemynonexistentuser",
120 ConsoleAccess: true,
121 }).pipe(
122 Effect.map(() => false),
123 Effect.catchTag("NotFoundException", () => Effect.succeed(true)),
124 );
125 return yield* HttpServerResponse.json({ typed });
126 }
127
128 if (request.method === "POST" && pathname === "/promote") {
129 // The bound broker has no CRDR replication, so Promote returns the
130 // typed BadRequestException — proving the grant without touching
131 // broker state.
132 const typed = yield* promote({ Mode: "SWITCHOVER" }).pipe(
133 Effect.map(() => false),
134 Effect.catchTag("BadRequestException", () => Effect.succeed(true)),
135 );
136 return yield* HttpServerResponse.json({ typed });
137 }
138
139 if (request.method === "POST" && pathname === "/reboot") {
140 // A real reboot of the RUNNING broker — call this from the LAST
141 // test; the Broker provider's delete waits for the broker to
142 // settle before deleting.
143 yield* rebootBroker();
144 return yield* HttpServerResponse.json({ rebooting: true });
145 }
146
147 return yield* HttpServerResponse.json(
148 { error: "Not found", method: request.method, pathname },
149 { status: 404 },
150 );
151 }).pipe(Effect.orDie),
152 };
153 }).pipe(
154 Effect.provide(
155 Layer.mergeAll(
156 MQ.DescribeBrokerHttp,
157 MQ.RebootBrokerHttp,
158 MQ.PromoteHttp,
159 MQ.CreateUserHttp,
160 MQ.UpdateUserHttp,
161 MQ.DeleteUserHttp,
162 MQ.DescribeUserHttp,
163 MQ.ListUsersHttp,
164 MQ.ListBrokersHttp,
165 ),
166 ),
167 ),
168 );