From ccb9641fb94ed7fbe5424f06b3916da48ed1da99 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 31 Jan 2026 00:27:54 -0300 Subject: [PATCH] pool: maxWaitForConnection parameter. this was so obvious. --- abstract-pool.ts | 28 +++++++++++++++++++++++----- jsr.json | 2 +- package.json | 2 +- pool.ts | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/abstract-pool.ts b/abstract-pool.ts index 4624097..0e7f2a2 100644 --- a/abstract-pool.ts +++ b/abstract-pool.ts @@ -25,6 +25,9 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & { // allowConnectingToRelay takes a relay URL and the operation being performed // return false to skip connecting to that relay allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean + // maxWaitForConnection takes a number in milliseconds that will be given to ensureRelay such that we + // don't get stuck forever when attempting to connect to a relay, it is 3000 (3 seconds) by default + maxWaitForConnection: number } export type SubscribeManyParams = Omit & { @@ -48,6 +51,7 @@ export class AbstractSimplePool { public trustedRelayURLs: Set = new Set() public onRelayConnectionFailure?: (url: string) => void public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean + public maxWaitForConnection: number private _WebSocket?: typeof WebSocket @@ -59,6 +63,7 @@ export class AbstractSimplePool { this.automaticallyAuth = opts.automaticallyAuth this.onRelayConnectionFailure = opts.onRelayConnectionFailure this.allowConnectingToRelay = opts.allowConnectingToRelay + this.maxWaitForConnection = opts.maxWaitForConnection || 3000 } async ensureRelay( @@ -199,7 +204,10 @@ export class AbstractSimplePool { let relay: AbstractRelay try { relay = await this.ensureRelay(url, { - connectionTimeout: params.maxWait ? Math.max(params.maxWait * 0.8, params.maxWait - 1000) : undefined, + connectionTimeout: + this.maxWaitForConnection < (params.maxWait || 0) + ? Math.max(params.maxWait! * 0.8, params.maxWait! - 1000) + : this.maxWaitForConnection, abort: params.abort, }) } catch (err) { @@ -314,7 +322,11 @@ export class AbstractSimplePool { publish( relays: string[], event: Event, - options?: { onauth?: (evt: EventTemplate) => Promise }, + params?: { + onauth?: (evt: EventTemplate) => Promise + maxWait?: number + abort?: AbortSignal + }, ): Promise[] { return relays.map(normalizeURL).map(async (url, i, arr) => { if (arr.indexOf(url) !== i) { @@ -328,7 +340,13 @@ export class AbstractSimplePool { let r: Relay try { - r = await this.ensureRelay(url) + r = await this.ensureRelay(url, { + connectionTimeout: + this.maxWaitForConnection < (params?.maxWait || 0) + ? Math.max(params!.maxWait! * 0.8, params!.maxWait! - 1000) + : this.maxWaitForConnection, + abort: params?.abort, + }) } catch (err) { this.onRelayConnectionFailure?.(url) return String('connection failure: ' + String(err)) @@ -337,8 +355,8 @@ export class AbstractSimplePool { return r .publish(event) .catch(async err => { - if (err instanceof Error && err.message.startsWith('auth-required: ') && options?.onauth) { - await r.auth(options.onauth) + if (err instanceof Error && err.message.startsWith('auth-required: ') && params?.onauth) { + await r.auth(params.onauth) return r.publish(event) // retry } throw err diff --git a/jsr.json b/jsr.json index 4808d42..be2d1d7 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@nostr/tools", - "version": "2.22.1", + "version": "2.22.2", "exports": { ".": "./index.ts", "./core": "./core.ts", diff --git a/package.json b/package.json index f272da1..f518977 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.22.1", + "version": "2.22.2", "description": "Tools for making a Nostr client.", "repository": { "type": "git", diff --git a/pool.ts b/pool.ts index 8c1d3eb..350c8bd 100644 --- a/pool.ts +++ b/pool.ts @@ -15,7 +15,7 @@ export function useWebSocketImplementation(websocketImplementation: any) { export class SimplePool extends AbstractSimplePool { constructor(options?: Pick) { - super({ verifyEvent, websocketImplementation: _WebSocket, ...options }) + super({ verifyEvent, websocketImplementation: _WebSocket, maxWaitForConnection: 3000, ...options }) } }