@libp2p/interface 2.11.0-9a9b11fd4 → 2.11.0-da78fa851

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/src/pubsub.ts ADDED
@@ -0,0 +1,284 @@
1
+ import type { Stream, PublicKey, PeerId } from './index.js'
2
+ import type { Pushable } from 'it-pushable'
3
+ import type { TypedEventTarget } from 'main-event'
4
+ import type { Uint8ArrayList } from 'uint8arraylist'
5
+
6
+ /**
7
+ * On the producing side:
8
+ * - Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
9
+ *
10
+ * On the consuming side:
11
+ * - Enforce the fields to be present, reject otherwise.
12
+ * - Propagate only if the fields are valid and signature can be verified, reject otherwise.
13
+ */
14
+ export const StrictSign = 'StrictSign'
15
+
16
+ /**
17
+ * On the producing side:
18
+ * - Build messages without the signature, key, from and seqno fields.
19
+ * - The corresponding protobuf key-value pairs are absent from the marshaled message, not just empty.
20
+ *
21
+ * On the consuming side:
22
+ * - Enforce the fields to be absent, reject otherwise.
23
+ * - Propagate only if the fields are absent, reject otherwise.
24
+ * - 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.
25
+ */
26
+ export const StrictNoSign = 'StrictNoSign'
27
+
28
+ export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign
29
+
30
+ export interface SignedMessage {
31
+ type: 'signed'
32
+ from: PeerId
33
+ topic: string
34
+ data: Uint8Array
35
+ sequenceNumber: bigint
36
+ signature: Uint8Array
37
+ key: PublicKey
38
+ }
39
+
40
+ export interface UnsignedMessage {
41
+ type: 'unsigned'
42
+ topic: string
43
+ data: Uint8Array
44
+ }
45
+
46
+ export type Message = SignedMessage | UnsignedMessage
47
+
48
+ export interface PubSubRPCMessage {
49
+ from?: Uint8Array
50
+ topic?: string
51
+ data?: Uint8Array
52
+ sequenceNumber?: Uint8Array
53
+ signature?: Uint8Array
54
+ key?: Uint8Array
55
+ }
56
+
57
+ export interface PubSubRPCSubscription {
58
+ subscribe?: boolean
59
+ topic?: string
60
+ }
61
+
62
+ export interface PubSubRPC {
63
+ subscriptions: PubSubRPCSubscription[]
64
+ messages: PubSubRPCMessage[]
65
+ }
66
+
67
+ export interface PeerStreams extends TypedEventTarget<PeerStreamEvents> {
68
+ id: PeerId
69
+ protocol: string
70
+ outboundStream?: Pushable<Uint8ArrayList>
71
+ inboundStream?: AsyncIterable<Uint8ArrayList>
72
+ isWritable: boolean
73
+
74
+ close(): void
75
+ write(buf: Uint8Array | Uint8ArrayList): void
76
+ attachInboundStream(stream: Stream): AsyncIterable<Uint8ArrayList>
77
+ attachOutboundStream(stream: Stream): Promise<Pushable<Uint8ArrayList>>
78
+ }
79
+
80
+ export interface PubSubInit {
81
+ enabled?: boolean
82
+
83
+ multicodecs?: string[]
84
+
85
+ /**
86
+ * defines how signatures should be handled
87
+ */
88
+ globalSignaturePolicy?: SignaturePolicy
89
+
90
+ /**
91
+ * if can relay messages not subscribed
92
+ */
93
+ canRelayMessage?: boolean
94
+
95
+ /**
96
+ * if publish should emit to self, if subscribed
97
+ */
98
+ emitSelf?: boolean
99
+
100
+ /**
101
+ * handle this many incoming pubsub messages concurrently
102
+ */
103
+ messageProcessingConcurrency?: number
104
+
105
+ /**
106
+ * How many parallel incoming streams to allow on the pubsub protocol per-connection
107
+ */
108
+ maxInboundStreams?: number
109
+
110
+ /**
111
+ * How many parallel outgoing streams to allow on the pubsub protocol per-connection
112
+ */
113
+ maxOutboundStreams?: number
114
+ }
115
+
116
+ export interface Subscription {
117
+ topic: string
118
+ subscribe: boolean
119
+ }
120
+
121
+ export interface SubscriptionChangeData {
122
+ peerId: PeerId
123
+ subscriptions: Subscription[]
124
+ }
125
+
126
+ export interface PubSubEvents {
127
+ 'subscription-change': CustomEvent<SubscriptionChangeData>
128
+ message: CustomEvent<Message>
129
+ }
130
+
131
+ export interface PublishResult {
132
+ recipients: PeerId[]
133
+ }
134
+
135
+ export enum TopicValidatorResult {
136
+ /**
137
+ * The message is considered valid, and it should be delivered and forwarded to the network
138
+ */
139
+ Accept = 'accept',
140
+ /**
141
+ * The message is neither delivered nor forwarded to the network
142
+ */
143
+ Ignore = 'ignore',
144
+ /**
145
+ * The message is considered invalid, and it should be rejected
146
+ */
147
+ Reject = 'reject'
148
+ }
149
+
150
+ export interface TopicValidatorFn {
151
+ (peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>
152
+ }
153
+
154
+ /**
155
+ * @deprecated This will be removed from `@libp2p/interface` in a future release, pubsub implementations should declare their own types
156
+ */
157
+ export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends TypedEventTarget<Events> {
158
+ /**
159
+ * The global signature policy controls whether or not we sill send and receive
160
+ * signed or unsigned messages.
161
+ *
162
+ * Signed messages prevent spoofing message senders and should be preferred to
163
+ * using unsigned messages.
164
+ */
165
+ globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign
166
+
167
+ /**
168
+ * A list of multicodecs that contain the pubsub protocol name.
169
+ */
170
+ multicodecs: string[]
171
+
172
+ /**
173
+ * Pubsub routers support message validators per topic, which will validate the message
174
+ * before its propagations. They are stored in a map where keys are the topic name and
175
+ * values are the validators.
176
+ *
177
+ * @example
178
+ *
179
+ * ```TypeScript
180
+ * const topic = 'topic'
181
+ * const validateMessage = (msgTopic, msg) => {
182
+ * const input = uint8ArrayToString(msg.data)
183
+ * const validInputs = ['a', 'b', 'c']
184
+ *
185
+ * if (!validInputs.includes(input)) {
186
+ * throw new Error('no valid input received')
187
+ * }
188
+ * }
189
+ * libp2p.pubsub.topicValidators.set(topic, validateMessage)
190
+ * ```
191
+ */
192
+ topicValidators: Map<string, TopicValidatorFn>
193
+
194
+ getPeers(): PeerId[]
195
+
196
+ /**
197
+ * Gets a list of topics the node is subscribed to.
198
+ *
199
+ * ```TypeScript
200
+ * const topics = libp2p.pubsub.getTopics()
201
+ * ```
202
+ */
203
+ getTopics(): string[]
204
+
205
+ /**
206
+ * Subscribes to a pubsub topic.
207
+ *
208
+ * @example
209
+ *
210
+ * ```TypeScript
211
+ * const topic = 'topic'
212
+ * const handler = (msg) => {
213
+ * if (msg.topic === topic) {
214
+ * // msg.data - pubsub data received
215
+ * }
216
+ * }
217
+ *
218
+ * libp2p.pubsub.addEventListener('message', handler)
219
+ * libp2p.pubsub.subscribe(topic)
220
+ * ```
221
+ */
222
+ subscribe(topic: string): void
223
+
224
+ /**
225
+ * Unsubscribes from a pubsub topic.
226
+ *
227
+ * @example
228
+ *
229
+ * ```TypeScript
230
+ * const topic = 'topic'
231
+ * const handler = (msg) => {
232
+ * // msg.data - pubsub data received
233
+ * }
234
+ *
235
+ * libp2p.pubsub.removeEventListener(topic handler)
236
+ * libp2p.pubsub.unsubscribe(topic)
237
+ * ```
238
+ */
239
+ unsubscribe(topic: string): void
240
+
241
+ /**
242
+ * Gets a list of the PeerIds that are subscribed to one topic.
243
+ *
244
+ * @example
245
+ *
246
+ * ```TypeScript
247
+ * const peerIds = libp2p.pubsub.getSubscribers(topic)
248
+ * ```
249
+ */
250
+ getSubscribers(topic: string): PeerId[]
251
+
252
+ /**
253
+ * Publishes messages to the given topic.
254
+ *
255
+ * @example
256
+ *
257
+ * ```TypeScript
258
+ * const topic = 'topic'
259
+ * const data = uint8ArrayFromString('data')
260
+ *
261
+ * await libp2p.pubsub.publish(topic, data)
262
+ * ```
263
+ */
264
+ publish(topic: string, data: Uint8Array): Promise<PublishResult>
265
+ }
266
+
267
+ export interface PeerStreamEvents {
268
+ 'stream:inbound': CustomEvent<never>
269
+ 'stream:outbound': CustomEvent<never>
270
+ close: CustomEvent<never>
271
+ }
272
+
273
+ /**
274
+ * All Pubsub implementations must use this symbol as the name of a property
275
+ * with a boolean `true` value
276
+ */
277
+ export const pubSubSymbol = Symbol.for('@libp2p/pubsub')
278
+
279
+ /**
280
+ * Returns true if the passed argument is a PubSub implementation
281
+ */
282
+ export function isPubSub (obj?: any): obj is PubSub {
283
+ return Boolean(obj?.[pubSubSymbol])
284
+ }