| 67 | // status line as bytes pass through: loaded/total drives the bar, |
| 68 | // wireBytes is the transfer size shown to the user. |
| 69 | function countProgress(stream, total, wireBytes) { |
| 70 | const ofMB = wireBytes > 0 ? ` of ${(wireBytes / (1 << 20)).toFixed(1)} MB` : ""; |
| 71 | const bar = $("load-progress"); |
| 72 | let loaded = 0; |
| 73 | return stream.pipeThrough(new TransformStream({ |
| 74 | transform(chunk, controller) { |
| 75 | loaded += chunk.byteLength; |
| 76 | if (total > 0) { |
| 77 | bar.value = loaded / total; |
| 78 | const pct = Math.min(100, Math.floor(100 * loaded / total)); |
| 79 | setStatus(`Loading WebAssembly… ${pct}%${ofMB}`); |
| 80 | } else { |
| 81 | setStatus(`Loading WebAssembly…`); |
| 82 | } |
| 83 | controller.enqueue(chunk); |
| 84 | }, |
| 85 | })); |
| 86 | } |
| 87 | |
| 88 | // fetchWasm fetches the wasm with load progress. Static hosts like |
| 89 | // GitHub Pages can't do Content-Encoding negotiation (and don't |