diff --git a/lib/examples/progress-browser.client.ts b/lib/examples/progress-browser.client.ts new file mode 100644 index 0000000000000000000000000000000000000000..637c0f5ee82dac090c66a6f9a2c1f7523222ad72 --- /dev/null +++ b/lib/examples/progress-browser.client.ts @@ -0,0 +1,22 @@ +// the browser half of `progress-browser.ts`: streams the server's root, and +// takes over the server rendered tree once the stream has caught up to it. +const theme = UNWRAP(document.querySelector("button")); +theme.addEventListener("click", () => { + theme.setAttribute("aria-pressed", String(document.documentElement.classList.toggle("dark"))); +}); + +const root = new progress.Root(); +const connect = () => fetch("/progress").then((response) => progress.decodeByteStream(UNWRAP(response.body), root)); + +let stream = connect(); +await Promise.race([root.once("change"), stream]).catch(() => {}); +progress.attachToDocument(root, UNWRAP(document.getElementById("progress"))); +while (true) { + await stream.catch((error) => console.warn("progress stream ended", error)); + await async.delay(1000); + stream = connect(); +} + +import { UNWRAP } from "../assert.ts"; +import * as async from "../async.ts"; +import * as progress from "../progress.ts"; diff --git a/lib/examples/progress-browser.ts b/lib/examples/progress-browser.ts new file mode 100644 index 0000000000000000000000000000000000000000..63ffb10856c8b69182f7077cdddc7ddc296bbb04 --- /dev/null +++ b/lib/examples/progress-browser.ts @@ -0,0 +1,179 @@ +// a pretend site build reports into a progress root on this server. the page +// is rendered with the tree as it stands, then the browser streams the same +// root to keep it live. +// +// node lib/examples/progress-browser.ts + +const root = new progress.Root(); +void (async () => { + while (true) { + await Promise.all([build(root), index(root)]); + await async.delay(3000); + } +})(); + +const { outputFiles } = await esbuild.build({ + entryPoints: [path.join(import.meta.dirname, "progress-browser.client.ts")], + bundle: true, + format: "esm", + minify: true, + write: false, + // leaves the node.js bindings out of the bundle + define: { "globalThis.process": "undefined" }, +}); +const script = UNWRAP(outputFiles[0]).text; + +const server = await http.serve({ + port: Number(process.env.PORT ?? 3000), + async respond(request) { + const { pathname } = new URL(request.url); + if (pathname === "/client.js") { + return new Response(script, { headers: { "content-type": "text/javascript" } }); + } + if (pathname === "/progress") { + return new Response(progress.encodeByteStream(root), { + headers: { "content-type": progress.contentType }, + }); + } + return new Response(page(progress.formatHtml(root.active)), { + headers: { "content-type": "text/html; charset=utf-8" }, + }); + }, +}); +log.info(`open ${server.url}`); + +async function build(p: progress.Ref) { + using node = p.start("Building paperclover.net"); + await Promise.all([scripts(node), videos(node), assets(node)]); + await pages(node); +} + +async function scripts(p: progress.Ref) { + using node = p.start("bundle client scripts", { total: 48 }); + const names = ["file-viewer", "clofi", "canvas_2023", "q+a", "guestbook", "friends", "video", "blog"]; + for (let i = 0; i < 48; i += 1) { + await wait(150, 450); + const name = UNWRAP(names[i % names.length]); + const size = (4 + Math.random() * 40).toFixed(1); + node.log.write(`${ansi.style(ansi.fgGreen, "✓")} /js/${name}.js ${ansi.style(ansi.dim, `${size} kB`)}\n`); + node.inc(); + } +} + +async function videos(p: progress.Ref) { + const files = [ + { name: "in-the-summer.mov", bytes: 940_000_000, rate: 32_000_000 }, + { name: "waterfalls.mov", bytes: 1_430_000_000, rate: 41_000_000 }, + { name: "history-of-japan-reanimated.mov", bytes: 610_000_000, rate: 22_000_000 }, + ]; + using group = p.start("encode videos", { + units: "bytes", + total: files.reduce((sum, file) => sum + file.bytes, 0), + }); + await Promise.all(files.map(async ({ name, bytes, rate }, i) => { + using node = group.start(name, { units: "bytes", total: bytes }); + let frame = 0; + while (node.value < bytes) { + await wait(200, 400); + const step = Math.min(bytes - node.value, rate * (0.15 + Math.random() * 0.2)); + node.value += step; + group.value += step; + frame += Math.round(24 * (0.4 + Math.random() * 0.4)); + if (frame % 9 === 0) { + const speed = (0.9 + Math.random() * 1.4).toFixed(2); + node.log.write(`frame=${String(frame).padStart(6)} fps=48 q=28.0 speed=${speed}x\n`); + } + if (i === 0 && frame > 400 && frame < 440) { + node.log.warn("bitrate capped at 8 Mbps for a 2160p source"); + } + if (i === 1 && frame > 700 && frame < 740) { + node.log.error(`frame ${frame}: corrupt packet, skipping`); + } + } + })); +} + +async function assets(p: progress.Ref) { + using node = p.start("fetch remote assets"); + const urls = [ + "fonts.paperclover.net/recultramin.woff2", + "fonts.paperclover.net/recmono.woff2", + "cdn.paperclover.net/hls/waterfalls/manifest.m3u8", + "cdn.paperclover.net/thumbs/in-the-summer.webp", + "cdn.paperclover.net/hls/in-the-summer/manifest.m3u8", + ]; + await Promise.all(urls.map(async (url) => { + await wait(0, 4000); + using file = node.start(url, { units: "bytes" }); + // the size is unknown until the response headers arrive + await wait(800, 2500); + file.total = Math.round(200_000 + Math.random() * 4_000_000); + while (file.value < file.total) { + await wait(100, 300); + file.value = Math.min(file.total, file.value + file.total * Math.random() * 0.12); + } + })); +} + +async function pages(p: progress.Ref) { + using node = p.start("render pages", { total: 312 }); + const routes = ["/q+a/", "/file/Music/", "/blog/", "/friends/"]; + await Promise.all(Array.from({ length: 3 }, async () => { + while (node.value < 312) { + const route = UNWRAP(routes[Math.floor(Math.random() * routes.length)]); + using page = node.start(route + Math.floor(Math.random() * 900)); + await wait(40, 160); + node.value += 1; + } + })); +} + +async function index(p: progress.Ref) { + using group = p.start("indexing files", { passive: true }); + for (const name of ["Clover - Waterfalls.flac", "Clover - Summer (demo).wav", "photos/IMG_2094.HEIC"]) { + using file = group.start(`/Music/${name} - hashing`, { units: "bytes", total: 80_000_000 }); + while (file.value < file.total) { + await wait(200, 500); + file.value = Math.min(file.total, file.value + 1_200_000 + Math.random() * 1_500_000); + } + } +} + +function wait(min: number, max: number) { + return async.delay(min + Math.random() * (max - min)); +} + +function page(ssr: string) { + return ` + + + +progress + +
+

@clo/lib/progress

+
${ssr}
+
+ +`; +} + +import * as esbuild from "esbuild"; +import * as path from "node:path"; +import { UNWRAP } from "../assert.ts"; +import * as async from "../async.ts"; +import * as http from "../http.ts"; +import * as log from "../log.ts"; +import * as progress from "../progress.ts"; +import * as ansi from "../string/ansi.ts";