The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Channel } from "./Channel.ts"; |
| 5 | import type { ChannelGroup } from "./ChannelGroup.ts"; |
| 6 | import type { OriginEndpoint } from "./OriginEndpoint.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Shared scaffolding for AWS Elemental MediaPackage v2 HTTP bindings. |
| 10 | * |
| 11 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 12 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the three |
| 13 | * builders below. Everything except the operation and the IAM action list |
| 14 | * is boilerplate: each builder injects the bound resource's identifying |
| 15 | * names (`ChannelGroupName` / `ChannelName` / `OriginEndpointName`) into |
| 16 | * every request and grants `actions` on the resource's ARN. |
| 17 | * |
| 18 | * Harvest-job operations authorize against the *harvest job* ARN, which is |
| 19 | * the origin endpoint ARN with a `/harvestJob/{name}` suffix — set |
| 20 | * `harvestJobScoped` to grant on that pattern instead of the endpoint ARN. |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Build the impl Effect for a MediaPackage v2 operation scoped to a |
| 25 | * {@link ChannelGroup}: the runtime callable injects the group's |
| 26 | * `ChannelGroupName` and the deploy-time half grants `actions` on the |
| 27 | * group ARN (plus everything beneath it — channels, endpoints, harvest |
| 28 | * jobs — for list operations that enumerate child resources). |
| 29 | */ |
| 30 | export const makeMediaPackageV2ChannelGroupHttpBinding = < |
| 31 | I extends { ChannelGroupName: string }, |
| 32 | A, |
| 33 | E, |
| 34 | R, |
| 35 | >(options: { |
| 36 | /** Fully-qualified binding tag, e.g. `AWS.MediaPackageV2.ListHarvestJobs`. */ |
| 37 | tag: string; |
| 38 | /** The distilled operation; `ChannelGroupName` is injected from the group. */ |
| 39 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 40 | /** IAM actions granted on the group ARN and its child-resource pattern. */ |
| 41 | actions: readonly string[]; |
| 42 | }) => |
| 43 | Effect.gen(function* () { |
| 44 | const bind = yield* AWSBindings.Bind; |
| 45 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 46 | |
| 47 | return Effect.fn(function* <G extends ChannelGroup>(group: G) { |
| 48 | const ChannelGroupName = yield* group.channelGroupName; |
| 49 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 50 | yield* bind`${options.tag}(${group})`({ |
| 51 | policyStatements: [ |
| 52 | { |
| 53 | Effect: "Allow", |
| 54 | Action: [...options.actions], |
| 55 | Resource: [ |
| 56 | Output.interpolate`${group.channelGroupArn}`, |
| 57 | Output.interpolate`${group.channelGroupArn}/*`, |
| 58 | ], |
| 59 | }, |
| 60 | ], |
| 61 | }); |
| 62 | } |
| 63 | return Effect.fn(`${options.tag}(${group.LogicalId})`)(function* ( |
| 64 | request?: Omit<I, "ChannelGroupName">, |
| 65 | ) { |
| 66 | return yield* op({ |
| 67 | ...request, |
| 68 | ChannelGroupName: yield* ChannelGroupName, |
| 69 | } as I); |
| 70 | }); |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | /** |
| 75 | * Build the impl Effect for a MediaPackage v2 operation scoped to a |
| 76 | * {@link Channel}: the runtime callable injects the channel's |
| 77 | * `ChannelGroupName` + `ChannelName` and the deploy-time half grants |
| 78 | * `actions` on the channel ARN. |
| 79 | */ |
| 80 | export const makeMediaPackageV2ChannelHttpBinding = < |
| 81 | I extends { ChannelGroupName: string; ChannelName: string }, |
| 82 | A, |
| 83 | E, |
| 84 | R, |
| 85 | >(options: { |
| 86 | /** Fully-qualified binding tag, e.g. `AWS.MediaPackageV2.ResetChannelState`. */ |
| 87 | tag: string; |
| 88 | /** The distilled operation; the channel's names are injected. */ |
| 89 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 90 | /** IAM actions granted on the channel ARN. */ |
| 91 | actions: readonly string[]; |
| 92 | }) => |
| 93 | Effect.gen(function* () { |
| 94 | const bind = yield* AWSBindings.Bind; |
| 95 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 96 | |
| 97 | return Effect.fn(function* <C extends Channel>(channel: C) { |
| 98 | const ChannelGroupName = yield* channel.channelGroupName; |
| 99 | const ChannelName = yield* channel.channelName; |
| 100 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 101 | yield* bind`${options.tag}(${channel})`({ |
| 102 | policyStatements: [ |
| 103 | { |
| 104 | Effect: "Allow", |
| 105 | Action: [...options.actions], |
| 106 | Resource: [Output.interpolate`${channel.channelArn}`], |
| 107 | }, |
| 108 | ], |
| 109 | }); |
| 110 | } |
| 111 | return Effect.fn(`${options.tag}(${channel.LogicalId})`)(function* ( |
| 112 | request?: Omit<I, "ChannelGroupName" | "ChannelName">, |
| 113 | ) { |
| 114 | return yield* op({ |
| 115 | ...request, |
| 116 | ChannelGroupName: yield* ChannelGroupName, |
| 117 | ChannelName: yield* ChannelName, |
| 118 | } as I); |
| 119 | }); |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | /** |
| 124 | * Build the impl Effect for a MediaPackage v2 operation scoped to an |
| 125 | * {@link OriginEndpoint}: the runtime callable injects the endpoint's |
| 126 | * `ChannelGroupName` + `ChannelName` + `OriginEndpointName` and the |
| 127 | * deploy-time half grants `actions` on the endpoint ARN — plus, when |
| 128 | * `harvestJobScoped` is set, on the endpoint's harvest-job ARN pattern |
| 129 | * (`{endpointArn}/harvestJob/*`) and the parent channel-group ARN: |
| 130 | * MediaPackage authorizes `CreateHarvestJob` against the bare channel |
| 131 | * group (observed live: "not authorized to perform |
| 132 | * mediapackagev2:CreateHarvestJob on resource: arn:…:channelGroup/{g}"). |
| 133 | */ |
| 134 | export const makeMediaPackageV2OriginEndpointHttpBinding = < |
| 135 | I extends { |
| 136 | ChannelGroupName: string; |
| 137 | ChannelName: string; |
| 138 | OriginEndpointName: string; |
| 139 | }, |
| 140 | A, |
| 141 | E, |
| 142 | R, |
| 143 | >(options: { |
| 144 | /** Fully-qualified binding tag, e.g. `AWS.MediaPackageV2.CreateHarvestJob`. */ |
| 145 | tag: string; |
| 146 | /** The distilled operation; the endpoint's names are injected. */ |
| 147 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 148 | /** IAM actions granted on the endpoint (or harvest-job pattern) ARN. */ |
| 149 | actions: readonly string[]; |
| 150 | /** Additionally grant on `{endpointArn}/harvestJob/*` (harvest-job ops). */ |
| 151 | harvestJobScoped?: boolean; |
| 152 | }) => |
| 153 | Effect.gen(function* () { |
| 154 | const bind = yield* AWSBindings.Bind; |
| 155 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 156 | |
| 157 | return Effect.fn(function* <O extends OriginEndpoint>(endpoint: O) { |
| 158 | const ChannelGroupName = yield* endpoint.channelGroupName; |
| 159 | const ChannelName = yield* endpoint.channelName; |
| 160 | const OriginEndpointName = yield* endpoint.originEndpointName; |
| 161 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 162 | yield* bind`${options.tag}(${endpoint})`({ |
| 163 | policyStatements: [ |
| 164 | { |
| 165 | Effect: "Allow", |
| 166 | Action: [...options.actions], |
| 167 | Resource: [ |
| 168 | Output.interpolate`${endpoint.originEndpointArn}`, |
| 169 | ...(options.harvestJobScoped |
| 170 | ? [ |
| 171 | Output.interpolate`${endpoint.originEndpointArn}/harvestJob/*`, |
| 172 | // The endpoint ARN is `…:channelGroup/{g}/channel/{c}/originEndpoint/{e}`; |
| 173 | // harvest-job actions authorize against the bare |
| 174 | // channel-group ARN prefix. |
| 175 | Output.map( |
| 176 | endpoint.originEndpointArn, |
| 177 | (arn): string => arn.split("/channel/")[0]!, |
| 178 | ), |
| 179 | ] |
| 180 | : []), |
| 181 | ], |
| 182 | }, |
| 183 | ], |
| 184 | }); |
| 185 | } |
| 186 | return Effect.fn(`${options.tag}(${endpoint.LogicalId})`)(function* ( |
| 187 | request?: Omit<I, "ChannelGroupName" | "ChannelName" | "OriginEndpointName">, |
| 188 | ) { |
| 189 | return yield* op({ |
| 190 | ...request, |
| 191 | ChannelGroupName: yield* ChannelGroupName, |
| 192 | ChannelName: yield* ChannelName, |
| 193 | OriginEndpointName: yield* OriginEndpointName, |
| 194 | } as I); |
| 195 | }); |
| 196 | }); |
| 197 | }); |