@libp2p/gossipsub 14.1.1-6059227cb
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 +85 -0
- package/dist/index.min.js +19 -0
- package/dist/index.min.js.map +7 -0
- package/dist/src/config.d.ts +32 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +2 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/constants.d.ts +213 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +217 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/errors.d.ts +9 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +15 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/gossipsub.d.ts +419 -0
- package/dist/src/gossipsub.d.ts.map +1 -0
- package/dist/src/gossipsub.js +2520 -0
- package/dist/src/gossipsub.js.map +1 -0
- package/dist/src/index.d.ts +344 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +43 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/message/decodeRpc.d.ts +11 -0
- package/dist/src/message/decodeRpc.d.ts.map +1 -0
- package/dist/src/message/decodeRpc.js +10 -0
- package/dist/src/message/decodeRpc.js.map +1 -0
- package/dist/src/message/index.d.ts +2 -0
- package/dist/src/message/index.d.ts.map +1 -0
- package/dist/src/message/index.js +2 -0
- package/dist/src/message/index.js.map +1 -0
- package/dist/src/message/rpc.d.ts +99 -0
- package/dist/src/message/rpc.d.ts.map +1 -0
- package/dist/src/message/rpc.js +663 -0
- package/dist/src/message/rpc.js.map +1 -0
- package/dist/src/message-cache.d.ts +80 -0
- package/dist/src/message-cache.d.ts.map +1 -0
- package/dist/src/message-cache.js +144 -0
- package/dist/src/message-cache.js.map +1 -0
- package/dist/src/metrics.d.ts +467 -0
- package/dist/src/metrics.d.ts.map +1 -0
- package/dist/src/metrics.js +896 -0
- package/dist/src/metrics.js.map +1 -0
- package/dist/src/score/compute-score.d.ts +4 -0
- package/dist/src/score/compute-score.d.ts.map +1 -0
- package/dist/src/score/compute-score.js +75 -0
- package/dist/src/score/compute-score.js.map +1 -0
- package/dist/src/score/index.d.ts +4 -0
- package/dist/src/score/index.d.ts.map +1 -0
- package/dist/src/score/index.js +4 -0
- package/dist/src/score/index.js.map +1 -0
- package/dist/src/score/message-deliveries.d.ts +45 -0
- package/dist/src/score/message-deliveries.d.ts.map +1 -0
- package/dist/src/score/message-deliveries.js +75 -0
- package/dist/src/score/message-deliveries.js.map +1 -0
- package/dist/src/score/peer-score-params.d.ts +125 -0
- package/dist/src/score/peer-score-params.d.ts.map +1 -0
- package/dist/src/score/peer-score-params.js +159 -0
- package/dist/src/score/peer-score-params.js.map +1 -0
- package/dist/src/score/peer-score-thresholds.d.ts +31 -0
- package/dist/src/score/peer-score-thresholds.d.ts.map +1 -0
- package/dist/src/score/peer-score-thresholds.js +32 -0
- package/dist/src/score/peer-score-thresholds.js.map +1 -0
- package/dist/src/score/peer-score.d.ts +119 -0
- package/dist/src/score/peer-score.d.ts.map +1 -0
- package/dist/src/score/peer-score.js +459 -0
- package/dist/src/score/peer-score.js.map +1 -0
- package/dist/src/score/peer-stats.d.ts +32 -0
- package/dist/src/score/peer-stats.d.ts.map +1 -0
- package/dist/src/score/peer-stats.js +2 -0
- package/dist/src/score/peer-stats.js.map +1 -0
- package/dist/src/score/scoreMetrics.d.ts +23 -0
- package/dist/src/score/scoreMetrics.d.ts.map +1 -0
- package/dist/src/score/scoreMetrics.js +155 -0
- package/dist/src/score/scoreMetrics.js.map +1 -0
- package/dist/src/stream.d.ts +30 -0
- package/dist/src/stream.d.ts.map +1 -0
- package/dist/src/stream.js +55 -0
- package/dist/src/stream.js.map +1 -0
- package/dist/src/tracer.d.ts +53 -0
- package/dist/src/tracer.d.ts.map +1 -0
- package/dist/src/tracer.js +155 -0
- package/dist/src/tracer.js.map +1 -0
- package/dist/src/types.d.ts +148 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +90 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils/buildRawMessage.d.ts +20 -0
- package/dist/src/utils/buildRawMessage.d.ts.map +1 -0
- package/dist/src/utils/buildRawMessage.js +151 -0
- package/dist/src/utils/buildRawMessage.js.map +1 -0
- package/dist/src/utils/create-gossip-rpc.d.ts +7 -0
- package/dist/src/utils/create-gossip-rpc.d.ts.map +1 -0
- package/dist/src/utils/create-gossip-rpc.js +31 -0
- package/dist/src/utils/create-gossip-rpc.js.map +1 -0
- package/dist/src/utils/index.d.ts +4 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +4 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/messageIdToString.d.ts +5 -0
- package/dist/src/utils/messageIdToString.d.ts.map +1 -0
- package/dist/src/utils/messageIdToString.js +8 -0
- package/dist/src/utils/messageIdToString.js.map +1 -0
- package/dist/src/utils/msgIdFn.d.ts +10 -0
- package/dist/src/utils/msgIdFn.d.ts.map +1 -0
- package/dist/src/utils/msgIdFn.js +23 -0
- package/dist/src/utils/msgIdFn.js.map +1 -0
- package/dist/src/utils/multiaddr.d.ts +3 -0
- package/dist/src/utils/multiaddr.d.ts.map +1 -0
- package/dist/src/utils/multiaddr.js +15 -0
- package/dist/src/utils/multiaddr.js.map +1 -0
- package/dist/src/utils/publishConfig.d.ts +8 -0
- package/dist/src/utils/publishConfig.d.ts.map +1 -0
- package/dist/src/utils/publishConfig.js +25 -0
- package/dist/src/utils/publishConfig.js.map +1 -0
- package/dist/src/utils/set.d.ts +14 -0
- package/dist/src/utils/set.d.ts.map +1 -0
- package/dist/src/utils/set.js +41 -0
- package/dist/src/utils/set.js.map +1 -0
- package/dist/src/utils/shuffle.d.ts +7 -0
- package/dist/src/utils/shuffle.d.ts.map +1 -0
- package/dist/src/utils/shuffle.js +21 -0
- package/dist/src/utils/shuffle.js.map +1 -0
- package/dist/src/utils/time-cache.d.ts +22 -0
- package/dist/src/utils/time-cache.d.ts.map +1 -0
- package/dist/src/utils/time-cache.js +54 -0
- package/dist/src/utils/time-cache.js.map +1 -0
- package/package.json +142 -0
- package/src/config.ts +31 -0
- package/src/constants.ts +261 -0
- package/src/errors.ts +17 -0
- package/src/gossipsub.ts +3061 -0
- package/src/index.ts +404 -0
- package/src/message/decodeRpc.ts +19 -0
- package/src/message/index.ts +1 -0
- package/src/message/rpc.proto +58 -0
- package/src/message/rpc.ts +848 -0
- package/src/message-cache.ts +196 -0
- package/src/metrics.ts +1014 -0
- package/src/score/compute-score.ts +98 -0
- package/src/score/index.ts +3 -0
- package/src/score/message-deliveries.ts +95 -0
- package/src/score/peer-score-params.ts +316 -0
- package/src/score/peer-score-thresholds.ts +70 -0
- package/src/score/peer-score.ts +565 -0
- package/src/score/peer-stats.ts +33 -0
- package/src/score/scoreMetrics.ts +215 -0
- package/src/stream.ts +79 -0
- package/src/tracer.ts +177 -0
- package/src/types.ts +178 -0
- package/src/utils/buildRawMessage.ts +174 -0
- package/src/utils/create-gossip-rpc.ts +34 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/messageIdToString.ts +8 -0
- package/src/utils/msgIdFn.ts +24 -0
- package/src/utils/multiaddr.ts +19 -0
- package/src/utils/publishConfig.ts +33 -0
- package/src/utils/set.ts +43 -0
- package/src/utils/shuffle.ts +21 -0
- package/src/utils/time-cache.ts +71 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import * as constants from './constants.js'
|
|
2
|
+
import { GossipSub as GossipSubClass } from './gossipsub.ts'
|
|
3
|
+
import { MessageCache } from './message-cache.js'
|
|
4
|
+
import type { GossipsubOptsSpec } from './config.js'
|
|
5
|
+
import type { DecodeRPCLimits } from './message/decodeRpc.js'
|
|
6
|
+
import type { MetricsRegister, TopicStrToLabel } from './metrics.js'
|
|
7
|
+
import type { PeerScoreParams, PeerScoreThresholds } from './score/index.js'
|
|
8
|
+
import type { MsgIdFn, MsgIdStr, FastMsgIdFn, AddrInfo, DataTransform, MsgIdToStrFn } from './types.js'
|
|
9
|
+
import type {
|
|
10
|
+
PeerId, PeerStore,
|
|
11
|
+
ComponentLogger,
|
|
12
|
+
PrivateKey,
|
|
13
|
+
PublicKey,
|
|
14
|
+
TypedEventTarget,
|
|
15
|
+
MessageStreamDirection
|
|
16
|
+
} from '@libp2p/interface'
|
|
17
|
+
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* On the producing side:
|
|
21
|
+
* - Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
|
|
22
|
+
*
|
|
23
|
+
* On the consuming side:
|
|
24
|
+
* - Enforce the fields to be present, reject otherwise.
|
|
25
|
+
* - Propagate only if the fields are valid and signature can be verified, reject otherwise.
|
|
26
|
+
*/
|
|
27
|
+
export const StrictSign = 'StrictSign'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* On the producing side:
|
|
31
|
+
* - Build messages without the signature, key, from and seqno fields.
|
|
32
|
+
* - The corresponding protobuf key-value pairs are absent from the marshaled message, not just empty.
|
|
33
|
+
*
|
|
34
|
+
* On the consuming side:
|
|
35
|
+
* - Enforce the fields to be absent, reject otherwise.
|
|
36
|
+
* - Propagate only if the fields are absent, reject otherwise.
|
|
37
|
+
* - A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
|
|
38
|
+
*/
|
|
39
|
+
export const StrictNoSign = 'StrictNoSign'
|
|
40
|
+
|
|
41
|
+
export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign
|
|
42
|
+
|
|
43
|
+
export enum TopicValidatorResult {
|
|
44
|
+
/**
|
|
45
|
+
* The message is considered valid, and it should be delivered and forwarded to the network
|
|
46
|
+
*/
|
|
47
|
+
Accept = 'accept',
|
|
48
|
+
/**
|
|
49
|
+
* The message is neither delivered nor forwarded to the network
|
|
50
|
+
*/
|
|
51
|
+
Ignore = 'ignore',
|
|
52
|
+
/**
|
|
53
|
+
* The message is considered invalid, and it should be rejected
|
|
54
|
+
*/
|
|
55
|
+
Reject = 'reject'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface SignedMessage {
|
|
59
|
+
type: 'signed'
|
|
60
|
+
from: PeerId
|
|
61
|
+
topic: string
|
|
62
|
+
data: Uint8Array
|
|
63
|
+
sequenceNumber: bigint
|
|
64
|
+
signature: Uint8Array
|
|
65
|
+
key: PublicKey
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface UnsignedMessage {
|
|
69
|
+
type: 'unsigned'
|
|
70
|
+
topic: string
|
|
71
|
+
data: Uint8Array
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type Message = SignedMessage | UnsignedMessage
|
|
75
|
+
|
|
76
|
+
export interface PublishResult {
|
|
77
|
+
recipients: PeerId[]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface Subscription {
|
|
81
|
+
topic: string
|
|
82
|
+
subscribe: boolean
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface SubscriptionChangeData {
|
|
86
|
+
peerId: PeerId
|
|
87
|
+
subscriptions: Subscription[]
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface TopicValidatorFn {
|
|
91
|
+
(peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const multicodec: string = constants.GossipsubIDv12
|
|
95
|
+
|
|
96
|
+
export interface GossipsubOpts extends GossipsubOptsSpec {
|
|
97
|
+
/** if dial should fallback to floodsub */
|
|
98
|
+
fallbackToFloodsub: boolean
|
|
99
|
+
/** if self-published messages should be sent to all peers */
|
|
100
|
+
floodPublish: boolean
|
|
101
|
+
/** serialize message once and send to all peers without control messages */
|
|
102
|
+
batchPublish: boolean
|
|
103
|
+
/** whether PX is enabled; this should be enabled in bootstrappers and other well connected/trusted nodes. */
|
|
104
|
+
doPX: boolean
|
|
105
|
+
/** peers with which we will maintain direct connections */
|
|
106
|
+
directPeers: AddrInfo[]
|
|
107
|
+
/**
|
|
108
|
+
* If true will not forward messages to mesh peers until reportMessageValidationResult() is called.
|
|
109
|
+
* Messages will be cached in mcache for some time after which they are evicted. Calling
|
|
110
|
+
* reportMessageValidationResult() after the message is dropped from mcache won't forward the message.
|
|
111
|
+
*/
|
|
112
|
+
asyncValidation: boolean
|
|
113
|
+
/**
|
|
114
|
+
* Do not throw `PublishError.NoPeersSubscribedToTopic` error if there are no
|
|
115
|
+
* peers listening on the topic.
|
|
116
|
+
*
|
|
117
|
+
* N.B. if you sent this option to true, and you publish a message on a topic
|
|
118
|
+
* with no peers listening on that topic, no other network node will ever
|
|
119
|
+
* receive the message.
|
|
120
|
+
*/
|
|
121
|
+
allowPublishToZeroTopicPeers: boolean
|
|
122
|
+
/** Do not throw `PublishError.Duplicate` if publishing duplicate messages */
|
|
123
|
+
ignoreDuplicatePublishError: boolean
|
|
124
|
+
/** For a single stream, await processing each RPC before processing the next */
|
|
125
|
+
awaitRpcHandler: boolean
|
|
126
|
+
/** For a single RPC, await processing each message before processing the next */
|
|
127
|
+
awaitRpcMessageHandler: boolean
|
|
128
|
+
|
|
129
|
+
/** message id function */
|
|
130
|
+
msgIdFn: MsgIdFn
|
|
131
|
+
/** fast message id function */
|
|
132
|
+
fastMsgIdFn: FastMsgIdFn
|
|
133
|
+
/** Uint8Array message id to string function */
|
|
134
|
+
msgIdToStrFn: MsgIdToStrFn
|
|
135
|
+
/** override the default MessageCache */
|
|
136
|
+
messageCache: MessageCache
|
|
137
|
+
/** peer score parameters */
|
|
138
|
+
scoreParams: Partial<PeerScoreParams>
|
|
139
|
+
/** peer score thresholds */
|
|
140
|
+
scoreThresholds: Partial<PeerScoreThresholds>
|
|
141
|
+
/** customize GossipsubIWantFollowupTime in order not to apply IWANT penalties */
|
|
142
|
+
gossipsubIWantFollowupMs: number
|
|
143
|
+
|
|
144
|
+
/** override constants for fine tuning */
|
|
145
|
+
prunePeers?: number
|
|
146
|
+
pruneBackoff?: number
|
|
147
|
+
unsubcribeBackoff?: number
|
|
148
|
+
graftFloodThreshold?: number
|
|
149
|
+
opportunisticGraftPeers?: number
|
|
150
|
+
opportunisticGraftTicks?: number
|
|
151
|
+
directConnectTicks?: number
|
|
152
|
+
|
|
153
|
+
dataTransform?: DataTransform
|
|
154
|
+
metricsRegister?: MetricsRegister | null
|
|
155
|
+
metricsTopicStrToLabel?: TopicStrToLabel
|
|
156
|
+
|
|
157
|
+
// Debug
|
|
158
|
+
/** Prefix tag for debug logs */
|
|
159
|
+
debugName?: string
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Specify the maximum number of inbound gossipsub protocol
|
|
163
|
+
* streams that are allowed to be open concurrently
|
|
164
|
+
*/
|
|
165
|
+
maxInboundStreams?: number
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Specify the maximum number of outbound gossipsub protocol
|
|
169
|
+
* streams that are allowed to be open concurrently
|
|
170
|
+
*/
|
|
171
|
+
maxOutboundStreams?: number
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Pass true to run on limited connections - data or time-limited
|
|
175
|
+
* connections that may be closed at any time such as circuit relay
|
|
176
|
+
* connections.
|
|
177
|
+
*
|
|
178
|
+
* @default false
|
|
179
|
+
*/
|
|
180
|
+
runOnLimitedConnection?: boolean
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Specify max buffer size in bytes for OutboundStream.
|
|
184
|
+
* If full it will throw and reject sending any more data.
|
|
185
|
+
*/
|
|
186
|
+
maxOutboundBufferSize?: number
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Specify max size to skip decoding messages whose data
|
|
190
|
+
* section exceeds this size.
|
|
191
|
+
*
|
|
192
|
+
*/
|
|
193
|
+
maxInboundDataLength?: number
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* If provided, only allow topics in this list
|
|
197
|
+
*/
|
|
198
|
+
allowedTopics?: string[] | Set<string>
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Limits to bound protobuf decoding
|
|
202
|
+
*/
|
|
203
|
+
decodeRpcLimits?: DecodeRPCLimits
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* If true, will utilize the libp2p connection manager tagging system to prune/graft connections to peers, defaults to true
|
|
207
|
+
*/
|
|
208
|
+
tagMeshPeers: boolean
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Specify what percent of peers to send gossip to. If the percent results in
|
|
212
|
+
* a number smaller than `Dlazy`, `Dlazy` will be used instead.
|
|
213
|
+
*
|
|
214
|
+
* It should be a number between 0 and 1, with a reasonable default of 0.25
|
|
215
|
+
*/
|
|
216
|
+
gossipFactor: number
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The minimum message size in bytes to be considered for sending IDONTWANT messages
|
|
220
|
+
*
|
|
221
|
+
* @default 512
|
|
222
|
+
*/
|
|
223
|
+
idontwantMinDataSize?: number
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The maximum number of IDONTWANT messages per heartbeat per peer
|
|
227
|
+
*
|
|
228
|
+
* @default 512
|
|
229
|
+
*/
|
|
230
|
+
idontwantMaxMessages?: number
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Override the protocol registered with the registrar
|
|
234
|
+
*
|
|
235
|
+
* @default ['/floodsub/1.0.0']
|
|
236
|
+
*/
|
|
237
|
+
protocols?: string[]
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* defines how signatures should be handled
|
|
241
|
+
*/
|
|
242
|
+
globalSignaturePolicy?: SignaturePolicy
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* if can relay messages not subscribed
|
|
246
|
+
*/
|
|
247
|
+
canRelayMessage?: boolean
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* if publish should emit to self, if subscribed
|
|
251
|
+
*/
|
|
252
|
+
emitSelf?: boolean
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* handle this many incoming pubsub messages concurrently
|
|
256
|
+
*/
|
|
257
|
+
messageProcessingConcurrency?: number
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export interface GossipsubMessage {
|
|
261
|
+
propagationSource: PeerId
|
|
262
|
+
msgId: MsgIdStr
|
|
263
|
+
msg: Message
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface MeshPeer {
|
|
267
|
+
peerId: string
|
|
268
|
+
topic: string
|
|
269
|
+
direction: MessageStreamDirection
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export interface GossipSubEvents {
|
|
273
|
+
'subscription-change': CustomEvent<SubscriptionChangeData>
|
|
274
|
+
message: CustomEvent<Message>
|
|
275
|
+
'gossipsub:heartbeat': CustomEvent
|
|
276
|
+
'gossipsub:message': CustomEvent<GossipsubMessage>
|
|
277
|
+
'gossipsub:graft': CustomEvent<MeshPeer>
|
|
278
|
+
'gossipsub:prune': CustomEvent<MeshPeer>
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export interface GossipSubComponents {
|
|
282
|
+
privateKey: PrivateKey
|
|
283
|
+
peerId: PeerId
|
|
284
|
+
peerStore: PeerStore
|
|
285
|
+
registrar: Registrar
|
|
286
|
+
connectionManager: ConnectionManager
|
|
287
|
+
logger: ComponentLogger
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export interface GossipSub extends TypedEventTarget<GossipSubEvents> {
|
|
291
|
+
/**
|
|
292
|
+
* The global signature policy controls whether or not we sill send and receive
|
|
293
|
+
* signed or unsigned messages.
|
|
294
|
+
*
|
|
295
|
+
* Signed messages prevent spoofing message senders and should be preferred to
|
|
296
|
+
* using unsigned messages.
|
|
297
|
+
*/
|
|
298
|
+
globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* A list of multicodecs that contain the pubsub protocol name.
|
|
302
|
+
*/
|
|
303
|
+
protocols: string[]
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Pubsub routers support message validators per topic, which will validate the message
|
|
307
|
+
* before its propagations. They are stored in a map where keys are the topic name and
|
|
308
|
+
* values are the validators.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
*
|
|
312
|
+
* ```TypeScript
|
|
313
|
+
* const topic = 'topic'
|
|
314
|
+
* const validateMessage = (msgTopic, msg) => {
|
|
315
|
+
* const input = uint8ArrayToString(msg.data)
|
|
316
|
+
* const validInputs = ['a', 'b', 'c']
|
|
317
|
+
*
|
|
318
|
+
* if (!validInputs.includes(input)) {
|
|
319
|
+
* throw new Error('no valid input received')
|
|
320
|
+
* }
|
|
321
|
+
* }
|
|
322
|
+
* libp2p.pubsub.topicValidators.set(topic, validateMessage)
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
topicValidators: Map<string, TopicValidatorFn>
|
|
326
|
+
|
|
327
|
+
getPeers(): PeerId[]
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Gets a list of topics the node is subscribed to.
|
|
331
|
+
*
|
|
332
|
+
* ```TypeScript
|
|
333
|
+
* const topics = libp2p.pubsub.getTopics()
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
getTopics(): string[]
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Subscribes to a pubsub topic.
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
*
|
|
343
|
+
* ```TypeScript
|
|
344
|
+
* const topic = 'topic'
|
|
345
|
+
* const handler = (msg) => {
|
|
346
|
+
* if (msg.topic === topic) {
|
|
347
|
+
* // msg.data - pubsub data received
|
|
348
|
+
* }
|
|
349
|
+
* }
|
|
350
|
+
*
|
|
351
|
+
* libp2p.pubsub.addEventListener('message', handler)
|
|
352
|
+
* libp2p.pubsub.subscribe(topic)
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
subscribe(topic: string): void
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Unsubscribes from a pubsub topic.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
*
|
|
362
|
+
* ```TypeScript
|
|
363
|
+
* const topic = 'topic'
|
|
364
|
+
* const handler = (msg) => {
|
|
365
|
+
* // msg.data - pubsub data received
|
|
366
|
+
* }
|
|
367
|
+
*
|
|
368
|
+
* libp2p.pubsub.removeEventListener(topic handler)
|
|
369
|
+
* libp2p.pubsub.unsubscribe(topic)
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
unsubscribe(topic: string): void
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Gets a list of the PeerIds that are subscribed to one topic.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
*
|
|
379
|
+
* ```TypeScript
|
|
380
|
+
* const peerIds = libp2p.pubsub.getSubscribers(topic)
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
getSubscribers(topic: string): PeerId[]
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Publishes messages to the given topic.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
*
|
|
390
|
+
* ```TypeScript
|
|
391
|
+
* const topic = 'topic'
|
|
392
|
+
* const data = uint8ArrayFromString('data')
|
|
393
|
+
*
|
|
394
|
+
* await libp2p.pubsub.publish(topic, data)
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
publish(topic: string, data?: Uint8Array): Promise<PublishResult>
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
export function gossipsub (
|
|
401
|
+
init: Partial<GossipsubOpts> = {}
|
|
402
|
+
): (components: GossipSubComponents) => GossipSub {
|
|
403
|
+
return (components: GossipSubComponents) => new GossipSubClass(components, init)
|
|
404
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface DecodeRPCLimits {
|
|
2
|
+
maxSubscriptions: number
|
|
3
|
+
maxMessages: number
|
|
4
|
+
maxIhaveMessageIDs: number
|
|
5
|
+
maxIwantMessageIDs: number
|
|
6
|
+
maxIdontwantMessageIDs: number
|
|
7
|
+
maxControlMessages: number
|
|
8
|
+
maxPeerInfos: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const defaultDecodeRpcLimits: DecodeRPCLimits = {
|
|
12
|
+
maxSubscriptions: Infinity,
|
|
13
|
+
maxMessages: Infinity,
|
|
14
|
+
maxIhaveMessageIDs: Infinity,
|
|
15
|
+
maxIwantMessageIDs: Infinity,
|
|
16
|
+
maxIdontwantMessageIDs: Infinity,
|
|
17
|
+
maxControlMessages: Infinity,
|
|
18
|
+
maxPeerInfos: Infinity
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './rpc.js'
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
message RPC {
|
|
4
|
+
repeated SubOpts subscriptions = 1;
|
|
5
|
+
repeated Message messages = 2;
|
|
6
|
+
optional ControlMessage control = 3;
|
|
7
|
+
|
|
8
|
+
message SubOpts {
|
|
9
|
+
optional bool subscribe = 1; // subscribe or unsubcribe
|
|
10
|
+
optional string topic = 2;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
message Message {
|
|
14
|
+
optional bytes from = 1;
|
|
15
|
+
optional bytes data = 2;
|
|
16
|
+
optional bytes seqno = 3;
|
|
17
|
+
string topic = 4;
|
|
18
|
+
optional bytes signature = 5;
|
|
19
|
+
optional bytes key = 6;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
message ControlMessage {
|
|
23
|
+
repeated ControlIHave ihave = 1;
|
|
24
|
+
repeated ControlIWant iwant = 2;
|
|
25
|
+
repeated ControlGraft graft = 3;
|
|
26
|
+
repeated ControlPrune prune = 4;
|
|
27
|
+
repeated ControlIDontWant idontwant = 5;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
message ControlIHave {
|
|
31
|
+
optional string topicID = 1;
|
|
32
|
+
repeated bytes messageIDs = 2;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
message ControlIWant {
|
|
36
|
+
repeated bytes messageIDs = 1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
message ControlGraft {
|
|
40
|
+
optional string topicID = 1;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
message ControlPrune {
|
|
44
|
+
optional string topicID = 1;
|
|
45
|
+
repeated PeerInfo peers = 2;
|
|
46
|
+
optional uint64 backoff = 3 [jstype = JS_NUMBER];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
message PeerInfo {
|
|
50
|
+
optional bytes peerID = 1;
|
|
51
|
+
optional bytes signedPeerRecord = 2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
message ControlIDontWant {
|
|
55
|
+
repeated bytes messageIDs = 1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|