| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Binding from "../../Binding.ts"; |
| 3 | import * as Output from "../../Output.ts"; |
| 4 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 5 | import type { Vault } from "./Vault.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for the Glacier runtime bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 11 | * thin `Layer.effect(Cap, makeGlacierVaultHttpBinding({ … }))` over the |
| 12 | * builder below. Everything except the operation and the IAM action list is |
| 13 | * boilerplate: Glacier's REST API scopes every data-plane operation to a |
| 14 | * vault, so the runtime callable injects the bound {@link Vault}'s name (and |
| 15 | * the `-` account-id path segment, meaning "the account that signed the |
| 16 | * request") and the deploy-time half grants `actions` on the vault's ARN. |
| 17 | */ |
| 18 | export const makeGlacierVaultHttpBinding = < |
| 19 | I extends { accountId: string; vaultName: string }, |
| 20 | A, |
| 21 | E, |
| 22 | R, |
| 23 | >(options: { |
| 24 | /** Fully-qualified binding tag, e.g. `AWS.Glacier.UploadArchive`. */ |
| 25 | tag: string; |
| 26 | /** |
| 27 | * The distilled operation; `accountId` (always `-`) and `vaultName` are |
| 28 | * injected from the bound vault. |
| 29 | */ |
| 30 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 31 | /** IAM actions granted on the vault ARN. */ |
| 32 | actions: readonly string[]; |
| 33 | }) => |
| 34 | Effect.gen(function* () { |
| 35 | const op = yield* options.operation; |
| 36 | |
| 37 | return Effect.fn(function* (vault: Vault) { |
| 38 | const vaultName = yield* vault.vaultName; |
| 39 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 40 | const host = yield* Binding.Host; |
| 41 | if (isBindingHost(host)) { |
| 42 | yield* host.bind`Allow(${host}, ${options.tag}(${vault}))`({ |
| 43 | policyStatements: [ |
| 44 | { |
| 45 | Effect: "Allow", |
| 46 | Action: [...options.actions], |
| 47 | Resource: [Output.interpolate`${vault.vaultArn}`], |
| 48 | }, |
| 49 | ], |
| 50 | }); |
| 51 | } |
| 52 | } |
| 53 | return Effect.fn(`${options.tag}(${vault.LogicalId})`)(function* ( |
| 54 | request?: Omit<I, "accountId" | "vaultName">, |
| 55 | ) { |
| 56 | return yield* op({ |
| 57 | ...request, |
| 58 | accountId: "-", |
| 59 | vaultName: yield* vaultName, |
| 60 | } as I); |
| 61 | }); |
| 62 | }); |
| 63 | }); |