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

main
ts 91 lines 3.25 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { Channel } from "./Channel.ts";
4
5 /**
6 * Shared scaffolding for Amazon IVS stream data-plane HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service
9 * is a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
10 * builders below. Everything except the operation and the IAM action list
11 * is boilerplate: channel-scoped bindings inject the bound channel's ARN
12 * as the request's `channelArn` and grant `actions` on that ARN;
13 * account-scoped bindings pass the request through and grant `actions` on
14 * `*`.
15 */
16
17 /**
18 * Build the impl Effect for an IVS operation scoped to a {@link Channel}:
19 * the deploy-time half grants `actions` on the bound channel's ARN, and
20 * the runtime half injects the channel's ARN into every request as
21 * `channelArn`.
22 */
23 export const makeIvsChannelHttpBinding = <I extends { channelArn: string }, A, E, R>(options: {
24 /** Fully-qualified binding tag, e.g. `AWS.IVS.GetStream`. */
25 tag: string;
26 /** The distilled operation; `channelArn` is injected from the channel. */
27 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
28 /** IAM actions granted on the channel ARN. */
29 actions: readonly string[];
30 }) =>
31 Effect.gen(function* () {
32 const bind = yield* AWSBindings.Bind;
33 const op = yield* AWSBindings.withCredentials(options.operation);
34
35 return Effect.fn(function* (channel: Channel) {
36 const ChannelArn = yield* channel.channelArn;
37 if (!globalThis.__ALCHEMY_RUNTIME__) {
38 yield* bind`${options.tag}(${channel})`({
39 policyStatements: [
40 {
41 Effect: "Allow",
42 Action: [...options.actions],
43 Resource: [channel.channelArn],
44 },
45 ],
46 });
47 }
48 return Effect.fn(`${options.tag}(${channel.LogicalId})`)(function* (
49 request?: Omit<I, "channelArn">,
50 ) {
51 const channelArn = yield* ChannelArn;
52 return yield* op({ ...request, channelArn } as I);
53 });
54 });
55 });
56
57 /**
58 * Build the impl Effect for an account-level IVS operation (e.g.
59 * enumerating the account's live streams). The deploy-time half grants
60 * `actions` on `*` — these list actions are not scoped to a single
61 * channel resource.
62 */
63 export const makeIvsAccountHttpBinding = <I, A, E, R>(options: {
64 /** Fully-qualified binding tag, e.g. `AWS.IVS.ListStreams`. */
65 tag: string;
66 /** The distilled operation, invoked with the caller's request as-is. */
67 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
68 /** IAM actions granted on `*`. */
69 actions: readonly string[];
70 }) =>
71 Effect.gen(function* () {
72 const bind = yield* AWSBindings.Bind;
73 const op = yield* AWSBindings.withCredentials(options.operation);
74
75 return Effect.fn(function* () {
76 if (!globalThis.__ALCHEMY_RUNTIME__) {
77 yield* bind`${options.tag}()`({
78 policyStatements: [
79 {
80 Effect: "Allow",
81 Action: [...options.actions],
82 Resource: ["*"],
83 },
84 ],
85 });
86 }
87 return Effect.fn(options.tag)(function* (request?: I) {
88 return yield* op((request ?? {}) as I);
89 });
90 });
91 });