libp2p 1.9.4 → 2.0.0-7cd984569

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 (100) hide show
  1. package/dist/index.min.js +17 -18
  2. package/dist/src/components.d.ts.map +1 -1
  3. package/dist/src/components.js +4 -3
  4. package/dist/src/components.js.map +1 -1
  5. package/dist/src/config.d.ts +1 -1
  6. package/dist/src/config.d.ts.map +1 -1
  7. package/dist/src/config.js +2 -7
  8. package/dist/src/config.js.map +1 -1
  9. package/dist/src/connection/index.d.ts +3 -3
  10. package/dist/src/connection/index.d.ts.map +1 -1
  11. package/dist/src/connection/index.js +7 -7
  12. package/dist/src/connection/index.js.map +1 -1
  13. package/dist/src/connection-manager/connection-pruner.js +1 -1
  14. package/dist/src/connection-manager/constants.browser.d.ts +0 -8
  15. package/dist/src/connection-manager/constants.browser.d.ts.map +1 -1
  16. package/dist/src/connection-manager/constants.browser.js +0 -8
  17. package/dist/src/connection-manager/constants.browser.js.map +1 -1
  18. package/dist/src/connection-manager/constants.d.ts +0 -8
  19. package/dist/src/connection-manager/constants.d.ts.map +1 -1
  20. package/dist/src/connection-manager/constants.defaults.d.ts +12 -22
  21. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  22. package/dist/src/connection-manager/constants.defaults.js +12 -22
  23. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  24. package/dist/src/connection-manager/constants.js +0 -8
  25. package/dist/src/connection-manager/constants.js.map +1 -1
  26. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  27. package/dist/src/connection-manager/dial-queue.js +29 -15
  28. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  29. package/dist/src/connection-manager/index.d.ts +47 -50
  30. package/dist/src/connection-manager/index.d.ts.map +1 -1
  31. package/dist/src/connection-manager/index.js +23 -57
  32. package/dist/src/connection-manager/index.js.map +1 -1
  33. package/dist/src/connection-manager/reconnect-queue.d.ts +35 -0
  34. package/dist/src/connection-manager/reconnect-queue.d.ts.map +1 -0
  35. package/dist/src/connection-manager/reconnect-queue.js +104 -0
  36. package/dist/src/connection-manager/reconnect-queue.js.map +1 -0
  37. package/dist/src/connection-monitor.d.ts +1 -0
  38. package/dist/src/connection-monitor.d.ts.map +1 -1
  39. package/dist/src/connection-monitor.js +15 -5
  40. package/dist/src/connection-monitor.js.map +1 -1
  41. package/dist/src/content-routing.js +6 -6
  42. package/dist/src/content-routing.js.map +1 -1
  43. package/dist/src/errors.d.ts +41 -61
  44. package/dist/src/errors.d.ts.map +1 -1
  45. package/dist/src/errors.js +84 -63
  46. package/dist/src/errors.js.map +1 -1
  47. package/dist/src/get-peer.d.ts.map +1 -1
  48. package/dist/src/get-peer.js +4 -5
  49. package/dist/src/get-peer.js.map +1 -1
  50. package/dist/src/index.d.ts +27 -9
  51. package/dist/src/index.d.ts.map +1 -1
  52. package/dist/src/index.js +10 -3
  53. package/dist/src/index.js.map +1 -1
  54. package/dist/src/libp2p.d.ts +11 -10
  55. package/dist/src/libp2p.d.ts.map +1 -1
  56. package/dist/src/libp2p.js +13 -31
  57. package/dist/src/libp2p.js.map +1 -1
  58. package/dist/src/peer-routing.js +8 -8
  59. package/dist/src/peer-routing.js.map +1 -1
  60. package/dist/src/registrar.js +7 -7
  61. package/dist/src/registrar.js.map +1 -1
  62. package/dist/src/transport-manager.d.ts.map +1 -1
  63. package/dist/src/transport-manager.js +15 -23
  64. package/dist/src/transport-manager.js.map +1 -1
  65. package/dist/src/upgrader.d.ts +8 -8
  66. package/dist/src/upgrader.d.ts.map +1 -1
  67. package/dist/src/upgrader.js +51 -51
  68. package/dist/src/upgrader.js.map +1 -1
  69. package/dist/src/version.d.ts +1 -1
  70. package/dist/src/version.d.ts.map +1 -1
  71. package/dist/src/version.js +1 -1
  72. package/dist/src/version.js.map +1 -1
  73. package/package.json +21 -21
  74. package/src/components.ts +4 -3
  75. package/src/config.ts +4 -10
  76. package/src/connection/index.ts +9 -9
  77. package/src/connection-manager/connection-pruner.ts +1 -1
  78. package/src/connection-manager/constants.browser.ts +0 -10
  79. package/src/connection-manager/constants.defaults.ts +14 -27
  80. package/src/connection-manager/constants.ts +0 -10
  81. package/src/connection-manager/dial-queue.ts +30 -15
  82. package/src/connection-manager/index.ts +80 -118
  83. package/src/connection-manager/reconnect-queue.ts +134 -0
  84. package/src/connection-monitor.ts +15 -6
  85. package/src/content-routing.ts +6 -6
  86. package/src/errors.ts +96 -61
  87. package/src/get-peer.ts +4 -5
  88. package/src/index.ts +42 -12
  89. package/src/libp2p.ts +22 -36
  90. package/src/peer-routing.ts +8 -8
  91. package/src/registrar.ts +7 -7
  92. package/src/transport-manager.ts +15 -23
  93. package/src/upgrader.ts +55 -56
  94. package/src/version.ts +1 -1
  95. package/dist/src/connection-manager/auto-dial.d.ts +0 -47
  96. package/dist/src/connection-manager/auto-dial.d.ts.map +0 -1
  97. package/dist/src/connection-manager/auto-dial.js +0 -223
  98. package/dist/src/connection-manager/auto-dial.js.map +0 -1
  99. package/dist/typedoc-urls.json +0 -14
  100. package/src/connection-manager/auto-dial.ts +0 -285
@@ -1,14 +0,0 @@
1
- {
2
- "Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.Libp2pInit.html",
3
- ".:Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.Libp2pInit.html",
4
- "Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.index.Libp2pOptions.html",
5
- ".:Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.index.Libp2pOptions.html",
6
- "ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.index.ServiceFactoryMap.html",
7
- ".:ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.index.ServiceFactoryMap.html",
8
- "createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.index.createLibp2p.html",
9
- ".:createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.index.createLibp2p.html",
10
- "name": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.name.html",
11
- "./version:name": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.name.html",
12
- "version": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.version.html",
13
- "./version:version": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.version.html"
14
- }
@@ -1,285 +0,0 @@
1
- import { PeerMap, PeerSet } from '@libp2p/peer-collections'
2
- import { PeerQueue } from '@libp2p/utils/peer-queue'
3
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
- import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PEER_RETRY_THRESHOLD, AUTO_DIAL_PRIORITY, LAST_DIAL_FAILURE_KEY, MIN_CONNECTIONS } from './constants.js'
5
- import type { Libp2pEvents, Logger, ComponentLogger, TypedEventTarget, PeerStore, Startable, Metrics } from '@libp2p/interface'
6
- import type { ConnectionManager } from '@libp2p/interface-internal'
7
-
8
- interface AutoDialInit {
9
- minConnections?: number
10
- maxQueueLength?: number
11
- autoDialConcurrency?: number
12
- autoDialPriority?: number
13
- autoDialInterval?: number
14
- autoDialPeerRetryThreshold?: number
15
- autoDialDiscoveredPeersDebounce?: number
16
- }
17
-
18
- interface AutoDialComponents {
19
- connectionManager: ConnectionManager
20
- peerStore: PeerStore
21
- events: TypedEventTarget<Libp2pEvents>
22
- logger: ComponentLogger
23
- metrics?: Metrics
24
- }
25
-
26
- const defaultOptions = {
27
- minConnections: MIN_CONNECTIONS,
28
- maxQueueLength: AUTO_DIAL_MAX_QUEUE_LENGTH,
29
- autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
30
- autoDialPriority: AUTO_DIAL_PRIORITY,
31
- autoDialInterval: AUTO_DIAL_INTERVAL,
32
- autoDialPeerRetryThreshold: AUTO_DIAL_PEER_RETRY_THRESHOLD,
33
- autoDialDiscoveredPeersDebounce: AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE
34
- }
35
-
36
- export class AutoDial implements Startable {
37
- private readonly connectionManager: ConnectionManager
38
- private readonly peerStore: PeerStore
39
- private readonly queue: PeerQueue<void>
40
- private readonly minConnections: number
41
- private readonly autoDialPriority: number
42
- private readonly autoDialIntervalMs: number
43
- private readonly autoDialMaxQueueLength: number
44
- private readonly autoDialPeerRetryThresholdMs: number
45
- private readonly autoDialDiscoveredPeersDebounce: number
46
- private autoDialInterval?: ReturnType<typeof setInterval>
47
- private started: boolean
48
- private running: boolean
49
- private readonly log: Logger
50
-
51
- /**
52
- * Proactively tries to connect to known peers stored in the PeerStore.
53
- * It will keep the number of connections below the upper limit and sort
54
- * the peers to connect based on whether we know their keys and protocols.
55
- */
56
- constructor (components: AutoDialComponents, init: AutoDialInit) {
57
- this.connectionManager = components.connectionManager
58
- this.peerStore = components.peerStore
59
- this.minConnections = init.minConnections ?? defaultOptions.minConnections
60
- this.autoDialPriority = init.autoDialPriority ?? defaultOptions.autoDialPriority
61
- this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval
62
- this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
63
- this.autoDialPeerRetryThresholdMs = init.autoDialPeerRetryThreshold ?? defaultOptions.autoDialPeerRetryThreshold
64
- this.autoDialDiscoveredPeersDebounce = init.autoDialDiscoveredPeersDebounce ?? defaultOptions.autoDialDiscoveredPeersDebounce
65
- this.log = components.logger.forComponent('libp2p:connection-manager:auto-dial')
66
- this.started = false
67
- this.running = false
68
- this.queue = new PeerQueue({
69
- concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency,
70
- metricName: 'libp2p_autodial_queue',
71
- metrics: components.metrics
72
- })
73
- this.queue.addEventListener('error', (evt) => {
74
- this.log.error('error during auto-dial', evt.detail)
75
- })
76
-
77
- // check the min connection limit whenever a peer disconnects
78
- components.events.addEventListener('connection:close', () => {
79
- this.autoDial()
80
- .catch(err => {
81
- this.log.error(err)
82
- })
83
- })
84
-
85
- // sometimes peers are discovered in quick succession so add a small
86
- // debounce to ensure all eligible peers are autodialed
87
- let debounce: ReturnType<typeof setTimeout>
88
-
89
- // when new peers are discovered, dial them if we don't have
90
- // enough connections
91
- components.events.addEventListener('peer:discovery', () => {
92
- clearTimeout(debounce)
93
- debounce = setTimeout(() => {
94
- this.autoDial()
95
- .catch(err => {
96
- this.log.error(err)
97
- })
98
- }, this.autoDialDiscoveredPeersDebounce)
99
- })
100
- }
101
-
102
- isStarted (): boolean {
103
- return this.started
104
- }
105
-
106
- start (): void {
107
- this.started = true
108
- }
109
-
110
- afterStart (): void {
111
- this.autoDial()
112
- .catch(err => {
113
- this.log.error('error while autodialing', err)
114
- })
115
- }
116
-
117
- stop (): void {
118
- // clear the queue
119
- this.queue.clear()
120
- clearTimeout(this.autoDialInterval)
121
- this.started = false
122
- this.running = false
123
- }
124
-
125
- async autoDial (): Promise<void> {
126
- if (!this.started || this.running) {
127
- return
128
- }
129
-
130
- const connections = this.connectionManager.getConnectionsMap()
131
- const numConnections = connections.size
132
-
133
- // already have enough connections
134
- if (numConnections >= this.minConnections) {
135
- if (this.minConnections > 0) {
136
- this.log.trace('have enough connections %d/%d', numConnections, this.minConnections)
137
- }
138
-
139
- // no need to schedule next autodial as it will be run when on
140
- // connection:close event
141
- return
142
- }
143
-
144
- if (this.queue.size > this.autoDialMaxQueueLength) {
145
- this.log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
146
- this.sheduleNextAutodial()
147
- return
148
- }
149
-
150
- this.running = true
151
-
152
- this.log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
153
-
154
- const dialQueue = new PeerSet(
155
- // @ts-expect-error boolean filter removes falsy peer IDs
156
- this.connectionManager.getDialQueue()
157
- .map(queue => queue.peerId)
158
- .filter(Boolean)
159
- )
160
-
161
- // sort peers on whether we know protocols or public keys for them
162
- const peers = await this.peerStore.all({
163
- filters: [
164
- // remove some peers
165
- (peer) => {
166
- // remove peers without addresses
167
- if (peer.addresses.length === 0) {
168
- this.log.trace('not autodialing %p because they have no addresses', peer.id)
169
- return false
170
- }
171
-
172
- // remove peers we are already connected to
173
- if (connections.has(peer.id)) {
174
- this.log.trace('not autodialing %p because they are already connected', peer.id)
175
- return false
176
- }
177
-
178
- // remove peers we are already dialling
179
- if (dialQueue.has(peer.id)) {
180
- this.log.trace('not autodialing %p because they are already being dialed', peer.id)
181
- return false
182
- }
183
-
184
- // remove peers already in the autodial queue
185
- if (this.queue.has(peer.id)) {
186
- this.log.trace('not autodialing %p because they are already being autodialed', peer.id)
187
- return false
188
- }
189
-
190
- return true
191
- }
192
- ]
193
- })
194
-
195
- // shuffle the peers - this is so peers with the same tag values will be
196
- // dialled in a different order each time
197
- const shuffledPeers = peers.sort(() => Math.random() > 0.5 ? 1 : -1)
198
-
199
- // sort shuffled peers by tag value
200
- const peerValues = new PeerMap<number>()
201
- for (const peer of shuffledPeers) {
202
- if (peerValues.has(peer.id)) {
203
- continue
204
- }
205
-
206
- // sum all tag values
207
- peerValues.set(peer.id, [...peer.tags.values()].reduce((acc, curr) => {
208
- return acc + curr.value
209
- }, 0))
210
- }
211
-
212
- // sort by value, highest to lowest
213
- const sortedPeers = shuffledPeers.sort((a, b) => {
214
- const peerAValue = peerValues.get(a.id) ?? 0
215
- const peerBValue = peerValues.get(b.id) ?? 0
216
-
217
- if (peerAValue > peerBValue) {
218
- return -1
219
- }
220
-
221
- if (peerAValue < peerBValue) {
222
- return 1
223
- }
224
-
225
- return 0
226
- })
227
-
228
- const peersThatHaveNotFailed = sortedPeers.filter(peer => {
229
- const lastDialFailure = peer.metadata.get(LAST_DIAL_FAILURE_KEY)
230
-
231
- if (lastDialFailure == null) {
232
- return true
233
- }
234
-
235
- const lastDialFailureTimestamp = parseInt(uint8ArrayToString(lastDialFailure))
236
-
237
- if (isNaN(lastDialFailureTimestamp)) {
238
- return true
239
- }
240
-
241
- // only dial if the time since the last failure is above the retry threshold
242
- return Date.now() - lastDialFailureTimestamp > this.autoDialPeerRetryThresholdMs
243
- })
244
-
245
- this.log('selected %d/%d peers to dial', peersThatHaveNotFailed.length, peers.length)
246
-
247
- for (const peer of peersThatHaveNotFailed) {
248
- this.queue.add(async () => {
249
- const numConnections = this.connectionManager.getConnectionsMap().size
250
-
251
- // Check to see if we still need to auto dial
252
- if (numConnections >= this.minConnections) {
253
- this.log('got enough connections now %d/%d', numConnections, this.minConnections)
254
- this.queue.clear()
255
- return
256
- }
257
-
258
- this.log('connecting to a peerStore stored peer %p', peer.id)
259
- await this.connectionManager.openConnection(peer.id, {
260
- priority: this.autoDialPriority
261
- })
262
- }, {
263
- peerId: peer.id
264
- }).catch(err => {
265
- this.log.error('could not connect to peerStore stored peer', err)
266
- })
267
- }
268
-
269
- this.running = false
270
- this.sheduleNextAutodial()
271
- }
272
-
273
- private sheduleNextAutodial (): void {
274
- if (!this.started) {
275
- return
276
- }
277
-
278
- this.autoDialInterval = setTimeout(() => {
279
- this.autoDial()
280
- .catch(err => {
281
- this.log.error('error while autodialing', err)
282
- })
283
- }, this.autoDialIntervalMs)
284
- }
285
- }