@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.
Files changed (2) hide show
  1. package/README.md +57 -18
  2. 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
- - `Connection` - the main api for dealing with relay connections
6
- - `ConnectionAuth` - tracks auth status for a connection
7
- - `ConnectionSender` - a send queue for connections
8
- - `ConnectionState` - tracks pending publishes and requests for a connection
9
- - `ConnectionStats` - tracks timing and error stats for a connection
10
- - `Context` - provides default values for configuring `ctx.net`
11
- - `Executor` - implements common nostr flows on a given `target`
12
- - `Pool` - a thin wrapper around `Map` which stores `Connection`s
13
- - `Publish` - utilities for publishing events
14
- - `Socket` - a wrapper around isomorphic-ws that handles json parsing/serialization
15
- - `Subscribe` - utilities for making requests against nostr relays
16
- - `Tracker` - tracks which relays a given event was seen on
17
-
18
- Executor `target`s extend `Emitter`, and have a `send` method, a `cleanup` method, and a `connections` getter. They are intended to be passed to an `Executor` for use.
19
-
20
- - `targets/Multi` allows you to compose multiple targets together.
21
- - `targets/Relay` takes a `Connection` and provides listeners for different verbs.
22
- - `targets/Relays` takes an array of `Connection`s and provides listeners for different verbs, merging all events into a single stream.
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.41",
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.29",
37
- "@welshman/util": "~0.0.48",
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
  }