| ... | @@ -0,0 +1,179 @@ |
| 1 | // a pretend site build reports into a progress root on this server. the page |
| 2 | // is rendered with the tree as it stands, then the browser streams the same |
| 3 | // root to keep it live. |
| 4 | // |
| 5 | // node lib/examples/progress-browser.ts |
| 6 | |
| 7 | const root = new progress.Root(); |
| 8 | void (async () => { |
| 9 | while (true) { |
| 10 | await Promise.all([build(root), index(root)]); |
| 11 | await async.delay(3000); |
| 12 | } |
| 13 | })(); |
| 14 | |
| 15 | const { outputFiles } = await esbuild.build({ |
| 16 | entryPoints: [path.join(import.meta.dirname, "progress-browser.client.ts")], |
| 17 | bundle: true, |
| 18 | format: "esm", |
| 19 | minify: true, |
| 20 | write: false, |
| 21 | // leaves the node.js bindings out of the bundle |
| 22 | define: { "globalThis.process": "undefined" }, |
| 23 | }); |
| 24 | const script = UNWRAP(outputFiles[0]).text; |
| 25 | |
| 26 | const server = await http.serve({ |
| 27 | port: Number(process.env.PORT ?? 3000), |
| 28 | async respond(request) { |
| 29 | const { pathname } = new URL(request.url); |
| 30 | if (pathname === "/client.js") { |
| 31 | return new Response(script, { headers: { "content-type": "text/javascript" } }); |
| 32 | } |
| 33 | if (pathname === "/progress") { |
| 34 | return new Response(progress.encodeByteStream(root), { |
| 35 | headers: { "content-type": progress.contentType }, |
| 36 | }); |
| 37 | } |
| 38 | return new Response(page(progress.formatHtml(root.active)), { |
| 39 | headers: { "content-type": "text/html; charset=utf-8" }, |
| 40 | }); |
| 41 | }, |
| 42 | }); |
| 43 | log.info(`open ${server.url}`); |
| 44 | |
| 45 | async function build(p: progress.Ref) { |
| 46 | using node = p.start("Building paperclover.net"); |
| 47 | await Promise.all([scripts(node), videos(node), assets(node)]); |
| 48 | await pages(node); |
| 49 | } |
| 50 | |
| 51 | async function scripts(p: progress.Ref) { |
| 52 | using node = p.start("bundle client scripts", { total: 48 }); |
| 53 | const names = ["file-viewer", "clofi", "canvas_2023", "q+a", "guestbook", "friends", "video", "blog"]; |
| 54 | for (let i = 0; i < 48; i += 1) { |
| 55 | await wait(150, 450); |
| 56 | const name = UNWRAP(names[i % names.length]); |
| 57 | const size = (4 + Math.random() * 40).toFixed(1); |
| 58 | node.log.write(`${ansi.style(ansi.fgGreen, "✓")} /js/${name}.js ${ansi.style(ansi.dim, `${size} kB`)}\n`); |
| 59 | node.inc(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | async function videos(p: progress.Ref) { |
| 64 | const files = [ |
| 65 | { name: "in-the-summer.mov", bytes: 940_000_000, rate: 32_000_000 }, |
| 66 | { name: "waterfalls.mov", bytes: 1_430_000_000, rate: 41_000_000 }, |
| 67 | { name: "history-of-japan-reanimated.mov", bytes: 610_000_000, rate: 22_000_000 }, |
| 68 | ]; |
| 69 | using group = p.start("encode videos", { |
| 70 | units: "bytes", |
| 71 | total: files.reduce((sum, file) => sum + file.bytes, 0), |
| 72 | }); |
| 73 | await Promise.all(files.map(async ({ name, bytes, rate }, i) => { |
| 74 | using node = group.start(name, { units: "bytes", total: bytes }); |
| 75 | let frame = 0; |
| 76 | while (node.value < bytes) { |
| 77 | await wait(200, 400); |
| 78 | const step = Math.min(bytes - node.value, rate * (0.15 + Math.random() * 0.2)); |
| 79 | node.value += step; |
| 80 | group.value += step; |
| 81 | frame += Math.round(24 * (0.4 + Math.random() * 0.4)); |
| 82 | if (frame % 9 === 0) { |
| 83 | const speed = (0.9 + Math.random() * 1.4).toFixed(2); |
| 84 | node.log.write(`frame=${String(frame).padStart(6)} fps=48 q=28.0 speed=${speed}x\n`); |
| 85 | } |
| 86 | if (i === 0 && frame > 400 && frame < 440) { |
| 87 | node.log.warn("bitrate capped at 8 Mbps for a 2160p source"); |
| 88 | } |
| 89 | if (i === 1 && frame > 700 && frame < 740) { |
| 90 | node.log.error(`frame ${frame}: corrupt packet, skipping`); |
| 91 | } |
| 92 | } |
| 93 | })); |
| 94 | } |
| 95 | |
| 96 | async function assets(p: progress.Ref) { |
| 97 | using node = p.start("fetch remote assets"); |
| 98 | const urls = [ |
| 99 | "fonts.paperclover.net/recultramin.woff2", |
| 100 | "fonts.paperclover.net/recmono.woff2", |
| 101 | "cdn.paperclover.net/hls/waterfalls/manifest.m3u8", |
| 102 | "cdn.paperclover.net/thumbs/in-the-summer.webp", |
| 103 | "cdn.paperclover.net/hls/in-the-summer/manifest.m3u8", |
| 104 | ]; |
| 105 | await Promise.all(urls.map(async (url) => { |
| 106 | await wait(0, 4000); |
| 107 | using file = node.start(url, { units: "bytes" }); |
| 108 | // the size is unknown until the response headers arrive |
| 109 | await wait(800, 2500); |
| 110 | file.total = Math.round(200_000 + Math.random() * 4_000_000); |
| 111 | while (file.value < file.total) { |
| 112 | await wait(100, 300); |
| 113 | file.value = Math.min(file.total, file.value + file.total * Math.random() * 0.12); |
| 114 | } |
| 115 | })); |
| 116 | } |
| 117 | |
| 118 | async function pages(p: progress.Ref) { |
| 119 | using node = p.start("render pages", { total: 312 }); |
| 120 | const routes = ["/q+a/", "/file/Music/", "/blog/", "/friends/"]; |
| 121 | await Promise.all(Array.from({ length: 3 }, async () => { |
| 122 | while (node.value < 312) { |
| 123 | const route = UNWRAP(routes[Math.floor(Math.random() * routes.length)]); |
| 124 | using page = node.start(route + Math.floor(Math.random() * 900)); |
| 125 | await wait(40, 160); |
| 126 | node.value += 1; |
| 127 | } |
| 128 | })); |
| 129 | } |
| 130 | |
| 131 | async function index(p: progress.Ref) { |
| 132 | using group = p.start("indexing files", { passive: true }); |
| 133 | for (const name of ["Clover - Waterfalls.flac", "Clover - Summer (demo).wav", "photos/IMG_2094.HEIC"]) { |
| 134 | using file = group.start(`/Music/${name} - hashing`, { units: "bytes", total: 80_000_000 }); |
| 135 | while (file.value < file.total) { |
| 136 | await wait(200, 500); |
| 137 | file.value = Math.min(file.total, file.value + 1_200_000 + Math.random() * 1_500_000); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | function wait(min: number, max: number) { |
| 143 | return async.delay(min + Math.random() * (max - min)); |
| 144 | } |
| 145 | |
| 146 | function page(ssr: string) { |
| 147 | return `<!doctype html> |
| 148 | <html lang=en> |
| 149 | <meta charset=utf-8> |
| 150 | <meta name=viewport content="width=device-width,initial-scale=1"> |
| 151 | <title>progress</title> |
| 152 | <style> |
| 153 | :root { color-scheme: light; font: 13px/1.4 system-ui, sans-serif; background: light-dark(#e9edf2, #121417); |
| 154 | color: light-dark(#1e1e1e, #e3e5e8) } |
| 155 | :root.dark { color-scheme: dark } |
| 156 | main { max-width: 760px; margin: 40px auto; padding: 16px 20px 20px; border-radius: 10px; |
| 157 | background: light-dark(#fff, #1c1f24); box-shadow: 0 0 0 1px light-dark(#0000000f, #ffffff12), 0 8px 30px #0000001a } |
| 158 | header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px } |
| 159 | h1 { margin: 0; font-size: 15px; font-weight: 600 } |
| 160 | button { font: inherit; padding: 3px 10px; border-radius: 6px; color: inherit; cursor: pointer; |
| 161 | border: 1px solid light-dark(#c9d1da, #3d444d); background: light-dark(#f6f8fa, #262a30) } |
| 162 | #progress { min-height: 280px } |
| 163 | </style> |
| 164 | <main> |
| 165 | <header><h1>@clo/lib/progress</h1><button aria-pressed=false>Dark theme</button></header> |
| 166 | <div id=progress>${ssr}</div> |
| 167 | </main> |
| 168 | <script type=module src=/client.js></script> |
| 169 | `; |
| 170 | } |
| 171 | |
| 172 | import * as esbuild from "esbuild"; |
| 173 | import * as path from "node:path"; |
| 174 | import { UNWRAP } from "../assert.ts"; |
| 175 | import * as async from "../async.ts"; |
| 176 | import * as http from "../http.ts"; |
| 177 | import * as log from "../log.ts"; |
| 178 | import * as progress from "../progress.ts"; |
| 179 | import * as ansi from "../string/ansi.ts"; |