The best way to control Claude Code, Codex and more from any computer or phone

main
ts 161 lines 5.5 KB
Raw
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 { RuntimeContext } from "alchemy/RuntimeContext";
6 import * as Config from "effect/Config";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as FetchHttpClient from "effect/http/FetchHttpClient";
10 import type * as HttpClient from "effect/http/HttpClient";
11 import * as Layer from "effect/Layer";
12 import * as Redacted from "effect/Redacted";
13 import type { Bucket } from "./Bucket.ts";
14 import { isRailwayHost } from "./MountVolume.ts";
15
16 /**
17 * Shared scaffolding for Railway S3 bindings.
18 *
19 * Railway buckets speak the S3 API. Each `{Op}Http.ts` is a thin
20 * `Layer.effect(Cap, makeRailwayS3Binding({ operation }))` that:
21 * - registers the bucket on the host so Service reconcile can write
22 * `AWS_*` / `BUCKET_NAME` variables
23 * - calls `@distilled.cloud/aws/s3` with those credentials and endpoint
24 *
25 * NOT exported from `index.ts`.
26 */
27 export class RailwayS3CredentialsMissing extends Data.TaggedError("Railway.S3CredentialsMissing")<{
28 resourceName: string;
29 }> {}
30
31 export interface RailwayS3Scope {
32 bucketName: string;
33 accessKeyId: string;
34 secretAccessKey: string;
35 endpoint: string;
36 region: RegionName;
37 }
38
39 const asPlain = (value: unknown): string | undefined => {
40 if (typeof value === "string" && value.length > 0) return value;
41 if (Redacted.isRedacted(value)) return value.pipe(Redacted.value, asPlain);
42 return undefined;
43 };
44
45 const readValue = (value: unknown): Effect.Effect<string | undefined> =>
46 Effect.gen(function* () {
47 const direct = asPlain(value);
48 if (direct !== undefined) return direct;
49 if (Effect.isEffect(value)) {
50 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- resource attribute Effects resolve without requirements
51 return asPlain(yield* value as Effect.Effect<unknown>);
52 }
53 return undefined;
54 });
55
56 const scopeFromResource = (bucket: Bucket) =>
57 Effect.gen(function* () {
58 const bucketName = (yield* readValue(bucket.s3BucketName)) ?? (yield* readValue(bucket.name));
59 const accessKeyId = yield* readValue(bucket.accessKeyId);
60 const secretAccessKey = yield* readValue(bucket.secretAccessKey);
61 const endpoint = yield* readValue(bucket.endpoint);
62 const region =
63 (yield* readValue(bucket.s3Region)) ?? (yield* readValue(bucket.region)) ?? "auto";
64 if (
65 bucketName === undefined ||
66 accessKeyId === undefined ||
67 secretAccessKey === undefined ||
68 endpoint === undefined
69 ) {
70 return yield* new RailwayS3CredentialsMissing({
71 resourceName: bucketName ?? bucket.LogicalId,
72 });
73 }
74 return {
75 bucketName,
76 accessKeyId,
77 secretAccessKey,
78 endpoint,
79 region: region as RegionName,
80 } satisfies RailwayS3Scope;
81 });
82
83 const scopeFromEnv = Effect.gen(function* () {
84 const bucketName = yield* Config.String("BUCKET_NAME").pipe(
85 Config.orElse(() => Config.String("AWS_S3_BUCKET_NAME")),
86 );
87 const accessKeyId = yield* Config.String("AWS_ACCESS_KEY_ID");
88 const secretAccessKey = yield* Config.Redacted("AWS_SECRET_ACCESS_KEY");
89 const endpoint = yield* Config.String("AWS_ENDPOINT_URL_S3").pipe(
90 Config.orElse(() => Config.String("AWS_ENDPOINT_URL")),
91 );
92 const region = yield* Config.String("AWS_REGION").pipe(Config.withDefault("auto"));
93 return {
94 bucketName,
95 accessKeyId,
96 secretAccessKey: Redacted.value(secretAccessKey),
97 endpoint,
98 region: region as RegionName,
99 } satisfies RailwayS3Scope;
100 });
101
102 const authorizeS3 = <A, E>(
103 scope: RailwayS3Scope,
104 operation: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>,
105 ): Effect.Effect<A, E, RuntimeContext> =>
106 operation.pipe(
107 Effect.provide(
108 Layer.mergeAll(
109 fromCredentials(
110 {
111 accessKeyId: Redacted.make(scope.accessKeyId),
112 secretAccessKey: Redacted.make(scope.secretAccessKey),
113 },
114 scope.region,
115 ),
116 AwsEndpoint.of(scope.endpoint),
117 FetchHttpClient.layer,
118 ),
119 ),
120 ) as Effect.Effect<A, E, RuntimeContext>;
121
122 export const makeRailwayS3Binding = <I extends { Bucket?: string }, A, E>(options: {
123 tag: string;
124 operation: (input: I) => Effect.Effect<A, E, Credentials | HttpClient.HttpClient>;
125 }) =>
126 Effect.succeed(
127 Effect.fn(function* (bucket: Bucket) {
128 if (!globalThis.__ALCHEMY_RUNTIME__) {
129 const host = yield* Binding.Host;
130 if (isRailwayHost(host)) {
131 yield* host.bind`${bucket}`({
132 env: {
133 AWS_ACCESS_KEY_ID: bucket.accessKeyId,
134 AWS_SECRET_ACCESS_KEY: bucket.secretAccessKey,
135 AWS_ENDPOINT_URL_S3: bucket.endpoint,
136 AWS_ENDPOINT_URL: bucket.endpoint,
137 AWS_REGION: bucket.s3Region,
138 BUCKET_NAME: bucket.s3BucketName,
139 AWS_S3_BUCKET_NAME: bucket.s3BucketName,
140 AWS_S3_URL_STYLE: bucket.urlStyle,
141 },
142 });
143 }
144 }
145
146 return Effect.fn(`${options.tag}(${bucket.LogicalId})`)(function* (
147 request?: Omit<I, "Bucket">,
148 ) {
149 const scope = globalThis.__ALCHEMY_RUNTIME__
150 ? yield* scopeFromEnv
151 : yield* scopeFromResource(bucket);
152 return yield* authorizeS3(
153 scope,
154 options.operation({
155 ...request,
156 Bucket: scope.bucketName,
157 } as I),
158 );
159 });
160 }),
161 );