@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 184 lines 7.39 KB
Raw
1 import * as DocDB from "@alchemy.run/aws/DocDB";
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 path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "handler.ts");
10
11 // Well-formed-but-nonexistent identifiers — the probe routes drive the
12 // bindings against them so the fixture exercises the IAM grants and the
13 // typed error decode at zero cost (no cluster/snapshot is ever created).
14 const NONEXISTENT_CLUSTER_ID = "alchemy-nonexistent-docdb-probe";
15 const NONEXISTENT_INSTANCE_ID = "alchemy-nonexistent-docdb-instance-probe";
16 const NONEXISTENT_SNAPSHOT_ID = "alchemy-nonexistent-docdb-snapshot-probe";
17
18 // The DocDB barrel re-exports the mongo Effect client; if tree-shaking
19 // retains it, the mongodb driver's optional companion packages must stay
20 // external (they are `require`d in try/catch fallbacks).
21 const MONGODB_OPTIONAL_DEPS = [
22 "kerberos",
23 "@mongodb-js/zstd",
24 "@aws-sdk/credential-providers",
25 "gcp-metadata",
26 "snappy",
27 "socks",
28 "aws4",
29 "mongodb-client-encryption",
30 ];
31
32 export class DocDBTestFunction extends Lambda.Function<Lambda.Function>()("DocDBTestFunction") {}
33
34 export default DocDBTestFunction.make(
35 {
36 main,
37 functionUrl: true,
38 build: { external: MONGODB_OPTIONAL_DEPS },
39 },
40 Effect.gen(function* () {
41 const describeDBClusters = yield* DocDB.DescribeDBClusters();
42 const describeDBInstances = yield* DocDB.DescribeDBInstances();
43 const describeEvents = yield* DocDB.DescribeEvents();
44 const describeDBClusterSnapshots = yield* DocDB.DescribeDBClusterSnapshots();
45 const deleteDBClusterSnapshot = yield* DocDB.DeleteDBClusterSnapshot();
46 const copyDBClusterSnapshot = yield* DocDB.CopyDBClusterSnapshot();
47 const describePendingMaintenanceActions = yield* DocDB.DescribePendingMaintenanceActions();
48 const applyPendingMaintenanceAction = yield* DocDB.ApplyPendingMaintenanceAction();
49
50 const bound = {
51 describeDBClusters,
52 describeDBInstances,
53 describeEvents,
54 describeDBClusterSnapshots,
55 deleteDBClusterSnapshot,
56 copyDBClusterSnapshot,
57 describePendingMaintenanceActions,
58 applyPendingMaintenanceAction,
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
67 if (request.method === "GET" && pathname === "/bindings") {
68 return yield* HttpServerResponse.json({
69 bound: Object.keys(bound),
70 });
71 }
72
73 if (request.method === "GET" && pathname === "/clusters") {
74 // A nonexistent cluster identifier must surface the service's
75 // typed not-found tag — an IAM gap would surface
76 // AccessDeniedException and fail the route with an opaque 500.
77 const tag = yield* describeDBClusters({
78 DBClusterIdentifier: NONEXISTENT_CLUSTER_ID,
79 }).pipe(
80 Effect.map(() => "Found"),
81 Effect.catchTag("DBClusterNotFoundFault", (e) => Effect.succeed(e._tag)),
82 );
83 return yield* HttpServerResponse.json({ tag });
84 }
85
86 if (request.method === "GET" && pathname === "/events") {
87 // Listing the account's recent DocumentDB events succeeds
88 // (possibly empty) — proves the grant and the schema decode.
89 const result = yield* describeEvents();
90 return yield* HttpServerResponse.json({
91 count: (result.Events ?? []).length,
92 });
93 }
94
95 if (request.method === "GET" && pathname === "/instances") {
96 // Nonexistent instance identifier → typed not-found tag.
97 const tag = yield* describeDBInstances({
98 DBInstanceIdentifier: NONEXISTENT_INSTANCE_ID,
99 }).pipe(
100 Effect.map(() => "Found"),
101 Effect.catchTag("DBInstanceNotFoundFault", (e) => Effect.succeed(e._tag)),
102 );
103 return yield* HttpServerResponse.json({ tag });
104 }
105
106 if (request.method === "GET" && pathname === "/snapshots") {
107 // Listing the account's cluster snapshots succeeds (possibly
108 // empty) — proves the grant and the schema decode.
109 const result = yield* describeDBClusterSnapshots();
110 return yield* HttpServerResponse.json({
111 count: (result.DBClusterSnapshots ?? []).length,
112 });
113 }
114
115 if (request.method === "GET" && pathname === "/delete-snapshot-probe") {
116 // Deleting a nonexistent snapshot must surface the typed
117 // not-found tag — proves the write-side grant without ever
118 // creating a snapshot.
119 const tag = yield* deleteDBClusterSnapshot({
120 DBClusterSnapshotIdentifier: NONEXISTENT_SNAPSHOT_ID,
121 }).pipe(
122 Effect.map(() => "Deleted"),
123 Effect.catchTag("DBClusterSnapshotNotFoundFault", (e) => Effect.succeed(e._tag)),
124 );
125 return yield* HttpServerResponse.json({ tag });
126 }
127
128 if (request.method === "GET" && pathname === "/copy-snapshot-probe") {
129 // Copying from a nonexistent source snapshot must surface the
130 // typed not-found tag — proves the copy grant at zero cost.
131 const tag = yield* copyDBClusterSnapshot({
132 SourceDBClusterSnapshotIdentifier: NONEXISTENT_SNAPSHOT_ID,
133 TargetDBClusterSnapshotIdentifier: `${NONEXISTENT_SNAPSHOT_ID}-copy`,
134 }).pipe(
135 Effect.map(() => "Copied"),
136 Effect.catchTag("DBClusterSnapshotNotFoundFault", (e) => Effect.succeed(e._tag)),
137 );
138 return yield* HttpServerResponse.json({ tag });
139 }
140
141 if (request.method === "GET" && pathname === "/pending-maintenance") {
142 // Listing pending maintenance actions succeeds (possibly empty).
143 const result = yield* describePendingMaintenanceActions();
144 return yield* HttpServerResponse.json({
145 count: (result.PendingMaintenanceActions ?? []).length,
146 });
147 }
148
149 if (request.method === "GET" && pathname === "/apply-probe") {
150 // Applying maintenance to a nonexistent resource ARN must surface
151 // the typed ResourceNotFoundFault — proves the apply grant.
152 const arn = url.searchParams.get("arn") ?? "";
153 const tag = yield* applyPendingMaintenanceAction({
154 ResourceIdentifier: arn,
155 ApplyAction: "system-update",
156 OptInType: "next-maintenance",
157 }).pipe(
158 Effect.map(() => "Applied"),
159 Effect.catchTag("ResourceNotFoundFault", (e) => Effect.succeed(e._tag)),
160 );
161 return yield* HttpServerResponse.json({ tag });
162 }
163
164 return yield* HttpServerResponse.json(
165 { error: "Not found", method: request.method, pathname },
166 { status: 404 },
167 );
168 }).pipe(Effect.orDie),
169 };
170 }).pipe(
171 Effect.provide(
172 Layer.mergeAll(
173 DocDB.DescribeDBClustersHttp,
174 DocDB.DescribeDBInstancesHttp,
175 DocDB.DescribeEventsHttp,
176 DocDB.DescribeDBClusterSnapshotsHttp,
177 DocDB.DeleteDBClusterSnapshotHttp,
178 DocDB.CopyDBClusterSnapshotHttp,
179 DocDB.DescribePendingMaintenanceActionsHttp,
180 DocDB.ApplyPendingMaintenanceActionHttp,
181 ),
182 ),
183 ),
184 );