libp2p 0.41.0-3e53c19 → 0.41.0-3e8501d

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2p",
3
- "version": "0.41.0-3e53c19",
3
+ "version": "0.41.0-3e8501d",
4
4
  "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p#readme",
@@ -59,10 +59,6 @@
59
59
  "./pnet": {
60
60
  "types": "./dist/src/pnet/index.d.ts",
61
61
  "import": "./dist/src/pnet/index.js"
62
- },
63
- "./transport-manager": {
64
- "types": "./dist/src/transport-manager.d.ts",
65
- "import": "./dist/src/transport-manager.js"
66
62
  }
67
63
  },
68
64
  "eslintConfig": {
@@ -104,27 +100,27 @@
104
100
  "@libp2p/interface-connection": "^3.0.2",
105
101
  "@libp2p/interface-connection-encrypter": "^3.0.1",
106
102
  "@libp2p/interface-connection-manager": "^1.1.1",
107
- "@libp2p/interface-content-routing": "^1.0.2",
108
- "@libp2p/interface-dht": "^1.0.1",
103
+ "@libp2p/interface-content-routing": "^2.0.0",
104
+ "@libp2p/interface-dht": "^2.0.0",
109
105
  "@libp2p/interface-libp2p": "^1.0.0",
110
106
  "@libp2p/interface-metrics": "^4.0.0",
111
107
  "@libp2p/interface-peer-discovery": "^1.0.1",
112
- "@libp2p/interface-peer-id": "^1.0.4",
108
+ "@libp2p/interface-peer-id": "^2.0.0",
113
109
  "@libp2p/interface-peer-info": "^1.0.3",
114
110
  "@libp2p/interface-peer-routing": "^1.0.1",
115
111
  "@libp2p/interface-peer-store": "^1.2.2",
116
112
  "@libp2p/interface-pubsub": "^3.0.0",
117
113
  "@libp2p/interface-registrar": "^2.0.3",
118
114
  "@libp2p/interface-stream-muxer": "^3.0.0",
119
- "@libp2p/interface-transport": "^2.0.0",
115
+ "@libp2p/interface-transport": "^2.1.0",
120
116
  "@libp2p/interfaces": "^3.0.3",
121
117
  "@libp2p/logger": "^2.0.1",
122
118
  "@libp2p/multistream-select": "^3.0.0",
123
- "@libp2p/peer-collections": "^2.0.0",
124
- "@libp2p/peer-id": "^1.1.15",
125
- "@libp2p/peer-id-factory": "^1.0.18",
126
- "@libp2p/peer-record": "^4.0.3",
127
- "@libp2p/peer-store": "^5.0.0",
119
+ "@libp2p/peer-collections": "^3.0.0",
120
+ "@libp2p/peer-id": "^2.0.0",
121
+ "@libp2p/peer-id-factory": "^2.0.0",
122
+ "@libp2p/peer-record": "^5.0.0",
123
+ "@libp2p/peer-store": "^6.0.0",
128
124
  "@libp2p/tracked-map": "^3.0.0",
129
125
  "@libp2p/utils": "^3.0.2",
130
126
  "@multiformats/mafmt": "^11.0.2",
@@ -150,7 +146,7 @@
150
146
  "it-sort": "^2.0.0",
151
147
  "it-stream-types": "^1.0.4",
152
148
  "merge-options": "^3.0.4",
153
- "multiformats": "^10.0.0",
149
+ "multiformats": "^11.0.0",
154
150
  "node-forge": "^1.3.1",
155
151
  "p-fifo": "^1.0.0",
156
152
  "p-retry": "^5.0.0",
@@ -168,24 +164,24 @@
168
164
  "xsalsa20": "^1.1.0"
169
165
  },
170
166
  "devDependencies": {
171
- "@chainsafe/libp2p-noise": "^10.0.1",
167
+ "@chainsafe/libp2p-noise": "^11.0.0",
172
168
  "@chainsafe/libp2p-yamux": "^3.0.3",
173
- "@libp2p/bootstrap": "^5.0.0",
174
- "@libp2p/daemon-client": "^3.0.5",
175
- "@libp2p/daemon-server": "^3.0.4",
176
- "@libp2p/floodsub": "^5.0.0",
169
+ "@libp2p/bootstrap": "^6.0.0",
170
+ "@libp2p/daemon-client": "^4.0.1",
171
+ "@libp2p/daemon-server": "^4.0.1",
172
+ "@libp2p/floodsub": "^6.0.0",
177
173
  "@libp2p/interface-compliance-tests": "^3.0.2",
178
174
  "@libp2p/interface-connection-compliance-tests": "^2.0.3",
179
- "@libp2p/interface-connection-encrypter-compliance-tests": "^3.0.0",
180
- "@libp2p/interface-mocks": "^8.0.0",
181
- "@libp2p/interop": "^3.0.1",
182
- "@libp2p/kad-dht": "^6.0.0",
183
- "@libp2p/mdns": "^5.0.0",
175
+ "@libp2p/interface-connection-encrypter-compliance-tests": "^4.0.0",
176
+ "@libp2p/interface-mocks": "^9.0.0",
177
+ "@libp2p/interop": "^4.0.0",
178
+ "@libp2p/kad-dht": "^7.0.0",
179
+ "@libp2p/mdns": "^6.0.0",
184
180
  "@libp2p/mplex": "^7.0.0",
185
- "@libp2p/pubsub": "^5.0.0",
181
+ "@libp2p/pubsub": "^6.0.0",
186
182
  "@libp2p/tcp": "^6.0.0",
187
- "@libp2p/topology": "^3.0.1",
188
- "@libp2p/webrtc-star": "^5.0.2",
183
+ "@libp2p/topology": "^4.0.0",
184
+ "@libp2p/webrtc-star": "^6.0.0",
189
185
  "@libp2p/websockets": "^5.0.0",
190
186
  "@types/node-forge": "^1.0.0",
191
187
  "@types/p-fifo": "^1.0.0",
package/src/config.ts CHANGED
@@ -4,7 +4,7 @@ import * as Constants from './constants.js'
4
4
  import { AGENT_VERSION } from './identify/consts.js'
5
5
  import * as RelayConstants from './circuit/constants.js'
6
6
  import { publicAddressesFirst } from '@libp2p/utils/address-sort'
7
- import { FaultTolerance } from './transport-manager.js'
7
+ import { FaultTolerance } from '@libp2p/interface-transport'
8
8
  import type { Multiaddr } from '@multiformats/multiaddr'
9
9
  import type { Libp2pInit } from './index.js'
10
10
  import { codes, messages } from './errors.js'
@@ -31,15 +31,7 @@ const DefaultConfig: Partial<Libp2pInit> = {
31
31
  resolvers: {
32
32
  dnsaddr: dnsaddrResolver
33
33
  },
34
- addressSorter: publicAddressesFirst,
35
- maxEventLoopDelay: Infinity,
36
- pollInterval: Infinity,
37
- maxAddrsToDial: Infinity,
38
- startupReconnectTimeout: Infinity,
39
- allow: [],
40
- deny: [],
41
- maxIncomingPendingConnections: Infinity,
42
- inboundConnectionThreshold: Infinity
34
+ addressSorter: publicAddressesFirst
43
35
  },
44
36
  connectionGater: {},
45
37
  transportManager: {
@@ -50,8 +42,7 @@ const DefaultConfig: Partial<Libp2pInit> = {
50
42
  enabled: true,
51
43
  interval: 6e5,
52
44
  bootDelay: 10e3
53
- },
54
- routers: []
45
+ }
55
46
  },
56
47
  nat: {
57
48
  enabled: true,
@@ -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
@@ -32,7 +32,7 @@ import type { Connection } from '@libp2p/interface-connection'
32
32
  import type { PeerRouting } from '@libp2p/interface-peer-routing'
33
33
  import type { ContentRouting } from '@libp2p/interface-content-routing'
34
34
  import type { PubSub } from '@libp2p/interface-pubsub'
35
- import type { Registrar, StreamHandler, StreamHandlerOptions } from '@libp2p/interface-registrar'
35
+ import type { Registrar, StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
36
36
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
37
37
  import type { PeerInfo } from '@libp2p/interface-peer-info'
38
38
  import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
@@ -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
 
@@ -462,6 +474,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
462
474
  )
463
475
  }
464
476
 
477
+ async register (protocol: string, topology: Topology): Promise<string> {
478
+ return await this.registrar.register(protocol, topology)
479
+ }
480
+
481
+ unregister (id: string) {
482
+ this.registrar.unregister(id)
483
+ }
484
+
465
485
  /**
466
486
  * Called whenever peer discovery services emit `peer` events.
467
487
  * Known peers may be emitted.
@@ -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-3e8501d'
2
2
  export const name = 'libp2p'