The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as AWSBindings from "../AWSBindings.ts"; |
| 3 | import type { Stage } from "./Stage.ts"; |
| 4 | |
| 5 | /** |
| 6 | * Shared scaffolding for Amazon IVS Real-Time HTTP bindings. |
| 7 | * |
| 8 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 9 | * a `Layer.effect(Cap, makeIvsRealtime…HttpBinding({ … }))` over one of the |
| 10 | * builders below. Everything except the operation, the IAM action list, and |
| 11 | * an optional request-shape mapper is boilerplate. |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Compositions are addressed by server-generated ARNs only known at runtime, |
| 16 | * so composition-plane grants use this wildcard. |
| 17 | */ |
| 18 | export const COMPOSITION_ARN_WILDCARD = "arn:aws:ivs:*:*:composition/*"; |
| 19 | |
| 20 | /** |
| 21 | * Encoder configurations referenced by a composition's destinations are |
| 22 | * runtime data, so StartComposition grants include this wildcard. |
| 23 | */ |
| 24 | export const ENCODER_CONFIGURATION_ARN_WILDCARD = "arn:aws:ivs:*:*:encoder-configuration/*"; |
| 25 | |
| 26 | /** |
| 27 | * Storage configurations referenced by a composition's S3 destinations are |
| 28 | * runtime data, so StartComposition grants include this wildcard. |
| 29 | */ |
| 30 | export const STORAGE_CONFIGURATION_ARN_WILDCARD = "arn:aws:ivs:*:*:storage-configuration/*"; |
| 31 | |
| 32 | /** |
| 33 | * Build the impl Effect for an IVS Real-Time operation scoped to a |
| 34 | * {@link Stage}: the deploy-time half grants `actions` on the bound stage's |
| 35 | * ARN (plus any `extraResources`), and the runtime half injects the stage's |
| 36 | * ARN under `requestKey` (default `stageArn`; `ListParticipantReplicas` |
| 37 | * addresses the stage as `sourceStageArn`). |
| 38 | * |
| 39 | * `prepare` (optional) maps a friendlier public request shape onto the wire |
| 40 | * request — e.g. `CreateParticipantToken` converts a `Duration.Input` into |
| 41 | * the wire `duration` minutes. It defaults to identity. |
| 42 | */ |
| 43 | export const makeIvsRealtimeStageHttpBinding = < |
| 44 | I extends { [P in K]: string }, |
| 45 | A, |
| 46 | E, |
| 47 | R, |
| 48 | K extends string = "stageArn", |
| 49 | Req = Omit<I, K>, |
| 50 | >(options: { |
| 51 | /** Fully-qualified binding tag, e.g. `AWS.IVSRealtime.CreateParticipantToken`. */ |
| 52 | tag: string; |
| 53 | /** The distilled operation. */ |
| 54 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 55 | /** IAM actions granted on the stage ARN. */ |
| 56 | actions: readonly string[]; |
| 57 | /** Request field carrying the bound stage's ARN. @default "stageArn" */ |
| 58 | requestKey?: K; |
| 59 | /** Static IAM resources granted in addition to the stage ARN. */ |
| 60 | extraResources?: readonly string[]; |
| 61 | /** Map the public request shape to the wire request (defaults to identity). */ |
| 62 | prepare?: (request: Req) => Omit<I, K>; |
| 63 | }) => |
| 64 | Effect.gen(function* () { |
| 65 | const bind = yield* AWSBindings.Bind; |
| 66 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 67 | |
| 68 | return Effect.fn(function* (stage: Stage) { |
| 69 | // Output yields a DEFERRED effect — resolve again per invocation below. |
| 70 | const StageArn = yield* stage.stageArn; |
| 71 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 72 | yield* bind`${options.tag}(${stage})`({ |
| 73 | policyStatements: [ |
| 74 | { |
| 75 | Effect: "Allow", |
| 76 | Action: [...options.actions], |
| 77 | Resource: [stage.stageArn, ...(options.extraResources ?? [])], |
| 78 | }, |
| 79 | ], |
| 80 | }); |
| 81 | } |
| 82 | return Effect.fn(`${options.tag}(${stage.LogicalId})`)(function* (request?: Req) { |
| 83 | const stageArn = yield* StageArn; |
| 84 | const wire = options.prepare |
| 85 | ? options.prepare((request ?? {}) as Req) |
| 86 | : ((request ?? {}) as unknown as Omit<I, K>); |
| 87 | return yield* op({ |
| 88 | ...wire, |
| 89 | [options.requestKey ?? "stageArn"]: stageArn, |
| 90 | } as I); |
| 91 | }); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | /** |
| 96 | * Build the impl Effect for an IVS Real-Time participant-replication |
| 97 | * operation: the runtime callable is bound to a **source** and a |
| 98 | * **destination** {@link Stage}, injects both ARNs, and the deploy-time half |
| 99 | * grants `actions` on both stage ARNs. |
| 100 | */ |
| 101 | export const makeIvsRealtimeReplicationHttpBinding = < |
| 102 | I extends { sourceStageArn: string; destinationStageArn: string }, |
| 103 | A, |
| 104 | E, |
| 105 | R, |
| 106 | >(options: { |
| 107 | /** Fully-qualified binding tag, e.g. `AWS.IVSRealtime.StartParticipantReplication`. */ |
| 108 | tag: string; |
| 109 | /** The distilled operation; both stage ARNs are injected. */ |
| 110 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 111 | /** IAM actions granted on both stage ARNs. */ |
| 112 | actions: readonly string[]; |
| 113 | }) => |
| 114 | Effect.gen(function* () { |
| 115 | const bind = yield* AWSBindings.Bind; |
| 116 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 117 | |
| 118 | return Effect.fn(function* (sourceStage: Stage, destinationStage: Stage) { |
| 119 | const SourceStageArn = yield* sourceStage.stageArn; |
| 120 | const DestinationStageArn = yield* destinationStage.stageArn; |
| 121 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 122 | yield* bind`${options.tag}(${sourceStage}, ${destinationStage})`({ |
| 123 | policyStatements: [ |
| 124 | { |
| 125 | Effect: "Allow", |
| 126 | Action: [...options.actions], |
| 127 | Resource: [sourceStage.stageArn, destinationStage.stageArn], |
| 128 | }, |
| 129 | ], |
| 130 | }); |
| 131 | } |
| 132 | return Effect.fn(`${options.tag}(${sourceStage.LogicalId}, ${destinationStage.LogicalId})`)( |
| 133 | function* (request: Omit<I, "sourceStageArn" | "destinationStageArn">) { |
| 134 | return yield* op({ |
| 135 | ...request, |
| 136 | sourceStageArn: yield* SourceStageArn, |
| 137 | destinationStageArn: yield* DestinationStageArn, |
| 138 | } as I); |
| 139 | }, |
| 140 | ); |
| 141 | }); |
| 142 | }); |
| 143 | |
| 144 | /** |
| 145 | * Build the impl Effect for an account-level IVS Real-Time operation |
| 146 | * (composition management — compositions are addressed by server-generated |
| 147 | * ARNs that are runtime data). The deploy-time half grants `actions` on |
| 148 | * `resources` (default `*`). |
| 149 | */ |
| 150 | export const makeIvsRealtimeAccountHttpBinding = <I, A, E, R>(options: { |
| 151 | /** Fully-qualified binding tag, e.g. `AWS.IVSRealtime.ListCompositions`. */ |
| 152 | tag: string; |
| 153 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 154 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 155 | /** IAM actions granted. */ |
| 156 | actions: readonly string[]; |
| 157 | /** |
| 158 | * IAM resources the actions are granted on. |
| 159 | * @default ["*"] |
| 160 | */ |
| 161 | resources?: readonly string[]; |
| 162 | }) => |
| 163 | Effect.gen(function* () { |
| 164 | const bind = yield* AWSBindings.Bind; |
| 165 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 166 | |
| 167 | return Effect.fn(function* () { |
| 168 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 169 | yield* bind`${options.tag}()`({ |
| 170 | policyStatements: [ |
| 171 | { |
| 172 | Effect: "Allow", |
| 173 | Action: [...options.actions], |
| 174 | Resource: [...(options.resources ?? ["*"])], |
| 175 | }, |
| 176 | ], |
| 177 | }); |
| 178 | } |
| 179 | return Effect.fn(options.tag)(function* (request?: I) { |
| 180 | return yield* op((request ?? {}) as I); |
| 181 | }); |
| 182 | }); |
| 183 | }); |