@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, 04:45 UTC

main
ts 118 lines 3.67 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Stream from "effect/Stream";
3 import type { Bucket } from "./Bucket.ts";
4 import { BucketEventSource } from "./BucketEventSource.ts";
5 import type { S3EventType } from "./S3Event.ts";
6
7 /**
8 * A normalized S3 event notification record.
9 */
10 export type BucketNotification = {
11 /** The S3 event type, including the `s3:` prefix. */
12 type: S3EventType;
13 /** Name of the bucket the event originated from. */
14 bucket: string;
15 /** URL-decoded object key that the event applies to. */
16 key: string;
17 /** Size in bytes, when supplied; removal events may omit it. */
18 size?: number;
19 /** Object ETag, when supplied; removal events may omit it. */
20 eTag?: string;
21 /**
22 * Object or delete-marker version ID. Pass as GetObject's VersionId to read
23 * the exact data version; delete markers are not readable objects.
24 */
25 versionId?: string;
26 /** Ordering token for events affecting the same key, when supplied. */
27 sequencer?: string;
28 };
29
30 export interface NotificationsProps<Events extends S3EventType[]> {
31 /** S3 event types to subscribe to. Defaults to `s3:ObjectCreated:*`. */
32 events?: Events;
33 /**
34 * Only deliver events for object keys beginning with this prefix.
35 * Maps to an S3 `prefix` filter rule on the notification configuration.
36 */
37 prefix?: string;
38 /**
39 * Only deliver events for object keys ending with this suffix.
40 * Maps to an S3 `suffix` filter rule on the notification configuration.
41 */
42 suffix?: string;
43 }
44
45 /**
46 * Subscribe to S3 bucket event notifications.
47 *
48 * The handler receives a `Stream<BucketNotification>` for processing events
49 * and is passed as the final positional argument.
50 * ### Subscribing to Events
51 * **Example:** Process all object creation events
52 * ```typescript
53 * import * as S3 from "@alchemy.run/aws/S3";
54 *
55 * yield* S3.consumeBucketEvents(
56 * bucket,
57 * { events: ["s3:ObjectCreated:*"] },
58 * (stream) =>
59 * stream.pipe(
60 * Stream.runForEach((event) =>
61 * Effect.log(`New object: ${event.key} (${event.size} bytes)`),
62 * ),
63 * ),
64 * );
65 * ```
66 *
67 * **Example:** Process object creation events without a key filter
68 * ```typescript
69 * yield* S3.consumeBucketEvents(bucket, (stream) =>
70 * stream.pipe(
71 * Stream.runForEach((event) =>
72 * Effect.log(`${event.type}: ${event.key}`),
73 * ),
74 * ),
75 * );
76 * ```
77 *
78 * @binding
79 */
80 export function consumeBucketEvents<B extends Bucket, Req = never, StreamReq = never>(
81 bucket: B,
82 handler: (
83 stream: Stream.Stream<BucketNotification, never, StreamReq>,
84 ) => Effect.Effect<void, never, Req>,
85 ): Effect.Effect<void, never, Req>;
86 export function consumeBucketEvents<
87 B extends Bucket,
88 Req = never,
89 StreamReq = never,
90 const Events extends S3EventType[] = S3EventType[],
91 >(
92 bucket: B,
93 props: NotificationsProps<Events>,
94 handler: (
95 stream: Stream.Stream<BucketNotification, never, StreamReq>,
96 ) => Effect.Effect<void, never, Req>,
97 ): Effect.Effect<void, never, Req>;
98 export function consumeBucketEvents<
99 B extends Bucket,
100 Req = never,
101 StreamReq = never,
102 const Events extends S3EventType[] = S3EventType[],
103 >(
104 bucket: B,
105 propsOrHandler:
106 | NotificationsProps<Events>
107 | ((
108 stream: Stream.Stream<BucketNotification, never, StreamReq>,
109 ) => Effect.Effect<void, never, Req>),
110 maybeHandler?: (
111 stream: Stream.Stream<BucketNotification, never, StreamReq>,
112 ) => Effect.Effect<void, never, Req>,
113 ) {
114 const props: NotificationsProps<Events> =
115 typeof propsOrHandler === "function" ? {} : propsOrHandler;
116 const handler = typeof propsOrHandler === "function" ? propsOrHandler : maybeHandler!;
117 return BucketEventSource.use((source) => source(bucket, props, handler));
118 }