libp2p 0.37.2 → 0.37.3-d4dd664

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 (133) hide show
  1. package/dist/src/address-manager/index.d.ts +2 -2
  2. package/dist/src/address-manager/index.d.ts.map +1 -1
  3. package/dist/src/circuit/auto-relay.d.ts +4 -4
  4. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  5. package/dist/src/circuit/circuit/hop.d.ts +2 -2
  6. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  7. package/dist/src/circuit/circuit/stop.d.ts +1 -1
  8. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  9. package/dist/src/circuit/circuit/stream-handler.d.ts +1 -1
  10. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  11. package/dist/src/circuit/index.d.ts +2 -2
  12. package/dist/src/circuit/index.d.ts.map +1 -1
  13. package/dist/src/circuit/listener.d.ts +3 -3
  14. package/dist/src/circuit/listener.d.ts.map +1 -1
  15. package/dist/src/circuit/transport.d.ts +5 -5
  16. package/dist/src/circuit/transport.d.ts.map +1 -1
  17. package/dist/src/circuit/transport.js +2 -2
  18. package/dist/src/circuit/transport.js.map +1 -1
  19. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  20. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  21. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +2 -2
  22. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -1
  23. package/dist/src/connection-manager/dialer/dial-request.d.ts +1 -1
  24. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  25. package/dist/src/connection-manager/dialer/index.d.ts +5 -5
  26. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  27. package/dist/src/connection-manager/dialer/index.js +1 -1
  28. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  29. package/dist/src/connection-manager/index.d.ts +9 -9
  30. package/dist/src/connection-manager/index.d.ts.map +1 -1
  31. package/dist/src/connection-manager/index.js +42 -32
  32. package/dist/src/connection-manager/index.js.map +1 -1
  33. package/dist/src/content-routing/index.d.ts +4 -4
  34. package/dist/src/content-routing/index.d.ts.map +1 -1
  35. package/dist/src/content-routing/utils.d.ts +2 -2
  36. package/dist/src/content-routing/utils.d.ts.map +1 -1
  37. package/dist/src/dht/dht-content-routing.d.ts +3 -3
  38. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  39. package/dist/src/dht/dht-peer-routing.d.ts +5 -5
  40. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  41. package/dist/src/dht/dummy-dht.d.ts +3 -3
  42. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  43. package/dist/src/dht/dummy-dht.js +1 -1
  44. package/dist/src/dht/dummy-dht.js.map +1 -1
  45. package/dist/src/fetch/index.d.ts +4 -4
  46. package/dist/src/fetch/index.d.ts.map +1 -1
  47. package/dist/src/get-peer.d.ts +2 -2
  48. package/dist/src/get-peer.d.ts.map +1 -1
  49. package/dist/src/get-peer.js +1 -1
  50. package/dist/src/get-peer.js.map +1 -1
  51. package/dist/src/identify/index.d.ts +3 -7
  52. package/dist/src/identify/index.d.ts.map +1 -1
  53. package/dist/src/identify/index.js +7 -20
  54. package/dist/src/identify/index.js.map +1 -1
  55. package/dist/src/index.d.ts +15 -15
  56. package/dist/src/index.d.ts.map +1 -1
  57. package/dist/src/insecure/index.d.ts +2 -2
  58. package/dist/src/insecure/index.d.ts.map +1 -1
  59. package/dist/src/insecure/index.js +1 -1
  60. package/dist/src/insecure/index.js.map +1 -1
  61. package/dist/src/keychain/index.d.ts +2 -2
  62. package/dist/src/keychain/index.d.ts.map +1 -1
  63. package/dist/src/libp2p.d.ts +13 -13
  64. package/dist/src/libp2p.d.ts.map +1 -1
  65. package/dist/src/libp2p.js +13 -3
  66. package/dist/src/libp2p.js.map +1 -1
  67. package/dist/src/metrics/index.d.ts +2 -2
  68. package/dist/src/metrics/index.d.ts.map +1 -1
  69. package/dist/src/metrics/moving-average.d.ts +1 -1
  70. package/dist/src/metrics/moving-average.d.ts.map +1 -1
  71. package/dist/src/metrics/stats.d.ts +1 -1
  72. package/dist/src/metrics/stats.d.ts.map +1 -1
  73. package/dist/src/nat-manager.d.ts +1 -1
  74. package/dist/src/nat-manager.d.ts.map +1 -1
  75. package/dist/src/peer-record-updater.d.ts +1 -1
  76. package/dist/src/peer-record-updater.d.ts.map +1 -1
  77. package/dist/src/peer-routing.d.ts +4 -4
  78. package/dist/src/peer-routing.d.ts.map +1 -1
  79. package/dist/src/ping/index.d.ts +3 -3
  80. package/dist/src/ping/index.d.ts.map +1 -1
  81. package/dist/src/pnet/index.d.ts +1 -2
  82. package/dist/src/pnet/index.d.ts.map +1 -1
  83. package/dist/src/pnet/index.js.map +1 -1
  84. package/dist/src/pubsub/dummy-pubsub.d.ts +2 -2
  85. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  86. package/dist/src/registrar.d.ts +8 -8
  87. package/dist/src/registrar.d.ts.map +1 -1
  88. package/dist/src/registrar.js +55 -43
  89. package/dist/src/registrar.js.map +1 -1
  90. package/dist/src/transport-manager.d.ts +3 -3
  91. package/dist/src/transport-manager.d.ts.map +1 -1
  92. package/dist/src/upgrader.d.ts +6 -6
  93. package/dist/src/upgrader.d.ts.map +1 -1
  94. package/dist/src/upgrader.js +12 -2
  95. package/dist/src/upgrader.js.map +1 -1
  96. package/package.json +50 -28
  97. package/src/address-manager/index.ts +2 -2
  98. package/src/circuit/auto-relay.ts +4 -4
  99. package/src/circuit/circuit/hop.ts +2 -2
  100. package/src/circuit/circuit/stop.ts +1 -1
  101. package/src/circuit/circuit/stream-handler.ts +1 -1
  102. package/src/circuit/index.ts +2 -2
  103. package/src/circuit/listener.ts +3 -3
  104. package/src/circuit/transport.ts +5 -5
  105. package/src/connection-manager/auto-dialler.ts +1 -1
  106. package/src/connection-manager/dialer/auto-dialer.ts +2 -2
  107. package/src/connection-manager/dialer/dial-request.ts +1 -1
  108. package/src/connection-manager/dialer/index.ts +5 -5
  109. package/src/connection-manager/index.ts +55 -39
  110. package/src/content-routing/index.ts +2 -2
  111. package/src/content-routing/utils.ts +2 -2
  112. package/src/dht/dht-content-routing.ts +2 -2
  113. package/src/dht/dht-peer-routing.ts +3 -3
  114. package/src/dht/dummy-dht.ts +3 -3
  115. package/src/fetch/index.ts +4 -4
  116. package/src/get-peer.ts +3 -3
  117. package/src/identify/index.ts +10 -26
  118. package/src/index.ts +15 -15
  119. package/src/insecure/index.ts +3 -3
  120. package/src/keychain/index.ts +2 -2
  121. package/src/libp2p.ts +30 -14
  122. package/src/metrics/index.ts +2 -2
  123. package/src/metrics/moving-average.ts +1 -1
  124. package/src/metrics/stats.ts +1 -1
  125. package/src/nat-manager.ts +1 -1
  126. package/src/peer-record-updater.ts +1 -1
  127. package/src/peer-routing.ts +4 -4
  128. package/src/ping/index.ts +3 -3
  129. package/src/pnet/index.ts +1 -2
  130. package/src/pubsub/dummy-pubsub.ts +2 -2
  131. package/src/registrar.ts +71 -56
  132. package/src/transport-manager.ts +3 -3
  133. package/src/upgrader.ts +20 -8
@@ -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'
@@ -79,8 +79,6 @@ export class IdentifyService implements Startable {
79
79
  this.started = false
80
80
  this.init = init
81
81
 
82
- this.handleMessage = this.handleMessage.bind(this)
83
-
84
82
  this.identifyProtocolStr = `/${init.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`
85
83
  this.identifyPushProtocolStr = `/${init.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
86
84
 
@@ -127,11 +125,13 @@ export class IdentifyService implements Startable {
127
125
  await this.components.getPeerStore().metadataBook.setValue(this.components.getPeerId(), 'AgentVersion', uint8ArrayFromString(this.host.agentVersion))
128
126
  await this.components.getPeerStore().metadataBook.setValue(this.components.getPeerId(), 'ProtocolVersion', uint8ArrayFromString(this.host.protocolVersion))
129
127
 
130
- await this.components.getRegistrar().handle([
131
- this.identifyProtocolStr,
132
- this.identifyPushProtocolStr
133
- ], (data) => {
134
- void this.handleMessage(data)?.catch(err => {
128
+ await this.components.getRegistrar().handle(this.identifyProtocolStr, (data) => {
129
+ void this._handleIdentify(data).catch(err => {
130
+ log.error(err)
131
+ })
132
+ })
133
+ await this.components.getRegistrar().handle(this.identifyPushProtocolStr, (data) => {
134
+ void this._handlePush(data).catch(err => {
135
135
  log.error(err)
136
136
  })
137
137
  })
@@ -353,22 +353,6 @@ export class IdentifyService implements Startable {
353
353
  // this.components.getAddressManager().addObservedAddr(observedAddr)
354
354
  }
355
355
 
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
356
  /**
373
357
  * Sends the `Identify` response with the Signed Peer Record
374
358
  * 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, ProtocolStream } 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 } 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'
@@ -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 } 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'
@@ -491,11 +491,27 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
491
491
  }
492
492
 
493
493
  async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
494
- return await this.components.getRegistrar().handle(protocols, handler)
494
+ if (!Array.isArray(protocols)) {
495
+ protocols = [protocols]
496
+ }
497
+
498
+ await Promise.all(
499
+ protocols.map(async protocol => {
500
+ await this.components.getRegistrar().handle(protocol, handler)
501
+ })
502
+ )
495
503
  }
496
504
 
497
505
  async unhandle (protocols: string[] | string): Promise<void> {
498
- return await this.components.getRegistrar().unhandle(protocols)
506
+ if (!Array.isArray(protocols)) {
507
+ protocols = [protocols]
508
+ }
509
+
510
+ await Promise.all(
511
+ protocols.map(async protocol => {
512
+ await this.components.getRegistrar().unhandle(protocol)
513
+ })
514
+ )
499
515
  }
500
516
 
501
517
  /**
@@ -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'
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,20 @@
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 type { Registrar, StreamHandler, Topology } from '@libp2p/interface-registrar'
6
+ import type { PeerProtocolsChangeData } from '@libp2p/interface-peer-store'
7
+ import type { Connection } from '@libp2p/interface-connection'
8
+ import type { Components } from '@libp2p/components'
9
9
 
10
10
  const log = logger('libp2p:registrar')
11
11
 
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
- }
21
-
22
12
  /**
23
13
  * Responsible for notifying registered protocols of events in the network.
24
14
  */
25
15
  export class DefaultRegistrar implements Registrar {
26
- private readonly topologies: Map<string, { topology: Topology, protocols: string[] }>
27
- private readonly handlers: Map<string, StreamHandler>
16
+ private readonly topologies: Map<string, Map<string, Topology>>
17
+ private readonly handlers: Map<string, StreamHandlerRecord>
28
18
  private readonly components: Components
29
19
 
30
20
  constructor (components: Components) {
@@ -42,17 +32,10 @@ export class DefaultRegistrar implements Registrar {
42
32
  }
43
33
 
44
34
  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()
35
+ return Array.from(new Set<string>([
36
+ ...this.topologies.keys(),
37
+ ...this.handlers.keys()
38
+ ])).sort()
56
39
  }
57
40
 
58
41
  getHandler (protocol: string) {
@@ -66,33 +49,32 @@ export class DefaultRegistrar implements Registrar {
66
49
  }
67
50
 
68
51
  getTopologies (protocol: string) {
69
- const output: Topology[] = []
52
+ const topologies = this.topologies.get(protocol)
70
53
 
71
- for (const { topology, protocols } of this.topologies.values()) {
72
- if (protocols.includes(protocol)) {
73
- output.push(topology)
74
- }
54
+ if (topologies == null) {
55
+ return []
75
56
  }
76
57
 
77
- return output
58
+ return [
59
+ ...topologies.values()
60
+ ]
78
61
  }
79
62
 
80
63
  /**
81
64
  * Registers the `handler` for each protocol
82
65
  */
83
- async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
84
- const protocolList = Array.isArray(protocols) ? protocols : [protocols]
85
-
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
- }
90
-
91
- this.handlers.set(protocol, handler)
66
+ async handle (protocol: string, handler: StreamHandler, options: StreamHandlerOptions = { maxConcurrentStreams: 1 }): Promise<void> {
67
+ if (this.handlers.has(protocol)) {
68
+ throw errCode(new Error(`Handler already registered for protocol ${protocol}`), codes.ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED)
92
69
  }
93
70
 
71
+ this.handlers.set(protocol, {
72
+ handler,
73
+ options
74
+ })
75
+
94
76
  // Add new protocols to self protocols in the Protobook
95
- await this.components.getPeerStore().protoBook.add(this.components.getPeerId(), protocolList)
77
+ await this.components.getPeerStore().protoBook.add(this.components.getPeerId(), [protocol])
96
78
  }
97
79
 
98
80
  /**
@@ -113,7 +95,7 @@ export class DefaultRegistrar implements Registrar {
113
95
  /**
114
96
  * Register handlers for a set of multicodecs given
115
97
  */
116
- async register (protocols: string | string[], topology: Topology): Promise<string> {
98
+ async register (protocol: string, topology: Topology): Promise<string> {
117
99
  if (!isTopology(topology)) {
118
100
  log.error('topology must be an instance of interfaces/topology')
119
101
  throw errCode(new Error('topology must be an instance of interfaces/topology'), codes.ERR_INVALID_PARAMETERS)
@@ -122,10 +104,14 @@ export class DefaultRegistrar implements Registrar {
122
104
  // Create topology
123
105
  const id = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
124
106
 
125
- this.topologies.set(id, {
126
- topology,
127
- protocols: Array.isArray(protocols) ? protocols : [protocols]
128
- })
107
+ let topologies = this.topologies.get(protocol)
108
+
109
+ if (topologies == null) {
110
+ topologies = new Map<string, Topology>()
111
+ this.topologies.set(protocol, topologies)
112
+ }
113
+
114
+ topologies.set(id, topology)
129
115
 
130
116
  // Set registrar
131
117
  await topology.setRegistrar(this)
@@ -137,7 +123,15 @@ export class DefaultRegistrar implements Registrar {
137
123
  * Unregister topology
138
124
  */
139
125
  unregister (id: string) {
140
- this.topologies.delete(id)
126
+ for (const [protocol, topologies] of this.topologies.entries()) {
127
+ if (topologies.has(id)) {
128
+ topologies.delete(id)
129
+
130
+ if (topologies.size === 0) {
131
+ this.topologies.delete(protocol)
132
+ }
133
+ }
134
+ }
141
135
  }
142
136
 
143
137
  /**
@@ -148,8 +142,15 @@ export class DefaultRegistrar implements Registrar {
148
142
 
149
143
  void this.components.getPeerStore().protoBook.get(connection.remotePeer)
150
144
  .then(peerProtocols => {
151
- for (const { topology, protocols } of this.topologies.values()) {
152
- if (supportsProtocol(peerProtocols, protocols)) {
145
+ for (const protocol of peerProtocols) {
146
+ const topologies = this.topologies.get(protocol)
147
+
148
+ if (topologies == null) {
149
+ // no topologies are interested in this protocol
150
+ continue
151
+ }
152
+
153
+ for (const topology of topologies.values()) {
153
154
  topology.onDisconnect(connection.remotePeer)
154
155
  }
155
156
  }
@@ -168,14 +169,28 @@ export class DefaultRegistrar implements Registrar {
168
169
  const removed = oldProtocols.filter(protocol => !protocols.includes(protocol))
169
170
  const added = protocols.filter(protocol => !oldProtocols.includes(protocol))
170
171
 
171
- for (const { topology, protocols } of this.topologies.values()) {
172
- if (supportsProtocol(removed, protocols)) {
172
+ for (const protocol of removed) {
173
+ const topologies = this.topologies.get(protocol)
174
+
175
+ if (topologies == null) {
176
+ // no topologies are interested in this protocol
177
+ continue
178
+ }
179
+
180
+ for (const topology of topologies.values()) {
173
181
  topology.onDisconnect(peerId)
174
182
  }
175
183
  }
176
184
 
177
- for (const { topology, protocols } of this.topologies.values()) {
178
- if (supportsProtocol(added, protocols)) {
185
+ for (const protocol of added) {
186
+ const topologies = this.topologies.get(protocol)
187
+
188
+ if (topologies == null) {
189
+ // no topologies are interested in this protocol
190
+ continue
191
+ }
192
+
193
+ for (const topology of topologies.values()) {
179
194
  const connection = this.components.getConnectionManager().getConnections(peerId)[0]
180
195
 
181
196
  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')
package/src/upgrader.ts CHANGED
@@ -8,13 +8,13 @@ import { codes } from './errors.js'
8
8
  import { createConnection } from '@libp2p/connection'
9
9
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
10
10
  import { peerIdFromString } from '@libp2p/peer-id'
11
- import type { Connection, ProtocolStream, Stream } from '@libp2p/interfaces/connection'
12
- import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
13
- import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
14
- import type { PeerId } from '@libp2p/interfaces/peer-id'
15
- import type { MultiaddrConnection, Upgrader, UpgraderEvents } from '@libp2p/interfaces/transport'
11
+ import type { MultiaddrConnection, Connection, ProtocolStream, Stream } from '@libp2p/interface-connection'
12
+ import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
13
+ import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
14
+ import type { PeerId } from '@libp2p/interface-peer-id'
15
+ import type { Upgrader, UpgraderEvents } from '@libp2p/interface-transport'
16
16
  import type { Duplex } from 'it-stream-types'
17
- import type { Components } from '@libp2p/interfaces/components'
17
+ import { Components, isInitializable } from '@libp2p/components'
18
18
  import type { AbortOptions } from '@libp2p/interfaces'
19
19
 
20
20
  const log = logger('libp2p:upgrader')
@@ -272,7 +272,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
272
272
 
273
273
  if (muxerFactory != null) {
274
274
  // Create the muxer
275
- muxer = muxerFactory.createStreamMuxer(this.components, {
275
+ muxer = muxerFactory.createStreamMuxer({
276
276
  // Run anytime a remote stream is created
277
277
  onIncomingStream: muxedStream => {
278
278
  if (connection == null) {
@@ -301,6 +301,10 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
301
301
  })
302
302
  .catch(err => {
303
303
  log.error(err)
304
+
305
+ if (muxedStream.timeline.close == null) {
306
+ muxedStream.close()
307
+ }
304
308
  })
305
309
  },
306
310
  // Run anytime a stream closes
@@ -309,6 +313,10 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
309
313
  }
310
314
  })
311
315
 
316
+ if (isInitializable(muxer)) {
317
+ muxer.init(this.components)
318
+ }
319
+
312
320
  newStream = async (protocols: string[], options: AbortOptions = {}): Promise<ProtocolStream> => {
313
321
  if (muxer == null) {
314
322
  throw errCode(new Error('Stream is not multiplexed'), codes.ERR_MUXER_UNAVAILABLE)
@@ -330,6 +338,10 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
330
338
  } catch (err: any) {
331
339
  log.error('could not create new stream', err)
332
340
 
341
+ if (muxedStream.timeline.close == null) {
342
+ muxedStream.close()
343
+ }
344
+
333
345
  if (err.code != null) {
334
346
  throw err
335
347
  }
@@ -409,7 +421,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
409
421
  */
410
422
  _onStream (opts: OnStreamOptions): void {
411
423
  const { connection, stream, protocol } = opts
412
- const handler = this.components.getRegistrar().getHandler(protocol)
424
+ const { handler } = this.components.getRegistrar().getHandler(protocol)
413
425
  handler({ connection, stream, protocol })
414
426
  }
415
427