Typescriptify & use service worker for MSC3916 authentication (#27326)
* Typescriptify & use service worker for MSC3916 authentication * appease the linter * appease jest * appease linter * Get the access token directly * Add a bit of jitter * Improve legibility, use factored-out functions for pickling * Add docs * Appease the linter * Document risks of postMessage * Split service worker post message handling out to function * Move registration to async function * Use more early returns * Thanks(?), WebStorm * Handle case of no access token for /versions * Appease linter * Apply suggestions from code review Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * Remove spurious try/catch * Factor out fetch config stuff * Apply suggestions from code review Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * Finish applying code review suggestions --------- Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
This commit is contained in:
parent
482b81b0ed
commit
bcd5c838e8
4 changed files with 227 additions and 7 deletions
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
Copyright 2016 Aviral Dasgupta
|
||||
Copyright 2016 OpenMarket Ltd
|
||||
Copyright 2017-2020 New Vector Ltd
|
||||
Copyright 2017-2020, 2024 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
@ -44,9 +44,41 @@ export default class WebPlatform extends VectorBasePlatform {
|
|||
|
||||
public constructor() {
|
||||
super();
|
||||
// Register service worker if available on this platform
|
||||
if ("serviceWorker" in navigator) {
|
||||
navigator.serviceWorker.register("sw.js");
|
||||
|
||||
// Register the service worker in the background
|
||||
this.tryRegisterServiceWorker().catch((e) => console.error("Error registering/updating service worker:", e));
|
||||
}
|
||||
|
||||
private async tryRegisterServiceWorker(): Promise<void> {
|
||||
if (!("serviceWorker" in navigator)) {
|
||||
return; // not available on this platform - don't try to register the service worker
|
||||
}
|
||||
|
||||
// sw.js is exported by webpack, sourced from `/src/serviceworker/index.ts`
|
||||
const registration = await navigator.serviceWorker.register("sw.js");
|
||||
if (!registration) {
|
||||
// Registration didn't work for some reason - assume failed and ignore.
|
||||
// This typically happens in Jest.
|
||||
return;
|
||||
}
|
||||
|
||||
await registration.update();
|
||||
navigator.serviceWorker.addEventListener("message", this.onServiceWorkerPostMessage.bind(this));
|
||||
}
|
||||
|
||||
private onServiceWorkerPostMessage(event: MessageEvent): void {
|
||||
try {
|
||||
if (event.data?.["type"] === "userinfo" && event.data?.["responseKey"]) {
|
||||
const userId = localStorage.getItem("mx_user_id");
|
||||
const deviceId = localStorage.getItem("mx_device_id");
|
||||
event.source!.postMessage({
|
||||
responseKey: event.data["responseKey"],
|
||||
userId,
|
||||
deviceId,
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error responding to service worker: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue