Skip to content

Commit 9c50b2c

Browse files
im-adithyafiatjaf
authored andcommitted
fix: clear timeout in publish and auth
1 parent bbb0942 commit 9c50b2c

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

abstract-relay.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export class AbstractRelay {
200200
const reason: string = data[3]
201201
const ep = this.openEventPublishes.get(id) as EventPublishResolver
202202
if (ep) {
203+
clearTimeout(ep.timeout)
203204
if (ok) ep.resolve(reason)
204205
else ep.reject(new Error(reason))
205206
this.openEventPublishes.delete(id)
@@ -240,24 +241,31 @@ export class AbstractRelay {
240241
if (!this.challenge) throw new Error("can't perform auth, no challenge was received")
241242
const evt = await signAuthEvent(makeAuthEvent(this.url, this.challenge))
242243
const ret = new Promise<string>((resolve, reject) => {
243-
this.openEventPublishes.set(evt.id, { resolve, reject })
244+
const timeout = setTimeout(() => {
245+
const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
246+
if (ep) {
247+
ep.reject(new Error('auth timed out'))
248+
this.openEventPublishes.delete(evt.id)
249+
}
250+
}, this.publishTimeout)
251+
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
244252
})
245253
this.send('["AUTH",' + JSON.stringify(evt) + ']')
246254
return ret
247255
}
248256

249257
public async publish(event: Event): Promise<string> {
250258
const ret = new Promise<string>((resolve, reject) => {
251-
this.openEventPublishes.set(event.id, { resolve, reject })
259+
const timeout = setTimeout(() => {
260+
const ep = this.openEventPublishes.get(event.id) as EventPublishResolver
261+
if (ep) {
262+
ep.reject(new Error('publish timed out'))
263+
this.openEventPublishes.delete(event.id)
264+
}
265+
}, this.publishTimeout)
266+
this.openEventPublishes.set(event.id, { resolve, reject, timeout })
252267
})
253268
this.send('["EVENT",' + JSON.stringify(event) + ']')
254-
setTimeout(() => {
255-
const ep = this.openEventPublishes.get(event.id) as EventPublishResolver
256-
if (ep) {
257-
ep.reject(new Error('publish timed out'))
258-
this.openEventPublishes.delete(event.id)
259-
}
260-
}, this.publishTimeout)
261269
return ret
262270
}
263271

@@ -381,4 +389,5 @@ export type CountResolver = {
381389
export type EventPublishResolver = {
382390
resolve: (reason: string) => void
383391
reject: (err: Error) => void
392+
timeout: ReturnType<typeof setTimeout>
384393
}

0 commit comments

Comments
 (0)