libp2p 0.41.0-3e53c19 → 0.41.0-40fe372

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.
@@ -143,7 +143,7 @@ export interface DefaultConnectionManagerComponents {
143
143
  dialer: Dialer
144
144
  }
145
145
 
146
- export type ConnectionManagerInit = Required<ConnectionManagerConfig>
146
+ export type ConnectionManagerInit = ConnectionManagerConfig
147
147
 
148
148
  /**
149
149
  * Responsible for managing known connections.
@@ -153,7 +153,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
153
153
  private readonly opts: ConnectionManagerInit
154
154
  private readonly connections: Map<string, Connection[]>
155
155
  private started: boolean
156
- private readonly latencyMonitor: LatencyMonitor
156
+ private readonly latencyMonitor?: LatencyMonitor
157
157
  private readonly startupReconnectTimeout: number
158
158
  private connectOnStartupController?: TimeoutController
159
159
  private readonly dialTimeout: number
@@ -182,10 +182,12 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
182
182
 
183
183
  this.started = false
184
184
 
185
- this.latencyMonitor = new LatencyMonitor({
186
- latencyCheckIntervalMs: init.pollInterval,
187
- dataEmitIntervalMs: init.pollInterval
188
- })
185
+ if (init.maxEventLoopDelay != null && init.maxEventLoopDelay > 0 && init.maxEventLoopDelay !== Infinity) {
186
+ this.latencyMonitor = new LatencyMonitor({
187
+ latencyCheckIntervalMs: init.pollInterval,
188
+ dataEmitIntervalMs: init.pollInterval
189
+ })
190
+ }
189
191
 
190
192
  try {
191
193
  // This emitter gets listened to a lot
@@ -297,9 +299,9 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
297
299
  })
298
300
 
299
301
  // latency monitor
300
- this.latencyMonitor.start()
302
+ this.latencyMonitor?.start()
301
303
  this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
302
- this.latencyMonitor.addEventListener('data', this._onLatencyMeasure)
304
+ this.latencyMonitor?.addEventListener('data', this._onLatencyMeasure)
303
305
 
304
306
  this.started = true
305
307
  log('started')
@@ -361,8 +363,8 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
361
363
  * Stops the Connection Manager
362
364
  */
363
365
  async stop () {
364
- this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure)
365
- this.latencyMonitor.stop()
366
+ this.latencyMonitor?.removeEventListener('data', this._onLatencyMeasure)
367
+ this.latencyMonitor?.stop()
366
368
 
367
369
  this.started = false
368
370
  await this._close()
@@ -579,6 +581,12 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
579
581
  */
580
582
  async _checkMaxLimit (name: keyof ConnectionManagerInit, value: number, toPrune: number = 1) {
581
583
  const limit = this.opts[name]
584
+
585
+ if (limit == null) {
586
+ log.trace('limit %s was not set so it cannot be applied', name)
587
+ return
588
+ }
589
+
582
590
  log.trace('checking limit of %s. current value: %d of %d', name, value, limit)
583
591
  if (value > limit) {
584
592
  log('%s: limit exceeded: %p, %d/%d, pruning %d connection(s)', this.components.peerId, name, value, limit, toPrune)
@@ -623,6 +631,18 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
623
631
  return -1
624
632
  }
625
633
 
634
+ // if the peers have an equal tag value then we want to close short-lived connections first
635
+ const connectionALifespan = a.stat.timeline.open
636
+ const connectionBLifespan = b.stat.timeline.open
637
+
638
+ if (connectionALifespan < connectionBLifespan) {
639
+ return 1
640
+ }
641
+
642
+ if (connectionALifespan > connectionBLifespan) {
643
+ return -1
644
+ }
645
+
626
646
  return 0
627
647
  })
628
648
 
@@ -79,7 +79,7 @@ export class IdentifyService implements Startable {
79
79
  private readonly components: IdentifyServiceComponents
80
80
  private readonly identifyProtocolStr: string
81
81
  private readonly identifyPushProtocolStr: string
82
- private readonly host: {
82
+ public readonly host: {
83
83
  protocolVersion: string
84
84
  agentVersion: string
85
85
  }
package/src/libp2p.ts CHANGED
@@ -56,7 +56,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
56
56
  public peerId: PeerId
57
57
  public dht: DualDHT
58
58
  public pubsub: PubSub
59
- public identifyService?: IdentifyService
59
+ public identifyService: IdentifyService
60
60
  public fetchService: FetchService
61
61
  public pingService: PingService
62
62
  public components: Components
@@ -132,6 +132,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
132
132
  // Create the Connection Manager
133
133
  this.connectionManager = this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager)
134
134
 
135
+ // forward connection manager events
136
+ this.components.connectionManager.addEventListener('peer:disconnect', (event) => {
137
+ this.dispatchEvent(new CustomEvent<Connection>('peer:disconnect', { detail: event.detail }))
138
+ })
139
+ this.components.connectionManager.addEventListener('peer:connect', (event) => {
140
+ this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: event.detail }))
141
+ })
142
+
135
143
  // Create the Registrar
136
144
  this.registrar = this.components.registrar = new DefaultRegistrar(this.components)
137
145
 
@@ -373,6 +381,10 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
373
381
  return this.components.addressManager.getAddresses()
374
382
  }
375
383
 
384
+ getProtocols (): string[] {
385
+ return this.components.registrar.getProtocols()
386
+ }
387
+
376
388
  async hangUp (peer: PeerId | Multiaddr): Promise<void> {
377
389
  const { id } = getPeer(peer)
378
390
 
@@ -50,7 +50,7 @@ export interface RefreshManagerInit {
50
50
  }
51
51
 
52
52
  export interface PeerRoutingInit {
53
- routers: PeerRouting[]
53
+ routers?: PeerRouting[]
54
54
  refreshManager?: RefreshManagerInit
55
55
  }
56
56
 
@@ -69,7 +69,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
69
69
 
70
70
  constructor (components: DefaultPeerRoutingComponents, init: PeerRoutingInit) {
71
71
  this.components = components
72
- this.routers = init.routers
72
+ this.routers = init.routers ?? []
73
73
  this.refreshManagerInit = init.refreshManager ?? {}
74
74
  this.started = false
75
75
 
@@ -2,6 +2,7 @@ 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 { FaultTolerance } from '@libp2p/interface-transport'
5
6
  import type { Listener, Transport, TransportManager, TransportManagerEvents, Upgrader } from '@libp2p/interface-transport'
6
7
  import type { Multiaddr } from '@multiformats/multiaddr'
7
8
  import type { Connection } from '@libp2p/interface-connection'
@@ -270,18 +271,3 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
270
271
  await Promise.all(tasks)
271
272
  }
272
273
  }
273
-
274
- /**
275
- * Enum Transport Manager Fault Tolerance values
276
- */
277
- export enum FaultTolerance {
278
- /**
279
- * should be used for failing in any listen circumstance
280
- */
281
- FATAL_ALL = 0,
282
-
283
- /**
284
- * should be used for not failing when not listening
285
- */
286
- NO_FATAL
287
- }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.41.0-3e53c19'
1
+ export const version = '0.41.0-40fe372'
2
2
  export const name = 'libp2p'