From 15c59603f2f9b50531f82dcd36294719c7185ed6 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 10 Sep 2023 19:06:55 -0400 Subject: [PATCH] feat: add service worker support --- src/kernel/queries.ts | 54 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/kernel/queries.ts b/src/kernel/queries.ts index dee9aa6..a724696 100644 --- a/src/kernel/queries.ts +++ b/src/kernel/queries.ts @@ -67,6 +67,19 @@ let nonceCounter: number; let bgConn: any; +let serviceWorker: ServiceWorker; +let kernelIframe: HTMLIFrameElement; + +async function serviceWorkerReady() { + const sw = await navigator.serviceWorker.ready; + navigator.serviceWorker.onmessage = (...args) => handleMessage(...args); + serviceWorker = sw.active as ServiceWorker; +} + +function getServiceWorker() { + return serviceWorker; +} + function initNonce() { nonceSeed = new Uint8Array(16); nonceCounter = 0; @@ -117,6 +130,28 @@ function handleMessage(event: MessageEvent) { return; } + if (event.source === kernelIframe?.contentWindow) { + if ( + ["response", "queryUpdate", "responseNonce", "responseUpdate"].includes( + event.data.method, + ) && + event.data.sw + ) { + delete event.data.sw; + serviceWorker?.postMessage(event.data); + return; + } + } + if (event.source === serviceWorker) { + if (["moduleCall", "queryUpdate", "response"].includes(event.data.method)) { + kernelIframe?.contentWindow?.postMessage( + { ...event.data, sw: true }, + "https://kernel.lumeweb.com", + ); + return; + } + } + if (IS_EXTENSION && !event.data?.data) { event.data.data = Object.assign({}, event.data); } @@ -249,14 +284,14 @@ function handleMessage(event: MessageEvent) { } function launchKernelFrame() { - const iframe = document.createElement("iframe"); - iframe.src = "https://kernel.lumeweb.com"; - iframe.width = "0"; - iframe.height = "0"; - iframe.style.border = "0"; - iframe.style.position = "absolute"; - document.body.appendChild(iframe); - kernelSource = iframe.contentWindow; + kernelIframe = document.createElement("iframe"); + kernelIframe.src = "https://kernel.lumeweb.com"; + kernelIframe.width = "0"; + kernelIframe.height = "0"; + kernelIframe.style.border = "0"; + kernelIframe.style.position = "absolute"; + document.body.appendChild(kernelIframe); + kernelSource = kernelIframe.contentWindow; kernelOrigin = EXTENSION_HOSTED_ORIGIN; kernelAuthLocation = `${EXTENSION_HOSTED_ORIGIN}/auth.html`; sourceDefer.resolve(); @@ -269,7 +304,7 @@ function launchKernelFrame() { } initResolved = true; initDefer.resolve( - "tried to open kernel in iframe, but hit a timeout" as any, + "tried to open kernel in kernelIframe, but hit a timeout" as any, ); }, 24000); } @@ -647,4 +682,5 @@ export { loginDefer, logoutDefer, newKernelQuery, + serviceWorkerReady, };