libp2p 0.35.7 → 0.35.8-00e4959

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 (89) hide show
  1. package/dist/index.min.js +55 -0
  2. package/dist/src/circuit/auto-relay.d.ts +8 -10
  3. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  4. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  5. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  6. package/dist/src/connection-manager/index.d.ts +1 -2
  7. package/dist/src/connection-manager/index.d.ts.map +1 -1
  8. package/dist/src/content-routing/utils.d.ts +3 -3
  9. package/dist/src/content-routing/utils.d.ts.map +1 -1
  10. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  11. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  12. package/dist/src/dialer/index.d.ts +5 -5
  13. package/dist/src/dialer/index.d.ts.map +1 -1
  14. package/dist/src/identify/index.d.ts +4 -4
  15. package/dist/src/identify/index.d.ts.map +1 -1
  16. package/dist/src/index.d.ts +6 -6
  17. package/dist/src/index.d.ts.map +1 -1
  18. package/dist/src/metrics/index.d.ts +9 -4
  19. package/dist/src/metrics/index.d.ts.map +1 -1
  20. package/dist/src/metrics/tracked-map.d.ts +6 -1
  21. package/dist/src/metrics/tracked-map.d.ts.map +1 -1
  22. package/dist/src/peer-routing.d.ts +1 -1
  23. package/dist/src/peer-routing.d.ts.map +1 -1
  24. package/dist/src/peer-store/address-book.d.ts +28 -84
  25. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  26. package/dist/src/peer-store/index.d.ts +25 -94
  27. package/dist/src/peer-store/index.d.ts.map +1 -1
  28. package/dist/src/peer-store/key-book.d.ts +31 -15
  29. package/dist/src/peer-store/key-book.d.ts.map +1 -1
  30. package/dist/src/peer-store/metadata-book.d.ts +33 -29
  31. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  32. package/dist/src/peer-store/pb/peer.d.ts +222 -0
  33. package/dist/src/peer-store/proto-book.d.ts +27 -28
  34. package/dist/src/peer-store/proto-book.d.ts.map +1 -1
  35. package/dist/src/peer-store/store.d.ts +73 -0
  36. package/dist/src/peer-store/store.d.ts.map +1 -0
  37. package/dist/src/peer-store/types.d.ts +202 -0
  38. package/dist/src/peer-store/types.d.ts.map +1 -0
  39. package/dist/src/ping/index.d.ts.map +1 -1
  40. package/dist/src/record/peer-record/index.d.ts +2 -2
  41. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  42. package/dist/src/registrar.d.ts +5 -5
  43. package/dist/src/registrar.d.ts.map +1 -1
  44. package/dist/src/upgrader.d.ts.map +1 -1
  45. package/package.json +21 -28
  46. package/src/circuit/auto-relay.js +52 -38
  47. package/src/connection-manager/auto-dialler.js +9 -4
  48. package/src/connection-manager/index.js +42 -22
  49. package/src/content-routing/utils.js +4 -4
  50. package/src/dht/dht-peer-routing.js +2 -6
  51. package/src/dialer/dial-request.js +4 -1
  52. package/src/dialer/index.js +22 -10
  53. package/src/identify/index.js +47 -38
  54. package/src/index.js +33 -27
  55. package/src/metrics/index.js +15 -8
  56. package/src/metrics/tracked-map.js +45 -13
  57. package/src/peer-routing.js +4 -1
  58. package/src/peer-store/address-book.js +242 -227
  59. package/src/peer-store/index.js +77 -110
  60. package/src/peer-store/key-book.js +99 -54
  61. package/src/peer-store/metadata-book.js +171 -101
  62. package/src/peer-store/pb/peer.d.ts +222 -0
  63. package/src/peer-store/pb/peer.js +643 -0
  64. package/src/peer-store/pb/peer.proto +31 -0
  65. package/src/peer-store/proto-book.js +171 -105
  66. package/src/peer-store/store.js +250 -0
  67. package/src/peer-store/types.ts +245 -0
  68. package/src/ping/index.js +6 -0
  69. package/src/record/peer-record/index.js +1 -1
  70. package/src/record/utils.js +1 -1
  71. package/src/registrar.js +4 -4
  72. package/src/upgrader.js +3 -2
  73. package/dist/src/peer-store/book.d.ts +0 -81
  74. package/dist/src/peer-store/book.d.ts.map +0 -1
  75. package/dist/src/peer-store/persistent/consts.d.ts +0 -6
  76. package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
  77. package/dist/src/peer-store/persistent/index.d.ts +0 -129
  78. package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
  79. package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  80. package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  81. package/src/peer-store/book.js +0 -124
  82. package/src/peer-store/persistent/consts.js +0 -15
  83. package/src/peer-store/persistent/index.js +0 -408
  84. package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  85. package/src/peer-store/persistent/pb/address-book.js +0 -522
  86. package/src/peer-store/persistent/pb/address-book.proto +0 -27
  87. package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  88. package/src/peer-store/persistent/pb/proto-book.js +0 -157
  89. package/src/peer-store/persistent/pb/proto-book.proto +0 -5
package/src/index.js CHANGED
@@ -9,7 +9,7 @@ const { EventEmitter } = require('events')
9
9
  const errCode = require('err-code')
10
10
  const PeerId = require('peer-id')
11
11
  const { Multiaddr } = require('multiaddr')
12
-
12
+ const { MemoryDatastore } = require('datastore-core/memory')
13
13
  const PeerRouting = require('./peer-routing')
14
14
  const ContentRouting = require('./content-routing')
15
15
  const getPeer = require('./get-peer')
@@ -28,7 +28,6 @@ const TransportManager = require('./transport-manager')
28
28
  const Upgrader = require('./upgrader')
29
29
  const PeerStore = require('./peer-store')
30
30
  const PubsubAdapter = require('./pubsub-adapter')
31
- const PersistentPeerStore = require('./peer-store/persistent')
32
31
  const Registrar = require('./registrar')
33
32
  const ping = require('./ping')
34
33
  const IdentifyService = require('./identify')
@@ -112,7 +111,7 @@ const { updateSelfPeerRecord } = require('./record/utils')
112
111
  * @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
113
112
  * @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
114
113
  * @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
115
- * @property {PeerStoreOptions & PersistentPeerStoreOptions} [peerStore]
114
+ * @property {PeerStoreOptions} [peerStore]
116
115
  * @property {import('./transport-manager').TransportManagerOptions} [transportManager]
117
116
  * @property {Libp2pConfig} [config]
118
117
  *
@@ -172,13 +171,11 @@ class Libp2p extends EventEmitter {
172
171
  this.metrics = metrics
173
172
  }
174
173
 
175
- this.peerStore = (this.datastore && this._options.peerStore.persistence)
176
- ? new PersistentPeerStore({
177
- peerId: this.peerId,
178
- datastore: this.datastore,
179
- ...this._options.peerStore
180
- })
181
- : new PeerStore({ peerId: this.peerId })
174
+ /** @type {import('./peer-store/types').PeerStore} */
175
+ this.peerStore = new PeerStore({
176
+ peerId: this.peerId,
177
+ datastore: (this.datastore && this._options.peerStore.persistence) ? this.datastore : new MemoryDatastore()
178
+ })
182
179
 
183
180
  // Addresses {listen, announce, noAnnounce}
184
181
  this.addresses = this._options.addresses
@@ -290,7 +287,6 @@ class Libp2p extends EventEmitter {
290
287
 
291
288
  // Add the identify service since we can multiplex
292
289
  this.identifyService = new IdentifyService({ libp2p: this })
293
- this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
294
290
  }
295
291
 
296
292
  // Attach private network protector
@@ -356,6 +352,10 @@ class Libp2p extends EventEmitter {
356
352
  async start () {
357
353
  log('libp2p is starting')
358
354
 
355
+ if (this.identifyService) {
356
+ await this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
357
+ }
358
+
359
359
  try {
360
360
  await this._onStarting()
361
361
  await this._onDidStart()
@@ -380,9 +380,13 @@ class Libp2p extends EventEmitter {
380
380
  try {
381
381
  this._isStarted = false
382
382
 
383
+ if (this.identifyService) {
384
+ await this.identifyService.stop()
385
+ }
386
+
383
387
  this.relay && this.relay.stop()
384
388
  this.peerRouting.stop()
385
- this._autodialler.stop()
389
+ await this._autodialler.stop()
386
390
  await (this._dht && this._dht.stop())
387
391
 
388
392
  for (const service of this._discovery.values()) {
@@ -393,7 +397,6 @@ class Libp2p extends EventEmitter {
393
397
 
394
398
  this._discovery = new Map()
395
399
 
396
- await this.peerStore.stop()
397
400
  await this.connectionManager.stop()
398
401
 
399
402
  await Promise.all([
@@ -499,7 +502,7 @@ class Libp2p extends EventEmitter {
499
502
  if (!connection) {
500
503
  connection = await this.dialer.connectToPeer(peer, options)
501
504
  } else if (multiaddrs) {
502
- this.peerStore.addressBook.add(id, multiaddrs)
505
+ await this.peerStore.addressBook.add(id, multiaddrs)
503
506
  }
504
507
 
505
508
  return connection
@@ -579,14 +582,14 @@ class Libp2p extends EventEmitter {
579
582
  * @param {string[]|string} protocols
580
583
  * @param {(props: HandlerProps) => void} handler
581
584
  */
582
- handle (protocols, handler) {
585
+ async handle (protocols, handler) {
583
586
  protocols = Array.isArray(protocols) ? protocols : [protocols]
584
587
  protocols.forEach(protocol => {
585
588
  this.upgrader.protocols.set(protocol, handler)
586
589
  })
587
590
 
588
591
  // Add new protocols to self protocols in the Protobook
589
- this.peerStore.protoBook.add(this.peerId, protocols)
592
+ await this.peerStore.protoBook.add(this.peerId, protocols)
590
593
  }
591
594
 
592
595
  /**
@@ -595,14 +598,14 @@ class Libp2p extends EventEmitter {
595
598
  *
596
599
  * @param {string[]|string} protocols
597
600
  */
598
- unhandle (protocols) {
601
+ async unhandle (protocols) {
599
602
  protocols = Array.isArray(protocols) ? protocols : [protocols]
600
603
  protocols.forEach(protocol => {
601
604
  this.upgrader.protocols.delete(protocol)
602
605
  })
603
606
 
604
607
  // Remove protocols from self protocols in the Protobook
605
- this.peerStore.protoBook.remove(this.peerId, protocols)
608
+ await this.peerStore.protoBook.remove(this.peerId, protocols)
606
609
  }
607
610
 
608
611
  async _onStarting () {
@@ -613,11 +616,8 @@ class Libp2p extends EventEmitter {
613
616
  // Manage your NATs
614
617
  this.natManager.start()
615
618
 
616
- // Start PeerStore
617
- await this.peerStore.start()
618
-
619
619
  if (this._config.pubsub.enabled) {
620
- this.pubsub && this.pubsub.start()
620
+ this.pubsub && await this.pubsub.start()
621
621
  }
622
622
 
623
623
  // DHT subsystem
@@ -631,6 +631,10 @@ class Libp2p extends EventEmitter {
631
631
 
632
632
  // Start metrics if present
633
633
  this.metrics && this.metrics.start()
634
+
635
+ if (this.identifyService) {
636
+ await this.identifyService.start()
637
+ }
634
638
  }
635
639
 
636
640
  /**
@@ -643,17 +647,19 @@ class Libp2p extends EventEmitter {
643
647
 
644
648
  this.peerStore.on('peer', peerId => {
645
649
  this.emit('peer:discovery', peerId)
646
- this._maybeConnect(peerId)
650
+ this._maybeConnect(peerId).catch(err => {
651
+ log.error(err)
652
+ })
647
653
  })
648
654
 
649
655
  // Once we start, emit any peers we may have already discovered
650
656
  // TODO: this should be removed, as we already discovered these peers in the past
651
- for (const peer of this.peerStore.peers.values()) {
657
+ for await (const peer of this.peerStore.getPeers()) {
652
658
  this.emit('peer:discovery', peer.id)
653
659
  }
654
660
 
655
661
  this.connectionManager.start()
656
- this._autodialler.start()
662
+ await this._autodialler.start()
657
663
 
658
664
  // Peer discovery
659
665
  await this._setupPeerDiscovery()
@@ -677,8 +683,8 @@ class Libp2p extends EventEmitter {
677
683
  return
678
684
  }
679
685
 
680
- peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs)
681
- peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols)
686
+ peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
687
+ peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
682
688
  }
683
689
 
684
690
  /**
@@ -44,7 +44,7 @@ class Metrics {
44
44
  this._oldPeers = oldPeerLRU(this._options.maxOldPeersRetention)
45
45
  this._running = false
46
46
  this._onMessage = this._onMessage.bind(this)
47
- this._componentMetrics = new Map()
47
+ this._systems = new Map()
48
48
  }
49
49
 
50
50
  /**
@@ -89,19 +89,26 @@ class Metrics {
89
89
  }
90
90
 
91
91
  /**
92
- * @returns {Map}
92
+ * @returns {Map<string, Map<string, Map<string, any>>>}
93
93
  */
94
94
  getComponentMetrics () {
95
- return this._componentMetrics
95
+ return this._systems
96
96
  }
97
97
 
98
- updateComponentMetric (component, metric, value) {
99
- if (!this._componentMetrics.has(component)) {
100
- this._componentMetrics.set(component, new Map())
98
+ updateComponentMetric ({ system = 'libp2p', component, metric, value }) {
99
+ if (!this._systems.has(system)) {
100
+ this._systems.set(system, new Map())
101
101
  }
102
102
 
103
- const map = this._componentMetrics.get(component)
104
- map.set(metric, value)
103
+ const systemMetrics = this._systems.get(system)
104
+
105
+ if (!systemMetrics.has(component)) {
106
+ systemMetrics.set(component, new Map())
107
+ }
108
+
109
+ const componentMetrics = systemMetrics.get(component)
110
+
111
+ componentMetrics.set(metric, value)
105
112
  }
106
113
 
107
114
  /**
@@ -6,18 +6,27 @@
6
6
  */
7
7
  class TrackedMap extends Map {
8
8
  /**
9
- * @param {string} component
10
- * @param {string} name
11
- * @param {import('.')} metrics
9
+ * @param {object} options
10
+ * @param {string} options.system
11
+ * @param {string} options.component
12
+ * @param {string} options.metric
13
+ * @param {import('.')} options.metrics
12
14
  */
13
- constructor (component, name, metrics) {
15
+ constructor (options) {
14
16
  super()
15
17
 
18
+ const { system, component, metric, metrics } = options
19
+ this._system = system
16
20
  this._component = component
17
- this._name = name
21
+ this._metric = metric
18
22
  this._metrics = metrics
19
23
 
20
- this._metrics.updateComponentMetric(this._component, this._name, this.size)
24
+ this._metrics.updateComponentMetric({
25
+ system: this._system,
26
+ component: this._component,
27
+ metric: this._metric,
28
+ value: this.size
29
+ })
21
30
  }
22
31
 
23
32
  /**
@@ -26,7 +35,12 @@ class TrackedMap extends Map {
26
35
  */
27
36
  set (key, value) {
28
37
  super.set(key, value)
29
- this._metrics.updateComponentMetric(this._component, this._name, this.size)
38
+ this._metrics.updateComponentMetric({
39
+ system: this._system,
40
+ component: this._component,
41
+ metric: this._metric,
42
+ value: this.size
43
+ })
30
44
  return this
31
45
  }
32
46
 
@@ -35,25 +49,43 @@ class TrackedMap extends Map {
35
49
  */
36
50
  delete (key) {
37
51
  const deleted = super.delete(key)
38
- this._metrics.updateComponentMetric(this._component, this._name, this.size)
52
+ this._metrics.updateComponentMetric({
53
+ system: this._system,
54
+ component: this._component,
55
+ metric: this._metric,
56
+ value: this.size
57
+ })
39
58
  return deleted
40
59
  }
60
+
61
+ clear () {
62
+ super.clear()
63
+
64
+ this._metrics.updateComponentMetric({
65
+ system: this._system,
66
+ component: this._component,
67
+ metric: this._metric,
68
+ value: this.size
69
+ })
70
+ }
41
71
  }
42
72
 
43
73
  /**
44
74
  * @template K
45
75
  * @template V
46
- * @param {string} component
47
- * @param {string} name
48
- * @param {import('.')} [metrics]
76
+ * @param {object} options
77
+ * @param {string} [options.system]
78
+ * @param {string} options.component
79
+ * @param {string} options.metric
80
+ * @param {import('.')} [options.metrics]
49
81
  * @returns {Map<K, V>}
50
82
  */
51
- module.exports = (component, name, metrics) => {
83
+ module.exports = ({ system = 'libp2p', component, metric, metrics }) => {
52
84
  /** @type {Map<K, V>} */
53
85
  let map
54
86
 
55
87
  if (metrics) {
56
- map = new TrackedMap(component, name, metrics)
88
+ map = new TrackedMap({ system, component, metric, metrics })
57
89
  } else {
58
90
  map = new Map()
59
91
  }
@@ -154,7 +154,10 @@ class PeerRouting {
154
154
  const controller = new TimeoutController(options.timeout)
155
155
  // this controller will potentially be used while dialing lots of
156
156
  // peers so prevent MaxListenersExceededWarning appearing in the console
157
- setMaxListeners && setMaxListeners(Infinity, controller.signal)
157
+ try {
158
+ // fails on node < 15.4
159
+ setMaxListeners && setMaxListeners(Infinity, controller.signal)
160
+ } catch {}
158
161
 
159
162
  options.signal = controller.signal
160
163
  }