The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Output as OutputType } from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Function } from "./Function.ts"; |
| 5 | import { qualifiedArnOf } from "./QualifiedArn.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS Lambda control/data-plane HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every near-identical `{Op}Http.ts` in this |
| 11 | * service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of |
| 12 | * the builders below. Everything except the operation, the IAM action list, |
| 13 | * and the granted ARNs is boilerplate. How the host reaches AWS (native vs |
| 14 | * external, see `AWSBindings.ts`) is handled by `AWSBindings.withCredentials` |
| 15 | * and `AWSBindings.Bind`. |
| 16 | */ |
| 17 | |
| 18 | /** Region segment of an AWS ARN (`arn:partition:service:<region>:...`). */ |
| 19 | export const regionFromArn = (arn: string): string => arn.split(":")[3] ?? "us-east-1"; |
| 20 | |
| 21 | /** |
| 22 | * Build the impl Effect for a function-scoped operation (`Invoke`, |
| 23 | * `GetFunction`, `InvokeWithResponseStream`): the runtime callable injects |
| 24 | * the bound {@link Function}'s ARN as `FunctionName` and the deploy-time |
| 25 | * half grants `actions` on `resources` (default: the function ARN). |
| 26 | */ |
| 27 | export const makeFunctionHttpBinding = <I extends { FunctionName?: string }, A, E, R>(options: { |
| 28 | /** Fully-qualified binding tag, e.g. `AWS.Lambda.GetFunction`. */ |
| 29 | tag: string; |
| 30 | /** |
| 31 | * The distilled operation; `FunctionName` is injected from the function. |
| 32 | * Resolved with the host's credentials (see `AWSBindings.withCredentials`), |
| 33 | * signing in the function's region on a host with a created key. |
| 34 | */ |
| 35 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 36 | /** IAM actions granted on `resources`. */ |
| 37 | actions: readonly string[]; |
| 38 | /** ARNs the actions are granted on. @default the function ARN */ |
| 39 | resources?: (func: Function) => (string | OutputType<string>)[]; |
| 40 | /** |
| 41 | * Target the Function's alias (see `FunctionProps.alias`) instead of the |
| 42 | * unqualified function, for operations that invoke it. Grants the actions |
| 43 | * on the alias ARN unless `resources` overrides it. |
| 44 | */ |
| 45 | qualified?: boolean; |
| 46 | }) => |
| 47 | Effect.gen(function* () { |
| 48 | const bind = yield* AWSBindings.Bind; |
| 49 | return Effect.fn(function* (func: Function) { |
| 50 | const target = options.qualified ? qualifiedArnOf(func) : func.functionArn; |
| 51 | const FunctionArn = options.qualified |
| 52 | ? // Same runtime path as `yield* output` (which keys the value by the |
| 53 | // expression's source text), with a readable key; typed like the |
| 54 | // iterator, whose RuntimeContext the host always provides. |
| 55 | // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- the host provides RuntimeContext at runtime |
| 56 | yield* target.bind(`${func.LogicalId}.qualifiedArn`) as Effect.Effect< |
| 57 | Effect.Effect<string> |
| 58 | > |
| 59 | : yield* func.functionArn; |
| 60 | // resolved per bind because a host with a created key signs in the function's own region |
| 61 | const op = yield* AWSBindings.withCredentials( |
| 62 | options.operation, |
| 63 | Effect.succeed(Effect.map(FunctionArn, regionFromArn)), |
| 64 | ); |
| 65 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 66 | yield* bind`${options.tag}(${func})`({ |
| 67 | policyStatements: [ |
| 68 | { |
| 69 | Effect: "Allow", |
| 70 | Action: [...options.actions], |
| 71 | Resource: options.resources?.(func) ?? [target], |
| 72 | }, |
| 73 | ], |
| 74 | }); |
| 75 | } |
| 76 | return Effect.fn(`${options.tag}(${func.LogicalId})`)(function* ( |
| 77 | request?: Omit<I, "FunctionName">, |
| 78 | ) { |
| 79 | return yield* op({ ...request, FunctionName: yield* FunctionArn } as I); |
| 80 | }); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | /** |
| 85 | * Build the impl Effect for an account-level operation |
| 86 | * (`GetAccountSettings`, `ListFunctions`): the runtime callable passes the |
| 87 | * caller's request through unchanged and the deploy-time half grants |
| 88 | * `actions` on `*` (these Lambda actions do not support resource-level |
| 89 | * permissions). |
| 90 | */ |
| 91 | export const makeLambdaAccountHttpBinding = <I, A, E, R>(options: { |
| 92 | /** Fully-qualified binding tag, e.g. `AWS.Lambda.ListFunctions`. */ |
| 93 | tag: string; |
| 94 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 95 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 96 | /** IAM actions granted on `*`. */ |
| 97 | actions: readonly string[]; |
| 98 | }) => |
| 99 | Effect.gen(function* () { |
| 100 | const bind = yield* AWSBindings.Bind; |
| 101 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 102 | |
| 103 | return Effect.fn(function* () { |
| 104 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 105 | yield* bind`${options.tag}()`({ |
| 106 | policyStatements: [ |
| 107 | { |
| 108 | Effect: "Allow", |
| 109 | Action: [...options.actions], |
| 110 | Resource: ["*"], |
| 111 | }, |
| 112 | ], |
| 113 | }); |
| 114 | } |
| 115 | return Effect.fn(options.tag)(function* (request?: I) { |
| 116 | return yield* op((request ?? {}) as I); |
| 117 | }); |
| 118 | }); |
| 119 | }); |