@@ -25,6 +25,9 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
2525 // allowConnectingToRelay takes a relay URL and the operation being performed
2626 // return false to skip connecting to that relay
2727 allowConnectingToRelay ?: ( url : string , operation : [ 'read' , Filter [ ] ] | [ 'write' , Event ] ) => boolean
28+ // maxWaitForConnection takes a number in milliseconds that will be given to ensureRelay such that we
29+ // don't get stuck forever when attempting to connect to a relay, it is 3000 (3 seconds) by default
30+ maxWaitForConnection : number
2831}
2932
3033export type SubscribeManyParams = Omit < SubscriptionParams , 'onclose' > & {
@@ -48,6 +51,7 @@ export class AbstractSimplePool {
4851 public trustedRelayURLs : Set < string > = new Set ( )
4952 public onRelayConnectionFailure ?: ( url : string ) => void
5053 public allowConnectingToRelay ?: ( url : string , operation : [ 'read' , Filter [ ] ] | [ 'write' , Event ] ) => boolean
54+ public maxWaitForConnection : number
5155
5256 private _WebSocket ?: typeof WebSocket
5357
@@ -59,6 +63,7 @@ export class AbstractSimplePool {
5963 this . automaticallyAuth = opts . automaticallyAuth
6064 this . onRelayConnectionFailure = opts . onRelayConnectionFailure
6165 this . allowConnectingToRelay = opts . allowConnectingToRelay
66+ this . maxWaitForConnection = opts . maxWaitForConnection || 3000
6267 }
6368
6469 async ensureRelay (
@@ -199,7 +204,10 @@ export class AbstractSimplePool {
199204 let relay : AbstractRelay
200205 try {
201206 relay = await this . ensureRelay ( url , {
202- connectionTimeout : params . maxWait ? Math . max ( params . maxWait * 0.8 , params . maxWait - 1000 ) : undefined ,
207+ connectionTimeout :
208+ this . maxWaitForConnection < ( params . maxWait || 0 )
209+ ? Math . max ( params . maxWait ! * 0.8 , params . maxWait ! - 1000 )
210+ : this . maxWaitForConnection ,
203211 abort : params . abort ,
204212 } )
205213 } catch ( err ) {
@@ -314,7 +322,11 @@ export class AbstractSimplePool {
314322 publish (
315323 relays : string [ ] ,
316324 event : Event ,
317- options ?: { onauth ?: ( evt : EventTemplate ) => Promise < VerifiedEvent > } ,
325+ params ?: {
326+ onauth ?: ( evt : EventTemplate ) => Promise < VerifiedEvent >
327+ maxWait ?: number
328+ abort ?: AbortSignal
329+ } ,
318330 ) : Promise < string > [ ] {
319331 return relays . map ( normalizeURL ) . map ( async ( url , i , arr ) => {
320332 if ( arr . indexOf ( url ) !== i ) {
@@ -328,7 +340,13 @@ export class AbstractSimplePool {
328340
329341 let r : Relay
330342 try {
331- r = await this . ensureRelay ( url )
343+ r = await this . ensureRelay ( url , {
344+ connectionTimeout :
345+ this . maxWaitForConnection < ( params ?. maxWait || 0 )
346+ ? Math . max ( params ! . maxWait ! * 0.8 , params ! . maxWait ! - 1000 )
347+ : this . maxWaitForConnection ,
348+ abort : params ?. abort ,
349+ } )
332350 } catch ( err ) {
333351 this . onRelayConnectionFailure ?.( url )
334352 return String ( 'connection failure: ' + String ( err ) )
@@ -337,8 +355,8 @@ export class AbstractSimplePool {
337355 return r
338356 . publish ( event )
339357 . catch ( async err => {
340- if ( err instanceof Error && err . message . startsWith ( 'auth-required: ' ) && options ?. onauth ) {
341- await r . auth ( options . onauth )
358+ if ( err instanceof Error && err . message . startsWith ( 'auth-required: ' ) && params ?. onauth ) {
359+ await r . auth ( params . onauth )
342360 return r . publish ( event ) // retry
343361 }
344362 throw err
0 commit comments