The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createHash } from "node:crypto"; |
| 2 | import { Credentials } from "@distilled.cloud/gcp/Credentials"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as HttpClient from "effect/http/HttpClient"; |
| 5 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 6 | import type * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | /** Image name {@link pushDockerVersion} pushes into a repository. */ |
| 11 | export const IMAGE = "hello"; |
| 12 | |
| 13 | const sha256 = (bytes: Uint8Array) => |
| 14 | Effect.sync(() => `sha256:${createHash("sha256").update(bytes).digest("hex")}`); |
| 15 | |
| 16 | const header = (response: HttpClientResponse.HttpClientResponse, name: string) => { |
| 17 | const headers = response.headers as { |
| 18 | get?: (key: string) => string | undefined; |
| 19 | } & Record<string, string | undefined>; |
| 20 | return headers.get?.(name) ?? headers[name] ?? headers[name.toLowerCase()] ?? ""; |
| 21 | }; |
| 22 | |
| 23 | const failHttp = (label: string, response: HttpClientResponse.HttpClientResponse) => |
| 24 | Effect.gen(function* () { |
| 25 | const body = yield* response.text.pipe(Effect.catch(() => Effect.succeed(""))); |
| 26 | return yield* Effect.fail(new Error(`${label} failed: ${response.status} ${body}`)); |
| 27 | }); |
| 28 | |
| 29 | /** |
| 30 | * Push a minimal (layerless) Docker image tagged `marker` into `repository` |
| 31 | * over the registry v2 API, as the deploying principal. Returns the |
| 32 | * version's full resource name. |
| 33 | */ |
| 34 | export const pushDockerVersion = ( |
| 35 | repository: { |
| 36 | name: string; |
| 37 | repositoryId: string; |
| 38 | project: string; |
| 39 | location: string; |
| 40 | }, |
| 41 | marker: string, |
| 42 | ) => |
| 43 | Effect.gen(function* () { |
| 44 | const creds = yield* yield* Credentials; |
| 45 | const client = yield* HttpClient.HttpClient; |
| 46 | const token = Redacted.value(creds.accessToken); |
| 47 | const host = `https://${repository.location}-docker.pkg.dev`; |
| 48 | const imagePath = `${repository.project}/${repository.repositoryId}/${IMAGE}`; |
| 49 | const auth = (request: HttpClientRequest.HttpClientRequest) => |
| 50 | request.pipe(HttpClientRequest.setHeader("Authorization", `Bearer ${token}`)); |
| 51 | |
| 52 | const config = yield* Effect.sync(() => |
| 53 | new TextEncoder().encode( |
| 54 | JSON.stringify({ |
| 55 | architecture: "amd64", |
| 56 | os: "linux", |
| 57 | rootfs: { type: "layers", diff_ids: [] }, |
| 58 | config: { Env: [`MARKER=${marker}`] }, |
| 59 | }), |
| 60 | ), |
| 61 | ); |
| 62 | const configDigest = yield* sha256(config); |
| 63 | |
| 64 | const start = yield* client.execute( |
| 65 | auth(HttpClientRequest.post(`${host}/v2/${imagePath}/blobs/uploads/`)), |
| 66 | ); |
| 67 | if (start.status !== 202) { |
| 68 | return yield* failHttp("blob upload start", start); |
| 69 | } |
| 70 | let location = header(start, "location"); |
| 71 | if (location.startsWith("/")) location = `${host}${location}`; |
| 72 | const sep = location.includes("?") ? "&" : "?"; |
| 73 | const uploaded = yield* client.execute( |
| 74 | auth( |
| 75 | HttpClientRequest.put(`${location}${sep}digest=${configDigest}`).pipe( |
| 76 | HttpClientRequest.bodyUint8Array(config, "application/octet-stream"), |
| 77 | ), |
| 78 | ), |
| 79 | ); |
| 80 | if (uploaded.status < 200 || uploaded.status >= 300) { |
| 81 | return yield* failHttp("blob upload", uploaded); |
| 82 | } |
| 83 | |
| 84 | const manifest = yield* Effect.sync(() => |
| 85 | new TextEncoder().encode( |
| 86 | JSON.stringify({ |
| 87 | schemaVersion: 2, |
| 88 | mediaType: "application/vnd.docker.distribution.manifest.v2+json", |
| 89 | config: { |
| 90 | mediaType: "application/vnd.docker.container.image.v1+json", |
| 91 | size: config.byteLength, |
| 92 | digest: configDigest, |
| 93 | }, |
| 94 | layers: [], |
| 95 | }), |
| 96 | ), |
| 97 | ); |
| 98 | const manifestDigest = yield* sha256(manifest); |
| 99 | const published = yield* client.execute( |
| 100 | auth( |
| 101 | HttpClientRequest.put(`${host}/v2/${imagePath}/manifests/${marker}`).pipe( |
| 102 | HttpClientRequest.bodyUint8Array( |
| 103 | manifest, |
| 104 | "application/vnd.docker.distribution.manifest.v2+json", |
| 105 | ), |
| 106 | ), |
| 107 | ), |
| 108 | ); |
| 109 | if (published.status < 200 || published.status >= 300) { |
| 110 | return yield* failHttp("manifest put", published); |
| 111 | } |
| 112 | |
| 113 | return `${repository.name}/packages/${IMAGE}/versions/${manifestDigest}`; |
| 114 | }).pipe( |
| 115 | Effect.retry({ |
| 116 | times: 5, |
| 117 | schedule: Schedule.spaced("2 seconds"), |
| 118 | }), |
| 119 | ); |