Skip to content

Commit cc8e341

Browse files
committed
most simple relay pool.
1 parent 9082953 commit cc8e341

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ To use this on Node.js you first must install `websocket-polyfill` and import it
120120
import 'websocket-polyfill'
121121
```
122122

123+
### Interacting with multiple relays
124+
125+
```js
126+
import {pool} from 'nostr-tools'
127+
128+
const p = pool()
129+
130+
["wss://relay.example.com", "wss://relay.example2.com"].forEach(async url => {
131+
let relay = pool.ensureRelay(url)
132+
await relay.connect()
133+
134+
relay.sub(...) // same as above
135+
relay.publish(...) // etc
136+
})
137+
```
138+
123139
### Querying profile data from a NIP-05 address
124140

125141
```js
@@ -195,7 +211,7 @@ let event = {
195211
sendEvent(event)
196212

197213
// on the receiver side
198-
sub.on('event', (event) => {
214+
sub.on('event', event => {
199215
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
200216
pk1 === sender
201217
let plaintext = await nip04.decrypt(sk2, pk1, event.content)

index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './keys'
22
export * from './relay'
33
export * from './event'
44
export * from './filter'
5+
export * from './pool'
56

67
export * as nip04 from './nip04'
78
export * as nip05 from './nip05'

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nostr-tools",
3-
"version": "1.2.1",
3+
"version": "1.2.3",
44
"description": "Tools for making a Nostr client.",
55
"repository": {
66
"type": "git",

pool.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Relay, relayInit} from './relay'
2+
import {normalizeURL} from './utils'
3+
4+
export function pool(defaultRelays: string[] = []) {
5+
return new SimplePool(defaultRelays)
6+
}
7+
8+
class SimplePool {
9+
private _conn: {[url: string]: Relay}
10+
private _knownIds: Set<string> = new Set()
11+
12+
constructor(defaultRelays: string[]) {
13+
this._conn = {}
14+
defaultRelays.forEach(this.ensureRelay)
15+
}
16+
17+
ensureRelay(url: string): Relay {
18+
const nm = normalizeURL(url)
19+
const existing = this._conn[nm]
20+
if (existing) return existing
21+
22+
const hasEventId = (id: string): boolean => this._knownIds.has(id)
23+
const relay = relayInit(nm, hasEventId)
24+
this._conn[nm] = relay
25+
return relay
26+
}
27+
}

utils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import {Event} from './event'
33
export const utf8Decoder = new TextDecoder('utf-8')
44
export const utf8Encoder = new TextEncoder()
55

6+
export function normalizeURL(url: string): string {
7+
let p = new URL(url)
8+
p.pathname = p.pathname.replace(/\/+/g, '/')
9+
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
10+
if (
11+
(p.port === '80' && p.protocol === 'ws:') ||
12+
(p.port === '443' && p.protocol === 'wss:')
13+
)
14+
p.port = ''
15+
p.searchParams.sort()
16+
p.hash = ''
17+
return p.toString()
18+
}
19+
620
//
721
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
822
//

0 commit comments

Comments
 (0)