From c015b6e794186f61c6617514303ef8d2ef674ca3 Mon Sep 17 00:00:00 2001 From: codytseng Date: Fri, 9 May 2025 10:36:20 +0800 Subject: [PATCH] fix bug where concurrent auth calls returned only one response --- abstract-relay.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/abstract-relay.ts b/abstract-relay.ts index 48fb387..d0dccd8 100644 --- a/abstract-relay.ts +++ b/abstract-relay.ts @@ -224,7 +224,6 @@ export class AbstractRelay { return case 'AUTH': { this.challenge = data[1] as string - this.authPromise = undefined this._onauth?.(data[1] as string) return } @@ -243,10 +242,12 @@ export class AbstractRelay { } public async auth(signAuthEvent: (evt: EventTemplate) => Promise): Promise { - if (!this.challenge) throw new Error("can't perform auth, no challenge was received") + const challenge = this.challenge + if (!challenge) throw new Error("can't perform auth, no challenge was received") if (this.authPromise) return this.authPromise - const evt = await signAuthEvent(makeAuthEvent(this.url, this.challenge)) - this.authPromise = new Promise((resolve, reject) => { + + this.authPromise = new Promise(async (resolve, reject) => { + const evt = await signAuthEvent(makeAuthEvent(this.url, challenge)) const timeout = setTimeout(() => { const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver if (ep) { @@ -255,8 +256,8 @@ export class AbstractRelay { } }, this.publishTimeout) this.openEventPublishes.set(evt.id, { resolve, reject, timeout }) + this.send('["AUTH",' + JSON.stringify(evt) + ']') }) - this.send('["AUTH",' + JSON.stringify(evt) + ']') return this.authPromise }