@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, 03:37 UTC

main
ts 95 lines 3.36 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import * as Effect from "effect/Effect";
3 import * as Stream from "effect/Stream";
4 import type { Bucket } from "./Bucket.ts";
5 import type { BucketNotification, NotificationsProps } from "./BucketNotifications.ts";
6 import type { S3EventType } from "./S3Event.ts";
7
8 /**
9 * Event source that streams a bucket's notifications (object created, object
10 * removed, ...) into the host Lambda Function. Usually consumed through the
11 * `consumeBucketEvents` helper, which provisions the bucket-notification
12 * subscription at deploy time and registers the stream handler at runtime.
13 * Provide the implementation with `Effect.provide(Lambda.BucketEventSource)`.
14 * ### Consuming Bucket Events
15 * **Example:** Process Object-Created Events
16 * ```typescript
17 * export default MyFunction.make(
18 * { main: import.meta.url },
19 * Effect.gen(function* () {
20 * const bucket = yield* AWS.S3.Bucket("UploadsBucket");
21 * const putObject = yield* AWS.S3.PutObject(bucket);
22 *
23 * // filter to `incoming/` so the derived `processed/` write does not
24 * // re-trigger the subscription
25 * yield* AWS.S3.consumeBucketEvents(
26 * bucket,
27 * { events: ["s3:ObjectCreated:*"], prefix: "incoming/" },
28 * (stream) =>
29 * stream.pipe(
30 * Stream.runForEach((event) =>
31 * putObject({
32 * Key: `processed/${event.key.slice("incoming/".length)}`,
33 * Body: JSON.stringify({ key: event.key, size: event.size }),
34 * }).pipe(Effect.orDie),
35 * ),
36 * ),
37 * );
38 *
39 * return {
40 * fetch: Effect.succeed(HttpServerResponse.text("ok")),
41 * };
42 * }).pipe(
43 * Effect.provide(
44 * Layer.mergeAll(Lambda.BucketEventSource, AWS.S3.PutObjectHttp),
45 * ),
46 * ),
47 * );
48 * ```
49 *
50 * ### Reading the Event's Object Version
51 * **Example:** Read the version that triggered a creation event
52 * ```typescript
53 * const getObject = yield* AWS.S3.GetObject(bucket);
54 * yield* AWS.S3.consumeBucketEvents(
55 * bucket,
56 * { events: ["s3:ObjectCreated:*"], prefix: "incoming/" },
57 * (events) => events.pipe(
58 * Stream.runForEach((event) =>
59 * getObject({ Key: event.key, VersionId: event.versionId }).pipe(
60 * Effect.flatMap(({ Body }) => Stream.runDrain(Body!)),
61 * Effect.orDie,
62 * ),
63 * ),
64 * ),
65 * );
66 * // Provide Lambda.BucketEventSource and AWS.S3.GetObjectHttp on the function.
67 * ```
68 *
69 * Passing `event.versionId` reads the triggering data version even if the key
70 * has since been overwritten. Unversioned events omit it and read the current
71 * object. Removal events can identify a delete marker or an already-deleted
72 * version; they may omit `size` and `eTag` and are not object-read signals.
73 * `sequencer`, when present, orders events only for the same object key.
74 *
75 * @binding
76 */
77 export interface BucketEventSource extends Binding.Service<
78 BucketEventSource,
79 "BucketNotificationStream",
80 BucketEventSourceService
81 > {}
82
83 export const BucketEventSource = Binding.Service<BucketEventSource>("BucketNotificationStream");
84
85 export type BucketEventSourceService = <
86 Events extends S3EventType[],
87 StreamReq = never,
88 Req = never,
89 >(
90 bucket: Bucket,
91 props: NotificationsProps<Events>,
92 process: (
93 stream: Stream.Stream<BucketNotification, never, StreamReq>,
94 ) => Effect.Effect<void, never, Req>,
95 ) => Effect.Effect<void, never, never>;