@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 95 lines 3.11 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as Effect from "effect/Effect";
3 import type { Function as LambdaFunction } from "../Lambda/Function.ts";
4 import { Permission as LambdaPermission } from "../Lambda/Permission.ts";
5 import { qualifiedArnOf, qualifiedNameOf } from "../Lambda/QualifiedArn.ts";
6 import type { EventBus } from "./EventBus.ts";
7 import { Rule, type RuleProps, type RuleTarget } from "./Rule.ts";
8
9 interface EventDescriptor {
10 id?: string;
11 bus?: EventBus;
12 pattern: Record<string, any>;
13 props?: Pick<RuleProps, "description" | "state">;
14 }
15
16 export interface LambdaRouteTargetProps extends Pick<
17 RuleTarget,
18 "Input" | "InputPath" | "InputTransformer" | "RetryPolicy" | "DeadLetterConfig"
19 > {}
20
21 /**
22 * Routes matching events from an EventBridge bus to a Lambda function.
23 *
24 * Creates a {@link Rule} targeting the function and a Lambda permission
25 * allowing `events.amazonaws.com` to invoke it. Usually reached through the
26 * `events(...)` builder rather than called directly.
27 * **Example:** Route Matching Events to a Lambda Function
28 * ```typescript
29 * yield* AWS.EventBridge.events(bus, { source: ["my.app"] }).toLambda(fn);
30 * ```
31 *
32 * **Example:** Transform the Event Payload Before Invoking
33 * ```typescript
34 * yield* AWS.EventBridge.events(bus, { source: ["my.app"] }).toLambda(fn, {
35 * InputTransformer: {
36 * InputPathsMap: { orderId: "$.detail.orderId" },
37 * InputTemplate: '{"orderId": <orderId>}',
38 * },
39 * });
40 * ```
41 *
42 * @binding
43 */
44 export const toLambda = (
45 descriptor: EventDescriptor,
46 fn: LambdaFunction,
47 props: LambdaRouteTargetProps = {},
48 ) =>
49 Effect.gen(function* () {
50 const routeId = descriptor.id ?? createRouteId(descriptor, `${fn.LogicalId}Lambda`);
51
52 const rule = yield* Rule(routeId, {
53 description: descriptor.props?.description,
54 state: descriptor.props?.state,
55 eventBusName: descriptor.bus?.eventBusName,
56 eventPattern: descriptor.pattern,
57 targets: [
58 {
59 Id: `${fn.LogicalId}Target`,
60 Arn: qualifiedArnOf(fn) as any,
61 Input: props.Input,
62 InputPath: props.InputPath,
63 InputTransformer: props.InputTransformer,
64 RetryPolicy: props.RetryPolicy,
65 DeadLetterConfig: props.DeadLetterConfig,
66 },
67 ],
68 });
69
70 // Deploy-time: grant EventBridge permission to invoke the Lambda. Skipped
71 // once running inside the deployed Function by the global guard. The
72 // permission is explicitly named, so no Namespace.push is required.
73 if (!globalThis.__ALCHEMY_RUNTIME__) {
74 yield* LambdaPermission(`${routeId}${fn.LogicalId}InvokePermission`, {
75 action: "lambda:InvokeFunction",
76 functionName: qualifiedNameOf(fn),
77 principal: "events.amazonaws.com",
78 sourceArn: rule.ruleArn as any,
79 }).pipe(Effect.asVoid);
80 }
81
82 return rule;
83 });
84
85 const createRouteId = (descriptor: EventDescriptor, suffix: string) =>
86 `EventBridge${createHash("sha1")
87 .update(
88 JSON.stringify({
89 bus: descriptor.bus?.LogicalId ?? "default",
90 pattern: descriptor.pattern,
91 suffix,
92 }),
93 )
94 .digest("hex")
95 .slice(0, 10)}`;