mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-02-03 23:36:12 +00:00
pool: maxWaitForConnection parameter.
this was so obvious.
This commit is contained in:
@@ -25,6 +25,9 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
|
|||||||
// allowConnectingToRelay takes a relay URL and the operation being performed
|
// allowConnectingToRelay takes a relay URL and the operation being performed
|
||||||
// return false to skip connecting to that relay
|
// return false to skip connecting to that relay
|
||||||
allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
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<SubscriptionParams, 'onclose'> & {
|
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||||
@@ -48,6 +51,7 @@ export class AbstractSimplePool {
|
|||||||
public trustedRelayURLs: Set<string> = new Set()
|
public trustedRelayURLs: Set<string> = new Set()
|
||||||
public onRelayConnectionFailure?: (url: string) => void
|
public onRelayConnectionFailure?: (url: string) => void
|
||||||
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
||||||
|
public maxWaitForConnection: number
|
||||||
|
|
||||||
private _WebSocket?: typeof WebSocket
|
private _WebSocket?: typeof WebSocket
|
||||||
|
|
||||||
@@ -59,6 +63,7 @@ export class AbstractSimplePool {
|
|||||||
this.automaticallyAuth = opts.automaticallyAuth
|
this.automaticallyAuth = opts.automaticallyAuth
|
||||||
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
|
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
|
||||||
this.allowConnectingToRelay = opts.allowConnectingToRelay
|
this.allowConnectingToRelay = opts.allowConnectingToRelay
|
||||||
|
this.maxWaitForConnection = opts.maxWaitForConnection || 3000
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(
|
async ensureRelay(
|
||||||
@@ -199,7 +204,10 @@ export class AbstractSimplePool {
|
|||||||
let relay: AbstractRelay
|
let relay: AbstractRelay
|
||||||
try {
|
try {
|
||||||
relay = await this.ensureRelay(url, {
|
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,
|
abort: params.abort,
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -314,7 +322,11 @@ export class AbstractSimplePool {
|
|||||||
publish(
|
publish(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
event: Event,
|
event: Event,
|
||||||
options?: { onauth?: (evt: EventTemplate) => Promise<VerifiedEvent> },
|
params?: {
|
||||||
|
onauth?: (evt: EventTemplate) => Promise<VerifiedEvent>
|
||||||
|
maxWait?: number
|
||||||
|
abort?: AbortSignal
|
||||||
|
},
|
||||||
): Promise<string>[] {
|
): Promise<string>[] {
|
||||||
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
||||||
if (arr.indexOf(url) !== i) {
|
if (arr.indexOf(url) !== i) {
|
||||||
@@ -328,7 +340,13 @@ export class AbstractSimplePool {
|
|||||||
|
|
||||||
let r: Relay
|
let r: Relay
|
||||||
try {
|
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) {
|
} catch (err) {
|
||||||
this.onRelayConnectionFailure?.(url)
|
this.onRelayConnectionFailure?.(url)
|
||||||
return String('connection failure: ' + String(err))
|
return String('connection failure: ' + String(err))
|
||||||
@@ -337,8 +355,8 @@ export class AbstractSimplePool {
|
|||||||
return r
|
return r
|
||||||
.publish(event)
|
.publish(event)
|
||||||
.catch(async err => {
|
.catch(async err => {
|
||||||
if (err instanceof Error && err.message.startsWith('auth-required: ') && options?.onauth) {
|
if (err instanceof Error && err.message.startsWith('auth-required: ') && params?.onauth) {
|
||||||
await r.auth(options.onauth)
|
await r.auth(params.onauth)
|
||||||
return r.publish(event) // retry
|
return r.publish(event) // retry
|
||||||
}
|
}
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.22.1",
|
"version": "2.22.2",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.22.1",
|
"version": "2.22.2",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
2
pool.ts
2
pool.ts
@@ -15,7 +15,7 @@ export function useWebSocketImplementation(websocketImplementation: any) {
|
|||||||
|
|
||||||
export class SimplePool extends AbstractSimplePool {
|
export class SimplePool extends AbstractSimplePool {
|
||||||
constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) {
|
constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) {
|
||||||
super({ verifyEvent, websocketImplementation: _WebSocket, ...options })
|
super({ verifyEvent, websocketImplementation: _WebSocket, maxWaitForConnection: 3000, ...options })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user