@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 102 lines 3.89 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
6 /**
7 * Shared scaffolding for Amazon EMR HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation, the IAM action list, and
12 * the injected cluster identifier is boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for a cluster-scoped operation. The deploy-time half
17 * grants `actions` on the bound {@link Cluster}'s ARN (EMR authorizes
18 * cluster-addressed actions against the cluster ARN); the runtime callable
19 * injects the cluster's identifier into the request as `inject`:
20 *
21 * - `"ClusterId"` (default) / `"JobFlowId"` — the cluster id (`j-…`)
22 * - `"TargetResourceArn"` — the cluster ARN (persistent app UIs)
23 * - `"none"` — pass the caller's request through unchanged (id-addressed
24 * companions like `DescribePersistentAppUI` that EMR still authorizes
25 * against the cluster the target belongs to)
26 */
27 export const makeEmrClusterHttpBinding = <I, A, E, R>(options: {
28 /** Fully-qualified binding tag, e.g. `AWS.EMR.DescribeStep`. */
29 tag: string;
30 /** The distilled operation. */
31 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
32 /** IAM actions granted on the cluster ARN. */
33 actions: readonly string[];
34 /** Request field the bound cluster is injected as. */
35 inject?: "ClusterId" | "JobFlowId" | "TargetResourceArn" | "none";
36 }) =>
37 Effect.gen(function* () {
38 const bind = yield* AWSBindings.Bind;
39 const op = yield* AWSBindings.withCredentials(options.operation);
40 const inject = options.inject ?? "ClusterId";
41
42 return Effect.fn(function* <C extends Cluster>(cluster: C) {
43 // Outputs yield a DEFERRED effect — resolve again per invocation below.
44 const Injected = yield* inject === "TargetResourceArn"
45 ? cluster.clusterArn
46 : cluster.clusterId;
47 if (!globalThis.__ALCHEMY_RUNTIME__) {
48 yield* bind`${options.tag}(${cluster})`({
49 policyStatements: [
50 {
51 Effect: "Allow",
52 Action: [...options.actions],
53 Resource: [Output.interpolate`${cluster.clusterArn}`],
54 },
55 ],
56 });
57 }
58 return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* (
59 request?: Omit<I, "ClusterId" | "JobFlowId" | "TargetResourceArn">,
60 ) {
61 return yield* op(
62 (inject === "none" ? { ...request } : { ...request, [inject]: yield* Injected }) as I,
63 );
64 });
65 });
66 });
67
68 /**
69 * Build the impl Effect for an account-level operation (cluster inventory,
70 * release-label catalog). The deploy-time half grants `actions` on `*` —
71 * these read-only discovery actions span every cluster/release label in the
72 * account and support no resource-level scoping.
73 */
74 export const makeEmrAccountHttpBinding = <I, A, E, R>(options: {
75 /** Fully-qualified binding tag, e.g. `AWS.EMR.ListClusters`. */
76 tag: string;
77 /** The distilled operation, invoked with the caller's request as-is. */
78 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
79 /** IAM actions granted on `*`. */
80 actions: readonly string[];
81 }) =>
82 Effect.gen(function* () {
83 const bind = yield* AWSBindings.Bind;
84 const op = yield* AWSBindings.withCredentials(options.operation);
85
86 return Effect.fn(function* () {
87 if (!globalThis.__ALCHEMY_RUNTIME__) {
88 yield* bind`${options.tag}()`({
89 policyStatements: [
90 {
91 Effect: "Allow",
92 Action: [...options.actions],
93 Resource: ["*"],
94 },
95 ],
96 });
97 }
98 return Effect.fn(options.tag)(function* (request?: I) {
99 return yield* op((request ?? {}) as I);
100 });
101 });
102 });