libp2p 1.3.2 → 1.3.3-b17824a1d

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.
@@ -5,6 +5,7 @@ import { defaultAddressSort } from '@libp2p/utils/address-sort'
5
5
  import { Queue, type QueueAddOptions } from '@libp2p/utils/queue'
6
6
  import { type Multiaddr, type Resolver, resolvers, multiaddr } from '@multiformats/multiaddr'
7
7
  import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
8
+ import { Circuit } from '@multiformats/multiaddr-matcher'
8
9
  import { type ClearableSignal, anySignal } from 'any-signal'
9
10
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
11
  import { codes } from '../errors.js'
@@ -13,10 +14,11 @@ import {
13
14
  DIAL_TIMEOUT,
14
15
  MAX_PARALLEL_DIALS,
15
16
  MAX_PEER_ADDRS_TO_DIAL,
16
- LAST_DIAL_FAILURE_KEY
17
+ LAST_DIAL_FAILURE_KEY,
18
+ MAX_DIAL_QUEUE_LENGTH
17
19
  } from './constants.js'
18
20
  import { resolveMultiaddrs } from './utils.js'
19
- import type { AddressSorter, AbortOptions, ComponentLogger, Logger, Connection, ConnectionGater, Metrics, PeerId, Address, PeerStore, PeerRouting } from '@libp2p/interface'
21
+ import type { AddressSorter, AbortOptions, ComponentLogger, Logger, Connection, ConnectionGater, Metrics, PeerId, Address, PeerStore, PeerRouting, IsDialableOptions } from '@libp2p/interface'
20
22
  import type { TransportManager } from '@libp2p/interface-internal'
21
23
  import type { DNS } from '@multiformats/dns'
22
24
 
@@ -38,6 +40,7 @@ interface DialQueueJobOptions extends QueueAddOptions {
38
40
  interface DialerInit {
39
41
  addressSorter?: AddressSorter
40
42
  maxParallelDials?: number
43
+ maxDialQueueLength?: number
41
44
  maxPeerAddrsToDial?: number
42
45
  dialTimeout?: number
43
46
  resolvers?: Record<string, Resolver>
@@ -47,6 +50,7 @@ interface DialerInit {
47
50
  const defaultOptions = {
48
51
  addressSorter: defaultAddressSort,
49
52
  maxParallelDials: MAX_PARALLEL_DIALS,
53
+ maxDialQueueLength: MAX_DIAL_QUEUE_LENGTH,
50
54
  maxPeerAddrsToDial: MAX_PEER_ADDRS_TO_DIAL,
51
55
  dialTimeout: DIAL_TIMEOUT,
52
56
  resolvers: {
@@ -70,6 +74,7 @@ export class DialQueue {
70
74
  private readonly components: DialQueueComponents
71
75
  private readonly addressSorter: AddressSorter
72
76
  private readonly maxPeerAddrsToDial: number
77
+ private readonly maxDialQueueLength: number
73
78
  private readonly dialTimeout: number
74
79
  private shutDownController: AbortController
75
80
  private readonly connections: PeerMap<Connection[]>
@@ -78,6 +83,7 @@ export class DialQueue {
78
83
  constructor (components: DialQueueComponents, init: DialerInit = {}) {
79
84
  this.addressSorter = init.addressSorter ?? defaultOptions.addressSorter
80
85
  this.maxPeerAddrsToDial = init.maxPeerAddrsToDial ?? defaultOptions.maxPeerAddrsToDial
86
+ this.maxDialQueueLength = init.maxDialQueueLength ?? defaultOptions.maxDialQueueLength
81
87
  this.dialTimeout = init.dialTimeout ?? defaultOptions.dialTimeout
82
88
  this.connections = init.connections ?? new PeerMap()
83
89
  this.log = components.logger.forComponent('libp2p:connection-manager:dial-queue')
@@ -185,6 +191,10 @@ export class DialQueue {
185
191
  return existingDial.join(options)
186
192
  }
187
193
 
194
+ if (this.queue.size >= this.maxDialQueueLength) {
195
+ throw new CodeError('Dial queue is full', 'ERR_DIAL_QUEUE_FULL')
196
+ }
197
+
188
198
  this.log('creating dial target for %p', peerId, multiaddrs.map(ma => ma.toString()))
189
199
 
190
200
  return this.queue.add(async (options) => {
@@ -447,4 +457,27 @@ export class DialQueue {
447
457
 
448
458
  return sortedGatedAddrs
449
459
  }
460
+
461
+ async isDialable (multiaddr: Multiaddr | Multiaddr[], options: IsDialableOptions = {}): Promise<boolean> {
462
+ if (!Array.isArray(multiaddr)) {
463
+ multiaddr = [multiaddr]
464
+ }
465
+
466
+ try {
467
+ const addresses = await this.calculateMultiaddrs(undefined, new Set(multiaddr.map(ma => ma.toString())), options)
468
+
469
+ if (options.runOnTransientConnection === false) {
470
+ // return true if any resolved multiaddrs are not relay addresses
471
+ return addresses.find(addr => {
472
+ return !Circuit.matches(addr.multiaddr)
473
+ }) != null
474
+ }
475
+
476
+ return true
477
+ } catch (err) {
478
+ this.log.trace('error calculating if multiaddr(s) were dialable', err)
479
+ }
480
+
481
+ return false
482
+ }
450
483
  }
@@ -8,9 +8,9 @@ import { codes } from '../errors.js'
8
8
  import { getPeerAddress } from '../get-peer.js'
9
9
  import { AutoDial } from './auto-dial.js'
10
10
  import { ConnectionPruner } from './connection-pruner.js'
11
- 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'
11
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_DIAL_QUEUE_LENGTH, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
12
12
  import { DialQueue } from './dial-queue.js'
13
- import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions, ComponentLogger, Logger, Connection, MultiaddrConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, Peer, PeerStore, Startable, PendingDialStatus, PeerRouting } from '@libp2p/interface'
13
+ import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions, ComponentLogger, Logger, Connection, MultiaddrConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, Peer, PeerStore, Startable, PendingDialStatus, PeerRouting, IsDialableOptions } from '@libp2p/interface'
14
14
  import type { ConnectionManager, OpenConnectionOptions, TransportManager } from '@libp2p/interface-internal'
15
15
  import type { JobStatus } from '@libp2p/utils/queue'
16
16
 
@@ -81,6 +81,15 @@ export interface ConnectionManagerInit {
81
81
  */
82
82
  maxParallelDials?: number
83
83
 
84
+ /**
85
+ * The maximum size the dial queue is allowed to grow to. Promises returned
86
+ * when dialing peers after this limit is reached will not resolve until the
87
+ * queue size falls beneath this size.
88
+ *
89
+ * @default 500
90
+ */
91
+ maxDialQueueLength?: number
92
+
84
93
  /**
85
94
  * Maximum number of addresses allowed for a given peer before giving up
86
95
  *
@@ -168,7 +177,6 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
168
177
  public readonly autoDial: AutoDial
169
178
  public readonly connectionPruner: ConnectionPruner
170
179
  private readonly inboundConnectionRateLimiter: RateLimiter
171
-
172
180
  private readonly peerStore: PeerStore
173
181
  private readonly metrics?: Metrics
174
182
  private readonly events: TypedEventTarget<Libp2pEvents>
@@ -238,6 +246,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
238
246
  this.dialQueue = new DialQueue(components, {
239
247
  addressSorter: init.addressSorter ?? defaultAddressSort,
240
248
  maxParallelDials: init.maxParallelDials ?? MAX_PARALLEL_DIALS,
249
+ maxDialQueueLength: init.maxDialQueueLength ?? MAX_DIAL_QUEUE_LENGTH,
241
250
  maxPeerAddrsToDial: init.maxPeerAddrsToDial ?? MAX_PEER_ADDRS_TO_DIAL,
242
251
  dialTimeout: init.dialTimeout ?? DIAL_TIMEOUT,
243
252
  resolvers: init.resolvers ?? {
@@ -611,4 +620,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
611
620
  }
612
621
  })
613
622
  }
623
+
624
+ async isDialable (multiaddr: Multiaddr | Multiaddr[], options: IsDialableOptions = {}): Promise<boolean> {
625
+ return this.dialQueue.isDialable(multiaddr, options)
626
+ }
614
627
  }
package/src/libp2p.ts CHANGED
@@ -23,7 +23,7 @@ import { DefaultUpgrader } from './upgrader.js'
23
23
  import * as pkg from './version.js'
24
24
  import type { Components } from './components.js'
25
25
  import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
26
- import type { PeerRouting, ContentRouting, Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger, Connection, NewStreamOptions, Stream, Metrics, PeerId, PeerInfo, PeerStore, Topology, Libp2pStatus } from '@libp2p/interface'
26
+ import type { PeerRouting, ContentRouting, Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger, Connection, NewStreamOptions, Stream, Metrics, PeerId, PeerInfo, PeerStore, Topology, Libp2pStatus, IsDialableOptions } from '@libp2p/interface'
27
27
  import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal'
28
28
 
29
29
  export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends TypedEventEmitter<Libp2pEvents> implements Libp2p<T> {
@@ -375,6 +375,10 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
375
375
  this.components.registrar.unregister(id)
376
376
  }
377
377
 
378
+ async isDialable (multiaddr: Multiaddr, options: IsDialableOptions = {}): Promise<boolean> {
379
+ return this.components.connectionManager.isDialable(multiaddr, options)
380
+ }
381
+
378
382
  /**
379
383
  * Called whenever peer discovery services emit `peer` events and adds peers
380
384
  * to the peer store.
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '1.3.2'
1
+ export const version = '1.3.3-b17824a1d'
2
2
  export const name = 'libp2p'
@@ -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
- }