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

main
ts 184 lines 6.91 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 { AWSEnvironment } from "../Environment.ts";
5 import type { Thing } from "./Thing.ts";
6
7 /**
8 * Shared scaffolding for AWS IoT HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every near-identical `{Op}Http.ts` in this
11 * service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of
12 * the builders below. Everything except the operation, the IAM action list,
13 * and the identifier scope (account / thing / topic filter / client filter)
14 * is boilerplate.
15 */
16
17 const currentEnvironment = Effect.gen(function* () {
18 const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
19 accountId: string;
20 region: string;
21 }>;
22 return { accountId, region };
23 });
24
25 /**
26 * Build the impl Effect for an account-level operation (`DescribeEndpoint`,
27 * `ListThings`, `ListRetainedMessages`): the runtime callable passes the
28 * caller's request through unchanged and the deploy-time half grants
29 * `actions` on `*` (these IoT actions do not support resource-level
30 * permissions).
31 */
32 export const makeIotAccountHttpBinding = <I, A, E, R>(options: {
33 /** Fully-qualified binding tag, e.g. `AWS.IoT.DescribeEndpoint`. */
34 tag: string;
35 /** The distilled operation, invoked with the caller's request as-is. */
36 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
37 /** IAM actions granted on `*`. */
38 actions: readonly string[];
39 }) =>
40 Effect.gen(function* () {
41 const bind = yield* AWSBindings.Bind;
42 const op = yield* AWSBindings.withCredentials(options.operation);
43
44 return Effect.fn(function* () {
45 if (!globalThis.__ALCHEMY_RUNTIME__) {
46 yield* bind`${options.tag}()`({
47 policyStatements: [
48 {
49 Effect: "Allow",
50 Action: [...options.actions],
51 Resource: ["*"],
52 },
53 ],
54 });
55 }
56 return Effect.fn(options.tag)(function* (request?: I) {
57 return yield* op((request ?? {}) as I);
58 });
59 });
60 });
61
62 /**
63 * Build the impl Effect for a thing-scoped operation (device shadows,
64 * `DescribeThing`): the runtime callable injects the bound {@link Thing}'s
65 * physical name as `thingName` and the deploy-time half grants `actions` on
66 * the thing ARN.
67 */
68 export const makeIotThingHttpBinding = <I extends { thingName: string }, A, E, R>(options: {
69 /** Fully-qualified binding tag, e.g. `AWS.IoT.GetThingShadow`. */
70 tag: string;
71 /** The distilled operation; `thingName` is injected from the thing. */
72 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
73 /** IAM actions granted on the thing ARN. */
74 actions: readonly string[];
75 }) =>
76 Effect.gen(function* () {
77 const bind = yield* AWSBindings.Bind;
78 const op = yield* AWSBindings.withCredentials(options.operation);
79
80 return Effect.fn(function* (thing: Thing) {
81 const thingName = yield* thing.thingName;
82 if (!globalThis.__ALCHEMY_RUNTIME__) {
83 yield* bind`${options.tag}(${thing})`({
84 policyStatements: [
85 {
86 Effect: "Allow",
87 Action: [...options.actions],
88 // `thing/<name>` authorizes the classic shadow and the
89 // registry; `thing/<name>/*` authorizes NAMED shadows,
90 // whose IAM resource is `thing/<thingName>/<shadowName>`.
91 Resource: [thing.thingArn, Output.interpolate`${thing.thingArn}/*`],
92 },
93 ],
94 });
95 }
96 return Effect.fn(`${options.tag}(${thing.LogicalId})`)(function* (
97 request?: Omit<I, "thingName">,
98 ) {
99 return yield* op({
100 ...request,
101 thingName: yield* thingName,
102 } as I);
103 });
104 });
105 });
106
107 /**
108 * Build the impl Effect for a topic-scoped operation (`Publish`,
109 * `GetRetainedMessage`): the binding takes an MQTT topic filter, the
110 * deploy-time half grants `actions` on the matching topic ARN
111 * (`arn:aws:iot:{region}:{account}:topic/{filter}`, or all topics when the
112 * filter is omitted), and the runtime callable passes the caller's request
113 * (which carries the concrete `topic`) through unchanged.
114 */
115 export const makeIotTopicHttpBinding = <I, A, E, R>(options: {
116 /** Fully-qualified binding tag, e.g. `AWS.IoT.Publish`. */
117 tag: string;
118 /** The distilled operation, invoked with the caller's request as-is. */
119 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
120 /** IAM actions granted on the topic-filter ARN. */
121 actions: readonly string[];
122 }) =>
123 Effect.gen(function* () {
124 const bind = yield* AWSBindings.Bind;
125 const op = yield* AWSBindings.withCredentials(options.operation);
126
127 return Effect.fn(function* (topicFilter?: string) {
128 if (!globalThis.__ALCHEMY_RUNTIME__) {
129 const { accountId, region } = yield* currentEnvironment;
130 yield* bind`${options.tag}(${topicFilter ?? "*"})`({
131 policyStatements: [
132 {
133 Effect: "Allow",
134 Action: [...options.actions],
135 Resource: [`arn:aws:iot:${region}:${accountId}:topic/${topicFilter ?? "*"}`],
136 },
137 ],
138 });
139 }
140 return Effect.fn(`${options.tag}(${topicFilter ?? "*"})`)(function* (request: I) {
141 return yield* op(request);
142 });
143 });
144 });
145
146 /**
147 * Build the impl Effect for a client-scoped MQTT connection operation
148 * (`GetConnection`, `DeleteConnection`, `ListSubscriptions`,
149 * `SendDirectMessage`): the binding takes a client id filter, the
150 * deploy-time half grants `actions` on the matching client ARN
151 * (`arn:aws:iot:{region}:{account}:client/{filter}`, or all clients when the
152 * filter is omitted), and the runtime callable passes the caller's request
153 * (which carries the concrete `clientId`) through unchanged.
154 */
155 export const makeIotClientHttpBinding = <I, A, E, R>(options: {
156 /** Fully-qualified binding tag, e.g. `AWS.IoT.GetConnection`. */
157 tag: string;
158 /** The distilled operation, invoked with the caller's request as-is. */
159 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
160 /** IAM actions granted on the client-filter ARN. */
161 actions: readonly string[];
162 }) =>
163 Effect.gen(function* () {
164 const bind = yield* AWSBindings.Bind;
165 const op = yield* AWSBindings.withCredentials(options.operation);
166
167 return Effect.fn(function* (clientIdFilter?: string) {
168 if (!globalThis.__ALCHEMY_RUNTIME__) {
169 const { accountId, region } = yield* currentEnvironment;
170 yield* bind`${options.tag}(${clientIdFilter ?? "*"})`({
171 policyStatements: [
172 {
173 Effect: "Allow",
174 Action: [...options.actions],
175 Resource: [`arn:aws:iot:${region}:${accountId}:client/${clientIdFilter ?? "*"}`],
176 },
177 ],
178 });
179 }
180 return Effect.fn(`${options.tag}(${clientIdFilter ?? "*"})`)(function* (request: I) {
181 return yield* op(request);
182 });
183 });
184 });