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
| 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 { |
| 6 | ClusterEventSource as DocDBClusterEventSourceContract, |
| 7 | type ClusterEventSourceProps, |
| 8 | type ClusterEventSourceService, |
| 9 | type DocumentDBRecord, |
| 10 | } from "../DocDB/ClusterEventSource.ts"; |
| 11 | import type { DBCluster } from "../DocDB/DBCluster.ts"; |
| 12 | import { EventSourceMapping } from "./EventSourceMapping.ts"; |
| 13 | import * as Lambda from "./Function.ts"; |
| 14 | |
| 15 | export const isDocumentDBEvent = ( |
| 16 | event: any, |
| 17 | ): event is { eventSourceArn?: string; events: unknown[] } => |
| 18 | typeof event?.eventSourceArn === "string" && Array.isArray(event?.events); |
| 19 | |
| 20 | /** @binding */ |
| 21 | export const DocDBClusterEventSource = Layer.effect( |
| 22 | DocDBClusterEventSourceContract, |
| 23 | Effect.gen(function* () { |
| 24 | const host = yield* Lambda.eventSourceHost("AWS.Lambda.DocDBClusterEventSource"); |
| 25 | const Mapping = yield* EventSourceMapping; |
| 26 | |
| 27 | return Effect.fn(function* <TDoc = unknown, Req = never>( |
| 28 | cluster: DBCluster, |
| 29 | props: ClusterEventSourceProps, |
| 30 | process: (stream: Stream.Stream<DocumentDBRecord<TDoc>>) => Effect.Effect<void, never, Req>, |
| 31 | ) { |
| 32 | const ClusterArn = yield* cluster.dbClusterArn; |
| 33 | |
| 34 | // Deploy-time: grant IAM (secret access) and create the DocumentDB |
| 35 | // event-source mapping. Skipped once running inside the deployed Function |
| 36 | // (the global guard), where the only work is registering the runtime |
| 37 | // handler below. Namespaced under the host so the mapping's logical |
| 38 | // identity is stable. |
| 39 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 40 | yield* Namespace.push( |
| 41 | host.LogicalId, |
| 42 | Effect.gen(function* () { |
| 43 | yield* host.bind`Allow(${host}, AWS.DocDB.ClusterEventSource(${cluster}))`({ |
| 44 | policyStatements: [ |
| 45 | { |
| 46 | Effect: "Allow", |
| 47 | Action: ["secretsmanager:GetSecretValue"], |
| 48 | Resource: [props.secretArn], |
| 49 | }, |
| 50 | ], |
| 51 | }); |
| 52 | |
| 53 | yield* Mapping( |
| 54 | `AWS.Lambda.EventSourceMapping(${host.LogicalId}, ${cluster.LogicalId})`, |
| 55 | { |
| 56 | functionName: Lambda.qualifiedNameOf(host), |
| 57 | eventSourceArn: cluster.dbClusterArn, |
| 58 | startingPosition: props.startingPosition ?? "LATEST", |
| 59 | batchSize: props.batchSize, |
| 60 | maximumBatchingWindow: props.maximumBatchingWindow, |
| 61 | enabled: props.enabled ?? true, |
| 62 | documentDBEventSourceConfig: { |
| 63 | DatabaseName: props.databaseName, |
| 64 | CollectionName: props.collectionName, |
| 65 | FullDocument: props.fullDocument ?? "Default", |
| 66 | }, |
| 67 | sourceAccessConfigurations: [{ Type: "BASIC_AUTH", URI: props.secretArn }], |
| 68 | }, |
| 69 | ); |
| 70 | }), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | yield* host.listen( |
| 75 | Effect.gen(function* () { |
| 76 | const clusterArn = yield* ClusterArn; |
| 77 | return (event: any) => { |
| 78 | if (isDocumentDBEvent(event) && event.eventSourceArn === clusterArn) { |
| 79 | return process(Stream.fromArray(event.events as DocumentDBRecord<TDoc>[])).pipe( |
| 80 | Effect.orDie, |
| 81 | ); |
| 82 | } |
| 83 | }; |
| 84 | }), |
| 85 | ); |
| 86 | }) as ClusterEventSourceService; |
| 87 | }), |
| 88 | ); |