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, 06:00 UTC
| 1 | import * as Effect from "effect/Effect" |
| 2 | import * as HttpClient from "effect/http/HttpClient" |
| 3 | import * as HttpClientResponse from "effect/http/HttpClientResponse" |
| 4 | |
| 5 | export interface Published { |
| 6 | readonly latest: string |
| 7 | readonly versions: ReadonlyArray<string> |
| 8 | /** The `latest` a stale CDN copy of the abbreviated metadata still shows. */ |
| 9 | readonly cachedLatest?: string |
| 10 | /** A status the live dist-tags endpoint fails with instead of answering. */ |
| 11 | readonly liveTagsStatus?: number |
| 12 | /** A status the abbreviated metadata endpoint fails with instead of answering. */ |
| 13 | readonly metadataStatus?: number |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Serves abbreviated metadata documents and the live dist-tags endpoint for |
| 18 | * the given packages. Unknown packages return 404. |
| 19 | */ |
| 20 | export const registryClient = (registry: Record<string, Published>): HttpClient.HttpClient => |
| 21 | HttpClient.make((request) => |
| 22 | Effect.sync(() => { |
| 23 | const url = new URL(request.url) |
| 24 | const distTags = /^\/-\/package\/(.+)\/dist-tags$/.exec(url.pathname) |
| 25 | const name = decodeURIComponent(distTags === null ? url.pathname.slice(1) : distTags[1]) |
| 26 | const published = registry[name] |
| 27 | if (distTags === null && request.headers.accept !== "application/vnd.npm.install-v1+json") { |
| 28 | return HttpClientResponse.fromWeb(request, new Response(null, { status: 406 })) |
| 29 | } |
| 30 | if (published === undefined) { |
| 31 | return HttpClientResponse.fromWeb(request, new Response(null, { status: 404 })) |
| 32 | } |
| 33 | if (distTags !== null) { |
| 34 | if (published.liveTagsStatus !== undefined) { |
| 35 | return HttpClientResponse.fromWeb(request, new Response(null, { status: published.liveTagsStatus })) |
| 36 | } |
| 37 | return HttpClientResponse.fromWeb( |
| 38 | request, |
| 39 | Response.json({ latest: published.latest, staging: published.versions.at(-1) }) |
| 40 | ) |
| 41 | } |
| 42 | if (published.metadataStatus !== undefined) { |
| 43 | return HttpClientResponse.fromWeb(request, new Response(null, { status: published.metadataStatus })) |
| 44 | } |
| 45 | return HttpClientResponse.fromWeb( |
| 46 | request, |
| 47 | Response.json({ |
| 48 | name, |
| 49 | "dist-tags": { latest: published.cachedLatest ?? published.latest, staging: published.versions.at(-1) }, |
| 50 | versions: Object.fromEntries(published.versions.map((version) => [version, { name, version }])) |
| 51 | }) |
| 52 | ) |
| 53 | }) |
| 54 | ) |