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

main
ts 76 lines 3.17 KB
Raw
1 /**
2 * Enforces the deploy-target decoupling contract: the framework-generic
3 * modules (`SvelteKit.ts`, `index.ts`) must contain ZERO Cloudflare-specific
4 * imports. Everything platform-specific (the adapter fork, the worker shim,
5 * the rolldown finishing pass) lives behind the `./cloudflare` subpath
6 * module.
7 */
8 import * as NodeFs from "node:fs";
9 import * as NodePath from "node:path";
10 import { fileURLToPath } from "node:url";
11 import { describe, expect, it } from "vitest";
12
13 const srcDir = NodePath.resolve(NodePath.dirname(fileURLToPath(import.meta.url)), "..");
14
15 /** Every static import / re-export specifier in a module's source. */
16 const importSpecifiers = (file: string): Array<string> => {
17 const source = NodeFs.readFileSync(NodePath.join(srcDir, file), "utf8");
18 const specifiers: Array<string> = [];
19 // `import ... from "x"`, `export ... from "x"`, and bare `import "x"`.
20 const pattern = /(?:^|\n)\s*(?:import|export)\s+(?:[^"'\n]*?from\s*)?["']([^"']+)["']/g;
21 for (const match of source.matchAll(pattern)) {
22 specifiers.push(match[1] as string);
23 }
24 return specifiers;
25 };
26
27 // Cloudflare-specific dependency surface: platform packages, the bundler for
28 // the workerd finishing pass, and the in-package cloudflare modules.
29 const FORBIDDEN = [
30 /^@alchemy\.run\/cloudflare-runtime(?:\/|$)/,
31 /^@cloudflare\//,
32 /^cloudflare:/,
33 /^\.\/cloudflare(?:\.ts)?$/,
34 /^rolldown$/,
35 /\/Adapter(\.ts)?$/,
36 /\.\/Adapter/,
37 /\.\/WorkerShim/,
38 ];
39
40 const FRAMEWORK_GENERIC_MODULES = ["SvelteKit.ts", "UserConfig.ts", "index.ts"];
41
42 describe("target decoupling", () => {
43 for (const file of FRAMEWORK_GENERIC_MODULES) {
44 it(`${file} imports nothing Cloudflare-specific`, () => {
45 const specifiers = importSpecifiers(file);
46 expect(specifiers.length).toBeGreaterThan(0);
47 const offending = specifiers.filter((specifier) =>
48 FORBIDDEN.some((pattern) => pattern.test(specifier)),
49 );
50 expect(offending).toEqual([]);
51 });
52 }
53
54 it("platform target modules are the only src modules importing the adapter and rolldown", () => {
55 const files = NodeFs.readdirSync(srcDir).filter((file) => file.endsWith(".ts"));
56 for (const file of files) {
57 if (file === "cloudflare.ts" || file === "Adapter.ts" || file === "WorkerShim.ts") continue;
58 // `source.ts` is Cloudflare-specific by contract (it implements
59 // alchemy's Cloudflare Worker source) — it may import ./cloudflare.ts
60 // but must not reach around it to the adapter/bundler internals.
61 // `aws.ts` is the AWS target module — platform-specific by the same
62 // contract: it bundles the Lambda server with rolldown, but must
63 // stay decoupled from the Cloudflare adapter internals.
64 const allowRolldown = file === "aws.ts" || file === "node.ts";
65 const specifiers = importSpecifiers(file);
66 const offending = specifiers.filter(
67 (specifier) =>
68 (!allowRolldown && /^rolldown$/.test(specifier)) ||
69 /\.\/Adapter/.test(specifier) ||
70 /\.\/WorkerShim/.test(specifier) ||
71 /@alchemy\.run\/cloudflare-runtime/.test(specifier),
72 );
73 expect(offending, `${file} must not import ${offending.join(", ")}`).toEqual([]);
74 }
75 });
76 });