Compare commits

..

3 Commits

2 changed files with 17 additions and 20 deletions

View File

@@ -22,6 +22,8 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
// onRelayConnectionFailure is called with the URL of a relay that failed the initial connection
onRelayConnectionFailure?: (url: string) => void
// onRelayConnectionSuccess is called with the URL of a relay that succeeds the initial connection
onRelayConnectionSuccess?: (url: string) => void
// 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
@@ -50,6 +52,7 @@ export class AbstractSimplePool {
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
public trustedRelayURLs: Set<string> = new Set()
public onRelayConnectionFailure?: (url: string) => void
public onRelayConnectionSuccess?: (url: string) => void
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
public maxWaitForConnection: number
@@ -62,6 +65,7 @@ export class AbstractSimplePool {
this.enableReconnect = opts.enableReconnect || false
this.automaticallyAuth = opts.automaticallyAuth
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
this.onRelayConnectionSuccess = opts.onRelayConnectionSuccess
this.allowConnectingToRelay = opts.allowConnectingToRelay
this.maxWaitForConnection = opts.maxWaitForConnection || 3000
}
@@ -216,6 +220,8 @@ export class AbstractSimplePool {
return
}
this.onRelayConnectionSuccess?.(url)
let subscription = relay.subscribe(filters, {
...params,
oneose: () => handleEose(i),

View File

@@ -45,7 +45,7 @@ export class AbstractRelay {
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
private reconnectAttempts: number = 0
private closedIntentionally: boolean = false
private skipReconnection: boolean = false
private connectionPromise: Promise<void> | undefined
private openCountRequests = new Map<string, CountResolver>()
@@ -120,12 +120,9 @@ export class AbstractRelay {
this._connected = false
this.connectionPromise = undefined
const wasIntentional = this.closedIntentionally
this.closedIntentionally = false // reset for next time
this.onclose?.()
if (this.enableReconnect && !wasIntentional) {
if (this.enableReconnect && !this.skipReconnection) {
this.reconnect()
} else {
this.closeAllSubscriptions(reason)
@@ -139,11 +136,13 @@ export class AbstractRelay {
this.challenge = undefined
this.authPromise = undefined
this.skipReconnection = false
this.connectionPromise = new Promise((resolve, reject) => {
if (opts?.timeout) {
connectionTimeoutHandle = setTimeout(() => {
reject('connection timed out')
this.connectionPromise = undefined
this.skipReconnection = true
this.onclose?.()
this.handleHardClose('relay connection timed out')
}, opts.timeout)
@@ -153,17 +152,8 @@ export class AbstractRelay {
opts.abort.onabort = reject
}
const connectionFailed = () => {
clearTimeout(connectionTimeoutHandle)
reject('connection failed')
this.connectionPromise = undefined
this.onclose?.()
this.handleHardClose('relay connection failed')
}
try {
this.ws = new this._WebSocket(this.url)
this.ws.addEventListener('error', connectionFailed)
} catch (err) {
clearTimeout(connectionTimeoutHandle)
reject(err)
@@ -171,8 +161,6 @@ export class AbstractRelay {
}
this.ws.onopen = () => {
this.ws?.removeEventListener('error', connectionFailed)
if (this.reconnectTimeoutHandle) {
clearTimeout(this.reconnectTimeoutHandle)
this.reconnectTimeoutHandle = undefined
@@ -202,10 +190,13 @@ export class AbstractRelay {
resolve()
}
this.ws.onerror = ev => {
this.ws.onerror = () => {
clearTimeout(connectionTimeoutHandle)
reject((ev as any).message || 'websocket error')
this.handleHardClose('relay connection errored')
reject('connection failed')
this.connectionPromise = undefined
this.skipReconnection = true
this.onclose?.()
this.handleHardClose('relay connection failed')
}
this.ws.onclose = ev => {
@@ -477,7 +468,7 @@ export class AbstractRelay {
}
public close() {
this.closedIntentionally = true
this.skipReconnection = true
if (this.reconnectTimeoutHandle) {
clearTimeout(this.reconnectTimeoutHandle)
this.reconnectTimeoutHandle = undefined