@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 77 lines 2.72 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
5 /**
6 * Shared HTTP scaffolding for the AWS Elemental MediaConvert runtime
7 * bindings.
8 *
9 * MediaConvert jobs are created *at runtime* with server-assigned ids, so
10 * their ARNs are unknowable at deploy time — every binding is account-level
11 * and grants its action(s) on `Resource: ["*"]`. The only variation between
12 * bindings is the distilled operation, the IAM action, and whether the
13 * operation passes an S3-access role to the service (`CreateJob`, which
14 * additionally needs `iam:PassRole` conditioned to
15 * `mediaconvert.amazonaws.com`).
16 *
17 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
18 * a `Layer.effect(Cap, makeMediaConvertHttpBinding({ … }))`.
19 */
20 export const makeMediaConvertHttpBinding = <I, A, E, R>(options: {
21 /**
22 * Short capability name used in the binding sid and runtime span, e.g.
23 * `"CreateJob"`.
24 */
25 capability: string;
26 /**
27 * IAM actions granted on `Resource: ["*"]` (job ARNs are server-assigned
28 * at runtime and unknowable at deploy time).
29 */
30 iamActions: readonly string[];
31 /**
32 * The distilled operation implementing the capability.
33 */
34 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
35 /**
36 * Grant `iam:PassRole` (conditioned to `mediaconvert.amazonaws.com`) so
37 * the function can hand the service the role it assumes to read the input
38 * from and write the output to S3. Set on `CreateJob`.
39 */
40 passRole?: boolean;
41 }) =>
42 Effect.gen(function* () {
43 const bind = yield* AWSBindings.Bind;
44 const op = yield* AWSBindings.withCredentials(options.operation);
45
46 return Effect.fn(function* () {
47 if (!globalThis.__ALCHEMY_RUNTIME__) {
48 const policyStatements: PolicyStatement[] = [
49 {
50 Effect: "Allow",
51 Action: [...options.iamActions],
52 // Job ARNs are server-assigned at runtime; queue/preset/template
53 // references inside a job are resolved by the service.
54 Resource: ["*"],
55 },
56 ];
57 if (options.passRole) {
58 policyStatements.push({
59 Effect: "Allow",
60 Action: ["iam:PassRole"],
61 Resource: ["*"],
62 Condition: {
63 StringEquals: {
64 "iam:PassedToService": "mediaconvert.amazonaws.com",
65 },
66 },
67 });
68 }
69 yield* bind`AWS.MediaConvert.${options.capability}()`({
70 policyStatements,
71 });
72 }
73 return Effect.fn(`AWS.MediaConvert.${options.capability}`)(function* (request?: I) {
74 return yield* op((request ?? {}) as I);
75 });
76 });
77 });