@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, 04:45 UTC

main
ts 111 lines 4.32 KB
Raw
1 import * as Namespace from "alchemy/Namespace";
2 import type lambda from "aws-lambda";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import { processBatch } from "../internal/EventSourceBatch.ts";
6 import type { Stream as KinesisStream } from "../Kinesis/Stream.ts";
7 import {
8 StreamEventSource as KinesisStreamEventSource,
9 type StreamEventSourceProps,
10 type StreamEventSourceService,
11 type StreamRecordsHandler,
12 } from "../Kinesis/StreamEventSource.ts";
13 import { EventSourceMapping } from "./EventSourceMapping.ts";
14 import * as Lambda from "./Function.ts";
15
16 export const isKinesisStreamEvent = (event: any): event is lambda.KinesisStreamEvent =>
17 Array.isArray(event?.Records) &&
18 event.Records.length > 0 &&
19 event.Records[0].eventSource === "aws:kinesis";
20
21 /** @binding */
22 export const StreamEventSource = Layer.effect(
23 KinesisStreamEventSource,
24 Effect.gen(function* () {
25 const host = yield* Lambda.eventSourceHost("AWS.Lambda.StreamEventSource");
26 const Mapping = yield* EventSourceMapping;
27
28 return Effect.fn(function* <E = never, Req = never>(
29 stream: KinesisStream,
30 props: StreamEventSourceProps,
31 process: StreamRecordsHandler<E, Req>,
32 ) {
33 const StreamArn = yield* stream.streamArn;
34
35 // Deploy-time: grant IAM and create the event-source mapping. Skipped once
36 // running inside the deployed Function (the global guard), where the only
37 // work is registering the runtime handler below. Namespaced under the host
38 // so the mapping's logical identity matches the previous Binding.Policy.
39 if (!globalThis.__ALCHEMY_RUNTIME__) {
40 yield* Namespace.push(
41 host.LogicalId,
42 Effect.gen(function* () {
43 yield* host.bind`Allow(${host}, AWS.Lambda.StreamEventSource(${stream}))`({
44 policyStatements: [
45 {
46 Effect: "Allow",
47 Action: [
48 "kinesis:DescribeStream",
49 "kinesis:GetRecords",
50 "kinesis:GetShardIterator",
51 "kinesis:ListShards",
52 ],
53 Resource: [stream.streamArn],
54 },
55 ],
56 });
57
58 yield* Mapping(
59 `AWS.Lambda.EventSourceMapping(${host.LogicalId}, ${stream.LogicalId})`,
60 {
61 functionName: Lambda.qualifiedNameOf(host),
62 eventSourceArn: stream.streamArn,
63 batchSize: props.batchSize,
64 maximumBatchingWindow: props.maximumBatchingWindow,
65 enabled: true,
66 startingPosition: props.startingPosition ?? "LATEST",
67 startingPositionTimestamp: props.startingPositionTimestamp,
68 parallelizationFactor: props.parallelizationFactor,
69 bisectBatchOnFunctionError: props.bisectBatchOnFunctionError,
70 maximumRecordAge: props.maximumRecordAge,
71 maximumRetryAttempts: props.maximumRetryAttempts,
72 tumblingWindow: props.tumblingWindow,
73 functionResponseTypes: props.functionResponseTypes,
74 destinationConfig: props.destinationConfig,
75 filterCriteria: props.filterCriteria,
76 kmsKeyArn: props.kmsKeyArn,
77 metricsConfig: props.metricsConfig,
78 },
79 );
80 }),
81 );
82 }
83
84 yield* host.listen(
85 Effect.gen(function* () {
86 const streamArn = yield* StreamArn;
87
88 return (event: any) => {
89 if (isKinesisStreamEvent(event)) {
90 const records = event.Records.filter(
91 (record) => record.eventSourceARN?.startsWith(streamArn) === true,
92 );
93 if (records.length > 0) {
94 return process({ Records: records }).pipe(
95 Effect.map((response) => response ?? { batchItemFailures: [] }),
96 processBatch({
97 system: "aws_kinesis",
98 destination: streamArn.split("/")[1] ?? streamArn,
99 count: records.length,
100 }),
101 // A failure fails the invocation, so Lambda retries the whole batch.
102 Effect.orDie,
103 );
104 }
105 }
106 };
107 }),
108 );
109 }) as StreamEventSourceService;
110 }),
111 );