Add message to handlers
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/util",
|
||||
"version": "0.0.27",
|
||||
"version": "0.0.28",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of nostr-related utilities.",
|
||||
|
||||
@@ -51,10 +51,10 @@ export const createEvent = (kind: number, {content = "", tags = [], created_at =
|
||||
({kind, content, tags, created_at})
|
||||
|
||||
export const isEventTemplate = (e: EventTemplate): e is EventTemplate =>
|
||||
Boolean(typeof e.kind === "number" && e.tags && typeof e.content === "string")
|
||||
Boolean(typeof e.kind === "number" && Array.isArray(e.tags) && typeof e.content === "string")
|
||||
|
||||
export const isStampedEvent = (e: StampedEvent): e is StampedEvent =>
|
||||
Boolean(isEventTemplate(e) && e.created_at)
|
||||
Boolean(isEventTemplate(e) && e.created_at >= 0)
|
||||
|
||||
export const isOwnedEvent = (e: OwnedEvent): e is OwnedEvent =>
|
||||
Boolean(isStampedEvent(e) && e.pubkey)
|
||||
|
||||
@@ -2,7 +2,7 @@ import {last, Emitter, normalizeUrl, sleep, stripProtocol} from '@welshman/lib'
|
||||
import {matchFilters} from './Filters'
|
||||
import type {Repository} from './Repository'
|
||||
import type {Filter} from './Filters'
|
||||
import type {TrustedEvent} from './Events'
|
||||
import type {HashedEvent, TrustedEvent} from './Events'
|
||||
|
||||
// Constants and types
|
||||
|
||||
@@ -75,22 +75,22 @@ export const displayRelayUrl = (url: string) => last(url.split("://")).replace(/
|
||||
|
||||
// In-memory relay implementation backed by Repository
|
||||
|
||||
export class Relay extends Emitter {
|
||||
export class Relay<E extends HashedEvent = TrustedEvent> extends Emitter {
|
||||
subs = new Map<string, Filter[]>()
|
||||
|
||||
constructor(readonly repository: Repository) {
|
||||
constructor(readonly repository: Repository<E>) {
|
||||
super()
|
||||
}
|
||||
|
||||
send(type: string, ...message: any[]) {
|
||||
switch(type) {
|
||||
case 'EVENT': return this.handleEVENT(message as [TrustedEvent])
|
||||
case 'EVENT': return this.handleEVENT(message as [E])
|
||||
case 'CLOSE': return this.handleCLOSE(message as [string])
|
||||
case 'REQ': return this.handleREQ(message as [string, ...Filter[]])
|
||||
}
|
||||
}
|
||||
|
||||
handleEVENT([event]: [TrustedEvent]) {
|
||||
handleEVENT([event]: [E]) {
|
||||
this.repository.publish(event)
|
||||
|
||||
// Callers generally expect async relays
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import {flatten, Emitter, sortBy, inc, chunk, sleep, uniq, omit, now, range, identity} from '@welshman/lib'
|
||||
import {DELETE} from './Kinds'
|
||||
import {EPOCH, matchFilter} from './Filters'
|
||||
import {isReplaceable, isTrustedEvent} from './Events'
|
||||
import {isReplaceable, isUnwrappedEvent} from './Events'
|
||||
import {getAddress} from './Address'
|
||||
import type {Filter} from './Filters'
|
||||
import type {TrustedEvent} from './Events'
|
||||
import type {TrustedEvent, HashedEvent} from './Events'
|
||||
|
||||
export const DAY = 86400
|
||||
|
||||
const getDay = (ts: number) => Math.floor(ts / DAY)
|
||||
|
||||
export class Repository extends Emitter {
|
||||
eventsById = new Map<string, TrustedEvent>()
|
||||
eventsByWrap = new Map<string, TrustedEvent>()
|
||||
eventsByAddress = new Map<string, TrustedEvent>()
|
||||
eventsByTag = new Map<string, TrustedEvent[]>()
|
||||
eventsByDay = new Map<number, TrustedEvent[]>()
|
||||
eventsByAuthor = new Map<string, TrustedEvent[]>()
|
||||
export class Repository<E extends HashedEvent = TrustedEvent> extends Emitter {
|
||||
eventsById = new Map<string, E>()
|
||||
eventsByWrap = new Map<string, E>()
|
||||
eventsByAddress = new Map<string, E>()
|
||||
eventsByTag = new Map<string, E[]>()
|
||||
eventsByDay = new Map<number, E[]>()
|
||||
eventsByAuthor = new Map<string, E[]>()
|
||||
deletes = new Map<string, number>()
|
||||
|
||||
// Dump/load/clear
|
||||
@@ -25,7 +25,7 @@ export class Repository extends Emitter {
|
||||
return Array.from(this.eventsById.values())
|
||||
}
|
||||
|
||||
load = async (events: TrustedEvent[], chunkSize = 1000) => {
|
||||
load = async (events: E[], chunkSize = 1000) => {
|
||||
this.clear()
|
||||
|
||||
const added = []
|
||||
@@ -69,7 +69,7 @@ export class Repository extends Emitter {
|
||||
: this.eventsById.get(idOrAddress)
|
||||
}
|
||||
|
||||
hasEvent = (event: TrustedEvent) => {
|
||||
hasEvent = (event: E) => {
|
||||
const duplicate = (
|
||||
this.eventsById.get(event.id) ||
|
||||
this.eventsByAddress.get(getAddress(event))
|
||||
@@ -79,12 +79,12 @@ export class Repository extends Emitter {
|
||||
}
|
||||
|
||||
query = (filters: Filter[], {includeDeleted = false} = {}) => {
|
||||
const result: TrustedEvent[][] = []
|
||||
const result: E[][] = []
|
||||
for (let filter of filters) {
|
||||
let events: TrustedEvent[] = Array.from(this.eventsById.values())
|
||||
let events: E[] = Array.from(this.eventsById.values())
|
||||
|
||||
if (filter.ids) {
|
||||
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as TrustedEvent[]
|
||||
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as E[]
|
||||
filter = omit(['ids'], filter)
|
||||
} else if (filter.authors) {
|
||||
events = uniq(filter.authors!.flatMap(pubkey => this.eventsByAuthor.get(pubkey) || []))
|
||||
@@ -112,8 +112,8 @@ export class Repository extends Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
const chunk: TrustedEvent[] = []
|
||||
for (const event of sortBy((e: TrustedEvent) => -e.created_at, events)) {
|
||||
const chunk: E[] = []
|
||||
for (const event of sortBy((e: E) => -e.created_at, events)) {
|
||||
if (filter.limit && chunk.length >= filter.limit) {
|
||||
break
|
||||
}
|
||||
@@ -133,11 +133,7 @@ export class Repository extends Emitter {
|
||||
return uniq(flatten(result))
|
||||
}
|
||||
|
||||
publish = (event: TrustedEvent, {shouldNotify = true} = {}): boolean => {
|
||||
if (!isTrustedEvent(event)) {
|
||||
throw new Error("Invalid event published to Repository", event)
|
||||
}
|
||||
|
||||
publish = (event: E, {shouldNotify = true} = {}): boolean => {
|
||||
// If we've already seen this event, or it's been deleted, we're done
|
||||
if (this.eventsById.get(event.id) || this.isDeleted(event)) {
|
||||
return false
|
||||
@@ -169,7 +165,7 @@ export class Repository extends Emitter {
|
||||
}
|
||||
|
||||
// Save wrapper index
|
||||
if (event.wrap) {
|
||||
if (isUnwrappedEvent(event)) {
|
||||
this.eventsByWrap.set(event.wrap.id, event)
|
||||
}
|
||||
|
||||
@@ -203,19 +199,19 @@ export class Repository extends Emitter {
|
||||
return true
|
||||
}
|
||||
|
||||
isDeletedByAddress = (event: TrustedEvent) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||
isDeletedByAddress = (event: E) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||
|
||||
isDeletedById = (event: TrustedEvent) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||
isDeletedById = (event: E) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||
|
||||
isDeleted = (event: TrustedEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||
isDeleted = (event: E) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||
|
||||
// Utilities
|
||||
|
||||
_updateIndex<K>(m: Map<K, TrustedEvent[]>, k: K, e: TrustedEvent, duplicate?: TrustedEvent) {
|
||||
_updateIndex<K>(m: Map<K, E[]>, k: K, e: E, duplicate?: E) {
|
||||
let a = m.get(k) || []
|
||||
|
||||
if (duplicate) {
|
||||
a = a.filter((x: TrustedEvent) => x !== duplicate)
|
||||
a = a.filter((x: E) => x !== duplicate)
|
||||
}
|
||||
|
||||
a.push(e)
|
||||
|
||||
Reference in New Issue
Block a user