The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import crypto from "node:crypto"; |
| 2 | import * as ECRPublic from "@alchemy.run/aws/ECRPublic"; |
| 3 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import path from "pathe"; |
| 11 | |
| 12 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 13 | |
| 14 | // A well-formed-but-nonexistent layer digest — batchCheckLayerAvailability |
| 15 | // reports it as a per-layer failure, proving the repository-scoped grant. |
| 16 | const NONEXISTENT_DIGEST = `sha256:${"0".repeat(64)}`; |
| 17 | |
| 18 | const sha256Hex = (bytes: Uint8Array) => |
| 19 | Effect.sync(() => crypto.createHash("sha256").update(bytes).digest("hex")); |
| 20 | |
| 21 | export class EcrPublicTestFunction extends Lambda.Function<Lambda.Function>()( |
| 22 | "EcrPublicTestFunction", |
| 23 | ) {} |
| 24 | |
| 25 | export default EcrPublicTestFunction.make( |
| 26 | { |
| 27 | main, |
| 28 | functionUrl: true, |
| 29 | // The push-flow route performs several sequential ECR Public calls; the |
| 30 | // AWS default 3s Lambda timeout is too tight. |
| 31 | timeout: Duration.seconds(60), |
| 32 | }, |
| 33 | Effect.gen(function* () { |
| 34 | const repository = yield* ECRPublic.PublicRepository("BindingsRepo", { |
| 35 | catalogData: { |
| 36 | description: "alchemy ECRPublic bindings fixture", |
| 37 | architectures: ["x86-64"], |
| 38 | operatingSystems: ["Linux"], |
| 39 | }, |
| 40 | }); |
| 41 | |
| 42 | // --- repository-scoped bindings --- |
| 43 | const describeImages = yield* ECRPublic.DescribeImages(repository); |
| 44 | const describeImageTags = yield* ECRPublic.DescribeImageTags(repository); |
| 45 | const getRepositoryCatalogData = yield* ECRPublic.GetRepositoryCatalogData(repository); |
| 46 | const batchCheckLayerAvailability = yield* ECRPublic.BatchCheckLayerAvailability(repository); |
| 47 | const batchDeleteImage = yield* ECRPublic.BatchDeleteImage(repository); |
| 48 | const initiateLayerUpload = yield* ECRPublic.InitiateLayerUpload(repository); |
| 49 | const uploadLayerPart = yield* ECRPublic.UploadLayerPart(repository); |
| 50 | const completeLayerUpload = yield* ECRPublic.CompleteLayerUpload(repository); |
| 51 | const putImage = yield* ECRPublic.PutImage(repository); |
| 52 | |
| 53 | // --- registry-level bindings --- |
| 54 | const getAuthorizationToken = yield* ECRPublic.GetAuthorizationToken(); |
| 55 | const describeRegistries = yield* ECRPublic.DescribeRegistries(); |
| 56 | const getRegistryCatalogData = yield* ECRPublic.GetRegistryCatalogData(); |
| 57 | |
| 58 | const bound = { |
| 59 | describeImages, |
| 60 | describeImageTags, |
| 61 | getRepositoryCatalogData, |
| 62 | batchCheckLayerAvailability, |
| 63 | batchDeleteImage, |
| 64 | initiateLayerUpload, |
| 65 | uploadLayerPart, |
| 66 | completeLayerUpload, |
| 67 | putImage, |
| 68 | getAuthorizationToken, |
| 69 | describeRegistries, |
| 70 | getRegistryCatalogData, |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * Upload one blob as a layer: initiate → upload the single (last) part → |
| 75 | * complete with the blob's sha256. A re-run finds the layer already |
| 76 | * sealed — `LayerAlreadyExistsException` converges to the same digest. |
| 77 | */ |
| 78 | const uploadBlob = Effect.fn(function* (blob: Uint8Array) { |
| 79 | const digest = `sha256:${yield* sha256Hex(blob)}`; |
| 80 | const { uploadId } = yield* initiateLayerUpload(); |
| 81 | yield* uploadLayerPart({ |
| 82 | uploadId: uploadId!, |
| 83 | partFirstByte: 0, |
| 84 | partLastByte: blob.byteLength - 1, |
| 85 | layerPartBlob: blob, |
| 86 | }); |
| 87 | yield* completeLayerUpload({ |
| 88 | uploadId: uploadId!, |
| 89 | layerDigests: [digest], |
| 90 | }).pipe(Effect.catchTag("LayerAlreadyExistsException", () => Effect.void)); |
| 91 | return digest; |
| 92 | }); |
| 93 | |
| 94 | return { |
| 95 | fetch: Effect.gen(function* () { |
| 96 | const request = yield* HttpServerRequest; |
| 97 | const url = new URL(request.originalUrl); |
| 98 | const pathname = url.pathname; |
| 99 | |
| 100 | if (request.method === "GET" && pathname === "/bindings") { |
| 101 | return yield* HttpServerResponse.json({ |
| 102 | bound: Object.keys(bound), |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | if (request.method === "GET" && pathname === "/registries") { |
| 107 | const result = yield* describeRegistries(); |
| 108 | const registry = result.registries?.[0]; |
| 109 | return yield* HttpServerResponse.json({ |
| 110 | count: (result.registries ?? []).length, |
| 111 | alias: registry?.aliases?.[0]?.name ?? null, |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | if (request.method === "GET" && pathname === "/registry-catalog") { |
| 116 | const result = yield* getRegistryCatalogData(); |
| 117 | return yield* HttpServerResponse.json({ |
| 118 | tag: "Ok", |
| 119 | displayName: result.registryCatalogData.displayName ?? null, |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | if (request.method === "GET" && pathname === "/auth-token") { |
| 124 | const result = yield* getAuthorizationToken(); |
| 125 | const token = result.authorizationData?.authorizationToken; |
| 126 | // The token is Redacted in the distilled response — never echo it. |
| 127 | const hasToken = |
| 128 | token !== undefined && |
| 129 | (typeof token === "string" ? token.length > 0 : Redacted.value(token).length > 0); |
| 130 | return yield* HttpServerResponse.json({ hasToken }); |
| 131 | } |
| 132 | |
| 133 | if (request.method === "GET" && pathname === "/repo-catalog") { |
| 134 | const result = yield* getRepositoryCatalogData(); |
| 135 | return yield* HttpServerResponse.json({ |
| 136 | description: result.catalogData?.description ?? null, |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | if (request.method === "GET" && pathname === "/images") { |
| 141 | const result = yield* describeImages(); |
| 142 | return yield* HttpServerResponse.json({ |
| 143 | count: (result.imageDetails ?? []).length, |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | if (request.method === "GET" && pathname === "/image-tags") { |
| 148 | const result = yield* describeImageTags(); |
| 149 | return yield* HttpServerResponse.json({ |
| 150 | count: (result.imageTagDetails ?? []).length, |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | if (request.method === "GET" && pathname === "/check-layers") { |
| 155 | const result = yield* batchCheckLayerAvailability({ |
| 156 | layerDigests: [NONEXISTENT_DIGEST], |
| 157 | }); |
| 158 | // ECR Public reports a nonexistent digest either as a per-layer |
| 159 | // failure or as a layer with `layerAvailability: "UNAVAILABLE"`. |
| 160 | return yield* HttpServerResponse.json({ |
| 161 | failures: (result.failures ?? []).length, |
| 162 | unavailable: (result.layers ?? []).filter((l) => l.layerAvailability !== "AVAILABLE") |
| 163 | .length, |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | if (request.method === "GET" && pathname === "/delete-missing") { |
| 168 | const result = yield* batchDeleteImage({ |
| 169 | imageIds: [{ imageTag: "does-not-exist" }], |
| 170 | }); |
| 171 | return yield* HttpServerResponse.json({ |
| 172 | failureCode: result.failures?.[0]?.failureCode ?? null, |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | if (request.method === "GET" && pathname === "/push-flow") { |
| 177 | // A genuine (minimal) image push: upload a config blob and a layer |
| 178 | // blob, then put a docker v2 manifest referencing both. Re-runs |
| 179 | // converge — existing layers and an identical manifest are fine. |
| 180 | const layerBlob = new TextEncoder().encode("alchemy-ecr-public-bindings-fixture-layer"); |
| 181 | const configBlob = new TextEncoder().encode( |
| 182 | JSON.stringify({ |
| 183 | architecture: "amd64", |
| 184 | os: "linux", |
| 185 | config: {}, |
| 186 | rootfs: { type: "layers", diff_ids: [NONEXISTENT_DIGEST] }, |
| 187 | }), |
| 188 | ); |
| 189 | const layerDigest = yield* uploadBlob(layerBlob); |
| 190 | const configDigest = yield* uploadBlob(configBlob); |
| 191 | const imageManifest = JSON.stringify({ |
| 192 | schemaVersion: 2, |
| 193 | mediaType: "application/vnd.docker.distribution.manifest.v2+json", |
| 194 | config: { |
| 195 | mediaType: "application/vnd.docker.container.image.v1+json", |
| 196 | size: configBlob.byteLength, |
| 197 | digest: configDigest, |
| 198 | }, |
| 199 | layers: [ |
| 200 | { |
| 201 | mediaType: "application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 202 | size: layerBlob.byteLength, |
| 203 | digest: layerDigest, |
| 204 | }, |
| 205 | ], |
| 206 | }); |
| 207 | const result = yield* putImage({ |
| 208 | imageManifest, |
| 209 | imageManifestMediaType: "application/vnd.docker.distribution.manifest.v2+json", |
| 210 | imageTag: "bindings-test", |
| 211 | }).pipe( |
| 212 | Effect.map((r) => ({ |
| 213 | tag: "Ok", |
| 214 | imageDigest: r.image?.imageId?.imageDigest ?? null, |
| 215 | })), |
| 216 | // Re-running the route pushes the identical manifest+tag again. |
| 217 | Effect.catchTag( |
| 218 | ["ImageAlreadyExistsException", "ImageTagAlreadyExistsException"], |
| 219 | (e) => Effect.succeed({ tag: e._tag, imageDigest: null }), |
| 220 | ), |
| 221 | ); |
| 222 | return yield* HttpServerResponse.json(result); |
| 223 | } |
| 224 | |
| 225 | return yield* HttpServerResponse.json( |
| 226 | { error: "Not found", method: request.method, pathname }, |
| 227 | { status: 404 }, |
| 228 | ); |
| 229 | }).pipe(Effect.orDie), |
| 230 | }; |
| 231 | }).pipe( |
| 232 | Effect.provide( |
| 233 | Layer.mergeAll( |
| 234 | ECRPublic.DescribeImagesHttp, |
| 235 | ECRPublic.DescribeImageTagsHttp, |
| 236 | ECRPublic.GetRepositoryCatalogDataHttp, |
| 237 | ECRPublic.BatchCheckLayerAvailabilityHttp, |
| 238 | ECRPublic.BatchDeleteImageHttp, |
| 239 | ECRPublic.InitiateLayerUploadHttp, |
| 240 | ECRPublic.UploadLayerPartHttp, |
| 241 | ECRPublic.CompleteLayerUploadHttp, |
| 242 | ECRPublic.PutImageHttp, |
| 243 | ECRPublic.GetAuthorizationTokenHttp, |
| 244 | ECRPublic.DescribeRegistriesHttp, |
| 245 | ECRPublic.GetRegistryCatalogDataHttp, |
| 246 | ), |
| 247 | ), |
| 248 | ), |
| 249 | ); |