relay: separate auth() and publish() methods

This commit is contained in:
Lynn Zenn
2023-04-18 19:37:41 +02:00
committed by fiatjaf_
parent 6e58fe371c
commit 32c47e9bd8
2 changed files with 36 additions and 29 deletions

View File

@@ -28,14 +28,14 @@ export const authenticate = async ({
], ],
content: '' content: ''
} }
const sub = relay.publish(await sign(e), 'AUTH') const pub = relay.auth(await sign(e))
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
sub.on('ok', function ok() { pub.on('ok', function ok() {
sub.off('ok', ok) pub.off('ok', ok)
resolve() resolve()
}) })
sub.on('failed', function fail(reason: string) { pub.on('failed', function fail(reason: string) {
sub.off('failed', fail) pub.off('failed', fail)
reject(reason) reject(reason)
}) })
}) })

View File

@@ -4,7 +4,6 @@ import {Event, verifySignature, validateEvent} from './event'
import {Filter, matchFilters} from './filter' import {Filter, matchFilters} from './filter'
import {getHex64, getSubscriptionId} from './fakejson' import {getHex64, getSubscriptionId} from './fakejson'
type OutgoingEventType = 'EVENT' | 'AUTH'
type RelayEvent = { type RelayEvent = {
connect: () => void | Promise<void> connect: () => void | Promise<void>
disconnect: () => void | Promise<void> disconnect: () => void | Promise<void>
@@ -32,7 +31,8 @@ export type Relay = {
filters: Filter[], filters: Filter[],
opts?: SubscriptionOptions opts?: SubscriptionOptions
) => Promise<CountPayload | null> ) => Promise<CountPayload | null>
publish: (event: Event, type?: OutgoingEventType) => Pub publish: (event: Event) => Pub
auth: (event: Event) => Pub
off: <T extends keyof RelayEvent, U extends RelayEvent[T]>( off: <T extends keyof RelayEvent, U extends RelayEvent[T]>(
event: T, event: T,
listener: U listener: U
@@ -179,7 +179,7 @@ export function relayInit(
let id = data[1] let id = data[1]
let payload = data[2] let payload = data[2]
if (openSubs[id]) { if (openSubs[id]) {
(subListeners[id]?.count || []).forEach(cb => cb(payload)) ;(subListeners[id]?.count || []).forEach(cb => cb(payload))
} }
return return
case 'EOSE': { case 'EOSE': {
@@ -298,6 +298,29 @@ export function relayInit(
} }
} }
function _publishEvent(event: Event, type: string) {
if (!event.id) throw new Error(`event ${event} has no id`)
let id = event.id
trySend([type, event])
return {
on: (type: 'ok' | 'failed', cb: any) => {
pubListeners[id] = pubListeners[id] || {
ok: [],
failed: []
}
pubListeners[id][type].push(cb)
},
off: (type: 'ok' | 'failed', cb: any) => {
let listeners = pubListeners[id]
if (!listeners) return
let idx = listeners[type].indexOf(cb)
if (idx >= 0) listeners[type].splice(idx, 1)
}
}
}
return { return {
url, url,
sub, sub,
@@ -364,27 +387,11 @@ export function relayInit(
resolve(event) resolve(event)
}) })
}), }),
publish(event, type = 'EVENT'): Pub { publish(event): Pub {
if (!event.id) throw new Error(`event ${event} has no id`) return _publishEvent(event, 'EVENT')
let id = event.id },
auth(event): Pub {
trySend([type, event]) return _publishEvent(event, 'AUTH')
return {
on: (type: 'ok' | 'failed', cb: any) => {
pubListeners[id] = pubListeners[id] || {
ok: [],
failed: []
}
pubListeners[id][type].push(cb)
},
off: (type: 'ok' | 'failed', cb: any) => {
let listeners = pubListeners[id]
if (!listeners) return
let idx = listeners[type].indexOf(cb)
if (idx >= 0) listeners[type].splice(idx, 1)
}
}
}, },
connect, connect,
close(): void { close(): void {