@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 152 lines 5.76 KB
Raw
1 import type { Credentials } from "@distilled.cloud/aws/Credentials";
2 import { Region as AwsRegion } from "@distilled.cloud/aws/Region";
3 import * as Effect from "effect/Effect";
4 import type * as HttpClient from "effect/http/HttpClient";
5 import * as AWSBindings from "../AWSBindings.ts";
6 import type { Certificate } from "./Certificate.ts";
7
8 /**
9 * Shared HTTP scaffolding for the ACM runtime bindings.
10 *
11 * Every ACM capability follows the same shape — resolve the distilled
12 * operation, register an IAM policy statement on the binding host, and return
13 * a runtime callable that injects the certificate ARN. The only variation is
14 * the operation, the IAM action(s), and whether the binding is scoped to one
15 * certificate or to the whole account, so those are the only inputs.
16 *
17 * All calls are pinned to `us-east-1`, matching where the
18 * {@link Certificate} resource provider requests certificates (the region
19 * required for CloudFront viewer certificates).
20 *
21 * @internal — not exported from `index.ts`.
22 */
23
24 const ACM_REGION = "us-east-1" as const;
25
26 type AcmRequirements = Credentials | AwsRegion | HttpClient.HttpClient;
27
28 const withAcmRegion = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
29 // `AwsRegion`'s service value is an `Effect<RegionName>` (see
30 // `@distilled.cloud/aws/Region`), so it must be provided as an effect, not
31 // a bare string.
32 effect.pipe(Effect.provideService(AwsRegion, Effect.succeed(ACM_REGION)));
33
34 export interface AcmHttpBindingConfig<Req extends object, Out, Err> {
35 /**
36 * Short capability name used in the binding sid and runtime span, e.g.
37 * `"DescribeCertificate"`.
38 */
39 capability: string;
40 /**
41 * IAM actions granted to the binding host, e.g. `["acm:GetCertificate"]`.
42 */
43 iamActions: readonly string[];
44 /**
45 * The distilled ACM operation implementing the capability.
46 */
47 operation: Effect.Effect<
48 (input: Req & { CertificateArn: string }) => Effect.Effect<Out, Err>,
49 never,
50 AcmRequirements
51 >;
52 }
53
54 /**
55 * Build the implementation effect for a certificate-scoped ACM capability:
56 * `Layer.effect(Cap, makeAcmCertificateHttpBinding({ ... }))`.
57 *
58 * `Req` infers as the operation's *full* distilled request (inference against
59 * `Req & { CertificateArn: string }` captures the whole source type). The
60 * runtime callable injects the bound certificate's ARN, so its request type
61 * is `Omit<Req, "CertificateArn">` — matching the capability contracts, which
62 * declare `Omit<acm.XRequest, "CertificateArn">`.
63 */
64 export const makeAcmCertificateHttpBinding = <Req extends object, Out, Err>(
65 config: AcmHttpBindingConfig<Req, Out, Err>,
66 ) =>
67 Effect.gen(function* () {
68 const bind = yield* AWSBindings.Bind;
69 const op = yield* config.operation.pipe(withAcmRegion, AWSBindings.withCredentials);
70
71 return Effect.fn(function* (certificate: Certificate) {
72 const CertificateArn = yield* certificate.certificateArn;
73 if (!globalThis.__ALCHEMY_RUNTIME__) {
74 yield* bind`AWS.ACM.${config.capability}(${certificate})`({
75 policyStatements: [
76 {
77 Effect: "Allow",
78 Action: [...config.iamActions],
79 Resource: [certificate.certificateArn],
80 },
81 ],
82 });
83 }
84 return Effect.fn(`AWS.ACM.${config.capability}(${certificate.LogicalId})`)(function* (
85 request?: Omit<Req, "CertificateArn">,
86 ) {
87 // Sound: at instantiation `Req` always contains `CertificateArn: string`
88 // (every certificate-scoped distilled request does), so
89 // `Omit<Req, "CertificateArn"> & { CertificateArn: string }` is exactly
90 // `Req & { CertificateArn: string }`. TypeScript cannot prove this for
91 // an unresolved type parameter, hence the precise assertion.
92 //
93 // The region must be pinned HERE, at the call site: the yield-time
94 // snapshot is only a fallback — the calling fiber's ambient Region
95 // (the host Function's own region) wins over it, so pinning only at
96 // the yield would silently route the call to the wrong region.
97 return yield* withAcmRegion(
98 op({
99 ...request,
100 CertificateArn: yield* CertificateArn,
101 } as Req & { CertificateArn: string }),
102 );
103 });
104 });
105 });
106
107 export interface AcmAccountHttpBindingConfig<Req extends object, Out, Err> {
108 /**
109 * Short capability name used in the binding sid and runtime span, e.g.
110 * `"ListCertificates"`.
111 */
112 capability: string;
113 /**
114 * IAM actions granted to the binding host on `Resource: ["*"]` (ACM
115 * account-level operations are not resource-scoped).
116 */
117 iamActions: readonly string[];
118 /**
119 * The distilled ACM operation implementing the capability.
120 */
121 operation: Effect.Effect<(input: Req) => Effect.Effect<Out, Err>, never, AcmRequirements>;
122 }
123
124 /**
125 * Build the implementation effect for an account-level ACM capability (no
126 * certificate argument): `Layer.effect(Cap, makeAcmAccountHttpBinding({ ... }))`.
127 */
128 export const makeAcmAccountHttpBinding = <Req extends object, Out, Err>(
129 config: AcmAccountHttpBindingConfig<Req, Out, Err>,
130 ) =>
131 Effect.gen(function* () {
132 const bind = yield* AWSBindings.Bind;
133 const op = yield* config.operation.pipe(withAcmRegion, AWSBindings.withCredentials);
134
135 return Effect.fn(function* () {
136 if (!globalThis.__ALCHEMY_RUNTIME__) {
137 yield* bind`AWS.ACM.${config.capability}()`({
138 policyStatements: [
139 {
140 Effect: "Allow",
141 Action: [...config.iamActions],
142 Resource: ["*"],
143 },
144 ],
145 });
146 }
147 return Effect.fn(`AWS.ACM.${config.capability}`)(function* (request?: Req) {
148 // Call-site pin — see makeAcmCertificateHttpBinding above.
149 return yield* withAcmRegion(op(request ?? ({} as Req)));
150 });
151 });
152 });