Add relay package
This commit is contained in:
@@ -1,253 +0,0 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {describe, it, expect, beforeEach, vi, afterEach} from "vitest"
|
||||
import {
|
||||
Relay,
|
||||
normalizeRelayUrl,
|
||||
isRelayUrl,
|
||||
isOnionUrl,
|
||||
isLocalUrl,
|
||||
isIPAddress,
|
||||
isShareableRelayUrl,
|
||||
displayRelayUrl,
|
||||
displayRelayProfile,
|
||||
} from "../src/Relay"
|
||||
import {Repository} from "../src/Repository"
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
|
||||
describe("Relay", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
// Realistic Nostr data
|
||||
const pubkey = "ee".repeat(32)
|
||||
const id = "ff".repeat(32)
|
||||
const sig = "00".repeat(64)
|
||||
const currentTime = now()
|
||||
const onionUrl = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion"
|
||||
|
||||
const createEvent = (overrides = {}): TrustedEvent => ({
|
||||
id: id,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: 1,
|
||||
tags: [],
|
||||
content: "Hello Nostr!",
|
||||
sig: sig,
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("URL utilities", () => {
|
||||
describe("isRelayUrl", () => {
|
||||
it("should validate proper relay URLs", () => {
|
||||
expect(isRelayUrl("wss://relay.example.com")).toBe(true)
|
||||
expect(isRelayUrl("ws://relay.example.com")).toBe(true)
|
||||
expect(isRelayUrl("relay.example.com")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject invalid URLs", () => {
|
||||
expect(isRelayUrl("http://relay.example.com")).toBe(false)
|
||||
expect(isRelayUrl("not-a-url")).toBe(false)
|
||||
expect(isRelayUrl("ws:\\example.com\\path\\to\\file.ext")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isOnionUrl", () => {
|
||||
it("should validate onion URLs", () => {
|
||||
expect(isOnionUrl(onionUrl)).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject non-onion URLs", () => {
|
||||
expect(isOnionUrl("wss://relay.example.com")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isLocalUrl", () => {
|
||||
it("should validate local URLs", () => {
|
||||
expect(isLocalUrl("wss://relay.local")).toBe(true)
|
||||
expect(isLocalUrl("ws://localhost:8080")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject non-local URLs", () => {
|
||||
expect(isLocalUrl("wss://relay.example.com")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isIPAddress", () => {
|
||||
it("should validate IP addresses", () => {
|
||||
expect(isIPAddress("wss://192.168.1.1")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject domains", () => {
|
||||
expect(isIPAddress("wss://relay.example.com")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isShareableRelayUrl", () => {
|
||||
it("should validate shareable URLs", () => {
|
||||
expect(isShareableRelayUrl("wss://relay.example.com")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject local URLs", () => {
|
||||
expect(isShareableRelayUrl("wss://relay.local")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("normalizeRelayUrl", () => {
|
||||
it("should normalize URLs consistently", () => {
|
||||
expect(normalizeRelayUrl("relay.example.com")).toBe("wss://relay.example.com/")
|
||||
expect(normalizeRelayUrl("wss://RELAY.EXAMPLE.COM")).toBe("wss://relay.example.com/")
|
||||
})
|
||||
|
||||
it("should handle onion URLs", () => {
|
||||
expect(normalizeRelayUrl(onionUrl)).toBe(`ws://${onionUrl}/`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayRelayUrl", () => {
|
||||
it("should format URLs for display", () => {
|
||||
expect(displayRelayUrl("wss://relay.example.com/")).toBe("relay.example.com")
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayRelayProfile", () => {
|
||||
it("should display profile name when available", () => {
|
||||
const profile = {url: "wss://relay.example.com", name: "Test Relay"}
|
||||
expect(displayRelayProfile(profile)).toBe("Test Relay")
|
||||
})
|
||||
|
||||
it("should use fallback when no name", () => {
|
||||
const profile = {url: "wss://relay.example.com"}
|
||||
expect(displayRelayProfile(profile, "Fallback")).toBe("Fallback")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("Relay class", () => {
|
||||
let relay: Relay
|
||||
let repository: Repository<TrustedEvent>
|
||||
|
||||
beforeEach(() => {
|
||||
repository = new Repository<TrustedEvent>()
|
||||
relay = new Relay(repository)
|
||||
})
|
||||
|
||||
describe("EVENT handling", () => {
|
||||
it("should publish events to repository", async () => {
|
||||
const event = createEvent()
|
||||
const publishSpy = vi.spyOn(repository, "publish")
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
expect(publishSpy).toHaveBeenCalledWith(event)
|
||||
|
||||
// Should emit OK
|
||||
const okHandler = vi.fn()
|
||||
relay.on("OK", okHandler)
|
||||
|
||||
// Wait for async operations
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(okHandler).toHaveBeenCalledWith(event.id, true, "")
|
||||
})
|
||||
|
||||
it("should notify matching subscribers", async () => {
|
||||
const event = createEvent()
|
||||
const subId = "test-sub"
|
||||
const filter = {kinds: [1]}
|
||||
|
||||
relay.send("REQ", subId, filter)
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).toHaveBeenCalledWith(subId, event)
|
||||
})
|
||||
|
||||
it("should not notify for deleted events", async () => {
|
||||
const event = createEvent()
|
||||
repository.removeEvent(event.id)
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("REQ handling", () => {
|
||||
it("should handle subscription requests", async () => {
|
||||
const event = createEvent()
|
||||
repository.publish(event)
|
||||
|
||||
const subId = "test-sub"
|
||||
const filter = {kinds: [1]}
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
const eoseHandler = vi.fn()
|
||||
|
||||
relay.on("EVENT", eventHandler)
|
||||
relay.on("EOSE", eoseHandler)
|
||||
|
||||
relay.send("REQ", subId, filter)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).toHaveBeenCalledWith(subId, event)
|
||||
expect(eoseHandler).toHaveBeenCalledWith(subId)
|
||||
})
|
||||
|
||||
it("should handle multiple filters", async () => {
|
||||
const event1 = createEvent({kind: 1})
|
||||
const event2 = createEvent({kind: 2, id: "ee".repeat(31)})
|
||||
repository.publish(event1)
|
||||
repository.publish(event2)
|
||||
|
||||
const subId = "test-sub"
|
||||
const filters = [{kinds: [1]}, {kinds: [2]}]
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("REQ", subId, ...filters)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("CLOSE handling", () => {
|
||||
it("should close subscriptions", async () => {
|
||||
const subId = "test-sub"
|
||||
relay.send("REQ", subId, {kinds: [1]})
|
||||
relay.send("CLOSE", subId)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
const event = createEvent()
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,337 +0,0 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {getAddress} from "@welshman/util"
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {Repository} from "../src/Repository"
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
import {DELETE, MUTES} from "../src/Kinds"
|
||||
|
||||
describe("Repository", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
// Realistic Nostr data
|
||||
const pubkey = "ee".repeat(32)
|
||||
const id = "ff".repeat(32)
|
||||
const sig = "00".repeat(64)
|
||||
const currentTime = now()
|
||||
|
||||
const createEvent = (overrides = {}): TrustedEvent => ({
|
||||
id: id,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: 1,
|
||||
tags: [],
|
||||
content: "Hello Nostr!",
|
||||
sig: sig,
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("basic operations", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should publish and retrieve events", () => {
|
||||
const event = createEvent()
|
||||
expect(repo.publish(event)).toBe(true)
|
||||
expect(repo.getEvent(event.id)).toEqual(event)
|
||||
})
|
||||
|
||||
it("should not publish invalid events", () => {
|
||||
const invalidEvent = {} as TrustedEvent
|
||||
const result = repo.publish(invalidEvent)
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
|
||||
it("should handle duplicate events", () => {
|
||||
const event = createEvent()
|
||||
expect(repo.publish(event)).toBe(true)
|
||||
expect(repo.publish(event)).toBe(false)
|
||||
})
|
||||
|
||||
it("should check if events exist", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
expect(repo.hasEvent(event)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("replaceable events", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should handle replaceable events", () => {
|
||||
const event1 = createEvent({kind: MUTES, created_at: currentTime - 100})
|
||||
const event2 = createEvent({kind: MUTES, created_at: currentTime, id: "ee".repeat(32)})
|
||||
|
||||
const address1 = getAddress(event1)
|
||||
const address2 = getAddress(event2)
|
||||
|
||||
repo.publish(event1)
|
||||
repo.publish(event2)
|
||||
|
||||
expect(repo.getEvent(event1.id)).toEqual(event1)
|
||||
expect(repo.getEvent(address1)).toEqual(event2)
|
||||
expect(repo.getEvent(event2.id)).toEqual(event2)
|
||||
expect(repo.getEvent(address2)).toEqual(event2)
|
||||
|
||||
const event3 = createEvent({kind: MUTES, created_at: currentTime - 50, id: "dd".repeat(32)})
|
||||
|
||||
repo.publish(event3)
|
||||
|
||||
expect(repo.getEvent(event3.id)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should not replace with older events", () => {
|
||||
const event1 = createEvent({kind: MUTES, created_at: currentTime})
|
||||
const event2 = createEvent({kind: MUTES, created_at: currentTime - 100})
|
||||
|
||||
repo.publish(event1)
|
||||
repo.publish(event2)
|
||||
|
||||
expect(repo.getEvent(event1.id)).toEqual(event1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete events", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should handle delete events", () => {
|
||||
const event = createEvent()
|
||||
const deleteEvent = createEvent({
|
||||
id: "ee".repeat(32),
|
||||
kind: DELETE,
|
||||
tags: [["e", event.id]],
|
||||
created_at: currentTime + 100,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.publish(deleteEvent)
|
||||
|
||||
expect(repo.isDeleted(event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should handle delete by address", () => {
|
||||
const event = createEvent({kind: MUTES})
|
||||
const deleteEvent = createEvent({
|
||||
id: "ee".repeat(32),
|
||||
kind: DELETE,
|
||||
tags: [["a", `10000:${event.pubkey}:`]],
|
||||
created_at: currentTime + 100,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.publish(deleteEvent)
|
||||
|
||||
expect(repo.isDeletedByAddress(event)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("query operations", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should throw on invalid queries", () => {
|
||||
expect(() => repo.query([{limit: 10}], {shouldSort: false})).toThrow()
|
||||
})
|
||||
|
||||
it("should query by ids", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{ids: [event.id]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by authors", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{authors: [event.pubkey]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by kinds", () => {
|
||||
const event = createEvent({kind: 1})
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{kinds: [1]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by tags", () => {
|
||||
const event = createEvent({tags: [["p", pubkey]]})
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{"#p": [pubkey]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by time range", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([
|
||||
{
|
||||
since: currentTime - 3600,
|
||||
until: currentTime + 3600,
|
||||
},
|
||||
])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should handle multiple filters", () => {
|
||||
const event = createEvent({kind: 1})
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{kinds: [1]}, {authors: [event.pubkey]}])
|
||||
expect(results).toHaveLength(1)
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should respect limit parameter", () => {
|
||||
const events = [
|
||||
createEvent({id: id + "1", created_at: currentTime}),
|
||||
createEvent({id: id + "2", created_at: currentTime - 100}),
|
||||
]
|
||||
|
||||
events.forEach(e => repo.publish(e))
|
||||
|
||||
const results = repo.query([{limit: 1}])
|
||||
expect(results).toHaveLength(1)
|
||||
expect(results[0]).toEqual(events[0]) // Most recent event
|
||||
})
|
||||
|
||||
it("should not return deleted events", () => {
|
||||
const event = createEvent()
|
||||
const deleteEvent = createEvent({
|
||||
id: "ee".repeat(32),
|
||||
kind: DELETE,
|
||||
tags: [["e", event.id]],
|
||||
created_at: currentTime + 100,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.publish(deleteEvent)
|
||||
|
||||
const results = repo.query([{kinds: [1]}])
|
||||
expect(results).not.toContain(event)
|
||||
})
|
||||
})
|
||||
|
||||
describe("dump and load", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should dump all events", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const dumped = repo.dump()
|
||||
expect(dumped).toContain(event)
|
||||
})
|
||||
|
||||
it("should load events", () => {
|
||||
const event = createEvent()
|
||||
repo.load([event])
|
||||
|
||||
expect(repo.getEvent(event.id)).toEqual(event)
|
||||
})
|
||||
|
||||
it("should handle chunked loading", () => {
|
||||
const events = Array.from({length: 1500}, (_, i) => createEvent({id: id.slice(0, -1) + i}))
|
||||
|
||||
repo.load(events, 500)
|
||||
expect(repo.dump()).toHaveLength(1500)
|
||||
})
|
||||
|
||||
it("should emit update events", () => {
|
||||
const event = createEvent()
|
||||
const updateHandler = vi.fn()
|
||||
|
||||
repo.on("update", updateHandler)
|
||||
repo.load([event])
|
||||
|
||||
expect(updateHandler).toHaveBeenCalledWith({
|
||||
added: [event],
|
||||
removed: new Set(),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("wrapped events", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should handle wrapped events", () => {
|
||||
const wrapped = createEvent()
|
||||
const event = createEvent({
|
||||
wrap: wrapped,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
expect(repo.eventsByWrap.get(wrapped.id)).toEqual(event)
|
||||
})
|
||||
})
|
||||
|
||||
describe("event removal", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should remove events", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
repo.removeEvent(event.id)
|
||||
|
||||
expect(repo.getEvent(event.id)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should remove wrapped events", () => {
|
||||
const wrapped = createEvent()
|
||||
const event = createEvent({
|
||||
wrap: wrapped,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.removeEvent(event.id)
|
||||
|
||||
expect(repo.eventsByWrap.get(wrapped.id)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should emit update on removal", () => {
|
||||
const event = createEvent()
|
||||
const updateHandler = vi.fn()
|
||||
|
||||
repo.on("update", updateHandler)
|
||||
repo.publish(event)
|
||||
repo.removeEvent(event.id)
|
||||
|
||||
expect(updateHandler).toHaveBeenLastCalledWith({
|
||||
added: [],
|
||||
removed: [event.id],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,15 +1,7 @@
|
||||
import {last, Emitter, normalizeUrl, sleep, stripProtocol} from "@welshman/lib"
|
||||
import {matchFilters} from "./Filters.js"
|
||||
import type {Repository} from "./Repository.js"
|
||||
import type {Filter} from "./Filters.js"
|
||||
import type {HashedEvent, TrustedEvent} from "./Events.js"
|
||||
import {last, normalizeUrl, stripProtocol} from "@welshman/lib"
|
||||
|
||||
// Constants and types
|
||||
|
||||
export const LOCAL_RELAY_URL = "local://welshman.relay/"
|
||||
|
||||
export const BOGUS_RELAY_URL = "bogus://welshman.relay/"
|
||||
|
||||
export type RelayProfile = {
|
||||
url: string
|
||||
icon?: string
|
||||
@@ -83,58 +75,3 @@ export const displayRelayUrl = (url: string) => last(url.split("://")).replace(/
|
||||
|
||||
export const displayRelayProfile = (profile?: RelayProfile, fallback = "") =>
|
||||
profile?.name || fallback
|
||||
|
||||
// In-memory relay implementation backed by Repository
|
||||
|
||||
export class Relay<E extends HashedEvent = TrustedEvent> extends Emitter {
|
||||
subs = new Map<string, Filter[]>()
|
||||
|
||||
constructor(readonly repository: Repository<E>) {
|
||||
super()
|
||||
}
|
||||
|
||||
send(type: string, ...message: any[]) {
|
||||
switch (type) {
|
||||
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]: [E]) {
|
||||
this.repository.publish(event)
|
||||
|
||||
// Callers generally expect async relays
|
||||
void sleep(1).then(() => {
|
||||
this.emit("OK", event.id, true, "")
|
||||
|
||||
if (!this.repository.isDeleted(event)) {
|
||||
for (const [subId, filters] of this.subs.entries()) {
|
||||
if (matchFilters(filters, event)) {
|
||||
this.emit("EVENT", subId, event)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
handleCLOSE([subId]: [string]) {
|
||||
this.subs.delete(subId)
|
||||
}
|
||||
|
||||
handleREQ([subId, ...filters]: [string, ...Filter[]]) {
|
||||
this.subs.set(subId, filters)
|
||||
|
||||
// Callers generally expect async relays
|
||||
void sleep(1).then(() => {
|
||||
for (const event of this.repository.query(filters)) {
|
||||
this.emit("EVENT", subId, event)
|
||||
}
|
||||
|
||||
this.emit("EOSE", subId)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,348 +0,0 @@
|
||||
import {flatten, pluck, Emitter, sortBy, inc, chunk, uniq, omit, now, range} from "@welshman/lib"
|
||||
import {DELETE} from "./Kinds.js"
|
||||
import {EPOCH, matchFilter} from "./Filters.js"
|
||||
import {isReplaceable, isUnwrappedEvent} from "./Events.js"
|
||||
import {getAddress} from "./Address.js"
|
||||
import type {Filter} from "./Filters.js"
|
||||
import type {TrustedEvent, HashedEvent} from "./Events.js"
|
||||
|
||||
export const DAY = 86400
|
||||
|
||||
const getDay = (ts: number) => Math.floor(ts / DAY)
|
||||
|
||||
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[]>()
|
||||
eventsByKind = new Map<number, E[]>()
|
||||
deletes = new Map<string, number>()
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
this.setMaxListeners(100)
|
||||
}
|
||||
|
||||
// Dump/load/clear
|
||||
|
||||
dump = () => {
|
||||
return Array.from(this.eventsById.values())
|
||||
}
|
||||
|
||||
load = (events: E[], chunkSize = 1000) => {
|
||||
const stale = new Set(this.eventsById.keys())
|
||||
|
||||
this.eventsById.clear()
|
||||
this.eventsByWrap.clear()
|
||||
this.eventsByAddress.clear()
|
||||
this.eventsByTag.clear()
|
||||
this.eventsByDay.clear()
|
||||
this.eventsByAuthor.clear()
|
||||
this.eventsByKind.clear()
|
||||
this.deletes.clear()
|
||||
|
||||
const added = []
|
||||
|
||||
for (const eventsChunk of chunk(chunkSize, events)) {
|
||||
for (const event of eventsChunk) {
|
||||
if (this.publish(event, {shouldNotify: false})) {
|
||||
// Don't send duplicate events to subscribers
|
||||
if (!stale.has(event.id)) {
|
||||
added.push(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const removed = new Set<string>()
|
||||
|
||||
// Anything we had before clearing the repository has been removed
|
||||
for (const id of stale) {
|
||||
if (!this.eventsById.has(id)) {
|
||||
removed.add(id)
|
||||
}
|
||||
}
|
||||
|
||||
// Anything removed via delete or replace has been removed
|
||||
for (const id of this.deletes.keys()) {
|
||||
removed.add(id)
|
||||
}
|
||||
|
||||
this.emit("update", {added, removed})
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
getEvent = (idOrAddress: string) => {
|
||||
return idOrAddress.includes(":")
|
||||
? this.eventsByAddress.get(idOrAddress)
|
||||
: this.eventsById.get(idOrAddress)
|
||||
}
|
||||
|
||||
hasEvent = (event: E) => {
|
||||
const duplicate = this.eventsById.get(event.id) || this.eventsByAddress.get(getAddress(event))
|
||||
|
||||
return duplicate && duplicate.created_at >= event.created_at
|
||||
}
|
||||
|
||||
removeEvent = (idOrAddress: string) => {
|
||||
const event = this.getEvent(idOrAddress)
|
||||
|
||||
if (event) {
|
||||
this.eventsById.delete(event.id)
|
||||
|
||||
if (isUnwrappedEvent(event)) {
|
||||
this.eventsByWrap.delete(event.wrap.id)
|
||||
}
|
||||
|
||||
this.eventsByAddress.delete(getAddress(event))
|
||||
|
||||
for (const [k, v] of event.tags) {
|
||||
if (k.length === 1) {
|
||||
this._updateIndex(this.eventsByTag, `${k}:${v}`, undefined, event)
|
||||
}
|
||||
}
|
||||
|
||||
this._updateIndex(this.eventsByDay, getDay(event.created_at), undefined, event)
|
||||
this._updateIndex(this.eventsByAuthor, event.pubkey, undefined, event)
|
||||
this._updateIndex(this.eventsByKind, event.kind, undefined, event)
|
||||
|
||||
this.emit("update", {added: [], removed: [event.id]})
|
||||
}
|
||||
}
|
||||
|
||||
query = (filters: Filter[], {includeDeleted = false, shouldSort = true} = {}) => {
|
||||
const result: E[][] = []
|
||||
for (const originalFilter of filters) {
|
||||
if (originalFilter.limit !== undefined && !shouldSort) {
|
||||
throw new Error("Unable to skip sorting if limit is defined")
|
||||
}
|
||||
|
||||
// Attempt to fulfill the query using one of our indexes. Fall back to all events.
|
||||
const applied = this._applyAnyFilter(originalFilter)
|
||||
const filter = applied?.filter || originalFilter
|
||||
const events = applied ? this._getEvents(applied!.ids) : this.dump()
|
||||
const sorted = this._sortEvents(shouldSort && Boolean(filter.limit), events)
|
||||
|
||||
const chunk: E[] = []
|
||||
for (const event of sorted) {
|
||||
if (filter.limit && chunk.length >= filter.limit) {
|
||||
break
|
||||
}
|
||||
|
||||
if (!includeDeleted && this.isDeleted(event)) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (matchFilter(filter, event)) {
|
||||
chunk.push(event)
|
||||
}
|
||||
}
|
||||
|
||||
result.push(chunk)
|
||||
}
|
||||
|
||||
// Only re-sort if we had multiple filters, or if our single filter wasn't sorted
|
||||
const shouldSortAll = shouldSort && (filters.length > 1 || !filters[0]?.limit)
|
||||
|
||||
return this._sortEvents(shouldSortAll, uniq(flatten(result)))
|
||||
}
|
||||
|
||||
publish = (event: E, {shouldNotify = true} = {}): boolean => {
|
||||
if (!event?.id) {
|
||||
console.warn("Attempted to publish invalid event to repository", event)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
const removed = new Set<string>()
|
||||
const address = getAddress(event)
|
||||
const duplicate = this.eventsByAddress.get(address)
|
||||
|
||||
if (duplicate) {
|
||||
// If our event is younger than the duplicate, we're done
|
||||
if (event.created_at <= duplicate.created_at) {
|
||||
return false
|
||||
}
|
||||
|
||||
// If our event is newer than what it's replacing, delete the old version
|
||||
this.deletes.set(duplicate.id, event.created_at)
|
||||
|
||||
// Notify listeners that it's been removed
|
||||
removed.add(duplicate.id)
|
||||
}
|
||||
|
||||
// Add our new event by id
|
||||
this.eventsById.set(event.id, event)
|
||||
|
||||
// Add our new event by address
|
||||
if (isReplaceable(event)) {
|
||||
this.eventsByAddress.set(address, event)
|
||||
}
|
||||
|
||||
// Save wrapper index
|
||||
if (isUnwrappedEvent(event)) {
|
||||
this.eventsByWrap.set(event.wrap.id, event)
|
||||
}
|
||||
|
||||
// Update our timestamp and author indexes
|
||||
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate)
|
||||
this._updateIndex(this.eventsByAuthor, event.pubkey, event, duplicate)
|
||||
this._updateIndex(this.eventsByKind, event.kind, event, duplicate)
|
||||
|
||||
// Update our tag indexes
|
||||
for (const tag of event.tags) {
|
||||
if (tag[0]?.length === 1) {
|
||||
this._updateIndex(this.eventsByTag, tag.slice(0, 2).join(":"), event, duplicate)
|
||||
|
||||
// If this is a delete event, the tag value is an id or address. Track when it was
|
||||
// deleted so that replaceables can be restored.
|
||||
if (event.kind === DELETE && ["a", "e"].includes(tag[0]) && tag[1]) {
|
||||
this.deletes.set(tag[1], Math.max(event.created_at, this.deletes.get(tag[1]) || 0))
|
||||
|
||||
const deletedEvent = this.getEvent(tag[1])
|
||||
|
||||
if (deletedEvent && this.isDeleted(deletedEvent)) {
|
||||
removed.add(deletedEvent.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldNotify) {
|
||||
this.emit("update", {added: [event], removed})
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
isDeletedByAddress = (event: E) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||
|
||||
isDeletedById = (event: E) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||
|
||||
isDeleted = (event: E) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||
|
||||
// Utilities
|
||||
|
||||
_sortEvents = (shouldSort: boolean, events: E[]) =>
|
||||
shouldSort ? sortBy(e => -e.created_at, events) : events
|
||||
|
||||
_updateIndex = <K>(m: Map<K, E[]>, k: K, add?: E, remove?: E) => {
|
||||
let a = m.get(k) || []
|
||||
|
||||
if (remove) {
|
||||
a = a.filter((x: E) => x !== remove)
|
||||
}
|
||||
|
||||
if (add) {
|
||||
a.push(add)
|
||||
}
|
||||
|
||||
m.set(k, a)
|
||||
}
|
||||
|
||||
_getEvents = (ids: Iterable<string>) => {
|
||||
const events: E[] = []
|
||||
|
||||
for (const id of ids) {
|
||||
const event = this.eventsById.get(id)
|
||||
|
||||
if (event) {
|
||||
events.push(event)
|
||||
}
|
||||
}
|
||||
|
||||
return events
|
||||
}
|
||||
|
||||
_applyIdsFilter = (filter: Filter) => {
|
||||
if (!filter.ids) return undefined
|
||||
|
||||
return {
|
||||
filter: omit(["ids"], filter),
|
||||
ids: new Set(filter.ids),
|
||||
}
|
||||
}
|
||||
|
||||
_applyAuthorsFilter = (filter: Filter) => {
|
||||
if (!filter.authors) return undefined
|
||||
|
||||
const events = filter.authors.flatMap(pubkey => this.eventsByAuthor.get(pubkey) || [])
|
||||
|
||||
return {
|
||||
filter: omit(["authors"], filter),
|
||||
ids: new Set(pluck<string>("id", events)),
|
||||
}
|
||||
}
|
||||
|
||||
_applyTagsFilter = (filter: Filter) => {
|
||||
for (const [k, values] of Object.entries(filter)) {
|
||||
if (!k.startsWith("#") || k.length !== 2) {
|
||||
continue
|
||||
}
|
||||
|
||||
const ids = new Set<string>()
|
||||
|
||||
for (const v of values as string[]) {
|
||||
for (const event of this.eventsByTag.get(`${k[1]}:${v}`) || []) {
|
||||
ids.add(event.id)
|
||||
}
|
||||
}
|
||||
|
||||
return {filter: omit([k], filter), ids}
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
_applyKindsFilter = (filter: Filter) => {
|
||||
if (!filter.kinds) return undefined
|
||||
|
||||
const events = filter.kinds.flatMap(kind => this.eventsByKind.get(kind) || [])
|
||||
|
||||
return {
|
||||
filter: omit(["kinds"], filter),
|
||||
ids: new Set(pluck<string>("id", events)),
|
||||
}
|
||||
}
|
||||
|
||||
_applyDaysFilter = (filter: Filter) => {
|
||||
if (!filter.since && !filter.until) return undefined
|
||||
|
||||
const sinceDay = getDay(filter.since || EPOCH)
|
||||
const untilDay = getDay(filter.until || now())
|
||||
const days = Array.from(range(sinceDay, inc(untilDay)))
|
||||
const events = days.flatMap((day: number) => this.eventsByDay.get(day) || [])
|
||||
const ids = new Set(pluck<string>("id", events))
|
||||
|
||||
return {filter, ids}
|
||||
}
|
||||
|
||||
_applyAnyFilter = (filter: Filter) => {
|
||||
const matchers = [
|
||||
this._applyIdsFilter,
|
||||
this._applyAuthorsFilter,
|
||||
this._applyTagsFilter,
|
||||
this._applyKindsFilter,
|
||||
this._applyDaysFilter,
|
||||
]
|
||||
|
||||
for (const matcher of matchers) {
|
||||
const result = matcher(filter)
|
||||
|
||||
if (result) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,5 @@ export * from "./Links.js"
|
||||
export * from "./List.js"
|
||||
export * from "./Profile.js"
|
||||
export * from "./Relay.js"
|
||||
export * from "./Repository.js"
|
||||
export * from "./Tags.js"
|
||||
export * from "./Zaps.js"
|
||||
|
||||
Reference in New Issue
Block a user