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

main
ts 164 lines 6.07 KB
Raw
1 import * as ElastiCache from "@alchemy.run/aws/ElastiCache";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
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 Stream from "effect/Stream";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
11
12 export class ElastiCacheBindingsTestFunction extends Lambda.Function<Lambda.Function>()(
13 "ElastiCacheBindingsTestFunction",
14 ) {}
15
16 /**
17 * Account-level binding fixture: no serverless cache is ever created. List
18 * routes prove each grant and response decode against real (possibly empty)
19 * account data; probe routes drive name-addressed operations against
20 * nonexistent names (passed by the test as a query parameter) and must
21 * surface the service's typed not-found tag — an IAM gap would surface
22 * AccessDeniedException and fail the route with an opaque 500.
23 *
24 * The cache-scoped `CreateServerlessCacheSnapshot` binding needs a live
25 * cache and is exercised by the slow (skipped under `--fast`) lifecycle fixture
26 * instead.
27 */
28 export default ElastiCacheBindingsTestFunction.make(
29 {
30 main,
31 functionUrl: true,
32 },
33 Effect.gen(function* () {
34 // Event source: subscribe the host to ElastiCache cache/snapshot
35 // lifecycle events. The deploy proves the EventBridge rule + invoke
36 // permission wiring.
37 yield* ElastiCache.consumeCacheEvents(
38 { kinds: ["cache-limit-approaching", "snapshot-creation-failed"] },
39 (events) =>
40 Stream.runForEach(events, (event) =>
41 Effect.log(`elasticache event: ${event["detail-type"]} -> ${event.resources.join(", ")}`),
42 ),
43 );
44
45 const describeCaches = yield* ElastiCache.DescribeServerlessCaches();
46 const describeSnapshots = yield* ElastiCache.DescribeServerlessCacheSnapshots();
47 const deleteSnapshot = yield* ElastiCache.DeleteServerlessCacheSnapshot();
48 const copySnapshot = yield* ElastiCache.CopyServerlessCacheSnapshot();
49 const exportSnapshot = yield* ElastiCache.ExportServerlessCacheSnapshot();
50 const describeEvents = yield* ElastiCache.DescribeEvents();
51
52 const bound = {
53 describeCaches,
54 describeSnapshots,
55 deleteSnapshot,
56 copySnapshot,
57 exportSnapshot,
58 describeEvents,
59 };
60
61 return {
62 fetch: Effect.gen(function* () {
63 const request = yield* HttpServerRequest;
64 const url = new URL(request.originalUrl);
65 const pathname = url.pathname;
66 const name = url.searchParams.get("name") ?? "";
67
68 if (request.method === "GET" && pathname === "/bindings") {
69 return yield* HttpServerResponse.json({
70 bound: Object.keys(bound),
71 });
72 }
73
74 if (request.method === "GET" && pathname === "/caches") {
75 const result = yield* describeCaches();
76 return yield* HttpServerResponse.json({
77 count: (result.ServerlessCaches ?? []).length,
78 });
79 }
80
81 if (request.method === "GET" && pathname === "/snapshots") {
82 const result = yield* describeSnapshots();
83 return yield* HttpServerResponse.json({
84 count: (result.ServerlessCacheSnapshots ?? []).length,
85 });
86 }
87
88 if (request.method === "GET" && pathname === "/events") {
89 const result = yield* describeEvents({
90 SourceType: "serverless-cache",
91 });
92 return yield* HttpServerResponse.json({
93 count: (result.Events ?? []).length,
94 });
95 }
96
97 if (request.method === "GET" && pathname === "/cache-probe") {
98 const tag = yield* describeCaches({
99 ServerlessCacheName: name,
100 }).pipe(
101 Effect.map(() => "Found"),
102 Effect.catchTag("ServerlessCacheNotFoundFault", (e) => Effect.succeed(e._tag)),
103 );
104 return yield* HttpServerResponse.json({ tag });
105 }
106
107 if (request.method === "GET" && pathname === "/delete-probe") {
108 // ElastiCache validates the account's service-linked role before
109 // snapshot existence, so an account that never kept a cache
110 // surfaces ServiceLinkedRoleNotFoundFault instead of not-found.
111 const tag = yield* deleteSnapshot({
112 ServerlessCacheSnapshotName: name,
113 }).pipe(
114 Effect.map(() => "Deleted"),
115 Effect.catchTag(
116 ["ServerlessCacheSnapshotNotFoundFault", "ServiceLinkedRoleNotFoundFault"],
117 (e) => Effect.succeed(e._tag),
118 ),
119 );
120 return yield* HttpServerResponse.json({ tag });
121 }
122
123 if (request.method === "GET" && pathname === "/copy-probe") {
124 const tag = yield* copySnapshot({
125 SourceServerlessCacheSnapshotName: name,
126 TargetServerlessCacheSnapshotName: `${name}-copy`,
127 }).pipe(
128 Effect.map(() => "Copied"),
129 Effect.catch((e) => Effect.succeed(e._tag)),
130 );
131 return yield* HttpServerResponse.json({ tag });
132 }
133
134 if (request.method === "GET" && pathname === "/export-probe") {
135 const tag = yield* exportSnapshot({
136 ServerlessCacheSnapshotName: name,
137 S3BucketName: "alchemy-elasticache-export-probe",
138 }).pipe(
139 Effect.map(() => "Exported"),
140 Effect.catch((e) => Effect.succeed(e._tag)),
141 );
142 return yield* HttpServerResponse.json({ tag });
143 }
144
145 return yield* HttpServerResponse.json(
146 { error: "Not found", method: request.method, pathname },
147 { status: 404 },
148 );
149 }).pipe(Effect.orDie),
150 };
151 }).pipe(
152 Effect.provide(
153 Layer.mergeAll(
154 Lambda.EventSource,
155 ElastiCache.DescribeServerlessCachesHttp,
156 ElastiCache.DescribeServerlessCacheSnapshotsHttp,
157 ElastiCache.DeleteServerlessCacheSnapshotHttp,
158 ElastiCache.CopyServerlessCacheSnapshotHttp,
159 ElastiCache.ExportServerlessCacheSnapshotHttp,
160 ElastiCache.DescribeEventsHttp,
161 ),
162 ),
163 ),
164 );