Get rid of domain module, allow app to override default event type

This commit is contained in:
Jon Staab
2024-08-13 15:45:20 -07:00
parent 5a63273b9d
commit 149c29472c
33 changed files with 201 additions and 285 deletions
+2 -2
View File
@@ -1,10 +1,10 @@
import type {Event, Filter} from 'nostr-tools'
import type {SignedEvent, Filter} from '@welshman/util'
import type {Message} from './Socket'
import type {Connection} from './Connection'
export type PublishMeta = {
sent: number
event: Event
event: SignedEvent
}
export type RequestMeta = {
+5 -6
View File
@@ -1,6 +1,5 @@
import type {Event} from 'nostr-tools'
import {matchFilters, hasValidSignature} from '@welshman/util'
import type {Filter} from '@welshman/util'
import type {Filter, SignedEvent} from '@welshman/util'
import {Pool} from "./Pool"
import {Executor} from "./Executor"
import {Relays} from "./target/Relays"
@@ -10,17 +9,17 @@ export const defaultPool = new Pool()
export const defaultGetExecutor = (relays: string[]) =>
new Executor(new Relays(relays.map((relay: string) => NetworkContext.pool.get(relay))))
const defaultOnEvent = (url: string, event: Event) => null
const defaultOnEvent = (url: string, event: SignedEvent) => null
const defaultOnAuth = (url: string, challenge: string) => null
const defaultOnOk = (url: string, id: string, ok: boolean, message: string) => null
const defaultIsDeleted = (url: string, event: Event) => false
const defaultIsDeleted = (url: string, event: SignedEvent) => false
const defaultHasValidSignature = (url: string, event: Event) => hasValidSignature(event)
const defaultHasValidSignature = (url: string, event: SignedEvent) => hasValidSignature(event)
const defaultMatchFilters = (url: string, filters: Filter[], event: Event) => matchFilters(filters, event)
const defaultMatchFilters = (url: string, filters: Filter[], event: SignedEvent) => matchFilters(filters, event)
export const NetworkContext = {
pool: defaultPool,
+4 -4
View File
@@ -1,5 +1,5 @@
import type {Event, Filter} from 'nostr-tools'
import type {Emitter} from '@welshman/lib'
import type {SignedEvent, Filter} from '@welshman/util'
import type {Message} from './Socket'
import type {Connection} from './Connection'
import {NetworkContext} from './Context'
@@ -10,7 +10,7 @@ export type Target = Emitter & {
cleanup: () => void
}
type EventCallback = (url: string, event: Event) => void
type EventCallback = (url: string, event: SignedEvent) => void
type EoseCallback = (url: string) => void
type OkCallback = (url: string, id: string, ...extra: any[]) => void
type ErrorCallback = (url: string, id: string, ...extra: any[]) => void
@@ -31,7 +31,7 @@ export class Executor {
const id = createSubId('REQ')
const eventListener = (url: string, subid: string, e: Event) => {
const eventListener = (url: string, subid: string, e: SignedEvent) => {
if (subid === id) {
NetworkContext.onEvent(url, e)
onEvent?.(url, e)
@@ -61,7 +61,7 @@ export class Executor {
}
}
publish(event: Event, {verb = 'EVENT', onOk, onError}: PublishOpts = {}) {
publish(event: SignedEvent, {verb = 'EVENT', onOk, onError}: PublishOpts = {}) {
const okListener = (url: string, id: string, ...payload: any[]) => {
if (id === event.id) {
NetworkContext.onEvent(url, event)
+2 -2
View File
@@ -1,7 +1,7 @@
import type {Event} from 'nostr-tools'
import {Emitter, now, randomId, defer} from '@welshman/lib'
import type {Deferred} from '@welshman/lib'
import {asSignedEvent} from '@welshman/util'
import type {SignedEvent} from '@welshman/util'
import {NetworkContext} from './Context'
export enum PublishStatus {
@@ -15,7 +15,7 @@ export enum PublishStatus {
export type PublishStatusMap = Map<string, PublishStatus>
export type PublishRequest = {
event: Event
event: SignedEvent
relays: string[]
signal?: AbortSignal
timeout?: number
+6 -7
View File
@@ -1,6 +1,5 @@
import type {Event} from 'nostr-tools'
import {Emitter, chunk, randomId, once, groupBy, batch, uniq} from '@welshman/lib'
import {matchFilters, unionFilters} from '@welshman/util'
import {matchFilters, unionFilters, SignedEvent} from '@welshman/util'
import type {Filter} from '@welshman/util'
import {Tracker} from "./Tracker"
import {Connection} from './Connection'
@@ -101,7 +100,7 @@ export const mergeSubscriptions = (subs: Subscription[]) => {
controller.signal.addEventListener('abort', onAbort)
}
mergedSub.emitter.on(SubscriptionEvent.Event, (url: string, event: Event) => {
mergedSub.emitter.on(SubscriptionEvent.Event, (url: string, event: SignedEvent) => {
for (const sub of callerSubs) {
if (sub.tracker.track(event.id, url)) {
continue
@@ -117,7 +116,7 @@ export const mergeSubscriptions = (subs: Subscription[]) => {
// Pass events back to caller
const propagateEvent = (type: SubscriptionEvent, checkFilter: boolean) =>
mergedSub.emitter.on(type, (url: string, event: Event) => {
mergedSub.emitter.on(type, (url: string, event: SignedEvent) => {
for (const sub of callerSubs) {
if (!checkFilter || matchFilters(sub.request.filters, event)) {
sub.emitter.emit(type, url, event)
@@ -172,11 +171,11 @@ export const executeSubscription = (sub: Subscription) => {
const executor = NetworkContext.getExecutor(relays)
const subs: {unsubscribe: () => void}[] = []
const completedRelays = new Set()
const events: Event[] = []
const events: SignedEvent[] = []
// Hook up our events
emitter.on(SubscriptionEvent.Event, (url: string, event: Event) => {
emitter.on(SubscriptionEvent.Event, (url: string, event: SignedEvent) => {
events.push(event)
})
@@ -205,7 +204,7 @@ export const executeSubscription = (sub: Subscription) => {
// Functions for emitting events
const onEvent = (url: string, event: Event) => {
const onEvent = (url: string, event: SignedEvent) => {
if (tracker.track(event.id, url)) {
emitter.emit(SubscriptionEvent.Duplicate, url, event)
} else if (NetworkContext.isDeleted(url, event)) {