The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Railway CLI `up` equivalent: POST a gzipped Docker context to |
| 3 | * backboard `/project/:id/environment/:id/up`. Railway builds the |
| 4 | * generated Dockerfile; Alchemy never pushes an image. |
| 5 | */ |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import { RailwayEnvironment } from "./Environment.ts"; |
| 12 | |
| 13 | export class DeployUploadFailed extends Data.TaggedError("Railway.DeployUploadFailed")<{ |
| 14 | status: number; |
| 15 | message: string; |
| 16 | }> {} |
| 17 | |
| 18 | export type UpResponse = { |
| 19 | readonly deploymentId: string; |
| 20 | readonly url: string; |
| 21 | readonly logsUrl: string; |
| 22 | readonly deploymentDomain: string; |
| 23 | }; |
| 24 | |
| 25 | const parseUpResponse = (value: unknown): UpResponse | undefined => { |
| 26 | if (value === null || typeof value !== "object") return undefined; |
| 27 | const rec = value as Record<string, unknown>; |
| 28 | if (typeof rec.deploymentId !== "string" || rec.deploymentId.length === 0) { |
| 29 | return undefined; |
| 30 | } |
| 31 | return { |
| 32 | deploymentId: rec.deploymentId, |
| 33 | url: typeof rec.url === "string" ? rec.url : "", |
| 34 | logsUrl: typeof rec.logsUrl === "string" ? rec.logsUrl : "", |
| 35 | deploymentDomain: typeof rec.deploymentDomain === "string" ? rec.deploymentDomain : "", |
| 36 | }; |
| 37 | }; |
| 38 | |
| 39 | const uploadUrl = (input: { |
| 40 | apiBaseUrl: string; |
| 41 | projectId: string; |
| 42 | environmentId: string; |
| 43 | serviceId: string; |
| 44 | message?: string; |
| 45 | }): string => { |
| 46 | const base = input.apiBaseUrl.replace(/\/+$/, ""); |
| 47 | const url = new URL(`${base}/project/${input.projectId}/environment/${input.environmentId}/up`); |
| 48 | url.searchParams.set("serviceId", input.serviceId); |
| 49 | if (input.message !== undefined && input.message.length > 0) { |
| 50 | url.searchParams.set("message", input.message); |
| 51 | } |
| 52 | return url.toString(); |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Upload a gzipped tar of a generated Docker context. Railway builds |
| 57 | * and deploys it. Same contract as `railway up`. |
| 58 | */ |
| 59 | export const uploadDeployTarball = Effect.fn(function* (input: { |
| 60 | projectId: string; |
| 61 | environmentId: string; |
| 62 | serviceId: string; |
| 63 | tarball: Uint8Array; |
| 64 | message?: string; |
| 65 | }) { |
| 66 | const env = yield* yield* RailwayEnvironment; |
| 67 | const http = yield* HttpClient.HttpClient; |
| 68 | const token = Redacted.value(env.token); |
| 69 | const url = uploadUrl({ |
| 70 | apiBaseUrl: env.apiBaseUrl, |
| 71 | projectId: input.projectId, |
| 72 | environmentId: input.environmentId, |
| 73 | serviceId: input.serviceId, |
| 74 | message: input.message, |
| 75 | }); |
| 76 | let request = HttpClientRequest.post(url).pipe( |
| 77 | HttpClientRequest.bodyUint8Array(input.tarball, "application/gzip"), |
| 78 | ); |
| 79 | request = |
| 80 | env.tokenKind === "project" |
| 81 | ? request.pipe(HttpClientRequest.setHeader("Project-Access-Token", token)) |
| 82 | : request.pipe(HttpClientRequest.bearerToken(token)); |
| 83 | const response = yield* http.execute(request); |
| 84 | if (response.status !== 200) { |
| 85 | const body = yield* response.text.pipe(Effect.orElseSucceed(() => "")); |
| 86 | return yield* new DeployUploadFailed({ |
| 87 | status: response.status, |
| 88 | message: |
| 89 | body.length > 0 ? body : `Failed to upload code with status ${String(response.status)}`, |
| 90 | }); |
| 91 | } |
| 92 | const json = yield* response.json; |
| 93 | const parsed = parseUpResponse(json); |
| 94 | if (parsed === undefined) { |
| 95 | return yield* new DeployUploadFailed({ |
| 96 | status: response.status, |
| 97 | message: "Railway /up did not return a deploymentId", |
| 98 | }); |
| 99 | } |
| 100 | return parsed; |
| 101 | }); |