@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 81 lines 3.1 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 { sanitizeRuleName } from "../IoT/internal.ts";
6 import { TopicRule } from "../IoT/TopicRule.ts";
7 import {
8 type IoTMessage,
9 TopicRuleEventSource as IoTTopicRuleEventSource,
10 type TopicRuleEventSourceProps,
11 type TopicRuleEventSourceService,
12 } from "../IoT/TopicRuleEventSource.ts";
13 import * as Lambda from "./Function.ts";
14 import { Permission as LambdaPermission } from "./Permission.ts";
15
16 /**
17 * An IoT rule action delivers the raw SQL `SELECT` payload as the Lambda
18 * event — an arbitrary JSON object. We discriminate it from the Records-based
19 * AWS events (SQS/S3/SNS/DynamoDB) and from HTTP invocations (function URL /
20 * API Gateway / ALB, which carry `requestContext`) so a single function can
21 * host an IoT source alongside its HTTP handler.
22 */
23 export const isIoTMessage = (event: any): event is IoTMessage =>
24 event != null &&
25 typeof event === "object" &&
26 !Array.isArray(event) &&
27 !("Records" in event) &&
28 !("requestContext" in event);
29
30 /** @binding */
31 export const TopicRuleEventSource = Layer.effect(
32 IoTTopicRuleEventSource,
33 // @effect-diagnostics-next-line missingEffectContext:off
34 Effect.gen(function* () {
35 const host = yield* Lambda.eventSourceHost("AWS.Lambda.TopicRuleEventSource");
36 const Rule = yield* TopicRule;
37 const Permission = yield* LambdaPermission;
38
39 return Effect.fn(function* <Req = never>(
40 topicFilter: string,
41 props: TopicRuleEventSourceProps,
42 process: (stream: Stream.Stream<IoTMessage>) => Effect.Effect<void, never, Req>,
43 ) {
44 // Deploy-time: create the IoT topic rule with a Lambda action targeting
45 // this function, and grant iot.amazonaws.com permission to invoke it
46 // (scoped to the rule ARN). Skipped once running inside the deployed
47 // function, where the only work is registering the runtime handler.
48 if (!globalThis.__ALCHEMY_RUNTIME__) {
49 yield* Namespace.push(
50 host.LogicalId,
51 Effect.gen(function* () {
52 const ruleName = props.ruleName ?? sanitizeRuleName(`${host.LogicalId}_${topicFilter}`);
53 const rule = yield* Rule(`${host.LogicalId}-TopicRule`, {
54 ruleName,
55 sql: props.sql ?? `SELECT * FROM '${topicFilter}'`,
56 awsIotSqlVersion: props.awsIotSqlVersion,
57 actions: [{ lambda: { functionArn: Lambda.qualifiedArnOf(host) } }],
58 });
59
60 yield* Permission(`AWS.IoT.TopicRuleInvoke(${ruleName})`, {
61 action: "lambda:InvokeFunction",
62 functionName: Lambda.qualifiedNameOf(host),
63 principal: "iot.amazonaws.com",
64 sourceArn: rule.ruleArn,
65 });
66 }),
67 );
68 }
69
70 yield* host.listen(
71 Effect.gen(function* () {
72 return (event: any) => {
73 if (isIoTMessage(event)) {
74 return process(Stream.fromArray([event])).pipe(Effect.orDie);
75 }
76 };
77 }),
78 );
79 }) as TopicRuleEventSourceService;
80 }),
81 );