libp2p 0.45.9 → 0.46.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 (194) hide show
  1. package/README.md +96 -77
  2. package/dist/index.min.js +27 -27
  3. package/dist/src/address-manager/index.d.ts +5 -5
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/utils.d.ts.map +1 -1
  6. package/dist/src/address-manager/utils.js.map +1 -1
  7. package/dist/src/autonat/index.d.ts +26 -6
  8. package/dist/src/autonat/index.d.ts.map +1 -1
  9. package/dist/src/autonat/index.js +23 -3
  10. package/dist/src/autonat/index.js.map +1 -1
  11. package/dist/src/circuit-relay/index.d.ts +36 -1
  12. package/dist/src/circuit-relay/index.d.ts.map +1 -1
  13. package/dist/src/circuit-relay/index.js +35 -0
  14. package/dist/src/circuit-relay/index.js.map +1 -1
  15. package/dist/src/circuit-relay/server/advert-service.d.ts +3 -3
  16. package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
  17. package/dist/src/circuit-relay/server/advert-service.js +1 -1
  18. package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
  19. package/dist/src/circuit-relay/server/index.d.ts +8 -8
  20. package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
  21. package/dist/src/circuit-relay/server/index.js +30 -28
  22. package/dist/src/circuit-relay/server/index.js.map +1 -1
  23. package/dist/src/circuit-relay/server/reservation-store.d.ts +3 -3
  24. package/dist/src/circuit-relay/server/reservation-store.d.ts.map +1 -1
  25. package/dist/src/circuit-relay/server/reservation-voucher.d.ts +2 -2
  26. package/dist/src/circuit-relay/transport/discovery.d.ts +8 -8
  27. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  28. package/dist/src/circuit-relay/transport/discovery.js +3 -4
  29. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  30. package/dist/src/circuit-relay/transport/index.d.ts +21 -10
  31. package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
  32. package/dist/src/circuit-relay/transport/index.js +52 -25
  33. package/dist/src/circuit-relay/transport/index.js.map +1 -1
  34. package/dist/src/circuit-relay/transport/listener.d.ts +2 -2
  35. package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
  36. package/dist/src/circuit-relay/transport/listener.js +4 -3
  37. package/dist/src/circuit-relay/transport/listener.js.map +1 -1
  38. package/dist/src/circuit-relay/transport/reservation-store.d.ts +13 -7
  39. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  40. package/dist/src/circuit-relay/transport/reservation-store.js +24 -10
  41. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  42. package/dist/src/circuit-relay/utils.d.ts +1 -1
  43. package/dist/src/components.d.ts +15 -14
  44. package/dist/src/components.d.ts.map +1 -1
  45. package/dist/src/components.js +2 -2
  46. package/dist/src/components.js.map +1 -1
  47. package/dist/src/config/connection-gater.browser.d.ts +1 -1
  48. package/dist/src/config/connection-gater.d.ts +1 -1
  49. package/dist/src/config.d.ts +1 -2
  50. package/dist/src/config.d.ts.map +1 -1
  51. package/dist/src/config.js +2 -2
  52. package/dist/src/config.js.map +1 -1
  53. package/dist/src/connection/index.d.ts +22 -13
  54. package/dist/src/connection/index.d.ts.map +1 -1
  55. package/dist/src/connection/index.js +54 -30
  56. package/dist/src/connection/index.js.map +1 -1
  57. package/dist/src/connection-manager/auto-dial.d.ts +5 -5
  58. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  59. package/dist/src/connection-manager/auto-dial.js +3 -1
  60. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  61. package/dist/src/connection-manager/connection-pruner.d.ts +4 -4
  62. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  63. package/dist/src/connection-manager/connection-pruner.js +2 -2
  64. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  65. package/dist/src/connection-manager/constants.d.ts.map +1 -1
  66. package/dist/src/connection-manager/constants.js.map +1 -1
  67. package/dist/src/connection-manager/dial-queue.d.ts +11 -17
  68. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  69. package/dist/src/connection-manager/dial-queue.js +6 -6
  70. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  71. package/dist/src/connection-manager/index.d.ts +11 -12
  72. package/dist/src/connection-manager/index.d.ts.map +1 -1
  73. package/dist/src/connection-manager/index.js +17 -11
  74. package/dist/src/connection-manager/index.js.map +1 -1
  75. package/dist/src/content-routing/index.d.ts +5 -5
  76. package/dist/src/content-routing/index.d.ts.map +1 -1
  77. package/dist/src/content-routing/index.js +1 -1
  78. package/dist/src/content-routing/index.js.map +1 -1
  79. package/dist/src/content-routing/utils.d.ts +2 -2
  80. package/dist/src/content-routing/utils.js +1 -1
  81. package/dist/src/content-routing/utils.js.map +1 -1
  82. package/dist/src/fetch/constants.d.ts.map +1 -1
  83. package/dist/src/fetch/constants.js.map +1 -1
  84. package/dist/src/fetch/index.d.ts +5 -5
  85. package/dist/src/fetch/index.d.ts.map +1 -1
  86. package/dist/src/fetch/index.js +5 -5
  87. package/dist/src/fetch/index.js.map +1 -1
  88. package/dist/src/get-peer.d.ts +1 -1
  89. package/dist/src/get-peer.js +2 -2
  90. package/dist/src/get-peer.js.map +1 -1
  91. package/dist/src/identify/consts.d.ts.map +1 -1
  92. package/dist/src/identify/consts.js.map +1 -1
  93. package/dist/src/identify/identify.d.ts +8 -7
  94. package/dist/src/identify/identify.d.ts.map +1 -1
  95. package/dist/src/identify/identify.js +66 -72
  96. package/dist/src/identify/identify.js.map +1 -1
  97. package/dist/src/identify/index.d.ts +28 -9
  98. package/dist/src/identify/index.d.ts.map +1 -1
  99. package/dist/src/identify/index.js.map +1 -1
  100. package/dist/src/index.d.ts +11 -12
  101. package/dist/src/index.d.ts.map +1 -1
  102. package/dist/src/index.js.map +1 -1
  103. package/dist/src/insecure/index.d.ts +1 -1
  104. package/dist/src/insecure/index.js +1 -1
  105. package/dist/src/insecure/index.js.map +1 -1
  106. package/dist/src/libp2p.d.ts +13 -13
  107. package/dist/src/libp2p.d.ts.map +1 -1
  108. package/dist/src/libp2p.js +13 -8
  109. package/dist/src/libp2p.js.map +1 -1
  110. package/dist/src/peer-routing.d.ts +5 -5
  111. package/dist/src/peer-routing.d.ts.map +1 -1
  112. package/dist/src/peer-routing.js +1 -1
  113. package/dist/src/peer-routing.js.map +1 -1
  114. package/dist/src/ping/constants.d.ts.map +1 -1
  115. package/dist/src/ping/constants.js.map +1 -1
  116. package/dist/src/ping/index.d.ts +5 -4
  117. package/dist/src/ping/index.d.ts.map +1 -1
  118. package/dist/src/ping/index.js +32 -21
  119. package/dist/src/ping/index.js.map +1 -1
  120. package/dist/src/pnet/index.d.ts +1 -1
  121. package/dist/src/pnet/index.js +1 -1
  122. package/dist/src/pnet/index.js.map +1 -1
  123. package/dist/src/registrar.d.ts +7 -6
  124. package/dist/src/registrar.d.ts.map +1 -1
  125. package/dist/src/registrar.js +7 -11
  126. package/dist/src/registrar.js.map +1 -1
  127. package/dist/src/transport-manager.d.ts +9 -9
  128. package/dist/src/transport-manager.d.ts.map +1 -1
  129. package/dist/src/transport-manager.js +7 -4
  130. package/dist/src/transport-manager.js.map +1 -1
  131. package/dist/src/upgrader.d.ts +13 -12
  132. package/dist/src/upgrader.d.ts.map +1 -1
  133. package/dist/src/upgrader.js +37 -26
  134. package/dist/src/upgrader.js.map +1 -1
  135. package/dist/src/upnp-nat/index.d.ts +4 -4
  136. package/dist/src/upnp-nat/index.d.ts.map +1 -1
  137. package/dist/src/upnp-nat/index.js +1 -1
  138. package/dist/src/upnp-nat/index.js.map +1 -1
  139. package/dist/src/utils/peer-job-queue.d.ts +1 -1
  140. package/dist/src/utils/peer-job-queue.js +1 -1
  141. package/dist/src/utils/peer-job-queue.js.map +1 -1
  142. package/dist/src/version.d.ts +1 -1
  143. package/dist/src/version.js +1 -1
  144. package/package.json +52 -85
  145. package/src/address-manager/README.md +1 -1
  146. package/src/address-manager/index.ts +5 -5
  147. package/src/address-manager/utils.ts +0 -1
  148. package/src/autonat/index.ts +33 -12
  149. package/src/circuit-relay/index.ts +37 -1
  150. package/src/circuit-relay/server/advert-service.ts +3 -3
  151. package/src/circuit-relay/server/index.ts +41 -38
  152. package/src/circuit-relay/server/reservation-store.ts +3 -3
  153. package/src/circuit-relay/server/reservation-voucher.ts +2 -2
  154. package/src/circuit-relay/transport/discovery.ts +10 -11
  155. package/src/circuit-relay/transport/index.ts +76 -38
  156. package/src/circuit-relay/transport/listener.ts +9 -7
  157. package/src/circuit-relay/transport/reservation-store.ts +41 -18
  158. package/src/circuit-relay/utils.ts +1 -1
  159. package/src/components.ts +16 -15
  160. package/src/config/connection-gater.browser.ts +1 -1
  161. package/src/config/connection-gater.ts +1 -1
  162. package/src/config.ts +3 -4
  163. package/src/connection/index.ts +82 -40
  164. package/src/connection-manager/auto-dial.ts +8 -6
  165. package/src/connection-manager/connection-pruner.ts +6 -6
  166. package/src/connection-manager/constants.ts +0 -1
  167. package/src/connection-manager/dial-queue.ts +20 -27
  168. package/src/connection-manager/index.ts +27 -22
  169. package/src/content-routing/index.ts +6 -6
  170. package/src/content-routing/utils.ts +3 -3
  171. package/src/fetch/constants.ts +0 -1
  172. package/src/fetch/index.ts +11 -11
  173. package/src/get-peer.ts +3 -3
  174. package/src/identify/consts.ts +0 -1
  175. package/src/identify/identify.ts +85 -92
  176. package/src/identify/index.ts +32 -8
  177. package/src/index.ts +11 -12
  178. package/src/insecure/index.ts +3 -3
  179. package/src/libp2p.ts +24 -18
  180. package/src/peer-routing.ts +6 -6
  181. package/src/ping/constants.ts +0 -1
  182. package/src/ping/index.ts +45 -27
  183. package/src/pnet/index.ts +2 -2
  184. package/src/registrar.ts +14 -17
  185. package/src/transport-manager.ts +16 -12
  186. package/src/upgrader.ts +52 -39
  187. package/src/upnp-nat/index.ts +5 -5
  188. package/src/utils/peer-job-queue.ts +2 -2
  189. package/src/version.ts +1 -1
  190. package/dist/src/pubsub/dummy-pubsub.d.ts +0 -18
  191. package/dist/src/pubsub/dummy-pubsub.d.ts.map +0 -1
  192. package/dist/src/pubsub/dummy-pubsub.js +0 -38
  193. package/dist/src/pubsub/dummy-pubsub.js.map +0 -1
  194. package/src/pubsub/dummy-pubsub.ts +0 -53
@@ -22,15 +22,15 @@
22
22
  * ```
23
23
  */
24
24
 
25
- import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interface-connection-encrypter/errors'
25
+ import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interface/errors'
26
26
  import { logger } from '@libp2p/logger'
27
27
  import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
28
28
  import { handshake } from 'it-handshake'
29
29
  import * as lp from 'it-length-prefixed'
30
30
  import map from 'it-map'
31
31
  import { Exchange, KeyType } from './pb/proto.js'
32
- import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
33
- import type { PeerId } from '@libp2p/interface-peer-id'
32
+ import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface/connection-encrypter'
33
+ import type { PeerId } from '@libp2p/interface/peer-id'
34
34
  import type { Duplex, Source } from 'it-stream-types'
35
35
  import type { Uint8ArrayList } from 'uint8arraylist'
36
36
 
package/src/libp2p.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { setMaxListeners } from 'events'
2
2
  import { unmarshalPublicKey } from '@libp2p/crypto/keys'
3
- import { type ContentRouting, contentRouting } from '@libp2p/interface-content-routing'
4
- import { peerDiscovery } from '@libp2p/interface-peer-discovery'
5
- import { type PeerRouting, peerRouting } from '@libp2p/interface-peer-routing'
6
- import { CodeError } from '@libp2p/interfaces/errors'
7
- import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
3
+ import { type ContentRouting, contentRouting } from '@libp2p/interface/content-routing'
4
+ import { CodeError } from '@libp2p/interface/errors'
5
+ import { EventEmitter, CustomEvent } from '@libp2p/interface/events'
6
+ import { peerDiscovery } from '@libp2p/interface/peer-discovery'
7
+ import { type PeerRouting, peerRouting } from '@libp2p/interface/peer-routing'
8
8
  import { DefaultKeyChain } from '@libp2p/keychain'
9
9
  import { logger } from '@libp2p/logger'
10
10
  import { PeerSet } from '@libp2p/peer-collections'
@@ -29,15 +29,15 @@ import { DefaultTransportManager } from './transport-manager.js'
29
29
  import { DefaultUpgrader } from './upgrader.js'
30
30
  import type { Components } from './components.js'
31
31
  import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
32
- import type { Connection, Stream } from '@libp2p/interface-connection'
33
- import type { KeyChain } from '@libp2p/interface-keychain'
34
- import type { Libp2pEvents, PendingDial, ServiceMap } from '@libp2p/interface-libp2p'
35
- import type { Metrics } from '@libp2p/interface-metrics'
36
- import type { PeerId } from '@libp2p/interface-peer-id'
37
- import type { PeerInfo } from '@libp2p/interface-peer-info'
38
- import type { PeerStore } from '@libp2p/interface-peer-store'
39
- import type { StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
40
- import type { AbortOptions } from '@libp2p/interfaces'
32
+ import type { Libp2pEvents, PendingDial, ServiceMap, AbortOptions } from '@libp2p/interface'
33
+ import type { Connection, NewStreamOptions, Stream } from '@libp2p/interface/connection'
34
+ import type { KeyChain } from '@libp2p/interface/keychain'
35
+ import type { Metrics } from '@libp2p/interface/metrics'
36
+ import type { PeerId } from '@libp2p/interface/peer-id'
37
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
38
+ import type { PeerStore } from '@libp2p/interface/peer-store'
39
+ import type { Topology } from '@libp2p/interface/topology'
40
+ import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal/registrar'
41
41
  import type { Datastore } from 'interface-datastore'
42
42
 
43
43
  const log = logger('libp2p')
@@ -99,7 +99,13 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
99
99
  components.events.addEventListener('peer:update', evt => {
100
100
  // if there was no peer previously in the peer store this is a new peer
101
101
  if (evt.detail.previous == null) {
102
- this.safeDispatchEvent('peer:discovery', { detail: evt.detail.peer })
102
+ const peerInfo: PeerInfo = {
103
+ id: evt.detail.peer.id,
104
+ multiaddrs: evt.detail.peer.addresses.map(a => a.multiaddr),
105
+ protocols: evt.detail.peer.protocols
106
+ }
107
+
108
+ this.safeDispatchEvent('peer:discovery', { detail: peerInfo })
103
109
  }
104
110
  })
105
111
 
@@ -283,7 +289,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
283
289
  return this.components.connectionManager.openConnection(peer, options)
284
290
  }
285
291
 
286
- async dialProtocol (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options: AbortOptions = {}): Promise<Stream> {
292
+ async dialProtocol (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options: NewStreamOptions = {}): Promise<Stream> {
287
293
  if (protocols == null) {
288
294
  throw new CodeError('no protocols were provided to open a stream', codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
289
295
  }
@@ -307,12 +313,12 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
307
313
  return this.components.registrar.getProtocols()
308
314
  }
309
315
 
310
- async hangUp (peer: PeerId | Multiaddr): Promise<void> {
316
+ async hangUp (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<void> {
311
317
  if (isMultiaddr(peer)) {
312
318
  peer = peerIdFromString(peer.getPeerId() ?? '')
313
319
  }
314
320
 
315
- await this.components.connectionManager.closeConnections(peer)
321
+ await this.components.connectionManager.closeConnections(peer, options)
316
322
  }
317
323
 
318
324
  /**
@@ -1,4 +1,4 @@
1
- import { CodeError } from '@libp2p/interfaces/errors'
1
+ import { CodeError } from '@libp2p/interface/errors'
2
2
  import { logger } from '@libp2p/logger'
3
3
  import filter from 'it-filter'
4
4
  import first from 'it-first'
@@ -10,11 +10,11 @@ import {
10
10
  requirePeers
11
11
  } from './content-routing/utils.js'
12
12
  import { codes, messages } from './errors.js'
13
- import type { PeerId } from '@libp2p/interface-peer-id'
14
- import type { PeerInfo } from '@libp2p/interface-peer-info'
15
- import type { PeerRouting } from '@libp2p/interface-peer-routing'
16
- import type { PeerStore } from '@libp2p/interface-peer-store'
17
- import type { AbortOptions } from '@libp2p/interfaces'
13
+ import type { AbortOptions } from '@libp2p/interface'
14
+ import type { PeerId } from '@libp2p/interface/peer-id'
15
+ import type { PeerInfo } from '@libp2p/interface/peer-info'
16
+ import type { PeerRouting } from '@libp2p/interface/peer-routing'
17
+ import type { PeerStore } from '@libp2p/interface/peer-store'
18
18
 
19
19
  const log = logger('libp2p:peer-routing')
20
20
 
@@ -1,4 +1,3 @@
1
-
2
1
  export const PROTOCOL = '/ipfs/ping/1.0.0'
3
2
  export const PING_LENGTH = 32
4
3
  export const PROTOCOL_VERSION = '1.0.0'
package/src/ping/index.ts CHANGED
@@ -1,20 +1,18 @@
1
- import { setMaxListeners } from 'events'
2
1
  import { randomBytes } from '@libp2p/crypto'
3
- import { CodeError } from '@libp2p/interfaces/errors'
2
+ import { CodeError } from '@libp2p/interface/errors'
4
3
  import { logger } from '@libp2p/logger'
5
4
  import { abortableDuplex } from 'abortable-iterator'
6
- import { anySignal } from 'any-signal'
7
5
  import first from 'it-first'
8
6
  import { pipe } from 'it-pipe'
9
7
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
10
8
  import { codes } from '../errors.js'
11
9
  import { PROTOCOL_PREFIX, PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION, TIMEOUT, MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS } from './constants.js'
12
- import type { Stream } from '@libp2p/interface-connection'
13
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
14
- import type { PeerId } from '@libp2p/interface-peer-id'
15
- import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
16
- import type { AbortOptions } from '@libp2p/interfaces'
17
- import type { Startable } from '@libp2p/interfaces/startable'
10
+ import type { AbortOptions } from '@libp2p/interface'
11
+ import type { Stream } from '@libp2p/interface/connection'
12
+ import type { PeerId } from '@libp2p/interface/peer-id'
13
+ import type { Startable } from '@libp2p/interface/startable'
14
+ import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
15
+ import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar'
18
16
  import type { Multiaddr } from '@multiformats/multiaddr'
19
17
 
20
18
  const log = logger('libp2p:ping')
@@ -27,6 +25,7 @@ export interface PingServiceInit {
27
25
  protocolPrefix?: string
28
26
  maxInboundStreams?: number
29
27
  maxOutboundStreams?: number
28
+ runOnTransientConnection?: boolean
30
29
 
31
30
  /**
32
31
  * How long we should wait for a ping response
@@ -46,6 +45,7 @@ class DefaultPingService implements Startable, PingService {
46
45
  private readonly timeout: number
47
46
  private readonly maxInboundStreams: number
48
47
  private readonly maxOutboundStreams: number
48
+ private readonly runOnTransientConnection: boolean
49
49
 
50
50
  constructor (components: PingServiceComponents, init: PingServiceInit) {
51
51
  this.components = components
@@ -54,12 +54,14 @@ class DefaultPingService implements Startable, PingService {
54
54
  this.timeout = init.timeout ?? TIMEOUT
55
55
  this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS
56
56
  this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
57
+ this.runOnTransientConnection = init.runOnTransientConnection ?? true
57
58
  }
58
59
 
59
60
  async start (): Promise<void> {
60
61
  await this.components.registrar.handle(this.protocol, this.handleMessage, {
61
62
  maxInboundStreams: this.maxInboundStreams,
62
- maxOutboundStreams: this.maxOutboundStreams
63
+ maxOutboundStreams: this.maxOutboundStreams,
64
+ runOnTransientConnection: this.runOnTransientConnection
63
65
  })
64
66
  this.started = true
65
67
  }
@@ -77,11 +79,19 @@ class DefaultPingService implements Startable, PingService {
77
79
  * A handler to register with Libp2p to process ping messages
78
80
  */
79
81
  handleMessage (data: IncomingStreamData): void {
82
+ log('incoming ping from %p', data.connection.remotePeer)
83
+
80
84
  const { stream } = data
85
+ const start = Date.now()
81
86
 
82
87
  void pipe(stream, stream)
83
88
  .catch(err => {
84
- log.error(err)
89
+ log.error('incoming ping from %p failed with error', data.connection.remotePeer, err)
90
+ })
91
+ .finally(() => {
92
+ const ms = Date.now() - start
93
+
94
+ log('incoming ping from %p complete in %dms', data.connection.remotePeer, ms)
85
95
  })
86
96
  }
87
97
 
@@ -92,45 +102,53 @@ class DefaultPingService implements Startable, PingService {
92
102
  * @returns {Promise<number>}
93
103
  */
94
104
  async ping (peer: PeerId | Multiaddr | Multiaddr[], options: AbortOptions = {}): Promise<number> {
95
- log('dialing %s to %p', this.protocol, peer)
105
+ log('pinging %p', peer)
96
106
 
97
107
  const start = Date.now()
98
108
  const data = randomBytes(PING_LENGTH)
99
109
  const connection = await this.components.connectionManager.openConnection(peer, options)
100
110
  let stream: Stream | undefined
101
111
 
102
- const signal = anySignal([AbortSignal.timeout(this.timeout), options?.signal])
103
-
104
- try {
105
- // fails on node < 15.4
106
- setMaxListeners?.(Infinity, signal)
107
- } catch {}
112
+ options.signal = options.signal ?? AbortSignal.timeout(this.timeout)
108
113
 
109
114
  try {
110
- stream = await connection.newStream([this.protocol], {
111
- signal
115
+ stream = await connection.newStream(this.protocol, {
116
+ ...options,
117
+ runOnTransientConnection: this.runOnTransientConnection
112
118
  })
113
119
 
114
120
  // make stream abortable
115
- const source = abortableDuplex(stream, signal)
121
+ const source = abortableDuplex(stream, options.signal)
116
122
 
117
123
  const result = await pipe(
118
124
  [data],
119
125
  source,
120
126
  async (source) => first(source)
121
127
  )
122
- const end = Date.now()
123
128
 
124
- if (result == null || !uint8ArrayEquals(data, result.subarray())) {
125
- throw new CodeError('Received wrong ping ack', codes.ERR_WRONG_PING_ACK)
129
+ const ms = Date.now() - start
130
+
131
+ if (result == null) {
132
+ throw new CodeError(`Did not receive a ping ack after ${ms}ms`, codes.ERR_WRONG_PING_ACK)
133
+ }
134
+
135
+ if (!uint8ArrayEquals(data, result.subarray())) {
136
+ throw new CodeError(`Received wrong ping ack after ${ms}ms`, codes.ERR_WRONG_PING_ACK)
126
137
  }
127
138
 
128
- return end - start
139
+ log('ping %p complete in %dms', connection.remotePeer, ms)
140
+
141
+ return ms
142
+ } catch (err: any) {
143
+ log.error('error while pinging %p', connection.remotePeer, err)
144
+
145
+ stream?.abort(err)
146
+
147
+ throw err
129
148
  } finally {
130
149
  if (stream != null) {
131
- stream.close()
150
+ await stream.close()
132
151
  }
133
- signal.clear()
134
152
  }
135
153
  }
136
154
  }
package/src/pnet/index.ts CHANGED
@@ -23,7 +23,7 @@
23
23
  */
24
24
 
25
25
  import { randomBytes } from '@libp2p/crypto'
26
- import { CodeError } from '@libp2p/interfaces/errors'
26
+ import { CodeError } from '@libp2p/interface/errors'
27
27
  import { logger } from '@libp2p/logger'
28
28
  import { handshake } from 'it-handshake'
29
29
  import map from 'it-map'
@@ -37,7 +37,7 @@ import {
37
37
  } from './crypto.js'
38
38
  import * as Errors from './errors.js'
39
39
  import { NONCE_LENGTH } from './key-generator.js'
40
- import type { ConnectionProtector, MultiaddrConnection } from '@libp2p/interface-connection'
40
+ import type { ConnectionProtector, MultiaddrConnection } from '@libp2p/interface/connection'
41
41
 
42
42
  const log = logger('libp2p:pnet')
43
43
 
package/src/registrar.ts CHANGED
@@ -1,13 +1,14 @@
1
- import { isTopology, type StreamHandlerOptions, type StreamHandlerRecord, type Registrar, type StreamHandler, type Topology } from '@libp2p/interface-registrar'
2
- import { CodeError } from '@libp2p/interfaces/errors'
1
+ import { CodeError } from '@libp2p/interface/errors'
3
2
  import { logger } from '@libp2p/logger'
4
3
  import merge from 'merge-options'
5
4
  import { codes } from './errors.js'
6
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
7
- import type { Libp2pEvents, PeerUpdate } from '@libp2p/interface-libp2p'
8
- import type { PeerId } from '@libp2p/interface-peer-id'
9
- import type { PeerStore } from '@libp2p/interface-peer-store'
10
- import type { EventEmitter } from '@libp2p/interfaces/events'
5
+ import type { Libp2pEvents, PeerUpdate } from '@libp2p/interface'
6
+ import type { EventEmitter } from '@libp2p/interface/events'
7
+ import type { PeerId } from '@libp2p/interface/peer-id'
8
+ import type { PeerStore } from '@libp2p/interface/peer-store'
9
+ import type { Topology } from '@libp2p/interface/topology'
10
+ import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
11
+ import type { StreamHandlerOptions, StreamHandlerRecord, Registrar, StreamHandler } from '@libp2p/interface-internal/registrar'
11
12
 
12
13
  const log = logger('libp2p:registrar')
13
14
 
@@ -116,9 +117,8 @@ export class DefaultRegistrar implements Registrar {
116
117
  * Register handlers for a set of multicodecs given
117
118
  */
118
119
  async register (protocol: string, topology: Topology): Promise<string> {
119
- if (!isTopology(topology)) {
120
- log.error('topology must be an instance of interfaces/topology')
121
- throw new CodeError('topology must be an instance of interfaces/topology', codes.ERR_INVALID_PARAMETERS)
120
+ if (topology == null) {
121
+ throw new CodeError('invalid topology', codes.ERR_INVALID_PARAMETERS)
122
122
  }
123
123
 
124
124
  // Create topology
@@ -133,9 +133,6 @@ export class DefaultRegistrar implements Registrar {
133
133
 
134
134
  topologies.set(id, topology)
135
135
 
136
- // Set registrar
137
- await topology.setRegistrar(this)
138
-
139
136
  return id
140
137
  }
141
138
 
@@ -171,7 +168,7 @@ export class DefaultRegistrar implements Registrar {
171
168
  }
172
169
 
173
170
  for (const topology of topologies.values()) {
174
- topology.onDisconnect(remotePeer)
171
+ topology.onDisconnect?.(remotePeer)
175
172
  }
176
173
  }
177
174
  })
@@ -211,7 +208,7 @@ export class DefaultRegistrar implements Registrar {
211
208
  }
212
209
 
213
210
  for (const topology of topologies.values()) {
214
- topology.onConnect(remotePeer, connection)
211
+ topology.onConnect?.(remotePeer, connection)
215
212
  }
216
213
  }
217
214
  })
@@ -242,7 +239,7 @@ export class DefaultRegistrar implements Registrar {
242
239
  }
243
240
 
244
241
  for (const topology of topologies.values()) {
245
- topology.onDisconnect(peer.id)
242
+ topology.onDisconnect?.(peer.id)
246
243
  }
247
244
  }
248
245
 
@@ -260,7 +257,7 @@ export class DefaultRegistrar implements Registrar {
260
257
  if (connection == null) {
261
258
  continue
262
259
  }
263
- topology.onConnect(peer.id, connection)
260
+ topology.onConnect?.(peer.id, connection)
264
261
  }
265
262
  }
266
263
  }
@@ -1,16 +1,16 @@
1
- import { FaultTolerance } from '@libp2p/interface-transport'
2
- import { CodeError } from '@libp2p/interfaces/errors'
1
+ import { CodeError } from '@libp2p/interface/errors'
2
+ import { trackedMap } from '@libp2p/interface/metrics/tracked-map'
3
+ import { FaultTolerance } from '@libp2p/interface/transport'
3
4
  import { logger } from '@libp2p/logger'
4
- import { trackedMap } from '@libp2p/tracked-map'
5
5
  import { codes } from './errors.js'
6
- import type { AddressManager } from '@libp2p/interface-address-manager'
7
- import type { Connection } from '@libp2p/interface-connection'
8
- import type { Libp2pEvents } from '@libp2p/interface-libp2p'
9
- import type { Metrics } from '@libp2p/interface-metrics'
10
- import type { Listener, Transport, TransportManager, Upgrader } from '@libp2p/interface-transport'
11
- import type { AbortOptions } from '@libp2p/interfaces'
12
- import type { EventEmitter } from '@libp2p/interfaces/events'
13
- import type { Startable } from '@libp2p/interfaces/startable'
6
+ import type { Libp2pEvents, AbortOptions } from '@libp2p/interface'
7
+ import type { Connection } from '@libp2p/interface/connection'
8
+ import type { EventEmitter } from '@libp2p/interface/events'
9
+ import type { Metrics } from '@libp2p/interface/metrics'
10
+ import type { Startable } from '@libp2p/interface/startable'
11
+ import type { Listener, Transport, Upgrader } from '@libp2p/interface/transport'
12
+ import type { AddressManager } from '@libp2p/interface-internal/address-manager'
13
+ import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
14
14
  import type { Multiaddr } from '@multiformats/multiaddr'
15
15
 
16
16
  const log = logger('libp2p:transports')
@@ -177,6 +177,10 @@ export class DefaultTransportManager implements TransportManager, Startable {
177
177
  * Starts listeners for each listen Multiaddr
178
178
  */
179
179
  async listen (addrs: Multiaddr[]): Promise<void> {
180
+ if (!this.isStarted()) {
181
+ throw new CodeError('Not started', codes.ERR_NODE_NOT_STARTED)
182
+ }
183
+
180
184
  if (addrs == null || addrs.length === 0) {
181
185
  log('no addresses were provided for listening, this node is dial only')
182
186
  return
@@ -190,7 +194,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
190
194
 
191
195
  // For each supported multiaddr, create a listener
192
196
  for (const addr of supportedAddrs) {
193
- log('creating listener for %s on %s', key, addr)
197
+ log('creating listener for %s on %a', key, addr)
194
198
  const listener = transport.createListener({
195
199
  upgrader: this.components.upgrader
196
200
  })
package/src/upgrader.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { setMaxListeners } from 'events'
2
- import { CodeError } from '@libp2p/interfaces/errors'
2
+ import { CodeError } from '@libp2p/interface/errors'
3
3
  import { logger } from '@libp2p/logger'
4
4
  import * as mss from '@libp2p/multistream-select'
5
5
  import { peerIdFromString } from '@libp2p/peer-id'
@@ -9,19 +9,18 @@ import { createConnection } from './connection/index.js'
9
9
  import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
10
10
  import { codes } from './errors.js'
11
11
  import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
12
- import type { MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions } from '@libp2p/interface-connection'
13
- import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
14
- import type { ConnectionGater } from '@libp2p/interface-connection-gater'
15
- import type { ConnectionManager } from '@libp2p/interface-connection-manager'
16
- import type { Libp2pEvents } from '@libp2p/interface-libp2p'
17
- import type { Metrics } from '@libp2p/interface-metrics'
18
- import type { PeerId } from '@libp2p/interface-peer-id'
19
- import type { PeerStore } from '@libp2p/interface-peer-store'
20
- import type { Registrar } from '@libp2p/interface-registrar'
21
- import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
22
- import type { Upgrader, UpgraderOptions } from '@libp2p/interface-transport'
23
- import type { AbortOptions } from '@libp2p/interfaces'
24
- import type { EventEmitter } from '@libp2p/interfaces/events'
12
+ import type { Libp2pEvents, AbortOptions } from '@libp2p/interface'
13
+ import type { MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions } from '@libp2p/interface/connection'
14
+ import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface/connection-encrypter'
15
+ import type { ConnectionGater } from '@libp2p/interface/connection-gater'
16
+ import type { EventEmitter } from '@libp2p/interface/events'
17
+ import type { Metrics } from '@libp2p/interface/metrics'
18
+ import type { PeerId } from '@libp2p/interface/peer-id'
19
+ import type { PeerStore } from '@libp2p/interface/peer-store'
20
+ import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface/stream-muxer'
21
+ import type { Upgrader, UpgraderOptions } from '@libp2p/interface/transport'
22
+ import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
23
+ import type { Registrar } from '@libp2p/interface-internal/registrar'
25
24
  import type { Duplex, Source } from 'it-stream-types'
26
25
 
27
26
  const log = logger('libp2p:upgrader')
@@ -33,6 +32,7 @@ interface CreateConnectionOptions {
33
32
  upgradedConn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>
34
33
  remotePeer: PeerId
35
34
  muxerFactory?: StreamMuxerFactory
35
+ transient?: boolean
36
36
  }
37
37
 
38
38
  interface OnStreamOptions {
@@ -90,7 +90,7 @@ function countStreams (protocol: string, direction: 'inbound' | 'outbound', conn
90
90
  let streamCount = 0
91
91
 
92
92
  connection.streams.forEach(stream => {
93
- if (stream.stat.direction === direction && stream.stat.protocol === protocol) {
93
+ if (stream.direction === direction && stream.protocol === protocol) {
94
94
  streamCount++
95
95
  }
96
96
  })
@@ -252,7 +252,8 @@ export class DefaultUpgrader implements Upgrader {
252
252
  maConn,
253
253
  upgradedConn,
254
254
  muxerFactory,
255
- remotePeer
255
+ remotePeer,
256
+ transient: opts?.transient
256
257
  })
257
258
  } finally {
258
259
  this.components.connectionManager.afterUpgradeInbound()
@@ -349,7 +350,8 @@ export class DefaultUpgrader implements Upgrader {
349
350
  maConn,
350
351
  upgradedConn,
351
352
  muxerFactory,
352
- remotePeer
353
+ remotePeer,
354
+ transient: opts?.transient
353
355
  })
354
356
  }
355
357
 
@@ -363,7 +365,8 @@ export class DefaultUpgrader implements Upgrader {
363
365
  maConn,
364
366
  upgradedConn,
365
367
  remotePeer,
366
- muxerFactory
368
+ muxerFactory,
369
+ transient
367
370
  } = opts
368
371
 
369
372
  let muxer: StreamMuxer | undefined
@@ -404,7 +407,7 @@ export class DefaultUpgrader implements Upgrader {
404
407
  // the souce/sink
405
408
  muxedStream.source = stream.source
406
409
  muxedStream.sink = stream.sink
407
- muxedStream.stat.protocol = protocol
410
+ muxedStream.protocol = protocol
408
411
 
409
412
  // If a protocol stream has been successfully negotiated and is to be passed to the application,
410
413
  // the peerstore should ensure that the peer is registered with that protocol
@@ -417,11 +420,11 @@ export class DefaultUpgrader implements Upgrader {
417
420
 
418
421
  this._onStream({ connection, stream: muxedStream, protocol })
419
422
  })
420
- .catch(err => {
423
+ .catch(async err => {
421
424
  log.error(err)
422
425
 
423
- if (muxedStream.stat.timeline.close == null) {
424
- muxedStream.close()
426
+ if (muxedStream.timeline.close == null) {
427
+ await muxedStream.close()
425
428
  }
426
429
  })
427
430
  },
@@ -473,7 +476,7 @@ export class DefaultUpgrader implements Upgrader {
473
476
  // the souce/sink
474
477
  muxedStream.source = stream.source
475
478
  muxedStream.sink = stream.sink
476
- muxedStream.stat.protocol = protocol
479
+ muxedStream.protocol = protocol
477
480
 
478
481
  this.components.metrics?.trackProtocolStream(muxedStream, connection)
479
482
 
@@ -481,8 +484,8 @@ export class DefaultUpgrader implements Upgrader {
481
484
  } catch (err: any) {
482
485
  log.error('could not create new stream', err)
483
486
 
484
- if (muxedStream.stat.timeline.close == null) {
485
- muxedStream.close()
487
+ if (muxedStream.timeline.close == null) {
488
+ muxedStream.abort(err)
486
489
  }
487
490
 
488
491
  if (err.code != null) {
@@ -509,7 +512,7 @@ export class DefaultUpgrader implements Upgrader {
509
512
  // Wait for close to finish before notifying of the closure
510
513
  (async () => {
511
514
  try {
512
- if (connection.stat.status === 'OPEN') {
515
+ if (connection.status === 'open') {
513
516
  await connection.close()
514
517
  }
515
518
  } catch (err: any) {
@@ -537,20 +540,26 @@ export class DefaultUpgrader implements Upgrader {
537
540
  connection = createConnection({
538
541
  remoteAddr: maConn.remoteAddr,
539
542
  remotePeer,
540
- stat: {
541
- status: 'OPEN',
542
- direction,
543
- timeline: maConn.timeline,
544
- multiplexer: muxer?.protocol,
545
- encryption: cryptoProtocol
546
- },
543
+ status: 'open',
544
+ direction,
545
+ timeline: maConn.timeline,
546
+ multiplexer: muxer?.protocol,
547
+ encryption: cryptoProtocol,
548
+ transient,
547
549
  newStream: newStream ?? errConnectionNotMultiplexed,
548
- getStreams: () => { if (muxer != null) { return muxer.streams } else { return errConnectionNotMultiplexed() } },
549
- close: async () => {
550
- await maConn.close()
551
- // Ensure remaining streams are closed
550
+ getStreams: () => { if (muxer != null) { return muxer.streams } else { return [] } },
551
+ close: async (options?: AbortOptions) => {
552
+ await maConn.close(options)
553
+ // Ensure remaining streams are closed gracefully
552
554
  if (muxer != null) {
553
- muxer.close()
555
+ await muxer.close(options)
556
+ }
557
+ },
558
+ abort: (err) => {
559
+ maConn.abort(err)
560
+ // Ensure remaining streams are aborted
561
+ if (muxer != null) {
562
+ muxer.abort(err)
554
563
  }
555
564
  }
556
565
  })
@@ -567,7 +576,11 @@ export class DefaultUpgrader implements Upgrader {
567
576
  */
568
577
  _onStream (opts: OnStreamOptions): void {
569
578
  const { connection, stream, protocol } = opts
570
- const { handler } = this.components.registrar.getHandler(protocol)
579
+ const { handler, options } = this.components.registrar.getHandler(protocol)
580
+
581
+ if (connection.transient && options.runOnTransientConnection !== true) {
582
+ throw new CodeError('Cannot open protocol stream on transient connection', 'ERR_TRANSIENT_CONNECTION')
583
+ }
571
584
 
572
585
  handler({ connection, stream })
573
586
  }
@@ -1,5 +1,5 @@
1
1
  import { upnpNat, type NatAPI } from '@achingbrain/nat-port-mapper'
2
- import { CodeError } from '@libp2p/interfaces/errors'
2
+ import { CodeError } from '@libp2p/interface/errors'
3
3
  import { logger } from '@libp2p/logger'
4
4
  import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
5
5
  import { fromNodeAddress } from '@multiformats/multiaddr'
@@ -7,10 +7,10 @@ import isPrivateIp from 'private-ip'
7
7
  import { isBrowser } from 'wherearewe'
8
8
  import { codes } from '../errors.js'
9
9
  import * as pkg from '../version.js'
10
- import type { AddressManager } from '@libp2p/interface-address-manager'
11
- import type { PeerId } from '@libp2p/interface-peer-id'
12
- import type { TransportManager } from '@libp2p/interface-transport'
13
- import type { Startable } from '@libp2p/interfaces/startable'
10
+ import type { PeerId } from '@libp2p/interface/peer-id'
11
+ import type { Startable } from '@libp2p/interface/startable'
12
+ import type { AddressManager } from '@libp2p/interface-internal/address-manager'
13
+ import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
14
14
 
15
15
  const log = logger('libp2p:upnp-nat')
16
16
  const DEFAULT_TTL = 7200
@@ -1,9 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
 
3
- import { CodeError } from '@libp2p/interfaces/errors'
3
+ import { CodeError } from '@libp2p/interface/errors'
4
4
  import PQueue from 'p-queue'
5
5
  import { codes } from '../errors.js'
6
- import type { PeerId } from '@libp2p/interface-peer-id'
6
+ import type { PeerId } from '@libp2p/interface/peer-id'
7
7
  import type { QueueAddOptions, Options, Queue } from 'p-queue'
8
8
 
9
9
  // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.45.9'
1
+ export const version = '0.46.0'
2
2
  export const name = 'libp2p'