Web: WebAssembly build served cross-origin isolated, immutable and precompressed
The web app is built with --wasm (skwasm, JS/CanvasKit fallback beside it). The proxy serves it with COOP same-origin + COEP credentialless so skwasm renders on a worker; an installed build loads from /_v/<buildId>/ cached as immutable, other files revalidate by ETag, and the installer precompresses .br/.gz variants the proxy sends by Accept-Encoding. The bootstrap no longer registers Flutter's deprecated service worker (a reload per install). Read aloud on the web uses the Web Speech API through package:web: the flutter_tts web plugin's interop casts fail under wasm. Bench kit: isolation and headed modes, renderer and memory reporting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtemp, mkdir, utimes, writeFile } from "node:fs/promises";
|
||||
import { get } from "node:http";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
import { brotliCompressSync, brotliDecompressSync, gzipSync } from "node:zlib";
|
||||
import { acceptedEncodings } from "../src/appFiles.js";
|
||||
|
||||
const here = fileURLToPath(new URL("..", import.meta.url));
|
||||
|
||||
async function freePort() {
|
||||
const server = net.createServer().listen(0, "127.0.0.1");
|
||||
await new Promise((done) => server.once("listening", done));
|
||||
const { port } = server.address();
|
||||
await new Promise((done) => server.close(done));
|
||||
return port;
|
||||
}
|
||||
|
||||
async function start(root) {
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--root", root], {
|
||||
cwd: here,
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" },
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
await new Promise((done) => child.stdout.once("data", done));
|
||||
return { port, stop: () => child.kill() };
|
||||
}
|
||||
|
||||
/** A raw GET (fetch would decode the body and hide Content-Encoding). */
|
||||
function request(port, path, headers = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
get({ host: "127.0.0.1", port, path, headers }, (res) => {
|
||||
const chunks = [];
|
||||
res.on("data", (chunk) => chunks.push(chunk));
|
||||
res.on("end", () => resolve({ status: res.statusCode, headers: res.headers, body: Buffer.concat(chunks) }));
|
||||
}).on("error", reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function build({ release = true } = {}) {
|
||||
const root = join(await mkdtemp(join(tmpdir(), "app-files-")), "app");
|
||||
await mkdir(join(root, "canvaskit"), { recursive: true });
|
||||
await writeFile(join(root, "index.html"), '<!DOCTYPE html><html><head><base href="/"></head><body></body></html>');
|
||||
await writeFile(join(root, "flutter_bootstrap.js"), "bootstrap()");
|
||||
const script = "main();".repeat(400);
|
||||
await writeFile(join(root, "main.dart.js"), script);
|
||||
await writeFile(join(root, "main.dart.js.br"), brotliCompressSync(script));
|
||||
await writeFile(join(root, "main.dart.js.gz"), gzipSync(script));
|
||||
await writeFile(join(root, "canvaskit", "skwasm.wasm"), "wasm");
|
||||
if (release) await writeFile(join(root, "release.json"), '{"schema":1,"platform":"web","buildId":"web-abc123"}');
|
||||
return { root, script };
|
||||
}
|
||||
|
||||
test("the shell is isolated, uncached and names the versioned asset base", async (t) => {
|
||||
const { root } = await build();
|
||||
const server = await start(root);
|
||||
t.after(server.stop);
|
||||
for (const path of ["/", "/chat/7"]) {
|
||||
const page = await request(server.port, path);
|
||||
assert.equal(page.status, 200);
|
||||
assert.equal(page.headers["cross-origin-opener-policy"], "same-origin");
|
||||
assert.equal(page.headers["cross-origin-embedder-policy"], "credentialless");
|
||||
assert.equal(page.headers["cache-control"], "no-store");
|
||||
assert.match(page.body.toString(), /<meta name="pipelines-asset-base" content="\/_v\/web-abc123\/" \/>/);
|
||||
}
|
||||
});
|
||||
|
||||
test("versioned assets of the current build are immutable; another build's revalidate", async (t) => {
|
||||
const { root, script } = await build();
|
||||
const server = await start(root);
|
||||
t.after(server.stop);
|
||||
const current = await request(server.port, "/_v/web-abc123/main.dart.js", { "accept-encoding": "gzip, br" });
|
||||
assert.equal(current.status, 200);
|
||||
assert.equal(current.headers["cache-control"], "private, max-age=31536000, immutable");
|
||||
assert.equal(current.headers["content-encoding"], "br");
|
||||
assert.equal(current.headers.vary, "Accept-Encoding");
|
||||
assert.equal(current.headers["cross-origin-embedder-policy"], "credentialless");
|
||||
assert.equal(brotliDecompressSync(current.body).toString(), script);
|
||||
const wasm = await request(server.port, "/_v/web-abc123/canvaskit/skwasm.wasm");
|
||||
assert.equal(wasm.headers["content-type"], "application/wasm");
|
||||
assert.equal(wasm.body.toString(), "wasm");
|
||||
const stale = await request(server.port, "/_v/web-old/main.dart.js");
|
||||
assert.equal(stale.status, 200);
|
||||
assert.equal(stale.headers["cache-control"], "no-cache");
|
||||
assert.equal(stale.body.toString(), script);
|
||||
assert.equal((await request(server.port, "/_v/web-abc123/missing.js")).status, 404);
|
||||
assert.equal((await request(server.port, "/_v/web-abc123/%2e%2e/%2e%2e/etc/passwd")).status, 403);
|
||||
assert.equal((await request(server.port, "/_v/web-abc123/")).status, 404);
|
||||
});
|
||||
|
||||
test("unversioned files revalidate with an ETag, compressed only when accepted", async (t) => {
|
||||
const { root, script } = await build({ release: false });
|
||||
const server = await start(root);
|
||||
t.after(server.stop);
|
||||
const page = await request(server.port, "/");
|
||||
assert.doesNotMatch(page.body.toString(), /pipelines-asset-base/);
|
||||
const plain = await request(server.port, "/main.dart.js");
|
||||
assert.equal(plain.headers["cache-control"], "no-cache");
|
||||
assert.equal(plain.headers["content-encoding"], undefined);
|
||||
assert.equal(plain.body.toString(), script);
|
||||
const gzip = await request(server.port, "/main.dart.js", { "accept-encoding": "gzip" });
|
||||
assert.equal(gzip.headers["content-encoding"], "gzip");
|
||||
const again = await request(server.port, "/main.dart.js", { "if-none-match": plain.headers.etag });
|
||||
assert.equal(again.status, 304);
|
||||
assert.equal(again.body.length, 0);
|
||||
const bootstrap = await request(server.port, "/flutter_bootstrap.js");
|
||||
assert.equal(bootstrap.headers["cache-control"], "no-cache");
|
||||
// A variant older than its file (a hand-edited build) is not sent.
|
||||
const past = new Date(Date.now() - 60_000);
|
||||
await utimes(join(root, "main.dart.js.br"), past, past);
|
||||
const edited = await request(server.port, "/main.dart.js", { "accept-encoding": "br" });
|
||||
assert.equal(edited.headers["content-encoding"], undefined);
|
||||
});
|
||||
|
||||
test("accepted encodings honour q=0", () => {
|
||||
assert.deepEqual([...acceptedEncodings("gzip, deflate, br, zstd")], ["gzip", "deflate", "br", "zstd"]);
|
||||
assert.deepEqual([...acceptedEncodings("br;q=0, gzip;q=0.5")], ["gzip"]);
|
||||
assert.deepEqual([...acceptedEncodings(undefined)], []);
|
||||
});
|
||||
Reference in New Issue
Block a user