@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, 06:00 UTC

main
ts 359 lines 12.5 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Kinesis from "@distilled.cloud/aws/kinesis";
3 import * as Context from "effect/Context";
4 import * as Data from "effect/Data";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Layer from "effect/Layer";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12
13 export class KinesisApiFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
14 "KinesisApiFunction",
15 ) {}
16
17 export class StreamAndConsumer extends Context.Service<
18 StreamAndConsumer,
19 {
20 stream: AWS.Kinesis.Stream;
21 consumer: AWS.Kinesis.StreamConsumer;
22 }
23 >()("StreamAndConsumer") {}
24
25 export const StreamAndConsumerLive = Layer.effect(
26 StreamAndConsumer,
27 Effect.gen(function* () {
28 const stream = yield* AWS.Kinesis.Stream("FixtureStream", {
29 streamMode: "PROVISIONED",
30 shardCount: 1,
31 tags: {
32 fixture: "kinesis-bindings",
33 },
34 });
35
36 const consumer = yield* AWS.Kinesis.StreamConsumer("FixtureConsumer", {
37 streamArn: stream.streamArn,
38 tags: {
39 fixture: "kinesis-bindings",
40 },
41 });
42
43 return {
44 stream,
45 consumer,
46 };
47 }),
48 );
49
50 export const KinesisApiFunctionLive = KinesisApiFunction.make(
51 {
52 main: import.meta.url,
53 functionUrl: true,
54 // The sink's bounded partial-failure retry can sleep up to ~6s, which
55 // exceeds Lambda's 3s default timeout (see PATTERNS §7).
56 timeout: Duration.seconds(30),
57 },
58 Effect.gen(function* () {
59 const { stream, consumer } = yield* StreamAndConsumer;
60
61 const describeAccountSettings = yield* AWS.Kinesis.DescribeAccountSettings();
62 const describeLimits = yield* AWS.Kinesis.DescribeLimits();
63 const listStreams = yield* AWS.Kinesis.ListStreams();
64 const describeStream = yield* AWS.Kinesis.DescribeStream(stream);
65 const describeStreamSummary = yield* AWS.Kinesis.DescribeStreamSummary(stream);
66 const listShards = yield* AWS.Kinesis.ListShards(stream);
67 const getShardIterator = yield* AWS.Kinesis.GetShardIterator(stream);
68 const getRecords = yield* AWS.Kinesis.GetRecords(stream);
69 const getResourcePolicy = yield* AWS.Kinesis.GetResourcePolicy(stream);
70 const listStreamConsumers = yield* AWS.Kinesis.ListStreamConsumers(stream);
71 const describeStreamConsumer = yield* AWS.Kinesis.DescribeStreamConsumer(consumer);
72 const subscribeToShard = yield* AWS.Kinesis.SubscribeToShard(consumer);
73 const listTagsForResource = yield* AWS.Kinesis.ListTagsForResource(stream);
74 const putRecord = yield* AWS.Kinesis.PutRecord(stream);
75 const putRecords = yield* AWS.Kinesis.PutRecords(stream);
76 const sink = yield* AWS.Kinesis.StreamSink(stream);
77 const splitShard = yield* AWS.Kinesis.SplitShard(stream);
78 const mergeShards = yield* AWS.Kinesis.MergeShards(stream);
79
80 return {
81 fetch: Effect.gen(function* () {
82 const request = yield* HttpServerRequest;
83 const url = new URL(request.originalUrl);
84 const pathname = url.pathname;
85
86 if (request.method === "GET" && pathname === "/ready") {
87 return yield* HttpServerResponse.json({ ok: true });
88 }
89
90 if (request.method === "GET" && pathname === "/account-settings") {
91 const response = yield* describeAccountSettings().pipe(
92 Effect.match({
93 onFailure: (error) => ({
94 ok: false as const,
95 error:
96 typeof error === "object" && error !== null && "_tag" in error
97 ? (error as { _tag: string })._tag
98 : `${error}`,
99 }),
100 onSuccess: (value) => ({
101 ok: true as const,
102 value,
103 }),
104 }),
105 );
106 return yield* HttpServerResponse.json(response);
107 }
108
109 if (request.method === "GET" && pathname === "/limits") {
110 const response = yield* describeLimits().pipe(
111 Effect.match({
112 onFailure: (error) => ({
113 ok: false as const,
114 error:
115 typeof error === "object" && error !== null && "_tag" in error
116 ? (error as { _tag: string })._tag
117 : `${error}`,
118 }),
119 onSuccess: (value) => ({
120 ok: true as const,
121 value,
122 }),
123 }),
124 );
125 return yield* HttpServerResponse.json(response);
126 }
127
128 if (request.method === "GET" && pathname === "/streams") {
129 return yield* HttpServerResponse.json(yield* listStreams());
130 }
131
132 if (request.method === "GET" && pathname === "/stream") {
133 return yield* HttpServerResponse.json(yield* describeStream());
134 }
135
136 if (request.method === "GET" && pathname === "/stream-summary") {
137 return yield* HttpServerResponse.json(yield* describeStreamSummary());
138 }
139
140 if (request.method === "GET" && pathname === "/resource-policy") {
141 const response = yield* getResourcePolicy().pipe(
142 Effect.match({
143 onFailure: (error) => ({
144 ok: false as const,
145 error:
146 typeof error === "object" && error !== null && "_tag" in error
147 ? (error as { _tag: string })._tag
148 : `${error}`,
149 }),
150 onSuccess: (value) => ({
151 ok: true as const,
152 value,
153 }),
154 }),
155 );
156 return yield* HttpServerResponse.json(response);
157 }
158
159 if (request.method === "GET" && pathname === "/shards") {
160 return yield* HttpServerResponse.json(yield* listShards());
161 }
162
163 if (request.method === "GET" && pathname === "/stream-consumers") {
164 return yield* HttpServerResponse.json(yield* listStreamConsumers());
165 }
166
167 if (request.method === "GET" && pathname === "/consumer") {
168 return yield* HttpServerResponse.json(yield* describeStreamConsumer());
169 }
170
171 if (request.method === "GET" && pathname === "/tags") {
172 return yield* HttpServerResponse.json(yield* listTagsForResource());
173 }
174
175 if (request.method === "POST" && pathname === "/put-record") {
176 const body = (yield* request.json) as {
177 partitionKey: string;
178 data: string;
179 };
180 return yield* HttpServerResponse.json(
181 yield* putRecord({
182 PartitionKey: body.partitionKey,
183 Data: new TextEncoder().encode(body.data),
184 }),
185 );
186 }
187
188 if (request.method === "POST" && pathname === "/put-records") {
189 const body = (yield* request.json) as {
190 records: Array<{ partitionKey: string; data: string }>;
191 };
192 return yield* HttpServerResponse.json(
193 yield* putRecords({
194 Records: body.records.map((record) => ({
195 PartitionKey: record.partitionKey,
196 Data: new TextEncoder().encode(record.data),
197 })),
198 }),
199 );
200 }
201
202 if (request.method === "POST" && pathname === "/sink") {
203 const body = (yield* request.json) as {
204 records: Array<{ partitionKey: string; data: string }>;
205 };
206 yield* Stream.fromIterable(
207 body.records.map((record) => ({
208 PartitionKey: record.partitionKey,
209 Data: new TextEncoder().encode(record.data),
210 })),
211 ).pipe(Stream.run(sink));
212 return yield* HttpServerResponse.json({ ok: true });
213 }
214
215 if (request.method === "POST" && pathname === "/iterator") {
216 const body = (yield* request.json) as { shardId: string };
217 return yield* HttpServerResponse.json(
218 yield* getShardIterator({
219 ShardId: body.shardId,
220 ShardIteratorType: "LATEST",
221 }),
222 );
223 }
224
225 if (request.method === "POST" && pathname === "/records") {
226 const body = (yield* request.json) as {
227 shardId: string;
228 partitionKey: string;
229 data: string;
230 };
231
232 const iterator = yield* getShardIterator({
233 ShardId: body.shardId,
234 ShardIteratorType: "LATEST",
235 });
236 const shardIterator = iterator.ShardIterator;
237
238 if (!shardIterator) {
239 return yield* HttpServerResponse.json(
240 { error: "No shard iterator returned" },
241 { status: 500 },
242 );
243 }
244
245 yield* putRecord({
246 PartitionKey: body.partitionKey,
247 Data: new TextEncoder().encode(body.data),
248 });
249
250 const result = yield* waitForRecords(getRecords, shardIterator);
251 return yield* HttpServerResponse.json({
252 records: (result.Records ?? []).map((record) => ({
253 partitionKey: record.PartitionKey,
254 data: decodeText(record.Data),
255 })),
256 millisBehindLatest: result.MillisBehindLatest,
257 });
258 }
259
260 if (request.method === "POST" && pathname === "/split-shard") {
261 const body = (yield* request.json) as {
262 shardToSplit: string;
263 newStartingHashKey: string;
264 };
265 yield* splitShard({
266 ShardToSplit: body.shardToSplit,
267 NewStartingHashKey: body.newStartingHashKey,
268 });
269 return yield* HttpServerResponse.json({ ok: true });
270 }
271
272 if (request.method === "POST" && pathname === "/merge-shards") {
273 const body = (yield* request.json) as {
274 shardToMerge: string;
275 adjacentShardToMerge: string;
276 };
277 yield* mergeShards({
278 ShardToMerge: body.shardToMerge,
279 AdjacentShardToMerge: body.adjacentShardToMerge,
280 });
281 return yield* HttpServerResponse.json({ ok: true });
282 }
283
284 if (request.method === "POST" && pathname === "/subscribe") {
285 const body = (yield* request.json) as { shardId: string };
286 const result = yield* subscribeToShard({
287 ShardId: body.shardId,
288 StartingPosition: {
289 Type: "LATEST",
290 },
291 });
292 return yield* HttpServerResponse.json({
293 ok: result.EventStream !== undefined,
294 });
295 }
296
297 return yield* HttpServerResponse.json(
298 { error: "Not found", method: request.method, pathname },
299 { status: 404 },
300 );
301 }).pipe(Effect.orDie),
302 };
303 }).pipe(
304 Effect.provide(
305 Layer.provideMerge(
306 Layer.mergeAll(AWS.Kinesis.StreamSinkHttp, AWS.Kinesis.PutRecordsHttp),
307 Layer.mergeAll(
308 AWS.Kinesis.DescribeAccountSettingsHttp,
309 AWS.Kinesis.DescribeLimitsHttp,
310 AWS.Kinesis.DescribeStreamHttp,
311 AWS.Kinesis.DescribeStreamConsumerHttp,
312 AWS.Kinesis.DescribeStreamSummaryHttp,
313 AWS.Kinesis.GetRecordsHttp,
314 AWS.Kinesis.GetResourcePolicyHttp,
315 AWS.Kinesis.GetShardIteratorHttp,
316 AWS.Kinesis.ListShardsHttp,
317 AWS.Kinesis.ListStreamConsumersHttp,
318 AWS.Kinesis.ListStreamsHttp,
319 AWS.Kinesis.ListTagsForResourceHttp,
320 AWS.Kinesis.MergeShardsHttp,
321 AWS.Kinesis.PutRecordHttp,
322 AWS.Kinesis.PutRecordsHttp,
323 AWS.Kinesis.SplitShardHttp,
324 AWS.Kinesis.SubscribeToShardHttp,
325 StreamAndConsumerLive,
326 ),
327 ),
328 ),
329 ),
330 // Re-merge so the deploying Stack can `yield* StreamAndConsumer` and expose
331 // the stream/consumer names as deploy-time outputs. Reusing the same
332 // `StreamAndConsumerLive` reference keeps it a single shared stream/consumer.
333 ).pipe(Layer.provideMerge(StreamAndConsumerLive));
334
335 export default KinesisApiFunctionLive;
336
337 const decodeText = (value: Uint8Array<ArrayBufferLike>) => new TextDecoder().decode(value);
338
339 class RecordsNotReady extends Data.TaggedError("RecordsNotReady") {}
340
341 const waitForRecords = (
342 getRecords: (
343 request: Kinesis.GetRecordsInput,
344 ) => Effect.Effect<Kinesis.GetRecordsOutput, Kinesis.GetRecordsError>,
345 shardIterator: string,
346 ) =>
347 getRecords({
348 ShardIterator: shardIterator,
349 }).pipe(
350 Effect.flatMap((result) =>
351 (result.Records?.length ?? 0) > 0
352 ? Effect.succeed(result)
353 : Effect.fail(new RecordsNotReady()),
354 ),
355 Effect.retry({
356 while: (error) => error._tag === "RecordsNotReady",
357 schedule: Schedule.max([Schedule.fixed("1 second"), Schedule.recurs(10)]),
358 }),
359 );