From 4cb2f4c7b68dceaed8f7ffbd8f0db3cfbf3f1ddc Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 09:01:17 +0000 Subject: =?UTF-8?q?http:=20SSE=20log=20tail,=20htmx-refreshed=20builds=20t?= =?UTF-8?q?able,=20vendored=20JS=20budget=20(=C2=A713)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GET /builds/:id/log.stream serves the build log as server-sent events from a blocking std::io::Read polled by the connection's thread — no async runtime, no timer wheel. The pipeline appends to a plain file and never learns the reader exists, so a viewer that hangs up, stalls or never shows up cannot slow a build by a microsecond. The stream ends when the build reaches a terminal state instead of holding a connection open forever, and there is a 30-minute lifetime cap so a wedged build cannot wedge a browser tab indefinitely. The test proves it is actually live rather than buffered: a producer thread appends while the stream is open, and the reader must see those bytes before the build finishes, then see the end event. The builds table refreshes with hx-trigger="every 3s" against a fragment endpoint, so htmx swaps one
rather than a whole document. Without JavaScript the same HTML already contains the table — the refresh is an enhancement, never a requirement. Static assets are served from include_bytes!, precompressed, with the Content-Length forced so tiny_http does not chunk a file whose size we know. Vendoring decision, recorded in docs/DECISIONS.md: the design's '~14 KB htmx' does not exist — released htmx is 48 KB minified. We vendor 1.9.12 unmodified and serve the 15.6 KB gzip, and the budget is asserted on served bytes with a bare-socket test, because an HTTP client would transparently decompress and hide the number. htmx's SSE extension is deliberately NOT vendored: it swaps with innerHTML and a build log is untrusted text from someone else's PKGBUILD, so wiring it up would be an XSS hole opened to make a log scroll by itself. The review surface still ships no JavaScript at all, asserted by grepping the rendered page for String { + aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); + let c = candidates::pending_candidates(&env.db).unwrap().remove(0); + let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); + let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + aur_build::review::approve::approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap(); + let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap() +} + +/// Read a streaming response until EOF or the deadline, whichever comes first. +fn drain(mut r: impl Read, deadline: Duration) -> String { + let mut buf = [0u8; 4096]; + let mut out = Vec::new(); + let start = Instant::now(); + loop { + if start.elapsed() > deadline { + break; + } + match r.read(&mut buf) { + Ok(0) => break, + Ok(n) => out.extend_from_slice(&buf[..n]), + Err(_) => break, + } + } + String::from_utf8_lossy(&out).into_owned() +} + +#[test] +fn the_log_stream_delivers_bytes_as_they_are_appended_and_then_ends() { + let (env, fx) = env_with_fixture("plain-make"); + let web = Web::attach(env, fx); + let id = queued(&web.env, "plain-make"); + let log = web.env.cfg.build_log_path(&id); + std::fs::create_dir_all(log.parent().unwrap()).unwrap(); + std::fs::write(&log, "==> step materialize\n").unwrap(); + + // A producer appends while the stream is open, then finishes the build. If + // the endpoint buffered its response, everything would arrive only after the + // build ended and the "live" claim would be false. + let db = web.env.db.clone(); + let id2 = id.clone(); + let log2 = log.clone(); + let producer = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(400)); + append(&log2, "==> step build\n"); + std::thread::sleep(Duration::from_millis(400)); + append(&log2, "==> step publish\n"); + std::thread::sleep(Duration::from_millis(200)); + builds::set_state(&db, &id2, build_state::SUCCEEDED, None).unwrap(); + }); + + let resp = ureq::get(&web.url(&format!("/builds/{id}/log.stream"))).call().expect("stream"); + assert_eq!(resp.header("content-type").unwrap_or(""), "text/event-stream"); + assert_eq!(resp.header("cache-control").unwrap_or(""), "no-cache"); + let body = drain(resp.into_reader(), Duration::from_secs(15)); + producer.join().unwrap(); + + assert!(body.contains("data: ==> step materialize"), "the first line is missing: {body}"); + assert!(body.contains("data: ==> step build"), "appended bytes never arrived: {body}"); + assert!(body.contains("data: ==> step publish"), "the tail stopped early: {body}"); + // It ends when the build does, rather than holding the connection forever. + assert!(body.contains("event: end"), "the stream never ended: {body}"); + // SSE framing: each log line is its own event, so a client can append. + assert!(body.contains("\n\n"), "not event-framed: {body}"); +} + +#[test] +fn the_log_stream_is_404_for_a_build_that_does_not_exist() { + let (env, fx) = env_with_fixture("plain-make"); + let web = Web::attach(env, fx); + let resp = ureq::get(&web.url("/builds/nope/log.stream")).call(); + match resp { + Err(ureq::Error::Status(404, _)) => {} + other => panic!("expected 404, got {other:?}"), + } +} + +#[test] +fn the_builds_table_refreshes_via_htmx_and_still_works_without_js() { + let (env, fx) = env_with_fixture("plain-make"); + let web = Web::attach(env, fx); + let base = web.url("/builds"); + + // One queued build so the table has a row: the empty state is a paragraph, + // and asserting on it would prove nothing about server rendering. + queued(&web.env, "plain-make"); + let page = ureq::get(&base).call().unwrap().into_string().unwrap(); + // The refresh is an attribute on the table, not a script we wrote. + assert!(page.contains("hx-trigger=\"every 3s\""), "no htmx refresh: {}", &page[..400.min(page.len())]); + assert!(page.contains("hx-get=\"/builds?fragment=1\""), "the refresh has no target"); + assert!(page.contains("