mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-29 04:52:59 +02:00
add timeout when loading nostr-wasm
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { NostrEvent, VerifiedEvent, verifiedSymbol, verifyEvent } from "nostr-tools";
|
import { NostrEvent, VerifiedEvent, verifiedSymbol, verifyEvent } from "nostr-tools";
|
||||||
import { logger } from "../../helpers/debug";
|
import { logger } from "../../helpers/debug";
|
||||||
|
import { setNostrWasm, verifyEvent as wasmVerifyEvent } from "nostr-tools/wasm";
|
||||||
|
|
||||||
const localStorageKey = "verify-event-method";
|
const localStorageKey = "verify-event-method";
|
||||||
|
|
||||||
@@ -12,6 +13,26 @@ export function fakeVerifyEvent(event: NostrEvent): event is VerifiedEvent {
|
|||||||
return (event[verifiedSymbol] = true);
|
return (event[verifiedSymbol] = true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadWithTimeout() {
|
||||||
|
return new Promise<typeof verifyEvent>((res, rej) => {
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
log("Timeout");
|
||||||
|
rej(new Error("Timeout"));
|
||||||
|
}, 5_000);
|
||||||
|
|
||||||
|
return import("nostr-wasm").then(({ initNostrWasm }) => {
|
||||||
|
log("Initializing WebAssembly");
|
||||||
|
|
||||||
|
return initNostrWasm().then((nw) => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
setNostrWasm(nw);
|
||||||
|
res(wasmVerifyEvent);
|
||||||
|
return wasmVerifyEvent;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
selectedMethod = localStorage.getItem(localStorageKey) ?? "default";
|
selectedMethod = localStorage.getItem(localStorageKey) ?? "default";
|
||||||
|
|
||||||
@@ -19,7 +40,7 @@ try {
|
|||||||
case "wasm":
|
case "wasm":
|
||||||
if (!("WebAssembly" in window)) throw new Error("WebAssembly not supported");
|
if (!("WebAssembly" in window)) throw new Error("WebAssembly not supported");
|
||||||
log("Loading WebAssembly module");
|
log("Loading WebAssembly module");
|
||||||
verifyEventMethod = alwaysVerify = (await import("./wasm")).default;
|
verifyEventMethod = alwaysVerify = await loadWithTimeout();
|
||||||
log("Loaded");
|
log("Loaded");
|
||||||
break;
|
break;
|
||||||
case "none":
|
case "none":
|
||||||
@@ -37,6 +58,7 @@ try {
|
|||||||
console.error("Failed to initialize event verification method, falling back to default");
|
console.error("Failed to initialize event verification method, falling back to default");
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
|
||||||
|
localStorage.setItem(localStorageKey, "default");
|
||||||
verifyEventMethod = alwaysVerify = verifyEvent;
|
verifyEventMethod = alwaysVerify = verifyEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,7 +0,0 @@
|
|||||||
import { setNostrWasm, verifyEvent } from "nostr-tools/wasm";
|
|
||||||
import { initNostrWasm } from "nostr-wasm";
|
|
||||||
|
|
||||||
const wasm = await initNostrWasm();
|
|
||||||
setNostrWasm(wasm);
|
|
||||||
|
|
||||||
export default verifyEvent;
|
|
Reference in New Issue
Block a user