@libp2p/interface 2.11.0-6059227cb → 2.11.0-87bc8d4fb
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/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/connection-encrypter.d.ts +15 -17
- package/dist/src/connection-encrypter.d.ts.map +1 -1
- package/dist/src/connection-gater.d.ts +2 -1
- package/dist/src/connection-gater.d.ts.map +1 -1
- package/dist/src/connection.d.ts +233 -36
- package/dist/src/connection.d.ts.map +1 -1
- package/dist/src/connection.js.map +1 -1
- package/dist/src/errors.d.ts +0 -14
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +0 -20
- package/dist/src/errors.js.map +1 -1
- package/dist/src/index.d.ts +5 -35
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -7
- package/dist/src/index.js.map +1 -1
- package/dist/src/metrics.d.ts +2 -2
- package/dist/src/metrics.d.ts.map +1 -1
- package/dist/src/peer-store.d.ts +4 -0
- package/dist/src/peer-store.d.ts.map +1 -1
- package/dist/src/pubsub.d.ts +248 -0
- package/dist/src/pubsub.d.ts.map +1 -0
- package/dist/src/pubsub.js +47 -0
- package/dist/src/pubsub.js.map +1 -0
- package/dist/src/stream-handler.d.ts +13 -12
- package/dist/src/stream-handler.d.ts.map +1 -1
- package/dist/src/stream-muxer.d.ts +30 -113
- package/dist/src/stream-muxer.d.ts.map +1 -1
- package/dist/src/topology.d.ts +3 -3
- package/dist/src/topology.d.ts.map +1 -1
- package/dist/src/transport.d.ts +13 -20
- package/dist/src/transport.d.ts.map +1 -1
- package/dist/src/transport.js.map +1 -1
- package/package.json +6 -4
- package/src/connection-encrypter.ts +16 -19
- package/src/connection-gater.ts +2 -1
- package/src/connection.ts +270 -38
- package/src/errors.ts +0 -24
- package/src/index.ts +5 -38
- package/src/metrics.ts +2 -2
- package/src/peer-store.ts +5 -0
- package/src/pubsub.ts +286 -0
- package/src/stream-handler.ts +15 -14
- package/src/stream-muxer.ts +30 -122
- package/src/topology.ts +3 -3
- package/src/transport.ts +14 -25
- package/dist/src/connection-protector.d.ts +0 -9
- package/dist/src/connection-protector.d.ts.map +0 -1
- package/dist/src/connection-protector.js +0 -2
- package/dist/src/connection-protector.js.map +0 -1
- package/dist/src/events.d.ts +0 -26
- package/dist/src/events.d.ts.map +0 -1
- package/dist/src/events.js +0 -36
- package/dist/src/events.js.map +0 -1
- package/dist/src/message-stream.d.ts +0 -159
- package/dist/src/message-stream.d.ts.map +0 -1
- package/dist/src/message-stream.js +0 -2
- package/dist/src/message-stream.js.map +0 -1
- package/dist/src/multiaddr-connection.d.ts +0 -25
- package/dist/src/multiaddr-connection.d.ts.map +0 -1
- package/dist/src/multiaddr-connection.js +0 -2
- package/dist/src/multiaddr-connection.js.map +0 -1
- package/dist/src/stream.d.ts +0 -63
- package/dist/src/stream.d.ts.map +0 -1
- package/dist/src/stream.js +0 -2
- package/dist/src/stream.js.map +0 -1
- package/src/connection-protector.ts +0 -9
- package/src/events.ts +0 -44
- package/src/message-stream.ts +0 -183
- package/src/multiaddr-connection.ts +0 -27
- package/src/stream.ts +0 -70
package/src/index.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import type { Connection, NewStreamOptions } from './connection.js'
|
|
17
|
+
import type { Connection, NewStreamOptions, Stream } from './connection.js'
|
|
18
18
|
import type { ContentRouting } from './content-routing.js'
|
|
19
19
|
import type { Ed25519PublicKey, PublicKey, RSAPublicKey, Secp256k1PublicKey } from './keys.js'
|
|
20
20
|
import type { Metrics } from './metrics.js'
|
|
@@ -23,8 +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
|
|
27
|
-
import type { Stream } from './stream.js'
|
|
26
|
+
import type { StreamHandler, StreamHandlerOptions } from './stream-handler.js'
|
|
28
27
|
import type { Topology } from './topology.js'
|
|
29
28
|
import type { Listener, OutboundConnectionUpgradeEvents } from './transport.js'
|
|
30
29
|
import type { DNS } from '@multiformats/dns'
|
|
@@ -781,33 +780,6 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
|
|
|
781
780
|
*/
|
|
782
781
|
unregister(id: string): void
|
|
783
782
|
|
|
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
783
|
/**
|
|
812
784
|
* Returns the public key for the passed PeerId. If the PeerId is of the 'RSA'
|
|
813
785
|
* type this may mean searching the routing if the peer's key is not present
|
|
@@ -943,25 +915,20 @@ export const serviceDependencies = Symbol.for('@libp2p/service-dependencies')
|
|
|
943
915
|
export * from './connection.js'
|
|
944
916
|
export * from './connection-encrypter.js'
|
|
945
917
|
export * from './connection-gater.js'
|
|
946
|
-
export * from './connection-protector.js'
|
|
947
918
|
export * from './content-routing.js'
|
|
948
|
-
export * from './errors.js'
|
|
949
|
-
export * from './events.js'
|
|
950
919
|
export * from './keys.js'
|
|
951
|
-
export * from './message-stream.js'
|
|
952
920
|
export * from './metrics.js'
|
|
953
|
-
export * from './multiaddr-connection.js'
|
|
954
921
|
export * from './peer-discovery.js'
|
|
955
922
|
export * from './peer-id.js'
|
|
956
923
|
export * from './peer-info.js'
|
|
957
924
|
export * from './peer-routing.js'
|
|
958
925
|
export * from './peer-store.js'
|
|
926
|
+
export * from './pubsub.js'
|
|
959
927
|
export * from './record.js'
|
|
960
|
-
export * from './startable.js'
|
|
961
928
|
export * from './stream-handler.js'
|
|
962
929
|
export * from './stream-muxer.js'
|
|
963
|
-
export * from './stream.js'
|
|
964
930
|
export * from './topology.js'
|
|
965
931
|
export * from './transport.js'
|
|
966
|
-
|
|
932
|
+
export * from './errors.js'
|
|
967
933
|
export * from 'main-event'
|
|
934
|
+
export * from './startable.js'
|
package/src/metrics.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MultiaddrConnection, Stream } from './
|
|
1
|
+
import type { MultiaddrConnection, Stream, Connection } from './connection.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Create tracked metrics with these options. Loosely based on the
|
|
@@ -434,7 +434,7 @@ export interface Metrics {
|
|
|
434
434
|
/**
|
|
435
435
|
* Track a newly opened protocol stream
|
|
436
436
|
*/
|
|
437
|
-
trackProtocolStream(stream: Stream): void
|
|
437
|
+
trackProtocolStream(stream: Stream, connection: Connection): void
|
|
438
438
|
|
|
439
439
|
/**
|
|
440
440
|
* Register an arbitrary metric. Call this to set help/labels for metrics
|
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,286 @@
|
|
|
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
|
+
}
|
package/src/stream-handler.ts
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Connection, Stream } from './connection.js'
|
|
2
|
+
import type { AbortOptions } from './index.ts'
|
|
2
3
|
|
|
3
|
-
export interface
|
|
4
|
+
export interface IncomingStreamData {
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
+
* The newly opened stream
|
|
7
|
+
*/
|
|
8
|
+
stream: Stream
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The connection the stream was opened on
|
|
6
12
|
*/
|
|
7
|
-
|
|
13
|
+
connection: Connection
|
|
8
14
|
}
|
|
9
15
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
(
|
|
16
|
+
export interface StreamHandler {
|
|
17
|
+
/**
|
|
18
|
+
* A callback function that accepts the incoming stream data
|
|
19
|
+
*/
|
|
20
|
+
(data: IncomingStreamData): void | Promise<void>
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
export interface StreamHandlerOptions extends AbortOptions {
|
|
@@ -40,11 +46,6 @@ export interface StreamHandlerOptions extends AbortOptions {
|
|
|
40
46
|
* protocol(s), the existing handler will be discarded.
|
|
41
47
|
*/
|
|
42
48
|
force?: true
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Middleware allows accessing stream data outside of the stream handler
|
|
46
|
-
*/
|
|
47
|
-
middleware?: StreamMiddleware[]
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
export interface StreamHandlerRecord {
|
package/src/stream-muxer.ts
CHANGED
|
@@ -1,160 +1,68 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
export interface StreamMuxerOptions<MuxedStreamOptions extends StreamOptions = StreamOptions> {
|
|
7
|
-
/**
|
|
8
|
-
* Configuration options for each outgoing/incoming stream
|
|
9
|
-
*/
|
|
10
|
-
streamOptions?: MuxedStreamOptions
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* libp2p is notified of incoming streams via the muxer's 'stream' event.
|
|
14
|
-
*
|
|
15
|
-
* During connection establishment there may be a small window where a muxer
|
|
16
|
-
* starts to process incoming stream data before a listener has been added for
|
|
17
|
-
* the 'stream' event.
|
|
18
|
-
*
|
|
19
|
-
* If no handler is registered for this event incoming streams can be missed
|
|
20
|
-
* so when this is the case muxers queue streams internally as "early
|
|
21
|
-
* streams", and will defer emitting the 'stream' event until after a listener
|
|
22
|
-
* has been registered.
|
|
23
|
-
*
|
|
24
|
-
* Allowing an unlimited amount of early streams can cause excessive memory
|
|
25
|
-
* consumption so this setting controls how many early streams to store when
|
|
26
|
-
* no 'stream' listener has been registered.
|
|
27
|
-
*
|
|
28
|
-
* If more streams than this are opened before a listener is added the muxed
|
|
29
|
-
* connection will be reset.
|
|
30
|
-
*
|
|
31
|
-
* @default 10
|
|
32
|
-
*/
|
|
33
|
-
maxEarlyStreams?: number
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Maximum size of a message in bytes that we'll send on a stream - this
|
|
37
|
-
* ensures that a single stream doesn't hog a connection
|
|
38
|
-
*
|
|
39
|
-
* @default 65_536
|
|
40
|
-
*/
|
|
41
|
-
maxMessageSize?: number
|
|
1
|
+
import type { Direction, Stream } from './connection.js'
|
|
2
|
+
import type { AbortOptions, Logger } from './index.js'
|
|
3
|
+
import type { Duplex } from 'it-stream-types'
|
|
4
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
42
5
|
|
|
6
|
+
export interface StreamMuxerFactory {
|
|
43
7
|
/**
|
|
44
|
-
*
|
|
45
|
-
* tries to open more streams, those will be reset immediately
|
|
46
|
-
*
|
|
47
|
-
* @default 1_000
|
|
8
|
+
* The protocol used to select this muxer during connection opening
|
|
48
9
|
*/
|
|
49
|
-
|
|
10
|
+
protocol: string
|
|
50
11
|
|
|
51
12
|
/**
|
|
52
|
-
*
|
|
53
|
-
* application tries to open more streams, the call to `newStream` will throw
|
|
54
|
-
*
|
|
55
|
-
* @default 1_000
|
|
13
|
+
* Creates a new stream muxer to be used with a new connection
|
|
56
14
|
*/
|
|
57
|
-
|
|
15
|
+
createStreamMuxer(init?: StreamMuxerInit): StreamMuxer
|
|
58
16
|
}
|
|
59
17
|
|
|
60
18
|
/**
|
|
61
|
-
*
|
|
19
|
+
* A libp2p stream muxer
|
|
62
20
|
*/
|
|
63
|
-
export interface
|
|
64
|
-
/**
|
|
65
|
-
* If no data is sent or received in this number of ms the stream will be
|
|
66
|
-
* reset and an 'error' event emitted.
|
|
67
|
-
*
|
|
68
|
-
* @default 120_000
|
|
69
|
-
*/
|
|
70
|
-
inactivityTimeout?: number
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The maximum number of bytes to store when paused or before a 'message'
|
|
74
|
-
* event handler is added.
|
|
75
|
-
*
|
|
76
|
-
* If the internal buffer overflows this value the stream will be reset.
|
|
77
|
-
*
|
|
78
|
-
* @default 4_194_304
|
|
79
|
-
*/
|
|
80
|
-
maxReadBufferLength?: number
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* The maximum number of bytes to store when the remote end of the stream is
|
|
84
|
-
* applying backpressure, or when it is slow to accept new bytes.
|
|
85
|
-
*
|
|
86
|
-
* If the internal buffer overflows this value the stream will be reset.
|
|
87
|
-
*
|
|
88
|
-
* @default Infinity
|
|
89
|
-
*/
|
|
90
|
-
maxWriteBufferLength?: number
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export interface StreamMuxerFactory<Muxer extends StreamMuxer = StreamMuxer> {
|
|
21
|
+
export interface StreamMuxer extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> {
|
|
94
22
|
/**
|
|
95
23
|
* The protocol used to select this muxer during connection opening
|
|
96
24
|
*/
|
|
97
25
|
protocol: string
|
|
98
26
|
|
|
99
27
|
/**
|
|
100
|
-
*
|
|
101
|
-
*/
|
|
102
|
-
createStreamMuxer(maConn: MessageStream): Muxer
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export interface StreamMuxerEvents<MuxedStream extends Stream = Stream> {
|
|
106
|
-
/**
|
|
107
|
-
* An incoming stream was created
|
|
28
|
+
* A list of streams that are currently open. Closed streams will not be returned.
|
|
108
29
|
*/
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface CreateStreamOptions extends AbortOptions, StreamOptions {
|
|
30
|
+
readonly streams: Stream[]
|
|
113
31
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* will be run over the stream after opening.
|
|
32
|
+
* Initiate a new stream with the given name. If no name is
|
|
33
|
+
* provided, the id of the stream will be used.
|
|
117
34
|
*/
|
|
118
|
-
|
|
119
|
-
}
|
|
35
|
+
newStream(name?: string): Stream | Promise<Stream>
|
|
120
36
|
|
|
121
|
-
export type StreamMuxerStatus = 'open' | 'closing' | 'closed'
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* A libp2p stream muxer
|
|
125
|
-
*/
|
|
126
|
-
export interface StreamMuxer<MuxedStream extends Stream = Stream> extends TypedEventTarget<StreamMuxerEvents<MuxedStream>> {
|
|
127
37
|
/**
|
|
128
|
-
*
|
|
38
|
+
* Close or abort all tracked streams and stop the muxer
|
|
129
39
|
*/
|
|
130
|
-
|
|
40
|
+
close(options?: AbortOptions): Promise<void>
|
|
131
41
|
|
|
132
42
|
/**
|
|
133
|
-
*
|
|
43
|
+
* Close or abort all tracked streams and stop the muxer
|
|
134
44
|
*/
|
|
135
|
-
|
|
45
|
+
abort(err: Error): void
|
|
46
|
+
}
|
|
136
47
|
|
|
48
|
+
export interface StreamMuxerInit {
|
|
137
49
|
/**
|
|
138
|
-
*
|
|
50
|
+
* A callback function invoked every time an incoming stream is opened
|
|
139
51
|
*/
|
|
140
|
-
|
|
52
|
+
onIncomingStream?(stream: Stream): void
|
|
141
53
|
|
|
142
54
|
/**
|
|
143
|
-
*
|
|
55
|
+
* A callback function invoke every time a stream ends
|
|
144
56
|
*/
|
|
145
|
-
|
|
57
|
+
onStreamEnd?(stream: Stream): void
|
|
146
58
|
|
|
147
59
|
/**
|
|
148
|
-
*
|
|
149
|
-
* unsent/unread data.
|
|
60
|
+
* Outbound stream muxers are opened by the local node, inbound stream muxers are opened by the remote
|
|
150
61
|
*/
|
|
151
|
-
|
|
62
|
+
direction?: Direction
|
|
152
63
|
|
|
153
64
|
/**
|
|
154
|
-
*
|
|
155
|
-
* the returned promise will either resolve when any/all unsent data has been
|
|
156
|
-
* sent, or it will reject if the passed abort signal fires before this
|
|
157
|
-
* happens.
|
|
65
|
+
* The logger used by the connection
|
|
158
66
|
*/
|
|
159
|
-
|
|
67
|
+
log?: Logger
|
|
160
68
|
}
|
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/
|
|
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
|
|
@@ -40,11 +40,11 @@ export interface Topology {
|
|
|
40
40
|
* Invoked when a new connection is opened to a peer that supports the
|
|
41
41
|
* registered protocol
|
|
42
42
|
*/
|
|
43
|
-
onConnect?(peerId: PeerId, conn: Connection): void
|
|
43
|
+
onConnect?(peerId: PeerId, conn: Connection): void
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Invoked when the last connection to a peer that supports the registered
|
|
47
47
|
* protocol closes
|
|
48
48
|
*/
|
|
49
|
-
onDisconnect?(peerId: PeerId): void
|
|
49
|
+
onDisconnect?(peerId: PeerId): void
|
|
50
50
|
}
|