The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Stream from "effect/Stream"; |
| 3 | import type { Topic } from "../SNS/Topic.ts"; |
| 4 | import { consumeTopicNotifications, type TopicNotification } from "../SNS/TopicEventSource.ts"; |
| 5 | import type { Directory } from "./Directory.ts"; |
| 6 | import { EventTopic } from "./EventTopic.ts"; |
| 7 | |
| 8 | /** |
| 9 | * A directory status notification delivered to the handler. Directory |
| 10 | * Service publishes a message whenever the directory changes stage — e.g. |
| 11 | * from `Active` to `Impaired` or `Inoperable`, and back to `Active`. |
| 12 | */ |
| 13 | export interface DirectoryStatusEvent { |
| 14 | /** The raw SNS notification as delivered. */ |
| 15 | notification: TopicNotification; |
| 16 | /** |
| 17 | * The parsed JSON body of the notification, when the message is JSON |
| 18 | * (AWS does not document a strict schema for directory status messages). |
| 19 | */ |
| 20 | detail: Record<string, unknown> | undefined; |
| 21 | } |
| 22 | |
| 23 | const parseDirectoryStatus = (notification: TopicNotification): DirectoryStatusEvent => { |
| 24 | let detail: Record<string, unknown> | undefined; |
| 25 | try { |
| 26 | const parsed: unknown = JSON.parse(notification.Message); |
| 27 | detail = |
| 28 | typeof parsed === "object" && parsed !== null |
| 29 | ? (parsed as Record<string, unknown>) |
| 30 | : undefined; |
| 31 | } catch { |
| 32 | detail = undefined; |
| 33 | } |
| 34 | return { notification, detail }; |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * Event source connecting an AWS Directory Service {@link Directory}'s |
| 39 | * status notifications to the hosting Lambda function. Directory Service's |
| 40 | * native event mechanism is Amazon SNS: at deploy time this registers the |
| 41 | * directory as a publisher to the given {@link Topic} (an |
| 42 | * {@link EventTopic} association) and subscribes the host function to the |
| 43 | * topic; at runtime delivered status messages are dispatched to the |
| 44 | * handler. |
| 45 | * |
| 46 | * Provide `AWS.Lambda.TopicEventSource` on the Function effect (the SNS |
| 47 | * subscription machinery this event source delegates to). |
| 48 | * |
| 49 | * @example Alert When the Directory Becomes Impaired |
| 50 | * ```typescript |
| 51 | * export default AlertFunction.make( |
| 52 | * { main: import.meta.url }, |
| 53 | * Effect.gen(function* () { |
| 54 | * const topic = yield* SNS.Topic("DirectoryStatus", {}); |
| 55 | * const directory = yield* DirectoryService.Directory("Corp", { ... }); |
| 56 | * |
| 57 | * yield* DirectoryService.consumeDirectoryStatus(directory, topic, (events) => |
| 58 | * Stream.runForEach(events, (event) => |
| 59 | * Effect.logError(`directory status: ${event.notification.Message}`), |
| 60 | * ), |
| 61 | * ); |
| 62 | * return {}; |
| 63 | * }).pipe(Effect.provide(Lambda.TopicEventSource)), |
| 64 | * ); |
| 65 | * ``` |
| 66 | */ |
| 67 | export const consumeDirectoryStatus = <StreamReq = never, Req = never>( |
| 68 | directory: Directory, |
| 69 | topic: Topic, |
| 70 | process: ( |
| 71 | events: Stream.Stream<DirectoryStatusEvent, never, StreamReq>, |
| 72 | ) => Effect.Effect<void, never, Req>, |
| 73 | ) => |
| 74 | Effect.gen(function* () { |
| 75 | // Deploy-time: associate the directory with the SNS topic so status |
| 76 | // changes are published to it. |
| 77 | yield* EventTopic(`${directory.LogicalId}${topic.LogicalId}Status`, { |
| 78 | directoryId: directory.directoryId, |
| 79 | topicName: topic.topicName, |
| 80 | }); |
| 81 | // Subscribe the host function to the topic and dispatch parsed status |
| 82 | // messages to the handler. |
| 83 | yield* consumeTopicNotifications(topic, (stream) => |
| 84 | process(stream.pipe(Stream.map(parseDirectoryStatus))), |
| 85 | ); |
| 86 | }); |