@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 233 lines 8.1 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 { Discoverer } from "./Discoverer.ts";
5 import type { Registry } from "./Registry.ts";
6 import type { Schema } from "./Schema.ts";
7
8 /**
9 * Shared scaffolding for EventBridge Schemas HTTP bindings.
10 *
11 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
12 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
13 * below. Everything except the operation, the identifier resolver, and the
14 * IAM action list is boilerplate.
15 */
16
17 /**
18 * Build the impl Effect for a Schemas operation scoped to a {@link Schema}:
19 * the deploy-time half grants `actions` on the bound schema's ARN, and the
20 * runtime half injects the schema's `RegistryName` + `SchemaName` into every
21 * request.
22 */
23 export const makeSchemasSchemaHttpBinding = <
24 I extends { RegistryName: string; SchemaName: string },
25 A,
26 E,
27 R,
28 >(options: {
29 /** Fully-qualified binding tag, e.g. `AWS.Schemas.DescribeSchema`. */
30 tag: string;
31 /** The distilled operation. */
32 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
33 /** IAM actions granted on the schema ARN. */
34 actions: readonly string[];
35 }) =>
36 Effect.gen(function* () {
37 const bind = yield* AWSBindings.Bind;
38 const op = yield* AWSBindings.withCredentials(options.operation);
39
40 return Effect.fn(function* (schema: Schema) {
41 const RegistryName = yield* schema.registryName;
42 const SchemaName = yield* schema.schemaName;
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 yield* bind`${options.tag}(${schema})`({
45 policyStatements: [
46 {
47 Effect: "Allow",
48 Action: [...options.actions],
49 Resource: [schema.schemaArn],
50 },
51 ],
52 });
53 }
54 return Effect.fn(`${options.tag}(${schema.LogicalId})`)(function* (
55 request?: Omit<I, "RegistryName" | "SchemaName">,
56 ) {
57 return yield* op({
58 ...request,
59 RegistryName: yield* RegistryName,
60 SchemaName: yield* SchemaName,
61 } as I);
62 });
63 });
64 });
65
66 /**
67 * Build the impl Effect for a Schemas operation scoped to a {@link Registry}:
68 * the deploy-time half grants `actions` on the registry ARN and on the
69 * registry's schema-type ARN (`…:schema/{registryName}*` — the resource
70 * `schemas:SearchSchemas` authorizes against), and the runtime half injects
71 * the registry's `RegistryName` into every request.
72 */
73 export const makeSchemasRegistryHttpBinding = <
74 I extends { RegistryName: string },
75 A,
76 E,
77 R,
78 >(options: {
79 /** Fully-qualified binding tag, e.g. `AWS.Schemas.SearchSchemas`. */
80 tag: string;
81 /** The distilled operation. */
82 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
83 /** IAM actions granted on the registry ARN. */
84 actions: readonly string[];
85 }) =>
86 Effect.gen(function* () {
87 const bind = yield* AWSBindings.Bind;
88 const op = yield* AWSBindings.withCredentials(options.operation);
89
90 return Effect.fn(function* (registry: Registry) {
91 const RegistryName = yield* registry.registryName;
92 if (!globalThis.__ALCHEMY_RUNTIME__) {
93 yield* bind`${options.tag}(${registry})`({
94 policyStatements: [
95 {
96 Effect: "Allow",
97 Action: [...options.actions],
98 Resource: [
99 registry.registryArn,
100 // Registry-scoped reads (e.g. SearchSchemas) authorize
101 // against the schema-type ARN keyed by the registry name.
102 Output.map(
103 registry.registryArn,
104 (arn) => `${arn.replace(":registry/", ":schema/")}*`,
105 ),
106 ],
107 },
108 ],
109 });
110 }
111 return Effect.fn(`${options.tag}(${registry.LogicalId})`)(function* (
112 request?: Omit<I, "RegistryName">,
113 ) {
114 return yield* op({
115 ...request,
116 RegistryName: yield* RegistryName,
117 } as I);
118 });
119 });
120 });
121
122 /**
123 * Build the impl Effect for a Schemas operation scoped to a
124 * {@link Discoverer}: the deploy-time half grants `actions` on the bound
125 * discoverer's ARN (plus `ruleActions` on the discoverer's managed
126 * EventBridge rule, which Start/StopDiscoverer flip behind the scenes), and
127 * the runtime half injects the `DiscovererId` into every request.
128 */
129 export const makeSchemasDiscovererHttpBinding = <
130 I extends { DiscovererId: string },
131 A,
132 E,
133 R,
134 >(options: {
135 /** Fully-qualified binding tag, e.g. `AWS.Schemas.StartDiscoverer`. */
136 tag: string;
137 /** The distilled operation. */
138 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
139 /** IAM actions granted on the discoverer ARN. */
140 actions: readonly string[];
141 /**
142 * EventBridge actions granted on the discoverer's managed rule
143 * (`rule/{busName}/Schemas-{discovererId}`). StartDiscoverer enables the
144 * rule and StopDiscoverer disables it, so they additionally require
145 * `events:EnableRule` / `events:DisableRule`.
146 */
147 ruleActions?: readonly string[];
148 }) =>
149 Effect.gen(function* () {
150 const bind = yield* AWSBindings.Bind;
151 const op = yield* AWSBindings.withCredentials(options.operation);
152
153 return Effect.fn(function* (discoverer: Discoverer) {
154 const DiscovererId = yield* discoverer.discovererId;
155 if (!globalThis.__ALCHEMY_RUNTIME__) {
156 // The managed rule is named `Schemas-{discovererId}` but TRUNCATED
157 // to EventBridge's 64-char rule-name limit, so the grant wildcards
158 // the name under the `Schemas-` prefix instead of interpolating
159 // the full discoverer id.
160 const toRuleArn = (busSegment: string) =>
161 Output.map(
162 discoverer.discovererArn,
163 (arn) =>
164 `${arn.replace(":schemas:", ":events:").split(":discoverer/")[0]}:rule/${busSegment}Schemas-*`,
165 );
166 yield* bind`${options.tag}(${discoverer})`({
167 policyStatements: [
168 {
169 Effect: "Allow",
170 Action: [...options.actions],
171 Resource: [discoverer.discovererArn],
172 },
173 ...(options.ruleActions !== undefined
174 ? [
175 {
176 Effect: "Allow" as const,
177 Action: [...options.ruleActions],
178 // The managed rule lives on the discovered bus
179 // (`rule/{busName}/Schemas-{id}`) or, for the default
180 // bus, directly under `rule/Schemas-{id}`.
181 Resource: [toRuleArn("*/"), toRuleArn("")],
182 },
183 ]
184 : []),
185 ],
186 });
187 }
188 return Effect.fn(`${options.tag}(${discoverer.LogicalId})`)(function* (
189 request?: Omit<I, "DiscovererId">,
190 ) {
191 return yield* op({
192 ...request,
193 DiscovererId: yield* DiscovererId,
194 } as I);
195 });
196 });
197 });
198
199 /**
200 * Build the impl Effect for an account-level Schemas operation (e.g.
201 * `GetDiscoveredSchema`, which infers a schema from sample events and is not
202 * scoped to any registry or schema resource). The deploy-time half grants
203 * `actions` on `*`.
204 */
205 export const makeSchemasAccountHttpBinding = <I, A, E, R>(options: {
206 /** Fully-qualified binding tag, e.g. `AWS.Schemas.GetDiscoveredSchema`. */
207 tag: string;
208 /** The distilled operation, invoked with the caller's request as-is. */
209 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
210 /** IAM actions granted on `*`. */
211 actions: readonly string[];
212 }) =>
213 Effect.gen(function* () {
214 const bind = yield* AWSBindings.Bind;
215 const op = yield* AWSBindings.withCredentials(options.operation);
216
217 return Effect.fn(function* () {
218 if (!globalThis.__ALCHEMY_RUNTIME__) {
219 yield* bind`${options.tag}()`({
220 policyStatements: [
221 {
222 Effect: "Allow",
223 Action: [...options.actions],
224 Resource: ["*"],
225 },
226 ],
227 });
228 }
229 return Effect.fn(options.tag)(function* (request?: I) {
230 return yield* op((request ?? {}) as I);
231 });
232 });
233 });