@welshman/net 0.0.41 → 0.0.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -18
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,21 +2,60 @@
|
|
|
2
2
|
|
|
3
3
|
Utilities having to do with connection management and nostr messages.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
5
|
+
```typescript
|
|
6
|
+
import {ctx, setContext} from '@welshman/lib'
|
|
7
|
+
import {type TrustedEvent, createEvent, NOTE} from '@welshman/util'
|
|
8
|
+
import {subscribe, publish, getDefaultNetContext} from '@welshman/net'
|
|
9
|
+
|
|
10
|
+
// Sets up customizable event valdation, handlers, etc
|
|
11
|
+
setContext(getDefaultNetContext())
|
|
12
|
+
|
|
13
|
+
// Send a subscription
|
|
14
|
+
const sub = subscribe({
|
|
15
|
+
relays: ['wss://relay.example.com/'],
|
|
16
|
+
filters: [{kinds: [1], limit: 1}],
|
|
17
|
+
closeOnEose: true,
|
|
18
|
+
timeout: 10000,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
sub.emitter.on(SubscriptionEvent.Event, (url: string, event: TrustedEvent) => {
|
|
22
|
+
console.log(url, event)
|
|
23
|
+
sub.close()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
// Publish an event
|
|
27
|
+
const pub = publish({
|
|
28
|
+
relays: ['wss://relay.example.com/'],
|
|
29
|
+
event: createEvent(NOTE, {content: 'hi'}),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
pub.emitter.on('*', (status: PublishStatus, url: string) => {
|
|
33
|
+
console.log(status, url)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// The Tracker class can tell you which relays an event was read from or published to
|
|
37
|
+
console.log(ctx.net.tracker.getRelays(event.id))
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The main reason this module exists is to support different backends via Executor and different `target` classes. For example, to add a local relay that automatically gets used:
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import {setContext} from '@welshman/lib'
|
|
44
|
+
import {LOCAL_RELAY_URL, Relay, Repository} from '@welshman/util'
|
|
45
|
+
import {getDefaultNetContext, Multi, Local, Relays, Executor} from '@welshman/net'
|
|
46
|
+
|
|
47
|
+
const repository = new Repository()
|
|
48
|
+
|
|
49
|
+
const relay = new Relay(repository)
|
|
50
|
+
|
|
51
|
+
setContext(getDefaultNetContext({
|
|
52
|
+
getExecutor: (relays: string[]) => {
|
|
53
|
+
return new Executor(
|
|
54
|
+
new Multi([
|
|
55
|
+
new Local(relay),
|
|
56
|
+
new Relays(remoteUrls.map(url => ctx.net.pool.get(url))),
|
|
57
|
+
])
|
|
58
|
+
)
|
|
59
|
+
},
|
|
60
|
+
}))
|
|
61
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@welshman/net",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.42",
|
|
4
4
|
"author": "hodlbod",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Utilities for connecting with nostr relays.",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"typescript": "~5.1.6"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@welshman/lib": "~0.0.
|
|
37
|
-
"@welshman/util": "~0.0.
|
|
36
|
+
"@welshman/lib": "~0.0.33",
|
|
37
|
+
"@welshman/util": "~0.0.50",
|
|
38
38
|
"isomorphic-ws": "^5.0.0",
|
|
39
39
|
"ws": "^8.16.0"
|
|
40
40
|
}
|