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

main
ts 149 lines 5.83 KB
Raw
1 import { Endpoint } from "@distilled.cloud/aws";
2 import * as kv from "@distilled.cloud/aws/kinesis-video";
3 import * as Effect from "effect/Effect";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import { discoverDataEndpoint, discoverSignalingEndpoint } from "./internal.ts";
6 import type { SignalingChannel } from "./SignalingChannel.ts";
7 import type { Stream } from "./Stream.ts";
8
9 /**
10 * Shared scaffolding for AWS Kinesis Video HTTP bindings.
11 *
12 * NOT exported from `index.ts` — every near-identical `{Op}Http.ts` in this
13 * service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of
14 * the builders below. Kinesis Video data-plane calls are two-step: discover
15 * the per-stream data endpoint (`GetDataEndpoint`) or per-channel signaling
16 * endpoint (`GetSignalingChannelEndpoint`), then issue the signed data-plane
17 * call against that endpoint. Everything except the operation, the endpoint
18 * `APIName`/protocol, and the IAM action is boilerplate.
19 */
20
21 /**
22 * Build the impl Effect for a stream-scoped media/archived-media operation:
23 * the runtime callable resolves the per-stream data endpoint for `apiName`
24 * (cached), injects the bound {@link Stream}'s ARN as `StreamARN`, and the
25 * deploy-time half grants `kinesisvideo:GetDataEndpoint` + `actions` on the
26 * stream's ARN.
27 */
28 export const makeStreamMediaHttpBinding = <
29 I extends { StreamARN?: string; StreamName?: string },
30 A,
31 E,
32 R,
33 >(options: {
34 /** Fully-qualified binding tag, e.g. `AWS.KinesisVideo.GetClip`. */
35 tag: string;
36 /** The `GetDataEndpoint` API name the operation is served under. */
37 apiName: kv.APIName;
38 /** IAM actions granted on the stream ARN (GetDataEndpoint is implied). */
39 actions: readonly string[];
40 /** The distilled operation; `StreamARN` is injected from the stream. */
41 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
42 }) =>
43 Effect.gen(function* () {
44 const bind = yield* AWSBindings.Bind;
45 // Yield-first captures the operations' services (Credentials/Region/
46 // HttpClient) at layer init so the runtime callable is requirement-free.
47 const getDataEndpoint = yield* AWSBindings.withCredentials(kv.getDataEndpoint);
48 const op = yield* AWSBindings.withCredentials(options.operation);
49
50 return Effect.fn(function* (stream: Stream) {
51 const StreamArn = yield* stream.streamArn;
52 if (!globalThis.__ALCHEMY_RUNTIME__) {
53 yield* bind`${options.tag}(${stream})`({
54 policyStatements: [
55 {
56 Effect: "Allow",
57 Action: [
58 // the data plane requires per-stream endpoint discovery
59 "kinesisvideo:GetDataEndpoint",
60 ...options.actions,
61 ],
62 Resource: [stream.streamArn],
63 },
64 ],
65 });
66 }
67 return Effect.fn(`${options.tag}(${stream.LogicalId})`)(function* (
68 request?: Omit<I, "StreamARN" | "StreamName">,
69 ) {
70 const streamArn = yield* StreamArn;
71 const endpoint = yield* discoverDataEndpoint(streamArn, options.apiName, getDataEndpoint);
72 return yield* op({ ...request, StreamARN: streamArn } as I).pipe(
73 Effect.provideService(Endpoint.Endpoint, Effect.succeed(endpoint)),
74 );
75 });
76 });
77 });
78
79 /**
80 * Build the impl Effect for a channel-scoped signaling/WebRTC-storage
81 * operation: the runtime callable resolves the per-channel endpoint for
82 * `protocol` + `role` (cached), injects the bound {@link SignalingChannel}'s
83 * ARN under `key`, and the deploy-time half grants
84 * `kinesisvideo:GetSignalingChannelEndpoint` + `actions` on the channel ARN.
85 */
86 export const makeChannelSignalingHttpBinding = <
87 K extends "ChannelARN" | "channelArn",
88 I extends { [P in K]?: string },
89 A,
90 E,
91 R,
92 >(options: {
93 /** Fully-qualified binding tag, e.g. `AWS.KinesisVideo.JoinStorageSession`. */
94 tag: string;
95 /** The signaling endpoint protocol the operation is served under. */
96 protocol: kv.ChannelProtocol;
97 /** The peer role the endpoint is resolved for. */
98 role: kv.ChannelRole;
99 /**
100 * IAM actions granted on the channel ARN (GetSignalingChannelEndpoint is
101 * implied).
102 */
103 actions: readonly string[];
104 /** The request field the channel ARN is injected under. */
105 key: K;
106 /** The distilled operation; the channel ARN is injected under `key`. */
107 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
108 }) =>
109 Effect.gen(function* () {
110 const bind = yield* AWSBindings.Bind;
111 // Yield-first captures the operations' services (Credentials/Region/
112 // HttpClient) at layer init so the runtime callable is requirement-free.
113 const getSignalingChannelEndpoint = yield* AWSBindings.withCredentials(
114 kv.getSignalingChannelEndpoint,
115 );
116 const op = yield* AWSBindings.withCredentials(options.operation);
117
118 return Effect.fn(function* (channel: SignalingChannel) {
119 const ChannelArn = yield* channel.channelArn;
120 if (!globalThis.__ALCHEMY_RUNTIME__) {
121 yield* bind`${options.tag}(${channel})`({
122 policyStatements: [
123 {
124 Effect: "Allow",
125 Action: [
126 // the data plane requires per-channel endpoint discovery
127 "kinesisvideo:GetSignalingChannelEndpoint",
128 ...options.actions,
129 ],
130 Resource: [channel.channelArn],
131 },
132 ],
133 });
134 }
135 return Effect.fn(`${options.tag}(${channel.LogicalId})`)(function* (request?: Omit<I, K>) {
136 const channelArn = yield* ChannelArn;
137 const endpoint = yield* discoverSignalingEndpoint(
138 channelArn,
139 options.protocol,
140 options.role,
141 getSignalingChannelEndpoint,
142 );
143 return yield* op({
144 ...request,
145 [options.key]: channelArn,
146 } as I).pipe(Effect.provideService(Endpoint.Endpoint, Effect.succeed(endpoint)));
147 });
148 });
149 });