@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 122 lines 4.6 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 { Queue } from "./Queue.ts";
5
6 /**
7 * Shared scaffolding for AWS SQS 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 `QueueSink`, the dual-queue `StartMessageMoveTask`) stay
14 * bespoke.
15 */
16
17 const GrantOnQueue = Effect.gen(function* () {
18 const bind = yield* AWSBindings.Bind;
19 return Effect.fn(function* (tag: string, queue: Queue, actions: readonly string[]) {
20 if (!globalThis.__ALCHEMY_RUNTIME__) {
21 yield* bind`${tag}(${queue})`({
22 policyStatements: [
23 {
24 Effect: "Allow",
25 Action: [...actions],
26 Resource: [Output.interpolate`${queue.queueArn}`],
27 },
28 ],
29 });
30 }
31 });
32 });
33
34 /**
35 * Build the impl Effect for a queue-scoped operation whose request carries
36 * the queue identity as `QueueUrl` (`SendMessage`, `ReceiveMessage`,
37 * `PurgeQueue`, …): the runtime callable injects the bound {@link Queue}'s
38 * URL and the deploy-time half grants `actions` on the queue's ARN.
39 */
40 export const makeQueueUrlHttpBinding = <I extends { QueueUrl?: string }, A, E, R>(options: {
41 /** Fully-qualified binding tag, e.g. `AWS.SQS.SendMessage`. */
42 tag: string;
43 /** The distilled operation; the queue URL is injected as `QueueUrl`. */
44 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
45 /** IAM actions granted on the queue ARN. */
46 actions: readonly string[];
47 }) =>
48 Effect.gen(function* () {
49 const grantOnQueue = yield* GrantOnQueue;
50 const op = yield* AWSBindings.withCredentials(options.operation);
51
52 return Effect.fn(function* (queue: Queue) {
53 const QueueUrl = yield* queue.queueUrl;
54 yield* grantOnQueue(options.tag, queue, options.actions);
55 return Effect.fn(`${options.tag}(${queue.LogicalId})`)(function* (
56 request?: Omit<I, "QueueUrl">,
57 ) {
58 return yield* op({
59 ...request,
60 QueueUrl: yield* QueueUrl,
61 } as I);
62 });
63 });
64 });
65
66 /**
67 * Build the impl Effect for a dead-letter-queue-scoped message-move-task
68 * operation whose request carries the queue identity as `SourceArn`
69 * (`ListMessageMoveTasks`): the runtime callable injects the bound
70 * {@link Queue}'s ARN and the deploy-time half grants `actions` on it.
71 */
72 export const makeQueueArnHttpBinding = <I extends { SourceArn?: string }, A, E, R>(options: {
73 /** Fully-qualified binding tag, e.g. `AWS.SQS.ListMessageMoveTasks`. */
74 tag: string;
75 /** The distilled operation; the queue ARN is injected as `SourceArn`. */
76 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
77 /** IAM actions granted on the queue ARN. */
78 actions: readonly string[];
79 }) =>
80 Effect.gen(function* () {
81 const grantOnQueue = yield* GrantOnQueue;
82 const op = yield* AWSBindings.withCredentials(options.operation);
83
84 return Effect.fn(function* (queue: Queue) {
85 const SourceArn = yield* queue.queueArn;
86 yield* grantOnQueue(options.tag, queue, options.actions);
87 return Effect.fn(`${options.tag}(${queue.LogicalId})`)(function* (
88 request?: Omit<I, "SourceArn">,
89 ) {
90 return yield* op({
91 ...request,
92 SourceArn: yield* SourceArn,
93 } as I);
94 });
95 });
96 });
97
98 /**
99 * Build the impl Effect for a queue-scoped operation whose request carries
100 * no queue identity at all (`CancelMessageMoveTask`): the runtime callable
101 * passes the caller's request through unchanged while the deploy-time half
102 * still grants `actions` on the bound {@link Queue}'s ARN.
103 */
104 export const makeQueueGrantHttpBinding = <I, A, E, R>(options: {
105 /** Fully-qualified binding tag, e.g. `AWS.SQS.CancelMessageMoveTask`. */
106 tag: string;
107 /** The distilled operation, invoked with the caller's request as-is. */
108 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
109 /** IAM actions granted on the queue ARN. */
110 actions: readonly string[];
111 }) =>
112 Effect.gen(function* () {
113 const grantOnQueue = yield* GrantOnQueue;
114 const op = yield* AWSBindings.withCredentials(options.operation);
115
116 return Effect.fn(function* (queue: Queue) {
117 yield* grantOnQueue(options.tag, queue, options.actions);
118 return Effect.fn(`${options.tag}(${queue.LogicalId})`)(function* (request: I) {
119 return yield* op(request);
120 });
121 });
122 });