Merge pull request #323 from PR0M3TH3AN/codex/fix-loading-errors-on-the-site-k8wabg

Improve nostr-tools SimplePool detection
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2025-10-06 11:30:13 -04:00
commit 4926cb5242
1 file changed
+79 -8
+79 -8
View File
@@ -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,15 +4362,16 @@ class NostrClient {
}
const tools = await ensureNostrTools();
const SimplePool =
typeof tools?.SimplePool === "function"
? tools.SimplePool
: typeof tools?.pool?.SimplePool === "function"
? tools.pool.SimplePool
: typeof tools?.SimplePool?.SimplePool === "function"
? tools.SimplePool.SimplePool
: null;
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;