startup-budget.ts
This file
Path
scripts/startup-budget.ts
154 lines · 4.5 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 1why, one step back1/**Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 2–3why, one step back2 * The limits are regression alarms, not Cloudflare's upload limit. Each Worker3 * has its own, so one Worker's growth never hides in another's headroom.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 4why, one step back4 *Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 5–7why, one step back5 * A dynamic `import()` does not shrink the entry: Wrangler's esbuild build has6 * no code splitting and inlines it, so moving a dependency off the entry takes7 * a module of its own (`find_additional_modules`) or a Worker of its own.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 8why, one step back8 *Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 9–10why, one step back9 * The web's JavaScript counts the Worker's server modules only: chunks only10 * the browser loads are assets.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 11–15why, one step back11 */12import { BunRuntime, BunServices } from "@effect/platform-bun";13import { Console, Data, Effect, FileSystem, Path, pipe } from "effect";14import { ChildProcess, ChildProcessSpawner } from "effect/process";15Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 16–19why, one step back16interface Budget {17 readonly entry: number;18 readonly javascript: number;19}Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 20why, one step back20Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 21why, one step back21export const budgets = {Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 22why, one step back22 "apps/platform": { entry: 1_900_000, javascript: 1_900_000 },Close the code browser and drafting wave: its decisions in two ADRs, every rule it added held, and its leftovers folded inoscabrielbefore adoption009640c
line 23why, one step back23 "apps/sandbox": { entry: 656_000, javascript: 656_000 },Merge redesign into mainoscabrielbefore adoptioncaedd21
line 24why, one step back24 "apps/web": { entry: 355_000, javascript: 3_191_000 },Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 25why, one step back25} as const satisfies Record<string, Budget>;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 26–33why, one step back2627export const workers: readonly Worker[] = [28 { dir: "apps/platform", entry: "worker.js" },29 { dir: "apps/sandbox", entry: "worker.js" },30 { build: ["vite", "build"], dir: "apps/web", entry: "index.js" },31];3233interface Worker {Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 34why, one step back34 readonly dir: keyof typeof budgets;Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 35–120why, one step back35 readonly entry: string;36 readonly build?: readonly [string, ...string[]];37}3839class BuildFailed extends Data.TaggedError("BuildFailed")<{40 readonly worker: string;41 readonly exitCode: number;42}> {}4344class OverBudget extends Data.TaggedError("OverBudget")<{45 readonly worker: string;46 readonly scope: "entry" | "javascript";47 readonly bytes: number;48 readonly budget: number;49}> {}5051const env = { WRANGLER_LOG: "error", WRANGLER_SEND_METRICS: "false" };5253const runBuild = (54 dir: string,55 [command, ...args]: readonly [string, ...string[]]56) =>57 Effect.gen(function* () {58 const path = yield* Path.Path;59 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;6061 const exitCode = yield* spawner.exitCode(62 ChildProcess.make(path.resolve(dir, "node_modules/.bin", command), args, {63 cwd: dir,64 // A production build, whatever the shell says: the development65 // condition would measure the web's dev backend instead.66 env: { ...env, NODE_ENV: "production" },67 extendEnv: true,68 stderr: "inherit",69 stdout: "ignore",70 })71 );7273 return exitCode === 074 ? undefined75 : yield* new BuildFailed({ exitCode, worker: dir });76 });7778const measure = Effect.fnUntraced(function* (worker: Worker) {79 const fs = yield* FileSystem.FileSystem;80 const path = yield* Path.Path;81 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;82 const outdir = yield* fs.makeTempDirectoryScoped();8384 if (worker.build !== undefined) {85 yield* runBuild(worker.dir, worker.build);86 }8788 const exitCode = yield* spawner.exitCode(89 ChildProcess.make(90 path.resolve(worker.dir, "node_modules/.bin/wrangler"),91 ["deploy", "--dry-run", "--outdir", outdir],92 {93 cwd: worker.dir,94 env,95 extendEnv: true,96 stderr: "inherit",97 stdout: "ignore",98 }99 )100 );101102 if (exitCode !== 0) {103 return yield* new BuildFailed({ exitCode, worker: worker.dir });104 }105106 const javascript = (yield* fs.readDirectory(outdir, { recursive: true }))107 .filter((file) => file.endsWith(".js"))108 .toSorted();109110 const sizes = yield* pipe(111 javascript,112 Effect.forEach((file) =>113 fs114 .readFile(path.join(outdir, file))115 .pipe(Effect.map((bytes) => ({ bytes: bytes.length, file })))116 )117 );118119 const entry = sizes.find(({ file }) => file === worker.entry)?.bytes ?? 0;120 const total = sizes.reduce((sum, { bytes }) => sum + bytes, 0);Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 121why, one step back121 const budget: Budget = budgets[worker.dir];Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 122–123why, one step back122123 yield* Console.log(Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 124why, one step back124 `bytes:check: ${worker.dir}: entry ${entry} of ${budget.entry}; JavaScript ${total} of ${budget.javascript}`Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 125–126why, one step back125 );126Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 127why, one step back127 if (entry > budget.entry) {Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 128why, one step back128 return yield* new OverBudget({Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 129why, one step back129 budget: budget.entry,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 130–135why, one step back130 bytes: entry,131 scope: "entry",132 worker: worker.dir,133 });134 }135Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 136why, one step back136 if (total > budget.javascript) {Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 137why, one step back137 return yield* new OverBudget({Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 138why, one step back138 budget: budget.javascript,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 139–154why, one step back139 bytes: total,140 scope: "javascript",141 worker: worker.dir,142 });143 }144145 return { entry, total };146}, Effect.scoped);147148if (import.meta.main) {149 BunRuntime.runMain(150 Effect.forEach(workers, measure, { discard: true }).pipe(151 Effect.provide(BunServices.layer)152 )153 );154}