Files
pipelines/app/tool/capture/serve.mjs
T
alan 3b1760fe8e 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>
2026-12-01 21:05:17 +00:00

67 lines
2.4 KiB
JavaScript

// Static loopback server for a Flutter web build, on an OS-assigned port.
// Extension-less paths (`/runs/7`) fall back to index.html, as the Pipelines
// proxy serves the single page.
import { createServer } from 'node:http';
import { readFile, stat } from 'node:fs/promises';
import { once } from 'node:events';
import { extname, join, resolve, sep } from 'node:path';
const MIME = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.wasm': 'application/wasm',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.ttf': 'font/ttf',
'.otf': 'font/otf',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
};
// `CAPTURE_ISOLATE=1` adds the Pipelines proxy's cross-origin isolation
// headers (web/src/appFiles.js), so a `--wasm` build renders multi-threaded as
// it does when installed; without them skwasm runs single-threaded.
const ISOLATE = process.env.CAPTURE_ISOLATE === '1'
? { 'cross-origin-opener-policy': 'same-origin', 'cross-origin-embedder-policy': 'credentialless' }
: {};
/** Serves [dir]; resolves to `{ origin, close() }`. */
export async function serveBuild(dir) {
const root = resolve(dir);
if (!(await stat(join(root, 'index.html')).catch(() => null))) {
throw new Error(`No index.html in ${root}; build the probe first (see README).`);
}
const server = createServer(async (req, res) => {
try {
const pathname = decodeURIComponent(new URL(req.url, 'http://x').pathname);
let file = resolve(root, '.' + pathname);
if (file !== root && !file.startsWith(root + sep)) return res.writeHead(403).end();
try {
if ((await stat(file)).isDirectory()) file = join(file, 'index.html');
} catch {
if (!extname(pathname)) file = join(root, 'index.html');
}
const body = await readFile(file);
res.writeHead(200, {
'content-type': MIME[extname(file)] ?? 'application/octet-stream',
'cache-control': 'no-store',
...ISOLATE,
}).end(body);
} catch {
res.writeHead(404).end('not found');
}
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
return {
origin: `http://127.0.0.1:${server.address().port}`,
close: () => new Promise((done) => {
server.closeAllConnections?.();
server.close(() => done());
}),
};
}