Skip to content

Commit

Permalink
appease the linter
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Apr 11, 2024
1 parent 69817ca commit 296c82c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
76 changes: 40 additions & 36 deletions src/serviceworker/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const serverSupportMap: {
[serverUrl: string]: {
supportsMSC3916: boolean,
cacheExpires: number,
},
supportsMSC3916: boolean;
cacheExpires: number;
};
} = {};

const credentialStore: {
[serverUrl: string]: string,
[serverUrl: string]: string;
} = {};

// We skipWaiting() to update the service worker more frequently, particularly in development environments.
Expand All @@ -16,7 +16,9 @@ skipWaiting();
self.addEventListener("message", (event) => {
if (event.data?.type !== "credentials") return; // ignore
credentialStore[event.data.homeserverUrl] = event.data.accessToken;
console.log(`[Service Worker] Updated access token for ${event.data.homeserverUrl} (accessToken? ${Boolean(event.data.accessToken)})`);
console.log(
`[Service Worker] Updated access token for ${event.data.homeserverUrl} (accessToken? ${Boolean(event.data.accessToken)})`,
);
});

// @ts-expect-error - getting types to work for this is difficult, so we anticipate that "addEventListener" doesn't
Expand All @@ -38,40 +40,42 @@ self.addEventListener("fetch", (event: FetchEvent) => {
if (url.includes("/_matrix/media/v3/download") || url.includes("/_matrix/media/v3/thumbnail")) {
// We need to call respondWith synchronously, otherwise we may never execute properly. This means
// later on we need to proxy the request through if it turns out the server doesn't support authentication.
event.respondWith((async (): Promise<Response> => {
// Figure out which homeserver we're communicating with
const csApi = url.substring(0, url.indexOf("/_matrix/media/v3"));
event.respondWith(
(async (): Promise<Response> => {
// Figure out which homeserver we're communicating with
const csApi = url.substring(0, url.indexOf("/_matrix/media/v3"));

// Locate our access token, and populate the fetchConfig with the authentication header.
const accessToken = credentialStore[csApi];
let fetchConfig: {headers?: {[key: string]: string}} = {};
if (accessToken) {
fetchConfig = {
headers: {
Authorization: `Bearer ${accessToken}`,
},
};
}
// Locate our access token, and populate the fetchConfig with the authentication header.
const accessToken = credentialStore[csApi];
let fetchConfig: { headers?: { [key: string]: string } } = {};
if (accessToken) {
fetchConfig = {
headers: {
Authorization: `Bearer ${accessToken}`,
},
};
}

// Update or populate the server support map using a (usually) authenticated `/versions` call.
if (!serverSupportMap[csApi] || serverSupportMap[csApi].cacheExpires <= (new Date()).getTime()) {
const versions = await (await fetch(`${csApi}/_matrix/client/versions`, fetchConfig)).json();
serverSupportMap[csApi] = {
supportsMSC3916: Boolean(versions?.unstable_features?.["org.matrix.msc3916"]),
cacheExpires: (new Date()).getTime() + (2 * 60 * 60 * 1000), // 2 hours from now
};
}
// Update or populate the server support map using a (usually) authenticated `/versions` call.
if (!serverSupportMap[csApi] || serverSupportMap[csApi].cacheExpires <= new Date().getTime()) {
const versions = await (await fetch(`${csApi}/_matrix/client/versions`, fetchConfig)).json();
serverSupportMap[csApi] = {
supportsMSC3916: Boolean(versions?.unstable_features?.["org.matrix.msc3916"]),
cacheExpires: new Date().getTime() + 2 * 60 * 60 * 1000, // 2 hours from now
};
}

// If we have server support (and a means of authentication), rewrite the URL to use MSC3916 endpoints.
if (serverSupportMap[csApi].supportsMSC3916 && accessToken) {
// Currently unstable only.
url = url.replace(/\/media\/v3\/(.*)\//, "/client/unstable/org.matrix.msc3916/media/$1/");
} // else by default we make no changes
// If we have server support (and a means of authentication), rewrite the URL to use MSC3916 endpoints.
if (serverSupportMap[csApi].supportsMSC3916 && accessToken) {
// Currently unstable only.
url = url.replace(/\/media\/v3\/(.*)\//, "/client/unstable/org.matrix.msc3916/media/$1/");
} // else by default we make no changes

// Add authentication and send the request. We add authentication even if MSC3916 endpoints aren't
// being used to ensure patches like this work:
// https://github.com/matrix-org/synapse/commit/2390b66bf0ec3ff5ffb0c7333f3c9b239eeb92bb
return fetch(url, fetchConfig);
})());
// Add authentication and send the request. We add authentication even if MSC3916 endpoints aren't
// being used to ensure patches like this work:
// https://github.com/matrix-org/synapse/commit/2390b66bf0ec3ff5ffb0c7333f3c9b239eeb92bb
return fetch(url, fetchConfig);
})(),
);
}
});
7 changes: 4 additions & 3 deletions src/vector/platform/WebPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export default class WebPlatform extends VectorBasePlatform {
// Register service worker if available on this platform
if ("serviceWorker" in navigator) {
// sw.js is exported by webpack, sourced from `/src/serviceworker/index.ts`
navigator.serviceWorker.register("sw.js")
.then(r => r.update())
.catch(e => console.error("Error registering/updating service worker:", e));
navigator.serviceWorker
.register("sw.js")

Check warning on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

RETRY 1: WebPlatform › registers service worker

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:40:9)

Check warning on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

RETRY 2: WebPlatform › registers service worker

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:40:9)

Check failure on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

WebPlatform › registers service worker

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:40:9)

Check warning on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

RETRY 1: WebPlatform › should call reload on window location object

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:47:26)

Check warning on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

RETRY 2: WebPlatform › should call reload on window location object

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:47:26)

Check failure on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

WebPlatform › should call reload on window location object

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:47:26)

Check warning on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

RETRY 1: WebPlatform › should call reload to install update

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:56:26)

Check warning on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

RETRY 2: WebPlatform › should call reload to install update

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:56:26)

Check failure on line 51 in src/vector/platform/WebPlatform.ts

View workflow job for this annotation

GitHub Actions / Jest

WebPlatform › should call reload to install update

TypeError: Cannot read properties of undefined (reading 'then') at new WebPlatform (src/vector/platform/WebPlatform.ts:51:35) at Object.<anonymous> (test/unit-tests/vector/platform/WebPlatform-test.ts:56:26)
.then((r) => r.update())
.catch((e) => console.error("Error registering/updating service worker:", e));
}
}

Expand Down

0 comments on commit 296c82c

Please sign in to comment.