Skip to content

Commit

Permalink
fix: check controller existence before postMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
penpenpng committed Jul 13, 2024
1 parent 98a6e04 commit 1052234
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
51 changes: 30 additions & 21 deletions packages/crypto/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.",
Expand All @@ -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.");
}

Expand All @@ -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<boolean>((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<boolean>((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 = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"lib": ["ESNext", "DOM", "WebWorker"],
"moduleResolution": "bundler",
"strict": true,
"resolveJsonModule": true,
Expand Down

0 comments on commit 1052234

Please sign in to comment.