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

main
ts 304 lines 11.9 KB
Raw
1 import { makeFunctionBundler } from "@alchemy.run/aws/Lambda/FunctionBundle";
2 import {
3 buildMicrovmDockerfile,
4 bundleMicrovmProgram,
5 } from "@alchemy.run/aws/Lambda/MicrovmBundle";
6 import { getCompatibility } from "@alchemy.run/cloudflare/Workers/Compatibility";
7 import { WorkerBundle } from "@alchemy.run/cloudflare/Workers/Sources/Rolldown";
8 import { expect, it, layer } from "@alchemy.run/test";
9 import * as NodeServices from "@effect/platform-node/NodeServices";
10 import * as Bundle from "alchemy/Bundle/Bundle";
11 import { Stack } from "alchemy/Stack";
12 import * as Effect from "effect/Effect";
13 import * as FileSystem from "effect/FileSystem";
14 import * as FetchHttpClient from "effect/http/FetchHttpClient";
15 import * as HttpClient from "effect/http/HttpClient";
16 import * as Layer from "effect/Layer";
17 import * as Path from "effect/Path";
18 import { ChildProcess } from "effect/process";
19 import * as Schedule from "effect/Schedule";
20 import type * as rolldown from "rolldown";
21
22 /**
23 * Canary over the bundles Alchemy ships: each fixture is bundled with the
24 * exact options its provider deploys with, and the test checks how much of
25 * the module graph Rolldown had to parse, how big the output is, and which
26 * provider services ended up in it.
27 *
28 * Every runtime fixture comes in two styles — `import * as AWS from
29 * "@alchemy.run/aws"` and per-service imports (`@alchemy.run/aws/Lambda`) —
30 * and both must cost the same: `lazyBarrel` plus the namespace-import rewrite
31 * keep the root barrel from pulling every service into the build.
32 */
33
34 const fixture = (name: string) => `${import.meta.dirname}/fixtures/canary/${name}`;
35
36 interface BundleStats {
37 /** Modules Rolldown parsed (the cost of a build: time and memory). */
38 readonly parsed: ReadonlyArray<string>;
39 /** Modules that ended up in the output chunks. */
40 readonly bundled: ReadonlyArray<string>;
41 /** Total bytes of JavaScript emitted (source maps excluded). */
42 readonly bytes: number;
43 /** Total bytes of source maps emitted. */
44 readonly mapBytes: number;
45 }
46
47 /** A plugin that records {@link BundleStats} for the build it is added to. */
48 const measure = () => {
49 const parsed: string[] = [];
50 const bundled: string[] = [];
51 let bytes = 0;
52 let mapBytes = 0;
53 const plugin: rolldown.Plugin = {
54 name: "canary:measure",
55 moduleParsed(info) {
56 parsed.push(info.id);
57 },
58 // After Alchemy's own `generateBundle` hooks, which drop unreachable
59 // chunks: measure what actually ships.
60 generateBundle: {
61 order: "post",
62 handler(_options, bundle) {
63 for (const output of Object.values(bundle)) {
64 if (output.type === "asset") {
65 if (output.fileName.endsWith(".map")) mapBytes += output.source.length;
66 continue;
67 }
68 bundled.push(...Object.keys(output.modules));
69 bytes += output.code.length;
70 }
71 },
72 },
73 };
74 return { plugin, stats: (): BundleStats => ({ parsed, bundled, bytes, mapBytes }) };
75 };
76
77 /** The provider service directories (`AWS/S3`, `Cloudflare/KV`, …) a set of modules comes from. */
78 const servicesOf = (modules: ReadonlyArray<string>, pkg: "aws" | "cloudflare") => {
79 const re = new RegExp(`[\\\\/]packages[\\\\/]${pkg}[\\\\/]src[\\\\/]([^\\\\/]+)[\\\\/]`);
80 return [...new Set(modules.flatMap((id) => re.exec(id)?.[1] ?? []))].sort();
81 };
82
83 const bundleLambda = (main: string) =>
84 Effect.gen(function* () {
85 const { resolveBundlePlan } = yield* makeFunctionBundler;
86 const { plugin, stats } = measure();
87 const plan = yield* resolveBundlePlan({ main, build: { plugins: [plugin] } });
88 yield* Bundle.build(plan.inputOptions, plan.outputOptions, plan.extra);
89 return stats();
90 });
91
92 const bundleWorker = (main: string) =>
93 Effect.gen(function* () {
94 const bundler = yield* WorkerBundle;
95 const { plugin, stats } = measure();
96 yield* bundler.build({
97 id: "CanaryWorker",
98 main,
99 compatibility: getCompatibility({ main }),
100 entry: { kind: "effect", exports: {} },
101 stack: { name: "BundleCanary", stage: "test" },
102 extraOptions: { input: { plugins: [plugin] } },
103 });
104 return stats();
105 });
106
107 const bundleMicrovm = (main: string) =>
108 Effect.gen(function* () {
109 const { plugin, stats } = measure();
110 const { files } = yield* bundleMicrovmProgram({
111 main,
112 runtime: "node",
113 port: 8080,
114 build: { input: { plugins: [plugin] } },
115 }).pipe(
116 Effect.provideService(Stack, {
117 name: "BundleCanary",
118 stage: "test",
119 resources: {},
120 bindings: {},
121 actions: {},
122 }),
123 );
124 return { stats: stats(), files };
125 });
126
127 /**
128 * Writes a Node MicroVM bundle to disk and runs it the way its image's
129 * entrypoint does (`node --enable-source-maps index.mjs`), returning a GET
130 * against the running server. The process is killed when the scope closes.
131 */
132 const bootMicrovm = (files: ReadonlyArray<{ path: string; content: Uint8Array }>, port: number) =>
133 Effect.gen(function* () {
134 const fs = yield* FileSystem.FileSystem;
135 const path = yield* Path.Path;
136 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "bundle-canary-microvm-" });
137 yield* Effect.forEach(files, (file) => fs.writeFile(path.join(dir, file.path), file.content));
138 yield* ChildProcess.make("node", ["--enable-source-maps", "index.mjs"], {
139 cwd: dir,
140 // Placeholder credentials: the bootstrap resolves them on start, and
141 // without any it would wait on the instance metadata endpoint.
142 env: {
143 PORT: String(port),
144 AWS_REGION: "us-east-1",
145 AWS_ACCESS_KEY_ID: "canary",
146 AWS_SECRET_ACCESS_KEY: "canary",
147 },
148 extendEnv: true,
149 });
150 const client = yield* HttpClient.HttpClient;
151 return (route: string) =>
152 client.get(`http://127.0.0.1:${port}${route}`).pipe(
153 Effect.flatMap((response) => response.json),
154 Effect.timeout("2 seconds"),
155 // The server takes a moment to start listening.
156 Effect.retry({ schedule: Schedule.spaced("250 millis"), times: 40 }),
157 );
158 });
159
160 interface Budget {
161 /** Most modules Rolldown may parse for this runtime. */
162 readonly parsed: number;
163 /** Most JavaScript bytes the runtime may ship. */
164 readonly bytes: number;
165 /** Most source-map bytes the runtime may ship. */
166 readonly mapBytes: number;
167 /** The provider services whose code ships in the bundle — no others. */
168 readonly services: ReadonlyArray<string>;
169 /** Modules this runtime must not ship, on top of {@link DEPLOY_ONLY}. */
170 readonly forbidden?: ReadonlyArray<RegExp>;
171 }
172
173 const report = (label: string, stats: BundleStats, pkg: "aws" | "cloudflare") =>
174 Effect.log(
175 `${label}: parsed=${stats.parsed.length} bundled=${stats.bundled.length} bytes=${stats.bytes} mapBytes=${stats.mapBytes} services=${servicesOf(stats.bundled, pkg).join(",")}`,
176 );
177
178 /**
179 * Deploy-time code that must never ship in a runtime: the bundler, the CLI's
180 * platform layer (Bun's platform came with it), and profile and credential
181 * stores. `Auth/Registry.ts` is the exception: `Stack.ts` provides it, and it
182 * imports nothing but Effect.
183 */
184 const DEPLOY_ONLY = [
185 /[\\/](rolldown|@rolldown)[\\/]/,
186 /[\\/]@effect[\\/]platform-bun[\\/]/,
187 /[\\/]alchemy[\\/]src[\\/](Auth[\\/](?!Registry\.ts$)|Cli[\\/]|Bundle[\\/])/,
188 /[\\/]alchemy[\\/]src[\\/](EvalStack|Deploy|Destroy|Plan|Apply)\.ts$/,
189 ];
190
191 const expectWithinBudget = (stats: BundleStats, pkg: "aws" | "cloudflare", budget: Budget) => {
192 expect(stats.parsed.length).toBeLessThanOrEqual(budget.parsed);
193 expect(stats.bytes).toBeLessThanOrEqual(budget.bytes);
194 expect(stats.mapBytes).toBeLessThanOrEqual(budget.mapBytes);
195 expect(servicesOf(stats.bundled, pkg)).toEqual(budget.services);
196 const forbidden = [...DEPLOY_ONLY, ...(budget.forbidden ?? [])];
197 expect(stats.bundled.filter((id) => forbidden.some((re) => re.test(id)))).toEqual([]);
198 };
199
200 /**
201 * `ns.Service` still loads that one service's barrel in full, so the
202 * namespace style may parse somewhat more than the per-service style —
203 * never another provider's worth (before `lazyBarrel` and the rewrite,
204 * `import * as AWS` parsed ~8,500 modules against ~800).
205 */
206 const expectSameCost = (namespace: BundleStats, subpath: BundleStats) => {
207 expect(namespace.parsed.length).toBeLessThanOrEqual(Math.ceil(subpath.parsed.length * 1.25));
208 expect(Math.abs(namespace.bytes - subpath.bytes)).toBeLessThan(1_000);
209 expect(namespace.bundled.length).toBe(subpath.bundled.length);
210 };
211
212 // Budgets are measured values plus ~25% headroom. Raise them only for a
213 // change that is meant to grow every runtime.
214 const AWS_LAMBDA: Budget = {
215 parsed: 1_000,
216 bytes: 640_000,
217 mapBytes: 6_600_000,
218 services: ["Bootstrap", "Lambda", "S3"],
219 // No HTTP server in an event Lambda, so nothing needs WebSocket upgrades.
220 // (`ws` shipped here through the CLI's platform layer.)
221 forbidden: [/[\\/]node_modules[\\/](\.pnpm[\\/][^\\/]+[\\/]node_modules[\\/])?ws[\\/]/],
222 };
223 const AWS_MICROVM: Budget = {
224 parsed: 1_000,
225 bytes: 640_000,
226 mapBytes: 6_000_000,
227 services: ["Bootstrap", "Lambda"],
228 };
229 const CLOUDFLARE_WORKER: Budget = {
230 parsed: 1_500,
231 bytes: 480_000,
232 mapBytes: 5_800_000,
233 services: ["KV", "Workers"],
234 };
235
236 layer(NodeServices.layer)("bundle canary", (it) => {
237 it.effect(
238 "AWS Lambda with an S3 binding",
239 () =>
240 Effect.gen(function* () {
241 const namespace = yield* bundleLambda(fixture("aws-namespace.ts"));
242 const subpath = yield* bundleLambda(fixture("aws-subpath.ts"));
243 yield* report("import * as AWS", namespace, "aws");
244 yield* report("@alchemy.run/aws/<Service>", subpath, "aws");
245 expectWithinBudget(namespace, "aws", AWS_LAMBDA);
246 expectWithinBudget(subpath, "aws", AWS_LAMBDA);
247 expectSameCost(namespace, subpath);
248 }),
249 { tags: ["unit", "local"], timeout: 120_000 },
250 );
251
252 it.effect(
253 "AWS MicroVM program on Node",
254 () =>
255 Effect.gen(function* () {
256 const { stats, files } = yield* bundleMicrovm(fixture("aws-microvm.ts"));
257 yield* report("MicroVM import * as AWS", stats, "aws");
258 expectWithinBudget(stats, "aws", AWS_MICROVM);
259 // Node maps minified stack traces back through the shipped map.
260 expect(files.map((file) => file.path)).toContain("index.mjs.map");
261 const dockerfile = buildMicrovmDockerfile(undefined, "node", 8080, { sourceMaps: true });
262 expect(dockerfile).toContain("COPY *.map /app/");
263 expect(dockerfile).toContain(
264 'ENTRYPOINT ["node", "--enable-source-maps", "/app/index.mjs"]',
265 );
266 }),
267 { tags: ["unit", "local"], timeout: 120_000 },
268 );
269
270 it.effect(
271 "Cloudflare Worker with a KV binding",
272 () =>
273 Effect.gen(function* () {
274 const namespace = yield* bundleWorker(fixture("cloudflare-namespace.ts"));
275 const subpath = yield* bundleWorker(fixture("cloudflare-subpath.ts"));
276 yield* report("import * as Cloudflare", namespace, "cloudflare");
277 yield* report("@alchemy.run/cloudflare/<Service>", subpath, "cloudflare");
278 expectWithinBudget(namespace, "cloudflare", CLOUDFLARE_WORKER);
279 expectWithinBudget(subpath, "cloudflare", CLOUDFLARE_WORKER);
280 expectSameCost(namespace, subpath);
281 }),
282 { tags: ["unit", "local"], timeout: 120_000 },
283 );
284 });
285
286 // `it.live`: the readiness retry sleeps on the real clock, which `layer`'s
287 // `it.effect` replaces with a test clock.
288 it.live(
289 "AWS MicroVM program boots minified and maps stack traces to source",
290 () =>
291 Effect.gen(function* () {
292 const { files } = yield* bundleMicrovm(fixture("aws-microvm.ts"));
293 const get = yield* bootMicrovm(files, 38_231);
294 expect(yield* get("/hello")).toEqual({ path: "/hello" });
295 const { stack } = (yield* get("/trace")) as { stack: string };
296 // `keepNames` keeps the function name; the source map points at the
297 // fixture, not the minified chunk.
298 expect(stack).toMatch(/at canaryThrows \(.*aws-microvm\.ts:\d+:\d+\)/);
299 }).pipe(
300 Effect.scoped,
301 Effect.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
302 ),
303 { tags: ["unit", "local"], timeout: 120_000 },
304 );