The best way to control Claude Code, Codex and more from any computer or phone

main
ts 187 lines 5.94 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Namespace from "alchemy/Namespace";
3 import type * as ByteSize from "effect/ByteSize";
4 import * as Config from "effect/Config";
5 import type * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import * as Schema from "effect/Schema";
9 import {
10 APP_ID_ENV,
11 DEFAULT_API_URL,
12 DEFAULT_CRON,
13 DEFAULT_TTL,
14 PRIVATE_KEY_ENV,
15 RegistryConfig,
16 SETTINGS_ENV,
17 Settings,
18 type GitHubCredentials,
19 type WorkerOverrides,
20 } from "./Registry/Bindings.ts";
21 import { handler } from "./Registry/Handler.ts";
22
23 export { Bucket, Cache, Database, Policy, RegistryConfig, Settings } from "./Registry/Bindings.ts";
24
25 export interface RegistryOptions {
26 /**
27 * Worker props forwarded verbatim: `domain`, `name`, `compatibility`,
28 * `observability`, `access`, and so on. `main`, `env`, and `crons` are
29 * owned by the registry.
30 */
31 readonly worker?: WorkerOverrides;
32 /** GitHub App credentials, read at deploy time and bound to the Worker. */
33 readonly github: GitHubCredentials & {
34 /** @default "https://api.github.com" */
35 readonly apiUrl?: string;
36 };
37 readonly policy: {
38 /** Repositories allowed to publish, as `owner/name`. */
39 readonly repos: ReadonlyArray<string>;
40 /** @default Duration.weeks(1) */
41 readonly ttl?: Duration.Duration;
42 /** Upper bound on a single tarball. Absent means unlimited. */
43 readonly maxPackageSize?: ByteSize.ByteSize;
44 };
45 /**
46 * Hostname to package scope. Requests on an aliased host resolve an
47 * unscoped name under that scope, so `pkg.distilled.cloud/core/<sha>`
48 * serves `@distilled.cloud/core`.
49 */
50 readonly aliases?: Readonly<Record<string, string>>;
51 /**
52 * Cron expression for the expiry sweep.
53 * @default "0 * * * *"
54 */
55 readonly cron?: string;
56 }
57
58 /**
59 * The registry Worker. Its props are an Effect so the same declaration
60 * serves both sides of the bundle boundary: at plan time
61 * {@link RegistryConfig} is the caller's options, inside the isolate it is
62 * rebuilt from the Worker's bindings.
63 */
64 export class Registry extends Cloudflare.Worker<Registry, {}>()("Worker") {}
65
66 const RegistryLive = Registry.make(
67 Effect.gen(function* () {
68 const config = yield* RegistryConfig;
69 return {
70 ...config.worker,
71 main: import.meta.url,
72 env: {
73 [SETTINGS_ENV]: yield* Schema.encodeEffect(Settings)({
74 policy: config.policy,
75 aliases: config.aliases,
76 cron: config.cron,
77 github: { apiUrl: config.github.apiUrl },
78 }).pipe(Effect.orDie),
79 [APP_ID_ENV]: config.github.appId,
80 [PRIVATE_KEY_ENV]: config.github.privateKey,
81 },
82 };
83 }),
84 handler,
85 );
86
87 /**
88 * Runtime side of {@link RegistryConfig}: decode the settings binding and
89 * point the credentials at the bindings `RegistryLive` declared.
90 */
91 const RegistryConfigFromEnv = Layer.effect(
92 RegistryConfig,
93 Effect.gen(function* () {
94 const env = yield* Cloudflare.Workers.WorkerEnvironment;
95 const settings = yield* Schema.decodeUnknownEffect(Settings)(env[SETTINGS_ENV]).pipe(
96 Effect.orDie,
97 );
98 return {
99 ...settings,
100 worker: {},
101 github: {
102 ...settings.github,
103 appId: Config.String(APP_ID_ENV),
104 privateKey: Config.Redacted(PRIVATE_KEY_ENV),
105 },
106 };
107 }),
108 );
109
110 /**
111 * The bundle entry. `RegistryLive` names this file as `main`, so the
112 * generated Worker entry imports this default export and builds it once per
113 * isolate. User code never imports it; use {@link PkgRegistry}.
114 */
115 export default RegistryLive.pipe(Layer.provide(RegistryConfigFromEnv));
116
117 /**
118 * A preview package registry: a Worker serving install URLs and the publish
119 * API, an R2 bucket of content-addressed tarballs, a D1 index of tags, and a
120 * KV namespace caching what the Worker learns from GitHub. They are declared
121 * under `id` as `<id>/Worker`, `<id>/Bucket`, `<id>/Database`, and
122 * `<id>/Cache`.
123 *
124 * ### Deploying a registry
125 * **Example:** Stack
126 * ```typescript
127 * // stacks/pkg.ts
128 * import { PkgRegistry } from "@alchemy.run/pkg/Registry";
129 * import * as Alchemy from "alchemy";
130 * import * as Cloudflare from "@alchemy.run/cloudflare";
131 * import * as ByteSize from "effect/ByteSize";
132 * import * as Config from "effect/Config";
133 * import * as Duration from "effect/Duration";
134 * import * as Effect from "effect/Effect";
135 *
136 * export default Alchemy.Stack(
137 * "Pkg",
138 * { providers: Cloudflare.providers(), state: Cloudflare.state() },
139 * Effect.gen(function* () {
140 * const registry = yield* PkgRegistry("Pkg", {
141 * worker: { domain: "pkg.alchemy.run" },
142 * github: {
143 * appId: Config.String("GH_APP_ID"),
144 * privateKey: Config.Redacted("GH_APP_PRIVATE_KEY"),
145 * },
146 * policy: {
147 * repos: ["alchemy-run/alchemy", "alchemy-run/distilled"],
148 * ttl: Duration.weeks(1),
149 * maxPackageSize: ByteSize.megabytes(100),
150 * },
151 * });
152 * return { url: registry.url.as<string>() };
153 * }),
154 * );
155 * ```
156 *
157 * @resource
158 * @product Workers
159 * @category Workers & Compute
160 */
161 export const PkgRegistry = <const Id extends string>(id: Id, options: RegistryOptions) =>
162 Registry.pipe(
163 Effect.provide(
164 RegistryLive.pipe(
165 Layer.provide(
166 Layer.succeed(RegistryConfig, {
167 worker: options.worker ?? {},
168 policy: {
169 repos: options.policy.repos,
170 ttl: options.policy.ttl ?? DEFAULT_TTL,
171 ...(options.policy.maxPackageSize !== undefined
172 ? { maxPackageSize: options.policy.maxPackageSize }
173 : {}),
174 },
175 aliases: options.aliases ?? {},
176 cron: options.cron ?? DEFAULT_CRON,
177 github: {
178 apiUrl: options.github.apiUrl ?? DEFAULT_API_URL,
179 appId: options.github.appId,
180 privateKey: options.github.privateKey,
181 },
182 }),
183 ),
184 ),
185 ),
186 Namespace.push(id),
187 );