diff --git a/packages/crypto/src/service-worker.ts b/packages/crypto/src/service-worker.ts index 50dc6a8..afaf98f 100644 --- a/packages/crypto/src/service-worker.ts +++ b/packages/crypto/src/service-worker.ts @@ -3,9 +3,6 @@ import * as Nostr from "nostr-typedef"; import { Batch } from "./uitls/batch.js"; import { type EventVerifier, verifier as defaultVerifier } from "./verifier.js"; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -declare const WorkerGlobalScope: any; - interface VerificationRequest { reqId: number; event: Nostr.Event; @@ -20,8 +17,8 @@ export const startVerificationServiceHost = ( verifier: EventVerifier = defaultVerifier, ) => { if ( - typeof WorkerGlobalScope === "undefined" || - !(self instanceof WorkerGlobalScope) + typeof ServiceWorkerGlobalScope === "undefined" || + !(self instanceof ServiceWorkerGlobalScope) ) { throw new Error( "startVerificationServiceHost() must be called in a Service Worker context.", @@ -39,10 +36,15 @@ export const startVerificationServiceHost = ( } satisfies VerificationResponse); }, ); + + self.addEventListener("activate", (ev) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (ev as any).waitUntil((self as any).clients.claim()); + }); }; export const createVerificationServiceClient = () => { - if (!navigator?.serviceWorker?.controller) { + if (!navigator?.serviceWorker) { throw new Error("This runtime doesn't support Service Worker."); } @@ -62,27 +64,34 @@ export const createVerificationServiceClient = () => { const verify: EventVerifier = (event) => { if (disposed) { - throw new Error(""); + throw new Error("VerificationServiceClient is already disposed."); } - const reqId = nextReqId++; + const controller = navigator.serviceWorker.controller; + + if (controller) { + const reqId = nextReqId++; - const r = new Promise((resolve, reject) => { - resolvers.set(reqId, resolve); - batch.set(() => { - if (resolvers.get(reqId)) { - reject(new Error("Verification request was timed out.")); - resolvers.delete(reqId); - } + const r = new Promise((resolve, reject) => { + resolvers.set(reqId, resolve); + batch.set(() => { + if (resolvers.get(reqId)) { + reject(new Error("Verification request was timed out.")); + resolvers.delete(reqId); + } + }); }); - }); - navigator.serviceWorker.controller?.postMessage({ - reqId, - event, - } satisfies VerificationRequest); + controller.postMessage({ + reqId, + event, + } satisfies VerificationRequest); - return r; + return r; + } else { + // fallback + return defaultVerifier(event); + } }; const dispose = () => { diff --git a/packages/crypto/tsconfig.json b/packages/crypto/tsconfig.json index 19d60a4..7176678 100644 --- a/packages/crypto/tsconfig.json +++ b/packages/crypto/tsconfig.json @@ -3,7 +3,7 @@ "target": "ESNext", "useDefineForClassFields": true, "module": "ESNext", - "lib": ["ESNext", "DOM"], + "lib": ["ESNext", "DOM", "WebWorker"], "moduleResolution": "bundler", "strict": true, "resolveJsonModule": true,