Split The code is public; the app answers only to the crew.

One bundle, built at start-up: stop committing static/app.*.js so a merge cannot serve last week's game #30

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/cs:scout/26-one-bundle-built-at-start-up-stop-commit → main 1 commit fork

  • Merged. scout/26-one-bundle-built-at-start-up-stop-commit was merged into main as 214d1c9.

Review

Sign in to review this change.
8 files changed +82 −161
.gitignore +1
@@ -2,3 +2,4 @@ node_modules/
22 *.db
33 *.db-wal
44 *.db-shm
5 static/app.*.js
README.md +11 −5
@@ -24,15 +24,18 @@ The rest of the commands:
2424
2525 ```sh
2626 bun test # the check every pull runs
27 bun run scripts/build.ts # rebuild static/app.<hash>.js
27 bun run scripts/build.ts # write static/app.<hash>.js, for inspection
2828 bun run src/tty.ts # the terminal interface, same database
2929 bun run scripts/train.ts --iters 1000 # retrain the bots' policy
3030 bun run scripts/train.ts --iters 1000 --workers 4 # or on four cores
3131 bun run scripts/train.ts --bench # how fast the arena steps
3232 ```
3333
34 The bundle in `static/` is committed, so a fresh clone plays without building;
35 `bun test` fails if it is stale, and `bun run scripts/build.ts` fixes that.
34 The client is bundled at start-up, in memory, from the sources on disk — the
35 server logs how long it took — and is not committed: `static/app.*.js` is
36 git-ignored, so a merge can never serve a bundle that predates it.
37 `bun run scripts/build.ts` writes the same bundle to a file for anyone who
38 wants one, but the running server never reads it.
3639
3740 ## What is real and what is not
3841
@@ -141,8 +144,11 @@ with the canvas as you turn; nobody re-walks the grid to draw it.
141144 - `src/server.ts` — the HTTP server and the security floor. `src/db.ts` is the
142145 schema and its queries, `src/result.ts` validates what a browser may claim,
143146 `src/pages/game.ts` renders the page.
144 - `scripts/build.ts` bundles the client into `static/app.<hash>.js`, committed
145 and cached for a year. `bun test` fails if what is committed is stale.
147 - `src/bundle.ts` bundles the client; `src/server.ts` runs it at start-up and
148 serves the result from memory as `/static/app.<hash>.js`, cached for a year.
149 `scripts/build.ts` is a thin CLI over the same function, for a file on disk.
150 Neither the file nor the served bytes are ever stale, because there is
151 nothing committed to go stale.
146152 - No dependencies. Nothing is installed to run this.
147153
148154 It also has a terminal: the exact command is in `FORGE_APP_SSH` when the app
scripts/build.ts +5 −21
@@ -1,31 +1,15 @@
11 /**
2 * The build: one bundle, named by its content hash, written to static/.
3 * `bun run build` writes it; the test compares what is committed against what
4 * the sources produce now, so a stale bundle fails the check instead of the
5 * browser.
2 * `bun run build` writes the client bundle to static/, for anyone who wants
3 * a file on disk to inspect. The server does not read it: it builds its own
4 * copy into memory at start-up (see src/bundle.ts), so this file is never
5 * committed and never served.
66 */
77
88 import { mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
99 import { join } from "node:path";
10 import { bundle } from "../src/bundle.ts";
1011
1112 export const STATIC_DIR = join(import.meta.dir, "..", "static");
12 const ENTRY = join(import.meta.dir, "..", "src", "client", "main.ts");
13
14 /** Bundle the client and return the file name its contents deserve. */
15 export async function bundle(): Promise<{ name: string; code: string }> {
16 const built = await Bun.build({
17 entrypoints: [ENTRY],
18 target: "browser",
19 minify: true,
20 format: "esm",
21 });
22 if (!built.success) throw new Error(built.logs.map(String).join("\n"));
23 const output = built.outputs[0];
24 if (output === undefined) throw new Error("the bundle produced no output");
25 const code = await output.text();
26 const hash = new Bun.CryptoHasher("sha256").update(code).digest("hex").slice(0, 10);
27 return { name: `app.${hash}.js`, code };
28 }
2913
3014 export async function write(): Promise<string> {
3115 const { name, code } = await bundle();
src/bundle.ts added +23
@@ -0,0 +1,23 @@
1 /**
2 * The client bundle: one file, named by the hash of its own bytes, so a
3 * source change is a new URL and a stale cache is impossible.
4 */
5
6 import { join } from "node:path";
7
8 const ENTRY = join(import.meta.dir, "client", "main.ts");
9
10 export async function bundle(): Promise<{ name: string; code: string }> {
11 const built = await Bun.build({
12 entrypoints: [ENTRY],
13 target: "browser",
14 minify: true,
15 format: "esm",
16 });
17 if (!built.success) throw new Error(built.logs.map(String).join("\n"));
18 const output = built.outputs[0];
19 if (output === undefined) throw new Error("the bundle produced no output");
20 const code = await output.text();
21 const hash = new Bun.CryptoHasher("sha256").update(code).digest("hex").slice(0, 10);
22 return { name: `app.${hash}.js`, code };
23 }
src/server.test.ts +12 −7
@@ -1,8 +1,8 @@
11 import { expect, test } from "bun:test";
2 import { mkdtempSync, readdirSync } from "node:fs";
2 import { mkdtempSync } from "node:fs";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
5 import { bundle, STATIC_DIR } from "../scripts/build.ts";
5 import { bundle } from "./bundle.ts";
66 import { parseResult } from "./result.ts";
77
88 process.env.FORGE_APP_DATA = mkdtempSync(join(tmpdir(), "app-"));
@@ -132,18 +132,23 @@ test("a bug answers with the floor, not the runtime's page", async () => {
132132 expect(response.headers.get("content-security-policy")).toContain("default-src 'self'");
133133 });
134134
135 test("the bundle in static/ is the one the sources produce", async () => {
136 // A stale bundle is a browser running last week's game: fail here instead.
135 test("the page's script is the one the running sources produce", async () => {
136 // A start-up bundle stale against these sources is a browser running last
137 // week's game: fail here instead.
137138 const { name } = await bundle();
138 expect(readdirSync(STATIC_DIR)).toContain(name);
139 const page = await (await fetch(base + "/")).text();
140 expect(page).toContain(`/static/${name}`);
139141 });
140142
141 test("the script is served immutable and cached for a year", async () => {
143 test("the script is served immutable, as javascript, under the security floor", async () => {
142144 const page = await (await fetch(base + "/")).text();
143145 const src = /src="(\/static\/app\.[0-9a-f]+\.js)"/.exec(page)?.[1] as string;
144146 const response = await fetch(base + src);
145147 expect(response.status).toBe(200);
146 expect(response.headers.get("cache-control")).toContain("immutable");
148 expect(response.headers.get("content-type")).toBe("text/javascript; charset=utf-8");
149 expect(response.headers.get("cache-control")).toBe("public, max-age=31536000, immutable");
150 expect(response.headers.get("content-security-policy")).toContain("default-src 'self'");
151 expect(response.headers.get("x-content-type-options")).toBe("nosniff");
147152 });
148153
149154 test("a result parses to exactly what was sent, and nothing else", () => {
src/server.ts +30 −12
@@ -6,8 +6,8 @@
66 * FORGE_APP_DATA. Production and each preview get their own directory.
77 */
88
9 import { existsSync, readdirSync } from "node:fs";
109 import { join } from "node:path";
10 import { bundle } from "./bundle.ts";
1111 import { openDatabase, saveMatch } from "./db.ts";
1212 import { escape, gamePage, statsHtml } from "./pages/game.ts";
1313 import { parseResult } from "./result.ts";
@@ -109,14 +109,20 @@ const newSession = (): string =>
109109 const sessionCookie = (session: string): string =>
110110 `${SESSION_COOKIE}=${session}; Path=/; HttpOnly; SameSite=Lax; Max-Age=31536000${secure ? "; Secure" : ""}`;
111111
112 /** The built bundle, named by its content so it can be cached for a year. */
113 function findScript(): string {
114 const dir = join(import.meta.dir, "..", "static");
115 if (!existsSync(dir)) return "/static/app.js";
116 const name = readdirSync(dir).find((f) => /^app\.[0-9a-f]+\.js$/.test(f));
117 return `/static/${name ?? "app.js"}`;
112 // Built once at start-up, kept in memory: a browser can never be served a
113 // bundle that predates the sources it names. A build that fails is loud —
114 // it never gets to open the port on a page with no game.
115 const buildStart = performance.now();
116 let scriptName: string;
117 let scriptCode: string;
118 try {
119 ({ name: scriptName, code: scriptCode } = await bundle());
120 } catch (error) {
121 console.error("failed to build the client bundle:", error);
122 process.exit(1);
118123 }
119 const script = findScript();
124 const buildMs = Math.round(performance.now() - buildStart);
125 const script = `/static/${scriptName}`;
120126
121127 export const server = Bun.serve({
122128 port: Number(process.env.PORT ?? 8080),
@@ -153,9 +159,19 @@ export const server = Bun.serve({
153159 : response;
154160 }
155161
156 // Files under ./static are served as they are, cached for a year: they are
157 // named by content (app.3f2a9c.js), so a change is a new name, never a
158 // stale cache. The path is confined to the directory.
162 // The bundle lives in memory, not on disk: it is what start-up just
163 // built, so this is the one URL the page's <script src> can ever mean.
164 if ((request.method === "GET" || request.method === "HEAD") && url.pathname === script) {
165 const headers = {
166 "content-type": "text/javascript; charset=utf-8",
167 "cache-control": "public, max-age=31536000, immutable",
168 ...SECURITY,
169 };
170 return request.method === "HEAD" ? new Response(null, { headers }) : new Response(scriptCode, { headers });
171 }
172
173 // Other files under ./static are served as they are, cached for a year.
174 // The path is confined to the directory.
159175 if (url.pathname.startsWith("/static/") && !url.pathname.includes("..")) {
160176 const file = Bun.file(join(import.meta.dir, "..", url.pathname.slice(1)));
161177 if (await file.exists())
@@ -175,4 +191,6 @@ export const server = Bun.serve({
175191 },
176192 });
177193
178 console.log(`cs listening on http://${server.hostname}:${server.port} (${env}) serving ${script}`);
194 console.log(
195 `cs listening on http://${server.hostname}:${server.port} (${env}) serving ${script} (built in ${buildMs}ms)`,
196 );
static/app.942a1ec39a.js deleted −58

Diff not shown — a line 63,210 characters wide (limit 2,000). −58

View file
static/app.d616a602f9.js deleted −58

Diff not shown — a line 62,054 characters wide (limit 2,000). −58

View file

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs