@@ -150,7 +150,7 @@ function decryptChunk(container, path, enChunk) {
150
150
let ret = new Uint8Array ( HEAPU8 . subarray ( chunkPtr , chunkPtr + chunkLen ) ) ;
151
151
totalDone += chunkLen ;
152
152
Module . ccall (
153
- "free_chunk " ,
153
+ "free_chunk_nobuf " ,
154
154
"number" ,
155
155
[ "number" ] ,
156
156
[ chunk ] ,
@@ -288,6 +288,9 @@ class FileSlicer {
288
288
} else {
289
289
// Otherwise queue to the streamController since we're using a
290
290
// ServiceWorker for downloading
291
+ while ( this . output . desiredSize <= 0 ) {
292
+ await timeout ( 10 ) ;
293
+ }
291
294
this . output . enqueue ( decryptChunk (
292
295
this . container ,
293
296
this . path ,
@@ -307,7 +310,6 @@ class FileSlicer {
307
310
[ "number" ] ,
308
311
[ downloads [ this . container ] . files [ this . path ] . key ] ,
309
312
) ;
310
-
311
313
return true ;
312
314
}
313
315
}
@@ -374,12 +376,13 @@ async function beginDownloadInSession(
374
376
container ,
375
377
) {
376
378
aborted = false ; //reset with download start
377
- let fileStream = undefined ;
378
379
380
+ let fileHandle = downloads [ container ] . handle ;
381
+ let fileStream ;
379
382
if ( downloads [ container ] . direct ) {
380
- fileStream = await downloads [ container ] . handle . createWritable ( ) ;
383
+ fileStream = await fileHandle . createWritable ( ) ;
381
384
} else {
382
- fileStream = downloads [ container ] . handle ;
385
+ fileStream = fileHandle ;
383
386
}
384
387
385
388
// Add the archive folder structure
@@ -390,20 +393,20 @@ async function beginDownloadInSession(
390
393
. filter ( path => path . length > 0 ) // remove empty paths (root level files)
391
394
. sort ( ( a , b ) => a . length - b . length ) // sort by path length as levels
392
395
. reduce ( ( unique , path ) => { // strip paths down to just the unique ones
393
- let check = unique . find ( item => item . join ( "/" ) === path . join ( "/" ) ) ;
396
+ let check = unique . find ( item => item === path . join ( "/" ) ) ;
394
397
if ( check === undefined ) {
395
- unique . push ( path ) ;
398
+ unique . push ( path . join ( "/" ) ) ;
396
399
}
397
400
return unique ;
398
401
} , [ ] ) ;
399
402
400
403
for ( const path of folderPaths ) {
401
404
if ( downloads [ container ] . direct ) {
402
405
await fileStream . write (
403
- enc . encode ( addTarFolder ( path . slice ( - 1 ) [ 0 ] , path . slice ( 0 , - 1 ) . join ( "/" ) ) ) ,
406
+ addTarFolder ( path ) ,
404
407
) ;
405
408
} else {
406
- fileStream . enqueue ( enc . encode ( addTarFolder ( path . slice ( - 1 ) [ 0 ] , path . slice ( 0 , - 1 ) . join ( "/" ) ) ) ) ;
409
+ fileStream . enqueue ( addTarFolder ( path ) ) ;
407
410
}
408
411
}
409
412
}
@@ -431,23 +434,15 @@ async function beginDownloadInSession(
431
434
}
432
435
433
436
const response = await fetch ( downloads [ container ] . files [ file ] . url ) ;
437
+ let path = file . replace ( ".c4gh" , "" ) ;
434
438
435
439
if ( downloads [ container ] . archive ) {
436
440
const size = getFileSize ( response , downloads [ container ] . files [ file ] . key ) ;
437
- let path = file . split ( "/" ) ;
438
- let name = path . slice ( - 1 ) [ 0 ] ;
439
- let prefix ;
440
- if ( path . length > 1 ) {
441
- prefix = path . slice ( 0 , - 1 ) . join ( "/" ) ;
442
- } else {
443
- prefix = "" ;
444
- }
445
441
446
- let fileHeader = enc . encode ( addTarFile (
447
- downloads [ container ] . files [ file ] . key != 0 ? name . replace ( ".c4gh" , "" ) : name ,
448
- prefix ,
442
+ let fileHeader = addTarFile (
443
+ downloads [ container ] . files [ file ] . key != 0 ? path : file ,
449
444
size ,
450
- ) ) ;
445
+ ) ;
451
446
452
447
if ( downloads [ container ] . direct ) {
453
448
await fileStream . write ( fileHeader ) ;
@@ -476,47 +471,47 @@ async function beginDownloadInSession(
476
471
return ;
477
472
}
478
473
}
474
+ }
479
475
480
- if ( downloads [ container ] . archive ) {
481
- // Write the end of the archive
482
- if ( downloads [ container ] . direct ) {
483
- await fileStream . write ( enc . encode ( "\x00" . repeat ( 1024 ) ) ) ;
484
- } else {
485
- fileStream . enqueue ( enc . encode ( "\x00" . repeat ( 1024 ) ) ) ;
486
- }
487
- }
488
-
489
- // Sync the file if downloading directly into file, otherwise finish
490
- // the fetch request.
476
+ if ( downloads [ container ] . archive ) {
477
+ // Write the end of the archive
491
478
if ( downloads [ container ] . direct ) {
492
- await fileStream . close ( ) ;
493
- // downloads[container].handle.flush();
494
- // downloads[container].handle.close();
479
+ await fileStream . write ( enc . encode ( "\x00" . repeat ( 1024 ) ) ) ;
495
480
} else {
496
- fileStream . close ( ) ;
481
+ fileStream . enqueue ( enc . encode ( "\x00" . repeat ( 1024 ) ) ) ;
497
482
}
483
+ }
498
484
499
- if ( downloads [ container ] . direct ) {
500
- // Direct downloads need no further action, the resulting archive is
501
- // already in the filesystem.
502
- postMessage ( {
503
- eventType : "finished" ,
504
- direct : true ,
505
- container : container ,
506
- } ) ;
507
- } else {
508
- // Inform download with service worker finished
509
- self . clients . matchAll ( ) . then ( clients => {
510
- clients . forEach ( client =>
511
- client . postMessage ( {
512
- eventType : "downloadProgressFinished" ,
513
- container : container ,
514
- } ) ) ;
515
- } ) ;
516
- }
517
- finishDownloadSession ( container ) ;
518
- return ;
485
+ // Sync the file if downloading directly into file, otherwise finish
486
+ // the fetch request.
487
+ if ( downloads [ container ] . direct ) {
488
+ await fileStream . close ( ) ;
489
+ // downloads[container].handle.flush();
490
+ // downloads[container].handle.close();
491
+ } else {
492
+ fileStream . close ( ) ;
493
+ }
494
+
495
+ if ( downloads [ container ] . direct ) {
496
+ // Direct downloads need no further action, the resulting archive is
497
+ // already in the filesystem.
498
+ postMessage ( {
499
+ eventType : "finished" ,
500
+ direct : true ,
501
+ container : container ,
502
+ } ) ;
503
+ } else {
504
+ // Inform download with service worker finished
505
+ self . clients . matchAll ( ) . then ( clients => {
506
+ clients . forEach ( client =>
507
+ client . postMessage ( {
508
+ eventType : "downloadProgressFinished" ,
509
+ container : container ,
510
+ } ) ) ;
511
+ } ) ;
519
512
}
513
+ finishDownloadSession ( container ) ;
514
+ return ;
520
515
}
521
516
522
517
if ( inServiceWorker ) {
0 commit comments