The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as keyspacesstreams from "@distilled.cloud/aws/keyspacesstreams"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as AWSBindings from "../AWSBindings.ts"; |
| 6 | import type { Table } from "./Table.ts"; |
| 7 | import { |
| 8 | TableStreams, |
| 9 | type ListTableStreamsRequest, |
| 10 | type TableStreamsClient, |
| 11 | } from "./TableStreams.ts"; |
| 12 | |
| 13 | /** |
| 14 | * HTTP implementation of {@link TableStreams}: grants the CDC stream read |
| 15 | * actions (`cassandra:ListStreams` / `GetStream` / `GetShardIterator` / |
| 16 | * `GetRecords`) on the bound table and its streams, and calls the Keyspaces |
| 17 | * streams HTTP API with the function's IAM credentials. |
| 18 | */ |
| 19 | export const TableStreamsHttp = Layer.effect( |
| 20 | TableStreams, |
| 21 | Effect.gen(function* () { |
| 22 | const bind = yield* AWSBindings.Bind; |
| 23 | const listStreams = yield* AWSBindings.withCredentials(keyspacesstreams.listStreams); |
| 24 | const getStream = yield* AWSBindings.withCredentials(keyspacesstreams.getStream); |
| 25 | const getShardIterator = yield* AWSBindings.withCredentials(keyspacesstreams.getShardIterator); |
| 26 | const getRecords = yield* AWSBindings.withCredentials(keyspacesstreams.getRecords); |
| 27 | |
| 28 | return Effect.fn(function* <T extends Table>(table: T) { |
| 29 | const KeyspaceName = yield* table.keyspaceName; |
| 30 | const TableName = yield* table.tableName; |
| 31 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 32 | yield* bind`AWS.Keyspaces.TableStreams(${table})`({ |
| 33 | policyStatements: [ |
| 34 | { |
| 35 | Effect: "Allow", |
| 36 | Action: [ |
| 37 | "cassandra:GetRecords", |
| 38 | "cassandra:GetShardIterator", |
| 39 | "cassandra:GetStream", |
| 40 | "cassandra:ListStreams", |
| 41 | ], |
| 42 | Resource: [table.tableArn, Output.interpolate`${table.tableArn}/stream/*`], |
| 43 | }, |
| 44 | ], |
| 45 | }); |
| 46 | } |
| 47 | const logicalId = table.LogicalId; |
| 48 | const client: TableStreamsClient = { |
| 49 | listStreams: Effect.fn(`AWS.Keyspaces.TableStreams.listStreams(${logicalId})`)(function* ( |
| 50 | request?: ListTableStreamsRequest, |
| 51 | ) { |
| 52 | const keyspaceName = yield* KeyspaceName; |
| 53 | const tableName = yield* TableName; |
| 54 | return yield* listStreams({ ...request, keyspaceName, tableName }); |
| 55 | }), |
| 56 | getStream: Effect.fn(`AWS.Keyspaces.TableStreams.getStream(${logicalId})`)(function* ( |
| 57 | request: keyspacesstreams.GetStreamInput, |
| 58 | ) { |
| 59 | return yield* getStream(request); |
| 60 | }), |
| 61 | getShardIterator: Effect.fn(`AWS.Keyspaces.TableStreams.getShardIterator(${logicalId})`)( |
| 62 | function* (request: keyspacesstreams.GetShardIteratorInput) { |
| 63 | return yield* getShardIterator(request); |
| 64 | }, |
| 65 | ), |
| 66 | getRecords: Effect.fn(`AWS.Keyspaces.TableStreams.getRecords(${logicalId})`)(function* ( |
| 67 | request: keyspacesstreams.GetRecordsInput, |
| 68 | ) { |
| 69 | return yield* getRecords(request); |
| 70 | }), |
| 71 | }; |
| 72 | return client; |
| 73 | }); |
| 74 | }), |
| 75 | ); |