The best way to control Claude Code, Codex and more from any computer or phone

main
ts 118 lines 4.52 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { PlaybackConfiguration } from "./PlaybackConfiguration.ts";
5
6 /**
7 * Shared HTTP scaffolding for the AWS Elemental MediaTailor runtime
8 * bindings.
9 *
10 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
11 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
12 * builders below. Everything except the operation, the identifier resolver,
13 * and the IAM action list is boilerplate.
14 */
15
16 /**
17 * Builder for account-level MediaTailor bindings (channel assembly control
18 * and reads). Channel/program names are runtime request parameters, so the
19 * deploy-time half grants `iamActions` on `Resource: ["*"]` and the runtime
20 * callable passes the request through unchanged.
21 */
22 export const makeMediaTailorHttpBinding = <I, A, E, R>(options: {
23 /** Short capability name used in the binding sid and runtime span. */
24 capability: string;
25 /** IAM actions granted on `Resource: ["*"]`. */
26 iamActions: readonly string[];
27 /** The distilled operation implementing the capability. */
28 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
29 }) =>
30 Effect.gen(function* () {
31 const bind = yield* AWSBindings.Bind;
32 const op = yield* AWSBindings.withCredentials(options.operation);
33
34 return Effect.fn(function* () {
35 if (!globalThis.__ALCHEMY_RUNTIME__) {
36 yield* bind`AWS.MediaTailor.${options.capability}()`({
37 policyStatements: [
38 {
39 Effect: "Allow",
40 Action: [...options.iamActions],
41 // Channel-assembly resource names are runtime parameters;
42 // their ARNs are unknowable at deploy time.
43 Resource: ["*"],
44 },
45 ],
46 });
47 }
48 return Effect.fn(`AWS.MediaTailor.${options.capability}`)(function* (request: I) {
49 return yield* op(request);
50 });
51 });
52 });
53
54 /**
55 * Builder for bindings scoped to a {@link PlaybackConfiguration} (the
56 * prefetch-schedule data plane). The runtime callable injects the bound
57 * configuration's name as `PlaybackConfigurationName`, and the deploy-time
58 * half grants `iamActions` on the configuration's ARN and the derived
59 * `…:prefetchSchedule/{name}/*` ARN space.
60 */
61 export const makeMediaTailorPlaybackHttpBinding = <
62 I extends { PlaybackConfigurationName: string },
63 A,
64 E,
65 R,
66 >(options: {
67 /** Short capability name used in the binding sid and runtime span. */
68 capability: string;
69 /** IAM actions granted on the configuration + prefetch-schedule ARNs. */
70 iamActions: readonly string[];
71 /**
72 * The distilled operation; `PlaybackConfigurationName` is injected from
73 * the bound configuration.
74 */
75 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
76 }) =>
77 Effect.gen(function* () {
78 const bind = yield* AWSBindings.Bind;
79 const op = yield* AWSBindings.withCredentials(options.operation);
80
81 return Effect.fn(function* (config: PlaybackConfiguration) {
82 const name = yield* config.name;
83 if (!globalThis.__ALCHEMY_RUNTIME__) {
84 yield* bind`AWS.MediaTailor.${options.capability}(${config})`({
85 policyStatements: [
86 {
87 Effect: "Allow",
88 Action: [...options.iamActions],
89 Resource: [
90 Output.interpolate`${config.playbackConfigurationArn}`,
91 // Prefetch schedules live in a sibling ARN space:
92 // arn:…:prefetchSchedule/{configurationName}/{name}
93 Output.map(
94 config.playbackConfigurationArn,
95 (arn) => `${arn.replace(":playbackConfiguration/", ":prefetchSchedule/")}/*`,
96 ),
97 // ListPrefetchSchedules is authorized against the bare
98 // account/region wildcard `…:prefetchSchedule/*` (the
99 // configuration name is not part of the authorization
100 // resource), so every prefetch binding also grants it.
101 Output.map(config.playbackConfigurationArn, (arn) =>
102 arn.replace(/:playbackConfiguration\/.*$/, ":prefetchSchedule/*"),
103 ),
104 ],
105 },
106 ],
107 });
108 }
109 return Effect.fn(`AWS.MediaTailor.${options.capability}(${config.LogicalId})`)(function* (
110 request: Omit<I, "PlaybackConfigurationName">,
111 ) {
112 return yield* op({
113 ...request,
114 PlaybackConfigurationName: yield* name,
115 } as I);
116 });
117 });
118 });