@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 100 lines 3.78 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 { TopicRule } from "../IoT/TopicRule.ts";
6 import type { Destination } from "../IoTWireless/Destination.ts";
7 import {
8 DestinationEventSource as IoTWirelessDestinationEventSource,
9 type DestinationEventSourceService,
10 type WirelessUplinkHandlerFn,
11 type WirelessUplinkMessage,
12 } from "../IoTWireless/DestinationEventSource.ts";
13 import * as Lambda from "./Function.ts";
14 import { Permission as LambdaPermission } from "./Permission.ts";
15
16 /**
17 * An IoT Wireless uplink invocation — the envelope IoT Core for LoRaWAN
18 * delivers to a destination's rule: the base64 `PayloadData` plus the
19 * sending device's id.
20 */
21 export const isWirelessUplinkMessage = (event: any): event is WirelessUplinkMessage =>
22 event != null &&
23 typeof event === "object" &&
24 typeof event.WirelessDeviceId === "string" &&
25 typeof event.PayloadData === "string";
26
27 /**
28 * Connects an IoT Wireless {@link Destination}'s uplink traffic to the
29 * current Lambda function.
30 *
31 * At deploy time this layer creates the IoT topic rule named by the
32 * destination's `expression` (the destination must use
33 * `expressionType: "RuleName"`) with a Lambda action targeting this
34 * function, and grants `iot.amazonaws.com` permission to invoke it; at
35 * runtime it dispatches uplink invocations to the registered handler.
36 * ### Consuming wireless uplinks
37 * **Example:** Consume LoRaWAN uplinks
38 * ```typescript
39 * yield* IoTWireless.consumeUplinks(destination, (uplinks) =>
40 * uplinks.pipe(
41 * Stream.runForEach((uplink) => Effect.log(uplink.PayloadData)),
42 * Effect.orDie,
43 * ),
44 * );
45 * ```
46 *
47 * @binding
48 */
49 export const WirelessDestinationEventSource = Layer.effect(
50 IoTWirelessDestinationEventSource,
51 // @effect-diagnostics-next-line missingEffectContext:off
52 Effect.gen(function* () {
53 // this layer can only be used in a Lambda Function
54 const host = yield* Lambda.eventSourceHost("AWS.Lambda.WirelessDestinationEventSource");
55 const Rule = yield* TopicRule;
56 const Permission = yield* LambdaPermission;
57
58 return Effect.fn(function* <Req = never>(
59 destination: Destination,
60 process: WirelessUplinkHandlerFn<Req>,
61 ) {
62 // Deploy-time: create the IoT topic rule the destination's expression
63 // names, with a Lambda action targeting this function, and grant
64 // iot.amazonaws.com permission to invoke it (scoped to the rule ARN).
65 // IoT Wireless invokes the rule directly by name, so the SQL FROM
66 // clause is never matched against a real topic. Skipped once running
67 // inside the deployed function, where the only work is registering
68 // the runtime handler.
69 if (!globalThis.__ALCHEMY_RUNTIME__) {
70 yield* Namespace.push(
71 host.LogicalId,
72 Effect.gen(function* () {
73 const rule = yield* Rule(`${destination.LogicalId}-UplinkRule`, {
74 ruleName: destination.expression,
75 sql: "SELECT * FROM 'iotwireless/uplink'",
76 actions: [{ lambda: { functionArn: Lambda.qualifiedArnOf(host) } }],
77 });
78
79 yield* Permission(`AWS.IoTWireless.UplinkInvoke(${destination.LogicalId})`, {
80 action: "lambda:InvokeFunction",
81 functionName: Lambda.qualifiedNameOf(host),
82 principal: "iot.amazonaws.com",
83 sourceArn: rule.ruleArn,
84 });
85 }),
86 );
87 }
88
89 yield* host.listen(
90 Effect.gen(function* () {
91 return (event: any) => {
92 if (isWirelessUplinkMessage(event)) {
93 return process(Stream.fromArray([event])).pipe(Effect.orDie);
94 }
95 };
96 }),
97 );
98 }) as DestinationEventSourceService;
99 }),
100 );