aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 02a5afd8f7c3e78161e84421b78c61abf2d5819d
parent 92e3b10807c5101000a95e65cc6dd3b8e08b7602
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 04:54:47 +0000

Revalidate browser bundles with precise file versions

Diffstat:
Mdocs/repository-packaging.md | 17++++++++++-------
Mserver/browser_client.go | 17++++++++++++++++-
Mserver/browser_client_test.go | 64+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 89 insertions(+), 9 deletions(-)

diff --git a/docs/repository-packaging.md b/docs/repository-packaging.md @@ -6,7 +6,7 @@ local experiments/references, 4.8 GiB of client build products and 2.6 GiB of toolchains. Those files are not copied by Git clone. Current tracked source and documentation occupied about 565 MiB uncompressed. -No history rewrite was needed. Packed historical obsolete browser/code paths +That initial audit needed no history rewrite. Packed historical obsolete browser/code paths accounted for about 181 KB, and the largest packed source blob was 2.3 MB. The old 13.2 decompilation/build and original client installations are ignored local archives, not gigabytes hidden in Git history. @@ -31,17 +31,20 @@ or made public. ## Clone and run -Clone the repository normally, change into its root and run `go run ./server`. -Go 1.24 or newer and a compatible browser are sufficient. The first Go build may +Clone the repository normally and copy the complete `client/` bundle from a +client build machine using the [README instructions](../README.md). Generated +browser output is ignored and must not be committed. Change into the repository +root and run `go run ./server`. Go 1.24 or newer and a compatible browser are +sufficient once the bundle is copied. The first Go build may download the small Go module dependencies. Open `http://127.0.0.1:8080/`. -The server stays on loopback; run it on the laptop where the browser is running. +The server listens on all interfaces; use its HTTPS port from other machines. `go test ./server` prepares bundled inputs if necessary and then runs against temporary copies, away from saved characters. The authored NPC population expectations are checked in under `server/testdata/`; the full extraction catalog and Python data tools are not required for Go tests. -The checked-in browser build is about 46 MiB compressed. Runtime data occupies +The copied browser bundle is separate from Git. Checked-in runtime data occupies about 906 MiB compressed, split into 48 MiB parts so no individual blob is huge. This includes all portable 14.9.1 playfields, audio, textures, fonts/configuration, and the terrain, zone links, NPC data and map images used by the Go server. @@ -85,8 +88,8 @@ stopped afterward. ## Updating published files `make wasm -j8` builds dependencies and the client, then publishes the matching -page, JavaScript and compressed WASM to `client/`. Commit the four published -files together. `client/build.json` records the source revision, current source +page, JavaScript, profiler worker and compressed WASM to ignored `client/`. +Copy all five published files together. `client/build.json` records the source revision, current source digest, compiler identity and output hashes. Do not edit generated JavaScript directly; edit `src/runtime/` or `src/browser/` and republish. diff --git a/server/browser_client.go b/server/browser_client.go @@ -2,18 +2,33 @@ package main import ( "net/http" + "os" + "path/filepath" + "strings" ) func browserClient(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cross-Origin-Opener-Policy", "same-origin") w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp") w.Header().Set("Cache-Control", "no-cache") + // Fixed bundle URLs revalidate without transferring unchanged files. + name := strings.TrimPrefix(r.URL.Path, "/wasm/") + if name == "/" || name == "" { + name = "index.html" + } else if name == "game.wasm" { + name = "game.wasm.gz" + } + if filepath.IsLocal(name) { + if info, err := os.Stat(filepath.Join("client", name)); err == nil && info.Mode().IsRegular() { + w.Header().Set("ETag", browserAssetVersion(info)) + } + } if r.URL.Path == "/" { http.ServeFile(w, r, "client/index.html") return } - // Browsers decode the committed gzip representation before compiling WASM. + // Browsers decode the published gzip representation before compiling WASM. if r.URL.Path == "/wasm/game.wasm" { w.Header().Set("Content-Type", "application/wasm") w.Header().Set("Content-Encoding", "gzip") diff --git a/server/browser_client_test.go b/server/browser_client_test.go @@ -8,9 +8,10 @@ import ( "net/http/httptest" "os" "testing" + "time" ) -func TestBrowserClientServesCommittedWasm(t *testing.T) { +func TestBrowserClientServesPublishedWasm(t *testing.T) { t.Chdir(t.TempDir()) if err := os.Mkdir("client", 0755); err != nil { t.Fatal(err) @@ -49,6 +50,67 @@ func TestBrowserClientServesCommittedWasm(t *testing.T) { } } +func TestBrowserClientRevalidatesUnchangedFiles(t *testing.T) { + for _, name := range []string{"game.js", "game.wasm.gz", "index.html", "profile-worker.js", "build.json"} { + t.Run(name, func(t *testing.T) { + t.Chdir(t.TempDir()) + if err := os.Mkdir("client", 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile("client/"+name, []byte("bundle"), 0644); err != nil { + t.Fatal(err) + } + path := "/wasm/" + name + if name == "index.html" { + path = "/" + } + first := httptest.NewRecorder() + browserClient(first, httptest.NewRequest("GET", path, nil)) + request := httptest.NewRequest("GET", path, nil) + request.Header.Set("If-None-Match", first.Header().Get("ETag")) + response := httptest.NewRecorder() + + browserClient(response, request) + + if first.Header().Get("ETag") == "" || response.Code != http.StatusNotModified || response.Body.Len() != 0 { + t.Fatalf("cache revalidation: %d %v %q", response.Code, response.Header(), response.Body.String()) + } + }) + } +} + +func TestBrowserClientDetectsSameSecondReplacement(t *testing.T) { + t.Chdir(t.TempDir()) + if err := os.Mkdir("client", 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile("client/game.js", []byte("old"), 0644); err != nil { + t.Fatal(err) + } + stamp := time.Unix(1700000000, 100) + if err := os.Chtimes("client/game.js", stamp, stamp); err != nil { + t.Fatal(err) + } + first := httptest.NewRecorder() + browserClient(first, httptest.NewRequest("GET", "/wasm/game.js", nil)) + if err := os.WriteFile("client/game.js", []byte("new"), 0644); err != nil { + t.Fatal(err) + } + stamp = time.Unix(1700000000, 200) + if err := os.Chtimes("client/game.js", stamp, stamp); err != nil { + t.Fatal(err) + } + request := httptest.NewRequest("GET", "/wasm/game.js", nil) + request.Header.Set("If-None-Match", first.Header().Get("ETag")) + response := httptest.NewRecorder() + + browserClient(response, request) + + if response.Code != http.StatusOK || response.Body.String() != "new" || response.Header().Get("ETag") == first.Header().Get("ETag") { + t.Fatalf("replacement response: %d %v %q", response.Code, response.Header(), response.Body.String()) + } +} + func TestBrowserClientServesPublishedPageWithoutBuildDirectory(t *testing.T) { t.Chdir(t.TempDir()) if err := os.Mkdir("client", 0755); err != nil {