Skip to content

Commit

Permalink
Add a wait for libinit reinitializing before creating new download se…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
hannyle authored and Hang Le committed Dec 8, 2023
1 parent 826e215 commit 9e81768
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- (GL #1149) Fix headers no getting copied when replicating a container
- (GL #1154) Fix session cookie not getting properly clear on invalidation
- (GL #1160) Fix large uploaded file (> 5 GiB) showing wrong size in the UI
- (GL #1165) Fix download function not working reliably in Firefox

### Removed

Expand Down
67 changes: 41 additions & 26 deletions swift_browser_ui_frontend/wasm/js/crypt-post-downworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Schema for storing the download information:
let downloads = {};
// Text encoder for quickly encoding tar headers
let enc = new TextEncoder();
let libinitDone = false;

/*
This script supports being loaded both as a ServiceWorker and an ordinary
Expand Down Expand Up @@ -61,6 +62,7 @@ function createDownloadSession(container, handle, archive) {
[],
[],
);

let pubkeyPtr = Module.ccall(
"get_keypair_public_key",
"number",
Expand Down Expand Up @@ -257,8 +259,8 @@ function finishDownloadSession(container) {
"free_keypair",
undefined,
["number"],
[downloads[container].keypair]
)
[downloads[container].keypair],
);
delete downloads[container];
}

Expand Down Expand Up @@ -316,7 +318,7 @@ async function beginDownloadInSession(

let path = file.split("/");
let name = path.slice(-1)[0];
let prefix
let prefix;
if (path.length > 1) {
prefix = path.slice(0, -1).join("/");
} else {
Expand Down Expand Up @@ -352,7 +354,7 @@ async function beginDownloadInSession(
// Sync the file if downloading directly into file, otherwise finish
// the fetch request.
if (downloads[container].direct) {
await fileStream.close()
await fileStream.close();
// downloads[container].handle.flush();
// downloads[container].handle.close();
} else {
Expand Down Expand Up @@ -408,7 +410,7 @@ if (inServiceWorker) {
const response = new Response(stream);
response.headers.append(
"Content-Disposition",
'attachment; filename="' + fileName.replace(".c4gh", "") + '"',
"attachment; filename=\"" + fileName.replace(".c4gh", "") + "\"",
);

// Map the streamController as the stream for the download
Expand All @@ -424,24 +426,31 @@ if (inServiceWorker) {
});
}

self.addEventListener("message", (e) => {
self.addEventListener("message", async (e) => {
// Sanity check container name
if (checkPollutingName(e.data.container)) return;

switch(e.data.command) {
case "downloadFile":
if (inServiceWorker) {
createDownloadSession(e.data.container, undefined, false);
e.source.postMessage({
eventType: "getHeaders",
container: e.data.container,
files: [
e.data.file,
],
pubkey: downloads[e.data.container].pubkey,
owner: e.data.owner,
ownerName: e.data.ownerName,
});
while (!libinitDone) {
await timeout(250);
}
if (libinitDone) {
createDownloadSession(e.data.container, undefined, false);
e.source.postMessage({
eventType: "getHeaders",
container: e.data.container,
files: [
e.data.file,
],
pubkey: downloads[e.data.container].pubkey,
owner: e.data.owner,
ownerName: e.data.ownerName,
});

}

} else {
createDownloadSession(e.data.container, e.data.handle, false);
postMessage({
Expand All @@ -458,15 +467,20 @@ self.addEventListener("message", (e) => {
break;
case "downloadFiles":
if (inServiceWorker) {
createDownloadSession(e.data.container, undefined, true);
e.source.postMessage({
eventType: "getHeaders",
container: e.data.container,
files: e.data.files,
pubkey: downloads[e.data.container].pubkey,
owner: e.data.owner,
ownerName: e.data.ownerName,
});
while (!libinitDone) {
await timeout(250);
}
if (libinitDone) {
createDownloadSession(e.data.container, undefined, true);
e.source.postMessage({
eventType: "getHeaders",
container: e.data.container,
files: e.data.files,
pubkey: downloads[e.data.container].pubkey,
owner: e.data.owner,
ownerName: e.data.ownerName,
});
}
} else {
createDownloadSession(e.data.container, e.data.handle, true);
postMessage({
Expand Down Expand Up @@ -502,6 +516,7 @@ self.addEventListener("message", (e) => {

waitAsm().then(() => {
Module.ccall("libinit", undefined, undefined, undefined);
libinitDone = true;
});

export var downloadRuntime = Module;
Expand Down

0 comments on commit 9e81768

Please sign in to comment.