@libp2p/interface 2.11.0 → 3.0.0-334f91bdc

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