Remove generics for event type

This commit is contained in:
Jon Staab
2024-08-13 09:30:55 -07:00
parent 0576c3c0e0
commit 5a63273b9d
18 changed files with 154 additions and 136 deletions
+23 -4
View File
@@ -601,6 +601,10 @@
"resolved": "packages/content",
"link": true
},
"node_modules/@welshman/domain": {
"resolved": "packages/domain",
"link": true
},
"node_modules/@welshman/dvm": {
"resolved": "packages/dvm",
"link": true
@@ -3269,11 +3273,26 @@
},
"packages/content": {
"name": "@welshman/content",
"version": "0.0.6",
"version": "0.0.7",
"license": "MIT",
"dependencies": {
"@braintree/sanitize-url": "^7.0.2",
"nostr-tools": "^2.7.0"
"nostr-tools": "^2.7.2"
},
"devDependencies": {
"gts": "^5.0.1",
"tsc-multi": "^1.1.0",
"typescript": "~5.1.6"
}
},
"packages/domain": {
"name": "@welshman/domain",
"version": "0.0.1",
"license": "MIT",
"dependencies": {
"@welshman/lib": "0.0.14",
"@welshman/util": "0.0.25",
"nostr-tools": "^2.7.2"
},
"devDependencies": {
"gts": "^5.0.1",
@@ -3289,7 +3308,7 @@
"@welshman/lib": "0.0.14",
"@welshman/net": "0.0.18",
"@welshman/util": "0.0.25",
"nostr-tools": "^2.7.0"
"nostr-tools": "^2.7.2"
},
"devDependencies": {
"gts": "^5.0.1",
@@ -3408,7 +3427,7 @@
"license": "MIT",
"dependencies": {
"@welshman/lib": "0.0.14",
"nostr-tools": "^2.3.2"
"nostr-tools": "^2.7.2"
},
"devDependencies": {
"gts": "^5.0.1",
+1 -1
View File
@@ -32,6 +32,6 @@
},
"dependencies": {
"@braintree/sanitize-url": "^7.0.2",
"nostr-tools": "^2.7.0"
"nostr-tools": "^2.7.2"
}
}
+1 -1
View File
@@ -31,7 +31,7 @@
"typescript": "~5.1.6"
},
"dependencies": {
"nostr-tools": "^2.3.2",
"nostr-tools": "^2.7.2",
"@welshman/lib": "0.0.14",
"@welshman/util": "0.0.25"
}
+8 -8
View File
@@ -1,20 +1,20 @@
import {fromPairs, parseJson} from "@welshman/lib"
import {getAddress, Tags} from "@welshman/util"
import type {TrustedEvent} from "@welshman/util"
import type {ExtensibleTrustedEvent} from "@welshman/util"
export type Handler<E extends TrustedEvent> = {
export type Handler = {
kind: number
name: string
about: string
image: string
identifier: string
event: E
event: ExtensibleTrustedEvent
website?: string
lud16?: string
nip05?: string
}
export const readHandlers = <E extends TrustedEvent>(event: E) => {
export const readHandlers = (event: ExtensibleTrustedEvent) => {
const {d: identifier} = fromPairs(event.tags)
const meta = parseJson(event.content)
const normalizedMeta = {
@@ -35,14 +35,14 @@ export const readHandlers = <E extends TrustedEvent>(event: E) => {
.whereKey("k")
.values()
.valueOf()
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler<E>[]
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler[]
}
export const getHandlerKey = <E extends TrustedEvent>(handler: Handler<E>) => `${handler.kind}:${getAddress(handler.event)}`
export const getHandlerKey = (handler: Handler) => `${handler.kind}:${getAddress(handler.event)}`
export const displayHandler = <E extends TrustedEvent>(handler?: Handler<E>, fallback = "") => handler?.name || fallback
export const displayHandler = (handler?: Handler, fallback = "") => handler?.name || fallback
export const getHandlerAddress = <E extends TrustedEvent>(event: E) => {
export const getHandlerAddress = (event: ExtensibleTrustedEvent) => {
const tags = Tags.fromEvent(event).whereKey("a")
const tag = tags.filter(t => t.last() === "web").first() || tags.first()
+9 -9
View File
@@ -1,22 +1,22 @@
import {parseJson} from "@welshman/lib"
import {Address, isShareableRelayUrl, TrustedEvent} from "@welshman/util"
import {Address, isShareableRelayUrl} from "@welshman/util"
import {Encryptable, DecryptedEvent} from "./util"
export type ListParams = {
kind: number
}
export type List<E extends TrustedEvent> = ListParams & {
export type List = ListParams & {
publicTags: string[][]
privateTags: string[][]
event?: DecryptedEvent<E>
event?: DecryptedEvent
}
export type PublishedList<E extends TrustedEvent> = Omit<List<E>, "event"> & {
event: DecryptedEvent<E>
export type PublishedList = Omit<List, "event"> & {
event: DecryptedEvent
}
export const makeList = <E extends TrustedEvent>(list: ListParams & Partial<List<E>>): List<E> =>
export const makeList = (list: ListParams & Partial<List>): List =>
({publicTags: [], privateTags: [], ...list})
const isValidTag = (tag: string[]) => {
@@ -30,7 +30,7 @@ const isValidTag = (tag: string[]) => {
return true
}
export const readList = <E extends TrustedEvent>(event: DecryptedEvent<E>): PublishedList<E> => {
export const readList = (event: DecryptedEvent): PublishedList => {
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
const privateTags = getTags(parseJson(event.plaintext?.content))
const publicTags = getTags(event.tags)
@@ -38,8 +38,8 @@ export const readList = <E extends TrustedEvent>(event: DecryptedEvent<E>): Publ
return {event, kind: event.kind, publicTags, privateTags}
}
export const createList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: List<E>) =>
export const createList = ({kind, publicTags = [], privateTags = []}: List) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const editList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: PublishedList<E>) =>
export const editList = ({kind, publicTags = [], privateTags = []}: PublishedList) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
+13 -13
View File
@@ -1,8 +1,8 @@
import {nip19} from "nostr-tools"
import {ellipsize, parseJson} from "@welshman/lib"
import {PROFILE, TrustedEvent} from "@welshman/util"
import {PROFILE, ExtensibleTrustedEvent} from "@welshman/util"
export type Profile<E extends TrustedEvent> = {
export type Profile = {
name?: string
nip05?: string
lud06?: string
@@ -12,17 +12,17 @@ export type Profile<E extends TrustedEvent> = {
picture?: string
website?: string
display_name?: string
event?: E
event?: ExtensibleTrustedEvent
}
export type PublishedProfile<E extends TrustedEvent> = Omit<Profile<E>, "event"> & {
event: E
export type PublishedProfile = Omit<Profile, "event"> & {
event: ExtensibleTrustedEvent
}
export const isPublishedProfile = <E extends TrustedEvent>(profile: Profile<E>): profile is PublishedProfile<E> =>
export const isPublishedProfile = (profile: Profile): profile is PublishedProfile =>
Boolean(profile.event)
export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>> = {}): Profile<E> => ({
export const makeProfile = (profile: Partial<Profile> = {}): Profile => ({
name: "",
nip05: "",
lud06: "",
@@ -35,18 +35,18 @@ export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>>
...profile,
})
export const readProfile = <E extends TrustedEvent>(event: E) => {
export const readProfile = (event: ExtensibleTrustedEvent) => {
const profile = parseJson(event.content) || {}
return {...profile, event} as PublishedProfile<E>
return {...profile, event} as PublishedProfile
}
export const createProfile = <E extends TrustedEvent>({event, ...profile}: Profile<E>) => ({
export const createProfile = ({event, ...profile}: Profile) => ({
kind: PROFILE,
content: JSON.stringify(profile),
})
export const editProfile = <E extends TrustedEvent>({event, ...profile}: PublishedProfile<E>) => ({
export const editProfile = ({event, ...profile}: PublishedProfile) => ({
kind: PROFILE,
content: JSON.stringify(profile),
tags: event.tags,
@@ -58,7 +58,7 @@ export const displayPubkey = (pubkey: string) => {
return d.slice(0, 8) + "…" + d.slice(-5)
}
export const displayProfile = <E extends TrustedEvent>(profile?: Profile<E>, fallback = "") => {
export const displayProfile = (profile?: Profile, fallback = "") => {
const {display_name, name, event} = profile || {}
if (name) return ellipsize(name, 60)
@@ -68,4 +68,4 @@ export const displayProfile = <E extends TrustedEvent>(profile?: Profile<E>, fal
return fallback
}
export const profileHasName = <E extends TrustedEvent>(profile?: Profile<E>) => Boolean(profile?.name || profile?.display_name)
export const profileHasName = (profile?: Profile) => Boolean(profile?.name || profile?.display_name)
+9 -14
View File
@@ -1,21 +1,16 @@
import type {TrustedEvent} from "@welshman/util"
import type {EventContent, ExtensibleTrustedEvent} from "@welshman/util"
export type Encrypt = (x: string) => Promise<string>
export type EventContent = {
content?: string
tags?: string[][]
export type DecryptedEvent = ExtensibleTrustedEvent & {
plaintext: Partial<EventContent>
}
export type DecryptedEvent<E extends TrustedEvent> = E & {
plaintext: EventContent
}
export const asDecryptedEvent = (event: ExtensibleTrustedEvent, plaintext: Partial<EventContent>) =>
({...event, plaintext}) as DecryptedEvent
export const asDecryptedEvent = <E extends TrustedEvent>(event: E, plaintext: EventContent) =>
({...event, plaintext}) as DecryptedEvent<E>
export class Encryptable<E extends TrustedEvent> {
constructor(readonly event: Partial<E>, readonly updates: EventContent) {}
export class Encryptable<E extends Partial<EventContent>> {
constructor(readonly event: E, readonly updates: E) {}
async reconcile(encrypt: Encrypt) {
const encryptContent = () => {
@@ -41,8 +36,8 @@ export class Encryptable<E extends TrustedEvent> {
// Updates are optional. If not provided, fall back to the event's content and tags.
return {
...this.event,
tags: tags || this.event.tags,
content: content || this.event.content,
tags: tags || this.event.tags || [],
content: content || this.event.content || "",
}
}
}
+1 -1
View File
@@ -34,6 +34,6 @@
"@welshman/lib": "0.0.14",
"@welshman/net": "0.0.18",
"@welshman/util": "0.0.25",
"nostr-tools": "^2.7.0"
"nostr-tools": "^2.7.2"
}
}
+5 -5
View File
@@ -1,12 +1,12 @@
import {hexToBytes} from '@noble/hashes/utils'
import {getPublicKey, finalizeEvent} from 'nostr-tools'
import {now} from '@welshman/lib'
import type {TrustedEvent, EventTemplate, Filter} from '@welshman/util'
import type {ExtensibleTrustedEvent, EventTemplate, Filter} from '@welshman/util'
import {subscribe, publish} from '@welshman/net'
export type DVMHandler = {
stop?: () => void
handleEvent: (e: TrustedEvent) => AsyncGenerator<EventTemplate>
handleEvent: (e: ExtensibleTrustedEvent) => AsyncGenerator<EventTemplate>
}
export type CreateDVMHandler = (dvm: DVM) => DVMHandler
@@ -49,7 +49,7 @@ export class DVM {
const filters = [filter]
const sub = subscribe({relays, filters})
sub.emitter.on('event', (url: string, e: TrustedEvent) => this.onEvent(e))
sub.emitter.on('event', (url: string, e: ExtensibleTrustedEvent) => this.onEvent(e))
sub.emitter.on('complete', () => resolve())
})
}
@@ -63,7 +63,7 @@ export class DVM {
this.active = false
}
async onEvent(request: TrustedEvent) {
async onEvent(request: ExtensibleTrustedEvent) {
console.log(request)
const {expireAfter = 60 * 60} = this.opts
@@ -87,7 +87,7 @@ export class DVM {
if (event.kind !== 7000) {
event.tags.push(['request', JSON.stringify(request)])
const inputTag = request.tags.find(t => t[0] === 'i')
const inputTag = request.tags.find((t: string[]) => t[0] === 'i')
if (inputTag) {
event.tags.push(inputTag)
+8 -8
View File
@@ -1,12 +1,12 @@
import {uniq, identity, flatten, pushToMapKey, intersection, tryCatch, now} from '@welshman/lib'
import type {TrustedEvent, Filter} from '@welshman/util'
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
import {Tags, intersectFilters, matchFilter, getAddress, getIdFilters, unionFilters} from '@welshman/util'
import type {CreatedAtItem, RequestItem, ListItem, LabelItem, WOTItem, DVMItem, Scope, Feed, FeedOptions} from './core'
import {getFeedArgs, feedsFromTags} from './utils'
import {FeedType} from './core'
export class FeedCompiler<E extends TrustedEvent> {
constructor(readonly options: FeedOptions<E>) {}
export class FeedCompiler {
constructor(readonly options: FeedOptions) {}
canCompile(feed: Feed): boolean {
switch(feed[0]) {
@@ -109,7 +109,7 @@ export class FeedCompiler<E extends TrustedEvent> {
items.map(({mappings, ...request}) =>
this.options.requestDVM({
...request,
onEvent: async (e: E) => {
onEvent: async (e: ExtensibleTrustedEvent) => {
const tags = Tags.wrap(await tryCatch(() => JSON.parse(e.content)) || [])
for (const feed of feedsFromTags(tags, mappings)) {
@@ -215,11 +215,11 @@ export class FeedCompiler<E extends TrustedEvent> {
async _compileLists(listItems: ListItem[]): Promise<RequestItem[]> {
const addresses = uniq(listItems.flatMap(({addresses}) => addresses))
const eventsByAddress = new Map<string, E>()
const eventsByAddress = new Map<string, ExtensibleTrustedEvent>()
await this.options.request({
filters: getIdFilters(addresses),
onEvent: (e: E) => eventsByAddress.set(getAddress(e), e),
onEvent: (e: ExtensibleTrustedEvent) => eventsByAddress.set(getAddress(e), e),
})
const feeds = flatten(
@@ -246,14 +246,14 @@ export class FeedCompiler<E extends TrustedEvent> {
}
async _compileLabels(labelItems: LabelItem[]): Promise<RequestItem[]> {
const events: E[] = []
const events: ExtensibleTrustedEvent[] = []
await Promise.all(
labelItems.map(({mappings, relays, ...filter}) =>
this.options.request({
relays,
filters: [{kinds: [1985], ...filter}],
onEvent: (e: E) => events.push(e),
onEvent: (e: ExtensibleTrustedEvent) => events.push(e),
})
)
)
+8 -8
View File
@@ -1,4 +1,4 @@
import type {Filter} from '@welshman/util'
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
export enum FeedType {
Address = "address",
@@ -109,8 +109,8 @@ export type RequestItem = {
filters?: Filter[]
}
export type RequestOpts<E> = RequestItem & {
onEvent: (event: E) => void
export type RequestOpts = RequestItem & {
onEvent: (event: ExtensibleTrustedEvent) => void
}
export type DVMRequest = {
@@ -119,13 +119,13 @@ export type DVMRequest = {
relays?: string[],
}
export type DVMOpts<E> = DVMRequest & {
onEvent: (event: E) => void
export type DVMOpts = DVMRequest & {
onEvent: (event: ExtensibleTrustedEvent) => void
}
export type FeedOptions<E> = {
request: (opts: RequestOpts<E>) => Promise<void>
requestDVM: (opts: DVMOpts<E>) => Promise<void>
export type FeedOptions = {
request: (opts: RequestOpts) => Promise<void>
requestDVM: (opts: DVMOpts) => Promise<void>
getPubkeysForScope: (scope: Scope) => string[]
getPubkeysForWOTRange: (minWOT: number, maxWOT: number) => string[]
}
+18 -18
View File
@@ -1,26 +1,26 @@
import {inc, max, min, now} from '@welshman/lib'
import type {TrustedEvent, Filter} from '@welshman/util'
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
import {EPOCH, trimFilters, guessFilterDelta} from '@welshman/util'
import type {Feed, RequestItem, FeedOptions} from './core'
import {FeedType} from './core'
import {FeedCompiler} from './compiler'
export type LoadOpts<E> = {
onEvent?: (event: E) => void
export type LoadOpts = {
onEvent?: (event: ExtensibleTrustedEvent) => void
onExhausted?: () => void
useWindowing?: boolean
}
export type Loader = (limit: number) => Promise<void>
export class FeedLoader<E extends TrustedEvent> {
compiler: FeedCompiler<E>
export class FeedLoader {
compiler: FeedCompiler
constructor(readonly options: FeedOptions<E>) {
constructor(readonly options: FeedOptions) {
this.compiler = new FeedCompiler(options)
}
async getLoader([type, ...feed]: Feed, loadOpts: LoadOpts<E>) {
async getLoader([type, ...feed]: Feed, loadOpts: LoadOpts) {
if (this.compiler.canCompile([type, ...feed] as Feed)) {
return this.getRequestsLoader(await this.compiler.compile([type, ...feed] as Feed), loadOpts)
}
@@ -37,7 +37,7 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async getRequestsLoader(requests: RequestItem[], loadOpts: LoadOpts<E>) {
async getRequestsLoader(requests: RequestItem[], loadOpts: LoadOpts) {
const seen = new Set()
const exhausted = new Set()
const loaders = await Promise.all(
@@ -64,7 +64,7 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getRequestLoader({relays, filters}: RequestItem, {useWindowing = true, onEvent, onExhausted}: LoadOpts<E>) {
async _getRequestLoader({relays, filters}: RequestItem, {useWindowing = true, onEvent, onExhausted}: LoadOpts) {
// Make sure we have some kind of filter to send if we've been given an empty one, as happens with relay feeds
if (!filters || filters.length === 0) {
filters = [{}]
@@ -101,7 +101,7 @@ export class FeedLoader<E extends TrustedEvent> {
await this.options.request({
relays,
filters: trimFilters(requestFilters),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
count += 1
until = Math.min(until, event.created_at - 1)
onEvent?.(event)
@@ -127,17 +127,17 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getDifferenceLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
async _getDifferenceLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
const exhausted = new Set<number>()
const skip = new Set<string>()
const events: E[] = []
const events: ExtensibleTrustedEvent[] = []
const seen = new Set()
const loaders = await Promise.all(
feeds.map((feed: Feed, i: number) =>
this.getLoader(feed, {
onExhausted: () => exhausted.add(i),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
if (i === 0) {
events.push(event)
} else {
@@ -172,17 +172,17 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getIntersectionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
async _getIntersectionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
const exhausted = new Set<number>()
const counts = new Map<string, number>()
const events: E[] = []
const events: ExtensibleTrustedEvent[] = []
const seen = new Set()
const loaders = await Promise.all(
feeds.map((feed: Feed, i: number) =>
this.getLoader(feed, {
onExhausted: () => exhausted.add(i),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
events.push(event)
counts.set(event.id, inc(counts.get(event.id)))
},
@@ -214,7 +214,7 @@ export class FeedLoader<E extends TrustedEvent> {
}
}
async _getUnionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
async _getUnionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
const exhausted = new Set<number>()
const seen = new Set()
@@ -222,7 +222,7 @@ export class FeedLoader<E extends TrustedEvent> {
feeds.map((feed: Feed, i: number) =>
this.getLoader(feed, {
onExhausted: () => exhausted.add(i),
onEvent: (event: E) => {
onEvent: (event: ExtensibleTrustedEvent) => {
if (!seen.has(event.id)) {
onEvent?.(event)
seen.add(event.id)
+2 -3
View File
@@ -1,10 +1,9 @@
import {Emitter} from '@welshman/lib'
import type {TrustedEvent} from '@welshman/util'
import {Relay, LOCAL_RELAY_URL} from '@welshman/util'
import type {Message} from '../Socket'
export class Local<T extends TrustedEvent> extends Emitter {
constructor(readonly relay: Relay<T>) {
export class Local extends Emitter {
constructor(readonly relay: Relay) {
super()
relay.on('*', this.onMessage)
+14 -14
View File
@@ -4,7 +4,7 @@ import type {Readable, Writable} from "svelte/store"
import {identity, batch, partition, first} from "@welshman/lib"
import type {Repository} from "@welshman/util"
import {matchFilters, getIdAndAddress, getIdFilters} from "@welshman/util"
import type {Filter, TrustedEvent} from "@welshman/util"
import type {Filter, ExtensibleTrustedEvent} from "@welshman/util"
export const getter = <T>(store: Readable<T>) => {
let value: T
@@ -68,8 +68,8 @@ export function withGetter<T>(store: Readable<T> | Writable<T>) {
export const throttled = <T>(delay: number, store: Readable<T>) =>
custom(set => store.subscribe(throttle(delay, set)))
export const createEventStore = <E extends TrustedEvent>(repository: Repository<E>) => {
let subs: Sub<E[]>[] = []
export const createEventStore = (repository: Repository) => {
let subs: Sub<ExtensibleTrustedEvent[]>[] = []
const onUpdate = throttle(300, () => {
const $events = repository.dump()
@@ -81,8 +81,8 @@ export const createEventStore = <E extends TrustedEvent>(repository: Repository<
return {
get: () => repository.dump(),
set: (events: E[]) => repository.load(events),
subscribe: (f: Sub<E[]>) => {
set: (events: ExtensibleTrustedEvent[]) => repository.load(events),
subscribe: (f: Sub<ExtensibleTrustedEvent[]>) => {
f(repository.dump())
subs.push(f)
@@ -102,7 +102,7 @@ export const createEventStore = <E extends TrustedEvent>(repository: Repository<
}
}
export const deriveEventsMapped = <E extends TrustedEvent, T>({
export const deriveEventsMapped = <T>({
filters,
repository,
eventToItem,
@@ -110,9 +110,9 @@ export const deriveEventsMapped = <E extends TrustedEvent, T>({
includeDeleted = false,
}: {
filters: Filter[]
repository: Repository<E>,
eventToItem: (event: E) => T
itemToEvent: (item: T) => E
repository: Repository,
eventToItem: (event: ExtensibleTrustedEvent) => T
itemToEvent: (item: T) => ExtensibleTrustedEvent
includeDeleted?: boolean
}) =>
custom<T[]>(setter => {
@@ -120,7 +120,7 @@ export const deriveEventsMapped = <E extends TrustedEvent, T>({
setter(data)
const onUpdate = batch(300, (updates: {added: E[]; removed: Set<string>}[]) => {
const onUpdate = batch(300, (updates: {added: ExtensibleTrustedEvent[]; removed: Set<string>}[]) => {
const removed = new Set()
const added = new Map()
@@ -170,15 +170,15 @@ export const deriveEventsMapped = <E extends TrustedEvent, T>({
return () => repository.off("update", onUpdate)
})
export const deriveEvents = <E extends TrustedEvent>(repository: Repository<E>, opts: {filters: Filter[]; includeDeleted?: boolean}) =>
deriveEventsMapped<E, E>({
export const deriveEvents = (repository: Repository, opts: {filters: Filter[]; includeDeleted?: boolean}) =>
deriveEventsMapped<ExtensibleTrustedEvent>({
...opts,
repository,
eventToItem: identity,
itemToEvent: identity,
})
export const deriveEvent = <E extends TrustedEvent>(repository: Repository<E>, idOrAddress: string) =>
export const deriveEvent = (repository: Repository, idOrAddress: string) =>
derived(
deriveEvents(repository, {
filters: getIdFilters([idOrAddress]),
@@ -187,7 +187,7 @@ export const deriveEvent = <E extends TrustedEvent>(repository: Repository<E>, i
first
)
export const deriveIsDeletedByAddress = <E extends TrustedEvent>(repository: Repository<E>, event: E) =>
export const deriveIsDeletedByAddress = (repository: Repository, event: ExtensibleTrustedEvent) =>
custom<boolean>(setter => {
setter(repository.isDeletedByAddress(event))
+1 -1
View File
@@ -32,6 +32,6 @@
},
"dependencies": {
"@welshman/lib": "0.0.14",
"nostr-tools": "^2.3.2"
"nostr-tools": "^2.7.2"
}
}
+7 -2
View File
@@ -5,10 +5,13 @@ import {Tags} from './Tags'
import {getAddress} from './Address'
import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds'
export type EventTemplate = {
kind: number
export type EventContent = {
tags: string[][]
content: string
}
export type EventTemplate = EventContent & {
kind: number
created_at: number
}
@@ -35,6 +38,8 @@ export type TrustedEvent = HashedEvent & {
[verifiedSymbol]?: boolean
}
export type ExtensibleTrustedEvent = TrustedEvent & Record<string, any>
export type CreateEventOpts = {
content?: string
tags?: string[][]
+5 -5
View File
@@ -2,7 +2,7 @@ import {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 {ExtensibleTrustedEvent} from './Events'
export const LOCAL_RELAY_URL = "local://welshman.relay"
@@ -48,22 +48,22 @@ export const normalizeRelayUrl = (url: string, {allowInsecure = false}: Normaliz
return prefix + url
}
export class Relay<T extends TrustedEvent> extends Emitter {
export class Relay extends Emitter {
subs = new Map<string, Filter[]>()
constructor(readonly repository: Repository<T>) {
constructor(readonly repository: Repository) {
super()
}
send(type: string, ...message: any[]) {
switch(type) {
case 'EVENT': return this.handleEVENT(message as [T])
case 'EVENT': return this.handleEVENT(message as [ExtensibleTrustedEvent])
case 'CLOSE': return this.handleCLOSE(message as [string])
case 'REQ': return this.handleREQ(message as [string, ...Filter[]])
}
}
handleEVENT([event]: [T]) {
handleEVENT([event]: [ExtensibleTrustedEvent]) {
this.repository.publish(event)
// Callers generally expect async relays
+21 -21
View File
@@ -4,19 +4,19 @@ import {EPOCH, matchFilter} from './Filters'
import {isReplaceable, isTrustedEvent} from './Events'
import {getAddress} from './Address'
import type {Filter} from './Filters'
import type {TrustedEvent} from './Events'
import type {ExtensibleTrustedEvent} from './Events'
export const DAY = 86400
const getDay = (ts: number) => Math.floor(ts / DAY)
export class Repository<T extends TrustedEvent> extends Emitter {
eventsById = new Map<string, T>()
eventsByWrap = new Map<string, T>()
eventsByAddress = new Map<string, T>()
eventsByTag = new Map<string, T[]>()
eventsByDay = new Map<number, T[]>()
eventsByAuthor = new Map<string, T[]>()
export class Repository extends Emitter {
eventsById = new Map<string, ExtensibleTrustedEvent>()
eventsByWrap = new Map<string, ExtensibleTrustedEvent>()
eventsByAddress = new Map<string, ExtensibleTrustedEvent>()
eventsByTag = new Map<string, ExtensibleTrustedEvent[]>()
eventsByDay = new Map<number, ExtensibleTrustedEvent[]>()
eventsByAuthor = new Map<string, ExtensibleTrustedEvent[]>()
deletes = new Map<string, number>()
// Dump/load/clear
@@ -25,7 +25,7 @@ export class Repository<T extends TrustedEvent> extends Emitter {
return Array.from(this.eventsById.values())
}
load = async (events: T[], chunkSize = 1000) => {
load = async (events: ExtensibleTrustedEvent[], chunkSize = 1000) => {
this.clear()
const added = []
@@ -69,7 +69,7 @@ export class Repository<T extends TrustedEvent> extends Emitter {
: this.eventsById.get(idOrAddress)
}
hasEvent = (event: T) => {
hasEvent = (event: ExtensibleTrustedEvent) => {
const duplicate = (
this.eventsById.get(event.id) ||
this.eventsByAddress.get(getAddress(event))
@@ -79,12 +79,12 @@ export class Repository<T extends TrustedEvent> extends Emitter {
}
query = (filters: Filter[], {includeDeleted = false} = {}) => {
const result: T[][] = []
const result: ExtensibleTrustedEvent[][] = []
for (let filter of filters) {
let events: T[] = Array.from(this.eventsById.values())
let events: ExtensibleTrustedEvent[] = Array.from(this.eventsById.values())
if (filter.ids) {
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as T[]
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as ExtensibleTrustedEvent[]
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<T extends TrustedEvent> extends Emitter {
}
}
const chunk: T[] = []
for (const event of sortBy((e: T) => -e.created_at, events)) {
const chunk: ExtensibleTrustedEvent[] = []
for (const event of sortBy((e: ExtensibleTrustedEvent) => -e.created_at, events)) {
if (filter.limit && chunk.length >= filter.limit) {
break
}
@@ -133,7 +133,7 @@ export class Repository<T extends TrustedEvent> extends Emitter {
return uniq(flatten(result))
}
publish = (event: T, {shouldNotify = true} = {}): boolean => {
publish = (event: ExtensibleTrustedEvent, {shouldNotify = true} = {}): boolean => {
if (!isTrustedEvent(event)) {
throw new Error("Invalid event published to Repository", event)
}
@@ -203,19 +203,19 @@ export class Repository<T extends TrustedEvent> extends Emitter {
return true
}
isDeletedByAddress = (event: T) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
isDeletedByAddress = (event: ExtensibleTrustedEvent) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
isDeletedById = (event: T) => (this.deletes.get(event.id) || 0) > event.created_at
isDeletedById = (event: ExtensibleTrustedEvent) => (this.deletes.get(event.id) || 0) > event.created_at
isDeleted = (event: T) => this.isDeletedByAddress(event) || this.isDeletedById(event)
isDeleted = (event: ExtensibleTrustedEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
// Utilities
_updateIndex<K>(m: Map<K, T[]>, k: K, e: T, duplicate?: T) {
_updateIndex<K>(m: Map<K, ExtensibleTrustedEvent[]>, k: K, e: ExtensibleTrustedEvent, duplicate?: ExtensibleTrustedEvent) {
let a = m.get(k) || []
if (duplicate) {
a = a.filter((x: T) => x !== duplicate)
a = a.filter((x: ExtensibleTrustedEvent) => x !== duplicate)
}
a.push(e)