libp2p 0.37.2 → 0.37.3-2836acc

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 (151) hide show
  1. package/README.md +1 -0
  2. package/dist/src/address-manager/index.d.ts +2 -2
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/circuit/auto-relay.d.ts +4 -4
  5. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  6. package/dist/src/circuit/circuit/hop.d.ts +2 -2
  7. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  8. package/dist/src/circuit/circuit/hop.js +2 -2
  9. package/dist/src/circuit/circuit/hop.js.map +1 -1
  10. package/dist/src/circuit/circuit/stop.d.ts +1 -1
  11. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  12. package/dist/src/circuit/circuit/stop.js +1 -1
  13. package/dist/src/circuit/circuit/stop.js.map +1 -1
  14. package/dist/src/circuit/circuit/stream-handler.d.ts +1 -1
  15. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  16. package/dist/src/circuit/index.d.ts +2 -2
  17. package/dist/src/circuit/index.d.ts.map +1 -1
  18. package/dist/src/circuit/listener.d.ts +3 -3
  19. package/dist/src/circuit/listener.d.ts.map +1 -1
  20. package/dist/src/circuit/transport.d.ts +5 -5
  21. package/dist/src/circuit/transport.d.ts.map +1 -1
  22. package/dist/src/circuit/transport.js +2 -2
  23. package/dist/src/circuit/transport.js.map +1 -1
  24. package/dist/src/config.d.ts.map +1 -1
  25. package/dist/src/config.js +11 -3
  26. package/dist/src/config.js.map +1 -1
  27. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  28. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  29. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +2 -2
  30. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -1
  31. package/dist/src/connection-manager/dialer/dial-request.d.ts +1 -1
  32. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  33. package/dist/src/connection-manager/dialer/index.d.ts +5 -5
  34. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  35. package/dist/src/connection-manager/dialer/index.js +1 -1
  36. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  37. package/dist/src/connection-manager/index.d.ts +16 -19
  38. package/dist/src/connection-manager/index.d.ts.map +1 -1
  39. package/dist/src/connection-manager/index.js +95 -59
  40. package/dist/src/connection-manager/index.js.map +1 -1
  41. package/dist/src/content-routing/index.d.ts +4 -4
  42. package/dist/src/content-routing/index.d.ts.map +1 -1
  43. package/dist/src/content-routing/utils.d.ts +2 -2
  44. package/dist/src/content-routing/utils.d.ts.map +1 -1
  45. package/dist/src/dht/dht-content-routing.d.ts +3 -3
  46. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  47. package/dist/src/dht/dht-peer-routing.d.ts +5 -5
  48. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  49. package/dist/src/dht/dummy-dht.d.ts +3 -3
  50. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  51. package/dist/src/dht/dummy-dht.js +1 -1
  52. package/dist/src/dht/dummy-dht.js.map +1 -1
  53. package/dist/src/errors.d.ts +4 -1
  54. package/dist/src/errors.d.ts.map +1 -1
  55. package/dist/src/errors.js +3 -0
  56. package/dist/src/errors.js.map +1 -1
  57. package/dist/src/fetch/index.d.ts +7 -4
  58. package/dist/src/fetch/index.d.ts.map +1 -1
  59. package/dist/src/fetch/index.js +61 -39
  60. package/dist/src/fetch/index.js.map +1 -1
  61. package/dist/src/get-peer.d.ts +2 -2
  62. package/dist/src/get-peer.d.ts.map +1 -1
  63. package/dist/src/get-peer.js +1 -1
  64. package/dist/src/get-peer.js.map +1 -1
  65. package/dist/src/identify/index.d.ts +7 -7
  66. package/dist/src/identify/index.d.ts.map +1 -1
  67. package/dist/src/identify/index.js +15 -23
  68. package/dist/src/identify/index.js.map +1 -1
  69. package/dist/src/index.d.ts +17 -17
  70. package/dist/src/index.d.ts.map +1 -1
  71. package/dist/src/insecure/index.d.ts +2 -2
  72. package/dist/src/insecure/index.d.ts.map +1 -1
  73. package/dist/src/insecure/index.js +1 -1
  74. package/dist/src/insecure/index.js.map +1 -1
  75. package/dist/src/keychain/index.d.ts +2 -2
  76. package/dist/src/keychain/index.d.ts.map +1 -1
  77. package/dist/src/libp2p.d.ts +14 -14
  78. package/dist/src/libp2p.d.ts.map +1 -1
  79. package/dist/src/libp2p.js +14 -15
  80. package/dist/src/libp2p.js.map +1 -1
  81. package/dist/src/metrics/index.d.ts +2 -2
  82. package/dist/src/metrics/index.d.ts.map +1 -1
  83. package/dist/src/metrics/moving-average.d.ts +1 -1
  84. package/dist/src/metrics/moving-average.d.ts.map +1 -1
  85. package/dist/src/metrics/stats.d.ts +1 -1
  86. package/dist/src/metrics/stats.d.ts.map +1 -1
  87. package/dist/src/nat-manager.d.ts +1 -1
  88. package/dist/src/nat-manager.d.ts.map +1 -1
  89. package/dist/src/peer-record-updater.d.ts +1 -1
  90. package/dist/src/peer-record-updater.d.ts.map +1 -1
  91. package/dist/src/peer-routing.d.ts +4 -4
  92. package/dist/src/peer-routing.d.ts.map +1 -1
  93. package/dist/src/ping/index.d.ts +6 -3
  94. package/dist/src/ping/index.d.ts.map +1 -1
  95. package/dist/src/ping/index.js +6 -2
  96. package/dist/src/ping/index.js.map +1 -1
  97. package/dist/src/pnet/index.d.ts +1 -2
  98. package/dist/src/pnet/index.d.ts.map +1 -1
  99. package/dist/src/pnet/index.js.map +1 -1
  100. package/dist/src/pubsub/dummy-pubsub.d.ts +2 -2
  101. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  102. package/dist/src/registrar.d.ts +10 -8
  103. package/dist/src/registrar.d.ts.map +1 -1
  104. package/dist/src/registrar.js +63 -44
  105. package/dist/src/registrar.js.map +1 -1
  106. package/dist/src/transport-manager.d.ts +3 -3
  107. package/dist/src/transport-manager.d.ts.map +1 -1
  108. package/dist/src/upgrader.d.ts +6 -6
  109. package/dist/src/upgrader.d.ts.map +1 -1
  110. package/dist/src/upgrader.js +69 -8
  111. package/dist/src/upgrader.js.map +1 -1
  112. package/package.json +53 -30
  113. package/src/address-manager/index.ts +2 -2
  114. package/src/circuit/auto-relay.ts +4 -4
  115. package/src/circuit/circuit/hop.ts +4 -4
  116. package/src/circuit/circuit/stop.ts +2 -2
  117. package/src/circuit/circuit/stream-handler.ts +1 -1
  118. package/src/circuit/index.ts +2 -2
  119. package/src/circuit/listener.ts +3 -3
  120. package/src/circuit/transport.ts +5 -5
  121. package/src/config.ts +11 -3
  122. package/src/connection-manager/auto-dialler.ts +1 -1
  123. package/src/connection-manager/dialer/auto-dialer.ts +2 -2
  124. package/src/connection-manager/dialer/dial-request.ts +1 -1
  125. package/src/connection-manager/dialer/index.ts +5 -5
  126. package/src/connection-manager/index.ts +126 -71
  127. package/src/content-routing/index.ts +2 -2
  128. package/src/content-routing/utils.ts +2 -2
  129. package/src/dht/dht-content-routing.ts +2 -2
  130. package/src/dht/dht-peer-routing.ts +3 -3
  131. package/src/dht/dummy-dht.ts +3 -3
  132. package/src/errors.ts +4 -1
  133. package/src/fetch/index.ts +91 -50
  134. package/src/get-peer.ts +3 -3
  135. package/src/identify/index.ts +24 -29
  136. package/src/index.ts +17 -17
  137. package/src/insecure/index.ts +3 -3
  138. package/src/keychain/index.ts +2 -2
  139. package/src/libp2p.ts +31 -27
  140. package/src/metrics/index.ts +2 -2
  141. package/src/metrics/moving-average.ts +1 -1
  142. package/src/metrics/stats.ts +1 -1
  143. package/src/nat-manager.ts +1 -1
  144. package/src/peer-record-updater.ts +1 -1
  145. package/src/peer-routing.ts +4 -4
  146. package/src/ping/index.ts +12 -5
  147. package/src/pnet/index.ts +1 -2
  148. package/src/pubsub/dummy-pubsub.ts +2 -2
  149. package/src/registrar.ts +79 -55
  150. package/src/transport-manager.ts +3 -3
  151. package/src/upgrader.ts +94 -16
@@ -18,11 +18,11 @@ import {
18
18
  MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
19
19
  } from './consts.js'
20
20
  import { codes } from '../errors.js'
21
- import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
22
- import type { Connection, Stream } from '@libp2p/interfaces/connection'
21
+ import type { IncomingStreamData } from '@libp2p/interface-registrar'
22
+ import type { Connection, Stream } from '@libp2p/interface-connection'
23
23
  import type { Startable } from '@libp2p/interfaces/startable'
24
24
  import { peerIdFromKeys } from '@libp2p/peer-id'
25
- import type { Components } from '@libp2p/interfaces/components'
25
+ import type { Components } from '@libp2p/components'
26
26
  import { TimeoutController } from 'timeout-abort-controller'
27
27
  import type { AbortOptions } from '@libp2p/interfaces'
28
28
  import { abortableDuplex } from 'abortable-iterator'
@@ -60,6 +60,12 @@ export interface IdentifyServiceInit {
60
60
  * Identify responses larger than this in bytes will be rejected (default: 8192)
61
61
  */
62
62
  maxIdentifyMessageSize?: number
63
+
64
+ maxInboundStreams: number
65
+ maxOutboundStreams: number
66
+
67
+ maxPushIncomingStreams: number
68
+ maxPushOutgoingStreams: number
63
69
  }
64
70
 
65
71
  export class IdentifyService implements Startable {
@@ -79,8 +85,6 @@ export class IdentifyService implements Startable {
79
85
  this.started = false
80
86
  this.init = init
81
87
 
82
- this.handleMessage = this.handleMessage.bind(this)
83
-
84
88
  this.identifyProtocolStr = `/${init.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`
85
89
  this.identifyPushProtocolStr = `/${init.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
86
90
 
@@ -127,13 +131,21 @@ export class IdentifyService implements Startable {
127
131
  await this.components.getPeerStore().metadataBook.setValue(this.components.getPeerId(), 'AgentVersion', uint8ArrayFromString(this.host.agentVersion))
128
132
  await this.components.getPeerStore().metadataBook.setValue(this.components.getPeerId(), 'ProtocolVersion', uint8ArrayFromString(this.host.protocolVersion))
129
133
 
130
- await this.components.getRegistrar().handle([
131
- this.identifyProtocolStr,
132
- this.identifyPushProtocolStr
133
- ], (data) => {
134
- void this.handleMessage(data)?.catch(err => {
134
+ await this.components.getRegistrar().handle(this.identifyProtocolStr, (data) => {
135
+ void this._handleIdentify(data).catch(err => {
136
+ log.error(err)
137
+ })
138
+ }, {
139
+ maxInboundStreams: this.init.maxInboundStreams,
140
+ maxOutboundStreams: this.init.maxOutboundStreams
141
+ })
142
+ await this.components.getRegistrar().handle(this.identifyPushProtocolStr, (data) => {
143
+ void this._handlePush(data).catch(err => {
135
144
  log.error(err)
136
145
  })
146
+ }, {
147
+ maxInboundStreams: this.init.maxPushIncomingStreams,
148
+ maxOutboundStreams: this.init.maxPushOutgoingStreams
137
149
  })
138
150
 
139
151
  this.started = true
@@ -159,10 +171,9 @@ export class IdentifyService implements Startable {
159
171
  let stream: Stream | undefined
160
172
 
161
173
  try {
162
- const data = await connection.newStream([this.identifyPushProtocolStr], {
174
+ stream = await connection.newStream([this.identifyPushProtocolStr], {
163
175
  signal: timeoutController.signal
164
176
  })
165
- stream = data.stream
166
177
 
167
178
  // make stream abortable
168
179
  const source: Duplex<Uint8Array> = abortableDuplex(stream, timeoutController.signal)
@@ -218,7 +229,7 @@ export class IdentifyService implements Startable {
218
229
  }
219
230
 
220
231
  async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
221
- const { stream } = await connection.newStream([this.identifyProtocolStr], options)
232
+ const stream = await connection.newStream([this.identifyProtocolStr], options)
222
233
  let source: Duplex<Uint8Array> = stream
223
234
  let timeoutController
224
235
  let signal = options.signal
@@ -353,22 +364,6 @@ export class IdentifyService implements Startable {
353
364
  // this.components.getAddressManager().addObservedAddr(observedAddr)
354
365
  }
355
366
 
356
- /**
357
- * A handler to register with Libp2p to process identify messages
358
- */
359
- handleMessage (data: IncomingStreamData) {
360
- const { protocol } = data
361
-
362
- switch (protocol) {
363
- case this.identifyProtocolStr:
364
- return this._handleIdentify(data)
365
- case this.identifyPushProtocolStr:
366
- return this._handlePush(data)
367
- default:
368
- log.error('cannot handle unknown protocol %s', protocol)
369
- }
370
- }
371
-
372
367
  /**
373
368
  * Sends the `Identify` response with the Signed Peer Record
374
369
  * to the requesting peer over the given `connection`
package/src/index.ts CHANGED
@@ -5,23 +5,23 @@ import type { Startable } from '@libp2p/interfaces/startable'
5
5
  import type { Multiaddr } from '@multiformats/multiaddr'
6
6
  import type { FaultTolerance } from './transport-manager.js'
7
7
  import type { IdentifyServiceInit } from './identify/index.js'
8
- import type { DualDHT } from '@libp2p/interfaces/dht'
8
+ import type { DualDHT } from '@libp2p/interface-dht'
9
9
  import type { Datastore } from 'interface-datastore'
10
- import type { PeerStore, PeerStoreInit } from '@libp2p/interfaces/peer-store'
11
- import type { PeerId } from '@libp2p/interfaces/peer-id'
10
+ import type { PeerStore, PeerStoreInit } from '@libp2p/interface-peer-store'
11
+ import type { PeerId } from '@libp2p/interface-peer-id'
12
12
  import type { AutoRelayConfig, RelayAdvertiseConfig } from './circuit/index.js'
13
- import type { PeerDiscovery } from '@libp2p/interfaces/peer-discovery'
14
- import type { Connection, ConnectionGater, ConnectionProtector, ProtocolStream } from '@libp2p/interfaces/connection'
15
- import type { Transport } from '@libp2p/interfaces/transport'
16
- import type { StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
17
- import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
18
- import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
19
- import type { ContentRouting } from '@libp2p/interfaces/content-routing'
20
- import type { PubSub } from '@libp2p/interfaces/pubsub'
21
- import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
22
- import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
23
- import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
24
- import type { PeerInfo } from '@libp2p/interfaces/peer-info'
13
+ import type { PeerDiscovery } from '@libp2p/interface-peer-discovery'
14
+ import type { Connection, ConnectionGater, ConnectionProtector, Stream } from '@libp2p/interface-connection'
15
+ import type { Transport } from '@libp2p/interface-transport'
16
+ import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
17
+ import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter'
18
+ import type { PeerRouting } from '@libp2p/interface-peer-routing'
19
+ import type { ContentRouting } from '@libp2p/interface-content-routing'
20
+ import type { PubSub } from '@libp2p/interface-pubsub'
21
+ import type { Registrar, StreamHandler, StreamHandlerOptions } from '@libp2p/interface-registrar'
22
+ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
23
+ import type { Metrics, MetricsInit } from '@libp2p/interface-metrics'
24
+ import type { PeerInfo } from '@libp2p/interface-peer-info'
25
25
  import type { KeyChain } from './keychain/index.js'
26
26
  import type { ConnectionManagerInit } from './connection-manager/index.js'
27
27
  import type { PingServiceInit } from './ping/index.js'
@@ -177,7 +177,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
177
177
  * If successful, the known metadata of the peer will be added to the nodes `peerStore`,
178
178
  * and the `MuxedStream` will be returned together with the successful negotiated protocol.
179
179
  */
180
- dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<ProtocolStream>
180
+ dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<Stream>
181
181
 
182
182
  /**
183
183
  * Disconnects all connections to the given `peer`
@@ -187,7 +187,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
187
187
  /**
188
188
  * Registers the `handler` for each protocol
189
189
  */
190
- handle: (protocol: string | string[], handler: StreamHandler) => Promise<void>
190
+ handle: (protocol: string | string[], handler: StreamHandler, options?: StreamHandlerOptions) => Promise<void>
191
191
 
192
192
  /**
193
193
  * Removes the handler for each protocol. The protocol
@@ -1,11 +1,11 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import { handshake } from 'it-handshake'
3
3
  import * as lp from 'it-length-prefixed'
4
- import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interfaces/connection-encrypter/errors'
4
+ import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interface-connection-encrypter/errors'
5
5
  import { Exchange, KeyType } from './pb/proto.js'
6
- import type { PeerId } from '@libp2p/interfaces/peer-id'
6
+ import type { PeerId } from '@libp2p/interface-peer-id'
7
7
  import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
8
- import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
8
+ import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
9
9
  import type { Duplex } from 'it-stream-types'
10
10
 
11
11
  const log = logger('libp2p:plaintext')
@@ -10,8 +10,8 @@ import { codes } from '../errors.js'
10
10
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
11
11
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
12
12
  import { generateKeyPair, importKey, unmarshalPrivateKey } from '@libp2p/crypto/keys'
13
- import type { PeerId } from '@libp2p/interfaces/peer-id'
14
- import type { Components } from '@libp2p/interfaces/components'
13
+ import type { PeerId } from '@libp2p/interface-peer-id'
14
+ import type { Components } from '@libp2p/components'
15
15
  import { pbkdf2, randomBytes } from '@libp2p/crypto'
16
16
 
17
17
  const log = logger('libp2p:keychain')
package/src/libp2p.ts CHANGED
@@ -27,25 +27,25 @@ import { DHTPeerRouting } from './dht/dht-peer-routing.js'
27
27
  import { PersistentPeerStore } from '@libp2p/peer-store'
28
28
  import { DHTContentRouting } from './dht/dht-content-routing.js'
29
29
  import { AutoDialer } from './connection-manager/dialer/auto-dialer.js'
30
- import { Initializable, Components, isInitializable } from '@libp2p/interfaces/components'
31
- import type { PeerId } from '@libp2p/interfaces/peer-id'
32
- import type { Connection } from '@libp2p/interfaces/connection'
33
- import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
34
- import type { ContentRouting } from '@libp2p/interfaces/content-routing'
35
- import type { PubSub } from '@libp2p/interfaces/pubsub'
36
- import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
37
- import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
38
- import type { PeerInfo } from '@libp2p/interfaces/peer-info'
30
+ import { Initializable, Components, isInitializable } from '@libp2p/components'
31
+ import type { PeerId } from '@libp2p/interface-peer-id'
32
+ import type { Connection } from '@libp2p/interface-connection'
33
+ import type { PeerRouting } from '@libp2p/interface-peer-routing'
34
+ import type { ContentRouting } from '@libp2p/interface-content-routing'
35
+ import type { PubSub } from '@libp2p/interface-pubsub'
36
+ import type { Registrar, StreamHandler, StreamHandlerOptions } from '@libp2p/interface-registrar'
37
+ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
38
+ import type { PeerInfo } from '@libp2p/interface-peer-info'
39
39
  import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
40
40
  import { validateConfig } from './config.js'
41
41
  import { createEd25519PeerId } from '@libp2p/peer-id-factory'
42
- import type { PeerStore } from '@libp2p/interfaces/peer-store'
43
- import type { DualDHT } from '@libp2p/interfaces/dht'
42
+ import type { PeerStore } from '@libp2p/interface-peer-store'
43
+ import type { DualDHT } from '@libp2p/interface-dht'
44
44
  import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
45
45
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
46
46
  import errCode from 'err-code'
47
47
  import { unmarshalPublicKey } from '@libp2p/crypto/keys'
48
- import type { Metrics } from '@libp2p/interfaces/metrics'
48
+ import type { Metrics } from '@libp2p/interface-metrics'
49
49
  import { DummyDHT } from './dht/dummy-dht.js'
50
50
  import { DummyPubSub } from './pubsub/dummy-pubsub.js'
51
51
  import { PeerSet } from '@libp2p/peer-collections'
@@ -309,18 +309,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
309
309
  )
310
310
 
311
311
  log('libp2p has started')
312
-
313
- // Once we start, emit any peers we may have already discovered
314
- // TODO: this should be removed, as we already discovered these peers in the past
315
- await this.components.getPeerStore().forEach(peer => {
316
- this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', {
317
- detail: {
318
- id: peer.id,
319
- multiaddrs: peer.addresses.map(addr => addr.multiaddr),
320
- protocols: peer.protocols
321
- }
322
- }))
323
- })
324
312
  } catch (err: any) {
325
313
  log.error('An error occurred starting libp2p', err)
326
314
  await this.stop()
@@ -490,12 +478,28 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
490
478
  return await this.pingService.ping(id, options)
491
479
  }
492
480
 
493
- async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
494
- return await this.components.getRegistrar().handle(protocols, handler)
481
+ async handle (protocols: string | string[], handler: StreamHandler, options?: StreamHandlerOptions): Promise<void> {
482
+ if (!Array.isArray(protocols)) {
483
+ protocols = [protocols]
484
+ }
485
+
486
+ await Promise.all(
487
+ protocols.map(async protocol => {
488
+ await this.components.getRegistrar().handle(protocol, handler, options)
489
+ })
490
+ )
495
491
  }
496
492
 
497
493
  async unhandle (protocols: string[] | string): Promise<void> {
498
- return await this.components.getRegistrar().unhandle(protocols)
494
+ if (!Array.isArray(protocols)) {
495
+ protocols = [protocols]
496
+ }
497
+
498
+ await Promise.all(
499
+ protocols.map(async protocol => {
500
+ await this.components.getRegistrar().unhandle(protocol)
501
+ })
502
+ )
499
503
  }
500
504
 
501
505
  /**
@@ -3,8 +3,8 @@ import each from 'it-foreach'
3
3
  import LRU from 'hashlru'
4
4
  import { METRICS as defaultOptions } from '../constants.js'
5
5
  import { DefaultStats, StatsInit } from './stats.js'
6
- import type { ComponentMetricsUpdate, Metrics, Stats, TrackStreamOptions } from '@libp2p/interfaces/metrics'
7
- import type { PeerId } from '@libp2p/interfaces/peer-id'
6
+ import type { ComponentMetricsUpdate, Metrics, Stats, TrackStreamOptions } from '@libp2p/interface-metrics'
7
+ import type { PeerId } from '@libp2p/interface-peer-id'
8
8
  import type { Startable } from '@libp2p/interfaces/startable'
9
9
  import type { Duplex } from 'it-stream-types'
10
10
 
@@ -1,4 +1,4 @@
1
- import type { MovingAverage } from '@libp2p/interfaces/metrics'
1
+ import type { MovingAverage } from '@libp2p/interface-metrics'
2
2
 
3
3
  export class DefaultMovingAverage {
4
4
  public movingAverage: number
@@ -2,7 +2,7 @@ import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
2
2
  import { createMovingAverage } from './moving-average.js'
3
3
  // @ts-expect-error no types
4
4
  import retimer from 'retimer'
5
- import type { MovingAverages, Stats, TransferStats } from '@libp2p/interfaces/metrics'
5
+ import type { MovingAverages, Stats, TransferStats } from '@libp2p/interface-metrics'
6
6
 
7
7
  export interface StatsEvents {
8
8
  'update': CustomEvent<TransferStats>
@@ -8,7 +8,7 @@ import errCode from 'err-code'
8
8
  import { codes } from './errors.js'
9
9
  import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
10
10
  import type { Startable } from '@libp2p/interfaces/startable'
11
- import type { Components } from '@libp2p/interfaces/components'
11
+ import type { Components } from '@libp2p/components'
12
12
 
13
13
  const log = logger('libp2p:nat')
14
14
  const DEFAULT_TTL = 7200
@@ -1,5 +1,5 @@
1
1
  import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
2
- import type { Components } from '@libp2p/interfaces/components'
2
+ import type { Components } from '@libp2p/components'
3
3
  import type { Startable } from '@libp2p/interfaces/startable'
4
4
  import { logger } from '@libp2p/logger'
5
5
  import { protocols } from '@multiformats/multiaddr'
@@ -18,12 +18,12 @@ import {
18
18
  // @ts-expect-error module with no types
19
19
  } from 'set-delayed-interval'
20
20
  import { setMaxListeners } from 'events'
21
- import type { PeerId } from '@libp2p/interfaces/peer-id'
22
- import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
21
+ import type { PeerId } from '@libp2p/interface-peer-id'
22
+ import type { PeerRouting } from '@libp2p/interface-peer-routing'
23
23
  import type { AbortOptions } from '@libp2p/interfaces'
24
24
  import type { Startable } from '@libp2p/interfaces/startable'
25
- import type { PeerInfo } from '@libp2p/interfaces/peer-info'
26
- import type { Components } from '@libp2p/interfaces/components'
25
+ import type { PeerInfo } from '@libp2p/interface-peer-info'
26
+ import type { Components } from '@libp2p/components'
27
27
 
28
28
  const log = logger('libp2p:peer-routing')
29
29
 
package/src/ping/index.ts CHANGED
@@ -6,10 +6,10 @@ import { pipe } from 'it-pipe'
6
6
  import first from 'it-first'
7
7
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
8
8
  import { PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION } from './constants.js'
9
- import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
10
- import type { PeerId } from '@libp2p/interfaces/peer-id'
9
+ import type { IncomingStreamData } from '@libp2p/interface-registrar'
10
+ import type { PeerId } from '@libp2p/interface-peer-id'
11
11
  import type { Startable } from '@libp2p/interfaces/startable'
12
- import type { Components } from '@libp2p/interfaces/components'
12
+ import type { Components } from '@libp2p/components'
13
13
  import type { AbortOptions } from '@libp2p/interfaces'
14
14
  import type { Duplex } from 'it-stream-types'
15
15
  import { abortableDuplex } from 'abortable-iterator'
@@ -18,21 +18,28 @@ const log = logger('libp2p:ping')
18
18
 
19
19
  export interface PingServiceInit {
20
20
  protocolPrefix: string
21
+ maxInboundStreams: number
22
+ maxOutboundStreams: number
21
23
  }
22
24
 
23
25
  export class PingService implements Startable {
24
26
  public readonly protocol: string
25
27
  private readonly components: Components
26
28
  private started: boolean
29
+ private readonly init: PingServiceInit
27
30
 
28
31
  constructor (components: Components, init: PingServiceInit) {
29
32
  this.components = components
30
33
  this.started = false
31
34
  this.protocol = `/${init.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
35
+ this.init = init
32
36
  }
33
37
 
34
38
  async start () {
35
- await this.components.getRegistrar().handle(this.protocol, this.handleMessage)
39
+ await this.components.getRegistrar().handle(this.protocol, this.handleMessage, {
40
+ maxInboundStreams: this.init.maxInboundStreams,
41
+ maxOutboundStreams: this.init.maxOutboundStreams
42
+ })
36
43
  this.started = true
37
44
  }
38
45
 
@@ -67,7 +74,7 @@ export class PingService implements Startable {
67
74
  log('dialing %s to %p', this.protocol, peer)
68
75
 
69
76
  const connection = await this.components.getConnectionManager().openConnection(peer, options)
70
- const { stream } = await connection.newStream([this.protocol], options)
77
+ const stream = await connection.newStream([this.protocol], options)
71
78
  const start = Date.now()
72
79
  const data = randomBytes(PING_LENGTH)
73
80
 
package/src/pnet/index.ts CHANGED
@@ -12,8 +12,7 @@ import {
12
12
  } from './crypto.js'
13
13
  import { handshake } from 'it-handshake'
14
14
  import { NONCE_LENGTH } from './key-generator.js'
15
- import type { MultiaddrConnection } from '@libp2p/interfaces/transport'
16
- import type { ConnectionProtector } from '@libp2p/interfaces/connection'
15
+ import type { ConnectionProtector, MultiaddrConnection } from '@libp2p/interface-connection'
17
16
 
18
17
  const log = logger('libp2p:pnet')
19
18
 
@@ -1,6 +1,6 @@
1
1
  import { EventEmitter } from '@libp2p/interfaces/events'
2
- import type { PeerId } from '@libp2p/interfaces/peer-id'
3
- import type { PublishResult, PubSub, PubSubEvents, StrictNoSign, StrictSign } from '@libp2p/interfaces/pubsub'
2
+ import type { PeerId } from '@libp2p/interface-peer-id'
3
+ import type { PublishResult, PubSub, PubSubEvents, StrictNoSign, StrictSign } from '@libp2p/interface-pubsub'
4
4
  import errCode from 'err-code'
5
5
  import { messages, codes } from '../errors.js'
6
6
 
package/src/registrar.ts CHANGED
@@ -1,30 +1,24 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import errCode from 'err-code'
3
3
  import { codes } from './errors.js'
4
- import { isTopology, Topology } from '@libp2p/interfaces/topology'
5
- import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
6
- import type { PeerProtocolsChangeData } from '@libp2p/interfaces/peer-store'
7
- import type { Connection } from '@libp2p/interfaces/connection'
8
- import type { Components } from '@libp2p/interfaces/components'
4
+ import { isTopology, StreamHandlerOptions, StreamHandlerRecord } from '@libp2p/interface-registrar'
5
+ import merge from 'merge-options'
6
+ import type { Registrar, StreamHandler, Topology } from '@libp2p/interface-registrar'
7
+ import type { PeerProtocolsChangeData } from '@libp2p/interface-peer-store'
8
+ import type { Connection } from '@libp2p/interface-connection'
9
+ import type { Components } from '@libp2p/components'
9
10
 
10
11
  const log = logger('libp2p:registrar')
11
12
 
12
- function supportsProtocol (peerProtocols: string[], topologyProtocols: string[]) {
13
- for (const peerProtocol of peerProtocols) {
14
- if (topologyProtocols.includes(peerProtocol)) {
15
- return true
16
- }
17
- }
18
-
19
- return false
20
- }
13
+ export const DEFAULT_MAX_INBOUND_STREAMS = 1
14
+ export const DEFAULT_MAX_OUTBOUND_STREAMS = 1
21
15
 
22
16
  /**
23
17
  * Responsible for notifying registered protocols of events in the network.
24
18
  */
25
19
  export class DefaultRegistrar implements Registrar {
26
- private readonly topologies: Map<string, { topology: Topology, protocols: string[] }>
27
- private readonly handlers: Map<string, StreamHandler>
20
+ private readonly topologies: Map<string, Map<string, Topology>>
21
+ private readonly handlers: Map<string, StreamHandlerRecord>
28
22
  private readonly components: Components
29
23
 
30
24
  constructor (components: Components) {
@@ -42,57 +36,54 @@ export class DefaultRegistrar implements Registrar {
42
36
  }
43
37
 
44
38
  getProtocols () {
45
- const protocols = new Set<string>()
46
-
47
- for (const topology of this.topologies.values()) {
48
- topology.protocols.forEach(protocol => protocols.add(protocol))
49
- }
50
-
51
- for (const protocol of this.handlers.keys()) {
52
- protocols.add(protocol)
53
- }
54
-
55
- return Array.from(protocols).sort()
39
+ return Array.from(new Set<string>([
40
+ ...this.topologies.keys(),
41
+ ...this.handlers.keys()
42
+ ])).sort()
56
43
  }
57
44
 
58
45
  getHandler (protocol: string) {
59
46
  const handler = this.handlers.get(protocol)
60
47
 
61
48
  if (handler == null) {
62
- throw new Error(`No handler registered for protocol ${protocol}`)
49
+ throw errCode(new Error(`No handler registered for protocol ${protocol}`), codes.ERR_NO_HANDLER_FOR_PROTOCOL)
63
50
  }
64
51
 
65
52
  return handler
66
53
  }
67
54
 
68
55
  getTopologies (protocol: string) {
69
- const output: Topology[] = []
56
+ const topologies = this.topologies.get(protocol)
70
57
 
71
- for (const { topology, protocols } of this.topologies.values()) {
72
- if (protocols.includes(protocol)) {
73
- output.push(topology)
74
- }
58
+ if (topologies == null) {
59
+ return []
75
60
  }
76
61
 
77
- return output
62
+ return [
63
+ ...topologies.values()
64
+ ]
78
65
  }
79
66
 
80
67
  /**
81
68
  * Registers the `handler` for each protocol
82
69
  */
83
- async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
84
- const protocolList = Array.isArray(protocols) ? protocols : [protocols]
70
+ async handle (protocol: string, handler: StreamHandler, opts?: StreamHandlerOptions): Promise<void> {
71
+ if (this.handlers.has(protocol)) {
72
+ throw errCode(new Error(`Handler already registered for protocol ${protocol}`), codes.ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED)
73
+ }
85
74
 
86
- for (const protocol of protocolList) {
87
- if (this.handlers.has(protocol)) {
88
- throw errCode(new Error(`Handler already registered for protocol ${protocol}`), codes.ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED)
89
- }
75
+ const options = merge.bind({ ignoreUndefined: true })({
76
+ maxInboundStreams: DEFAULT_MAX_INBOUND_STREAMS,
77
+ maxOutboundStreams: DEFAULT_MAX_OUTBOUND_STREAMS
78
+ }, opts)
90
79
 
91
- this.handlers.set(protocol, handler)
92
- }
80
+ this.handlers.set(protocol, {
81
+ handler,
82
+ options
83
+ })
93
84
 
94
85
  // Add new protocols to self protocols in the Protobook
95
- await this.components.getPeerStore().protoBook.add(this.components.getPeerId(), protocolList)
86
+ await this.components.getPeerStore().protoBook.add(this.components.getPeerId(), [protocol])
96
87
  }
97
88
 
98
89
  /**
@@ -113,7 +104,7 @@ export class DefaultRegistrar implements Registrar {
113
104
  /**
114
105
  * Register handlers for a set of multicodecs given
115
106
  */
116
- async register (protocols: string | string[], topology: Topology): Promise<string> {
107
+ async register (protocol: string, topology: Topology): Promise<string> {
117
108
  if (!isTopology(topology)) {
118
109
  log.error('topology must be an instance of interfaces/topology')
119
110
  throw errCode(new Error('topology must be an instance of interfaces/topology'), codes.ERR_INVALID_PARAMETERS)
@@ -122,10 +113,14 @@ export class DefaultRegistrar implements Registrar {
122
113
  // Create topology
123
114
  const id = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
124
115
 
125
- this.topologies.set(id, {
126
- topology,
127
- protocols: Array.isArray(protocols) ? protocols : [protocols]
128
- })
116
+ let topologies = this.topologies.get(protocol)
117
+
118
+ if (topologies == null) {
119
+ topologies = new Map<string, Topology>()
120
+ this.topologies.set(protocol, topologies)
121
+ }
122
+
123
+ topologies.set(id, topology)
129
124
 
130
125
  // Set registrar
131
126
  await topology.setRegistrar(this)
@@ -137,7 +132,15 @@ export class DefaultRegistrar implements Registrar {
137
132
  * Unregister topology
138
133
  */
139
134
  unregister (id: string) {
140
- this.topologies.delete(id)
135
+ for (const [protocol, topologies] of this.topologies.entries()) {
136
+ if (topologies.has(id)) {
137
+ topologies.delete(id)
138
+
139
+ if (topologies.size === 0) {
140
+ this.topologies.delete(protocol)
141
+ }
142
+ }
143
+ }
141
144
  }
142
145
 
143
146
  /**
@@ -148,8 +151,15 @@ export class DefaultRegistrar implements Registrar {
148
151
 
149
152
  void this.components.getPeerStore().protoBook.get(connection.remotePeer)
150
153
  .then(peerProtocols => {
151
- for (const { topology, protocols } of this.topologies.values()) {
152
- if (supportsProtocol(peerProtocols, protocols)) {
154
+ for (const protocol of peerProtocols) {
155
+ const topologies = this.topologies.get(protocol)
156
+
157
+ if (topologies == null) {
158
+ // no topologies are interested in this protocol
159
+ continue
160
+ }
161
+
162
+ for (const topology of topologies.values()) {
153
163
  topology.onDisconnect(connection.remotePeer)
154
164
  }
155
165
  }
@@ -168,14 +178,28 @@ export class DefaultRegistrar implements Registrar {
168
178
  const removed = oldProtocols.filter(protocol => !protocols.includes(protocol))
169
179
  const added = protocols.filter(protocol => !oldProtocols.includes(protocol))
170
180
 
171
- for (const { topology, protocols } of this.topologies.values()) {
172
- if (supportsProtocol(removed, protocols)) {
181
+ for (const protocol of removed) {
182
+ const topologies = this.topologies.get(protocol)
183
+
184
+ if (topologies == null) {
185
+ // no topologies are interested in this protocol
186
+ continue
187
+ }
188
+
189
+ for (const topology of topologies.values()) {
173
190
  topology.onDisconnect(peerId)
174
191
  }
175
192
  }
176
193
 
177
- for (const { topology, protocols } of this.topologies.values()) {
178
- if (supportsProtocol(added, protocols)) {
194
+ for (const protocol of added) {
195
+ const topologies = this.topologies.get(protocol)
196
+
197
+ if (topologies == null) {
198
+ // no topologies are interested in this protocol
199
+ continue
200
+ }
201
+
202
+ for (const topology of topologies.values()) {
179
203
  const connection = this.components.getConnectionManager().getConnections(peerId)[0]
180
204
 
181
205
  if (connection == null) {