Resend messages rejected because of auth challenge

This commit is contained in:
Jon Staab
2024-02-14 09:43:32 -08:00
parent 4a0ae3770e
commit 516299e4c4
2 changed files with 39 additions and 30 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "paravel", "name": "paravel",
"version": "0.4.19", "version": "0.4.20",
"description": "Yet another toolkit for nostr", "description": "Yet another toolkit for nostr",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
+38 -29
View File
@@ -1,5 +1,6 @@
import type {Event, Filter} from 'nostr-tools' import type {Event, Filter} from 'nostr-tools'
import type {Connection} from './Connection' import type {Connection} from './Connection'
import type {Message} from './util/Socket'
export type PublishMeta = { export type PublishMeta = {
sent: number sent: number
@@ -57,32 +58,23 @@ export class ConnectionMeta {
this.lastFault = Date.now() this.lastFault = Date.now()
}) })
// @ts-ignore cxn.on('send', (cxn: Connection, message: Message) => {
cxn.on('send', (cxn, [verb, ...payload]) => { if (message[0] === 'REQ') this.onSendRequest(message)
// @ts-ignore if (message[0] === 'CLOSE') this.onSendClose(message)
if (verb === 'REQ') this.onSendRequest(...payload) if (message[0] === 'EVENT') this.onSendEvent(message)
// @ts-ignore
if (verb === 'CLOSE') this.onSendClose(...payload)
// @ts-ignore
if (verb === 'EVENT') this.onSendEvent(...payload)
}) })
// @ts-ignore cxn.on('receive', (cxn: Connection, message: Message) => {
cxn.on('receive', (cxn, [verb, ...payload]) => { if (message[0] === 'OK') this.onReceiveOk(message)
// @ts-ignore if (message[0] === 'AUTH') this.onReceiveAuth(message)
if (verb === 'OK') this.onReceiveOk(...payload) if (message[0] === 'EVENT') this.onReceiveEvent(message)
// @ts-ignore if (message[0] === 'EOSE') this.onReceiveEose(message)
if (verb === 'AUTH') this.onReceiveAuth(...payload) if (message[0] === 'CLOSED') this.onReceiveClosed(message)
// @ts-ignore if (message[0] === 'NOTICE') this.onReceiveNotice(message)
if (verb === 'EVENT') this.onReceiveEvent(...payload)
// @ts-ignore
if (verb === 'EOSE') this.onReceiveEose(...payload)
// @ts-ignore
if (verb === 'NOTICE') this.onReceiveNotice(...payload)
}) })
} }
onSendRequest(subId: string, ...filters: Filter[]) { onSendRequest([verb, subId, ...filters]: Message) {
this.requestCount++ this.requestCount++
this.lastRequest = Date.now() this.lastRequest = Date.now()
this.pendingRequests.set(subId, { this.pendingRequests.set(subId, {
@@ -92,17 +84,17 @@ export class ConnectionMeta {
}) })
} }
onSendClose(subId: string) { onSendClose([verb, subId]: Message) {
this.pendingRequests.delete(subId) this.pendingRequests.delete(subId)
} }
onSendEvent(event: Event) { onSendEvent([verb, event]: Message) {
this.publishCount++ this.publishCount++
this.lastPublish = Date.now() this.lastPublish = Date.now()
this.pendingPublishes.set(event.id, {sent: Date.now(), event}) this.pendingPublishes.set(event.id, {sent: Date.now(), event})
} }
onReceiveOk(eventId: string, ok: boolean) { onReceiveOk([verb, eventId, ok]: Message) {
const publish = this.pendingPublishes.get(eventId) const publish = this.pendingPublishes.get(eventId)
if (ok) { if (ok) {
@@ -116,16 +108,16 @@ export class ConnectionMeta {
} }
} }
onReceiveAuth(eventId: string) { onReceiveAuth([verb, eventId]: Message) {
this.authStatus = AuthStatus.Unauthorized this.authStatus = AuthStatus.Unauthorized
} }
onReceiveEvent(event: Event) { onReceiveEvent([verb, event]: Message) {
this.eventCount++ this.eventCount++
this.lastEvent = Date.now() this.lastEvent = Date.now()
} }
onReceiveEose(subId: string) { onReceiveEose([verb, subId]: Message) {
const request = this.pendingRequests.get(subId) const request = this.pendingRequests.get(subId)
// Only count the first eose // Only count the first eose
@@ -137,8 +129,25 @@ export class ConnectionMeta {
} }
} }
onReceiveNotice(message: string) { onReceiveClosed([verb, id, notice]: Message) {
console.warn('NOTICE', this.cxn.url, message) // Re-enqueue pending reqs/events when auth challenge is received
if (notice.startsWith('auth-required:')) {
const pub = this.pendingPublishes.get(id)
if (pub) {
this.cxn.send(['EVENT', pub.event])
}
const req = this.pendingRequests.get(id)
if (req) {
this.cxn.send(['REQ', id, ...req.filters])
}
}
}
onReceiveNotice([verb, notice]: Message) {
console.warn('NOTICE', this.cxn.url, notice)
} }
clearPending = () => { clearPending = () => {