feat(web): icons and the web manifest are public for browsers and app installers

This commit is contained in:
2026-10-23 21:51:49 +01:00
parent 8527506da1
commit a5f7cdf302
3 changed files with 48 additions and 4 deletions
+5
View File
@@ -51,6 +51,8 @@ async function staticFile(base, pathname, req, res) {
} catch { reply(res, 404, 'Not found'); }
}
const PUBLIC_ASSET = /^\/(favicon\.(svg|png|ico)|apple-touch-icon\.png|manifest\.json|icons\/[A-Za-z0-9-]+\.png)$/;
function upstreamRequest(target, options, callback) {
return (target.protocol === 'https:' ? httpsRequest : httpRequest)(
{ protocol: target.protocol, hostname: target.hostname, port: target.port || undefined,
@@ -125,6 +127,9 @@ export async function start({ root = resolve('build/web'), upstream = 'http://12
}
if (pathname === '/login' || pathname === '/logout' || pathname === '/chat-uploads' || pathname.startsWith('/artifacts/')) return proxy(req, res, target);
if (req.method !== 'GET' && req.method !== 'HEAD') return reply(res, 405);
// Icons and the web manifest are public: browsers and app installers
// fetch them without the session cookie.
if (PUBLIC_ASSET.test(pathname)) return staticFile(base, pathname, req, res);
authCheck(req, target, (authenticated) => {
if (res.destroyed) return;
if (authenticated === null) return reply(res, 502, 'Authentication upstream unavailable');
+16 -4
View File
@@ -304,6 +304,13 @@ function resolvePath(urlPath) {
return target;
}
/** Icons and the web manifest, which need no sign-in. Fixed names only. */
const PUBLIC_ASSET =
/^\/(favicon\.(svg|png|ico)|apple-touch-icon\.png|manifest\.json|icons\/[A-Za-z0-9-]+\.png)$/;
function isPublicAsset(pathname) {
return PUBLIC_ASSET.test(pathname);
}
/** One file of the Android app's update feed, or 404. */
async function serveUpdate(req, res, pathname) {
let name;
@@ -381,11 +388,16 @@ function proxyArtifact(req, res) {
const server = createServer(async (req, res) => {
const pathname = (req.url ?? "/").split("?")[0];
// Brand icons are public so the sign-in page itself can show the app icon.
if (req.method === "GET" && (pathname === "/favicon.svg" || pathname === "/apple-touch-icon.png")) {
const body = await readFile(join(HERE, pathname.slice(1))).catch(() => null);
// Icons and the web manifest are public: the sign-in page shows the icon,
// and browsers and app installers fetch them without the session cookie.
if ((req.method === "GET" || req.method === "HEAD") && isPublicAsset(pathname)) {
const name = pathname.slice(1);
const body =
(await readFile(join(ROOT, name)).catch(() => null)) ??
(await readFile(join(HERE, name)).catch(() => null));
if (body) {
res.writeHead(200, { "Content-Type": MIME[extname(pathname)], "Cache-Control": "no-store" }).end(body);
res.writeHead(200, { "Content-Type": MIME[extname(pathname)], "Cache-Control": "no-cache" });
res.end(req.method === "HEAD" ? undefined : body);
return;
}
}
+27
View File
@@ -62,3 +62,30 @@ test("explains how to build the app when it is missing", async (t) => {
assert.equal(page.status, 503);
assert.match(await page.text(), /\.\/install --web/);
});
test("icons and the manifest need no sign-in; the app does", async (t) => {
const dir = await mkdtemp(join(tmpdir(), "serve-"));
const root = join(dir, "app");
await mkdir(join(root, "icons"), { recursive: true });
await writeFile(join(root, "index.html"), "<html><head></head></html>");
await writeFile(join(root, "favicon.png"), "png");
await writeFile(join(root, "manifest.json"), "{}");
await writeFile(join(root, "icons", "Icon-192.png"), "png");
await writeFile(join(root, "main.dart.js"), "app");
const auth = join(dir, "auth.json");
await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) }));
const port = await freePort();
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--root", root], {
cwd: here,
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth },
stdio: ["ignore", "pipe", "inherit"],
});
await new Promise((done) => child.stdout.once("data", done));
t.after(() => child.kill());
const base = `http://127.0.0.1:${port}`;
for (const path of ["/favicon.png", "/favicon.svg", "/manifest.json", "/icons/Icon-192.png"]) {
assert.equal((await fetch(base + path)).status, 200, path);
}
assert.equal((await fetch(`${base}/main.dart.js`)).status, 401);
assert.equal((await fetch(`${base}/icons/../main.dart.js`)).status, 401);
});