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"; import { createServer } from "node:http"; import { once } from "node:events"; 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"), "app"); 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/); }); 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"), ""); 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); }); test("files require sign-in and proxy query, ranges and attachment headers", async (t) => { const requests = []; const upstream = createServer((req, res) => { requests.push({ method: req.method, url: req.url, range: req.headers.range }); res.writeHead(req.headers.range ? 206 : 200, { "Content-Type": "application/octet-stream", "Content-Disposition": 'attachment; filename="report.bin"', "Accept-Ranges": "bytes", ...(req.headers.range ? { "Content-Range": "bytes 0-2/6" } : {}), }); res.end(req.headers.range ? "abc" : "abcdef"); }).listen(0, "127.0.0.1"); await once(upstream, "listening"); t.after(() => new Promise((done) => upstream.close(done))); const dir = await mkdtemp(join(tmpdir(), "serve-files-")); 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), "--pipelines", `127.0.0.1:${upstream.address().port}`], { cwd: here, env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, stdio: ["ignore", "pipe", "inherit"], }); t.after(async () => { const exited = once(child, "exit"); child.kill(); await exited; }); await once(child.stdout, "data"); const base = `http://127.0.0.1:${port}`; const path = "/files?path=%2Fhome%2Freport%20one.bin&download=1"; assert.equal((await fetch(base + path)).status, 401); assert.equal(requests.length, 0); const login = await fetch(base + "/login", { method: "POST", body: new URLSearchParams({ password: "password-1" }), redirect: "manual", }); const cookie = login.headers.get("set-cookie").split(";")[0]; const reply = await fetch(base + path, { headers: { Cookie: cookie, Range: "bytes=0-2" } }); assert.equal(reply.status, 206); assert.equal(await reply.text(), "abc"); assert.equal(reply.headers.get("content-disposition"), 'attachment; filename="report.bin"'); assert.equal(reply.headers.get("content-range"), "bytes 0-2/6"); const head = await fetch(base + path, { method: "HEAD", headers: { Cookie: cookie } }); assert.equal(head.status, 200); assert.equal(await head.text(), ""); assert.deepEqual(requests, [ { method: "GET", url: path, range: "bytes=0-2" }, { method: "HEAD", url: path, range: undefined }, ]); }); test("file uploads require sign-in and reach the server with their name header", async (t) => { const requests = []; const upstream = createServer(async (req, res) => { let body = ""; for await (const chunk of req) body += chunk; requests.push({ method: req.method, url: req.url, upload: req.headers["x-pipelines-upload-name"], type: req.headers["content-type"], body, }); res.writeHead(409, { "Content-Type": "text/plain; charset=utf-8" }); res.end("a file named notes.txt already exists"); }).listen(0, "127.0.0.1"); await once(upstream, "listening"); t.after(() => new Promise((done) => upstream.close(done))); const dir = await mkdtemp(join(tmpdir(), "serve-upload-")); 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), "--pipelines", `127.0.0.1:${upstream.address().port}`], { cwd: here, env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, stdio: ["ignore", "pipe", "inherit"], }); t.after(async () => { const exited = once(child, "exit"); child.kill(); await exited; }); await once(child.stdout, "data"); const base = `http://127.0.0.1:${port}`; const path = "/files?path=%2Fhome%2Fproject"; const post = (headers) => fetch(base + path, { method: "POST", headers: { "Content-Type": "text/plain", "X-Pipelines-Upload-Name": "notes.txt", ...headers }, body: "hello upload", }); assert.equal((await post({})).status, 401); assert.equal(requests.length, 0); const login = await fetch(base + "/login", { method: "POST", body: new URLSearchParams({ password: "password-1" }), redirect: "manual", }); const cookie = login.headers.get("set-cookie").split(";")[0]; const reply = await post({ Cookie: cookie }); assert.equal(reply.status, 409); assert.equal(await reply.text(), "a file named notes.txt already exists"); assert.deepEqual(requests, [ { method: "POST", url: path, upload: "notes.txt", type: "text/plain", body: "hello upload" }, ]); });