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
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Bucket } from "./Bucket.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for AWS S3 HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in this |
| 10 | * service is a thin `Layer.effect(Cap, makeBucketHttpBinding({ … }))` over |
| 11 | * this builder. Everything except the operation, the IAM action(s), and the |
| 12 | * IAM resource shape is boilerplate: |
| 13 | * |
| 14 | * - the deploy-time half registers `Allow(host, tag(bucket))` with the |
| 15 | * requested actions on the bound bucket (object-level `${arn}/*` or the |
| 16 | * bucket ARN itself, per `iamResources`). On a host that runs as an IAM |
| 17 | * role the statements go on that role; on any other host they go on the |
| 18 | * Role Alchemy creates for it (see `AWSBindings.Bind`); |
| 19 | * - the runtime callable injects the resolved bucket name as `Bucket`; the |
| 20 | * operation is resolved with `AWSBindings.withCredentials`, so a host with |
| 21 | * a created key signs with the assumed Role in the bucket's region. |
| 22 | * |
| 23 | * Genuinely-different bindings stay bespoke: `PresignGetObject` / |
| 24 | * `PresignPutObject` (SigV4 presigners, not API operations). |
| 25 | */ |
| 26 | export const makeBucketHttpBinding = <I extends { Bucket?: string }, A, E, R>(options: { |
| 27 | /** Fully-qualified binding tag, e.g. `AWS.S3.GetObject`. */ |
| 28 | tag: string; |
| 29 | /** |
| 30 | * The distilled operation; `Bucket` is injected from the bound bucket. |
| 31 | * Resolved with the host's credentials (see `AWSBindings.withCredentials`), |
| 32 | * signing in the bucket's region on a host with a created key. |
| 33 | */ |
| 34 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 35 | /** IAM actions granted on the bound bucket. */ |
| 36 | actions: readonly string[]; |
| 37 | /** |
| 38 | * Which ARNs the actions are granted on: |
| 39 | * - `"objects"` (default): `${bucketArn}/*` — object-level actions |
| 40 | * (`s3:GetObject`, `s3:PutObject`, …); |
| 41 | * - `"bucket"`: the bucket ARN — bucket-level actions (`s3:ListBucket`, |
| 42 | * `s3:ListBucketVersions`, `s3:ListBucketMultipartUploads`). |
| 43 | */ |
| 44 | iamResources?: "objects" | "bucket"; |
| 45 | /** |
| 46 | * Additionally grant `s3:ListBucket` on the bucket ARN. Required by |
| 47 | * object-read actions so a missing key surfaces as `NoSuchKey`/`NotFound` |
| 48 | * (404) instead of `AccessDenied` (403). |
| 49 | * @see https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html |
| 50 | * @see https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-with-s3-policy-actions.html |
| 51 | */ |
| 52 | listBucket?: boolean; |
| 53 | }) => |
| 54 | Effect.gen(function* () { |
| 55 | const bind = yield* AWSBindings.Bind; |
| 56 | return Effect.fn(function* <B extends Bucket>(bucket: B) { |
| 57 | const BucketName = yield* bucket.bucketName; |
| 58 | const BucketRegion = yield* bucket.region; |
| 59 | // resolved per bind because a host with a created key signs in the bucket's own region |
| 60 | const op = yield* AWSBindings.withCredentials( |
| 61 | options.operation, |
| 62 | Effect.succeed(BucketRegion), |
| 63 | ); |
| 64 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 65 | yield* bind`${options.tag}(${bucket})`({ |
| 66 | policyStatements: [ |
| 67 | { |
| 68 | Effect: "Allow", |
| 69 | Action: [...options.actions], |
| 70 | Resource: |
| 71 | options.iamResources === "bucket" |
| 72 | ? [bucket.bucketArn] |
| 73 | : [Output.interpolate`${bucket.bucketArn}/*`], |
| 74 | }, |
| 75 | ...(options.listBucket |
| 76 | ? [ |
| 77 | { |
| 78 | Effect: "Allow" as const, |
| 79 | Action: ["s3:ListBucket"], |
| 80 | Resource: [bucket.bucketArn], |
| 81 | }, |
| 82 | ] |
| 83 | : []), |
| 84 | ], |
| 85 | }); |
| 86 | } |
| 87 | return Effect.fn(`${options.tag}(${bucket.LogicalId})`)(function* ( |
| 88 | request?: Omit<I, "Bucket">, |
| 89 | ) { |
| 90 | return yield* op({ ...request, Bucket: yield* BucketName } as I); |
| 91 | }); |
| 92 | }); |
| 93 | }); |