@@ -38,7 +38,7 @@ interface RpcRequest {
3838 args : unknown [ ] ;
3939}
4040
41- const MB = 1_048_576 ;
41+ const MB = 1_000_000 ;
4242let wasmReadyPromise : Promise < void > | null = null ;
4343
4444// ─────────────────────────────────────────────────────────────────────────────
@@ -58,42 +58,47 @@ function progress(percentage: number, text_id: string, other: string = '') {
5858
5959async function fetchWasm ( url : string ) : Promise < ArrayBuffer > {
6060 const response = await fetch ( url ) ;
61- if ( ! response . ok || ! response . body ) {
62- throw new Error ( `WASM fetch failed: HTTP ${ response . status } or missing body` ) ;
61+ if ( ! response . ok ) {
62+ throw new Error ( `WASM fetch failed: HTTP ${ response . status } ` ) ;
63+ }
64+ if ( ! response . body ) {
65+ throw new Error ( 'WASM fetch failed: missing body' ) ;
66+ }
67+
68+ const total = Number ( response . headers . get ( 'Content-Length' ) ) ;
69+ if ( ! total ) {
70+ throw new Error ( 'WASM fetch failed: missing Content-Length' ) ;
6371 }
6472
65- const contentLength = response . headers . get ( 'Content-Length' ) ;
66- const encoding = response . headers . get ( 'Content-Encoding' ) ;
67- const total = contentLength ? parseInt ( contentLength , 10 ) : 1 ;
6873 const reader = response . body . getReader ( ) ;
6974 const chunks : Uint8Array [ ] = [ ] ;
75+
7076 let received = 0 ;
7177
7278 while ( true ) {
7379 const { done, value } = await reader . read ( ) ;
74- if ( done ) break ;
80+ if ( done ) {
81+ break ;
82+ }
7583
7684 chunks . push ( value ) ;
7785 received += value . byteLength ;
78- const encodedRecieved = received / ( encoding == 'gzip' ? 4 : 1 ) ; // account for ~4x compression with Gzip
7986
80- const pct = total ? 5 + Math . round ( ( encodedRecieved / total ) * 85 ) : 50 ;
81- const sizeMsg = total
82- ? `${ ( encodedRecieved / MB ) . toFixed ( 1 ) } / ${ ( total / MB ) . toFixed ( 1 ) } MB`
83- : `${ ( encodedRecieved / MB ) . toFixed ( 1 ) } MB` ;
87+ const pct = 5 + Math . round ( ( received / total ) * 85 ) ;
88+ const sizeMsg = `${ ( received / MB ) . toFixed ( 1 ) } / ${ ( total / MB ) . toFixed ( 1 ) } MB` ;
8489
85- progress ( Math . min ( pct , 90 ) , ` fetchingWasm` , sizeMsg ) ;
90+ progress ( Math . min ( pct , 90 ) , ' fetchingWasm' , sizeMsg ) ;
8691 }
8792
88- // Merge chunks
89- const merged = new Uint8Array ( received ) ;
93+ const wasm = new Uint8Array ( received ) ;
9094 let offset = 0 ;
95+
9196 for ( const chunk of chunks ) {
92- merged . set ( chunk , offset ) ;
97+ wasm . set ( chunk , offset ) ;
9398 offset += chunk . byteLength ;
9499 }
95100
96- return merged . buffer ;
101+ return wasm . buffer ;
97102}
98103
99104// ─────────────────────────────────────────────────────────────────────────────
0 commit comments