The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as Redacted from "effect/Redacted"; |
| 2 | |
| 3 | export interface ImageRegistry { |
| 4 | /** Registry host, e.g. `ghcr.io`. */ |
| 5 | server: string; |
| 6 | /** Registry username. */ |
| 7 | username: string; |
| 8 | /** Registry password. Use `Redacted.make(...)` or `Config.Redacted(...)`. */ |
| 9 | password: Redacted.Redacted<string>; |
| 10 | } |
| 11 | |
| 12 | /** Strips the tag and digest from an image reference, leaving the repository. */ |
| 13 | export const repositoryFromImageRef = (imageRef: string): string => { |
| 14 | const withoutDigest = imageRef.includes("@") |
| 15 | ? imageRef.slice(0, imageRef.indexOf("@")) |
| 16 | : imageRef; |
| 17 | const tagSeparator = withoutDigest.lastIndexOf(":"); |
| 18 | const pathSeparator = withoutDigest.lastIndexOf("/"); |
| 19 | return tagSeparator > pathSeparator ? withoutDigest.slice(0, tagSeparator) : withoutDigest; |
| 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * Prefixes an image reference with the registry host unless the reference |
| 24 | * already carries a registry prefix (a dotted host, a host:port, or `localhost`). |
| 25 | */ |
| 26 | export const withRegistryHost = (imageRef: string, registry: { server: string }): string => { |
| 27 | const registryHost = registry.server.replace(/\/$/, ""); |
| 28 | const firstSegment = imageRef.split("/")[0]; |
| 29 | const hasRegistryPrefix = |
| 30 | imageRef.includes("/") && |
| 31 | (firstSegment.includes(".") || firstSegment.includes(":") || firstSegment === "localhost"); |
| 32 | return hasRegistryPrefix ? imageRef : `${registryHost}/${imageRef}`; |
| 33 | }; |
| 34 | |
| 35 | /** Extracts the `repository@sha256:...` digest from `docker push` output. */ |
| 36 | export const parseRepoDigest = (imageRef: string, output: string): string | undefined => { |
| 37 | const match = /digest:\s+([a-z0-9]+:[a-f0-9]{64})/i.exec(output); |
| 38 | if (!match) return undefined; |
| 39 | return `${repositoryFromImageRef(imageRef)}@${match[1]}`; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Picks the `repository@digest` that inspect reports for this image. |
| 44 | * |
| 45 | * A fallback when push output has no `digest:` line. Podman pushes report the |
| 46 | * published digest through `--digestfile` instead, because Podman can list a |
| 47 | * pulled image's source digest under the pushed name here. |
| 48 | */ |
| 49 | export const repoDigestFromInspect = ( |
| 50 | imageRef: string, |
| 51 | repoDigests: ReadonlyArray<string> | null | undefined, |
| 52 | ): string | undefined => { |
| 53 | const repository = repositoryFromImageRef(imageRef); |
| 54 | for (const digest of repoDigests ?? []) { |
| 55 | const at = digest.lastIndexOf("@"); |
| 56 | if (at > 0 && digest.slice(0, at) === repository) return digest; |
| 57 | } |
| 58 | return undefined; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * Digest from push output when the engine prints one, otherwise the matching |
| 63 | * `RepoDigests` entry. `registryRef` is the host-qualified name when it |
| 64 | * differs from the local reference that was pushed. |
| 65 | */ |
| 66 | export const publishedRepoDigest = ( |
| 67 | imageRef: string, |
| 68 | output: { stdout: string; stderr: string }, |
| 69 | repoDigests: ReadonlyArray<string> | null | undefined, |
| 70 | registryRef?: string, |
| 71 | ): string | undefined => { |
| 72 | const text = `${output.stdout}\n${output.stderr}`; |
| 73 | return ( |
| 74 | parseRepoDigest(imageRef, text) ?? |
| 75 | (registryRef === undefined ? undefined : repoDigestFromInspect(registryRef, repoDigests)) ?? |
| 76 | repoDigestFromInspect(imageRef, repoDigests) |
| 77 | ); |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * Parses an image's RFC 3339 `Created` timestamp into epoch milliseconds. |
| 82 | * |
| 83 | * Docker only reports `Created` when the image config carries a creation time: |
| 84 | * API >= 1.44 omits it, older APIs backfill the year-1 zero value |
| 85 | * (`0001-01-01T00:00:00Z`), and 25.0.0–25.0.3 returned an empty string. In any |
| 86 | * of those cases there is no real build time, so we fall back to the wall clock. |
| 87 | */ |
| 88 | export const parseCreatedAt = (created: string | undefined): number => { |
| 89 | const parsed = created ? Date.parse(created) : Number.NaN; |
| 90 | return Number.isNaN(parsed) || parsed <= 0 ? Date.now() : parsed; |
| 91 | }; |