libp2p 0.45.5 → 0.45.6-de61ddb4

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 (44) hide show
  1. package/dist/index.min.js +21 -21
  2. package/dist/src/autonat/index.js +4 -4
  3. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  4. package/dist/src/circuit-relay/transport/discovery.js +11 -4
  5. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  6. package/dist/src/circuit-relay/transport/reservation-store.d.ts +5 -0
  7. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  8. package/dist/src/circuit-relay/transport/reservation-store.js +14 -2
  9. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  10. package/dist/src/connection-manager/auto-dial.d.ts +3 -0
  11. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  12. package/dist/src/connection-manager/auto-dial.js +64 -27
  13. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  14. package/dist/src/connection-manager/constants.d.ts +4 -0
  15. package/dist/src/connection-manager/constants.d.ts.map +1 -1
  16. package/dist/src/connection-manager/constants.js +4 -0
  17. package/dist/src/connection-manager/constants.js.map +1 -1
  18. package/dist/src/connection-manager/index.d.ts +5 -0
  19. package/dist/src/connection-manager/index.d.ts.map +1 -1
  20. package/dist/src/connection-manager/index.js +11 -10
  21. package/dist/src/connection-manager/index.js.map +1 -1
  22. package/dist/src/upnp-nat/index.d.ts +1 -1
  23. package/dist/src/upnp-nat/index.d.ts.map +1 -1
  24. package/dist/src/upnp-nat/index.js +3 -3
  25. package/dist/src/upnp-nat/index.js.map +1 -1
  26. package/dist/src/utils/peer-job-queue.d.ts +33 -0
  27. package/dist/src/utils/peer-job-queue.d.ts.map +1 -0
  28. package/dist/src/utils/peer-job-queue.js +82 -0
  29. package/dist/src/utils/peer-job-queue.js.map +1 -0
  30. package/dist/src/version.d.ts +1 -1
  31. package/dist/src/version.d.ts.map +1 -1
  32. package/dist/src/version.js +1 -1
  33. package/dist/src/version.js.map +1 -1
  34. package/package.json +10 -8
  35. package/src/autonat/index.ts +4 -4
  36. package/src/circuit-relay/transport/discovery.ts +11 -4
  37. package/src/circuit-relay/transport/reservation-store.ts +22 -3
  38. package/src/connection-manager/auto-dial.ts +75 -31
  39. package/src/connection-manager/constants.ts +5 -0
  40. package/src/connection-manager/index.ts +18 -12
  41. package/src/upnp-nat/index.ts +3 -3
  42. package/src/utils/peer-job-queue.ts +119 -0
  43. package/src/version.ts +1 -1
  44. package/dist/typedoc-urls.json +0 -58
@@ -10,7 +10,7 @@ import { codes } from '../errors.js'
10
10
  import { getPeerAddress } from '../get-peer.js'
11
11
  import { AutoDial } from './auto-dial.js'
12
12
  import { ConnectionPruner } from './connection-pruner.js'
13
- import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
13
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
14
14
  import { DialQueue } from './dial-queue.js'
15
15
  import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
16
16
  import type { ConnectionGater } from '@libp2p/interface-connection-gater'
@@ -18,7 +18,7 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
18
18
  import type { PendingDial, AddressSorter, Libp2pEvents } from '@libp2p/interface-libp2p'
19
19
  import type { Metrics } from '@libp2p/interface-metrics'
20
20
  import type { PeerId } from '@libp2p/interface-peer-id'
21
- import type { PeerStore } from '@libp2p/interface-peer-store'
21
+ import type { Peer, PeerStore } from '@libp2p/interface-peer-store'
22
22
  import type { TransportManager } from '@libp2p/interface-transport'
23
23
  import type { AbortOptions } from '@libp2p/interfaces'
24
24
  import type { EventEmitter } from '@libp2p/interfaces/events'
@@ -61,6 +61,12 @@ export interface ConnectionManagerInit {
61
61
  */
62
62
  autoDialPriority?: number
63
63
 
64
+ /**
65
+ * Limit the maximum number of peers to dial when trying to keep the number of
66
+ * open connections above `minConnections`. (default: 100)
67
+ */
68
+ autoDialMaxQueueLength?: number
69
+
64
70
  /**
65
71
  * Sort the known addresses of a peer before trying to dial, By default public
66
72
  * addresses will be dialled before private (e.g. loopback or LAN) addresses.
@@ -136,7 +142,8 @@ const defaultOptions = {
136
142
  inboundConnectionThreshold: INBOUND_CONNECTION_THRESHOLD,
137
143
  maxIncomingPendingConnections: MAX_INCOMING_PENDING_CONNECTIONS,
138
144
  autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
139
- autoDialPriority: AUTO_DIAL_PRIORITY
145
+ autoDialPriority: AUTO_DIAL_PRIORITY,
146
+ autoDialMaxQueueLength: AUTO_DIAL_MAX_QUEUE_LENGTH
140
147
  }
141
148
 
142
149
  export interface DefaultConnectionManagerComponents {
@@ -217,7 +224,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
217
224
  }, {
218
225
  minConnections,
219
226
  autoDialConcurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency,
220
- autoDialPriority: init.autoDialPriority ?? defaultOptions.autoDialPriority
227
+ autoDialPriority: init.autoDialPriority ?? defaultOptions.autoDialPriority,
228
+ maxQueueLength: init.autoDialMaxQueueLength ?? defaultOptions.autoDialMaxQueueLength
221
229
  })
222
230
 
223
231
  // controls what happens when we have too many connections
@@ -344,17 +352,15 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
344
352
  // re-connect to any peers with the KEEP_ALIVE tag
345
353
  void Promise.resolve()
346
354
  .then(async () => {
347
- const keepAlivePeers: PeerId[] = []
348
-
349
- for (const peer of await this.peerStore.all()) {
350
- if (peer.tags.has(KEEP_ALIVE)) {
351
- keepAlivePeers.push(peer.id)
352
- }
353
- }
355
+ const keepAlivePeers: Peer[] = await this.peerStore.all({
356
+ filters: [(peer) => {
357
+ return peer.tags.has(KEEP_ALIVE)
358
+ }]
359
+ })
354
360
 
355
361
  await Promise.all(
356
362
  keepAlivePeers.map(async peer => {
357
- await this.openConnection(peer)
363
+ await this.openConnection(peer.id)
358
364
  .catch(err => {
359
365
  log.error(err)
360
366
  })
@@ -142,7 +142,7 @@ class UPnPNAT implements Startable {
142
142
  continue
143
143
  }
144
144
 
145
- const client = await this._getClient()
145
+ const client = this._getClient()
146
146
  const publicIp = this.externalAddress ?? await client.externalIp()
147
147
  const isPrivate = isPrivateIp(publicIp)
148
148
 
@@ -173,12 +173,12 @@ class UPnPNAT implements Startable {
173
173
  }
174
174
  }
175
175
 
176
- async _getClient (): Promise<NatAPI> {
176
+ _getClient (): NatAPI {
177
177
  if (this.client != null) {
178
178
  return this.client
179
179
  }
180
180
 
181
- this.client = await upnpNat({
181
+ this.client = upnpNat({
182
182
  description: this.description,
183
183
  ttl: this.ttl,
184
184
  keepAlive: this.keepAlive,
@@ -0,0 +1,119 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+
3
+ import { CodeError } from '@libp2p/interfaces/errors'
4
+ import PQueue from 'p-queue'
5
+ import { codes } from '../errors.js'
6
+ import type { PeerId } from '@libp2p/interface-peer-id'
7
+ import type { QueueAddOptions, Options, Queue } from 'p-queue'
8
+
9
+ // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
10
+ // Used to compute insertion index to keep queue sorted after insertion
11
+ function lowerBound<T> (array: readonly T[], value: T, comparator: (a: T, b: T) => number): number {
12
+ let first = 0
13
+ let count = array.length
14
+
15
+ while (count > 0) {
16
+ const step = Math.trunc(count / 2)
17
+ let it = first + step
18
+
19
+ if (comparator(array[it]!, value) <= 0) {
20
+ first = ++it
21
+ count -= step + 1
22
+ } else {
23
+ count = step
24
+ }
25
+ }
26
+
27
+ return first
28
+ }
29
+
30
+ interface RunFunction { (): Promise<unknown> }
31
+
32
+ export interface PeerPriorityQueueOptions extends QueueAddOptions {
33
+ peerId: PeerId
34
+ }
35
+
36
+ interface PeerJob {
37
+ priority: number
38
+ peerId: PeerId
39
+ run: RunFunction
40
+ }
41
+
42
+ /**
43
+ * Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
44
+ * that adds support for filtering jobs by peer id
45
+ */
46
+ class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
47
+ readonly #queue: PeerJob[] = []
48
+
49
+ enqueue (run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void {
50
+ const peerId = options?.peerId
51
+ const priority = options?.priority ?? 0
52
+
53
+ if (peerId == null) {
54
+ throw new CodeError('missing peer id', codes.ERR_INVALID_PARAMETERS)
55
+ }
56
+
57
+ const element: PeerJob = {
58
+ priority,
59
+ peerId,
60
+ run
61
+ }
62
+
63
+ if (this.size > 0 && this.#queue[this.size - 1]!.priority >= priority) {
64
+ this.#queue.push(element)
65
+ return
66
+ }
67
+
68
+ const index = lowerBound(
69
+ this.#queue, element,
70
+ (a: Readonly<PeerPriorityQueueOptions>, b: Readonly<PeerPriorityQueueOptions>) => b.priority! - a.priority!
71
+ )
72
+ this.#queue.splice(index, 0, element)
73
+ }
74
+
75
+ dequeue (): RunFunction | undefined {
76
+ const item = this.#queue.shift()
77
+ return item?.run
78
+ }
79
+
80
+ filter (options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[] {
81
+ if (options.peerId != null) {
82
+ const peerId = options.peerId
83
+
84
+ return this.#queue.filter(
85
+ (element: Readonly<PeerPriorityQueueOptions>) => peerId.equals(element.peerId)
86
+ ).map((element: Readonly<{ run: RunFunction }>) => element.run)
87
+ }
88
+
89
+ return this.#queue.filter(
90
+ (element: Readonly<PeerPriorityQueueOptions>) => element.priority === options.priority
91
+ ).map((element: Readonly<{ run: RunFunction }>) => element.run)
92
+ }
93
+
94
+ get size (): number {
95
+ return this.#queue.length
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Extends PQueue to add support for querying queued jobs by peer id
101
+ */
102
+ export class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
103
+ constructor (options: Options<PeerPriorityQueue, PeerPriorityQueueOptions> = {}) {
104
+ super({
105
+ ...options,
106
+ queueClass: PeerPriorityQueue
107
+ })
108
+ }
109
+
110
+ /**
111
+ * Returns true if this queue has a job for the passed peer id that has not yet
112
+ * started to run
113
+ */
114
+ hasJob (peerId: PeerId): boolean {
115
+ return this.sizeBy({
116
+ peerId
117
+ }) > 0
118
+ }
119
+ }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.45.5'
1
+ export const version = '0.45.6-de61ddb4'
2
2
  export const name = 'libp2p'
@@ -1,58 +0,0 @@
1
- {
2
- "AutoNATComponents": "https://libp2p.github.io/js-libp2p/interfaces/autonat.AutoNATComponents.html",
3
- "AutoNATServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/autonat.AutoNATServiceInit.html",
4
- "autoNATService": "https://libp2p.github.io/js-libp2p/functions/autonat.autoNATService.html",
5
- "codec": "https://libp2p.github.io/js-libp2p/functions/circuit_relay._internal_.Limit.codec.html",
6
- "decode": "https://libp2p.github.io/js-libp2p/functions/circuit_relay._internal_.Limit.decode.html",
7
- "encode": "https://libp2p.github.io/js-libp2p/functions/circuit_relay._internal_.Limit.encode.html",
8
- "AdvertServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.AdvertServiceComponents.html",
9
- "AdvertServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.AdvertServiceInit.html",
10
- "CircuitRelayServerComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayServerComponents.html",
11
- "CircuitRelayServerInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayServerInit.html",
12
- "CircuitRelayTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayTransportComponents.html",
13
- "CircuitRelayTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.CircuitRelayTransportInit.html",
14
- "Limit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.Limit-1.html",
15
- "RelayDiscoveryComponents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.RelayDiscoveryComponents.html",
16
- "RelayStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.RelayStoreInit.html",
17
- "ReservationStoreInit": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay._internal_.ReservationStoreInit.html",
18
- "CircuitRelayService": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay.CircuitRelayService.html",
19
- "CircuitRelayServiceEvents": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay.CircuitRelayServiceEvents.html",
20
- "RelayReservation": "https://libp2p.github.io/js-libp2p/interfaces/circuit_relay.RelayReservation.html",
21
- "circuitRelayServer": "https://libp2p.github.io/js-libp2p/functions/circuit_relay.circuitRelayServer.html",
22
- "circuitRelayTransport": "https://libp2p.github.io/js-libp2p/functions/circuit_relay.circuitRelayTransport.html",
23
- "FetchService": "https://libp2p.github.io/js-libp2p/interfaces/fetch.FetchService.html",
24
- "FetchServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/fetch.FetchServiceComponents.html",
25
- "FetchServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/fetch.FetchServiceInit.html",
26
- "HandleMessageOptions": "https://libp2p.github.io/js-libp2p/interfaces/fetch.HandleMessageOptions.html",
27
- "LookupFunction": "https://libp2p.github.io/js-libp2p/interfaces/fetch.LookupFunction.html",
28
- "fetchService": "https://libp2p.github.io/js-libp2p/functions/fetch.fetchService-1.html",
29
- "DefaultIdentifyService": "https://libp2p.github.io/js-libp2p/classes/identify._internal_.DefaultIdentifyService.html",
30
- "Identify": "https://libp2p.github.io/js-libp2p/interfaces/identify._internal_.Identify-1.html",
31
- "IdentifyServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/identify.IdentifyServiceComponents.html",
32
- "IdentifyServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/identify.IdentifyServiceInit.html",
33
- "Message": "https://libp2p.github.io/js-libp2p/variables/identify.Message.html",
34
- "multicodecs": "https://libp2p.github.io/js-libp2p/variables/identify.multicodecs.html",
35
- "identifyService": "https://libp2p.github.io/js-libp2p/functions/identify.identifyService.html",
36
- "AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.AddressFilter.html",
37
- "AddressManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.AddressManagerInit.html",
38
- "Components": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.Components.html",
39
- "ConnectionManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerInit.html",
40
- "TransportManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/index._internal_.TransportManagerInit.html",
41
- "Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/index.Libp2pInit.html",
42
- "Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/index.Libp2pOptions.html",
43
- "ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/index.ServiceFactoryMap.html",
44
- "createLibp2p": "https://libp2p.github.io/js-libp2p/functions/index.createLibp2p.html",
45
- "plaintext": "https://libp2p.github.io/js-libp2p/functions/insecure.plaintext.html",
46
- "PingService": "https://libp2p.github.io/js-libp2p/interfaces/ping.PingService.html",
47
- "PingServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/ping.PingServiceComponents.html",
48
- "PingServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/ping.PingServiceInit.html",
49
- "pingService": "https://libp2p.github.io/js-libp2p/functions/ping.pingService-1.html",
50
- "ProtectorInit": "https://libp2p.github.io/js-libp2p/interfaces/pnet.ProtectorInit.html",
51
- "generateKey": "https://libp2p.github.io/js-libp2p/functions/pnet.generateKey.html",
52
- "preSharedKey": "https://libp2p.github.io/js-libp2p/functions/pnet.preSharedKey.html",
53
- "UPnPNAT": "https://libp2p.github.io/js-libp2p/classes/upnp_nat._internal_.UPnPNAT.html",
54
- "PMPOptions": "https://libp2p.github.io/js-libp2p/interfaces/upnp_nat.PMPOptions.html",
55
- "UPnPNATComponents": "https://libp2p.github.io/js-libp2p/interfaces/upnp_nat.UPnPNATComponents.html",
56
- "UPnPNATInit": "https://libp2p.github.io/js-libp2p/interfaces/upnp_nat.UPnPNATInit.html",
57
- "uPnPNATService": "https://libp2p.github.io/js-libp2p/functions/upnp_nat.uPnPNATService.html"
58
- }