@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 77 lines 2.96 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import type { Cluster } from "./Cluster.ts";
6 import { Connect, connectEnvPrefix, type ConnectOptions } from "./Connect.ts";
7
8 /**
9 * Implementation of {@link Connect}. At deploy time it publishes the cluster
10 * endpoint as `MEMORYDB_{LOGICAL_ID}_{HOST,PORT,TLS}` environment variables
11 * on the host Function and grants `memorydb:Connect` on the cluster ARN plus
12 * any IAM-auth users' ARNs; at runtime it resolves the same values into a
13 * typed connection descriptor. The valkey/redis data plane itself is reached
14 * over the VPC network — password auth needs no IAM, while IAM auth
15 * additionally requires the `memorydb:Connect` grant this binding attaches.
16 */
17 export const ConnectHttp = Layer.effect(
18 Connect,
19 Effect.gen(function* () {
20 const bind = yield* AWSBindings.Bind;
21 return Effect.fn(function* (cluster: Cluster, options?: ConnectOptions) {
22 const Host = yield* cluster.endpointAddress;
23 const Port = yield* cluster.endpointPort;
24 const Tls = yield* cluster.tlsEnabled;
25
26 if (!globalThis.__ALCHEMY_RUNTIME__) {
27 // MemoryDB is VPC-only: attach the host to the cluster's VPC.
28 const vpc =
29 options?.subnetIds !== undefined || options?.securityGroupIds !== undefined
30 ? {
31 subnetIds: options?.subnetIds ?? [],
32 securityGroupIds: options?.securityGroupIds ?? [],
33 }
34 : undefined;
35 const prefix = connectEnvPrefix(cluster.LogicalId);
36 yield* bind`AWS.MemoryDB.Connect(${cluster})`({
37 env: {
38 [`${prefix}_HOST`]: cluster.endpointAddress,
39 // Lambda environment variables are strings — stringify the port.
40 [`${prefix}_PORT`]: Output.interpolate`${cluster.endpointPort}`,
41 [`${prefix}_TLS`]: Output.map(cluster.tlsEnabled, (tls) => String(tls ?? true)),
42 },
43 policyStatements: [
44 {
45 Effect: "Allow",
46 Action: ["memorydb:Connect"],
47 // IAM auth authorizes against both the cluster and the user.
48 Resource: [
49 Output.interpolate`${cluster.clusterArn}`,
50 ...(options?.users ?? []).map((user) => Output.interpolate`${user.userArn}`),
51 ],
52 },
53 ],
54 });
55 if (vpc !== undefined) {
56 yield* AWSBindings.bindVpc`AWS.MemoryDB.Connect(${cluster})`(vpc);
57 }
58 }
59
60 return Effect.gen(function* () {
61 const host = yield* Host;
62 const port = yield* Port;
63 const tls = yield* Tls;
64 if (!host || port === undefined) {
65 return yield* Effect.die(
66 `MemoryDB endpoint for '${cluster.LogicalId}' is not available yet`,
67 );
68 }
69 return {
70 host,
71 port,
72 tls: tls ?? true,
73 };
74 });
75 });
76 }),
77 );