Skip to content

Commit

Permalink
drive: fix "Service-Worker-Allowed" header setting, closes #638
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Oct 4, 2023
1 parent 283146d commit bfae0c6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
58 changes: 58 additions & 0 deletions patches/protonmail/proton-drive.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
diff --git a/applications/drive/src/.htaccess b/applications/drive/src/.htaccess
index 5b522f3deb..5523b5a5dc 100644
--- a/applications/drive/src/.htaccess
+++ b/applications/drive/src/.htaccess
@@ -39,11 +39,13 @@ ErrorDocument 404 /assets/404.html
Header set X-Robots-Tag "noindex"
</Files>

+# <electron-mail-mark>
# The download service worker gets chunk hash appended to the end of the filename
<Files ~ "^downloadSW">
Header set Service-Worker-Allowed "/"
Header set Service-Worker "script"
</Files>
+# </electron-mail-mark>

<IfModule mod_mime.c>
AddType application/font-woff2 .woff2

diff --git a/applications/drive/src/app/store/_downloads/fileSaver/download.ts b/applications/drive/src/app/store/_downloads/fileSaver/download.ts
index 66a9aba1cc..2a7ab63889 100644
--- a/applications/drive/src/app/store/_downloads/fileSaver/download.ts
+++ b/applications/drive/src/app/store/_downloads/fileSaver/download.ts
@@ -56,19 +56,26 @@ function serviceWorkerKeepAlive() {
}, 10000);
}

+/* <electron-mail-mark> */
export async function initDownloadSW() {
if (isUnsupported()) {
throw new Error('Saving file via download is unsupported by this browser');
}
- await navigator.serviceWorker.register(
- /* webpackChunkName: "downloadSW" */
- new URL('./downloadSW', import.meta.url),
- {
- scope: `/${stripLeadingAndTrailingSlash(PUBLIC_PATH)}`,
- }
- );
+ try {
+ await navigator.serviceWorker.register(
+ /* webpackChunkName: "downloadSW" */
+ new URL('./downloadSW', import.meta.url),
+ {
+ scope: `/${stripLeadingAndTrailingSlash(PUBLIC_PATH)}`,
+ }
+ );
+ } catch (e) {
+ console.error(`Failed to register service worker.`, e.message);
+ throw e;
+ }
serviceWorkerKeepAlive();
}
+/* </electron-mail-mark> */

/**
* Opens download stream into service worker. Use abort signal when pipeTo can't close the download stream.

diff --git a/applications/drive/package.json b/applications/drive/package.json
index 1ff78a9928..d232854a68 100644
--- a/applications/drive/package.json
Expand Down
18 changes: 18 additions & 0 deletions scripts/prepare-webclient/webclients.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fastGlob from "fast-glob";
import fs from "fs";
import fsExtra from "fs-extra";
import path from "path";
Expand Down Expand Up @@ -328,6 +329,23 @@ async function executeBuildFlow(
],
publicPath ? {printEnvWhitelist: ["PUBLIC_PATH"]} : undefined,
);

if (repoType === "proton-drive") {
// WARN if path changes, search "Service-Worker-Allowed" keyword in "src/electron-main" and make needed adjustments
const downloadSW = path.join(repoDistDir, "assets", "downloadSW.*.chunk.js");
if (
fastGlob.sync(
downloadSW,
{
deep: 1,
onlyFiles: true,
stats: false,
},
).length !== 1
) {
throw new Error(`Failed to resolve "${downloadSW}" assets file`);
}
}
}

writeFile(
Expand Down
2 changes: 1 addition & 1 deletion src/electron-main/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export async function registerAccountSessionProtocols(
// TODO tweak e2e test: navigate to "/drive" (requires to be signed-in into the mail account)
// so the scope misconfiguration-related error get printed to "log.log" file and the test gets failed then
if (resourceLocation.startsWith(
path.join(directory, PROVIDER_REPO_MAP["proton-drive"].basePath, "downloadSW."),
path.join(directory, PROVIDER_REPO_MAP["proton-drive"].basePath, "assets", "downloadSW."),
)) {
/* eslint-disable max-len */
// https://github.com/ProtonMail/proton-drive/blob/04d30ae6c9fbfbc33cfc91499831e2e6458a99b1/src/.htaccess#L42-L45
Expand Down

0 comments on commit bfae0c6

Please sign in to comment.