mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ff97b5488 |
12
README.md
12
README.md
@@ -111,6 +111,11 @@ pub.on('failed', reason => {
|
|||||||
console.log(`failed to publish to ${relay.url}: ${reason}`)
|
console.log(`failed to publish to ${relay.url}: ${reason}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let events = await relay.list([{kinds: [0, 1]}])
|
||||||
|
let event = await relay.get({
|
||||||
|
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
|
})
|
||||||
|
|
||||||
await relay.close()
|
await relay.close()
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -147,12 +152,17 @@ subs.forEach(sub =>
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
let pubs = pool.publish(newEvent)
|
let pubs = pool.publish(relays, newEvent)
|
||||||
pubs.forEach(pub =>
|
pubs.forEach(pub =>
|
||||||
pub.on('ok', () => {
|
pub.on('ok', () => {
|
||||||
// ...
|
// ...
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let events = await pool.list(relays, [{kinds: [0, 1]}])
|
||||||
|
let event = await pool.get(relays, {
|
||||||
|
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### Querying profile data from a NIP-05 address
|
### Querying profile data from a NIP-05 address
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.2.4",
|
"version": "1.3.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
62
pool.ts
62
pool.ts
@@ -37,6 +37,68 @@ export class SimplePool {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get(
|
||||||
|
relays: string[],
|
||||||
|
filter: Filter,
|
||||||
|
opts?: SubscriptionOptions
|
||||||
|
): Promise<Event | null> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
let subs = this.sub(relays, [filter], opts)
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
subs.forEach(sub => sub.unsub(), 1500)
|
||||||
|
resolve(null)
|
||||||
|
})
|
||||||
|
subs.forEach(sub => {
|
||||||
|
sub.on('event', (event: Event) => {
|
||||||
|
resolve(event)
|
||||||
|
clearTimeout(timeout)
|
||||||
|
subs.forEach(sub => {
|
||||||
|
sub.unsub()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
list(
|
||||||
|
relays: string[],
|
||||||
|
filters: Filter[],
|
||||||
|
opts?: SubscriptionOptions
|
||||||
|
): Promise<Event[]> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
let _knownIds: Set<string> = new Set()
|
||||||
|
let modifiedOpts = opts || {}
|
||||||
|
modifiedOpts.alreadyHaveEvent = id => _knownIds.has(id)
|
||||||
|
|
||||||
|
let events: Event[] = []
|
||||||
|
|
||||||
|
let subs = this.sub(relays, filters, modifiedOpts)
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
subs.forEach(sub => sub.unsub(), 1500)
|
||||||
|
resolve(events)
|
||||||
|
})
|
||||||
|
|
||||||
|
let pendingEoses = relays.length
|
||||||
|
|
||||||
|
subs.forEach(sub => {
|
||||||
|
sub.on('event', (event: Event) => {
|
||||||
|
events.push(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
sub.on('eose', () => {
|
||||||
|
pendingEoses--
|
||||||
|
if (pendingEoses === 0) {
|
||||||
|
resolve(events)
|
||||||
|
clearTimeout(timeout)
|
||||||
|
subs.forEach(sub => {
|
||||||
|
sub.unsub()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
publish(relays: string[], event: Event): Pub[] {
|
publish(relays: string[], event: Event): Pub[] {
|
||||||
return relays.map(relay => {
|
return relays.map(relay => {
|
||||||
let r = this._conn[relay]
|
let r = this._conn[relay]
|
||||||
|
|||||||
32
relay.ts
32
relay.ts
@@ -12,6 +12,8 @@ export type Relay = {
|
|||||||
connect: () => Promise<void>
|
connect: () => Promise<void>
|
||||||
close: () => Promise<void>
|
close: () => Promise<void>
|
||||||
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
||||||
|
list: (filters: Filter[], opts?: SubscriptionOptions) => Promise<Event[]>
|
||||||
|
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>
|
||||||
publish: (event: Event) => Pub
|
publish: (event: Event) => Pub
|
||||||
on: (type: RelayEvent, cb: any) => void
|
on: (type: RelayEvent, cb: any) => void
|
||||||
off: (type: RelayEvent, cb: any) => void
|
off: (type: RelayEvent, cb: any) => void
|
||||||
@@ -231,6 +233,36 @@ export function relayInit(url: string): Relay {
|
|||||||
let index = listeners[type].indexOf(cb)
|
let index = listeners[type].indexOf(cb)
|
||||||
if (index !== -1) listeners[type].splice(index, 1)
|
if (index !== -1) listeners[type].splice(index, 1)
|
||||||
},
|
},
|
||||||
|
list: (filters: Filter[], opts?: SubscriptionOptions): Promise<Event[]> =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
let s = sub(filters, opts)
|
||||||
|
let events: Event[] = []
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
s.unsub()
|
||||||
|
resolve(events)
|
||||||
|
}, 1500)
|
||||||
|
s.on('eose', () => {
|
||||||
|
s.unsub()
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve(events)
|
||||||
|
})
|
||||||
|
s.on('event', (event: Event) => {
|
||||||
|
events.push(event)
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
get: (filter: Filter, opts?: SubscriptionOptions): Promise<Event | null> =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
let s = sub([filter], opts)
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
s.unsub()
|
||||||
|
resolve(null)
|
||||||
|
}, 1500)
|
||||||
|
s.on('event', (event: Event) => {
|
||||||
|
s.unsub()
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve(event)
|
||||||
|
})
|
||||||
|
}),
|
||||||
publish(event: Event): Pub {
|
publish(event: Event): Pub {
|
||||||
if (!event.id) throw new Error(`event ${event} has no id`)
|
if (!event.id) throw new Error(`event ${event} has no id`)
|
||||||
let id = event.id
|
let id = event.id
|
||||||
|
|||||||
Reference in New Issue
Block a user