@@ -20,6 +20,22 @@ export function toLocale(n: number) {
20
20
function r ( s : number ) {
21
21
return toLocale ( Number . parseFloat ( s . toPrecision ( 3 ) ) )
22
22
}
23
+ function sum ( array : Uint8Array [ ] ) {
24
+ let sum = 0
25
+ for ( const entry of array ) {
26
+ sum += entry . length
27
+ }
28
+ return sum
29
+ }
30
+ function concatUint8Array ( args : Uint8Array [ ] ) {
31
+ const mergedArray = new Uint8Array ( sum ( args ) )
32
+ let offset = 0
33
+ for ( const entry of args ) {
34
+ mergedArray . set ( entry , offset )
35
+ offset += entry . length
36
+ }
37
+ return mergedArray
38
+ }
23
39
export function getProgressDisplayStr ( current : number , total : number ) {
24
40
if ( Math . floor ( total / 1_000_000 ) > 0 ) {
25
41
return `${ r ( current / 1_000_000 ) } /${ r ( total / 1_000_000 ) } Mb`
@@ -222,7 +238,7 @@ export default class RemoteFile implements GenericFilehandle {
222
238
const contentLength = res . headers . get ( 'content-length' )
223
239
const totalBytes = contentLength ? parseInt ( contentLength , 10 ) : undefined
224
240
225
- if ( statusCallback && res . body ) {
241
+ if ( statusCallback && res . body && totalBytes ) {
226
242
const reader = res . body . getReader ( )
227
243
const chunks : Uint8Array [ ] = [ ]
228
244
let receivedBytes = 0
@@ -238,28 +254,18 @@ export default class RemoteFile implements GenericFilehandle {
238
254
chunks . push ( value )
239
255
receivedBytes += value . length
240
256
241
- if ( statusCallback && totalBytes ) {
242
- statusCallback (
243
- `Downloading ${ getProgressDisplayStr ( receivedBytes , totalBytes ) } ` ,
244
- )
245
- }
246
- }
247
-
248
- // Concatenate chunks
249
- const chunksAll = new Uint8Array ( receivedBytes )
250
- let position = 0
251
- for ( const chunk of chunks ) {
252
- chunksAll . set ( chunk , position )
253
- position += chunk . length
257
+ statusCallback (
258
+ `Downloading ${ getProgressDisplayStr ( receivedBytes , totalBytes ) } ` ,
259
+ )
254
260
}
255
261
256
262
if ( encoding === 'utf8' ) {
257
263
const decoder = new TextDecoder ( 'utf-8' )
258
- return decoder . decode ( chunksAll )
264
+ return decoder . decode ( concatUint8Array ( chunks ) )
259
265
} else if ( encoding ) {
260
266
throw new Error ( `unsupported encoding: ${ encoding } ` )
261
267
} else {
262
- return chunksAll
268
+ return concatUint8Array ( chunks )
263
269
}
264
270
} else {
265
271
// If no statusCallback, use the simpler approach
0 commit comments