@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 126 lines 4.62 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { PolicyStatement } from "../IAM/Policy.ts";
4 import type { Accelerator } from "./Accelerator.ts";
5 import { withGaRegion } from "./common.ts";
6 import type { EndpointGroup } from "./EndpointGroup.ts";
7
8 /**
9 * Shared scaffolding for AWS Global Accelerator 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 two builders
13 * below. Everything except the operation and the IAM action list is
14 * boilerplate.
15 *
16 * The Global Accelerator control-plane API only exists in us-west-2, so the
17 * operation is resolved under {@link withGaRegion} — the binding works no
18 * matter which region the host Function is deployed to.
19 */
20
21 /**
22 * Build the impl Effect for a Global Accelerator operation scoped to an
23 * {@link Accelerator}: the deploy-time half grants `actions` on the bound
24 * accelerator's ARN, and the runtime half injects the accelerator's
25 * `AcceleratorArn` into every request.
26 */
27 export const makeGaAcceleratorHttpBinding = <
28 I extends { AcceleratorArn: string },
29 A,
30 E,
31 R,
32 >(options: {
33 /** Fully-qualified binding tag, e.g. `AWS.GlobalAccelerator.DescribeAccelerator`. */
34 tag: string;
35 /** The distilled operation. */
36 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
37 /** IAM actions granted on the accelerator ARN. */
38 actions: readonly string[];
39 }) =>
40 Effect.gen(function* () {
41 const bind = yield* AWSBindings.Bind;
42 const op = yield* options.operation.pipe(withGaRegion, AWSBindings.withCredentials);
43
44 return Effect.fn(function* (accelerator: Accelerator) {
45 const AcceleratorArn = yield* accelerator.acceleratorArn;
46 if (!globalThis.__ALCHEMY_RUNTIME__) {
47 yield* bind`${options.tag}(${accelerator})`({
48 policyStatements: [
49 {
50 Effect: "Allow",
51 Action: [...options.actions],
52 Resource: [accelerator.acceleratorArn],
53 },
54 ],
55 });
56 }
57 return Effect.fn(`${options.tag}(${accelerator.LogicalId})`)(function* (
58 request?: Omit<I, "AcceleratorArn">,
59 ) {
60 const acceleratorArn = yield* AcceleratorArn;
61 // The region must also be pinned at the call site: the yield-time
62 // snapshot is only a fallback — the calling fiber's ambient Region
63 // (the host Function's own region) wins over it.
64 return yield* withGaRegion(op({ ...request, AcceleratorArn: acceleratorArn } as I));
65 });
66 });
67 });
68
69 /**
70 * Build the impl Effect for a Global Accelerator operation scoped to an
71 * {@link EndpointGroup}: the deploy-time half grants `actions` on the bound
72 * endpoint group's ARN, and the runtime half injects the group's
73 * `EndpointGroupArn` into every request.
74 */
75 export const makeGaEndpointGroupHttpBinding = <
76 I extends { EndpointGroupArn: string },
77 A,
78 E,
79 R,
80 >(options: {
81 /** Fully-qualified binding tag, e.g. `AWS.GlobalAccelerator.AddEndpoints`. */
82 tag: string;
83 /** The distilled operation. */
84 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
85 /** IAM actions granted on the endpoint group ARN. */
86 actions: readonly string[];
87 /**
88 * Additional statements granted alongside the group-scoped one (e.g. the
89 * `ec2:Describe*` / `elasticloadbalancing:DescribeLoadBalancers` reads
90 * Global Accelerator performs on the caller's behalf when endpoints are
91 * added).
92 */
93 extraStatements?: readonly PolicyStatement[];
94 }) =>
95 Effect.gen(function* () {
96 const bind = yield* AWSBindings.Bind;
97 const op = yield* options.operation.pipe(withGaRegion, AWSBindings.withCredentials);
98
99 return Effect.fn(function* (endpointGroup: EndpointGroup) {
100 const EndpointGroupArn = yield* endpointGroup.endpointGroupArn;
101 if (!globalThis.__ALCHEMY_RUNTIME__) {
102 yield* bind`${options.tag}(${endpointGroup})`({
103 policyStatements: [
104 {
105 Effect: "Allow",
106 Action: [...options.actions],
107 Resource: [endpointGroup.endpointGroupArn],
108 },
109 ...(options.extraStatements ?? []),
110 ],
111 });
112 }
113 return Effect.fn(`${options.tag}(${endpointGroup.LogicalId})`)(function* (
114 request: Omit<I, "EndpointGroupArn">,
115 ) {
116 const endpointGroupArn = yield* EndpointGroupArn;
117 // Call-site region pin — see makeGaAcceleratorHttpBinding above.
118 return yield* withGaRegion(
119 op({
120 ...request,
121 EndpointGroupArn: endpointGroupArn,
122 } as I),
123 );
124 });
125 });
126 });