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

main
ts 143 lines 5.09 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { Stream } from "./Stream.ts";
4 import type { StreamConsumer } from "./StreamConsumer.ts";
5
6 /**
7 * Shared scaffolding for AWS Kinesis HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every near-identical `{Op}Http.ts` in this
10 * service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of
11 * the builders below. Everything except the operation, the IAM action list,
12 * and the injected identifier is boilerplate. Genuinely-different bindings
13 * (the batched `StreamSink`, the `Stream | StreamConsumer` polymorphic
14 * `ListTagsForResource`) stay bespoke.
15 */
16
17 /**
18 * Build the impl Effect for an account-level operation (`ListStreams`,
19 * `DescribeLimits`, `DescribeAccountSettings`): the runtime callable passes
20 * the caller's request through unchanged and the deploy-time half grants
21 * `actions` on `*` (these Kinesis actions do not support resource-level
22 * permissions).
23 */
24 export const makeKinesisAccountHttpBinding = <I, A, E, R>(options: {
25 /** Fully-qualified binding tag, e.g. `AWS.Kinesis.ListStreams`. */
26 tag: string;
27 /** The distilled operation, invoked with the caller's request as-is. */
28 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
29 /** IAM actions granted on `*`. */
30 actions: readonly string[];
31 }) =>
32 Effect.gen(function* () {
33 const bind = yield* AWSBindings.Bind;
34 const op = yield* AWSBindings.withCredentials(options.operation);
35
36 return Effect.fn(function* () {
37 if (!globalThis.__ALCHEMY_RUNTIME__) {
38 yield* bind`${options.tag}()`({
39 policyStatements: [
40 {
41 Effect: "Allow",
42 Action: [...options.actions],
43 Resource: ["*"],
44 },
45 ],
46 });
47 }
48 return Effect.fn(options.tag)(function* (request?: I) {
49 return yield* op((request ?? {}) as I);
50 });
51 });
52 });
53
54 /**
55 * Build the impl Effect for a stream-scoped operation: the runtime callable
56 * injects the bound {@link Stream}'s identity under `key` (`StreamARN` for
57 * the describe/shard APIs, `StreamName` for the producer APIs, `ResourceARN`
58 * for the policy APIs) and the deploy-time half grants `actions` on the
59 * stream's ARN.
60 */
61 export const makeStreamHttpBinding = <
62 K extends "StreamARN" | "StreamName" | "ResourceARN",
63 I extends { [P in K]?: string },
64 A,
65 E,
66 R,
67 >(options: {
68 /** Fully-qualified binding tag, e.g. `AWS.Kinesis.DescribeStream`. */
69 tag: string;
70 /** The distilled operation; the stream identity is injected under `key`. */
71 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
72 /** IAM actions granted on the stream ARN. */
73 actions: readonly string[];
74 /** The request field the stream identity is injected under. */
75 key: K;
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* (stream: Stream) {
82 const identity = yield* options.key === "StreamName" ? stream.streamName : stream.streamArn;
83 if (!globalThis.__ALCHEMY_RUNTIME__) {
84 yield* bind`${options.tag}(${stream})`({
85 policyStatements: [
86 {
87 Effect: "Allow",
88 Action: [...options.actions],
89 Resource: [stream.streamArn],
90 },
91 ],
92 });
93 }
94 return Effect.fn(`${options.tag}(${stream.LogicalId})`)(function* (request?: Omit<I, K>) {
95 return yield* op({
96 ...request,
97 [options.key]: yield* identity,
98 } as I);
99 });
100 });
101 });
102
103 /**
104 * Build the impl Effect for a consumer-scoped operation
105 * (`DescribeStreamConsumer`, `SubscribeToShard`): the runtime callable
106 * injects the bound {@link StreamConsumer}'s ARN as `ConsumerARN` and the
107 * deploy-time half grants `actions` on the consumer ARN.
108 */
109 export const makeConsumerHttpBinding = <I extends { ConsumerARN?: string }, A, E, R>(options: {
110 /** Fully-qualified binding tag, e.g. `AWS.Kinesis.SubscribeToShard`. */
111 tag: string;
112 /** The distilled operation; `ConsumerARN` is injected from the consumer. */
113 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
114 /** IAM actions granted on the consumer ARN. */
115 actions: readonly string[];
116 }) =>
117 Effect.gen(function* () {
118 const bind = yield* AWSBindings.Bind;
119 const op = yield* AWSBindings.withCredentials(options.operation);
120
121 return Effect.fn(function* (consumer: StreamConsumer) {
122 const ConsumerARN = yield* consumer.consumerArn;
123 if (!globalThis.__ALCHEMY_RUNTIME__) {
124 yield* bind`${options.tag}(${consumer})`({
125 policyStatements: [
126 {
127 Effect: "Allow",
128 Action: [...options.actions],
129 Resource: [consumer.consumerArn],
130 },
131 ],
132 });
133 }
134 return Effect.fn(`${options.tag}(${consumer.LogicalId})`)(function* (
135 request?: Omit<I, "ConsumerARN">,
136 ) {
137 return yield* op({
138 ...request,
139 ConsumerARN: yield* ConsumerARN,
140 } as I);
141 });
142 });
143 });