libp2p 0.43.4 → 0.44.0-44b30cc8

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 (105) hide show
  1. package/dist/index.min.js +30 -30
  2. package/dist/src/autonat/index.d.ts +1 -2
  3. package/dist/src/autonat/index.d.ts.map +1 -1
  4. package/dist/src/autonat/index.js +4 -6
  5. package/dist/src/autonat/index.js.map +1 -1
  6. package/dist/src/circuit-relay/transport/reservation-store.d.ts +1 -1
  7. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  8. package/dist/src/circuit-relay/utils.d.ts.map +1 -1
  9. package/dist/src/circuit-relay/utils.js +3 -1
  10. package/dist/src/circuit-relay/utils.js.map +1 -1
  11. package/dist/src/components.d.ts +1 -6
  12. package/dist/src/components.d.ts.map +1 -1
  13. package/dist/src/components.js +0 -10
  14. package/dist/src/components.js.map +1 -1
  15. package/dist/src/config.d.ts.map +1 -1
  16. package/dist/src/config.js +0 -9
  17. package/dist/src/config.js.map +1 -1
  18. package/dist/src/connection-manager/auto-dial.d.ts +36 -0
  19. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -0
  20. package/dist/src/connection-manager/auto-dial.js +122 -0
  21. package/dist/src/connection-manager/auto-dial.js.map +1 -0
  22. package/dist/src/connection-manager/connection-pruner.d.ts +28 -0
  23. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -0
  24. package/dist/src/connection-manager/connection-pruner.js +96 -0
  25. package/dist/src/connection-manager/connection-pruner.js.map +1 -0
  26. package/dist/src/connection-manager/constants.d.ts +49 -0
  27. package/dist/src/connection-manager/constants.d.ts.map +1 -0
  28. package/dist/src/connection-manager/constants.js +49 -0
  29. package/dist/src/connection-manager/constants.js.map +1 -0
  30. package/dist/src/connection-manager/dial-queue.d.ts +77 -0
  31. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -0
  32. package/dist/src/connection-manager/dial-queue.js +366 -0
  33. package/dist/src/connection-manager/dial-queue.js.map +1 -0
  34. package/dist/src/connection-manager/index.d.ts +64 -69
  35. package/dist/src/connection-manager/index.d.ts.map +1 -1
  36. package/dist/src/connection-manager/index.js +116 -234
  37. package/dist/src/connection-manager/index.js.map +1 -1
  38. package/dist/src/connection-manager/utils.d.ts +9 -0
  39. package/dist/src/connection-manager/utils.d.ts.map +1 -0
  40. package/dist/src/connection-manager/utils.js +66 -0
  41. package/dist/src/connection-manager/utils.js.map +1 -0
  42. package/dist/src/fetch/index.js +3 -3
  43. package/dist/src/fetch/index.js.map +1 -1
  44. package/dist/src/identify/index.js +5 -5
  45. package/dist/src/identify/index.js.map +1 -1
  46. package/dist/src/insecure/index.d.ts.map +1 -1
  47. package/dist/src/insecure/index.js +3 -1
  48. package/dist/src/insecure/index.js.map +1 -1
  49. package/dist/src/libp2p.d.ts +4 -2
  50. package/dist/src/libp2p.d.ts.map +1 -1
  51. package/dist/src/libp2p.js +7 -13
  52. package/dist/src/libp2p.js.map +1 -1
  53. package/dist/src/upgrader.d.ts +8 -8
  54. package/dist/src/upgrader.d.ts.map +1 -1
  55. package/dist/src/upgrader.js +2 -1
  56. package/dist/src/upgrader.js.map +1 -1
  57. package/package.json +40 -40
  58. package/src/autonat/index.ts +6 -9
  59. package/src/circuit-relay/transport/reservation-store.ts +1 -1
  60. package/src/circuit-relay/utils.ts +3 -1
  61. package/src/components.ts +1 -17
  62. package/src/config.ts +0 -9
  63. package/src/connection-manager/auto-dial.ts +170 -0
  64. package/src/connection-manager/connection-pruner.ts +137 -0
  65. package/src/connection-manager/constants.ts +60 -0
  66. package/src/connection-manager/dial-queue.ts +504 -0
  67. package/src/connection-manager/index.ts +189 -323
  68. package/src/connection-manager/utils.ts +76 -0
  69. package/src/fetch/index.ts +4 -4
  70. package/src/identify/index.ts +5 -5
  71. package/src/insecure/index.ts +9 -5
  72. package/src/libp2p.ts +9 -15
  73. package/src/upgrader.ts +14 -11
  74. package/src/version.ts +1 -1
  75. package/dist/src/connection-manager/auto-dialler.d.ts +0 -42
  76. package/dist/src/connection-manager/auto-dialler.d.ts.map +0 -1
  77. package/dist/src/connection-manager/auto-dialler.js +0 -105
  78. package/dist/src/connection-manager/auto-dialler.js.map +0 -1
  79. package/dist/src/connection-manager/dialer/dial-request.d.ts +0 -27
  80. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +0 -1
  81. package/dist/src/connection-manager/dialer/dial-request.js +0 -114
  82. package/dist/src/connection-manager/dialer/dial-request.js.map +0 -1
  83. package/dist/src/connection-manager/dialer/index.d.ts +0 -121
  84. package/dist/src/connection-manager/dialer/index.d.ts.map +0 -1
  85. package/dist/src/connection-manager/dialer/index.js +0 -324
  86. package/dist/src/connection-manager/dialer/index.js.map +0 -1
  87. package/dist/src/connection-manager/latency-monitor.d.ts +0 -120
  88. package/dist/src/connection-manager/latency-monitor.d.ts.map +0 -1
  89. package/dist/src/connection-manager/latency-monitor.js +0 -220
  90. package/dist/src/connection-manager/latency-monitor.js.map +0 -1
  91. package/dist/src/connection-manager/visibility-change-emitter.d.ts +0 -66
  92. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +0 -1
  93. package/dist/src/connection-manager/visibility-change-emitter.js +0 -118
  94. package/dist/src/connection-manager/visibility-change-emitter.js.map +0 -1
  95. package/dist/src/constants.d.ts +0 -27
  96. package/dist/src/constants.d.ts.map +0 -1
  97. package/dist/src/constants.js +0 -31
  98. package/dist/src/constants.js.map +0 -1
  99. package/dist/typedoc-urls.json +0 -41
  100. package/src/connection-manager/auto-dialler.ts +0 -158
  101. package/src/connection-manager/dialer/dial-request.ts +0 -144
  102. package/src/connection-manager/dialer/index.ts +0 -461
  103. package/src/connection-manager/latency-monitor.ts +0 -319
  104. package/src/connection-manager/visibility-change-emitter.ts +0 -135
  105. package/src/constants.ts +0 -36
@@ -0,0 +1,76 @@
1
+ import { AbortOptions, multiaddr } from '@multiformats/multiaddr'
2
+ import type { Multiaddr } from '@multiformats/multiaddr'
3
+ import { logger } from '@libp2p/logger'
4
+ import { ClearableSignal, anySignal } from 'any-signal'
5
+ import { setMaxListeners } from 'events'
6
+
7
+ const log = logger('libp2p:connection-manager:utils')
8
+
9
+ /**
10
+ * Resolve multiaddr recursively
11
+ */
12
+ export async function resolveMultiaddrs (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
13
+ // TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
14
+ // Now only supporting resolve for dnsaddr
15
+ const resolvableProto = ma.protoNames().includes('dnsaddr')
16
+
17
+ // Multiaddr is not resolvable? End recursion!
18
+ if (!resolvableProto) {
19
+ return [ma]
20
+ }
21
+
22
+ const resolvedMultiaddrs = await resolveRecord(ma, options)
23
+ const recursiveMultiaddrs = await Promise.all(resolvedMultiaddrs.map(async (nm) => {
24
+ return await resolveMultiaddrs(nm, options)
25
+ }))
26
+
27
+ const addrs = recursiveMultiaddrs.flat()
28
+ const output = addrs.reduce<Multiaddr[]>((array, newM) => {
29
+ if (array.find(m => m.equals(newM)) == null) {
30
+ array.push(newM)
31
+ }
32
+ return array
33
+ }, ([]))
34
+
35
+ log('resolved %s to', ma, output.map(ma => ma.toString()))
36
+
37
+ return output
38
+ }
39
+
40
+ /**
41
+ * Resolve a given multiaddr. If this fails, an empty array will be returned
42
+ */
43
+ async function resolveRecord (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
44
+ try {
45
+ ma = multiaddr(ma.toString()) // Use current multiaddr module
46
+ const multiaddrs = await ma.resolve(options)
47
+ return multiaddrs
48
+ } catch (err) {
49
+ log.error(`multiaddr ${ma.toString()} could not be resolved`, err)
50
+ return []
51
+ }
52
+ }
53
+
54
+ export function combineSignals (...signals: Array<AbortSignal | undefined>): ClearableSignal {
55
+ const sigs: AbortSignal[] = []
56
+
57
+ for (const sig of signals) {
58
+ if (sig != null) {
59
+ try {
60
+ // fails on node < 15.4
61
+ setMaxListeners?.(Infinity, sig)
62
+ } catch { }
63
+ sigs.push(sig)
64
+ }
65
+ }
66
+
67
+ // let any signal abort the dial
68
+ const signal = anySignal(sigs)
69
+
70
+ try {
71
+ // fails on node < 15.4
72
+ setMaxListeners?.(Infinity, signal)
73
+ } catch {}
74
+
75
+ return signal
76
+ }
@@ -127,9 +127,9 @@ export class FetchService implements Startable {
127
127
 
128
128
  const result = await pipe(
129
129
  [FetchRequest.encode({ identifier: key })],
130
- lp.encode(),
130
+ (source) => lp.encode(source),
131
131
  source,
132
- lp.decode(),
132
+ (source) => lp.decode(source),
133
133
  async function (source) {
134
134
  const buf = await first(source)
135
135
 
@@ -184,7 +184,7 @@ export class FetchService implements Startable {
184
184
 
185
185
  await pipe(
186
186
  stream,
187
- lp.decode(),
187
+ (source) => lp.decode(source),
188
188
  async function * (source) {
189
189
  const buf = await first(source)
190
190
 
@@ -215,7 +215,7 @@ export class FetchService implements Startable {
215
215
 
216
216
  yield FetchResponse.encode(response)
217
217
  },
218
- lp.encode(),
218
+ (source) => lp.encode(source),
219
219
  stream
220
220
  )
221
221
  }
@@ -197,7 +197,7 @@ export class IdentifyService implements Startable {
197
197
  signedPeerRecord,
198
198
  protocols
199
199
  })],
200
- lp.encode()
200
+ (source) => lp.encode(source)
201
201
  ))
202
202
  } catch (err: any) {
203
203
  // Just log errors
@@ -266,7 +266,7 @@ export class IdentifyService implements Startable {
266
266
  const data = await pipe(
267
267
  [],
268
268
  source,
269
- lp.decode({
269
+ (source) => lp.decode(source, {
270
270
  maxDataLength: this.init.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
271
271
  }),
272
272
  async (source) => await first(source)
@@ -348,7 +348,7 @@ export class IdentifyService implements Startable {
348
348
  await this.components.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(protocolVersion))
349
349
  }
350
350
 
351
- log('identify completed for peer %p with and protocols %o', id, protocols)
351
+ log('identify completed for peer %p with protocols %o', id, protocols)
352
352
 
353
353
  return
354
354
  }
@@ -431,7 +431,7 @@ export class IdentifyService implements Startable {
431
431
  // make stream abortable
432
432
  const source = abortableDuplex(stream, timeoutController.signal)
433
433
 
434
- const msgWithLenPrefix = pipe([message], lp.encode())
434
+ const msgWithLenPrefix = pipe([message], (source) => lp.encode(source))
435
435
  await source.sink(msgWithLenPrefix)
436
436
  } catch (err: any) {
437
437
  log.error('could not respond to identify request', err)
@@ -461,7 +461,7 @@ export class IdentifyService implements Startable {
461
461
  const data = await pipe(
462
462
  [],
463
463
  source,
464
- lp.decode({
464
+ (source) => lp.decode(source, {
465
465
  maxDataLength: this.init.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
466
466
  }),
467
467
  async (source) => await first(source)
@@ -30,7 +30,7 @@ import { Exchange, KeyType } from './pb/proto.js'
30
30
  import type { PeerId } from '@libp2p/interface-peer-id'
31
31
  import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
32
32
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
33
- import type { Duplex } from 'it-stream-types'
33
+ import type { Duplex, Source } from 'it-stream-types'
34
34
  import map from 'it-map'
35
35
  import type { Uint8ArrayList } from 'uint8arraylist'
36
36
 
@@ -46,7 +46,7 @@ function lpEncodeExchange (exchange: Exchange): Uint8ArrayList {
46
46
  /**
47
47
  * Encrypt connection
48
48
  */
49
- async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
49
+ async function encrypt (localId: PeerId, conn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remoteId?: PeerId): Promise<SecuredConnection> {
50
50
  const shake = handshake(conn)
51
51
 
52
52
  let type = KeyType.RSA
@@ -71,8 +71,12 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
71
71
  log('write pubkey exchange to peer %p', remoteId)
72
72
 
73
73
  // Get the Exchange message
74
- // @ts-expect-error needs to be generator
75
74
  const response = (await lp.decode.fromReader(shake.reader).next()).value
75
+
76
+ if (response == null) {
77
+ throw new Error('Did not read response')
78
+ }
79
+
76
80
  const id = Exchange.decode(response)
77
81
  log('read pubkey exchange from peer %p', remoteId)
78
82
 
@@ -120,11 +124,11 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
120
124
  class Plaintext implements ConnectionEncrypter {
121
125
  public protocol: string = PROTOCOL
122
126
 
123
- async secureInbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
127
+ async secureInbound (localId: PeerId, conn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remoteId?: PeerId): Promise<SecuredConnection> {
124
128
  return await encrypt(localId, conn, remoteId)
125
129
  }
126
130
 
127
- async secureOutbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
131
+ async secureOutbound (localId: PeerId, conn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remoteId?: PeerId): Promise<SecuredConnection> {
128
132
  return await encrypt(localId, conn, remoteId)
129
133
  }
130
134
  }
package/src/libp2p.ts CHANGED
@@ -9,7 +9,6 @@ import { CompoundContentRouting } from './content-routing/index.js'
9
9
  import { codes } from './errors.js'
10
10
  import { DefaultAddressManager } from './address-manager/index.js'
11
11
  import { DefaultConnectionManager } from './connection-manager/index.js'
12
- import { AutoDialler } from './connection-manager/auto-dialler.js'
13
12
  import { DefaultKeyChain } from '@libp2p/keychain'
14
13
  import { DefaultTransportManager } from './transport-manager.js'
15
14
  import { DefaultUpgrader } from './upgrader.js'
@@ -46,12 +45,12 @@ import type { Metrics } from '@libp2p/interface-metrics'
46
45
  import { DummyDHT } from './dht/dummy-dht.js'
47
46
  import { DummyPubSub } from './pubsub/dummy-pubsub.js'
48
47
  import { PeerSet } from '@libp2p/peer-collections'
49
- import { DefaultDialer } from './connection-manager/dialer/index.js'
50
48
  import { peerIdFromString } from '@libp2p/peer-id'
51
49
  import type { Datastore } from 'interface-datastore'
52
50
  import type { KeyChain } from '@libp2p/interface-keychain'
53
51
  import mergeOptions from 'merge-options'
54
52
  import type { CircuitRelayService } from './circuit-relay/index.js'
53
+ import type { PendingDial } from '@libp2p/interface-libp2p'
55
54
 
56
55
  const log = logger('libp2p')
57
56
 
@@ -131,8 +130,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
131
130
  inboundUpgradeTimeout: init.connectionManager.inboundUpgradeTimeout
132
131
  })
133
132
 
134
- // Create the dialer
135
- this.components.dialer = new DefaultDialer(this.components, init.connectionManager)
133
+ // Setup the transport manager
134
+ this.components.transportManager = new DefaultTransportManager(this.components, init.transportManager)
136
135
 
137
136
  // Create the Connection Manager
138
137
  this.connectionManager = this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager)
@@ -148,9 +147,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
148
147
  // Create the Registrar
149
148
  this.registrar = this.components.registrar = new DefaultRegistrar(this.components)
150
149
 
151
- // Setup the transport manager
152
- this.components.transportManager = new DefaultTransportManager(this.components, init.transportManager)
153
-
154
150
  // Addresses {listen, announce, noAnnounce}
155
151
  this.components.addressManager = new DefaultAddressManager(this.components, init.addresses)
156
152
 
@@ -230,11 +226,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
230
226
  ...init.autonat
231
227
  }))
232
228
 
233
- this.configureComponent(new AutoDialler(this.components, {
234
- minConnections: init.connectionManager.minConnections,
235
- autoDialInterval: init.connectionManager.autoDialInterval
236
- }))
237
-
238
229
  if (init.relay != null) {
239
230
  this.circuitService = this.configureComponent(init.relay(this.components))
240
231
  }
@@ -354,6 +345,10 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
354
345
  return this.components.connectionManager.getConnections(peerId)
355
346
  }
356
347
 
348
+ getDialQueue (): PendingDial[] {
349
+ return this.components.connectionManager.getDialQueue()
350
+ }
351
+
357
352
  getPeers (): PeerId[] {
358
353
  const peerSet = new PeerSet()
359
354
 
@@ -365,7 +360,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
365
360
  }
366
361
 
367
362
  async dial (peer: PeerId | Multiaddr | Multiaddr[], options: AbortOptions = {}): Promise<Connection> {
368
- // @ts-expect-error
369
363
  return await this.components.connectionManager.openConnection(peer, options)
370
364
  }
371
365
 
@@ -493,8 +487,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
493
487
  }
494
488
 
495
489
  /**
496
- * Called whenever peer discovery services emit `peer` events.
497
- * Known peers may be emitted.
490
+ * Called whenever peer discovery services emit `peer` events and adds peers
491
+ * to the peer store.
498
492
  */
499
493
  onDiscoveryPeer (evt: CustomEvent<PeerInfo>): void {
500
494
  const { detail: peer } = evt
package/src/upgrader.ts CHANGED
@@ -10,7 +10,7 @@ import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-c
10
10
  import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
11
11
  import type { PeerId } from '@libp2p/interface-peer-id'
12
12
  import type { Upgrader, UpgraderEvents, UpgraderOptions } from '@libp2p/interface-transport'
13
- import type { Duplex } from 'it-stream-types'
13
+ import type { Duplex, Source } from 'it-stream-types'
14
14
  import type { AbortOptions } from '@libp2p/interfaces'
15
15
  import type { Registrar } from '@libp2p/interface-registrar'
16
16
  import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
@@ -21,14 +21,15 @@ import type { Metrics } from '@libp2p/interface-metrics'
21
21
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
22
22
  import type { PeerStore } from '@libp2p/interface-peer-store'
23
23
  import type { ConnectionGater } from '@libp2p/interface-connection-gater'
24
+ import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
24
25
 
25
26
  const log = logger('libp2p:upgrader')
26
27
 
27
- interface CreateConectionOptions {
28
+ interface CreateConnectionOptions {
28
29
  cryptoProtocol: string
29
30
  direction: 'inbound' | 'outbound'
30
31
  maConn: MultiaddrConnection
31
- upgradedConn: Duplex<Uint8Array>
32
+ upgradedConn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>
32
33
  remotePeer: PeerId
33
34
  muxerFactory?: StreamMuxerFactory
34
35
  }
@@ -51,7 +52,7 @@ export interface UpgraderInit {
51
52
  * An amount of ms by which an inbound connection upgrade
52
53
  * must complete
53
54
  */
54
- inboundUpgradeTimeout: number
55
+ inboundUpgradeTimeout?: number
55
56
  }
56
57
 
57
58
  function findIncomingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
@@ -126,7 +127,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
126
127
  this.muxers.set(muxer.protocol, muxer)
127
128
  })
128
129
 
129
- this.inboundUpgradeTimeout = init.inboundUpgradeTimeout
130
+ this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ?? INBOUND_UPGRADE_TIMEOUT
130
131
  }
131
132
 
132
133
  /**
@@ -141,7 +142,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
141
142
 
142
143
  let encryptedConn
143
144
  let remotePeer
144
- let upgradedConn: Duplex<Uint8Array>
145
+ let upgradedConn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>
145
146
  let muxerFactory: StreamMuxerFactory | undefined
146
147
  let cryptoProtocol
147
148
 
@@ -263,7 +264,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
263
264
 
264
265
  let encryptedConn
265
266
  let remotePeer: PeerId
266
- let upgradedConn
267
+ let upgradedConn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>
267
268
  let cryptoProtocol
268
269
  let muxerFactory
269
270
 
@@ -349,7 +350,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
349
350
  /**
350
351
  * A convenience method for generating a new `Connection`
351
352
  */
352
- _createConnection (opts: CreateConectionOptions): Connection {
353
+ _createConnection (opts: CreateConnectionOptions): Connection {
353
354
  const {
354
355
  cryptoProtocol,
355
356
  direction,
@@ -569,7 +570,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
569
570
  /**
570
571
  * Attempts to encrypt the incoming `connection` with the provided `cryptos`
571
572
  */
572
- async _encryptInbound (connection: Duplex<Uint8Array>): Promise<CryptoResult> {
573
+ async _encryptInbound (connection: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>): Promise<CryptoResult> {
573
574
  const protocols = Array.from(this.connectionEncryption.keys())
574
575
  log('handling inbound crypto protocol selection', protocols)
575
576
 
@@ -627,7 +628,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
627
628
  * Selects one of the given muxers via multistream-select. That
628
629
  * muxer will be used for all future streams on the connection.
629
630
  */
630
- async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory }> {
631
+ async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, muxerFactory?: StreamMuxerFactory }> {
631
632
  const protocols = Array.from(muxers.keys())
632
633
  log('outbound selecting muxer %s', protocols)
633
634
  try {
@@ -636,6 +637,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
636
637
  })
637
638
  log('%s selected as muxer protocol', protocol)
638
639
  const muxerFactory = muxers.get(protocol)
640
+
639
641
  return { stream, muxerFactory }
640
642
  } catch (err: any) {
641
643
  log.error('error multiplexing outbound stream', err)
@@ -647,7 +649,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
647
649
  * Registers support for one of the given muxers via multistream-select. The
648
650
  * selected muxer will be used for all future streams on the connection.
649
651
  */
650
- async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<Uint8Array>, muxerFactory?: StreamMuxerFactory }> {
652
+ async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, muxerFactory?: StreamMuxerFactory }> {
651
653
  const protocols = Array.from(muxers.keys())
652
654
  log('inbound handling muxers %s', protocols)
653
655
  try {
@@ -655,6 +657,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
655
657
  writeBytes: true
656
658
  })
657
659
  const muxerFactory = muxers.get(protocol)
660
+
658
661
  return { stream, muxerFactory }
659
662
  } catch (err: any) {
660
663
  log.error('error multiplexing inbound stream', err)
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.43.4'
1
+ export const version = '0.44.0-44b30cc8'
2
2
  export const name = 'libp2p'
@@ -1,42 +0,0 @@
1
- import type { Startable } from '@libp2p/interfaces/startable';
2
- import type { PeerId } from '@libp2p/interface-peer-id';
3
- import type { ConnectionManager } from '@libp2p/interface-connection-manager';
4
- import type { PeerStore } from '@libp2p/interface-peer-store';
5
- export interface AutoDiallerInit {
6
- /**
7
- * The minimum number of connections to avoid pruning
8
- */
9
- minConnections?: number;
10
- /**
11
- * How often, in milliseconds, it should preemptively guarantee connections are above the low watermark
12
- */
13
- autoDialInterval?: number;
14
- }
15
- export interface AutoDiallerComponents {
16
- peerId: PeerId;
17
- connectionManager: ConnectionManager;
18
- peerStore: PeerStore;
19
- }
20
- export declare class AutoDialler implements Startable {
21
- private readonly components;
22
- private readonly options;
23
- private running;
24
- private autoDialTimeout?;
25
- /**
26
- * Proactively tries to connect to known peers stored in the PeerStore.
27
- * It will keep the number of connections below the upper limit and sort
28
- * the peers to connect based on wether we know their keys and protocols.
29
- */
30
- constructor(components: AutoDiallerComponents, init: AutoDiallerInit);
31
- isStarted(): boolean;
32
- /**
33
- * Starts the auto dialer
34
- */
35
- start(): Promise<void>;
36
- /**
37
- * Stops the auto dialler
38
- */
39
- stop(): Promise<void>;
40
- _autoDial(): Promise<void>;
41
- }
42
- //# sourceMappingURL=auto-dialler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auto-dialler.d.ts","sourceRoot":"","sources":["../../../src/connection-manager/auto-dialler.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AAC7E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAI7D,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAED,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAA;IACd,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,SAAS,EAAE,SAAS,CAAA;CACrB;AAOD,qBAAa,WAAY,YAAW,SAAS;IAC3C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAuB;IAClD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,eAAe,CAAC,CAAqB;IAE7C;;;;OAIG;gBACU,UAAU,EAAE,qBAAqB,EAAE,IAAI,EAAE,eAAe;IASrE,SAAS,IAAK,OAAO;IAIrB;;OAEG;IACG,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAU7B;;OAEG;IACG,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAUtB,SAAS,IAAK,OAAO,CAAC,IAAI,CAAC;CAyElC"}
@@ -1,105 +0,0 @@
1
- import { logger } from '@libp2p/logger';
2
- import mergeOptions from 'merge-options';
3
- // @ts-expect-error retimer does not have types
4
- import retimer from 'retimer';
5
- const log = logger('libp2p:connection-manager:auto-dialler');
6
- const defaultOptions = {
7
- minConnections: 0,
8
- autoDialInterval: 10000
9
- };
10
- export class AutoDialler {
11
- /**
12
- * Proactively tries to connect to known peers stored in the PeerStore.
13
- * It will keep the number of connections below the upper limit and sort
14
- * the peers to connect based on wether we know their keys and protocols.
15
- */
16
- constructor(components, init) {
17
- this.components = components;
18
- this.options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init);
19
- this.running = false;
20
- this._autoDial = this._autoDial.bind(this);
21
- log('options: %j', this.options);
22
- }
23
- isStarted() {
24
- return this.running;
25
- }
26
- /**
27
- * Starts the auto dialer
28
- */
29
- async start() {
30
- this.running = true;
31
- void this._autoDial().catch(err => {
32
- log.error('could not start autodial', err);
33
- });
34
- log('started');
35
- }
36
- /**
37
- * Stops the auto dialler
38
- */
39
- async stop() {
40
- this.running = false;
41
- if (this.autoDialTimeout != null) {
42
- this.autoDialTimeout.clear();
43
- }
44
- log('stopped');
45
- }
46
- async _autoDial() {
47
- if (this.autoDialTimeout != null) {
48
- this.autoDialTimeout.clear();
49
- }
50
- const minConnections = this.options.minConnections;
51
- // Already has enough connections
52
- if (this.components.connectionManager.getConnections().length >= minConnections) {
53
- this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval);
54
- return;
55
- }
56
- // Sort peers on whether we know protocols or public keys for them
57
- let peers = await this.components.peerStore.all();
58
- peers = peers.filter((peer) => {
59
- // do not dial ourselves
60
- if (peer.id.equals(this.components.peerId)) {
61
- return false;
62
- }
63
- // do not dial peers without multiaddrs
64
- if (peer.addresses.length === 0) {
65
- return false;
66
- }
67
- return true;
68
- });
69
- // shuffle the peers
70
- peers = peers.sort(() => Math.random() > 0.5 ? 1 : -1);
71
- peers = peers.sort((a, b) => {
72
- // dial peers with the most protocols first
73
- if (b.protocols.length > a.protocols.length) {
74
- return 1;
75
- }
76
- // dial peers with public keys first
77
- if (b.id.publicKey != null && a.id.publicKey == null) {
78
- return 1;
79
- }
80
- return -1;
81
- });
82
- for (let i = 0; this.running && i < peers.length && this.components.connectionManager.getConnections().length < minConnections; i++) {
83
- // Connection Manager was stopped during async dial
84
- if (!this.running) {
85
- return;
86
- }
87
- const peer = peers[i];
88
- if (this.components.connectionManager.getConnections(peer.id).length === 0) {
89
- log('connecting to a peerStore stored peer %p', peer.id);
90
- try {
91
- await this.components.connectionManager.openConnection(peer.id);
92
- }
93
- catch (err) {
94
- log.error('could not connect to peerStore stored peer', err);
95
- }
96
- }
97
- }
98
- // Connection Manager was stopped
99
- if (!this.running) {
100
- return;
101
- }
102
- this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval);
103
- }
104
- }
105
- //# sourceMappingURL=auto-dialler.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auto-dialler.js","sourceRoot":"","sources":["../../../src/connection-manager/auto-dialler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,YAAY,MAAM,eAAe,CAAA;AACxC,+CAA+C;AAC/C,OAAO,OAAO,MAAM,SAAS,CAAA;AAM7B,MAAM,GAAG,GAAG,MAAM,CAAC,wCAAwC,CAAC,CAAA;AAoB5D,MAAM,cAAc,GAA6B;IAC/C,cAAc,EAAE,CAAC;IACjB,gBAAgB,EAAE,KAAK;CACxB,CAAA;AAED,MAAM,OAAO,WAAW;IAMtB;;;;OAIG;IACH,YAAa,UAAiC,EAAE,IAAqB;QACnE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,EAAE,eAAe,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,IAAI,CAAC,CAAA;QACjF,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAE1C,GAAG,CAAC,aAAa,EAAE,IAAI,CAA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,CAAC,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;gBAC1E,GAAG,CAAC,0CAA0C,EAAE,IAAI,CAAC,EAAE,CAAC,CAAA;gBACxD,IAAI;oBACF,MAAM,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;iBAChE;gBAAC,OAAO,GAAQ,EAAE;oBACjB,GAAG,CAAC,KAAK,CAAC,4CAA4C,EAAE,GAAG,CAAC,CAAA;iBAC7D;aACF;SACF;QAED,iCAAiC;QACjC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;YACjB,OAAM;SACP;QAED,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA;IAC/E,CAAC;CACF"}
@@ -1,27 +0,0 @@
1
- import type { Multiaddr } from '@multiformats/multiaddr';
2
- import type { Connection } from '@libp2p/interface-connection';
3
- import type { AbortOptions } from '@libp2p/interfaces';
4
- import type { Dialer } from '@libp2p/interface-connection-manager';
5
- export interface DialAction {
6
- (m: Multiaddr, options: AbortOptions): Promise<Connection>;
7
- }
8
- export interface DialRequestOptions {
9
- addrs: Multiaddr[];
10
- dialAction: DialAction;
11
- dialer: Dialer;
12
- }
13
- export declare class DialRequest {
14
- private readonly addrs;
15
- private readonly dialer;
16
- private readonly dialAction;
17
- /**
18
- * Manages running the `dialAction` on multiple provided `addrs` in parallel
19
- * up to a maximum determined by the number of tokens returned
20
- * from `dialer.getTokens`. Once a DialRequest is created, it can be
21
- * started using `DialRequest.run(options)`. Once a single dial has succeeded,
22
- * all other dials in the request will be cancelled.
23
- */
24
- constructor(options: DialRequestOptions);
25
- run(options?: AbortOptions): Promise<Connection>;
26
- }
27
- //# sourceMappingURL=dial-request.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dial-request.d.ts","sourceRoot":"","sources":["../../../../src/connection-manager/dialer/dial-request.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAC9D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sCAAsC,CAAA;AAIlE,MAAM,WAAW,UAAU;IACzB,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;CAC3D;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,SAAS,EAAE,CAAA;IAClB,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,MAAM,CAAA;CACf;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IAEvC;;;;;;OAMG;gBACU,OAAO,EAAE,kBAAkB;IAYlC,GAAG,CAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;CAgG5D"}