@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, 06:00 UTC

main
ts 153 lines 5.72 KB
Raw
1 import * as DAX from "@alchemy.run/aws/DAX";
2 import { Role } from "@alchemy.run/aws/IAM/Role";
3 import * as Lambda from "@alchemy.run/aws/Lambda";
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 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "slow-handler.ts");
11
12 /**
13 * Deterministic name of the subnet group the gated test creates out-of-band
14 * (subnet ids come from a runtime default-VPC lookup the fixture module
15 * cannot perform) before deploying this fixture, and deletes after the
16 * cluster is destroyed.
17 */
18 export const SLOW_SUBNET_GROUP_NAME = "alchemy-dax-bindings-subnets";
19
20 export class DAXSlowTestFunction extends Lambda.Function<Lambda.Function>()(
21 "DAXSlowTestFunction",
22 ) {}
23
24 /**
25 * Cluster-scoped binding fixture: deploys a real single-node DAX cluster
26 * (~10 minutes, billed per node-hour — skipped under `--fast`) and a
27 * Lambda bound to it with ConnectReadWrite + RebootNode + DescribeClusters +
28 * IncreaseReplicationFactor + DecreaseReplicationFactor.
29 * The Lambda is not VPC-attached: the connect binding only resolves endpoint
30 * env/attributes and RebootNode is a control-plane call.
31 */
32 export default DAXSlowTestFunction.make(
33 {
34 main,
35 functionUrl: true,
36 },
37 Effect.gen(function* () {
38 const role = yield* Role("DaxBindingsRole", {
39 assumeRolePolicyDocument: {
40 Version: "2012-10-17",
41 Statement: [
42 {
43 Effect: "Allow",
44 Principal: { Service: "dax.amazonaws.com" },
45 Action: ["sts:AssumeRole"],
46 },
47 ],
48 },
49 managedPolicyArns: ["arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess"],
50 });
51 const cluster = yield* DAX.Cluster("Cache", {
52 nodeType: "dax.t3.small",
53 replicationFactor: 1,
54 iamRoleArn: role.roleArn,
55 subnetGroupName: SLOW_SUBNET_GROUP_NAME,
56 description: "alchemy dax bindings test cluster",
57 tags: { fixture: "dax-bindings" },
58 });
59
60 const connect = yield* DAX.ConnectReadWrite(cluster);
61 const rebootNode = yield* DAX.RebootNode(cluster);
62 const describeClusters = yield* DAX.DescribeClusters();
63 const increaseReplicationFactor = yield* DAX.IncreaseReplicationFactor(cluster);
64 const decreaseReplicationFactor = yield* DAX.DecreaseReplicationFactor(cluster);
65
66 return {
67 fetch: Effect.gen(function* () {
68 const request = yield* HttpServerRequest;
69 const url = new URL(request.originalUrl);
70 const pathname = url.pathname;
71
72 if (request.method === "GET" && pathname === "/connect") {
73 const info = yield* connect;
74 const prefix = DAX.connectEnvPrefix("Cache");
75 const env = yield* Effect.sync(() => ({
76 host: process.env[`${prefix}_HOST`],
77 port: process.env[`${prefix}_PORT`],
78 url: process.env[`${prefix}_URL`],
79 tls: process.env[`${prefix}_TLS`],
80 }));
81 return yield* HttpServerResponse.json({ info, env });
82 }
83
84 if (request.method === "GET" && pathname === "/reboot") {
85 const nodeId = url.searchParams.get("nodeId");
86 if (!nodeId) {
87 return yield* HttpServerResponse.json(
88 { error: "nodeId query parameter is required" },
89 { status: 400 },
90 );
91 }
92 const result = yield* rebootNode({ NodeId: nodeId });
93 const node = (result.Cluster?.Nodes ?? []).find((n) => n.NodeId === nodeId);
94 return yield* HttpServerResponse.json({
95 nodeStatus: node?.NodeStatus,
96 });
97 }
98
99 if (request.method === "GET" && pathname === "/scale-probe") {
100 // Cheap probes for the scaling bindings: invalid target factors
101 // must reach service-side validation and surface a typed tag.
102 // An IAM gap (or a ClusterName-injection bug) would surface
103 // AccessDeniedException / ClusterNotFoundFault instead.
104 const increaseTag = yield* increaseReplicationFactor({
105 // A single-node cluster cannot "increase" to its current size.
106 NewReplicationFactor: 1,
107 }).pipe(
108 Effect.map(() => "Increased"),
109 Effect.catchTag(["InvalidParameterValueException", "InvalidClusterStateFault"], (e) =>
110 Effect.succeed(e._tag),
111 ),
112 );
113 const decreaseTag = yield* decreaseReplicationFactor({
114 // Zero nodes is never a valid replication factor.
115 NewReplicationFactor: 0,
116 }).pipe(
117 Effect.map(() => "Decreased"),
118 Effect.catchTag(["InvalidParameterValueException", "InvalidClusterStateFault"], (e) =>
119 Effect.succeed(e._tag),
120 ),
121 );
122 return yield* HttpServerResponse.json({ increaseTag, decreaseTag });
123 }
124
125 if (request.method === "GET" && pathname === "/nodes") {
126 const info = yield* connect;
127 const clusterName = info.host.split(".")[0] ?? "";
128 const result = yield* describeClusters({
129 ClusterNames: [clusterName],
130 });
131 return yield* HttpServerResponse.json({
132 nodeIds: (result.Clusters?.[0]?.Nodes ?? []).map((n) => n.NodeId),
133 });
134 }
135
136 return yield* HttpServerResponse.json(
137 { error: "Not found", method: request.method, pathname },
138 { status: 404 },
139 );
140 }).pipe(Effect.orDie),
141 };
142 }).pipe(
143 Effect.provide(
144 Layer.mergeAll(
145 DAX.ConnectReadWriteHttp,
146 DAX.RebootNodeHttp,
147 DAX.DescribeClustersHttp,
148 DAX.IncreaseReplicationFactorHttp,
149 DAX.DecreaseReplicationFactorHttp,
150 ),
151 ),
152 ),
153 );