@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 80 lines 3.17 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 { ServerlessCluster } from "./ServerlessCluster.ts";
5
6 /**
7 * Shared scaffolding for AWS MSK (Kafka) HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, makeKafkaClusterHttpBinding({ … }))` over the
11 * builder below. Everything except the operation and the IAM action list is
12 * boilerplate.
13 */
14
15 /**
16 * The cluster ARN is `arn:…:cluster/name/uuid`. Topic resources live under a
17 * sibling `topic/` namespace scoped to the same cluster path; both the
18 * `kafka:*Topic*` control-plane actions and the `kafka-cluster:*` data-plane
19 * actions authorize against these ARNs.
20 */
21 export const topicArnGlob = (clusterArn: string) =>
22 `${clusterArn.replace(":cluster/", ":topic/")}/*`;
23
24 /** Consumer-group ARNs under the cluster's `group/` namespace. */
25 export const groupArnGlob = (clusterArn: string) =>
26 `${clusterArn.replace(":cluster/", ":group/")}/*`;
27
28 /** Transactional-id ARNs under the cluster's `transactional-id/` namespace. */
29 export const transactionalIdArnGlob = (clusterArn: string) =>
30 `${clusterArn.replace(":cluster/", ":transactional-id/")}/*`;
31
32 /**
33 * Build the impl Effect for a cluster-scoped MSK control-plane operation: the
34 * runtime callable injects the bound {@link ServerlessCluster}'s ARN as
35 * `ClusterArn` and the deploy-time half grants `actions` on the cluster ARN
36 * (plus the cluster's `topic/` ARN namespace when `topicScoped` is set —
37 * topic-management actions authorize against topic ARNs).
38 */
39 export const makeKafkaClusterHttpBinding = <I extends { ClusterArn: string }, A, E, R>(options: {
40 /** Fully-qualified binding tag, e.g. `AWS.Kafka.ListTopics`. */
41 tag: string;
42 /** The distilled operation; `ClusterArn` is injected from the cluster. */
43 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
44 /** IAM actions granted on the cluster ARN (and topic glob, if scoped). */
45 actions: readonly string[];
46 /** Also grant `actions` on the cluster's `topic/…/*` ARN namespace. */
47 topicScoped?: boolean;
48 }) =>
49 Effect.gen(function* () {
50 const bind = yield* AWSBindings.Bind;
51 const op = yield* AWSBindings.withCredentials(options.operation);
52
53 return Effect.fn(function* (cluster: ServerlessCluster) {
54 const ClusterArn = yield* cluster.clusterArn;
55 if (!globalThis.__ALCHEMY_RUNTIME__) {
56 yield* bind`${options.tag}(${cluster})`({
57 policyStatements: [
58 {
59 Effect: "Allow",
60 Action: [...options.actions],
61 Resource: options.topicScoped
62 ? [
63 Output.interpolate`${cluster.clusterArn}`,
64 cluster.clusterArn.pipe(Output.map(topicArnGlob)),
65 ]
66 : [Output.interpolate`${cluster.clusterArn}`],
67 },
68 ],
69 });
70 }
71 return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* (
72 request?: Omit<I, "ClusterArn">,
73 ) {
74 return yield* op({
75 ...request,
76 ClusterArn: yield* ClusterArn,
77 } as I);
78 });
79 });
80 });