From 82f779099a0159ae85186f8ea3aab1e76e40c3b9 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Mon, 6 Oct 2025 11:28:05 -0400 Subject: [PATCH] Improve nostr-tools SimplePool detection --- js/nostr.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/js/nostr.js b/js/nostr.js index 4ef33572..502ea32a 100644 --- a/js/nostr.js +++ b/js/nostr.js @@ -217,6 +217,76 @@ async function ensureNostrTools() { return cachedNostrTools || null; } +function isSimplePoolConstructor(candidate) { + if (typeof candidate !== "function") { + return false; + } + + const prototype = candidate.prototype; + if (!prototype || typeof prototype !== "object") { + return false; + } + + return typeof prototype.sub === "function" && typeof prototype.close === "function"; +} + +function unwrapSimplePool(candidate) { + if (!candidate) { + return null; + } + + if (isSimplePoolConstructor(candidate)) { + return candidate; + } + + if (typeof candidate === "object") { + if (isSimplePoolConstructor(candidate.SimplePool)) { + return candidate.SimplePool; + } + if (isSimplePoolConstructor(candidate.default)) { + return candidate.default; + } + } + + return null; +} + +function resolveSimplePoolConstructor(tools, scope = globalScope) { + const candidates = [ + tools?.SimplePool, + tools?.pool?.SimplePool, + tools?.pool, + tools?.SimplePool?.SimplePool, + tools?.SimplePool?.default, + tools?.pool?.default, + tools?.default?.SimplePool, + tools?.default?.pool?.SimplePool, + tools?.default?.pool, + ]; + + if (scope && typeof scope === "object") { + candidates.push(scope?.SimplePool); + candidates.push(scope?.pool?.SimplePool); + candidates.push(scope?.pool); + const scopedTools = + scope?.NostrTools && scope.NostrTools !== tools ? scope.NostrTools : null; + if (scopedTools) { + candidates.push(scopedTools.SimplePool); + candidates.push(scopedTools.pool?.SimplePool); + candidates.push(scopedTools.pool); + } + } + + for (const candidate of candidates) { + const resolved = unwrapSimplePool(candidate); + if (resolved) { + return resolved; + } + } + + return null; +} + let ingestLocalViewEventRef = null; async function loadIngestLocalViewEvent() { @@ -4292,8 +4362,15 @@ class NostrClient { } const tools = await ensureNostrTools(); - const SimplePool = tools?.SimplePool; + const SimplePool = resolveSimplePoolConstructor(tools); if (typeof SimplePool !== "function") { + if (isDevMode && tools && typeof tools === "object") { + const availableKeys = Object.keys(tools).join(", "); + console.warn( + "[nostr] NostrTools helpers did not expose SimplePool. Available keys:", + availableKeys + ); + } const error = new Error("NostrTools SimplePool is unavailable."); error.code = "nostr-simplepool-unavailable"; this.poolPromise = null;