most simple relay pool.

This commit is contained in:
fiatjaf 2023-02-08 08:17:12 -03:00
parent 9082953ede
commit cc8e34163d
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
5 changed files with 60 additions and 2 deletions

View File

@ -120,6 +120,22 @@ To use this on Node.js you first must install `websocket-polyfill` and import it
import 'websocket-polyfill'
```
### Interacting with multiple relays
```js
import {pool} from 'nostr-tools'
const p = pool()
["wss://relay.example.com", "wss://relay.example2.com"].forEach(async url => {
let relay = pool.ensureRelay(url)
await relay.connect()
relay.sub(...) // same as above
relay.publish(...) // etc
})
```
### Querying profile data from a NIP-05 address
```js
@ -195,7 +211,7 @@ let event = {
sendEvent(event)
// on the receiver side
sub.on('event', (event) => {
sub.on('event', event => {
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
pk1 === sender
let plaintext = await nip04.decrypt(sk2, pk1, event.content)

View File

@ -2,6 +2,7 @@ export * from './keys'
export * from './relay'
export * from './event'
export * from './filter'
export * from './pool'
export * as nip04 from './nip04'
export * as nip05 from './nip05'

View File

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

27
pool.ts Normal file
View File

@ -0,0 +1,27 @@
import {Relay, relayInit} from './relay'
import {normalizeURL} from './utils'
export function pool(defaultRelays: string[] = []) {
return new SimplePool(defaultRelays)
}
class SimplePool {
private _conn: {[url: string]: Relay}
private _knownIds: Set<string> = new Set()
constructor(defaultRelays: string[]) {
this._conn = {}
defaultRelays.forEach(this.ensureRelay)
}
ensureRelay(url: string): Relay {
const nm = normalizeURL(url)
const existing = this._conn[nm]
if (existing) return existing
const hasEventId = (id: string): boolean => this._knownIds.has(id)
const relay = relayInit(nm, hasEventId)
this._conn[nm] = relay
return relay
}
}

View File

@ -3,6 +3,20 @@ import {Event} from './event'
export const utf8Decoder = new TextDecoder('utf-8')
export const utf8Encoder = new TextEncoder()
export function normalizeURL(url: string): string {
let p = new URL(url)
p.pathname = p.pathname.replace(/\/+/g, '/')
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
if (
(p.port === '80' && p.protocol === 'ws:') ||
(p.port === '443' && p.protocol === 'wss:')
)
p.port = ''
p.searchParams.sort()
p.hash = ''
return p.toString()
}
//
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
//