libp2p 1.7.0 → 1.8.0

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.
@@ -7,6 +7,7 @@ import { type Multiaddr, type Resolver, resolvers, multiaddr } from '@multiforma
7
7
  import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
8
8
  import { Circuit } from '@multiformats/multiaddr-matcher'
9
9
  import { type ClearableSignal, anySignal } from 'any-signal'
10
+ import { CustomProgressEvent } from 'progress-events'
10
11
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
11
12
  import { codes } from '../errors.js'
12
13
  import { getPeerAddress } from '../get-peer.js'
@@ -19,21 +20,17 @@ import {
19
20
  } from './constants.js'
20
21
  import { resolveMultiaddrs } from './utils.js'
21
22
  import { DEFAULT_DIAL_PRIORITY } from './index.js'
22
- import type { AddressSorter, AbortOptions, ComponentLogger, Logger, Connection, ConnectionGater, Metrics, PeerId, Address, PeerStore, PeerRouting, IsDialableOptions } from '@libp2p/interface'
23
- import type { TransportManager } from '@libp2p/interface-internal'
23
+ import type { AddressSorter, ComponentLogger, Logger, Connection, ConnectionGater, Metrics, PeerId, Address, PeerStore, PeerRouting, IsDialableOptions } from '@libp2p/interface'
24
+ import type { OpenConnectionOptions, OpenConnectionProgressEvents, TransportManager } from '@libp2p/interface-internal'
24
25
  import type { DNS } from '@multiformats/dns'
26
+ import type { ProgressOptions } from 'progress-events'
25
27
 
26
28
  export interface PendingDialTarget {
27
29
  resolve(value: any): void
28
30
  reject(err: Error): void
29
31
  }
30
32
 
31
- export interface DialOptions extends AbortOptions {
32
- priority?: number
33
- force?: boolean
34
- }
35
-
36
- interface DialQueueJobOptions extends PriorityQueueJobOptions {
33
+ interface DialQueueJobOptions extends PriorityQueueJobOptions, ProgressOptions<OpenConnectionProgressEvents> {
37
34
  peerId?: PeerId
38
35
  multiaddrs: Set<string>
39
36
  }
@@ -134,7 +131,7 @@ export class DialQueue {
134
131
  * The dial to the first address that is successfully able to upgrade a
135
132
  * connection will be used, all other dials will be aborted when that happens.
136
133
  */
137
- async dial (peerIdOrMultiaddr: PeerId | Multiaddr | Multiaddr[], options: DialOptions = {}): Promise<Connection> {
134
+ async dial (peerIdOrMultiaddr: PeerId | Multiaddr | Multiaddr[], options: OpenConnectionOptions = {}): Promise<Connection> {
138
135
  const { peerId, multiaddrs } = getPeerAddress(peerIdOrMultiaddr)
139
136
 
140
137
  // make sure we don't have an existing connection to any of the addresses we
@@ -155,6 +152,7 @@ export class DialQueue {
155
152
 
156
153
  if (existingConnection != null) {
157
154
  this.log('already connected to %a', existingConnection.remoteAddr)
155
+ options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
158
156
  return existingConnection
159
157
  }
160
158
 
@@ -189,6 +187,7 @@ export class DialQueue {
189
187
  existingDial.options.multiaddrs.add(multiaddr.toString())
190
188
  }
191
189
 
190
+ options.onProgress?.(new CustomProgressEvent('dial-queue:already-in-dial-queue'))
192
191
  return existingDial.join(options)
193
192
  }
194
193
 
@@ -198,7 +197,9 @@ export class DialQueue {
198
197
 
199
198
  this.log('creating dial target for %p', peerId, multiaddrs.map(ma => ma.toString()))
200
199
 
200
+ options.onProgress?.(new CustomProgressEvent('dial-queue:add-to-dial-queue'))
201
201
  return this.queue.add(async (options) => {
202
+ options?.onProgress?.(new CustomProgressEvent('dial-queue:start-dial'))
202
203
  // create abort conditions - need to do this before `calculateMultiaddrs` as
203
204
  // we may be about to resolve a dns addr which can time out
204
205
  const signal = this.createDialAbortController(options?.signal)
@@ -212,6 +213,8 @@ export class DialQueue {
212
213
  signal
213
214
  })
214
215
 
216
+ options?.onProgress?.(new CustomProgressEvent<Address[]>('dial-queue:calculated-addresses', addrsToDial))
217
+
215
218
  addrsToDial.map(({ multiaddr }) => multiaddr.toString()).forEach(addr => {
216
219
  options?.multiaddrs.add(addr)
217
220
  })
@@ -280,7 +283,8 @@ export class DialQueue {
280
283
  peerId,
281
284
  priority: options.priority ?? DEFAULT_DIAL_PRIORITY,
282
285
  multiaddrs: new Set(multiaddrs.map(ma => ma.toString())),
283
- signal: options.signal
286
+ signal: options.signal,
287
+ onProgress: options.onProgress
284
288
  })
285
289
  }
286
290
 
@@ -299,7 +303,7 @@ export class DialQueue {
299
303
  }
300
304
 
301
305
  // eslint-disable-next-line complexity
302
- private async calculateMultiaddrs (peerId?: PeerId, multiaddrs: Set<string> = new Set<string>(), options: DialOptions = {}): Promise<Address[]> {
306
+ private async calculateMultiaddrs (peerId?: PeerId, multiaddrs: Set<string> = new Set<string>(), options: OpenConnectionOptions = {}): Promise<Address[]> {
303
307
  const addrs: Address[] = [...multiaddrs].map(ma => ({
304
308
  multiaddr: multiaddr(ma),
305
309
  isCertified: false
@@ -4,6 +4,7 @@ import { defaultAddressSort } from '@libp2p/utils/address-sort'
4
4
  import { RateLimiter } from '@libp2p/utils/rate-limiter'
5
5
  import { type Multiaddr, type Resolver, multiaddr } from '@multiformats/multiaddr'
6
6
  import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
7
+ import { CustomProgressEvent } from 'progress-events'
7
8
  import { codes } from '../errors.js'
8
9
  import { getPeerAddress } from '../get-peer.js'
9
10
  import { AutoDial } from './auto-dial.js'
@@ -509,6 +510,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
509
510
  if (existingConnection != null) {
510
511
  this.log('had an existing non-transient connection to %p', peerId)
511
512
 
513
+ options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
512
514
  return existingConnection
513
515
  }
514
516
  }
@@ -1,8 +1,9 @@
1
1
  import { CodeError, FaultTolerance } from '@libp2p/interface'
2
2
  import { trackedMap } from '@libp2p/utils/tracked-map'
3
+ import { CustomProgressEvent } from 'progress-events'
3
4
  import { codes } from './errors.js'
4
- import type { Libp2pEvents, AbortOptions, ComponentLogger, Logger, Connection, TypedEventTarget, Metrics, Startable, Listener, Transport, Upgrader } from '@libp2p/interface'
5
- import type { AddressManager, TransportManager } from '@libp2p/interface-internal'
5
+ import type { Libp2pEvents, ComponentLogger, Logger, Connection, TypedEventTarget, Metrics, Startable, Listener, Transport, Upgrader } from '@libp2p/interface'
6
+ import type { AddressManager, TransportManager, TransportManagerDialOptions } from '@libp2p/interface-internal'
6
7
  import type { Multiaddr } from '@multiformats/multiaddr'
7
8
 
8
9
  export interface TransportManagerInit {
@@ -107,14 +108,19 @@ export class DefaultTransportManager implements TransportManager, Startable {
107
108
  /**
108
109
  * Dials the given Multiaddr over it's supported transport
109
110
  */
110
- async dial (ma: Multiaddr, options?: AbortOptions): Promise<Connection> {
111
+ async dial (ma: Multiaddr, options?: TransportManagerDialOptions): Promise<Connection> {
111
112
  const transport = this.dialTransportForMultiaddr(ma)
112
113
 
113
114
  if (transport == null) {
114
115
  throw new CodeError(`No transport available for address ${String(ma)}`, codes.ERR_TRANSPORT_UNAVAILABLE)
115
116
  }
116
117
 
118
+ options?.onProgress?.(new CustomProgressEvent<string>('transport-manager:selected-transport', transport[Symbol.toStringTag]))
119
+
117
120
  try {
121
+ // @ts-expect-error the transport has a typed onProgress option but we
122
+ // can't predict what transport implementation we selected so all we can
123
+ // do is pass the onProgress handler in and hope for the best
118
124
  return await transport.dial(ma, {
119
125
  ...options,
120
126
  upgrader: this.components.upgrader
package/src/upgrader.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { CodeError, ERR_TIMEOUT, setMaxListeners } from '@libp2p/interface'
2
2
  import * as mss from '@libp2p/multistream-select'
3
3
  import { peerIdFromString } from '@libp2p/peer-id'
4
+ import { CustomProgressEvent } from 'progress-events'
4
5
  import { createConnection } from './connection/index.js'
5
6
  import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
6
7
  import { codes } from './errors.js'
@@ -185,6 +186,8 @@ export class DefaultUpgrader implements Upgrader {
185
186
  // Encrypt the connection
186
187
  encryptedConn = protectedConn
187
188
  if (opts?.skipEncryption !== true) {
189
+ opts?.onProgress?.(new CustomProgressEvent('upgrader:encrypt-inbound-connection'));
190
+
188
191
  ({
189
192
  conn: encryptedConn,
190
193
  remotePeer,
@@ -214,6 +217,8 @@ export class DefaultUpgrader implements Upgrader {
214
217
  if (opts?.muxerFactory != null) {
215
218
  muxerFactory = opts.muxerFactory
216
219
  } else if (this.muxers.size > 0) {
220
+ opts?.onProgress?.(new CustomProgressEvent('upgrader:multiplex-inbound-connection'))
221
+
217
222
  // Multiplex the connection
218
223
  const multiplexed = await this._multiplexInbound({
219
224
  ...protectedConn,
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '1.7.0'
1
+ export const version = '1.8.0'
2
2
  export const name = 'libp2p'