From 5f62fcde6a260a3b260b54185110ce4849ecbdad Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Mon, 19 Feb 2024 12:02:15 +0100 Subject: [PATCH] Use private fields with `#` --- .../Core/Notification/ServiceWorker.ts | 30 +++++++++---------- .../Core/Notification/ServiceWorker.js | 28 ++++++++--------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts b/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts index e3bc116d3c..ab1ff32f87 100644 --- a/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts +++ b/ts/WoltLabSuite/Core/Notification/ServiceWorker.ts @@ -11,32 +11,30 @@ import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend"; let _serviceWorker: ServiceWorker | null = null; class ServiceWorker { - private readonly publicKey: string; - private readonly serviceWorkerJsUrl: string; - private readonly registerUrl: string; - private readonly serviceWorkerRegistration: Promise; + readonly #publicKey: string; + readonly #serviceWorkerJsUrl: string; + readonly #registerUrl: string; + readonly #serviceWorkerRegistration: Promise; constructor(publicKey: string, serviceWorkerJsUrl: string, registerUrl: string) { - this.publicKey = publicKey; - this.serviceWorkerJsUrl = serviceWorkerJsUrl; - this.registerUrl = registerUrl; - void window.navigator.serviceWorker.register(this.serviceWorkerJsUrl, { + this.#publicKey = publicKey; + this.#serviceWorkerJsUrl = serviceWorkerJsUrl; + this.#registerUrl = registerUrl; + void window.navigator.serviceWorker.register(this.#serviceWorkerJsUrl, { scope: "/", }); - this.serviceWorkerRegistration = window.navigator.serviceWorker.ready; + this.#serviceWorkerRegistration = window.navigator.serviceWorker.ready; } async register(): Promise { - const currentSubscription = await (await this.serviceWorkerRegistration).pushManager.getSubscription(); + const currentSubscription = await(await this.#serviceWorkerRegistration).pushManager.getSubscription(); if (currentSubscription && this.#compareApplicationServerKey(currentSubscription)) { return; } await this.unsubscribe(currentSubscription); - const subscription = await ( - await this.serviceWorkerRegistration - ).pushManager.subscribe({ + const subscription = await(await this.#serviceWorkerRegistration).pushManager.subscribe({ userVisibleOnly: true, - applicationServerKey: this.#urlBase64ToUint8Array(this.publicKey), + applicationServerKey: this.#urlBase64ToUint8Array(this.#publicKey), }); if (!subscription) { // subscription failed @@ -57,7 +55,7 @@ class ServiceWorker { base64 = base64.replace(/\+/g, "-").replace(/\//g, "_"); base64 = base64.replace(/=+$/, ""); - return base64 === this.publicKey; + return base64 === this.#publicKey; } async #sendRequest(subscription: PushSubscription, remove: boolean = false): Promise { @@ -67,7 +65,7 @@ class ServiceWorker { // @see https://w3c.github.io/push-api/#dom-pushmanager-supportedcontentencodings const contentEncoding = (PushManager.supportedContentEncodings || ["aes128gcm"])[0]; try { - await prepareRequest(this.registerUrl) + await prepareRequest(this.#registerUrl) .post({ remove: remove, endpoint: subscription.endpoint, diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js index dc8c905250..2b4864d5c9 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Notification/ServiceWorker.js @@ -11,28 +11,28 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi exports.registerServiceWorker = exports.setup = void 0; let _serviceWorker = null; class ServiceWorker { - publicKey; - serviceWorkerJsUrl; - registerUrl; - serviceWorkerRegistration; + #publicKey; + #serviceWorkerJsUrl; + #registerUrl; + #serviceWorkerRegistration; constructor(publicKey, serviceWorkerJsUrl, registerUrl) { - this.publicKey = publicKey; - this.serviceWorkerJsUrl = serviceWorkerJsUrl; - this.registerUrl = registerUrl; - void window.navigator.serviceWorker.register(this.serviceWorkerJsUrl, { + this.#publicKey = publicKey; + this.#serviceWorkerJsUrl = serviceWorkerJsUrl; + this.#registerUrl = registerUrl; + void window.navigator.serviceWorker.register(this.#serviceWorkerJsUrl, { scope: "/", }); - this.serviceWorkerRegistration = window.navigator.serviceWorker.ready; + this.#serviceWorkerRegistration = window.navigator.serviceWorker.ready; } async register() { - const currentSubscription = await (await this.serviceWorkerRegistration).pushManager.getSubscription(); + const currentSubscription = await (await this.#serviceWorkerRegistration).pushManager.getSubscription(); if (currentSubscription && this.#compareApplicationServerKey(currentSubscription)) { return; } await this.unsubscribe(currentSubscription); - const subscription = await (await this.serviceWorkerRegistration).pushManager.subscribe({ + const subscription = await (await this.#serviceWorkerRegistration).pushManager.subscribe({ userVisibleOnly: true, - applicationServerKey: this.#urlBase64ToUint8Array(this.publicKey), + applicationServerKey: this.#urlBase64ToUint8Array(this.#publicKey), }); if (!subscription) { // subscription failed @@ -50,7 +50,7 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi let base64 = window.btoa(String.fromCharCode(...new Uint8Array(subscription.options.applicationServerKey))); base64 = base64.replace(/\+/g, "-").replace(/\//g, "_"); base64 = base64.replace(/=+$/, ""); - return base64 === this.publicKey; + return base64 === this.#publicKey; } async #sendRequest(subscription, remove = false) { const key = subscription.getKey("p256dh"); @@ -59,7 +59,7 @@ define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend"], function (requi // @see https://w3c.github.io/push-api/#dom-pushmanager-supportedcontentencodings const contentEncoding = (PushManager.supportedContentEncodings || ["aes128gcm"])[0]; try { - await (0, Backend_1.prepareRequest)(this.registerUrl) + await (0, Backend_1.prepareRequest)(this.#registerUrl) .post({ remove: remove, endpoint: subscription.endpoint, -- 2.20.1