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 { Credentials, fromCredentials } from "@distilled.cloud/aws/Credentials"; |
| 2 | import * as AwsEndpoint from "@distilled.cloud/aws/Endpoint"; |
| 3 | import type { RegionName } from "@distilled.cloud/aws/Region"; |
| 4 | import * as Binding from "alchemy/Binding"; |
| 5 | import type { Resource } from "alchemy/Resource"; |
| 6 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 9 | import type * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import type { Bucket } from "./Bucket.ts"; |
| 13 | import { TigrisCredentialsMissing } from "./Errors.ts"; |
| 14 | import type { ServiceBinding } from "./MountVolume.ts"; |
| 15 | |
| 16 | /** |
| 17 | * Shared scaffolding for Tigris S3 bindings. |
| 18 | * |
| 19 | * Tigris speaks the S3 API. Each `{Op}Http.ts` is a thin |
| 20 | * `Layer.effect(Cap, makeTigrisS3Binding({ operation }))` that: |
| 21 | * - registers the bucket on the host so Service reconcile can wait for |
| 22 | * it and attach Tigris App secrets |
| 23 | * - `yield*`s credential Outputs so RuntimeContext.set runs |
| 24 | * - calls `@distilled.cloud/aws/s3` with those credentials and endpoint |
| 25 | * |
| 26 | * NOT exported from `index.ts`. |
| 27 | */ |
| 28 | export interface TigrisS3Scope { |
| 29 | bucketName: string; |
| 30 | accessKeyId: string; |
| 31 | secretAccessKey: string; |
| 32 | endpoint: string; |
| 33 | region: RegionName; |
| 34 | } |
| 35 | |
| 36 | const isFlyHost = (value: unknown): value is Resource<string, any, any, ServiceBinding> => |
| 37 | typeof value === "object" && |
| 38 | value !== null && |
| 39 | ((value as { Type?: string }).Type === "Fly.Service" || |
| 40 | (value as { Type?: string }).Type === "Fly.Machine"); |
| 41 | |
| 42 | const asPlain = (value: unknown): string | undefined => { |
| 43 | if (typeof value === "string" && value.length > 0) return value; |
| 44 | if (Redacted.isRedacted(value)) return value.pipe(Redacted.value, asPlain); |
| 45 | return undefined; |
| 46 | }; |
| 47 | |
| 48 | const authorizeS3 = <A, E>( |
| 49 | scope: TigrisS3Scope, |
| 50 | operation: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>, |
| 51 | ): Effect.Effect<A, E, RuntimeContext> => |
| 52 | operation.pipe( |
| 53 | Effect.provide( |
| 54 | Layer.mergeAll( |
| 55 | fromCredentials( |
| 56 | { |
| 57 | accessKeyId: Redacted.make(scope.accessKeyId), |
| 58 | secretAccessKey: Redacted.make(scope.secretAccessKey), |
| 59 | }, |
| 60 | scope.region, |
| 61 | ), |
| 62 | AwsEndpoint.of(scope.endpoint), |
| 63 | FetchHttpClient.layer, |
| 64 | ), |
| 65 | ), |
| 66 | ) as Effect.Effect<A, E, RuntimeContext>; |
| 67 | |
| 68 | export const makeTigrisS3Binding = <I extends { Bucket?: string }, A, E>(options: { |
| 69 | tag: string; |
| 70 | operation: (input: I) => Effect.Effect<A, E, Credentials | HttpClient.HttpClient>; |
| 71 | }) => |
| 72 | Effect.succeed( |
| 73 | Effect.fn(function* (bucket: Bucket) { |
| 74 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 75 | const host = yield* Binding.Host; |
| 76 | if (isFlyHost(host)) { |
| 77 | // Pass the Bucket resource itself so waitForDeps waits for it |
| 78 | // and apply evaluates to attributes (including create-only |
| 79 | // Tigris credentials). PropExprs of `name` alone can resolve |
| 80 | // from the deterministic physical name without waiting. |
| 81 | yield* host.bind`${bucket}`({ |
| 82 | bucket: bucket as unknown as { name: string; id?: string }, |
| 83 | }); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | const bucketName = yield* bucket.bucketName; |
| 88 | const name = yield* bucket.name; |
| 89 | const accessKeyId = yield* bucket.accessKeyId; |
| 90 | const secretAccessKey = yield* bucket.secretAccessKey; |
| 91 | const endpoint = yield* bucket.endpoint; |
| 92 | const region = yield* bucket.region; |
| 93 | |
| 94 | return Effect.fn(`${options.tag}(${bucket.LogicalId})`)(function* ( |
| 95 | request?: Omit<I, "Bucket">, |
| 96 | ) { |
| 97 | const resolvedName = asPlain(yield* bucketName) ?? asPlain(yield* name); |
| 98 | const resolvedKey = asPlain(yield* accessKeyId); |
| 99 | const resolvedSecret = asPlain(yield* secretAccessKey); |
| 100 | const resolvedEndpoint = asPlain(yield* endpoint); |
| 101 | const resolvedRegion = asPlain(yield* region) ?? "auto"; |
| 102 | if ( |
| 103 | resolvedName === undefined || |
| 104 | resolvedKey === undefined || |
| 105 | resolvedSecret === undefined || |
| 106 | resolvedEndpoint === undefined |
| 107 | ) { |
| 108 | return yield* new TigrisCredentialsMissing({ |
| 109 | resourceName: resolvedName ?? bucket.LogicalId, |
| 110 | }); |
| 111 | } |
| 112 | const scope = { |
| 113 | bucketName: resolvedName, |
| 114 | accessKeyId: resolvedKey, |
| 115 | secretAccessKey: resolvedSecret, |
| 116 | endpoint: resolvedEndpoint, |
| 117 | region: resolvedRegion as RegionName, |
| 118 | } satisfies TigrisS3Scope; |
| 119 | return yield* authorizeS3( |
| 120 | scope, |
| 121 | options.operation({ |
| 122 | ...request, |
| 123 | Bucket: scope.bucketName, |
| 124 | } as I), |
| 125 | ); |
| 126 | }); |
| 127 | }), |
| 128 | ); |