akic404 runs `magpie web` on a public server over a ~3 Mbps link, and
the page took very long to load. Measured in a sandbox (`magpie web` on
127.0.0.1:3461/3463, sandbox HOME): every load sent the page's ~4.1 MB
of scripts and styles as they are (i18n.js 1.8 MB, app.js 1.1 MB,
app.css 282 KB, routing.js 279 KB, library.js 211 KB) with no
compression, whatever the browser's Accept-Encoding, and the API's JSON
the same way: a day of the Routing page's history
(/api/gateway/history, a real 267-request day copied in) was 1.23 MB,
and the routing trace's JSON grows with every account a request could
go to. Nothing is embedded, vendored or source-mapped in routing.js;
the bytes were the uncompressed transfer.
- webHandler wraps `magpie web`'s guarded handler in gzhttp
(klauspost/compress, already a dependency): text, scripts, CSS, JSON
and SVG are gzipped for a client that takes gzip, with Vary:
Accept-Encoding; PNG/WebP/ICO/JPEG go as they are. A gzipped
response's ETag ends -gzip, and If-None-Match with it is mapped back
so a revalidation is still a 304. A 32-byte content-keyed jitter keeps
the compressed size from being exact (BREACH), since the page carries
keys. The desktop windows read the page in-process through Wails'
asset server, with no network, so they stay uncompressed.
- revalidated: a script or stylesheet asked for under its current
content hash (?v=…, as versionedPage names it) is sent with
Cache-Control: max-age=31536000, immutable; any other hash, no hash,
and the page itself stay no-cache + ETag. Applies to both paths.
Numbers (curl, page + its 21 scripts/styles + /api/state):
before: 4,142,180 bytes, with or without Accept-Encoding: gzip
after: 1,258,298 bytes with gzip (i18n.js 565 KB, app.js 361 KB,
routing.js 89 KB), 4,142,180 without
/api/gateway/history?day=: 1,233,600 -> 72,538 bytes
Real browser (Playwright, ?view=routing, 6s, response body bytes):
Chromium first load 4,340,720 -> 1,362,922; reload 75,336 -> 10,597
WebKit first load 4,340,672 -> 1,362,843; reload 115,667 -> 34,006
both engines: Routing view shown, no page errors or console errors.
Tests: TestWebCompresses and TestVersionedFilesKept (web_compress_test.go).
Broken by hand, each goes red: webHandler without webCompressed
(Content-Encoding "" for app.js, routing.js, /, /api/state, SVG); no
immutable branch ("/app.js?v=…: Cache-Control no-cache"); no
If-None-Match mapping ("asked again with its gzipped ETag: 200, want
304"); compressing every type ("a PNG: Content-Encoding gzip"); no
ETag suffix ("gzipped keeps the ETag of other bytes").
Run: go test -tags nogui ./internal/gui (ok); full go test -tags nogui
-timeout 40m ./... under a sandbox HOME (all ok); gofmt -l clean; go vet
./internal/gui on darwin, GOOS=linux -tags nogui, GOOS=windows with and
without nogui. GUI: routing-*.test.cjs, panel-routing, web-page,
mobile-web, phone-web, gateway-mode in Chromium and WebKit; results
after rebase are in the report (mobile-web was red on the old base
before ca0b95e3b, which these assets don't touch).
143 lines
5.4 KiB
Go
143 lines
5.4 KiB
Go
package gui
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// `magpie web` sent its page's ~4 MB of scripts and styles, and the API's
|
|
// JSON, as they are: over a 3 Mbps link to a server every load waited for
|
|
// all of it (akic404 on Discord). A browser that takes gzip gets them
|
|
// gzipped, a client that doesn't gets the bytes as they are, pictures
|
|
// already compressed are left alone, and a file asked for again by its
|
|
// ETag is still a 304.
|
|
func TestWebCompresses(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
srv := webHandler("3430", "0123456789abcdef-key", 0, Handler(webHost{}, nil))
|
|
cookie := &http.Cookie{Name: "magpie_web_3430", Value: "0123456789abcdef-key"}
|
|
get := func(path, enc, etag string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest("GET", path, nil)
|
|
r.AddCookie(cookie)
|
|
if enc != "" {
|
|
r.Header.Set("Accept-Encoding", enc)
|
|
}
|
|
if etag != "" {
|
|
r.Header.Set("If-None-Match", etag)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, r)
|
|
return rec
|
|
}
|
|
// what Chrome and Safari send over plain http
|
|
const browser = "gzip, deflate"
|
|
for _, f := range []string{"i18n.js", "app.js", "routing.js", "app.css", "index.html"} {
|
|
want, err := fs.ReadFile(staticFS(), f)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/" + f
|
|
if f == "index.html" {
|
|
path, want = "/", versionedPage(want)
|
|
}
|
|
rec := get(path, browser, "")
|
|
if rec.Code != http.StatusOK || rec.Header().Get("Content-Encoding") != "gzip" {
|
|
t.Errorf("%s to a browser that takes gzip: %d, Content-Encoding %q", path, rec.Code, rec.Header().Get("Content-Encoding"))
|
|
continue
|
|
}
|
|
if rec.Body.Len()*2 > len(want) {
|
|
t.Errorf("%s: %d bytes gzipped of %d", path, rec.Body.Len(), len(want))
|
|
}
|
|
zr, err := gzip.NewReader(bytes.NewReader(rec.Body.Bytes()))
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", path, err)
|
|
}
|
|
got, err := io.ReadAll(zr)
|
|
if err != nil || !bytes.Equal(got, want) {
|
|
t.Errorf("%s unzipped isn't the file (%d bytes of %d): %v", path, len(got), len(want), err)
|
|
}
|
|
if !strings.Contains(rec.Header().Get("Vary"), "Accept-Encoding") {
|
|
t.Errorf("%s: Vary %q, a cache would hand gzip to a client that can't read it", path, rec.Header().Get("Vary"))
|
|
}
|
|
etag := rec.Header().Get("ETag")
|
|
if !strings.HasSuffix(etag, `-gzip"`) {
|
|
t.Errorf("%s gzipped keeps the ETag of other bytes: %q", path, etag)
|
|
}
|
|
if again := get(path, browser, etag); again.Code != http.StatusNotModified {
|
|
t.Errorf("%s asked again with its gzipped ETag: %d, want 304", path, again.Code)
|
|
}
|
|
plain := get(path, "", "")
|
|
if plain.Header().Get("Content-Encoding") != "" || !bytes.Equal(plain.Body.Bytes(), want) {
|
|
t.Errorf("%s to a client that takes no gzip: Content-Encoding %q, %d bytes of %d", path, plain.Header().Get("Content-Encoding"), plain.Body.Len(), len(want))
|
|
}
|
|
}
|
|
if rec := get("/api/state", browser, ""); rec.Code != http.StatusOK || rec.Header().Get("Content-Encoding") != "gzip" {
|
|
t.Errorf("/api/state: %d, Content-Encoding %q, want its JSON gzipped", rec.Code, rec.Header().Get("Content-Encoding"))
|
|
}
|
|
if rec := get("/icons/crush.png", browser, ""); rec.Code != http.StatusOK || rec.Header().Get("Content-Encoding") != "" {
|
|
t.Errorf("a PNG: %d, Content-Encoding %q, want it as it is", rec.Code, rec.Header().Get("Content-Encoding"))
|
|
}
|
|
if rec := get("/icons/openai.svg", browser, ""); rec.Header().Get("Content-Encoding") != "gzip" {
|
|
t.Errorf("an SVG is text: Content-Encoding %q, want gzip", rec.Header().Get("Content-Encoding"))
|
|
}
|
|
// and still behind the key
|
|
r := httptest.NewRequest("GET", "/api/state", nil)
|
|
r.Header.Set("Accept-Encoding", browser)
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, r)
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Errorf("no key: %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
// A script or stylesheet asked for by the address the page names it by,
|
|
// its content's hash, is kept by the browser for good and not asked for
|
|
// again at each load; the same file under another version's hash, or
|
|
// none, is still asked for again (TestPageFilesRevalidate).
|
|
func TestVersionedFilesKept(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
srv := Handler(webHost{}, nil)
|
|
get := func(path string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
|
|
return rec
|
|
}
|
|
page := get("/").Body.String()
|
|
for _, f := range []string{"app.js", "routing.js", "i18n.js", "app.css"} {
|
|
b, err := fs.ReadFile(staticFS(), f)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256(b)
|
|
named := "/" + f + "?v=" + hex.EncodeToString(sum[:6])
|
|
if !strings.Contains(page, `"`+named[1:]+`"`) {
|
|
t.Fatalf("the page doesn't name %s", named)
|
|
}
|
|
if cc := get(named).Header().Get("Cache-Control"); cc != "max-age=31536000, immutable" {
|
|
t.Errorf("%s: Cache-Control %q, want it kept for good", named, cc)
|
|
}
|
|
if cc := get("/" + f + "?v=000000000000").Header().Get("Cache-Control"); cc != "no-cache" {
|
|
t.Errorf("%s under another version's hash: Cache-Control %q, want no-cache", f, cc)
|
|
}
|
|
}
|
|
if cc := get("/").Header().Get("Cache-Control"); cc != "no-cache" {
|
|
t.Errorf("the page: Cache-Control %q, want no-cache", cc)
|
|
}
|
|
}
|