@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 325 lines 9.36 KB
Raw
1 import type * as pipes from "@distilled.cloud/aws/pipes";
2 import { toWireSeconds } from "alchemy/Util/Duration";
3 import type * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import type { Table } from "../DynamoDB/Table.ts";
6 import * as IAM from "../IAM/index.ts";
7 import type { Stream } from "../Kinesis/Stream.ts";
8 import type { Function } from "../Lambda/Function.ts";
9 import type { Queue } from "../SQS/Queue.ts";
10 import { Pipe, type PipeDesiredState } from "./Pipe.ts";
11
12 /**
13 * Resources the {@link from} builder accepts as a pipe source. DynamoDB
14 * tables must have streams enabled (`latestStreamArn` defined).
15 */
16 export type PipeSource = Queue | Stream | Table;
17
18 export interface PipeSourceOptions {
19 /**
20 * Maximum number of source records per delivered batch.
21 */
22 batchSize?: number;
23 /**
24 * Maximum time to gather records before delivering a batch (e.g.
25 * `"30 seconds"` or `Duration.seconds(30)`). Sent to the API in whole
26 * seconds.
27 */
28 maximumBatchingWindow?: Duration.Input;
29 /**
30 * Where to start reading a Kinesis or DynamoDB stream source. Ignored
31 * for SQS sources. Changing it triggers a replacement.
32 * @default "LATEST"
33 */
34 startingPosition?: "TRIM_HORIZON" | "LATEST";
35 }
36
37 export interface PipeTargetOptions {
38 /**
39 * Explicit physical pipe name. Also used as the logical ID, so it must
40 * be deterministic. If omitted, a logical ID is derived from the source
41 * and target logical IDs and the physical name is auto-generated.
42 */
43 name?: string;
44 /**
45 * A description of the pipe.
46 */
47 description?: string;
48 /**
49 * The state the pipe should be in.
50 * @default "RUNNING"
51 */
52 desiredState?: PipeDesiredState;
53 /**
54 * Input template applied to each record before delivery to the target.
55 */
56 inputTemplate?: string;
57 }
58
59 export interface LambdaTargetOptions extends PipeTargetOptions {
60 /**
61 * How the target Lambda function is invoked.
62 * @default "REQUEST_RESPONSE"
63 */
64 invocationType?: "REQUEST_RESPONSE" | "FIRE_AND_FORGET";
65 }
66
67 export interface QueueTargetOptions extends PipeTargetOptions {
68 /**
69 * Message group ID for FIFO target queues.
70 */
71 messageGroupId?: string;
72 /**
73 * Message deduplication ID for FIFO target queues.
74 */
75 messageDeduplicationId?: string;
76 }
77
78 interface PipeBuilderState {
79 source: PipeSource;
80 options: PipeSourceOptions;
81 filters: string[];
82 enrichment?: {
83 fn: Function;
84 parameters?: pipes.PipeEnrichmentParameters;
85 };
86 }
87
88 /**
89 * Start building an EventBridge Pipe from a source resource. The terminal
90 * `.toLambda(...)` / `.toQueue(...)` call synthesizes the
91 * `pipes.amazonaws.com` execution role — source-read plus target-invoke
92 * (plus enrichment-invoke) policies scoped to the exact resource ARNs —
93 * and yields the {@link Pipe}.
94 *
95 * ```typescript
96 * const pipe = yield* AWS.Pipes.from(queue, { batchSize: 1 })
97 * .filter(JSON.stringify({ body: { type: ["order.created"] } }))
98 * .toLambda(fn);
99 * ```
100 */
101 export const from = (source: PipeSource, options: PipeSourceOptions = {}) =>
102 makePipeBuilder({ source, options, filters: [] });
103
104 const makePipeBuilder = (state: PipeBuilderState) => ({
105 /**
106 * Add EventBridge event-pattern filters. Only source events matching at
107 * least one pattern reach the enrichment/target. Accepts pattern strings
108 * or plain objects (JSON-stringified).
109 */
110 filter: (...patterns: (string | Record<string, unknown>)[]) =>
111 makePipeBuilder({
112 ...state,
113 filters: [
114 ...state.filters,
115 ...patterns.map((pattern) =>
116 typeof pattern === "string" ? pattern : JSON.stringify(pattern),
117 ),
118 ],
119 }),
120
121 /**
122 * Enrich each batch with a Lambda function before delivery to the
123 * target. The synthesized role is granted `lambda:InvokeFunction` on the
124 * enrichment function.
125 */
126 enrich: (fn: Function, parameters?: pipes.PipeEnrichmentParameters) =>
127 makePipeBuilder({ ...state, enrichment: { fn, parameters } }),
128
129 /**
130 * Deliver batches to a Lambda function target.
131 */
132 toLambda: (fn: Function, options: LambdaTargetOptions = {}) =>
133 materializePipe(
134 state,
135 fn.LogicalId,
136 [
137 {
138 Effect: "Allow",
139 Action: ["lambda:InvokeFunction"],
140 Resource: [fn.functionArn],
141 },
142 ],
143 fn.functionArn,
144 {
145 InputTemplate: options.inputTemplate,
146 LambdaFunctionParameters: options.invocationType
147 ? { InvocationType: options.invocationType }
148 : undefined,
149 },
150 options,
151 ),
152
153 /**
154 * Deliver batches to an SQS queue target.
155 */
156 toQueue: (queue: Queue, options: QueueTargetOptions = {}) =>
157 materializePipe(
158 state,
159 queue.LogicalId,
160 [
161 {
162 Effect: "Allow",
163 Action: ["sqs:SendMessage"],
164 Resource: [queue.queueArn],
165 },
166 ],
167 queue.queueArn,
168 {
169 InputTemplate: options.inputTemplate,
170 SqsQueueParameters:
171 options.messageGroupId !== undefined || options.messageDeduplicationId !== undefined
172 ? {
173 MessageGroupId: options.messageGroupId,
174 MessageDeduplicationId: options.messageDeduplicationId,
175 }
176 : undefined,
177 },
178 options,
179 ),
180 });
181
182 interface SourceSpec {
183 arn: unknown;
184 statements: unknown[];
185 parameters: pipes.PipeSourceParameters;
186 }
187
188 const sourceSpec = (state: PipeBuilderState): SourceSpec => {
189 const { source, options, filters } = state;
190 // The wire field is whole seconds.
191 const maximumBatchingWindowInSeconds = toWireSeconds(options.maximumBatchingWindow);
192 const filterCriteria: Pick<pipes.PipeSourceParameters, "FilterCriteria"> =
193 filters.length > 0
194 ? { FilterCriteria: { Filters: filters.map((Pattern) => ({ Pattern })) } }
195 : {};
196 switch (source.Type) {
197 case "AWS.SQS.Queue":
198 return {
199 arn: source.queueArn,
200 statements: [
201 {
202 Effect: "Allow",
203 Action: ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"],
204 Resource: [source.queueArn],
205 },
206 ],
207 parameters: {
208 ...filterCriteria,
209 SqsQueueParameters: {
210 BatchSize: options.batchSize,
211 MaximumBatchingWindowInSeconds: maximumBatchingWindowInSeconds,
212 },
213 },
214 };
215 case "AWS.Kinesis.Stream":
216 return {
217 arn: source.streamArn,
218 statements: [
219 {
220 Effect: "Allow",
221 Action: [
222 "kinesis:DescribeStream",
223 "kinesis:DescribeStreamSummary",
224 "kinesis:GetRecords",
225 "kinesis:GetShardIterator",
226 "kinesis:ListShards",
227 "kinesis:ListStreams",
228 ],
229 Resource: [source.streamArn],
230 },
231 ],
232 parameters: {
233 ...filterCriteria,
234 KinesisStreamParameters: {
235 StartingPosition: options.startingPosition ?? "LATEST",
236 BatchSize: options.batchSize,
237 MaximumBatchingWindowInSeconds: maximumBatchingWindowInSeconds,
238 },
239 },
240 };
241 case "AWS.DynamoDB.Table":
242 return {
243 arn: source.latestStreamArn,
244 statements: [
245 {
246 Effect: "Allow",
247 Action: [
248 "dynamodb:DescribeStream",
249 "dynamodb:GetRecords",
250 "dynamodb:GetShardIterator",
251 "dynamodb:ListStreams",
252 ],
253 Resource: [source.latestStreamArn],
254 },
255 ],
256 parameters: {
257 ...filterCriteria,
258 DynamoDBStreamParameters: {
259 StartingPosition: options.startingPosition ?? "LATEST",
260 BatchSize: options.batchSize,
261 MaximumBatchingWindowInSeconds: maximumBatchingWindowInSeconds,
262 },
263 },
264 };
265 }
266 };
267
268 const materializePipe = (
269 state: PipeBuilderState,
270 targetId: string,
271 targetStatements: unknown[],
272 targetArn: unknown,
273 targetParameters: pipes.PipeTargetParameters,
274 options: PipeTargetOptions,
275 ) =>
276 Effect.gen(function* () {
277 const src = sourceSpec(state);
278 const pipeId = options.name ?? `${state.source.LogicalId}To${targetId}Pipe`;
279
280 const enrichmentStatements = state.enrichment
281 ? [
282 {
283 Effect: "Allow",
284 Action: ["lambda:InvokeFunction"],
285 Resource: [state.enrichment.fn.functionArn],
286 },
287 ]
288 : [];
289
290 const role = yield* IAM.Role(`${pipeId}Role`, {
291 assumeRolePolicyDocument: {
292 Version: "2012-10-17",
293 Statement: [
294 {
295 Effect: "Allow",
296 Principal: { Service: "pipes.amazonaws.com" },
297 Action: ["sts:AssumeRole"],
298 },
299 ],
300 },
301 inlinePolicies: {
302 PipeSource: {
303 Version: "2012-10-17",
304 Statement: src.statements as any,
305 },
306 PipeTarget: {
307 Version: "2012-10-17",
308 Statement: [...targetStatements, ...enrichmentStatements] as any,
309 },
310 },
311 });
312
313 return yield* Pipe(pipeId, {
314 pipeName: options.name,
315 description: options.description,
316 desiredState: options.desiredState,
317 source: src.arn as any,
318 sourceParameters: src.parameters,
319 enrichment: state.enrichment ? (state.enrichment.fn.functionArn as any) : undefined,
320 enrichmentParameters: state.enrichment?.parameters,
321 target: targetArn as any,
322 targetParameters,
323 roleArn: role.roleArn,
324 });
325 });