libp2p 0.37.3 → 0.38.0

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 (189) hide show
  1. package/README.md +1 -2
  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 +7 -5
  7. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  8. package/dist/src/circuit/circuit/hop.js +8 -4
  9. package/dist/src/circuit/circuit/hop.js.map +1 -1
  10. package/dist/src/circuit/circuit/stop.d.ts +6 -4
  11. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  12. package/dist/src/circuit/circuit/stop.js +4 -2
  13. package/dist/src/circuit/circuit/stop.js.map +1 -1
  14. package/dist/src/circuit/circuit/stream-handler.d.ts +3 -2
  15. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  16. package/dist/src/circuit/circuit/stream-handler.js +2 -2
  17. package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
  18. package/dist/src/circuit/index.d.ts +4 -12
  19. package/dist/src/circuit/index.d.ts.map +1 -1
  20. package/dist/src/circuit/index.js.map +1 -1
  21. package/dist/src/circuit/listener.d.ts +3 -3
  22. package/dist/src/circuit/listener.d.ts.map +1 -1
  23. package/dist/src/circuit/pb/index.d.ts +5 -4
  24. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  25. package/dist/src/circuit/pb/index.js +107 -11
  26. package/dist/src/circuit/pb/index.js.map +1 -1
  27. package/dist/src/circuit/transport.d.ts +8 -5
  28. package/dist/src/circuit/transport.d.ts.map +1 -1
  29. package/dist/src/circuit/transport.js +81 -59
  30. package/dist/src/circuit/transport.js.map +1 -1
  31. package/dist/src/config.d.ts.map +1 -1
  32. package/dist/src/config.js +27 -4
  33. package/dist/src/config.js.map +1 -1
  34. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  35. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  36. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +2 -2
  37. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -1
  38. package/dist/src/connection-manager/dialer/auto-dialer.js +6 -0
  39. package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -1
  40. package/dist/src/connection-manager/dialer/dial-request.d.ts +1 -1
  41. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  42. package/dist/src/connection-manager/dialer/index.d.ts +9 -9
  43. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  44. package/dist/src/connection-manager/dialer/index.js +11 -11
  45. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  46. package/dist/src/connection-manager/index.d.ts +20 -16
  47. package/dist/src/connection-manager/index.d.ts.map +1 -1
  48. package/dist/src/connection-manager/index.js +185 -65
  49. package/dist/src/connection-manager/index.js.map +1 -1
  50. package/dist/src/constants.d.ts +4 -0
  51. package/dist/src/constants.d.ts.map +1 -1
  52. package/dist/src/constants.js +4 -0
  53. package/dist/src/constants.js.map +1 -1
  54. package/dist/src/content-routing/index.d.ts +4 -4
  55. package/dist/src/content-routing/index.d.ts.map +1 -1
  56. package/dist/src/content-routing/utils.d.ts +2 -2
  57. package/dist/src/content-routing/utils.d.ts.map +1 -1
  58. package/dist/src/dht/dht-content-routing.d.ts +3 -3
  59. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  60. package/dist/src/dht/dht-peer-routing.d.ts +5 -5
  61. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  62. package/dist/src/dht/dummy-dht.d.ts +3 -3
  63. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  64. package/dist/src/dht/dummy-dht.js +1 -1
  65. package/dist/src/dht/dummy-dht.js.map +1 -1
  66. package/dist/src/errors.d.ts +4 -1
  67. package/dist/src/errors.d.ts.map +1 -1
  68. package/dist/src/errors.js +3 -0
  69. package/dist/src/errors.js.map +1 -1
  70. package/dist/src/fetch/index.d.ts +11 -4
  71. package/dist/src/fetch/index.d.ts.map +1 -1
  72. package/dist/src/fetch/index.js +82 -41
  73. package/dist/src/fetch/index.js.map +1 -1
  74. package/dist/src/fetch/pb/proto.d.ts +4 -3
  75. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  76. package/dist/src/fetch/pb/proto.js +88 -8
  77. package/dist/src/fetch/pb/proto.js.map +1 -1
  78. package/dist/src/get-peer.d.ts +2 -2
  79. package/dist/src/get-peer.d.ts.map +1 -1
  80. package/dist/src/get-peer.js +1 -1
  81. package/dist/src/get-peer.js.map +1 -1
  82. package/dist/src/identify/index.d.ts +8 -8
  83. package/dist/src/identify/index.d.ts.map +1 -1
  84. package/dist/src/identify/index.js +49 -34
  85. package/dist/src/identify/index.js.map +1 -1
  86. package/dist/src/identify/pb/message.d.ts +2 -1
  87. package/dist/src/identify/pb/message.d.ts.map +1 -1
  88. package/dist/src/identify/pb/message.js +94 -10
  89. package/dist/src/identify/pb/message.js.map +1 -1
  90. package/dist/src/index.d.ts +18 -17
  91. package/dist/src/index.d.ts.map +1 -1
  92. package/dist/src/index.js.map +1 -1
  93. package/dist/src/insecure/index.d.ts +2 -2
  94. package/dist/src/insecure/index.d.ts.map +1 -1
  95. package/dist/src/insecure/index.js +8 -4
  96. package/dist/src/insecure/index.js.map +1 -1
  97. package/dist/src/insecure/pb/proto.d.ts +4 -3
  98. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  99. package/dist/src/insecure/pb/proto.js +89 -9
  100. package/dist/src/insecure/pb/proto.js.map +1 -1
  101. package/dist/src/keychain/index.d.ts +2 -2
  102. package/dist/src/keychain/index.d.ts.map +1 -1
  103. package/dist/src/libp2p.d.ts +14 -14
  104. package/dist/src/libp2p.d.ts.map +1 -1
  105. package/dist/src/libp2p.js +17 -17
  106. package/dist/src/libp2p.js.map +1 -1
  107. package/dist/src/metrics/index.d.ts +4 -5
  108. package/dist/src/metrics/index.d.ts.map +1 -1
  109. package/dist/src/metrics/index.js +9 -6
  110. package/dist/src/metrics/index.js.map +1 -1
  111. package/dist/src/metrics/moving-average.d.ts +1 -1
  112. package/dist/src/metrics/moving-average.d.ts.map +1 -1
  113. package/dist/src/metrics/stats.d.ts +1 -1
  114. package/dist/src/metrics/stats.d.ts.map +1 -1
  115. package/dist/src/nat-manager.d.ts +1 -1
  116. package/dist/src/nat-manager.d.ts.map +1 -1
  117. package/dist/src/peer-record-updater.d.ts +1 -1
  118. package/dist/src/peer-record-updater.d.ts.map +1 -1
  119. package/dist/src/peer-routing.d.ts +4 -4
  120. package/dist/src/peer-routing.d.ts.map +1 -1
  121. package/dist/src/ping/index.d.ts +10 -3
  122. package/dist/src/ping/index.d.ts.map +1 -1
  123. package/dist/src/ping/index.js +32 -9
  124. package/dist/src/ping/index.js.map +1 -1
  125. package/dist/src/pnet/index.d.ts +1 -2
  126. package/dist/src/pnet/index.d.ts.map +1 -1
  127. package/dist/src/pnet/index.js +2 -1
  128. package/dist/src/pnet/index.js.map +1 -1
  129. package/dist/src/pubsub/dummy-pubsub.d.ts +2 -2
  130. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  131. package/dist/src/registrar.d.ts +10 -8
  132. package/dist/src/registrar.d.ts.map +1 -1
  133. package/dist/src/registrar.js +63 -44
  134. package/dist/src/registrar.js.map +1 -1
  135. package/dist/src/transport-manager.d.ts +3 -3
  136. package/dist/src/transport-manager.d.ts.map +1 -1
  137. package/dist/src/upgrader.d.ts +12 -6
  138. package/dist/src/upgrader.d.ts.map +1 -1
  139. package/dist/src/upgrader.js +183 -82
  140. package/dist/src/upgrader.js.map +1 -1
  141. package/dist/src/version.d.ts.map +1 -1
  142. package/dist/src/version.js.map +1 -1
  143. package/package.json +66 -42
  144. package/src/address-manager/index.ts +2 -2
  145. package/src/circuit/README.md +2 -2
  146. package/src/circuit/auto-relay.ts +4 -4
  147. package/src/circuit/circuit/hop.ts +20 -11
  148. package/src/circuit/circuit/stop.ts +10 -5
  149. package/src/circuit/circuit/stream-handler.ts +6 -5
  150. package/src/circuit/index.ts +4 -13
  151. package/src/circuit/listener.ts +3 -3
  152. package/src/circuit/pb/index.ts +133 -15
  153. package/src/circuit/transport.ts +92 -63
  154. package/src/config.ts +27 -4
  155. package/src/connection-manager/auto-dialler.ts +1 -1
  156. package/src/connection-manager/dialer/auto-dialer.ts +8 -2
  157. package/src/connection-manager/dialer/dial-request.ts +1 -1
  158. package/src/connection-manager/dialer/index.ts +15 -15
  159. package/src/connection-manager/index.ts +239 -82
  160. package/src/constants.ts +5 -0
  161. package/src/content-routing/index.ts +2 -2
  162. package/src/content-routing/utils.ts +2 -2
  163. package/src/dht/dht-content-routing.ts +2 -2
  164. package/src/dht/dht-peer-routing.ts +3 -3
  165. package/src/dht/dummy-dht.ts +3 -3
  166. package/src/errors.ts +4 -1
  167. package/src/fetch/index.ts +120 -53
  168. package/src/fetch/pb/proto.ts +109 -11
  169. package/src/get-peer.ts +3 -3
  170. package/src/identify/index.ts +64 -47
  171. package/src/identify/pb/message.ts +111 -11
  172. package/src/index.ts +18 -17
  173. package/src/insecure/index.ts +11 -6
  174. package/src/insecure/pb/proto.ts +111 -12
  175. package/src/keychain/index.ts +2 -2
  176. package/src/libp2p.ts +34 -29
  177. package/src/metrics/index.ts +13 -12
  178. package/src/metrics/moving-average.ts +1 -1
  179. package/src/metrics/stats.ts +1 -1
  180. package/src/nat-manager.ts +1 -1
  181. package/src/peer-record-updater.ts +1 -1
  182. package/src/peer-routing.ts +4 -4
  183. package/src/ping/index.ts +48 -15
  184. package/src/pnet/index.ts +3 -2
  185. package/src/pubsub/dummy-pubsub.ts +2 -2
  186. package/src/registrar.ts +79 -55
  187. package/src/transport-manager.ts +3 -3
  188. package/src/upgrader.ts +223 -93
  189. package/src/version.ts +1 -2
@@ -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,33 +6,47 @@ 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
- import type { Duplex } from 'it-stream-types'
15
14
  import { abortableDuplex } from 'abortable-iterator'
15
+ import { TimeoutController } from 'timeout-abort-controller'
16
+ import type { Stream } from '@libp2p/interface-connection'
17
+ import { setMaxListeners } from 'events'
16
18
 
17
19
  const log = logger('libp2p:ping')
18
20
 
19
21
  export interface PingServiceInit {
20
22
  protocolPrefix: string
23
+ maxInboundStreams: number
24
+ maxOutboundStreams: number
25
+
26
+ /**
27
+ * How long we should wait for a ping response
28
+ */
29
+ timeout: number
21
30
  }
22
31
 
23
32
  export class PingService implements Startable {
24
33
  public readonly protocol: string
25
34
  private readonly components: Components
26
35
  private started: boolean
36
+ private readonly init: PingServiceInit
27
37
 
28
38
  constructor (components: Components, init: PingServiceInit) {
29
39
  this.components = components
30
40
  this.started = false
31
41
  this.protocol = `/${init.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
42
+ this.init = init
32
43
  }
33
44
 
34
45
  async start () {
35
- await this.components.getRegistrar().handle(this.protocol, this.handleMessage)
46
+ await this.components.getRegistrar().handle(this.protocol, this.handleMessage, {
47
+ maxInboundStreams: this.init.maxInboundStreams,
48
+ maxOutboundStreams: this.init.maxOutboundStreams
49
+ })
36
50
  this.started = true
37
51
  }
38
52
 
@@ -66,19 +80,32 @@ export class PingService implements Startable {
66
80
  async ping (peer: PeerId, options: AbortOptions = {}): Promise<number> {
67
81
  log('dialing %s to %p', this.protocol, peer)
68
82
 
69
- const connection = await this.components.getConnectionManager().openConnection(peer, options)
70
- const { stream } = await connection.newStream([this.protocol], options)
71
83
  const start = Date.now()
72
84
  const data = randomBytes(PING_LENGTH)
73
-
74
- let source: Duplex<Uint8Array> = stream
75
-
76
- // make stream abortable if AbortSignal passed
77
- if (options.signal != null) {
78
- source = abortableDuplex(stream, options.signal)
85
+ const connection = await this.components.getConnectionManager().openConnection(peer, options)
86
+ let timeoutController
87
+ let signal = options.signal
88
+ let stream: Stream | undefined
89
+
90
+ // create a timeout if no abort signal passed
91
+ if (signal == null) {
92
+ timeoutController = new TimeoutController(this.init.timeout)
93
+ signal = timeoutController.signal
94
+
95
+ try {
96
+ // fails on node < 15.4
97
+ setMaxListeners?.(Infinity, timeoutController.signal)
98
+ } catch {}
79
99
  }
80
100
 
81
101
  try {
102
+ stream = await connection.newStream([this.protocol], {
103
+ signal
104
+ })
105
+
106
+ // make stream abortable
107
+ const source = abortableDuplex(stream, signal)
108
+
82
109
  const result = await pipe(
83
110
  [data],
84
111
  source,
@@ -86,13 +113,19 @@ export class PingService implements Startable {
86
113
  )
87
114
  const end = Date.now()
88
115
 
89
- if (result == null || !uint8ArrayEquals(data, result)) {
116
+ if (result == null || !uint8ArrayEquals(data, result.subarray())) {
90
117
  throw errCode(new Error('Received wrong ping ack'), codes.ERR_WRONG_PING_ACK)
91
118
  }
92
119
 
93
120
  return end - start
94
121
  } finally {
95
- stream.close()
122
+ if (timeoutController != null) {
123
+ timeoutController.clear()
124
+ }
125
+
126
+ if (stream != null) {
127
+ stream.close()
128
+ }
96
129
  }
97
130
  }
98
131
  }
package/src/pnet/index.ts CHANGED
@@ -12,8 +12,8 @@ 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'
16
+ import map from 'it-map'
17
17
 
18
18
  const log = logger('libp2p:pnet')
19
19
 
@@ -84,6 +84,7 @@ export class PreSharedKeyConnectionProtector implements ConnectionProtector {
84
84
  // Encrypt all outbound traffic
85
85
  createBoxStream(localNonce, this.psk),
86
86
  shake.stream,
87
+ (source) => map(source, (buf) => buf.subarray()),
87
88
  // Decrypt all inbound traffic
88
89
  createUnboxStream(remoteNonce, this.psk),
89
90
  external
@@ -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 = 32
14
+ export const DEFAULT_MAX_OUTBOUND_STREAMS = 64
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) {
@@ -2,13 +2,13 @@ import { logger } from '@libp2p/logger'
2
2
  import pSettle from 'p-settle'
3
3
  import { codes } from './errors.js'
4
4
  import errCode from 'err-code'
5
- import type { Listener, Transport, TransportManager, TransportManagerEvents } from '@libp2p/interfaces/transport'
5
+ import type { Listener, Transport, TransportManager, TransportManagerEvents } from '@libp2p/interface-transport'
6
6
  import type { Multiaddr } from '@multiformats/multiaddr'
7
- import type { Connection } from '@libp2p/interfaces/connection'
7
+ import type { Connection } from '@libp2p/interface-connection'
8
8
  import type { AbortOptions } from '@libp2p/interfaces'
9
9
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
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
  import { trackedMap } from '@libp2p/tracked-map'
13
13
 
14
14
  const log = logger('libp2p:transports')