@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, 06:00 UTC

main
ts 125 lines 4.66 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import { Region } from "../Region.ts";
4 import type { PublicRepository } from "./Repository.ts";
5
6 /**
7 * Shared HTTP scaffolding for the Amazon ECR Public runtime 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 builders
11 * below. Everything except the operation, the IAM action list, and (for the
12 * repository-scoped builder) the injected repository name is boilerplate.
13 *
14 * Amazon ECR Public is a single global registry served exclusively from the
15 * `us-east-1` endpoint, so every operation is resolved with the Region
16 * pinned via {@link pinEcrPublic} — exactly like the resource provider in
17 * `Repository.ts`.
18 *
19 * Per the `ecr-public` service authorization reference, repository-scoped
20 * actions authorize on the repository ARN, while registry-level actions
21 * (`GetAuthorizationToken`, `DescribeRegistries`, `GetRegistryCatalogData`)
22 * support no repository resource type, so the registry builder grants on
23 * `Resource: ["*"]`.
24 */
25
26 const US_EAST_1 = "us-east-1";
27
28 /** Pin a distilled `ecr-public` effect to the service's home region. */
29 export const pinEcrPublic = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
30 effect.pipe(Effect.provideService(Region, Effect.succeed(US_EAST_1)));
31
32 /**
33 * Build the impl Effect for an operation scoped to one
34 * {@link PublicRepository}. The runtime callable injects the bound
35 * repository's name as the request's `repositoryName`; the deploy-time half
36 * grants `iamActions` on the repository's ARN.
37 */
38 export const makePublicRepositoryHttpBinding = <
39 I extends { repositoryName: string },
40 A,
41 E,
42 R,
43 >(options: {
44 /**
45 * Short capability name used in the binding sid and runtime span, e.g.
46 * `"DescribeImages"`.
47 */
48 capability: string;
49 /** IAM actions granted on the repository ARN. */
50 iamActions: readonly string[];
51 /** The distilled operation; `repositoryName` is injected. */
52 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
53 }) =>
54 Effect.gen(function* () {
55 const bind = yield* AWSBindings.Bind;
56 const op = yield* options.operation.pipe(pinEcrPublic, AWSBindings.withCredentials);
57
58 return Effect.fn(function* (repository: PublicRepository) {
59 const RepositoryName = yield* repository.repositoryName;
60 if (!globalThis.__ALCHEMY_RUNTIME__) {
61 yield* bind`AWS.ECRPublic.${options.capability}(${repository})`({
62 policyStatements: [
63 {
64 Effect: "Allow",
65 Action: [...options.iamActions],
66 Resource: [repository.repositoryArn],
67 },
68 ],
69 });
70 }
71 return Effect.fn(`AWS.ECRPublic.${options.capability}(${repository.LogicalId})`)(function* (
72 request?: Omit<I, "repositoryName">,
73 ) {
74 // The region must also be pinned at the call site: the yield-time
75 // snapshot is only a fallback — the calling fiber's ambient Region
76 // (the host Function's own region) wins over it.
77 return yield* pinEcrPublic(
78 op({
79 ...request,
80 repositoryName: yield* RepositoryName,
81 } as unknown as I),
82 );
83 });
84 });
85 });
86
87 /**
88 * Build the impl Effect for a registry-level ECR Public operation
89 * (`GetAuthorizationToken`, `DescribeRegistries`, `GetRegistryCatalogData` —
90 * none of which are repository-scoped, so the grant is on
91 * `Resource: ["*"]`).
92 */
93 export const makePublicRegistryHttpBinding = <I extends object, A, E, R>(options: {
94 /**
95 * Short capability name used in the binding sid and runtime span, e.g.
96 * `"GetAuthorizationToken"`.
97 */
98 capability: string;
99 /** IAM actions granted on `Resource: ["*"]`. */
100 iamActions: readonly string[];
101 /** The distilled operation implementing the capability. */
102 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
103 }) =>
104 Effect.gen(function* () {
105 const bind = yield* AWSBindings.Bind;
106 const op = yield* options.operation.pipe(pinEcrPublic, AWSBindings.withCredentials);
107
108 return Effect.fn(function* () {
109 if (!globalThis.__ALCHEMY_RUNTIME__) {
110 yield* bind`AWS.ECRPublic.${options.capability}()`({
111 policyStatements: [
112 {
113 Effect: "Allow",
114 Action: [...options.iamActions],
115 Resource: ["*"],
116 },
117 ],
118 });
119 }
120 return Effect.fn(`AWS.ECRPublic.${options.capability}`)(function* (request?: I) {
121 // Call-site region pin — see makePublicRepositoryHttpBinding above.
122 return yield* pinEcrPublic(op((request ?? {}) as I));
123 });
124 });
125 });