From c9b941183dc9c8ee1920b8be34cbcd419dea8394 Mon Sep 17 00:00:00 2001 From: Codex Date: Sun, 1 Nov 2026 19:54:09 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Compress=20vendor=20assets=20and=20?= =?UTF-8?q?add=20bounded=20static=20runtime=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/api/main.go | 29 +++++-- cmd/api/static_compression_test.go | 79 +++++++++++++++++ public/vendor/movi-player-0.4.1/README.md | 21 ++++- .../vendor/movi-player-0.4.1/element.slim.js | 4 +- scripts/lib/precompress-assets.mjs | 13 +++ src/lib/movi-player.ts | 72 ++++++++-------- src/lib/static-runtime.js | 83 ++++++++++++++++++ tests/browser/static-wasm-fallback.mjs | 71 ++++++++++++++++ tests/frontend/movi-player.test.mjs | 2 +- tests/frontend/movi-stats-request.test.mjs | 5 +- tests/frontend/precompress-assets.test.mjs | 25 ++++++ tests/frontend/static-runtime.test.mjs | 84 +++++++++++++++++++ vite.config.ts | 35 ++------ 13 files changed, 444 insertions(+), 79 deletions(-) create mode 100644 cmd/api/static_compression_test.go create mode 100644 scripts/lib/precompress-assets.mjs create mode 100644 src/lib/static-runtime.js create mode 100644 tests/browser/static-wasm-fallback.mjs create mode 100644 tests/frontend/precompress-assets.test.mjs create mode 100644 tests/frontend/static-runtime.test.mjs diff --git a/cmd/api/main.go b/cmd/api/main.go index a72a87a3..68b58bd1 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -83,19 +83,38 @@ func spaHandler(apiHandler http.Handler, distDir string) http.Handler { } var precompressedContentTypes = map[string]string{ - ".js": "text/javascript; charset=utf-8", - ".css": "text/css; charset=utf-8", + ".js": "text/javascript; charset=utf-8", + ".css": "text/css; charset=utf-8", + ".wasm": "application/wasm", } func requestAcceptsGzip(request *http.Request) bool { + wildcard := false for _, encoding := range request.Header.Values("Accept-Encoding") { for _, part := range strings.Split(encoding, ",") { - if strings.TrimSpace(strings.SplitN(part, ";", 2)[0]) == "gzip" { - return true + fields := strings.Split(part, ";") + name := strings.ToLower(strings.TrimSpace(fields[0])) + quality := 1.0 + for _, parameter := range fields[1:] { + pair := strings.SplitN(strings.TrimSpace(parameter), "=", 2) + if len(pair) == 2 && strings.EqualFold(pair[0], "q") { + value, err := strconv.ParseFloat(pair[1], 64) + if err != nil || value < 0 || value > 1 { + quality = 0 + } else { + quality = value + } + } + } + if name == "gzip" { + return quality > 0 + } + if name == "*" { + wildcard = quality > 0 } } } - return false + return wildcard } // servePrecompressedAsset streams the build-time .gz sibling for compressible diff --git a/cmd/api/static_compression_test.go b/cmd/api/static_compression_test.go new file mode 100644 index 00000000..3f5e145d --- /dev/null +++ b/cmd/api/static_compression_test.go @@ -0,0 +1,79 @@ +package main + +import ( + "bytes" + "compress/gzip" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func TestVendorPrecompressionNegotiation(t *testing.T) { + root := t.TempDir() + for _, name := range []string{"element.slim.js", "movi.wasm"} { + path := filepath.Join(root, "vendor", "player", name) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + raw := []byte("frozen runtime bytes") + var compressed bytes.Buffer + z := gzip.NewWriter(&compressed) + _, _ = z.Write(raw) + _ = z.Close() + if err := os.WriteFile(path, raw, 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path+".gz", compressed.Bytes(), 0o644); err != nil { + t.Fatal(err) + } + handler := spaHandler(http.NotFoundHandler(), root) + for _, tc := range []struct { + encoding, method, rangeHeader string + gzip bool + status int + }{ + {"gzip", "GET", "", true, 200}, {"br, gzip;q=0.8", "GET", "", true, 200}, + {"gzip;q=0, *;q=1", "GET", "", false, 200}, {"br", "GET", "", false, 200}, + {"gzip", "HEAD", "", true, 200}, {"gzip", "GET", "bytes=0-3", false, 206}, + } { + request := httptest.NewRequest(tc.method, "/vendor/player/"+name, nil) + request.Header.Set("Accept-Encoding", tc.encoding) + if tc.rangeHeader != "" { + request.Header.Set("Range", tc.rangeHeader) + } + response := httptest.NewRecorder() + handler.ServeHTTP(response, request) + if response.Code != tc.status || (response.Header().Get("Content-Encoding") == "gzip") != tc.gzip { + t.Fatalf("%s %+v: status=%d headers=%v", name, tc, response.Code, response.Header()) + } + if tc.method == "HEAD" { + if response.Body.Len() != 0 { + t.Fatal("HEAD returned a body") + } + continue + } + body := response.Body.Bytes() + if tc.gzip { + reader, err := gzip.NewReader(bytes.NewReader(body)) + if err != nil { + t.Fatal(err) + } + body, err = io.ReadAll(reader) + if err != nil { + t.Fatal(err) + } + _ = reader.Close() + } + expected := raw + if tc.status == 206 { + expected = raw[:4] + } + if !bytes.Equal(body, expected) { + t.Fatalf("mismatched delivered bytes for %+v", tc) + } + } + } +} diff --git a/public/vendor/movi-player-0.4.1/README.md b/public/vendor/movi-player-0.4.1/README.md index 984cacc4..17f2993c 100644 --- a/public/vendor/movi-player-0.4.1/README.md +++ b/public/vendor/movi-player-0.4.1/README.md @@ -19,7 +19,7 @@ into a NEW versioned directory next to this one, update `MOVI_VERSION` in ## Local patch (2026-10-12): playback-rate cap raised 2 → 4 -`element.slim.js` differs from the pristine npm tarball by exactly one change: +`element.slim.js` differs from the pristine npm tarball by the rate-cap change and the bounded WASM loading extension below: `getMaxAllowedRate()` returns `4` instead of `2`. Upstream hard-caps the host element's `playbackRate` setter (`Math.max(0.25, Math.min(max, value))`), which silently clamped the workbench speed selector (0.5–4×) to 2× — reviewers and @@ -31,11 +31,11 @@ not, re-apply this one-line patch to the new dist and update the hash below. Because `/vendor/` is served with a year-long immutable Cache-Control (`cmd/api/main.go`), any patch to `element.slim.js` MUST also bump the `?v=` marker in `MOVI_ELEMENT_JS_URL` (`src/lib/movi-player.ts`); current marker -`rate-cap-4-20261012` (guarded by `tests/frontend/movi-player.test.mjs`). +`static-fallback-20261101` (guarded by `tests/frontend/movi-player.test.mjs`). SHA-256 (this directory, element.slim.js includes the patch above): -- `element.slim.js` `c243c027397494d93c6e6dd73260e8f9b7fb57525f2993f34067d54a64ca0c5c` +- `element.slim.js` `e79a25cf22b6be37702b582623dca8b934baaa28f80a0ab84be056eecbbc97dc` - `movi.wasm` `a90da45449126b10b68ac2b816aa28b9ce43cd7d3e307e7201e0ebd73f92f530` ## Host-element quirks (found in production, 2026-10-12) @@ -83,3 +83,18 @@ element via a plain module `