Compare commits

...

1 Commits

Author SHA1 Message Date
fiatjaf
d46c5f947c fix tests, .seenOn() method for pools. 2023-02-09 15:11:09 -03:00
5 changed files with 43 additions and 20 deletions

View File

@@ -158,6 +158,11 @@ let events = await pool.list(relays, [{kinds: [0, 1]}])
let event = await pool.get(relays, { let event = await pool.get(relays, {
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'] ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
}) })
let relaysForEvent = pool.seenOn(
'44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
)
// relaysForEvent will be an array of URLs from relays a given event was seen on
``` ```
### Querying profile data from a NIP-05 address ### Querying profile data from a NIP-05 address

View File

@@ -1,6 +1,6 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "1.3.1", "version": "1.3.2",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -20,7 +20,12 @@ let relays = [
] ]
afterAll(async () => { afterAll(async () => {
await pool.close([...relays, 'wss://nostr-relay.untethr.me']) await pool.close([
...relays,
'wss://nostr-relay.untethr.me',
'wss://offchain.pub',
'wss://eden.nostr.land'
])
}) })
test('removing duplicates when querying', async () => { test('removing duplicates when querying', async () => {
@@ -120,4 +125,9 @@ test('list()', async () => {
.reduce((acc, n) => (acc.indexOf(n) !== -1 ? acc : [...acc, n]), []) .reduce((acc, n) => (acc.indexOf(n) !== -1 ? acc : [...acc, n]), [])
.length .length
) )
let relaysForAllEvents = events
.map(event => pool.seenOn(event.id))
.reduce((acc, n) => acc.concat(n), [])
expect(relaysForAllEvents.length).toBeGreaterThanOrEqual(events.length)
}) })

38
pool.ts
View File

@@ -6,10 +6,10 @@ import {SubscriptionOptions, Sub, Pub} from './relay'
export class SimplePool { export class SimplePool {
private _conn: {[url: string]: Relay} private _conn: {[url: string]: Relay}
private _seenOn: {[id: string]: Set<string>} = {} // a map of all events we've seen in each relay
constructor(defaultRelays: string[] = []) { constructor() {
this._conn = {} this._conn = {}
defaultRelays.forEach(this.ensureRelay)
} }
async close(relays: string[]): Promise<void> { async close(relays: string[]): Promise<void> {
@@ -37,7 +37,12 @@ export class SimplePool {
sub(relays: string[], filters: Filter[], opts?: SubscriptionOptions): Sub { sub(relays: string[], filters: Filter[], opts?: SubscriptionOptions): Sub {
let _knownIds: Set<string> = new Set() let _knownIds: Set<string> = new Set()
let modifiedOpts = opts || {} let modifiedOpts = opts || {}
modifiedOpts.alreadyHaveEvent = id => _knownIds.has(id) modifiedOpts.alreadyHaveEvent = (id, url) => {
let set = this._seenOn[id] || new Set()
set.add(url)
this._seenOn[id] = set
return _knownIds.has(id)
}
let subs: Sub[] = [] let subs: Sub[] = []
let eventListeners: Set<(event: Event) => void> = new Set() let eventListeners: Set<(event: Event) => void> = new Set()
@@ -47,9 +52,7 @@ export class SimplePool {
let eoseSent = false let eoseSent = false
let eoseTimeout = setTimeout(() => { let eoseTimeout = setTimeout(() => {
eoseSent = true eoseSent = true
for (let cb of eoseListeners.values()) { for (let cb of eoseListeners.values()) cb()
cb()
}
}, 2400) }, 2400)
relays.forEach(async relay => { relays.forEach(async relay => {
@@ -58,9 +61,7 @@ export class SimplePool {
let s = r.sub(filters, modifiedOpts) let s = r.sub(filters, modifiedOpts)
s.on('event', (event: Event) => { s.on('event', (event: Event) => {
_knownIds.add(event.id as string) _knownIds.add(event.id as string)
for (let cb of eventListeners.values()) { for (let cb of eventListeners.values()) cb(event)
cb(event)
}
}) })
s.on('eose', () => { s.on('eose', () => {
if (eoseSent) return if (eoseSent) return
@@ -68,9 +69,7 @@ export class SimplePool {
eosesMissing-- eosesMissing--
if (eosesMissing === 0) { if (eosesMissing === 0) {
clearTimeout(eoseTimeout) clearTimeout(eoseTimeout)
for (let cb of eoseListeners.values()) { for (let cb of eoseListeners.values()) cb()
cb()
}
} }
}) })
subs.push(s) subs.push(s)
@@ -85,9 +84,14 @@ export class SimplePool {
subs.forEach(sub => sub.unsub()) subs.forEach(sub => sub.unsub())
}, },
on(type, cb) { on(type, cb) {
if (type === 'event') { switch (type) {
eventListeners.add(cb) case 'event':
} else if (type === 'eose') eoseListeners.add(cb) eventListeners.add(cb)
break
case 'eose':
eoseListeners.add(cb)
break
}
}, },
off(type, cb) { off(type, cb) {
if (type === 'event') { if (type === 'event') {
@@ -147,6 +151,10 @@ export class SimplePool {
return s return s
}) })
} }
seenOn(id: string): string[] {
return Array.from(this._seenOn[id]?.values?.() || [])
}
} }
function badPub(relay: string): Pub { function badPub(relay: string): Pub {

View File

@@ -30,9 +30,9 @@ export type Sub = {
} }
export type SubscriptionOptions = { export type SubscriptionOptions = {
skipVerification?: boolean
alreadyHaveEvent?: null | ((id: string) => boolean)
id?: string id?: string
skipVerification?: boolean
alreadyHaveEvent?: null | ((id: string, relay: string) => boolean)
} }
export function relayInit(url: string): Relay { export function relayInit(url: string): Relay {
@@ -112,7 +112,7 @@ export function relayInit(url: string): Relay {
if ( if (
so && so &&
so.alreadyHaveEvent && so.alreadyHaveEvent &&
so.alreadyHaveEvent(getHex64(json, 'id')) so.alreadyHaveEvent(getHex64(json, 'id'), url)
) { ) {
return return
} }