The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Region as AwsRegion } from "@distilled.cloud/aws/Region"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import { KVS_REGION, withKvsRegion } from "./common.ts"; |
| 5 | import type { Distribution } from "./Distribution.ts"; |
| 6 | import type { KeyValueStore } from "./KeyValueStore.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Shared scaffolding for CloudFront HTTP bindings. |
| 10 | * |
| 11 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 12 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders |
| 13 | * below. Everything except the operation, the IAM action list, and the |
| 14 | * injected identifier is boilerplate. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Build the impl Effect for a distribution-scoped operation. The runtime |
| 19 | * callable injects the bound {@link Distribution}'s id as the request's |
| 20 | * `DistributionId`; the deploy-time half grants `actions` on the |
| 21 | * distribution's ARN. |
| 22 | */ |
| 23 | export const makeDistributionScopedHttpBinding = < |
| 24 | I extends { DistributionId: string }, |
| 25 | A, |
| 26 | E, |
| 27 | R, |
| 28 | >(options: { |
| 29 | /** Fully-qualified binding tag, e.g. `AWS.CloudFront.GetInvalidation`. */ |
| 30 | tag: string; |
| 31 | /** The distilled operation; `DistributionId` is injected from the resource. */ |
| 32 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 33 | /** IAM actions granted on the distribution ARN. */ |
| 34 | actions: readonly string[]; |
| 35 | }) => |
| 36 | Effect.gen(function* () { |
| 37 | const bind = yield* AWSBindings.Bind; |
| 38 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 39 | |
| 40 | return Effect.fn(function* (distribution: Distribution) { |
| 41 | const DistributionId = yield* distribution.distributionId; |
| 42 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 43 | yield* bind`${options.tag}(${distribution})`({ |
| 44 | policyStatements: [ |
| 45 | { |
| 46 | Effect: "Allow", |
| 47 | Action: [...options.actions], |
| 48 | Resource: [distribution.distributionArn], |
| 49 | }, |
| 50 | ], |
| 51 | }); |
| 52 | } |
| 53 | return Effect.fn(`${options.tag}(${distribution.LogicalId})`)(function* ( |
| 54 | request: Omit<I, "DistributionId">, |
| 55 | ) { |
| 56 | return yield* op({ |
| 57 | ...request, |
| 58 | DistributionId: yield* DistributionId, |
| 59 | } as I); |
| 60 | }); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | /** |
| 65 | * Build the impl Effect for a KeyValueStore data-plane operation |
| 66 | * (`cloudfront-keyvaluestore`). The runtime callable injects the bound |
| 67 | * {@link KeyValueStore}'s ARN as the request's `KvsARN`; the deploy-time half |
| 68 | * grants `actions` on the store's ARN. The data-plane endpoint is global |
| 69 | * (derived from the ARN), but distilled signs with plain SigV4 against the |
| 70 | * context region and the service only accepts `us-east-1` signatures — so |
| 71 | * the operation is resolved with the Region pinned to {@link KVS_REGION}, |
| 72 | * exactly like the resource providers in `common.ts`. |
| 73 | */ |
| 74 | export const makeKeyValueStoreScopedHttpBinding = <I extends { KvsARN: string }, A, E, R>(options: { |
| 75 | /** Fully-qualified binding tag, e.g. `AWS.CloudFront.GetKey`. */ |
| 76 | tag: string; |
| 77 | /** The distilled operation; `KvsARN` is injected from the resource. */ |
| 78 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 79 | /** IAM actions granted on the KeyValueStore ARN. */ |
| 80 | actions: readonly string[]; |
| 81 | }) => |
| 82 | Effect.gen(function* () { |
| 83 | const bind = yield* AWSBindings.Bind; |
| 84 | // The distilled Region service value is `Effect<RegionName>`, not a raw |
| 85 | // string (see `withKvsRegion` in common.ts). |
| 86 | const op = yield* AWSBindings.withCredentials(options.operation).pipe( |
| 87 | Effect.provideService(AwsRegion, Effect.succeed(KVS_REGION)), |
| 88 | ); |
| 89 | |
| 90 | return Effect.fn(function* (store: KeyValueStore) { |
| 91 | const KvsARN = yield* store.keyValueStoreArn; |
| 92 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 93 | yield* bind`${options.tag}(${store})`({ |
| 94 | policyStatements: [ |
| 95 | { |
| 96 | Effect: "Allow", |
| 97 | Action: [...options.actions], |
| 98 | Resource: [store.keyValueStoreArn], |
| 99 | }, |
| 100 | ], |
| 101 | }); |
| 102 | } |
| 103 | return Effect.fn(`${options.tag}(${store.LogicalId})`)(function* ( |
| 104 | request: Omit<I, "KvsARN">, |
| 105 | ) { |
| 106 | // The region must also be pinned at the call site: the yield-time |
| 107 | // snapshot is only a fallback — the calling fiber's ambient Region |
| 108 | // (the host Function's own region) wins over it. |
| 109 | return yield* withKvsRegion( |
| 110 | op({ |
| 111 | ...request, |
| 112 | KvsARN: yield* KvsARN, |
| 113 | } as I), |
| 114 | ); |
| 115 | }); |
| 116 | }); |
| 117 | }); |