1import * as Effect from "effect/Effect";
2import * as Binding from "../../Binding.ts";
3import { isBindingHost } from "../Lambda/Function.ts";
4
5/**
6 * Shared scaffolding for AWS Shield HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
9 * a `Layer.effect(Cap, makeShieldHttpBinding({ … }))` over the builder below.
10 * Everything except the operation and the IAM action list is boilerplate.
11 *
12 * Shield is an account-level, us-east-1-pinned service: its IAM actions do
13 * not support resource-level permissions for the visibility operations bound
14 * here (attacks, subscription state, DRT access), and the group-membership
15 * read targets groups whose ids are only known at runtime — so the
16 * deploy-time half grants `actions` on `*` and the caller's request passes
17 * through as-is.
18 */
19export const makeShieldHttpBinding = <I, A, E, R>(options: {
20 /** Fully-qualified binding tag, e.g. `AWS.Shield.ListAttacks`. */
21 tag: string;
22 /** The distilled operation, invoked with the caller's request as-is. */
23 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
24 /** IAM actions granted on `*`. */
25 actions: readonly string[];
26}) =>
27 Effect.gen(function* () {
28 const op = yield* options.operation;
29
30 return Effect.fn(function* () {
31 if (!globalThis.__ALCHEMY_RUNTIME__) {
32 const host = yield* Binding.Host;
33 if (isBindingHost(host)) {
34 yield* host.bind`Allow(${host}, ${options.tag}())`({
35 policyStatements: [
36 {
37 Effect: "Allow",
38 Action: [...options.actions],
39 Resource: ["*"],
40 },
41 ],
42 });
43 }
44 }
45 return Effect.fn(options.tag)(function* (request?: I) {
46 return yield* op((request ?? {}) as I);
47 });
48 });
49 });