@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 101 lines 3.86 KB
Raw
1 import * as Namespace from "alchemy/Namespace";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Stream from "effect/Stream";
5 import type { Broker } from "../MQ/Broker.ts";
6 import {
7 BrokerEventSource as MQBrokerEventSource,
8 type BrokerEventSourceProps,
9 type MQEvent,
10 type MQMessage,
11 } from "../MQ/BrokerEventSource.ts";
12 import { EventSourceMapping } from "./EventSourceMapping.ts";
13 import * as Lambda from "./Function.ts";
14
15 /** Narrow an incoming Lambda event to an Amazon MQ (ActiveMQ/RabbitMQ) event. */
16 export const isMQEvent = (event: any): event is MQEvent =>
17 event?.eventSource === "aws:mq" || event?.eventSource === "aws:rmq";
18
19 /** Flatten an MQ event into a flat list of messages across all queues. */
20 const messagesOf = (event: MQEvent): MQMessage[] =>
21 event.eventSource === "aws:mq"
22 ? (event.messages ?? [])
23 : Object.values(event.rmqMessagesByQueue ?? {}).flat();
24
25 /** @binding */
26 export const BrokerEventSource = Layer.effect(
27 MQBrokerEventSource,
28 // @ts-expect-error - the impl resolves plan-time services (EventSourceMapping)
29 // whereas BrokerEventSourceService erases the requirement channel to `never`.
30 // @effect-diagnostics-next-line missingEffectContext:off
31 Effect.gen(function* () {
32 const host = yield* Lambda.eventSourceHost("AWS.Lambda.BrokerEventSource");
33 const Mapping = yield* EventSourceMapping;
34
35 return Effect.fn(function* <Req = never>(
36 broker: Broker,
37 props: BrokerEventSourceProps,
38 process: (stream: Stream.Stream<MQMessage>) => Effect.Effect<void, never, Req>,
39 ) {
40 // Deploy-time: grant IAM and create the event-source mapping. Skipped
41 // once running inside the deployed Function (the global guard).
42 // Namespaced under the host so the mapping's logical identity is stable.
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 yield* Namespace.push(
45 host.LogicalId,
46 Effect.gen(function* () {
47 yield* host.bind`Allow(${host}, AWS.MQ.BrokerEventSource(${broker}))`({
48 policyStatements: [
49 {
50 Effect: "Allow",
51 Action: ["mq:DescribeBroker"],
52 Resource: [broker.brokerArn],
53 },
54 {
55 Effect: "Allow",
56 Action: ["secretsmanager:GetSecretValue"],
57 Resource: [props.credentialsSecretArn],
58 },
59 {
60 // MQ event sources run in the broker's VPC; the poller
61 // manages elastic network interfaces on the function's
62 // behalf. These EC2 actions have no resource-level scoping.
63 Effect: "Allow",
64 Action: [
65 "ec2:CreateNetworkInterface",
66 "ec2:DeleteNetworkInterface",
67 "ec2:DescribeNetworkInterfaces",
68 "ec2:DescribeSecurityGroups",
69 "ec2:DescribeSubnets",
70 "ec2:DescribeVpcs",
71 ],
72 Resource: ["*"],
73 },
74 ],
75 });
76
77 yield* Mapping(`${broker.LogicalId}-EventSource`, {
78 functionName: Lambda.qualifiedNameOf(host),
79 eventSourceArn: broker.brokerArn,
80 queues: props.queues,
81 sourceAccessConfigurations: [{ Type: "BASIC_AUTH", URI: props.credentialsSecretArn }],
82 batchSize: props.batchSize ?? 100,
83 maximumBatchingWindow: props.maximumBatchingWindow,
84 enabled: props.enabled ?? true,
85 });
86 }),
87 );
88 }
89
90 yield* host.listen(
91 Effect.gen(function* () {
92 return (event: any) => {
93 if (isMQEvent(event)) {
94 return process(Stream.fromArray(messagesOf(event))).pipe(Effect.orDie);
95 }
96 };
97 }),
98 );
99 });
100 }),
101 );