| 93 | // plain main.wasm fetch below gets the negotiated encoding, which the |
| 94 | // browser decompresses itself. |
| 95 | async function fetchWasm() { |
| 96 | const gz = await fetch("main.wasm.gz"); |
| 97 | if (gz.ok) { |
| 98 | // The counted stream sees compressed bytes, so progress runs |
| 99 | // against the compressed size on the wire. |
| 100 | const size = Number(gz.headers.get("Content-Length")) || 0; |
| 101 | const wasm = countProgress(gz.body, size, size) |
| 102 | .pipeThrough(new DecompressionStream("gzip")); |
| 103 | return new Response(wasm, { headers: { "Content-Type": "application/wasm" } }); |
| 104 | } |
| 105 | |
| 106 | const resp = await fetch("main.wasm"); |
| 107 | if (!resp.ok) { |
| 108 | throw new Error(`fetching main.wasm: ${resp.status}`); |
| 109 | } |
| 110 | // The body stream yields decompressed bytes, so progress is |
| 111 | // tracked against the uncompressed size, but the size shown to the |
| 112 | // user is what actually crosses the wire. |
| 113 | const total = Number(resp.headers.get("X-Uncompressed-Size")) || |
| 114 | Number(resp.headers.get("Content-Length")) || 0; |
| 115 | const wireBytes = Number(resp.headers.get("X-Compressed-Size")) || |
| 116 | Number(resp.headers.get("Content-Length")) || 0; |
| 117 | const counted = countProgress(resp.body, total, wireBytes); |
| 118 | return new Response(counted, { headers: { "Content-Type": "application/wasm" } }); |
| 119 | } |
| 120 | |
| 121 | // Boot the wasm module. |
| 122 | const ready = new Promise((resolve) => { globalThis.onTailcatReady = resolve; }); |