@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 105 lines 3.59 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import type * as lambda from "aws-lambda";
3 import * as Effect from "effect/Effect";
4 import * as Stream from "effect/Stream";
5 import type { Topic } from "./Topic.ts";
6
7 export type TopicNotification = lambda.SNSMessage;
8
9 export interface TopicEventSourceProps {
10 /**
11 * Raw SNS subscription attributes for the Lambda subscription, such as
12 * `FilterPolicy` or `RedrivePolicy`.
13 */
14 attributes?: Record<string, string>;
15 }
16
17 /**
18 * Event source connecting an SNS {@link Topic} to the hosting Lambda
19 * function: it creates the `lambda`-protocol subscription (plus the invoke
20 * permission) at deploy time and dispatches delivered notifications to the
21 * registered handler at runtime.
22 *
23 * The contract is a `Binding.Service`; the Lambda implementation layer is
24 * `Lambda.TopicEventSource`. Consume it through the
25 * {@link consumeTopicNotifications} helper.
26 * ### Consuming a Topic
27 * **Example:** Consume Notifications in a Lambda Function
28 * ```typescript
29 * export default WorkerFunction.make(
30 * { main: import.meta.url },
31 * Effect.gen(function* () {
32 * const topic = yield* SNS.Topic("Events");
33 *
34 * // registers the subscription and the runtime dispatcher
35 * yield* SNS.consumeTopicNotifications(topic, (stream) =>
36 * stream.pipe(
37 * Stream.runForEach((message) => Effect.log(message.Message)),
38 * ),
39 * );
40 * }).pipe(Effect.provide(Lambda.TopicEventSource)),
41 * );
42 * ```
43 *
44 * @binding
45 */
46 export interface TopicEventSource extends Binding.Service<
47 TopicEventSource,
48 "AWS.SNS.TopicEventSource",
49 TopicEventSourceService
50 > {}
51 export const TopicEventSource = Binding.Service<TopicEventSource>("AWS.SNS.TopicEventSource");
52
53 export type TopicEventSourceService = <StreamReq = never, Req = never>(
54 topic: Topic,
55 props: TopicEventSourceProps,
56 process: (
57 stream: Stream.Stream<TopicNotification, never, StreamReq>,
58 ) => Effect.Effect<void, never, Req>,
59 ) => Effect.Effect<void, never, never>;
60
61 type TopicEventSourceHandler<Req, StreamReq> = (
62 stream: Stream.Stream<TopicNotification, never, StreamReq>,
63 ) => Effect.Effect<void, never, Req>;
64
65 /**
66 * Subscribe a Lambda Function to an SNS {@link Topic}, processing published
67 * notifications as a stream.
68 *
69 * @example
70 * ```typescript
71 * yield* consumeTopicNotifications(topic, (stream) =>
72 * stream.pipe(Stream.runForEach((message) => Effect.log(message.Message))),
73 * );
74 * ```
75 *
76 * @example With subscription attributes
77 * ```typescript
78 * yield* consumeTopicNotifications(
79 * topic,
80 * { attributes: { FilterPolicy: JSON.stringify({ type: ["order"] }) } },
81 * (stream) =>
82 * stream.pipe(Stream.runForEach((message) => Effect.log(message.Message))),
83 * );
84 * ```
85 */
86 export function consumeTopicNotifications<T extends Topic, Req = never, StreamReq = never>(
87 topic: T,
88 process: TopicEventSourceHandler<Req, StreamReq>,
89 ): Effect.Effect<void, never, TopicEventSource>;
90 export function consumeTopicNotifications<T extends Topic, Req = never, StreamReq = never>(
91 topic: T,
92 props: TopicEventSourceProps,
93 process: TopicEventSourceHandler<Req, StreamReq>,
94 ): Effect.Effect<void, never, TopicEventSource>;
95 export function consumeTopicNotifications<T extends Topic, Req = never, StreamReq = never>(
96 topic: T,
97 propsOrProcess: TopicEventSourceProps | TopicEventSourceHandler<Req, StreamReq>,
98 maybeProcess?: TopicEventSourceHandler<Req, StreamReq>,
99 ) {
100 const [props, process] =
101 typeof propsOrProcess === "function"
102 ? [{} as TopicEventSourceProps, propsOrProcess]
103 : [propsOrProcess, maybeProcess!];
104 return TopicEventSource.use((source) => source(topic, props, process));
105 }