@libp2p/pubsub 1.2.15 → 1.2.17

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/package.json +3 -3
  2. package/src/README.md +0 -251
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/pubsub",
3
- "version": "1.2.15",
3
+ "version": "1.2.17",
4
4
  "description": "libp2p pubsub base class",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-pubsub#readme",
@@ -179,21 +179,21 @@
179
179
  "@libp2p/peer-id": "^1.1.0",
180
180
  "@libp2p/topology": "^1.1.0",
181
181
  "@multiformats/multiaddr": "^10.1.5",
182
+ "abortable-iterator": "^4.0.2",
182
183
  "err-code": "^3.0.1",
183
184
  "iso-random-stream": "^2.0.0",
184
185
  "it-length-prefixed": "^7.0.1",
185
186
  "it-pipe": "^2.0.3",
187
+ "it-pushable": "^2.0.1",
186
188
  "multiformats": "^9.6.3",
187
189
  "p-queue": "^7.2.0",
188
190
  "uint8arrays": "^3.0.0"
189
191
  },
190
192
  "devDependencies": {
191
193
  "@libp2p/peer-id-factory": "^1.0.0",
192
- "abortable-iterator": "^4.0.2",
193
194
  "aegir": "^37.0.7",
194
195
  "delay": "^5.0.0",
195
196
  "it-pair": "^2.0.2",
196
- "it-pushable": "^2.0.1",
197
197
  "p-wait-for": "^4.1.0",
198
198
  "protons": "^3.0.2",
199
199
  "protons-runtime": "^1.0.2",
package/src/README.md DELETED
@@ -1,251 +0,0 @@
1
- # libp2p-pubsub <!-- omit in toc -->
2
-
3
- This module contains a base implementation for a libp2p pubsub router implementation. It should be extended to implement a pubsub router compatible with libp2p.
4
-
5
- ## Table of Contents <!-- omit in toc -->
6
-
7
- - [Implementations using this interface](#implementations-using-this-interface)
8
- - [Interface usage](#interface-usage)
9
- - [Extend interface](#extend-interface)
10
- - [Example](#example)
11
- - [API](#api)
12
- - [Constructor](#constructor)
13
- - [`new Pubsub({options})`](#new-pubsuboptions)
14
- - [Parameters](#parameters)
15
- - [Start](#start)
16
- - [`pubsub.start()`](#pubsubstart)
17
- - [Stop](#stop)
18
- - [`pubsub.stop()`](#pubsubstop)
19
- - [Publish](#publish)
20
- - [`pubsub.publish(topic, message)`](#pubsubpublishtopic-message)
21
- - [Parameters](#parameters-1)
22
- - [Returns](#returns)
23
- - [Subscribe](#subscribe)
24
- - [`pubsub.subscribe(topic)`](#pubsubsubscribetopic)
25
- - [Parameters](#parameters-2)
26
- - [Unsubscribe](#unsubscribe)
27
- - [`pubsub.unsubscribe(topic)`](#pubsubunsubscribetopic)
28
- - [Parameters](#parameters-3)
29
- - [Get Topics](#get-topics)
30
- - [`pubsub.getTopics()`](#pubsubgettopics)
31
- - [Returns](#returns-1)
32
- - [Get Peers Subscribed to a topic](#get-peers-subscribed-to-a-topic)
33
- - [`pubsub.getSubscribers(topic)`](#pubsubgetsubscriberstopic)
34
- - [Parameters](#parameters-4)
35
- - [Returns](#returns-2)
36
- - [Validate](#validate)
37
- - [`pubsub.validate(message)`](#pubsubvalidatemessage)
38
- - [Parameters](#parameters-5)
39
- - [Returns](#returns-3)
40
- - [Test suite usage](#test-suite-usage)
41
-
42
- ## Implementations using this interface
43
-
44
- You can check the following implementations as examples for building your own pubsub router.
45
-
46
- - [libp2p/js-libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub)
47
- - [ChainSafe/js-libp2p-gossipsub](https://github.com/ChainSafe/js-libp2p-gossipsub)
48
-
49
- ## Interface usage
50
-
51
- `interface-pubsub` abstracts the implementation protocol registration within `libp2p` and takes care of all the protocol connections and streams, as well as the subscription management and the features describe in the libp2p [pubsub specs](https://github.com/libp2p/specs/tree/master/pubsub). This way, a pubsub implementation can focus on its message routing algorithm, instead of also needing to create the setup for it.
52
-
53
- ### Extend interface
54
-
55
- A pubsub router implementation should start by extending the `interface-pubsub` class and **MUST** override the `_publish` function, according to the router algorithms. This function is responsible for forwarding publish messages to other peers, as well as forwarding received messages if the router provides the `canRelayMessage` option to the base implementation.
56
-
57
- Other functions, such as `start`, `stop`, `subscribe`, `unsubscribe`, `_encodeRpc`, `_decodeRpc`, `_processRpcMessage`, `_addPeer` and `_removePeer` may be overwritten if the pubsub implementation needs to customize their logic. Implementations overriding these functions **MUST** call `super`.
58
-
59
- The `start` and `stop` functions are responsible for the registration of the pubsub protocol with libp2p. The `stop` function also guarantees that the open streams in the protocol are properly closed.
60
-
61
- The `subscribe` and `unsubscribe` functions take care of the subscription management and its inherent message propagation.
62
-
63
- When using a custom protobuf definition for message marshalling, you should override `_encodeRpc` and `_decodeRpc` to use the new protobuf instead of the default one.
64
-
65
- `_processRpcMessage` is responsible for handling messages received from other peers. This should be extended if further operations/validations are needed by the router.
66
-
67
- The `_addPeer` and `_removePeer` functions are called when new peers running the pubsub router protocol establish a connection with the peer. They are used for tracking the open streams between the peers.
68
-
69
- All the remaining functions **MUST NOT** be overwritten.
70
-
71
- ### Example
72
-
73
- The following example aims to show how to create your pubsub implementation extending this base protocol. The pubsub implementation will handle the subscriptions logic.
74
-
75
- ```JavaScript
76
- const Pubsub = require('libp2p-interfaces/src/pubsub')
77
-
78
- class PubsubImplementation extends Pubsub {
79
- constructor({ libp2p, options })
80
- super({
81
- debugName: 'libp2p:pubsub',
82
- multicodecs: '/pubsub-implementation/1.0.0',
83
- libp2p,
84
- globalSigningPolicy: options.globalSigningPolicy
85
- })
86
- }
87
-
88
- _publish (message) {
89
- // Required to be implemented by the subclass
90
- // Routing logic for the message
91
- }
92
- }
93
- ```
94
-
95
- ## API
96
-
97
- The interface aims to specify a common interface that all pubsub router implementation should follow. A pubsub router implementation should extend the [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter). When peers receive pubsub messages, these messages will be emitted by the event emitter where the `eventName` will be the `topic` associated with the message.
98
-
99
- ### Constructor
100
-
101
- The base class constructor configures the pubsub instance for use with a libp2p instance. It includes settings for logging, signature policies, etc.
102
-
103
- #### `new Pubsub({options})`
104
-
105
- ##### Parameters
106
-
107
- | Name | Type | Description | Default |
108
- |------|------|-------------|---------|
109
- | options.libp2p | `Libp2p` | libp2p instance | required, no default |
110
- | options.debugName | `string` | log namespace | required, no default |
111
- | options.multicodecs | `string \| Array<string>` | protocol identifier(s) | required, no default |
112
- | options.globalSignaturePolicy | `'StrictSign' \| 'StrictNoSign'` | signature policy to be globally applied | `'StrictSign'` |
113
- | options.canRelayMessage | `boolean` | if can relay messages if not subscribed | `false` |
114
- | options.emitSelf | `boolean` | if `publish` should emit to self, if subscribed | `false` |
115
-
116
- ### Start
117
-
118
- Starts the pubsub subsystem. The protocol will be registered to `libp2p`, which will result in pubsub being notified when peers who support the protocol connect/disconnect to `libp2p`.
119
-
120
- #### `pubsub.start()`
121
-
122
- ### Stop
123
-
124
- Stops the pubsub subsystem. The protocol will be unregistered from `libp2p`, which will remove all listeners for the protocol and the established connections will be closed.
125
-
126
- #### `pubsub.stop()`
127
-
128
- ### Publish
129
-
130
- Publish data message to pubsub topics.
131
-
132
- #### `pubsub.publish(topic, message)`
133
-
134
- ##### Parameters
135
-
136
- | Name | Type | Description |
137
- |------|------|-------------|
138
- | topic | `string` | pubsub topic |
139
- | message | `Uint8Array` | message to publish |
140
-
141
- ##### Returns
142
-
143
- | Type | Description |
144
- |------|-------------|
145
- | `Promise<void>` | resolves once the message is published to the network |
146
-
147
- ### Subscribe
148
-
149
- Subscribe to the given topic.
150
-
151
- #### `pubsub.subscribe(topic)`
152
-
153
- ##### Parameters
154
-
155
- | Name | Type | Description |
156
- |------|------|-------------|
157
- | topic | `string` | pubsub topic |
158
-
159
- ### Unsubscribe
160
-
161
- Unsubscribe from the given topic.
162
-
163
- #### `pubsub.unsubscribe(topic)`
164
-
165
- ##### Parameters
166
-
167
- | Name | Type | Description |
168
- |------|------|-------------|
169
- | topic | `string` | pubsub topic |
170
-
171
- ### Get Topics
172
-
173
- Get the list of topics which the peer is subscribed to.
174
-
175
- #### `pubsub.getTopics()`
176
-
177
- ##### Returns
178
-
179
- | Type | Description |
180
- |------|-------------|
181
- | `Array<String>` | Array of subscribed topics |
182
-
183
- ### Get Peers Subscribed to a topic
184
-
185
- Get a list of the [PeerId](https://github.com/libp2p/js-peer-id) strings that are subscribed to one topic.
186
-
187
- #### `pubsub.getSubscribers(topic)`
188
-
189
- ##### Parameters
190
-
191
- | Name | Type | Description |
192
- |------|------|-------------|
193
- | topic | `string` | pubsub topic |
194
-
195
- ##### Returns
196
-
197
- | Type | Description |
198
- |------|-------------|
199
- | `Array<string>` | Array of base-58 PeerId's |
200
-
201
- ### Validate
202
-
203
- Validates a message according to the signature policy and topic-specific validation function.
204
-
205
- #### `pubsub.validate(message)`
206
-
207
- ##### Parameters
208
-
209
- | Name | Type | Description |
210
- |------|------|-------------|
211
- | message | `Message` | a pubsub message |
212
-
213
- #### Returns
214
-
215
- | Type | Description |
216
- |------|-------------|
217
- | `Promise<void>` | resolves if the message is valid |
218
-
219
- ## Test suite usage
220
-
221
- ```js
222
- 'use strict'
223
-
224
- const tests = require('libp2p-interfaces-compliance-tests/pubsub')
225
- const YourPubsubRouter = require('../src')
226
-
227
- describe('compliance', () => {
228
- let peers
229
- let pubsubNodes = []
230
-
231
- tests({
232
- async setup (number = 1, options = {}) {
233
- // Create number pubsub nodes with libp2p
234
- peers = await createPeers({ number })
235
-
236
- peers.forEach((peer) => {
237
- const ps = new YourPubsubRouter(peer, options)
238
-
239
- pubsubNodes.push(ps)
240
- })
241
-
242
- return pubsubNodes
243
- },
244
- async teardown () {
245
- // Clean up any resources created by setup()
246
- await Promise.all(pubsubNodes.map(ps => ps.stop()))
247
- peers.length && await Promise.all(peers.map(peer => peer.stop()))
248
- }
249
- })
250
- })
251
- ```