libp2p 0.46.19 → 0.46.20

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 (67) hide show
  1. package/dist/index.min.js +22 -22
  2. package/dist/src/components.d.ts +3 -1
  3. package/dist/src/components.d.ts.map +1 -1
  4. package/dist/src/components.js +4 -0
  5. package/dist/src/components.js.map +1 -1
  6. package/dist/src/connection/index.d.ts +3 -1
  7. package/dist/src/connection/index.d.ts.map +1 -1
  8. package/dist/src/connection/index.js +17 -11
  9. package/dist/src/connection/index.js.map +1 -1
  10. package/dist/src/connection-manager/auto-dial.d.ts +3 -1
  11. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  12. package/dist/src/connection-manager/auto-dial.js +20 -20
  13. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  14. package/dist/src/connection-manager/connection-pruner.d.ts +3 -1
  15. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  16. package/dist/src/connection-manager/connection-pruner.js +8 -8
  17. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  18. package/dist/src/connection-manager/dial-queue.d.ts +3 -1
  19. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  20. package/dist/src/connection-manager/dial-queue.js +26 -23
  21. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  22. package/dist/src/connection-manager/index.d.ts +3 -1
  23. package/dist/src/connection-manager/index.d.ts.map +1 -1
  24. package/dist/src/connection-manager/index.js +21 -18
  25. package/dist/src/connection-manager/index.js.map +1 -1
  26. package/dist/src/connection-manager/utils.d.ts +2 -1
  27. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  28. package/dist/src/connection-manager/utils.js +2 -4
  29. package/dist/src/connection-manager/utils.js.map +1 -1
  30. package/dist/src/identify/consts.d.ts +1 -1
  31. package/dist/src/identify/identify.d.ts.map +1 -1
  32. package/dist/src/identify/identify.js +28 -21
  33. package/dist/src/identify/identify.js.map +1 -1
  34. package/dist/src/identify/index.d.ts +2 -1
  35. package/dist/src/identify/index.d.ts.map +1 -1
  36. package/dist/src/identify/index.js.map +1 -1
  37. package/dist/src/index.d.ts +21 -1
  38. package/dist/src/index.d.ts.map +1 -1
  39. package/dist/src/index.js.map +1 -1
  40. package/dist/src/libp2p.d.ts +2 -1
  41. package/dist/src/libp2p.d.ts.map +1 -1
  42. package/dist/src/libp2p.js +20 -16
  43. package/dist/src/libp2p.js.map +1 -1
  44. package/dist/src/ping/index.d.ts.map +1 -1
  45. package/dist/src/ping/index.js +9 -3
  46. package/dist/src/ping/index.js.map +1 -1
  47. package/dist/src/upgrader.d.ts +3 -1
  48. package/dist/src/upgrader.d.ts.map +1 -1
  49. package/dist/src/upgrader.js +37 -31
  50. package/dist/src/upgrader.js.map +1 -1
  51. package/dist/src/version.d.ts +1 -1
  52. package/dist/src/version.js +1 -1
  53. package/package.json +21 -21
  54. package/src/components.ts +8 -1
  55. package/src/connection/index.ts +20 -13
  56. package/src/connection-manager/auto-dial.ts +22 -22
  57. package/src/connection-manager/connection-pruner.ts +10 -10
  58. package/src/connection-manager/dial-queue.ts +28 -25
  59. package/src/connection-manager/index.ts +23 -20
  60. package/src/connection-manager/utils.ts +5 -7
  61. package/src/identify/identify.ts +30 -23
  62. package/src/identify/index.ts +2 -1
  63. package/src/index.ts +22 -1
  64. package/src/libp2p.ts +21 -18
  65. package/src/ping/index.ts +10 -3
  66. package/src/upgrader.ts +39 -32
  67. package/src/version.ts +1 -1
package/src/upgrader.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  import { CodeError } from '@libp2p/interface/errors'
2
2
  import { setMaxListeners } from '@libp2p/interface/events'
3
- import { logger } from '@libp2p/logger'
4
3
  import * as mss from '@libp2p/multistream-select'
5
4
  import { peerIdFromString } from '@libp2p/peer-id'
6
5
  import { createConnection } from './connection/index.js'
7
6
  import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
8
7
  import { codes } from './errors.js'
9
8
  import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
10
- import type { Libp2pEvents, AbortOptions } from '@libp2p/interface'
9
+ import type { Libp2pEvents, AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
11
10
  import type { MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions } from '@libp2p/interface/connection'
12
11
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface/connection-encrypter'
13
12
  import type { ConnectionGater } from '@libp2p/interface/connection-gater'
@@ -21,7 +20,7 @@ import type { ConnectionManager } from '@libp2p/interface-internal/connection-ma
21
20
  import type { Registrar } from '@libp2p/interface-internal/registrar'
22
21
  import type { Duplex, Source } from 'it-stream-types'
23
22
 
24
- const log = logger('libp2p:upgrader')
23
+ const DEFAULT_PROTOCOL_SELECT_TIMEOUT = 30000
25
24
 
26
25
  interface CreateConnectionOptions {
27
26
  cryptoProtocol: string
@@ -105,6 +104,7 @@ export interface DefaultUpgraderComponents {
105
104
  registrar: Registrar
106
105
  peerStore: PeerStore
107
106
  events: TypedEventTarget<Libp2pEvents>
107
+ logger: ComponentLogger
108
108
  }
109
109
 
110
110
  type EncryptedConn = Duplex<AsyncGenerator<Uint8Array, any, unknown>, Source<Uint8Array>, Promise<void>>
@@ -117,10 +117,12 @@ export class DefaultUpgrader implements Upgrader {
117
117
  private readonly muxers: Map<string, StreamMuxerFactory>
118
118
  private readonly inboundUpgradeTimeout: number
119
119
  private readonly events: TypedEventTarget<Libp2pEvents>
120
+ readonly #log: Logger
120
121
 
121
122
  constructor (components: DefaultUpgraderComponents, init: UpgraderInit) {
122
123
  this.components = components
123
124
  this.connectionEncryption = new Map()
125
+ this.#log = components.logger.forComponent('libp2p:upgrader')
124
126
 
125
127
  init.connectionEncryption.forEach(encrypter => {
126
128
  this.connectionEncryption.set(encrypter.protocol, encrypter)
@@ -179,7 +181,7 @@ export class DefaultUpgrader implements Upgrader {
179
181
 
180
182
  this.components.metrics?.trackMultiaddrConnection(maConn)
181
183
 
182
- log('starting the inbound connection upgrade')
184
+ this.#log('starting the inbound connection upgrade')
183
185
 
184
186
  // Protect
185
187
  let protectedConn = maConn
@@ -188,7 +190,7 @@ export class DefaultUpgrader implements Upgrader {
188
190
  const protector = this.components.connectionProtector
189
191
 
190
192
  if (protector != null) {
191
- log('protecting the inbound connection')
193
+ this.#log('protecting the inbound connection')
192
194
  protectedConn = await protector.protect(maConn)
193
195
  }
194
196
  }
@@ -235,13 +237,13 @@ export class DefaultUpgrader implements Upgrader {
235
237
  upgradedConn = multiplexed.stream
236
238
  }
237
239
  } catch (err: any) {
238
- log.error('Failed to upgrade inbound connection', err)
240
+ this.#log.error('Failed to upgrade inbound connection', err)
239
241
  throw err
240
242
  }
241
243
 
242
244
  await this.shouldBlockConnection(remotePeer, maConn, 'denyInboundUpgradedConnection')
243
245
 
244
- log('Successfully upgraded inbound connection')
246
+ this.#log('Successfully upgraded inbound connection')
245
247
 
246
248
  return this._createConnection({
247
249
  cryptoProtocol,
@@ -280,7 +282,7 @@ export class DefaultUpgrader implements Upgrader {
280
282
 
281
283
  this.components.metrics?.trackMultiaddrConnection(maConn)
282
284
 
283
- log('Starting the outbound connection upgrade')
285
+ this.#log('Starting the outbound connection upgrade')
284
286
 
285
287
  // If the transport natively supports encryption, skip connection
286
288
  // protector and encryption
@@ -333,14 +335,14 @@ export class DefaultUpgrader implements Upgrader {
333
335
  upgradedConn = multiplexed.stream
334
336
  }
335
337
  } catch (err: any) {
336
- log.error('Failed to upgrade outbound connection', err)
338
+ this.#log.error('Failed to upgrade outbound connection', err)
337
339
  await maConn.close(err)
338
340
  throw err
339
341
  }
340
342
 
341
343
  await this.shouldBlockConnection(remotePeer, maConn, 'denyOutboundUpgradedConnection')
342
344
 
343
- log('Successfully upgraded outbound connection')
345
+ this.#log('Successfully upgraded outbound connection')
344
346
 
345
347
  return this._createConnection({
346
348
  cryptoProtocol,
@@ -385,7 +387,7 @@ export class DefaultUpgrader implements Upgrader {
385
387
  .then(async () => {
386
388
  const protocols = this.components.registrar.getProtocols()
387
389
  const { stream, protocol } = await mss.handle(muxedStream, protocols)
388
- log('%s: incoming stream opened on %s', direction, protocol)
390
+ this.#log('%s: incoming stream opened on %s', direction, protocol)
389
391
 
390
392
  if (connection == null) {
391
393
  return
@@ -418,7 +420,7 @@ export class DefaultUpgrader implements Upgrader {
418
420
  this._onStream({ connection, stream: muxedStream, protocol })
419
421
  })
420
422
  .catch(async err => {
421
- log.error(err)
423
+ this.#log.error(err)
422
424
 
423
425
  if (muxedStream.timeline.close == null) {
424
426
  await muxedStream.close()
@@ -432,16 +434,20 @@ export class DefaultUpgrader implements Upgrader {
432
434
  throw new CodeError('Stream is not multiplexed', codes.ERR_MUXER_UNAVAILABLE)
433
435
  }
434
436
 
435
- log('%s-%s: starting new stream on %s', connection.id, direction, protocols)
437
+ this.#log('%s-%s: starting new stream on %s', connection.id, direction, protocols)
436
438
  const muxedStream = await muxer.newStream()
437
439
 
438
440
  try {
439
441
  if (options.signal == null) {
440
- log('No abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
442
+ this.#log('No abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
441
443
 
442
- options.signal = AbortSignal.timeout(30000)
444
+ const signal = AbortSignal.timeout(DEFAULT_PROTOCOL_SELECT_TIMEOUT)
445
+ setMaxListeners(Infinity, signal)
443
446
 
444
- setMaxListeners(Infinity, options.signal)
447
+ options = {
448
+ ...options,
449
+ signal
450
+ }
445
451
  }
446
452
 
447
453
  const { stream, protocol } = await mss.select(muxedStream, protocols, options)
@@ -472,7 +478,7 @@ export class DefaultUpgrader implements Upgrader {
472
478
 
473
479
  return muxedStream
474
480
  } catch (err: any) {
475
- log.error('could not create new stream for protocols %s on connection with address %a', protocols, connection.remoteAddr, err)
481
+ this.#log.error('could not create new stream for protocols %s on connection with address %a', protocols, connection.remoteAddr, err)
476
482
 
477
483
  if (muxedStream.timeline.close == null) {
478
484
  muxedStream.abort(err)
@@ -491,7 +497,7 @@ export class DefaultUpgrader implements Upgrader {
491
497
  muxer.sink(upgradedConn.source),
492
498
  upgradedConn.sink(muxer.source)
493
499
  ]).catch(err => {
494
- log.error(err)
500
+ this.#log.error(err)
495
501
  })
496
502
  }
497
503
 
@@ -506,14 +512,14 @@ export class DefaultUpgrader implements Upgrader {
506
512
  await connection.close()
507
513
  }
508
514
  } catch (err: any) {
509
- log.error(err)
515
+ this.#log.error(err)
510
516
  } finally {
511
517
  this.events.safeDispatchEvent('connection:close', {
512
518
  detail: connection
513
519
  })
514
520
  }
515
521
  })().catch(err => {
516
- log.error(err)
522
+ this.#log.error(err)
517
523
  })
518
524
  }
519
525
 
@@ -536,19 +542,20 @@ export class DefaultUpgrader implements Upgrader {
536
542
  multiplexer: muxer?.protocol,
537
543
  encryption: cryptoProtocol,
538
544
  transient,
545
+ logger: this.components.logger,
539
546
  newStream: newStream ?? errConnectionNotMultiplexed,
540
547
  getStreams: () => { if (muxer != null) { return muxer.streams } else { return [] } },
541
548
  close: async (options?: AbortOptions) => {
542
549
  // Ensure remaining streams are closed gracefully
543
550
  if (muxer != null) {
544
- log.trace('close muxer')
551
+ this.#log.trace('close muxer')
545
552
  await muxer.close(options)
546
553
  }
547
554
 
548
- log.trace('close maconn')
555
+ this.#log.trace('close maconn')
549
556
  // close the underlying transport
550
557
  await maConn.close(options)
551
- log.trace('closed maconn')
558
+ this.#log.trace('closed maconn')
552
559
  },
553
560
  abort: (err) => {
554
561
  maConn.abort(err)
@@ -585,7 +592,7 @@ export class DefaultUpgrader implements Upgrader {
585
592
  */
586
593
  async _encryptInbound (connection: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>): Promise<CryptoResult> {
587
594
  const protocols = Array.from(this.connectionEncryption.keys())
588
- log('handling inbound crypto protocol selection', protocols)
595
+ this.#log('handling inbound crypto protocol selection', protocols)
589
596
 
590
597
  try {
591
598
  const { stream, protocol } = await mss.handle(connection, protocols, {
@@ -597,7 +604,7 @@ export class DefaultUpgrader implements Upgrader {
597
604
  throw new Error(`no crypto module found for ${protocol}`)
598
605
  }
599
606
 
600
- log('encrypting inbound connection...')
607
+ this.#log('encrypting inbound connection...')
601
608
 
602
609
  return {
603
610
  ...await encrypter.secureInbound(this.components.peerId, stream),
@@ -614,7 +621,7 @@ export class DefaultUpgrader implements Upgrader {
614
621
  */
615
622
  async _encryptOutbound (connection: MultiaddrConnection, remotePeerId?: PeerId): Promise<CryptoResult> {
616
623
  const protocols = Array.from(this.connectionEncryption.keys())
617
- log('selecting outbound crypto protocol', protocols)
624
+ this.#log('selecting outbound crypto protocol', protocols)
618
625
 
619
626
  try {
620
627
  const { stream, protocol } = await mss.select(connection, protocols, {
@@ -626,7 +633,7 @@ export class DefaultUpgrader implements Upgrader {
626
633
  throw new Error(`no crypto module found for ${protocol}`)
627
634
  }
628
635
 
629
- log('encrypting outbound connection to %p', remotePeerId)
636
+ this.#log('encrypting outbound connection to %p', remotePeerId)
630
637
 
631
638
  return {
632
639
  ...await encrypter.secureOutbound(this.components.peerId, stream, remotePeerId),
@@ -643,17 +650,17 @@ export class DefaultUpgrader implements Upgrader {
643
650
  */
644
651
  async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, muxerFactory?: StreamMuxerFactory }> {
645
652
  const protocols = Array.from(muxers.keys())
646
- log('outbound selecting muxer %s', protocols)
653
+ this.#log('outbound selecting muxer %s', protocols)
647
654
  try {
648
655
  const { stream, protocol } = await mss.select(connection, protocols, {
649
656
  writeBytes: true
650
657
  })
651
- log('%s selected as muxer protocol', protocol)
658
+ this.#log('%s selected as muxer protocol', protocol)
652
659
  const muxerFactory = muxers.get(protocol)
653
660
 
654
661
  return { stream, muxerFactory }
655
662
  } catch (err: any) {
656
- log.error('error multiplexing outbound stream', err)
663
+ this.#log.error('error multiplexing outbound stream', err)
657
664
  throw new CodeError(String(err), codes.ERR_MUXER_UNAVAILABLE)
658
665
  }
659
666
  }
@@ -664,7 +671,7 @@ export class DefaultUpgrader implements Upgrader {
664
671
  */
665
672
  async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, muxerFactory?: StreamMuxerFactory }> {
666
673
  const protocols = Array.from(muxers.keys())
667
- log('inbound handling muxers %s', protocols)
674
+ this.#log('inbound handling muxers %s', protocols)
668
675
  try {
669
676
  const { stream, protocol } = await mss.handle(connection, protocols, {
670
677
  writeBytes: true
@@ -673,7 +680,7 @@ export class DefaultUpgrader implements Upgrader {
673
680
 
674
681
  return { stream, muxerFactory }
675
682
  } catch (err: any) {
676
- log.error('error multiplexing inbound stream', err)
683
+ this.#log.error('error multiplexing inbound stream', err)
677
684
  throw new CodeError(String(err), codes.ERR_MUXER_UNAVAILABLE)
678
685
  }
679
686
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.46.19'
1
+ export const version = '0.46.20'
2
2
  export const name = 'libp2p'