The best way to control Claude Code, Codex and more from any computer or phone

main
ts 83 lines 3.24 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Layer from "effect/Layer";
3 import * as Stream from "effect/Stream";
4 import type { Bucket } from "../S3/Bucket.ts";
5 import type { BucketNotification, NotificationsProps } from "../S3/BucketNotifications.ts";
6 import * as S3 from "../S3/index.ts";
7 import { normalizeBucketNotification } from "../S3/normalizeBucketNotification.ts";
8 import type { S3EventType } from "../S3/S3Event.ts";
9 import * as SQS from "../SQS/index.ts";
10 import { SQSQueueEventSource } from "./SQSQueueEventSource.ts";
11
12 /** @binding */
13 export const S3BucketEventSource = Layer.effect(
14 S3.BucketEventSource,
15 Effect.gen(function* () {
16 const Queue = yield* SQS.Queue;
17
18 // Cast below: the service type erases `process`'s requirements, so every
19 // event source implementation casts its client.
20 return Effect.fn(function* <Events extends S3EventType[], StreamReq = never, Req = never>(
21 bucket: Bucket,
22 props: NotificationsProps<Events>,
23 process: (
24 stream: Stream.Stream<BucketNotification, never, StreamReq>,
25 ) => Effect.Effect<void, never, Req>,
26 ) {
27 const queue = yield* Queue(`${bucket.LogicalId}-BucketEvents`);
28
29 // Deploy-time: grant the bucket sqs:SendMessage on the queue and attach the
30 // bucket's notification config. Skipped once running inside the deployed
31 // Function (the global guard); the runtime only registers the consumer below.
32 if (!globalThis.__ALCHEMY_RUNTIME__) {
33 const events = props.events ?? ["s3:ObjectCreated:*"];
34 const filterRules = [
35 ...(props.prefix !== undefined ? [{ Name: "prefix" as const, Value: props.prefix }] : []),
36 ...(props.suffix !== undefined ? [{ Name: "suffix" as const, Value: props.suffix }] : []),
37 ];
38 yield* queue.bind(`AWS.SQS.SendMessage(${bucket.LogicalId})`, {
39 policyStatements: [
40 {
41 Sid: `AllowS3EventsFrom${bucket.LogicalId}`,
42 Effect: "Allow",
43 Principal: { Service: "s3.amazonaws.com" },
44 Action: ["sqs:SendMessage"],
45 Resource: [queue.queueArn],
46 Condition: {
47 ArnEquals: {
48 "aws:SourceArn": bucket.bucketArn,
49 },
50 },
51 },
52 ],
53 });
54 yield* bucket.bind(`AWS.S3.NotificationConfiguration(${queue.LogicalId})`, {
55 notificationConfiguration: {
56 QueueConfigurations: [
57 {
58 QueueArn: queue.queueArn,
59 Events: events,
60 ...(filterRules.length > 0
61 ? { Filter: { Key: { FilterRules: filterRules } } }
62 : {}),
63 },
64 ],
65 },
66 });
67 }
68
69 yield* SQS.consumeQueueMessages(queue, (event) =>
70 Stream.fromArray(event.Records).pipe(
71 Stream.mapEffect((record) =>
72 Effect.sync(
73 () => (JSON.parse(record.body) as { Records?: S3.S3Record[] }).Records ?? [],
74 ),
75 ),
76 Stream.flatMap((records) => Stream.fromArray(records)),
77 Stream.mapEffect(normalizeBucketNotification),
78 process,
79 ),
80 );
81 }) as S3.BucketEventSourceService;
82 }),
83 ).pipe(Layer.provideMerge(SQSQueueEventSource));