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

main
ts 193 lines 8.18 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 { AWSEnvironment } from "../Environment.ts";
5 import type { EventBus } from "./EventBus.ts";
6 import type { Rule } from "./Rule.ts";
7
8 /**
9 * Shared scaffolding for the EventBridge runtime bindings.
10 *
11 * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in
12 * this service is a thin `Layer.effect(Cap, makeEventBridge…HttpBinding({ … }))`
13 * over one of the three builders below. Everything except the operation, the
14 * IAM action list, and the injected identifier(s) is boilerplate:
15 *
16 * - {@link makeEventBridgeAccountHttpBinding} — account-level operations
17 * (`ListEventBuses`, `TestEventPattern`, `ListRuleNamesByTarget`, replay
18 * reads). The runtime callable passes the caller's request through
19 * unchanged; the deploy-time half grants `actions` on `resources`
20 * (default `*` — most EventBridge list/test actions do not support
21 * resource-level permissions).
22 * - {@link makeEventBridgeBusHttpBinding} — operations scoped to an optional
23 * bound {@link EventBus} (`DescribeEventBus`, `ListRules`). The runtime
24 * callable injects the bus name under `busNameKey` (omitted for the
25 * default bus); the deploy-time half grants `actions` on the bus ARN, or
26 * the account default bus ARN when no bus is bound.
27 * - {@link makeEventBridgeRuleHttpBinding} — operations scoped to a bound
28 * {@link Rule} (`DescribeRule`, `ListTargetsByRule`, `EnableRule`,
29 * `DisableRule`). The runtime callable injects the rule name under
30 * `ruleNameKey` and the bus name under `EventBusName` (omitted for the
31 * default bus); the deploy-time half grants `actions` on the rule ARN.
32 *
33 * Genuinely-different bindings stay bespoke: `PutEvents` (per-entry bus-name
34 * injection), `BusSink` (a batching sink over `PutEvents`), and
35 * `StartReplay` (archive-scoped with a replay-wildcard grant).
36 */
37
38 /**
39 * Build the impl Effect for an account-level EventBridge operation. The
40 * runtime callable passes the caller's request through unchanged; the
41 * deploy-time half grants `actions` on `resources` (default `*`).
42 */
43 export const makeEventBridgeAccountHttpBinding = <I, A, E, R>(options: {
44 /** Fully-qualified binding tag, e.g. `AWS.EventBridge.ListEventBuses`. */
45 tag: string;
46 /** The distilled operation, invoked with the caller's request as-is. */
47 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
48 /** IAM actions granted on `resources`. */
49 actions: readonly string[];
50 /**
51 * Resolve the IAM resources the actions are granted on, given the ambient
52 * account and region (e.g. a `replay/*` ARN). Defaults to `["*"]`.
53 */
54 resources?: (env: { accountId: string; region: string }) => readonly string[];
55 }) =>
56 Effect.gen(function* () {
57 const bind = yield* AWSBindings.Bind;
58 const op = yield* AWSBindings.withCredentials(options.operation);
59
60 return Effect.fn(function* () {
61 if (!globalThis.__ALCHEMY_RUNTIME__) {
62 const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
63 accountId: string;
64 region: string;
65 }>;
66 yield* bind`${options.tag}()`({
67 policyStatements: [
68 {
69 Effect: "Allow",
70 Action: [...options.actions],
71 Resource: options.resources ? [...options.resources({ accountId, region })] : ["*"],
72 },
73 ],
74 });
75 }
76 return Effect.fn(options.tag)(function* (request?: I) {
77 return yield* op((request ?? {}) as I);
78 });
79 });
80 });
81
82 /**
83 * Build the impl Effect for an EventBridge operation scoped to an optional
84 * bound {@link EventBus}. The runtime callable injects the bus name under
85 * `busNameKey` (omitted for the default bus); the deploy-time half grants
86 * `actions` on the bus ARN, or the account default bus ARN when no bus is
87 * bound.
88 */
89 export const makeEventBridgeBusHttpBinding = <I, A, E, R>(options: {
90 /** Fully-qualified binding tag, e.g. `AWS.EventBridge.DescribeEventBus`. */
91 tag: string;
92 /** The distilled operation; the bus name is injected from the bound bus. */
93 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
94 /** IAM actions granted on the bus ARN. */
95 actions: readonly string[];
96 /** Request field the bound bus's name is injected under. */
97 busNameKey: string;
98 /**
99 * Override the IAM resources the actions are granted on. Some bus-scoped
100 * operations (`events:ListRules`) do not support resource-level
101 * permissions and must be granted on `*`; the default is the bound bus's
102 * ARN (or the account default bus ARN when no bus is bound).
103 */
104 resources?: (env: { accountId: string; region: string }) => readonly string[];
105 }) =>
106 Effect.gen(function* () {
107 const bind = yield* AWSBindings.Bind;
108 const op = yield* AWSBindings.withCredentials(options.operation);
109
110 return Effect.fn(function* (bus?: EventBus) {
111 const EventBusName = bus ? yield* bus.eventBusName : undefined;
112 if (!globalThis.__ALCHEMY_RUNTIME__) {
113 const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
114 accountId: string;
115 region: string;
116 }>;
117 // Pass the ARN as an unresolved Output — binding data is resolved
118 // by the engine before the host reconciles. Eagerly yielding here
119 // (during plan) produces a deferred object that serializes into an
120 // invalid IAM policy (MalformedPolicyDocumentException).
121 const resource = bus
122 ? Output.interpolate`${bus.eventBusArn}`
123 : (`arn:aws:events:${region}:${accountId}:event-bus/default` as const);
124
125 yield* bind`${options.tag}(${bus ?? "default"})`({
126 policyStatements: [
127 {
128 Effect: "Allow",
129 Action: [...options.actions],
130 Resource: options.resources
131 ? [...options.resources({ accountId, region })]
132 : [resource],
133 },
134 ],
135 });
136 }
137 return Effect.fn(`${options.tag}(${bus?.LogicalId})`)(function* (request?: Partial<I>) {
138 const eventBusName = EventBusName ? yield* EventBusName : undefined;
139 const input: Record<string, unknown> = { ...request };
140 if (eventBusName !== undefined && eventBusName !== "default") {
141 input[options.busNameKey] = eventBusName;
142 }
143 return yield* op(input as I);
144 });
145 });
146 });
147
148 /**
149 * Build the impl Effect for an EventBridge operation scoped to a bound
150 * {@link Rule}. The runtime callable injects the rule name under
151 * `ruleNameKey` and the rule's bus name under `EventBusName` (omitted for
152 * the default bus); the deploy-time half grants `actions` on the rule ARN.
153 */
154 export const makeEventBridgeRuleHttpBinding = <I, A, E, R>(options: {
155 /** Fully-qualified binding tag, e.g. `AWS.EventBridge.DescribeRule`. */
156 tag: string;
157 /** The distilled operation; the rule and bus names are injected. */
158 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
159 /** IAM actions granted on the rule ARN. */
160 actions: readonly string[];
161 /** Request field the bound rule's name is injected under. */
162 ruleNameKey: string;
163 }) =>
164 Effect.gen(function* () {
165 const bind = yield* AWSBindings.Bind;
166 const op = yield* AWSBindings.withCredentials(options.operation);
167
168 return Effect.fn(function* (rule: Rule) {
169 const RuleName = yield* rule.ruleName;
170 const EventBusName = yield* rule.eventBusName;
171 if (!globalThis.__ALCHEMY_RUNTIME__) {
172 yield* bind`${options.tag}(${rule})`({
173 policyStatements: [
174 {
175 Effect: "Allow",
176 Action: [...options.actions],
177 Resource: [rule.ruleArn],
178 },
179 ],
180 });
181 }
182 return Effect.fn(`${options.tag}(${rule.LogicalId})`)(function* (request?: Partial<I>) {
183 const ruleName = yield* RuleName;
184 const eventBusName = yield* EventBusName;
185 const input: Record<string, unknown> = { ...request };
186 input[options.ruleNameKey] = ruleName;
187 if (eventBusName !== "default") {
188 input.EventBusName = eventBusName;
189 }
190 return yield* op(input as I);
191 });
192 });
193 });