feat(install): build the Flutter web app in Docker, optional ./install --apk, chosen web password, startup report
This commit is contained in:
+75
-15
@@ -1,10 +1,12 @@
|
||||
// Zero-dependency static file server + WebSocket proxy for the spike.
|
||||
// The authenticated web front door for Pipelines: the sign-in page, the Flutter
|
||||
// web app, and a proxy to the Pipelines server, with no dependencies.
|
||||
// node serve.mjs [--port 8090] [--host 0.0.0.0] [--pipelines 127.0.0.1:8080]
|
||||
// [--root DIR] [--updates DIR]
|
||||
//
|
||||
// Serves this directory, proxies GET/Upgrade /connect and GET/HEAD /artifacts/ to
|
||||
// the Pipelines server, so the page, the WebSocket and artifact files share one
|
||||
// origin. The API and its artifacts live on the server's port; the page is served
|
||||
// from another, so artifact URLs resolved against the page would 404 without this.
|
||||
// Serves the app (--root, default ../app/build/web) and proxies /connect,
|
||||
// /artifacts/ and /chat-uploads to the Pipelines server, so the page, the
|
||||
// WebSocket and artifact files share one origin. --updates serves the Android
|
||||
// app's update feed at /updates/.
|
||||
//
|
||||
// Why the proxy exists: internal/server/websocket.go calls
|
||||
// `websocket.Accept(w, r, nil)` (github.com/coder/websocket), which rejects an
|
||||
@@ -17,7 +19,7 @@
|
||||
// The default host is 127.0.0.1. Use --host 0.0.0.0 only on a trusted LAN/VPN.
|
||||
import { createServer, request } from "node:http";
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { createReadStream, readFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { readFile, stat } from "node:fs/promises";
|
||||
import { extname, join, normalize, resolve, sep } from "node:path";
|
||||
@@ -25,7 +27,13 @@ import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
import { sameOriginUpgrade } from "./src/proxySecurity.js";
|
||||
|
||||
const ROOT = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
// The proxy's own directory holds the sign-in page's icons.
|
||||
const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
// The page root: `--root DIR` (the installer's Flutter web build), by default
|
||||
// the checkout's app/build/web. `--updates DIR` serves the Android app's
|
||||
// version.json and APK at /updates/, behind the same sign-in.
|
||||
const ROOT = resolve(flag(process.argv, "--root", "") || join(HERE, "..", "app", "build", "web"));
|
||||
const UPDATES = flag(process.argv, "--updates", "") ? resolve(flag(process.argv, "--updates", "")) : null;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Access control
|
||||
@@ -239,6 +247,14 @@ const MIME = {
|
||||
".png": "image/png",
|
||||
".ico": "image/x-icon",
|
||||
".map": "application/json; charset=utf-8",
|
||||
".wasm": "application/wasm",
|
||||
".otf": "font/otf",
|
||||
".ttf": "font/ttf",
|
||||
".woff": "font/woff",
|
||||
".woff2": "font/woff2",
|
||||
".bin": "application/octet-stream",
|
||||
".frag": "application/octet-stream",
|
||||
".apk": "application/vnd.android.package-archive",
|
||||
};
|
||||
|
||||
// A path is asset-looking when its last segment ends in a known file type, and a
|
||||
@@ -288,9 +304,46 @@ function resolvePath(urlPath) {
|
||||
return target;
|
||||
}
|
||||
|
||||
/** One file of the Android app's update feed, or 404. */
|
||||
async function serveUpdate(req, res, pathname) {
|
||||
let name;
|
||||
try {
|
||||
name = decodeURIComponent(pathname.slice("/updates/".length));
|
||||
} catch {
|
||||
name = "";
|
||||
}
|
||||
const path = resolve(join(UPDATES, name));
|
||||
const info = name && path.startsWith(UPDATES + sep) ? await stat(path).catch(() => null) : null;
|
||||
if (!info?.isFile()) {
|
||||
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, {
|
||||
"Content-Type": MIME[extname(path)] ?? "application/octet-stream",
|
||||
"Content-Length": info.size,
|
||||
"Cache-Control": "no-store",
|
||||
});
|
||||
if (req.method === "HEAD") {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
createReadStream(path).pipe(res);
|
||||
}
|
||||
|
||||
/** Send index.html (the page shell). The router reads the real URL in the browser. */
|
||||
async function serveIndex(res) {
|
||||
const html = await readFile(join(ROOT, "index.html"), "utf8");
|
||||
const html = await readFile(join(ROOT, "index.html"), "utf8").catch(() => null);
|
||||
if (html === null) {
|
||||
// The installer builds the app in Docker; without it there is no page yet.
|
||||
res.writeHead(503, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
|
||||
res.end(
|
||||
'<!doctype html><meta charset="utf-8"><title>Pipelines</title>' +
|
||||
'<body style="font-family:system-ui;padding:2rem;background:#101013;color:#cfd9e6">' +
|
||||
"<h1>The web app is not built yet</h1>" +
|
||||
"<p>On the server, start Docker and run <code>./install --web</code>, then reload.</p>",
|
||||
);
|
||||
return;
|
||||
}
|
||||
// A deep URL such as /runs/440 would otherwise resolve the shell's relative
|
||||
// `./src/app.js` against /runs/. A root <base> keeps the shell's own asset URLs
|
||||
// absolute without changing the address bar (and without editing index.html).
|
||||
@@ -328,14 +381,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);
|
||||
if (body) {
|
||||
res.writeHead(200, { "Content-Type": MIME[extname(pathname)], "Cache-Control": "no-store" }).end(body);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (auth) {
|
||||
// Brand assets 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")
|
||||
) {
|
||||
// fall through to the static handler below
|
||||
} else if (pathname === "/login" && req.method === "GET") {
|
||||
if (pathname === "/login" && req.method === "GET") {
|
||||
if (isAuthed(req)) {
|
||||
res.writeHead(302, { Location: "/" }).end();
|
||||
return;
|
||||
@@ -392,6 +447,10 @@ const server = createServer(async (req, res) => {
|
||||
proxyArtifact(req, res);
|
||||
return;
|
||||
}
|
||||
if (UPDATES && pathname.startsWith("/updates/") && (req.method === "GET" || req.method === "HEAD")) {
|
||||
await serveUpdate(req, res, pathname);
|
||||
return;
|
||||
}
|
||||
if (pathname === "/favicon.ico") {
|
||||
res.writeHead(204).end(); // no icon; keeps the console free of a 404
|
||||
return;
|
||||
@@ -497,4 +556,5 @@ server.listen(port, host, () => {
|
||||
console.log(`web spike: http://${shown}:${port}/ (bound ${host}:${port})`);
|
||||
console.log(`proxying /connect (ws) and /artifacts/ (http) -> ${targetHost}:${targetPort}`);
|
||||
console.log(auth ? "auth: sign-in required" : "auth: DISABLED (tests)");
|
||||
console.log(`serving ${ROOT}${UPDATES ? `; app updates from ${UPDATES}` : ""}`);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtemp, mkdir, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
|
||||
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(args) {
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), ...args], {
|
||||
cwd: here,
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" },
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
await new Promise((done) => child.stdout.once("data", done));
|
||||
return { base: `http://127.0.0.1:${port}`, stop: () => child.kill() };
|
||||
}
|
||||
|
||||
test("serves the app root, deep routes and the update feed", async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "serve-"));
|
||||
const root = join(dir, "app");
|
||||
const updates = join(dir, "updates");
|
||||
await mkdir(join(root, "canvaskit"), { recursive: true });
|
||||
await mkdir(updates);
|
||||
await writeFile(join(root, "index.html"), "<html><head></head><body>app</body></html>");
|
||||
await writeFile(join(root, "canvaskit", "canvaskit.wasm"), "wasm");
|
||||
await writeFile(join(updates, "version.json"), '{"versionCode":1}');
|
||||
await writeFile(join(dir, "secret.txt"), "no");
|
||||
const server = await start(["--root", root, "--updates", updates]);
|
||||
t.after(server.stop);
|
||||
|
||||
const index = await fetch(`${server.base}/runs/12`);
|
||||
assert.equal(index.status, 200);
|
||||
assert.match(await index.text(), /app/);
|
||||
const wasm = await fetch(`${server.base}/canvaskit/canvaskit.wasm`);
|
||||
assert.equal(wasm.headers.get("content-type"), "application/wasm");
|
||||
const feed = await fetch(`${server.base}/updates/version.json`);
|
||||
assert.deepEqual(await feed.json(), { versionCode: 1 });
|
||||
assert.equal((await fetch(`${server.base}/updates/%2e%2e%2fsecret.txt`)).status, 404);
|
||||
assert.equal((await fetch(`${server.base}/updates/missing.apk`)).status, 404);
|
||||
const icon = await fetch(`${server.base}/favicon.svg`);
|
||||
assert.equal(icon.status, 200);
|
||||
});
|
||||
|
||||
test("explains how to build the app when it is missing", async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "serve-"));
|
||||
const server = await start(["--root", join(dir, "absent")]);
|
||||
t.after(server.stop);
|
||||
const page = await fetch(`${server.base}/`);
|
||||
assert.equal(page.status, 503);
|
||||
assert.match(await page.text(), /\.\/install --web/);
|
||||
});
|
||||
Reference in New Issue
Block a user