@libp2p/interface 2.11.0-6059227cb → 2.11.0-8484de8a2

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.
@@ -4,7 +4,7 @@ import type { PeerId } from './peer-id.js';
4
4
  * A topology filter - this can be used by topologies to ensure they do not
5
5
  * receive duplicate notifications of individual peers
6
6
  *
7
- * @see https://libp2p.github.io/js-libp2p/classes/_libp2p_peer-collections.PeerFilter.html
7
+ * @see https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_collections.peerFilter-1.html
8
8
  */
9
9
  export interface TopologyFilter {
10
10
  has(peerId: PeerId): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/interface",
3
- "version": "2.11.0-6059227cb",
3
+ "version": "2.11.0-8484de8a2",
4
4
  "description": "The interface implemented by a libp2p node",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/interface#readme",
@@ -43,13 +43,14 @@
43
43
  "dependencies": {
44
44
  "@multiformats/dns": "^1.0.6",
45
45
  "@multiformats/multiaddr": "^13.0.1",
46
+ "it-pushable": "^3.2.3",
46
47
  "main-event": "^1.0.1",
47
48
  "multiformats": "^13.4.0",
48
49
  "progress-events": "^1.0.1",
49
50
  "uint8arraylist": "^2.4.8"
50
51
  },
51
52
  "devDependencies": {
52
- "aegir": "^47.0.22"
53
+ "aegir": "^47.0.21"
53
54
  },
54
55
  "sideEffects": false
55
56
  }
package/src/errors.ts CHANGED
@@ -132,6 +132,34 @@ export class MuxerClosedError extends Error {
132
132
  }
133
133
  }
134
134
 
135
+ /**
136
+ * Thrown when a protocol stream is closed during an operation
137
+ *
138
+ * @deprecated delete if unused
139
+ */
140
+ export class StreamClosedError extends Error {
141
+ static name = 'StreamClosedError'
142
+
143
+ constructor (message = 'The stream has been closed') {
144
+ super(message)
145
+ this.name = 'StreamClosedError'
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Thrown when a protocol stream is closing during an operation
151
+ *
152
+ * @deprecated delete if unused
153
+ */
154
+ export class StreamClosingError extends Error {
155
+ static name = 'StreamClosingError'
156
+
157
+ constructor (message = 'The stream is closing') {
158
+ super(message)
159
+ this.name = 'StreamClosingError'
160
+ }
161
+ }
162
+
135
163
  /**
136
164
  * Thrown when a protocol stream is reset by the remote muxer
137
165
  */
package/src/index.ts CHANGED
@@ -23,7 +23,7 @@ import type { PeerInfo } from './peer-info.js'
23
23
  import type { PeerRouting } from './peer-routing.js'
24
24
  import type { Address, Peer, PeerStore } from './peer-store.js'
25
25
  import type { Startable } from './startable.js'
26
- import type { StreamHandler, StreamHandlerOptions, StreamMiddleware } from './stream-handler.js'
26
+ import type { StreamHandler, StreamHandlerOptions } from './stream-handler.js'
27
27
  import type { Stream } from './stream.js'
28
28
  import type { Topology } from './topology.js'
29
29
  import type { Listener, OutboundConnectionUpgradeEvents } from './transport.js'
@@ -781,33 +781,6 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
781
781
  */
782
782
  unregister(id: string): void
783
783
 
784
- /**
785
- * Registers one or more middleware implementations that will be invoked for
786
- * incoming and outgoing protocol streams that match the passed protocol.
787
- *
788
- * @example
789
- *
790
- * ```TypeScript
791
- * libp2p.use('/my/protocol/1.0.0', (stream, connection, next) => {
792
- * // do something with stream and/or connection
793
- * next(stream, connection)
794
- * })
795
- * ```
796
- */
797
- use (protocol: string, middleware: StreamMiddleware | StreamMiddleware[]): void
798
-
799
- /**
800
- * Deregisters all middleware for the passed protocol.
801
- *
802
- * @example
803
- *
804
- * ```TypeScript
805
- * libp2p.unuse('/my/protocol/1.0.0')
806
- * // any previously registered middleware will no longer be invoked
807
- * ```
808
- */
809
- unuse (protocol: string): void
810
-
811
784
  /**
812
785
  * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA'
813
786
  * type this may mean searching the routing if the peer's key is not present
@@ -956,6 +929,7 @@ export * from './peer-id.js'
956
929
  export * from './peer-info.js'
957
930
  export * from './peer-routing.js'
958
931
  export * from './peer-store.js'
932
+ export * from './pubsub.js'
959
933
  export * from './record.js'
960
934
  export * from './startable.js'
961
935
  export * from './stream-handler.js'
@@ -100,25 +100,10 @@ export interface MessageStream<Timeline extends MessageStreamTimeline = MessageS
100
100
  /**
101
101
  * The maximum number of bytes to store when paused. If receipt of more bytes
102
102
  * from the remote end of the stream causes the buffer size to exceed this
103
- * value the stream will be reset and a 'close' event emitted.
104
- *
105
- * This value can be changed at runtime.
103
+ * value the stream will be reset and an 'error' event emitted.
106
104
  */
107
105
  maxReadBufferLength: number
108
106
 
109
- /**
110
- * When the `.send` method returns false it means that the underlying resource
111
- * has signalled that it's write buffer is full. If the user continues to call
112
- * `.send`, outgoing bytes are stored in an internal buffer until the
113
- * underlying resource signals that it can accept more data.
114
- *
115
- * If the size of that internal buffer exceed this value the stream will be
116
- * reset and a 'close' event emitted.
117
- *
118
- * This value can be changed at runtime.
119
- */
120
- maxWriteBufferLength?: number
121
-
122
107
  /**
123
108
  * If no data is transmitted over the stream in this many ms, the stream will
124
109
  * be aborted with an InactivityTimeoutError
package/src/peer-store.ts CHANGED
@@ -314,4 +314,9 @@ export interface PeerStore {
314
314
  * ```
315
315
  */
316
316
  consumePeerRecord(buf: Uint8Array, options?: ConsumePeerRecordOptions): Promise<boolean>
317
+
318
+ /**
319
+ * @deprecated Pass `expectedPeer` as a property of `options` instead
320
+ */
321
+ consumePeerRecord(buf: Uint8Array, expectedPeer?: PeerId, options?: AbortOptions): Promise<boolean>
317
322
  }
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
+ }
@@ -7,13 +7,6 @@ export interface StreamHandler {
7
7
  (stream: Stream, connection: Connection): void | Promise<void>
8
8
  }
9
9
 
10
- /**
11
- * Stream middleware allows accessing stream data outside of the stream handler
12
- */
13
- export interface StreamMiddleware {
14
- (stream: Stream, connection: Connection, next: (stream: Stream, connection: Connection) => void): void | Promise<void>
15
- }
16
-
17
10
  export interface StreamHandlerOptions extends AbortOptions {
18
11
  /**
19
12
  * How many incoming streams can be open for this protocol at the same time on each connection
@@ -40,11 +33,6 @@ export interface StreamHandlerOptions extends AbortOptions {
40
33
  * protocol(s), the existing handler will be discarded.
41
34
  */
42
35
  force?: true
43
-
44
- /**
45
- * Middleware allows accessing stream data outside of the stream handler
46
- */
47
- middleware?: StreamMiddleware[]
48
36
  }
49
37
 
50
38
  export interface StreamHandlerRecord {
@@ -142,7 +142,7 @@ export interface StreamMuxer<MuxedStream extends Stream = Stream> extends TypedE
142
142
  /**
143
143
  * Create a new stream
144
144
  */
145
- createStream(options?: CreateStreamOptions): Promise<MuxedStream>
145
+ createStream(options?: CreateStreamOptions): MuxedStream | Promise<MuxedStream>
146
146
 
147
147
  /**
148
148
  * Immediately close the muxer, abort every open stream and discard any
package/src/topology.ts CHANGED
@@ -5,7 +5,7 @@ import type { PeerId } from './peer-id.js'
5
5
  * A topology filter - this can be used by topologies to ensure they do not
6
6
  * receive duplicate notifications of individual peers
7
7
  *
8
- * @see https://libp2p.github.io/js-libp2p/classes/_libp2p_peer-collections.PeerFilter.html
8
+ * @see https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_collections.peerFilter-1.html
9
9
  */
10
10
  export interface TopologyFilter {
11
11
  has (peerId: PeerId): boolean