Commit cc8e341 1 parent 9082953 commit cc8e341 Copy full SHA for cc8e341
File tree 5 files changed +60
-2
lines changed
5 files changed +60
-2
lines changed Original file line number Diff line number Diff line change @@ -120,6 +120,22 @@ To use this on Node.js you first must install `websocket-polyfill` and import it
120
120
import ' websocket-polyfill'
121
121
```
122
122
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
+
123
139
### Querying profile data from a NIP-05 address
124
140
125
141
``` js
@@ -195,7 +211,7 @@ let event = {
195
211
sendEvent (event )
196
212
197
213
// on the receiver side
198
- sub .on (' event' , ( event ) => {
214
+ sub .on (' event' , event => {
199
215
let sender = event .tags .find (([k , v ]) => k === ' p' && v && v !== ' ' )[1 ]
200
216
pk1 === sender
201
217
let plaintext = await nip04 .decrypt (sk2, pk1, event .content )
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ export * from './keys'
2
2
export * from './relay'
3
3
export * from './event'
4
4
export * from './filter'
5
+ export * from './pool'
5
6
6
7
export * as nip04 from './nip04'
7
8
export * as nip05 from './nip05'
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " nostr-tools" ,
3
- "version" : " 1.2.1 " ,
3
+ "version" : " 1.2.3 " ,
4
4
"description" : " Tools for making a Nostr client." ,
5
5
"repository" : {
6
6
"type" : " git" ,
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -3,6 +3,20 @@ import {Event} from './event'
3
3
export const utf8Decoder = new TextDecoder ( 'utf-8' )
4
4
export const utf8Encoder = new TextEncoder ( )
5
5
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
+
6
20
//
7
21
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
8
22
//
You can’t perform that action at this time.
0 commit comments