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

main
ts 106 lines 3.49 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Cluster } from "./Cluster.ts";
5 import { connectEnvPrefix } from "./Connect.ts";
6
7 /**
8 * Shared scaffolding for the DAX connect bindings.
9 *
10 * NOT exported from `index.ts` — `ConnectReadHttp.ts` / `ConnectWriteHttp.ts`
11 * / `ConnectReadWriteHttp.ts` are thin
12 * `Layer.effect(Cap, makeDaxConnectHttpBinding({ … }))` calls over the
13 * builder below. Only the IAM action list differs per access level.
14 */
15
16 /**
17 * Protocol actions every DAX client needs regardless of access level — the
18 * client discovers cluster nodes and negotiates the item schema before any
19 * item operation.
20 */
21 export const DAX_PROTOCOL_ACTIONS = [
22 "dax:DefineAttributeList",
23 "dax:DefineAttributeListId",
24 "dax:DefineKeySchema",
25 "dax:Endpoints",
26 ] as const;
27
28 /** Read-side DAX data-plane actions. */
29 export const DAX_READ_ACTIONS = [
30 "dax:GetItem",
31 "dax:BatchGetItem",
32 "dax:Query",
33 "dax:Scan",
34 ] as const;
35
36 /** Write-side DAX data-plane actions. */
37 export const DAX_WRITE_ACTIONS = [
38 "dax:PutItem",
39 "dax:UpdateItem",
40 "dax:DeleteItem",
41 "dax:BatchWriteItem",
42 "dax:ConditionCheckItem",
43 ] as const;
44
45 /**
46 * Build the impl Effect for a connect binding. At deploy time it grants
47 * `actions` on the cluster ARN and publishes the discovery endpoint as
48 * `DAX_{LOGICAL_ID}_{HOST,PORT,URL,TLS}` environment variables on the host
49 * Function; at runtime it resolves the same values into a typed connection
50 * descriptor.
51 */
52 export const makeDaxConnectHttpBinding = (options: {
53 /** Fully-qualified binding tag, e.g. `AWS.DAX.ConnectReadWrite`. */
54 tag: string;
55 /** IAM actions granted on the cluster ARN. */
56 actions: readonly string[];
57 }) =>
58 Effect.gen(function* () {
59 const bind = yield* AWSBindings.Bind;
60 return Effect.fn(function* (cluster: Cluster) {
61 const Host = yield* cluster.discoveryEndpointAddress;
62 const Port = yield* cluster.discoveryEndpointPort;
63 const Url = yield* cluster.discoveryEndpointUrl;
64 const EncryptionType = yield* cluster.clusterEndpointEncryptionType;
65
66 if (!globalThis.__ALCHEMY_RUNTIME__) {
67 const prefix = connectEnvPrefix(cluster.LogicalId);
68 yield* bind`${options.tag}(${cluster})`({
69 env: {
70 [`${prefix}_HOST`]: cluster.discoveryEndpointAddress,
71 // Lambda environment variables are strings — stringify the port.
72 [`${prefix}_PORT`]: Output.interpolate`${cluster.discoveryEndpointPort}`,
73 [`${prefix}_URL`]: cluster.discoveryEndpointUrl,
74 [`${prefix}_TLS`]: Output.map(cluster.clusterEndpointEncryptionType, (encryption) =>
75 encryption === "TLS" ? "true" : "false",
76 ),
77 },
78 policyStatements: [
79 {
80 Effect: "Allow",
81 Action: [...options.actions],
82 Resource: [Output.interpolate`${cluster.clusterArn}`],
83 },
84 ],
85 });
86 }
87
88 return Effect.gen(function* () {
89 const host = yield* Host;
90 const port = yield* Port;
91 const url = yield* Url;
92 const encryptionType = yield* EncryptionType;
93 if (!host || port === undefined || !url) {
94 return yield* Effect.die(
95 `DAX discovery endpoint for '${cluster.LogicalId}' is not available yet`,
96 );
97 }
98 return {
99 host,
100 port,
101 url,
102 tls: encryptionType === "TLS",
103 };
104 });
105 });
106 });