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 type { AddOnsResponseEdgesItemNode } from "@distilled.cloud/fly-io/addons"; |
| 2 | import * as addons from "@distilled.cloud/fly-io/addons"; |
| 3 | import * as machines from "@distilled.cloud/fly-io/machines"; |
| 4 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 5 | import { deepEqual, isResolved } from "alchemy/Diff"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import { Resource } from "alchemy/Resource"; |
| 8 | import { createInternalTags } from "alchemy/Tags"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import * as Duration from "effect/Duration"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Redacted from "effect/Redacted"; |
| 13 | import * as Result from "effect/Result"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | import { resolveOrgSlug } from "./Environment.ts"; |
| 16 | import { TigrisCredentialsMissing } from "./Errors.ts"; |
| 17 | import { createFlyAppName, matchesAlchemyPhysicalName } from "./Metadata.ts"; |
| 18 | import type { Providers } from "./Providers.ts"; |
| 19 | |
| 20 | /** Env-var names Tigris HTTP bindings write onto an App (same as `fly storage create`). */ |
| 21 | export const BUCKET_SECRET_NAMES = [ |
| 22 | "AWS_ACCESS_KEY_ID", |
| 23 | "AWS_SECRET_ACCESS_KEY", |
| 24 | "AWS_ENDPOINT_URL_S3", |
| 25 | "AWS_REGION", |
| 26 | "BUCKET_NAME", |
| 27 | ] as const; |
| 28 | |
| 29 | export type BucketSecretName = (typeof BUCKET_SECRET_NAMES)[number]; |
| 30 | |
| 31 | /** |
| 32 | * Shadow bucket used to migrate from another S3-compatible store. |
| 33 | * Every field is required when set. |
| 34 | */ |
| 35 | export interface BucketShadow { |
| 36 | /** Shadow-bucket access key id. */ |
| 37 | accessKey: string; |
| 38 | /** Shadow-bucket secret. Wrap with `Redacted.make`. */ |
| 39 | secretKey: Redacted.Redacted<string> | string; |
| 40 | /** Shadow-bucket region. */ |
| 41 | region: string; |
| 42 | /** Shadow-bucket name. */ |
| 43 | name: string; |
| 44 | /** Shadow-bucket S3 endpoint URL. */ |
| 45 | endpoint: string; |
| 46 | /** |
| 47 | * Dual-write new objects back to the shadow bucket. |
| 48 | * |
| 49 | * @default false |
| 50 | */ |
| 51 | writeThrough?: boolean; |
| 52 | } |
| 53 | |
| 54 | export interface BucketProps { |
| 55 | /** |
| 56 | * Tigris bucket / add-on name. Globally unique, DNS-compatible |
| 57 | * (lowercase letters, digits, hyphens), must start with a letter, |
| 58 | * max 63 characters. If omitted, a unique name is generated from |
| 59 | * the stack, stage and logical ID. Changing it replaces the Bucket. |
| 60 | */ |
| 61 | name?: string; |
| 62 | /** |
| 63 | * Organization slug. Defaults to the current token's org |
| 64 | * (`getCurrentToken`). Changing it replaces the Bucket. |
| 65 | */ |
| 66 | orgSlug?: string; |
| 67 | /** |
| 68 | * Serve objects without credentials. Default is private. |
| 69 | * |
| 70 | * @default false |
| 71 | */ |
| 72 | public?: boolean; |
| 73 | /** |
| 74 | * Custom domain for public website hosting. Maps to Tigris |
| 75 | * `options.website.domain_name`. Empty string when omitted |
| 76 | * (the Tigris API requires the field). |
| 77 | */ |
| 78 | domainName?: string; |
| 79 | /** |
| 80 | * Tigris accelerate. Default is off. |
| 81 | * |
| 82 | * @default false |
| 83 | */ |
| 84 | accelerate?: boolean; |
| 85 | /** |
| 86 | * Shadow bucket for zero-downtime migration from another |
| 87 | * S3-compatible store. Every nested field is required when set. |
| 88 | */ |
| 89 | shadowBucket?: BucketShadow; |
| 90 | } |
| 91 | |
| 92 | export type Bucket = Resource< |
| 93 | "Fly.Bucket", |
| 94 | BucketProps, |
| 95 | { |
| 96 | /** Fly GraphQL add-on id. Identity of the Bucket. */ |
| 97 | addOnId: string; |
| 98 | /** Physical Tigris add-on / bucket name. */ |
| 99 | name: string; |
| 100 | /** Observed provisioning status (`ready`, …). */ |
| 101 | status: string | undefined; |
| 102 | /** Organization slug. */ |
| 103 | orgSlug: string | undefined; |
| 104 | /** Whether the bucket is public. */ |
| 105 | public: boolean; |
| 106 | /** Custom website domain, if set. */ |
| 107 | domainName: string | undefined; |
| 108 | /** Tigris accelerate. */ |
| 109 | accelerate: boolean; |
| 110 | /** Tigris SSO dashboard URL, if the API returned one. */ |
| 111 | ssoLink: string | undefined; |
| 112 | /** RFC3339 creation timestamp. */ |
| 113 | createdAt: string | undefined; |
| 114 | /** |
| 115 | * `AWS_ACCESS_KEY_ID` from `createAddOn.environment`. Never logged. |
| 116 | */ |
| 117 | accessKeyId: Redacted.Redacted<string> | undefined; |
| 118 | /** |
| 119 | * `AWS_SECRET_ACCESS_KEY` from `createAddOn.environment`. Never logged. |
| 120 | */ |
| 121 | secretAccessKey: Redacted.Redacted<string> | undefined; |
| 122 | /** |
| 123 | * `AWS_ENDPOINT_URL_S3` from `createAddOn.environment`. Never logged. |
| 124 | */ |
| 125 | endpoint: Redacted.Redacted<string> | undefined; |
| 126 | /** |
| 127 | * `AWS_REGION` from `createAddOn.environment`. Never logged. |
| 128 | */ |
| 129 | region: Redacted.Redacted<string> | undefined; |
| 130 | /** |
| 131 | * `BUCKET_NAME` from `createAddOn.environment`. Never logged. |
| 132 | */ |
| 133 | bucketName: Redacted.Redacted<string> | undefined; |
| 134 | }, |
| 135 | never, |
| 136 | Providers |
| 137 | >; |
| 138 | |
| 139 | /** |
| 140 | * A Fly.Bucket is Tigris S3-compatible object storage, billed through |
| 141 | * Fly. It is an org-scoped GraphQL add-on (`AddOnType` `tigris`). |
| 142 | * |
| 143 | * @see https://fly.io/docs/tigris/ |
| 144 | * |
| 145 | * ### Create a Bucket |
| 146 | * Alchemy generates a unique name unless you pass one. |
| 147 | * |
| 148 | * **Example:** Generated name |
| 149 | * ```typescript |
| 150 | * const data = yield* Fly.Bucket("Data"); |
| 151 | * ``` |
| 152 | * |
| 153 | * ### A stable name |
| 154 | * Pass `name` when you need a stable Tigris bucket name. |
| 155 | * |
| 156 | * **Example:** Explicit name |
| 157 | * ```typescript |
| 158 | * const data = yield* Fly.Bucket("Data", { |
| 159 | * name: "my-bucket", |
| 160 | * }); |
| 161 | * ``` |
| 162 | * |
| 163 | * :::caution[Changing `name` replaces the Bucket] |
| 164 | * Tigris cannot rename a bucket. Alchemy creates the new name, then |
| 165 | * deletes the old one. |
| 166 | * ::: |
| 167 | * |
| 168 | * ### Organization |
| 169 | * Org defaults to the current token. Pass `orgSlug` to pin it. |
| 170 | * |
| 171 | * **Example:** Pin an org |
| 172 | * ```typescript |
| 173 | * const data = yield* Fly.Bucket("Data", { |
| 174 | * orgSlug: "my-org", |
| 175 | * }); |
| 176 | * ``` |
| 177 | * |
| 178 | * :::caution[Changing `orgSlug` replaces the Bucket] |
| 179 | * The bucket is created in the new org. The old bucket is deleted. |
| 180 | * ::: |
| 181 | * |
| 182 | * ### Public access |
| 183 | * Buckets are private by default. `public: true` serves objects |
| 184 | * without credentials. |
| 185 | * |
| 186 | * **Example:** Public bucket |
| 187 | * ```typescript |
| 188 | * const assets = yield* Fly.Bucket("Assets", { |
| 189 | * public: true, |
| 190 | * }); |
| 191 | * ``` |
| 192 | * |
| 193 | * ### Custom domain |
| 194 | * `domainName` sets `website.domain_name` on the add-on. Point a |
| 195 | * CNAME at `{name}.t3.tigrisbucket.io`. |
| 196 | * |
| 197 | * **Example:** Custom domain |
| 198 | * ```typescript |
| 199 | * const assets = yield* Fly.Bucket("Assets", { |
| 200 | * public: true, |
| 201 | * domainName: "assets.example.com", |
| 202 | * }); |
| 203 | * ``` |
| 204 | * |
| 205 | * ### Put and get objects |
| 206 | * Tigris speaks the S3 API. Bind {@link PutObject} / {@link GetObject} |
| 207 | * (and the other S3 object ops) in Service init. Alchemy injects the |
| 208 | * Tigris endpoint and credentials; you never read `AWS_*` yourself. |
| 209 | * |
| 210 | * **Example:** Put an object from a Service |
| 211 | * ```typescript |
| 212 | * import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 213 | * |
| 214 | * export default class Api extends Fly.Service<Api>()( |
| 215 | * "Api", |
| 216 | * { app: Site, main: import.meta.url, port: 3000 }, |
| 217 | * Effect.gen(function* () { |
| 218 | * const putObject = yield* Fly.PutObject(Data); |
| 219 | * const getObject = yield* Fly.GetObject(Data); |
| 220 | * |
| 221 | * return { |
| 222 | * fetch: Effect.gen(function* () { |
| 223 | * yield* putObject({ |
| 224 | * Key: "hello.txt", |
| 225 | * Body: "hello", |
| 226 | * ContentType: "text/plain", |
| 227 | * }); |
| 228 | * const obj = yield* getObject({ Key: "hello.txt" }); |
| 229 | * return HttpServerResponse.json({ ok: obj.ETag !== undefined }); |
| 230 | * }), |
| 231 | * }; |
| 232 | * }).pipe(Effect.provide([Fly.PutObjectHttp, Fly.GetObjectHttp])), |
| 233 | * ) {} |
| 234 | * ``` |
| 235 | * |
| 236 | * ### Fly.Secret |
| 237 | * You can also set each key yourself with {@link Secret}. |
| 238 | * |
| 239 | * **Example:** Manual secrets |
| 240 | * ```typescript |
| 241 | * yield* Fly.Secret("BucketName", { |
| 242 | * app: Site, |
| 243 | * name: "BUCKET_NAME", |
| 244 | * value: Data.bucketName, |
| 245 | * }); |
| 246 | * ``` |
| 247 | * |
| 248 | * @resource |
| 249 | * @product Bucket |
| 250 | */ |
| 251 | export const Bucket = Resource<Bucket>("Fly.Bucket"); |
| 252 | |
| 253 | export class BucketNotCreated extends Data.TaggedError("Fly.BucketNotCreated")<{ |
| 254 | resourceName: string; |
| 255 | }> {} |
| 256 | |
| 257 | export class BucketOrgMissing extends Data.TaggedError("Fly.BucketOrgMissing")<{ |
| 258 | orgSlug: string; |
| 259 | }> {} |
| 260 | |
| 261 | export class BucketProvisionFailed extends Data.TaggedError("Fly.BucketProvisionFailed")<{ |
| 262 | resourceName: string; |
| 263 | status: string | undefined; |
| 264 | errorMessage: string | undefined; |
| 265 | }> {} |
| 266 | |
| 267 | class BucketPending extends Data.TaggedError("Fly.BucketPending")<{ |
| 268 | resourceName: string; |
| 269 | status: string; |
| 270 | }> {} |
| 271 | |
| 272 | const TIGRIS = "tigris" as const; |
| 273 | |
| 274 | const backoff = Schedule.min([ |
| 275 | Schedule.exponential(Duration.millis(500), 1.5), |
| 276 | Schedule.spaced(Duration.seconds(5)), |
| 277 | ]); |
| 278 | |
| 279 | const sanitizeFlyBucketName = (name: string): string => { |
| 280 | const lowered = name |
| 281 | .toLowerCase() |
| 282 | .replace(/[^a-z0-9-]+/g, "-") |
| 283 | .replace(/-+/g, "-") |
| 284 | .replace(/^-+|-+$/g, ""); |
| 285 | const clipped = lowered.length > 63 ? lowered.slice(0, 63).replace(/-+$/g, "") : lowered; |
| 286 | const raw = clipped.length === 0 ? "f" : clipped; |
| 287 | return /^[a-z]/.test(raw) ? raw : `f${raw}`.slice(0, 63); |
| 288 | }; |
| 289 | |
| 290 | const resolveBucketName = (id: string, name: string | undefined, existing?: string) => |
| 291 | Effect.gen(function* () { |
| 292 | if (name !== undefined) return sanitizeFlyBucketName(name); |
| 293 | if (existing !== undefined) return existing; |
| 294 | return yield* createFlyAppName(id); |
| 295 | }); |
| 296 | |
| 297 | const asRecord = (value: unknown): Record<string, unknown> => { |
| 298 | if (Redacted.isRedacted(value)) { |
| 299 | return value.pipe(Redacted.value, asRecord); |
| 300 | } |
| 301 | if (typeof value === "string") { |
| 302 | try { |
| 303 | return asRecord(JSON.parse(value)); |
| 304 | } catch { |
| 305 | return {}; |
| 306 | } |
| 307 | } |
| 308 | if (value !== null && typeof value === "object" && !Array.isArray(value)) { |
| 309 | return value as Record<string, unknown>; |
| 310 | } |
| 311 | return {}; |
| 312 | }; |
| 313 | |
| 314 | const asString = (value: unknown): string | undefined => { |
| 315 | if (typeof value === "string") { |
| 316 | return value.length > 0 ? value : undefined; |
| 317 | } |
| 318 | if (Redacted.isRedacted(value)) { |
| 319 | return value.pipe(Redacted.value, asString); |
| 320 | } |
| 321 | return undefined; |
| 322 | }; |
| 323 | |
| 324 | const redact = (value: string | undefined): Redacted.Redacted<string> | undefined => |
| 325 | value !== undefined && value.length > 0 ? Redacted.make(value) : undefined; |
| 326 | |
| 327 | const keepSecret = ( |
| 328 | next: Redacted.Redacted<string> | undefined, |
| 329 | previous: Redacted.Redacted<string> | undefined, |
| 330 | ): Redacted.Redacted<string> | undefined => next ?? previous; |
| 331 | |
| 332 | const envString = (env: Record<string, unknown>, key: BucketSecretName): string | undefined => |
| 333 | asString(env[key]); |
| 334 | |
| 335 | type ObservedAddOn = Pick< |
| 336 | AddOnsResponseEdgesItemNode, |
| 337 | | "id" |
| 338 | | "name" |
| 339 | | "status" |
| 340 | | "errorMessage" |
| 341 | | "environment" |
| 342 | | "options" |
| 343 | | "metadata" |
| 344 | | "ssoLink" |
| 345 | | "createdAt" |
| 346 | > & { |
| 347 | organization?: { slug?: string | null; rawSlug?: string } | null; |
| 348 | }; |
| 349 | |
| 350 | const unwrapSecret = (value: Redacted.Redacted<string> | string): string => |
| 351 | Redacted.isRedacted(value) ? Redacted.value(value) : value; |
| 352 | |
| 353 | const desiredOptions = (props: BucketProps): Record<string, unknown> => { |
| 354 | const options: Record<string, unknown> = { |
| 355 | public: props.public ?? false, |
| 356 | accelerate: props.accelerate ?? false, |
| 357 | website: { domain_name: props.domainName ?? "" }, |
| 358 | }; |
| 359 | if (props.shadowBucket !== undefined) { |
| 360 | options.shadow_bucket = { |
| 361 | access_key: props.shadowBucket.accessKey, |
| 362 | secret_key: unwrapSecret(props.shadowBucket.secretKey), |
| 363 | region: props.shadowBucket.region, |
| 364 | name: props.shadowBucket.name, |
| 365 | endpoint: props.shadowBucket.endpoint, |
| 366 | write_through: props.shadowBucket.writeThrough ?? false, |
| 367 | }; |
| 368 | } |
| 369 | return options; |
| 370 | }; |
| 371 | |
| 372 | const websiteDomain = (options: Record<string, unknown>): string | undefined => { |
| 373 | const website = asRecord(options.website); |
| 374 | return asString(website.domain_name); |
| 375 | }; |
| 376 | |
| 377 | const toAttrs = ( |
| 378 | addOn: ObservedAddOn, |
| 379 | previous?: Bucket["Attributes"], |
| 380 | fallback?: { name?: string; orgSlug?: string }, |
| 381 | ): Bucket["Attributes"] => { |
| 382 | const options = asRecord(addOn.options); |
| 383 | const env = asRecord(addOn.environment); |
| 384 | const name = addOn.name ?? fallback?.name ?? previous?.name ?? ""; |
| 385 | const orgSlug = |
| 386 | asString(addOn.organization?.slug) ?? |
| 387 | asString(addOn.organization?.rawSlug) ?? |
| 388 | fallback?.orgSlug ?? |
| 389 | previous?.orgSlug; |
| 390 | return { |
| 391 | addOnId: addOn.id, |
| 392 | name, |
| 393 | status: addOn.status ?? undefined, |
| 394 | orgSlug, |
| 395 | public: typeof options.public === "boolean" ? options.public : (previous?.public ?? false), |
| 396 | domainName: websiteDomain(options), |
| 397 | accelerate: options.accelerate === true, |
| 398 | ssoLink: addOn.ssoLink ?? undefined, |
| 399 | createdAt: addOn.createdAt, |
| 400 | accessKeyId: keepSecret(redact(envString(env, "AWS_ACCESS_KEY_ID")), previous?.accessKeyId), |
| 401 | secretAccessKey: keepSecret( |
| 402 | redact(envString(env, "AWS_SECRET_ACCESS_KEY")), |
| 403 | previous?.secretAccessKey, |
| 404 | ), |
| 405 | endpoint: keepSecret(redact(envString(env, "AWS_ENDPOINT_URL_S3")), previous?.endpoint), |
| 406 | region: keepSecret(redact(envString(env, "AWS_REGION")), previous?.region), |
| 407 | bucketName: keepSecret(redact(envString(env, "BUCKET_NAME") ?? name), previous?.bucketName), |
| 408 | }; |
| 409 | }; |
| 410 | |
| 411 | const stringRecordOf = (value: unknown): Record<string, string> => |
| 412 | Object.fromEntries( |
| 413 | Object.entries(asRecord(value)).flatMap(([key, item]) => { |
| 414 | const text = asString(item); |
| 415 | return text !== undefined ? [[key, text]] : []; |
| 416 | }), |
| 417 | ); |
| 418 | |
| 419 | const hasAlchemyMetadata = (metadata: unknown): boolean => { |
| 420 | const tags = stringRecordOf(metadata); |
| 421 | const stack = tags["alchemy::stack"] ?? tags["alchemy.stack"]; |
| 422 | return stack !== undefined && stack.length > 0; |
| 423 | }; |
| 424 | |
| 425 | const isOwnedAddOn = (addOn: ObservedAddOn): boolean => |
| 426 | matchesAlchemyPhysicalName(addOn.name ?? undefined) || hasAlchemyMetadata(addOn.metadata); |
| 427 | |
| 428 | export const listTigrisAddOns = Effect.fn(function* () { |
| 429 | const rows: AddOnsResponseEdgesItemNode[] = []; |
| 430 | let after: string | undefined; |
| 431 | for (let i = 0; i < 8; i++) { |
| 432 | const page = yield* addons.addOns({ |
| 433 | type: TIGRIS, |
| 434 | first: 50, |
| 435 | after, |
| 436 | }); |
| 437 | for (const edge of page.edges ?? []) { |
| 438 | if (edge?.node != null) rows.push(edge.node); |
| 439 | } |
| 440 | if (!page.pageInfo.hasNextPage) break; |
| 441 | after = page.pageInfo.endCursor ?? undefined; |
| 442 | if (after === undefined || after.length === 0) break; |
| 443 | } |
| 444 | return rows; |
| 445 | }); |
| 446 | |
| 447 | const findById = (addOnId: string) => |
| 448 | listTigrisAddOns().pipe(Effect.map((addOns) => addOns.find((addOn) => addOn.id === addOnId))); |
| 449 | |
| 450 | const findByName = (name: string) => |
| 451 | listTigrisAddOns().pipe(Effect.map((addOns) => addOns.find((addOn) => addOn.name === name))); |
| 452 | |
| 453 | const failedStatus = (status: string | undefined) => status === "error" || status === "failed"; |
| 454 | |
| 455 | const pendingStatus = (status: string | undefined) => |
| 456 | status === "creating" || |
| 457 | status === "provisioning" || |
| 458 | status === "pending" || |
| 459 | status === "pending_create"; |
| 460 | |
| 461 | const waitUntilReady = (name: string, addOnId: string) => |
| 462 | findById(addOnId).pipe( |
| 463 | Effect.flatMap( |
| 464 | ( |
| 465 | addOn, |
| 466 | ): Effect.Effect<AddOnsResponseEdgesItemNode, BucketPending | BucketProvisionFailed> => { |
| 467 | if (addOn === undefined) { |
| 468 | return Effect.fail(new BucketPending({ resourceName: name, status: "missing" })); |
| 469 | } |
| 470 | const status = addOn.status ?? undefined; |
| 471 | if (failedStatus(status)) { |
| 472 | return Effect.fail( |
| 473 | new BucketProvisionFailed({ |
| 474 | resourceName: name, |
| 475 | status, |
| 476 | errorMessage: addOn.errorMessage ?? undefined, |
| 477 | }), |
| 478 | ); |
| 479 | } |
| 480 | if (pendingStatus(status)) { |
| 481 | return Effect.fail( |
| 482 | new BucketPending({ resourceName: name, status: status ?? "creating" }), |
| 483 | ); |
| 484 | } |
| 485 | return Effect.succeed(addOn); |
| 486 | }, |
| 487 | ), |
| 488 | Effect.retry({ |
| 489 | while: (e) => e._tag === "Fly.BucketPending", |
| 490 | times: 8, |
| 491 | schedule: backoff, |
| 492 | }), |
| 493 | Effect.catchTag("Fly.BucketPending", () => findById(addOnId)), |
| 494 | ); |
| 495 | |
| 496 | const waitUntilGone = (addOnId: string, name: string) => |
| 497 | Effect.gen(function* () { |
| 498 | // Best-effort: Tigris often answers "The specified bucket does not |
| 499 | // exist" (untyped UnknownFlyIoError) for a delete that already |
| 500 | // succeeded. Never fail the wait on that — only on the add-on still |
| 501 | // showing up in list. |
| 502 | yield* Effect.result( |
| 503 | addons.deleteAddOn({ |
| 504 | input: addOnId.length > 0 ? { addOnId } : { name, provider: TIGRIS }, |
| 505 | }), |
| 506 | ); |
| 507 | const still = |
| 508 | (yield* findById(addOnId)) ?? (name.length > 0 ? yield* findByName(name) : undefined); |
| 509 | if (still !== undefined) { |
| 510 | return yield* new BucketPending({ |
| 511 | resourceName: name, |
| 512 | status: still.status ?? "deleting", |
| 513 | }); |
| 514 | } |
| 515 | }).pipe( |
| 516 | Effect.retry({ |
| 517 | while: (e) => e._tag === "Fly.BucketPending", |
| 518 | schedule: Schedule.spaced("2 seconds"), |
| 519 | times: 20, |
| 520 | }), |
| 521 | ); |
| 522 | |
| 523 | const resolveOrganization = (orgSlug: string) => |
| 524 | addons |
| 525 | .organization({ slug: orgSlug }) |
| 526 | .pipe(Effect.catchTag("FlyIoParseError", () => Effect.undefined)); |
| 527 | |
| 528 | const ensureTos = (orgSlug: string, organizationId: string) => |
| 529 | Effect.gen(function* () { |
| 530 | const agreed = yield* addons.agreedToProviderTos({ |
| 531 | slug: orgSlug, |
| 532 | providerName: TIGRIS, |
| 533 | }); |
| 534 | if (agreed === true) return; |
| 535 | const tos = yield* Effect.result( |
| 536 | addons.createExtensionTosAgreement({ |
| 537 | input: { |
| 538 | addOnProviderName: TIGRIS, |
| 539 | organizationId, |
| 540 | }, |
| 541 | }), |
| 542 | ); |
| 543 | if (Result.isFailure(tos)) return; |
| 544 | }); |
| 545 | |
| 546 | const optionsEqual = ( |
| 547 | observed: Record<string, unknown>, |
| 548 | desired: Record<string, unknown>, |
| 549 | ): boolean => { |
| 550 | const observedPublic = observed.public === true; |
| 551 | const desiredPublic = desired.public === true; |
| 552 | const observedAccelerate = observed.accelerate === true; |
| 553 | const desiredAccelerate = desired.accelerate === true; |
| 554 | const observedDomain = websiteDomain(observed) ?? ""; |
| 555 | const desiredDomain = websiteDomain(desired) ?? ""; |
| 556 | if ( |
| 557 | observedPublic !== desiredPublic || |
| 558 | observedAccelerate !== desiredAccelerate || |
| 559 | observedDomain !== desiredDomain |
| 560 | ) { |
| 561 | return false; |
| 562 | } |
| 563 | if (desired.shadow_bucket !== undefined) { |
| 564 | return deepEqual(asRecord(observed.shadow_bucket), desired.shadow_bucket); |
| 565 | } |
| 566 | return true; |
| 567 | }; |
| 568 | |
| 569 | export const BucketProvider = () => |
| 570 | Provider.succeed(Bucket, { |
| 571 | stables: ["addOnId", "name", "orgSlug"], |
| 572 | |
| 573 | diff: Effect.fn(function* ({ news, output }) { |
| 574 | if (news === undefined || !isResolved(news)) return undefined; |
| 575 | if (output === undefined) return undefined; |
| 576 | const desiredName = news.name !== undefined ? sanitizeFlyBucketName(news.name) : output.name; |
| 577 | const nameChanged = desiredName !== output.name; |
| 578 | const orgChanged = news.orgSlug !== undefined && news.orgSlug !== output.orgSlug; |
| 579 | if (nameChanged || orgChanged) { |
| 580 | return { action: "replace" as const }; |
| 581 | } |
| 582 | return undefined; |
| 583 | }), |
| 584 | |
| 585 | read: Effect.fn(function* ({ id, olds, output }) { |
| 586 | const name = yield* resolveBucketName(id, olds?.name, output?.name); |
| 587 | const found = |
| 588 | (output?.addOnId !== undefined && output.addOnId.length > 0 |
| 589 | ? yield* findById(output.addOnId) |
| 590 | : undefined) ?? (yield* findByName(name)); |
| 591 | if (found === undefined) return undefined; |
| 592 | const attrs = toAttrs(found, output, { |
| 593 | name, |
| 594 | orgSlug: output?.orgSlug ?? olds?.orgSlug, |
| 595 | }); |
| 596 | if (output !== undefined) return attrs; |
| 597 | return isOwnedAddOn(found) ? attrs : Unowned(attrs); |
| 598 | }), |
| 599 | |
| 600 | list: Effect.fn(function* () { |
| 601 | const addOns = yield* listTigrisAddOns(); |
| 602 | return addOns.flatMap((addOn) => (isOwnedAddOn(addOn) ? [toAttrs(addOn)] : [])); |
| 603 | }), |
| 604 | |
| 605 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 606 | const props = news ?? ({} as BucketProps); |
| 607 | const name = yield* resolveBucketName(id, props.name, output?.name); |
| 608 | const orgSlug = props.orgSlug ?? output?.orgSlug ?? (yield* resolveOrgSlug()); |
| 609 | let previous = output; |
| 610 | |
| 611 | // Observe by cached id, then the desired name. |
| 612 | let current: ObservedAddOn | undefined = |
| 613 | output?.addOnId !== undefined && output.addOnId.length > 0 |
| 614 | ? yield* findById(output.addOnId) |
| 615 | : undefined; |
| 616 | if (current === undefined) { |
| 617 | current = yield* findByName(name); |
| 618 | } |
| 619 | |
| 620 | const org = yield* resolveOrganization(orgSlug); |
| 621 | if (org === undefined) { |
| 622 | return yield* new BucketOrgMissing({ orgSlug }); |
| 623 | } |
| 624 | |
| 625 | if (current === undefined) { |
| 626 | yield* ensureTos(orgSlug, org.id); |
| 627 | const created = yield* Effect.result( |
| 628 | addons.createAddOn({ |
| 629 | input: { |
| 630 | type: TIGRIS, |
| 631 | name, |
| 632 | organizationId: org.id, |
| 633 | options: desiredOptions(props), |
| 634 | }, |
| 635 | }), |
| 636 | ); |
| 637 | if (Result.isSuccess(created)) { |
| 638 | current = created.success.addOn; |
| 639 | } else { |
| 640 | current = yield* findByName(name); |
| 641 | if (current === undefined) { |
| 642 | return yield* created.failure; |
| 643 | } |
| 644 | } |
| 645 | const createdSecrets = current; |
| 646 | if (current.id.length > 0) { |
| 647 | current = (yield* waitUntilReady(name, current.id)) ?? current; |
| 648 | } |
| 649 | // List/get omit `environment` after create. Keep the create |
| 650 | // payload as the secrets source for keepSecret below. |
| 651 | previous = toAttrs(createdSecrets, previous, { name, orgSlug }); |
| 652 | } |
| 653 | |
| 654 | if (current === undefined || current.id.length === 0) { |
| 655 | return yield* new BucketNotCreated({ resourceName: name }); |
| 656 | } |
| 657 | |
| 658 | const status = current.status ?? undefined; |
| 659 | if (failedStatus(status)) { |
| 660 | return yield* new BucketProvisionFailed({ |
| 661 | resourceName: name, |
| 662 | status, |
| 663 | errorMessage: current.errorMessage ?? undefined, |
| 664 | }); |
| 665 | } |
| 666 | |
| 667 | const observedOptions = asRecord(current.options); |
| 668 | const desired = desiredOptions(props); |
| 669 | const internal = yield* createInternalTags(id); |
| 670 | const observedMeta = stringRecordOf(current.metadata); |
| 671 | const nextMeta = { ...observedMeta, ...internal }; |
| 672 | const optionsChanged = !optionsEqual(observedOptions, desired); |
| 673 | const metaChanged = !deepEqual(observedMeta, nextMeta); |
| 674 | if (optionsChanged || metaChanged) { |
| 675 | const nextOptions = { |
| 676 | ...observedOptions, |
| 677 | ...desired, |
| 678 | }; |
| 679 | const updated = yield* Effect.result( |
| 680 | addons.updateAddOn({ |
| 681 | input: { |
| 682 | addOnId: current.id, |
| 683 | options: optionsChanged ? nextOptions : undefined, |
| 684 | metadata: metaChanged ? nextMeta : undefined, |
| 685 | }, |
| 686 | }), |
| 687 | ); |
| 688 | if (Result.isSuccess(updated)) { |
| 689 | current = { |
| 690 | ...current, |
| 691 | ...updated.success.addOn, |
| 692 | }; |
| 693 | } else { |
| 694 | current = (yield* findById(current.id)) ?? current; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | const latest = (yield* findById(current.id)) ?? current; |
| 699 | // GraphQL list/get omit `environment` after create. Keep credentials |
| 700 | // from the create payload (or last persisted attrs) via keepSecret. |
| 701 | const attrs = toAttrs(latest, toAttrs(current, previous, { name, orgSlug }), { |
| 702 | name, |
| 703 | orgSlug, |
| 704 | }); |
| 705 | const observedPublic = asRecord(latest.options).public; |
| 706 | return { |
| 707 | ...attrs, |
| 708 | public: |
| 709 | typeof observedPublic === "boolean" ? observedPublic : (props.public ?? attrs.public), |
| 710 | domainName: websiteDomain(asRecord(latest.options)) ?? props.domainName ?? attrs.domainName, |
| 711 | accelerate: |
| 712 | typeof asRecord(latest.options).accelerate === "boolean" |
| 713 | ? (asRecord(latest.options).accelerate as boolean) |
| 714 | : (props.accelerate ?? attrs.accelerate), |
| 715 | }; |
| 716 | }), |
| 717 | |
| 718 | delete: Effect.fn(function* ({ output }) { |
| 719 | const addOnId = output.addOnId; |
| 720 | const name = output.name; |
| 721 | if (addOnId.length === 0 && name.length === 0) return; |
| 722 | // Always wait for the add-on to leave the list. A failed |
| 723 | // deleteAddOn plus a single list miss is not gone — Tigris |
| 724 | // list lags, and a bucket still attached to an App is a |
| 725 | // transient error. waitUntilGone retries delete and only |
| 726 | // succeeds once list (id and name) is empty. |
| 727 | yield* waitUntilGone(addOnId, name); |
| 728 | }), |
| 729 | }); |
| 730 | |
| 731 | class BucketSecretVersionMissing extends Data.TaggedError("Fly.BucketSecretVersionMissing")<{ |
| 732 | appName: string; |
| 733 | }> {} |
| 734 | |
| 735 | const bucketSecretVersion = ( |
| 736 | appName: string, |
| 737 | response: machines.AppSecretsUpdateResp | machines.SetAppSecretResponse, |
| 738 | ) => { |
| 739 | const version = response.version ?? response.Version; |
| 740 | return version !== undefined && Number.isSafeInteger(version) && version >= 0 |
| 741 | ? Effect.succeed(version) |
| 742 | : Effect.fail(new BucketSecretVersionMissing({ appName })); |
| 743 | }; |
| 744 | |
| 745 | const requireBucketSecrets = Effect.fn(function* (appName: string, values: Record<string, string>) { |
| 746 | const required = [ |
| 747 | "BUCKET_NAME", |
| 748 | "AWS_ACCESS_KEY_ID", |
| 749 | "AWS_SECRET_ACCESS_KEY", |
| 750 | "AWS_ENDPOINT_URL_S3", |
| 751 | ]; |
| 752 | if (required.some((key) => !values[key]?.trim())) { |
| 753 | return yield* new TigrisCredentialsMissing({ |
| 754 | resourceName: values.BUCKET_NAME || appName, |
| 755 | }); |
| 756 | } |
| 757 | }); |
| 758 | |
| 759 | const putSecretValues = (appName: string, values: Record<string, string>) => |
| 760 | Effect.gen(function* () { |
| 761 | yield* requireBucketSecrets(appName, values); |
| 762 | return yield* machines |
| 763 | .updateSecrets({ |
| 764 | app_name: appName, |
| 765 | values, |
| 766 | }) |
| 767 | .pipe( |
| 768 | Effect.flatMap((response) => bucketSecretVersion(appName, response)), |
| 769 | Effect.catchTag("NotFound", () => |
| 770 | Effect.gen(function* () { |
| 771 | const versions: number[] = []; |
| 772 | for (const [secretName, value] of Object.entries(values)) { |
| 773 | const version = yield* machines |
| 774 | .createSecret({ |
| 775 | app_name: appName, |
| 776 | secret_name: secretName, |
| 777 | value, |
| 778 | }) |
| 779 | .pipe( |
| 780 | Effect.flatMap((response) => bucketSecretVersion(appName, response)), |
| 781 | Effect.catchTag("Conflict", () => |
| 782 | machines |
| 783 | .updateSecrets({ |
| 784 | app_name: appName, |
| 785 | values: { [secretName]: value }, |
| 786 | }) |
| 787 | .pipe(Effect.flatMap((response) => bucketSecretVersion(appName, response))), |
| 788 | ), |
| 789 | ); |
| 790 | versions.push(version); |
| 791 | } |
| 792 | return Math.max(...versions); |
| 793 | }), |
| 794 | ), |
| 795 | ); |
| 796 | }); |
| 797 | |
| 798 | const envValuesOf = (addOn: ObservedAddOn): Record<string, string> => { |
| 799 | const env = asRecord(addOn.environment); |
| 800 | const values: Record<string, string> = {}; |
| 801 | for (const key of BUCKET_SECRET_NAMES) { |
| 802 | const value = envString(env, key); |
| 803 | if (value !== undefined) values[key] = value; |
| 804 | } |
| 805 | if (values.BUCKET_NAME === undefined && addOn.name != null) { |
| 806 | values.BUCKET_NAME = addOn.name; |
| 807 | } |
| 808 | if (values.AWS_ENDPOINT_URL === undefined && values.AWS_ENDPOINT_URL_S3 !== undefined) { |
| 809 | values.AWS_ENDPOINT_URL = values.AWS_ENDPOINT_URL_S3; |
| 810 | } |
| 811 | return values; |
| 812 | }; |
| 813 | |
| 814 | const findAttachedBucket = (input: { id?: string; name: string }) => |
| 815 | Effect.gen(function* () { |
| 816 | const listed = |
| 817 | input.id !== undefined && input.id.length > 0 |
| 818 | ? ((yield* findById(input.id)) ?? |
| 819 | (input.name.length > 0 ? yield* findByName(input.name) : undefined)) |
| 820 | : input.name.length > 0 |
| 821 | ? yield* findByName(input.name) |
| 822 | : undefined; |
| 823 | if (listed === undefined) { |
| 824 | return yield* new BucketPending({ |
| 825 | resourceName: input.name || input.id || "", |
| 826 | status: "missing", |
| 827 | }); |
| 828 | } |
| 829 | const detail = yield* addons |
| 830 | .addOn({ |
| 831 | id: listed.id, |
| 832 | name: listed.name ?? input.name, |
| 833 | provider: TIGRIS, |
| 834 | }) |
| 835 | .pipe(Effect.catchTag("FlyIoParseError", () => Effect.undefined)); |
| 836 | return detail ?? listed; |
| 837 | }); |
| 838 | |
| 839 | const secretsFromBoundEnv = (env: Record<string, string>): Record<string, string> => { |
| 840 | const values: Record<string, string> = {}; |
| 841 | for (const key of BUCKET_SECRET_NAMES) { |
| 842 | const value = env[key]; |
| 843 | if (value !== undefined && value.length > 0) values[key] = value; |
| 844 | } |
| 845 | if (values.AWS_ENDPOINT_URL === undefined && values.AWS_ENDPOINT_URL_S3 !== undefined) { |
| 846 | values.AWS_ENDPOINT_URL = values.AWS_ENDPOINT_URL_S3; |
| 847 | } |
| 848 | return values; |
| 849 | }; |
| 850 | |
| 851 | /** |
| 852 | * Write Tigris `AWS_*` / `BUCKET_NAME` secrets onto an App. Called from |
| 853 | * {@link Service} reconcile so the secrets exist before Machines boot. |
| 854 | * |
| 855 | * Looks up the add-on by id/name (retrying while Tigris is still |
| 856 | * provisioning) and also copies any already-evaluated `AWS_*` / |
| 857 | * `BUCKET_NAME` values from binding env — GraphQL `environment` is often |
| 858 | * empty after create. A missing add-on may use bound values only when the |
| 859 | * bucket name, credential pair, and S3 endpoint are complete. Partial values |
| 860 | * are never written. Region is optional, as in the Tigris bindings. |
| 861 | * Without an attachment, only an explicit `BUCKET_NAME` requests this mode; |
| 862 | * ordinary AWS region or credential settings do not imply a Bucket. |
| 863 | * Returns the highest accepted secret-version floor, or `undefined` when |
| 864 | * no Bucket was requested; this is not a vault snapshot. |
| 865 | */ |
| 866 | export const attachBucketSecrets = Effect.fn(function* ( |
| 867 | appName: string, |
| 868 | attached: readonly { |
| 869 | name: string; |
| 870 | id?: string; |
| 871 | /** Credentials from the bound Bucket's attributes. */ |
| 872 | values?: Record<string, string>; |
| 873 | }[], |
| 874 | boundEnv: Record<string, string> = {}, |
| 875 | ) { |
| 876 | if (appName.length === 0) return undefined; |
| 877 | if (attached.length === 0 && boundEnv.BUCKET_NAME === undefined) { |
| 878 | return undefined; |
| 879 | } |
| 880 | const fromEnv = secretsFromBoundEnv(boundEnv); |
| 881 | if (attached.length === 0) { |
| 882 | return yield* putSecretValues(appName, fromEnv); |
| 883 | } |
| 884 | const versions: number[] = []; |
| 885 | for (const item of attached) { |
| 886 | const name = item.name; |
| 887 | const id = item.id; |
| 888 | const fallbackValues: Record<string, string> = { |
| 889 | ...fromEnv, |
| 890 | ...item.values, |
| 891 | }; |
| 892 | if ( |
| 893 | fallbackValues.AWS_ENDPOINT_URL === undefined && |
| 894 | fallbackValues.AWS_ENDPOINT_URL_S3 !== undefined |
| 895 | ) { |
| 896 | fallbackValues.AWS_ENDPOINT_URL = fallbackValues.AWS_ENDPOINT_URL_S3; |
| 897 | } |
| 898 | if (fallbackValues.BUCKET_NAME === undefined && name.length > 0) { |
| 899 | fallbackValues.BUCKET_NAME = name; |
| 900 | } |
| 901 | const row = yield* findAttachedBucket({ id, name }).pipe( |
| 902 | Effect.retry({ |
| 903 | while: (error) => error._tag === "Fly.BucketPending", |
| 904 | times: 8, |
| 905 | schedule: backoff, |
| 906 | }), |
| 907 | Effect.catchTag("Fly.BucketPending", () => |
| 908 | requireBucketSecrets(appName, fallbackValues).pipe(Effect.as(undefined)), |
| 909 | ), |
| 910 | ); |
| 911 | const values = { |
| 912 | ...fallbackValues, |
| 913 | ...(row === undefined ? {} : envValuesOf(row)), |
| 914 | }; |
| 915 | versions.push(yield* putSecretValues(appName, values)); |
| 916 | } |
| 917 | return versions.length > 0 ? Math.max(...versions) : undefined; |
| 918 | }); |