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

main
ts 710 lines 24.3 KB
Raw
1 import { Query, type UnwrapPlan } from "@distilled.cloud/core/query";
2 import {
3 Railway,
4 type Bucket as RailwayBucket,
5 type BucketS3CompatibleCredentials,
6 } from "@distilled.cloud/railway";
7 import { Unowned } from "alchemy/AdoptPolicy";
8 import { isResolved } from "alchemy/Diff";
9 import * as Provider from "alchemy/Provider";
10 import { Resource } from "alchemy/Resource";
11 import * as Data from "effect/Data";
12 import * as Effect from "effect/Effect";
13 import * as Redacted from "effect/Redacted";
14 import * as Schedule from "effect/Schedule";
15 import * as Stream from "effect/Stream";
16 import { projectBuckets } from "./GraphQL.ts";
17 import { createRailwayName, matchesAlchemyPhysicalName, sanitizeRailwayName } from "./Metadata.ts";
18 import { ownedProjects, projectEnvironmentIds, type Project } from "./Project.ts";
19 import type { Providers } from "./Providers.ts";
20 import { withEnvironmentConfigLock } from "./transient.ts";
21
22 const bucketFields = <E>(bucket: Query<RailwayBucket, E>) => ({
23 id: bucket.id,
24 name: bucket.name,
25 projectId: bucket.projectId,
26 createdAt: bucket.createdAt,
27 updatedAt: bucket.updatedAt,
28 });
29 const credentialsFields = <E>(credentials: Query<BucketS3CompatibleCredentials, E>) => ({
30 accessKeyId: credentials.accessKeyId,
31 bucketName: credentials.bucketName,
32 endpoint: credentials.endpoint,
33 region: credentials.region,
34 secretAccessKey: credentials.secretAccessKey,
35 urlStyle: credentials.urlStyle,
36 });
37 type CloudBucket = UnwrapPlan<ReturnType<typeof bucketFields>>;
38 type BucketS3CredentialsResultItem = UnwrapPlan<ReturnType<typeof credentialsFields>>;
39
40 const readEnvironmentConfig = Query.fn((id: string, projectId: string) => ({
41 config: Railway.environment({ id, projectId }).config,
42 }));
43
44 const environmentPatchCommit = Query.fn(
45 (input: { environmentId: string; commitMessage: string; patch: Record<string, unknown> }) =>
46 Railway.environmentPatchCommit(input),
47 );
48
49 const readBucketS3Credentials = Query.fn(
50 (input: { bucketId: string; environmentId: string; projectId: string }) =>
51 Railway.bucketS3Credentials(input).pipe(Query.map(credentialsFields)),
52 );
53
54 const bucketCreate = Query.fn((input: { projectId: string; name: string }) =>
55 bucketFields(Railway.bucketCreate({ input })),
56 );
57
58 const bucketUpdate = Query.fn((id: string, name: string) =>
59 bucketFields(Railway.bucketUpdate({ id, input: { name } })),
60 );
61
62 /**
63 * A resource-valued prop: the resource itself, or an Effect that produces
64 * it (so `yield* Project(...)` and `Project(...)` both type-check).
65 */
66 type Ref<T> = T | Effect.Effect<T, never, Providers>;
67
68 /**
69 * Environment identity a Bucket instance is deployed into. Accepts a
70 * `Railway.Project` (its primary environment), a `Railway.Environment`,
71 * or an `{ environmentId }` stub.
72 */
73 export type BucketEnvironment = { readonly environmentId: string };
74
75 /** Default Railway bucket region (US West). Changing region replaces. */
76 export const DEFAULT_BUCKET_REGION = "sjc";
77
78 export interface BucketProps {
79 /**
80 * Parent Railway Project. Changing it replaces the Bucket.
81 */
82 project: Ref<Project>;
83 /**
84 * Environment to deploy the bucket instance into. Accepts a
85 * `Railway.Project` (primary environment), a `Railway.Environment`, or
86 * `{ environmentId }`. Defaults to the project's primary environment.
87 * Changing it replaces the Bucket.
88 */
89 environment?: Ref<BucketEnvironment>;
90 /**
91 * Display name. Unique per project. If omitted, a unique name is
92 * generated from the stack, stage and logical ID. Changing it updates
93 * in place via `bucketUpdate`. The S3 API name is this plus a hash
94 * (`s3BucketName`).
95 */
96 name?: string;
97 /**
98 * Bucket region (`sjc`, `iad`, `ams`, `sin`). Cannot be changed after
99 * create.
100 *
101 * @default "sjc"
102 */
103 region?: string;
104 }
105
106 export type Bucket = Resource<
107 "Railway.Bucket",
108 BucketProps,
109 {
110 /** Railway bucket id. */
111 bucketId: string;
112 /** Display name (unique per project). */
113 name: string;
114 /** Parent Railway project id. */
115 projectId: string;
116 /** Environment the instance is deployed in. */
117 environmentId: string;
118 /** Railway region code (`sjc`, `iad`, `ams`, `sin`). */
119 region: string | undefined;
120 /** Globally unique S3 bucket name (`{name}-{hash}`). */
121 s3BucketName: string | undefined;
122 /** S3 API endpoint (`https://storage.railway.app`). */
123 endpoint: string | undefined;
124 /** S3 URL style (`virtual` or `path`). */
125 urlStyle: string | undefined;
126 /** S3 region reported by credentials (often `auto`). */
127 s3Region: string | undefined;
128 /**
129 * S3 access key id from `bucketS3Credentials`. Never logged.
130 */
131 accessKeyId: Redacted.Redacted<string> | undefined;
132 /**
133 * S3 secret access key from `bucketS3Credentials`. Never logged.
134 */
135 secretAccessKey: Redacted.Redacted<string> | undefined;
136 /** RFC3339 creation timestamp. */
137 createdAt: string;
138 /** RFC3339 update timestamp. */
139 updatedAt: string;
140 },
141 never,
142 Providers
143 >;
144
145 const resolveBucketProps = (
146 props: BucketProps | Effect.Effect<BucketProps, never, Providers>,
147 ): Effect.Effect<BucketProps, never, Providers> =>
148 Effect.gen(function* () {
149 const resolved = Effect.isEffect(props) ? yield* props : props;
150 if (globalThis.__ALCHEMY_RUNTIME__) return resolved;
151 const project = Effect.isEffect(resolved.project)
152 ? yield* resolved.project as Effect.Effect<Project, never, Providers>
153 : resolved.project;
154 const environment =
155 resolved.environment === undefined
156 ? undefined
157 : Effect.isEffect(resolved.environment)
158 ? yield* resolved.environment as Effect.Effect<BucketEnvironment, never, Providers>
159 : resolved.environment;
160 return { ...resolved, project, environment };
161 });
162
163 const BucketResource = Resource<Bucket>("Railway.Bucket");
164
165 /**
166 * A Railway.Bucket is S3-compatible object storage in a Project. Each
167 * environment gets its own isolated instance and credentials. Bind
168 * {@link PutObject} / {@link GetObject} (and the other S3 object ops) in
169 * Service init — Alchemy injects the endpoint and credentials.
170 *
171 * Railway has no labels. Ownership is stamped into the display name via
172 * `createPhysicalName`. `name` updates in place. Changing `project`,
173 * `environment`, or `region` replaces the Bucket.
174 *
175 * @see https://docs.railway.com/storage-buckets
176 *
177 * ### Create a Bucket
178 * Pass a Project. Alchemy generates a unique name and deploys to the
179 * project's primary environment in `sjc`.
180 *
181 * **Example:** Generated name
182 * ```typescript
183 * const site = yield* Railway.Project("Site");
184 * const data = yield* Railway.Bucket("Data", { project: site });
185 * ```
186 *
187 * :::caution[Changing `project` replaces the Bucket]
188 * The Bucket is created in the new Project. The old Bucket is deleted.
189 * :::
190 *
191 * ### A stable name
192 * Pass `name` when you need a stable display name. The S3 API name is
193 * this plus a short hash (`s3BucketName`).
194 *
195 * **Example:** Explicit name
196 * ```typescript
197 * const data = yield* Railway.Bucket("Data", {
198 * project: site,
199 * name: "uploads",
200 * });
201 * ```
202 *
203 * ### Region
204 * Omit `region` to use `sjc`. Changing it replaces the Bucket.
205 *
206 * **Example:** Pin a region
207 * ```typescript
208 * const data = yield* Railway.Bucket("Data", {
209 * project: site,
210 * region: "iad",
211 * });
212 * ```
213 *
214 * :::caution[Changing `region` replaces the Bucket]
215 * Railway cannot move a bucket. A new Bucket is created, then the old
216 * one is deleted.
217 * :::
218 *
219 * ### Environment
220 * Defaults to the Project's primary environment. Pass a
221 * `Railway.Environment` (or `{ environmentId }`) to target another one.
222 *
223 * **Example:** Extra environment
224 * ```typescript
225 * const staging = yield* Railway.Environment("Staging", { project: site });
226 * const data = yield* Railway.Bucket("StagingData", {
227 * project: site,
228 * environment: staging,
229 * });
230 * ```
231 *
232 * :::caution[Changing `environment` replaces the Bucket]
233 * The Bucket instance is created in the new environment. The old
234 * instance is deleted.
235 * :::
236 *
237 * ### Put and get objects
238 * Railway buckets speak the S3 API. Bind {@link PutObject} /
239 * {@link GetObject} in Service init.
240 *
241 * **Example:** Put an object from a Service
242 * ```typescript
243 * import * as HttpServerResponse from "effect/http/HttpServerResponse";
244 *
245 * export default class Api extends Railway.Service<Api>()(
246 * "Api",
247 * { project: Site, main: import.meta.url },
248 * Effect.gen(function* () {
249 * const putObject = yield* Railway.PutObject(Data);
250 * const getObject = yield* Railway.GetObject(Data);
251 *
252 * return {
253 * fetch: Effect.gen(function* () {
254 * yield* putObject({
255 * Key: "hello.txt",
256 * Body: "hello",
257 * ContentType: "text/plain",
258 * });
259 * const obj = yield* getObject({ Key: "hello.txt" });
260 * return HttpServerResponse.json({ ok: obj.ETag !== undefined });
261 * }),
262 * };
263 * }).pipe(Effect.provide([Railway.PutObjectHttp, Railway.GetObjectHttp])),
264 * ) {}
265 * ```
266 *
267 * ### Module-scope declarations
268 * Declare the Project once. Pass it into every child. Resource-valued
269 * props accept the resource or an Effect producing it.
270 *
271 * **Example:** Module-scope Bucket
272 * ```typescript
273 * // src/data.ts
274 * import * as Railway from "@alchemy.run/railway";
275 *
276 * export const Site = Railway.Project("Site");
277 * export const Data = Railway.Bucket("Data", { project: Site });
278 * ```
279 *
280 * @resource
281 * @product Bucket
282 */
283 export const Bucket: typeof BucketResource = Object.assign(
284 (id: string, props: BucketProps | Effect.Effect<BucketProps, never, Providers>) =>
285 BucketResource(id, resolveBucketProps(props)),
286 BucketResource,
287 );
288
289 export class BucketNotCreated extends Data.TaggedError("Railway.BucketNotCreated")<{
290 resourceName: string;
291 projectId: string;
292 }> {}
293
294 export class BucketProjectRequired extends Data.TaggedError("Railway.BucketProjectRequired")<{
295 message: string;
296 }> {}
297
298 export class BucketEnvironmentRequired extends Data.TaggedError(
299 "Railway.BucketEnvironmentRequired",
300 )<{ message: string }> {}
301
302 class BucketCredentialsPending extends Data.TaggedError("Railway.BucketCredentialsPending")<{
303 bucketId: string;
304 }> {}
305
306 class BucketDeployPending extends Data.TaggedError("Railway.BucketDeployPending")<{
307 bucketId: string;
308 }> {}
309
310 type BucketInstanceConfig = {
311 region?: string | null;
312 isDeleted?: boolean | null;
313 isCreated?: boolean | null;
314 };
315
316 type EnvironmentConfigShape = { buckets?: Record<string, BucketInstanceConfig | null> | null };
317
318 const projectIdOf = (value: unknown): string | undefined => {
319 if (value === null || typeof value !== "object") return undefined;
320 const rec = value as { projectId?: unknown };
321 return typeof rec.projectId === "string" && rec.projectId.length > 0 ? rec.projectId : undefined;
322 };
323
324 const environmentIdOf = (value: unknown): string | undefined => {
325 if (value === null || typeof value !== "object") return undefined;
326 const rec = value as { environmentId?: unknown };
327 return typeof rec.environmentId === "string" && rec.environmentId.length > 0
328 ? rec.environmentId
329 : undefined;
330 };
331
332 const redact = (value: string | undefined): Redacted.Redacted<string> | undefined =>
333 value !== undefined && value.length > 0 ? Redacted.make(value) : undefined;
334
335 /** Railway marks `secretAccessKey` sensitive, so it already arrives `Redacted`. */
336 const nonEmpty = (
337 value: Redacted.Redacted<string> | undefined,
338 ): Redacted.Redacted<string> | undefined =>
339 value !== undefined && Redacted.value(value).length > 0 ? value : undefined;
340
341 const keepSecret = (
342 next: Redacted.Redacted<string> | undefined,
343 previous: Redacted.Redacted<string> | undefined,
344 ): Redacted.Redacted<string> | undefined => next ?? previous;
345
346 const parseEnvironmentConfig = (value: unknown): EnvironmentConfigShape => {
347 if (value === null || typeof value !== "object" || Array.isArray(value)) {
348 return {};
349 }
350 const buckets = (value as { buckets?: unknown }).buckets;
351 if (buckets === null || typeof buckets !== "object" || Array.isArray(buckets)) {
352 return {};
353 }
354 return { buckets: buckets as Record<string, BucketInstanceConfig | null> };
355 };
356
357 const instanceOf = (
358 config: EnvironmentConfigShape,
359 bucketId: string,
360 ): BucketInstanceConfig | undefined => {
361 const row = config.buckets?.[bucketId];
362 if (row === undefined || row === null) return undefined;
363 return row;
364 };
365
366 const isDeployed = (config: EnvironmentConfigShape, bucketId: string) => {
367 const row = instanceOf(config, bucketId);
368 return row !== undefined && row.isDeleted !== true;
369 };
370
371 const toAttrs = (
372 bucket: CloudBucket,
373 extra: {
374 environmentId: string;
375 region?: string;
376 credentials?: BucketS3CredentialsResultItem;
377 previous?: Bucket["Attributes"];
378 },
379 ): Bucket["Attributes"] => ({
380 bucketId: bucket.id,
381 name: bucket.name,
382 projectId: bucket.projectId,
383 environmentId: extra.environmentId,
384 region: extra.region ?? extra.previous?.region,
385 s3BucketName: extra.credentials?.bucketName ?? extra.previous?.s3BucketName,
386 endpoint: extra.credentials?.endpoint ?? extra.previous?.endpoint,
387 urlStyle: extra.credentials?.urlStyle ?? extra.previous?.urlStyle,
388 s3Region: extra.credentials?.region ?? extra.previous?.s3Region,
389 accessKeyId: keepSecret(redact(extra.credentials?.accessKeyId), extra.previous?.accessKeyId),
390 secretAccessKey: keepSecret(
391 nonEmpty(extra.credentials?.secretAccessKey),
392 extra.previous?.secretAccessKey,
393 ),
394 createdAt: bucket.createdAt,
395 updatedAt: bucket.updatedAt,
396 });
397
398 const resolveName = (id: string, name: string | undefined, existing?: string) =>
399 Effect.gen(function* () {
400 if (name !== undefined) return sanitizeRailwayName(name);
401 if (existing !== undefined) return existing;
402 return yield* createRailwayName(id);
403 });
404
405 const listProjectBuckets = (projectId: string) =>
406 projectBuckets(projectId, bucketFields).pipe(
407 Effect.catchTag("RailwayNotFound", () => Effect.succeed([] as CloudBucket[])),
408 );
409
410 const findInProject = (projectId: string, match: (bucket: CloudBucket) => boolean) =>
411 listProjectBuckets(projectId).pipe(Effect.map((buckets) => buckets.find(match)));
412
413 const getEnvironmentConfig = (environmentId: string, projectId: string) =>
414 readEnvironmentConfig(environmentId, projectId).pipe(
415 Effect.map((env) => parseEnvironmentConfig(env.config)),
416 Effect.retry({
417 while: (e) => e._tag === "RailwayNotFound",
418 times: 8,
419 schedule: Schedule.spaced("1 second"),
420 }),
421 Effect.catchTag("RailwayNotFound", () => Effect.succeed({} as EnvironmentConfigShape)),
422 );
423
424 const _environmentIdsOf = (project: { projectId: string; environmentId: string }) =>
425 Query.items(
426 Railway.environments({ projectId: project.projectId, first: 50 }).pipe(
427 Query.map((env) => ({ id: env.id, deletedAt: env.deletedAt })),
428 ),
429 ).pipe(
430 Stream.filter((env) => env.deletedAt == null),
431 Stream.map((env) => env.id),
432 Stream.runCollect,
433 Effect.map((ids) => {
434 const set = new Set(Array.from(ids));
435 if (project.environmentId.length > 0) {
436 set.add(project.environmentId);
437 }
438 return Array.from(set);
439 }),
440 Effect.catchTag("RailwayNotFound", () =>
441 Effect.succeed(project.environmentId.length > 0 ? [project.environmentId] : []),
442 ),
443 );
444
445 const commitBucketPatch = (input: {
446 environmentId: string;
447 commitMessage: string;
448 patch: Record<string, unknown>;
449 }) =>
450 withEnvironmentConfigLock(
451 input.environmentId,
452 environmentPatchCommit({
453 environmentId: input.environmentId,
454 commitMessage: input.commitMessage,
455 patch: input.patch,
456 }),
457 );
458
459 const ensureDeployed = Effect.fn(function* (input: {
460 bucketId: string;
461 environmentId: string;
462 projectId: string;
463 region: string;
464 name: string;
465 }) {
466 const config = yield* getEnvironmentConfig(input.environmentId, input.projectId);
467 if (isDeployed(config, input.bucketId)) {
468 return instanceOf(config, input.bucketId);
469 }
470 yield* commitBucketPatch({
471 environmentId: input.environmentId,
472 commitMessage: `Alchemy: create bucket ${input.name}`,
473 patch: { buckets: { [input.bucketId]: { region: input.region, isCreated: true } } },
474 }).pipe(
475 Effect.retry({
476 while: (e) => e._tag === "RailwayNotFound",
477 times: 8,
478 schedule: Schedule.spaced("1 second"),
479 }),
480 );
481 const synced = yield* getEnvironmentConfig(input.environmentId, input.projectId).pipe(
482 Effect.flatMap((next) => {
483 if (!isDeployed(next, input.bucketId)) {
484 return Effect.fail(new BucketDeployPending({ bucketId: input.bucketId }));
485 }
486 return Effect.succeed(instanceOf(next, input.bucketId));
487 }),
488 Effect.retry({
489 while: (e) => e._tag === "Railway.BucketDeployPending",
490 times: 8,
491 schedule: Schedule.spaced("1 second"),
492 }),
493 );
494 return synced;
495 });
496
497 const fetchCredentials = (input: { bucketId: string; environmentId: string; projectId: string }) =>
498 readBucketS3Credentials({
499 bucketId: input.bucketId,
500 environmentId: input.environmentId,
501 projectId: input.projectId,
502 }).pipe(
503 Effect.flatMap((items) => {
504 const first = items[0];
505 if (first === undefined) {
506 return Effect.fail(new BucketCredentialsPending({ bucketId: input.bucketId }));
507 }
508 return Effect.succeed(first);
509 }),
510 Effect.catchTag(["RailwayNotFound", "RailwayBucketCredentialsNotReady"], () =>
511 Effect.fail(new BucketCredentialsPending({ bucketId: input.bucketId })),
512 ),
513 Effect.retry({
514 while: (e) => e._tag === "Railway.BucketCredentialsPending",
515 times: 4,
516 schedule: Schedule.spaced("2 seconds"),
517 }),
518 );
519
520 const waitUntilGone = (input: { bucketId: string; projectId: string; environmentId: string }) =>
521 getEnvironmentConfig(input.environmentId, input.projectId).pipe(
522 Effect.flatMap((config) =>
523 !isDeployed(config, input.bucketId)
524 ? Effect.void
525 : Effect.fail(new BucketDeployPending({ bucketId: input.bucketId })),
526 ),
527 Effect.retry({
528 schedule: Schedule.spaced("1 second"),
529 while: (error) => error._tag === "Railway.BucketDeployPending",
530 times: 8,
531 }),
532 );
533
534 export const BucketProvider = () =>
535 Provider.succeed(Bucket, {
536 stables: ["bucketId", "projectId", "environmentId", "createdAt"],
537 nuke: { dependsOn: ["Railway.Project"] },
538
539 diff: Effect.fn(function* ({ news, output }) {
540 if (news === undefined || !isResolved(news)) return undefined;
541 if (output === undefined) return undefined;
542 const nextProject = projectIdOf(news.project);
543 const projectChanged = nextProject !== undefined && nextProject !== output.projectId;
544 const nextEnv = environmentIdOf(news.environment);
545 const environmentChanged = nextEnv !== undefined && nextEnv !== output.environmentId;
546 const desiredRegion = news.region ?? DEFAULT_BUCKET_REGION;
547 const regionChanged = output.region !== undefined && desiredRegion !== output.region;
548 if (projectChanged || environmentChanged || regionChanged) {
549 return { action: "replace" as const };
550 }
551 return undefined;
552 }),
553
554 read: Effect.fn(function* ({ id, olds, output }) {
555 const projectId =
556 output?.projectId ?? (olds !== undefined ? projectIdOf(olds.project) : undefined);
557 const environmentId =
558 output?.environmentId ??
559 (olds !== undefined
560 ? (environmentIdOf(olds.environment) ?? environmentIdOf(olds.project))
561 : undefined);
562 const name = yield* resolveName(id, olds?.name, output?.name);
563 if (projectId === undefined) return undefined;
564
565 const found =
566 (output?.bucketId !== undefined && output.bucketId.length > 0
567 ? yield* findInProject(projectId, (bucket) => bucket.id === output.bucketId)
568 : undefined) ?? (yield* findInProject(projectId, (bucket) => bucket.name === name));
569 if (found === undefined) return undefined;
570
571 const envId = environmentId ?? output?.environmentId ?? "";
572 const config =
573 envId.length > 0
574 ? yield* getEnvironmentConfig(envId, projectId)
575 : ({} as EnvironmentConfigShape);
576 if (envId.length > 0 && !isDeployed(config, found.id)) {
577 return undefined;
578 }
579 const attrs = toAttrs(found, {
580 environmentId: envId,
581 region: instanceOf(config, found.id)?.region ?? output?.region,
582 previous: output,
583 });
584 if (output !== undefined) return attrs;
585 return matchesAlchemyPhysicalName(found.name) ? attrs : Unowned(attrs);
586 }),
587
588 list: Effect.fn(function* () {
589 const projects = yield* ownedProjects();
590 const rows = yield* Effect.forEach(projects, (project) =>
591 Effect.gen(function* () {
592 const envIds = yield* projectEnvironmentIds(project);
593 const buckets = yield* listProjectBuckets(project.projectId);
594 const nested = yield* Effect.forEach(envIds, (environmentId) =>
595 getEnvironmentConfig(environmentId, project.projectId).pipe(
596 Effect.map((config) =>
597 buckets
598 .filter(
599 (bucket) =>
600 matchesAlchemyPhysicalName(bucket.name) && isDeployed(config, bucket.id),
601 )
602 .map((bucket) =>
603 toAttrs(bucket, {
604 environmentId,
605 region: instanceOf(config, bucket.id)?.region ?? undefined,
606 }),
607 ),
608 ),
609 ),
610 );
611 return nested.flat();
612 }),
613 );
614 const seen = new Set<string>();
615 const unique: Bucket["Attributes"][] = [];
616 for (const row of rows.flat()) {
617 const key = `${row.bucketId}:${row.environmentId}`;
618 if (seen.has(key)) continue;
619 seen.add(key);
620 unique.push(row);
621 }
622 return unique;
623 }),
624
625 reconcile: Effect.fn(function* ({ id, news, output }) {
626 const props = news ?? ({} as BucketProps);
627 const projectId = projectIdOf(props.project) ?? output?.projectId;
628 if (projectId === undefined) {
629 return yield* new BucketProjectRequired({
630 message: "Bucket requires a resolved Railway.Project",
631 });
632 }
633 const environmentId =
634 environmentIdOf(props.environment) ??
635 environmentIdOf(props.project) ??
636 output?.environmentId;
637 if (environmentId === undefined) {
638 return yield* new BucketEnvironmentRequired({
639 message:
640 "Bucket requires a Railway environment (pass environment or a Project with environmentId)",
641 });
642 }
643 const name = yield* resolveName(id, props.name, output?.name);
644 const region = props.region ?? output?.region ?? DEFAULT_BUCKET_REGION;
645
646 let current =
647 output?.bucketId !== undefined && output.bucketId.length > 0
648 ? yield* findInProject(projectId, (bucket) => bucket.id === output.bucketId)
649 : undefined;
650 if (current === undefined) {
651 current = yield* findInProject(projectId, (bucket) => bucket.name === name);
652 }
653
654 if (current === undefined) {
655 const created = yield* bucketCreate({ projectId, name }).pipe(
656 Effect.retry({
657 while: (e) => e._tag === "RailwayNotFound",
658 times: 8,
659 schedule: Schedule.spaced("1 second"),
660 }),
661 Effect.catchTag("RailwayValidationError", () => Effect.undefined),
662 );
663 current = created ?? (yield* findInProject(projectId, (bucket) => bucket.name === name));
664 }
665
666 if (current === undefined) {
667 return yield* new BucketNotCreated({ resourceName: name, projectId });
668 }
669
670 if (current.name !== name) {
671 current = yield* bucketUpdate(current.id, name);
672 }
673
674 const deployed = yield* ensureDeployed({
675 bucketId: current.id,
676 environmentId,
677 projectId,
678 region,
679 name,
680 });
681
682 const credentials = yield* fetchCredentials({
683 bucketId: current.id,
684 environmentId,
685 projectId,
686 });
687
688 return toAttrs(current, {
689 environmentId,
690 region: deployed?.region ?? region,
691 credentials,
692 previous: output,
693 });
694 }),
695
696 delete: Effect.fn(function* ({ output }) {
697 const bucketId = output.bucketId;
698 const environmentId = output.environmentId;
699 const projectId = output.projectId;
700 if (bucketId.length === 0 || environmentId.length === 0) return;
701 yield* commitBucketPatch({
702 environmentId,
703 commitMessage: `Alchemy: delete bucket ${output.name}`,
704 patch: { buckets: { [bucketId]: { isDeleted: true } } },
705 }).pipe(Effect.catchTag("RailwayNotFound", () => Effect.void));
706 if (projectId.length > 0) {
707 yield* waitUntilGone({ bucketId, projectId, environmentId });
708 }
709 }),
710 });