libp2p 0.37.2 → 0.37.3-3c0fb13

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 (165) hide show
  1. package/README.md +1 -2
  2. package/dist/src/address-manager/index.d.ts +2 -2
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/circuit/auto-relay.d.ts +4 -4
  5. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  6. package/dist/src/circuit/circuit/hop.d.ts +5 -4
  7. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  8. package/dist/src/circuit/circuit/hop.js +8 -4
  9. package/dist/src/circuit/circuit/hop.js.map +1 -1
  10. package/dist/src/circuit/circuit/stop.d.ts +3 -2
  11. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  12. package/dist/src/circuit/circuit/stop.js +4 -2
  13. package/dist/src/circuit/circuit/stop.js.map +1 -1
  14. package/dist/src/circuit/circuit/stream-handler.d.ts +1 -1
  15. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  16. package/dist/src/circuit/index.d.ts +4 -12
  17. package/dist/src/circuit/index.d.ts.map +1 -1
  18. package/dist/src/circuit/index.js.map +1 -1
  19. package/dist/src/circuit/listener.d.ts +3 -3
  20. package/dist/src/circuit/listener.d.ts.map +1 -1
  21. package/dist/src/circuit/transport.d.ts +8 -5
  22. package/dist/src/circuit/transport.d.ts.map +1 -1
  23. package/dist/src/circuit/transport.js +81 -59
  24. package/dist/src/circuit/transport.js.map +1 -1
  25. package/dist/src/config.d.ts.map +1 -1
  26. package/dist/src/config.js +27 -4
  27. package/dist/src/config.js.map +1 -1
  28. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  29. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  30. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +2 -2
  31. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -1
  32. package/dist/src/connection-manager/dialer/auto-dialer.js +6 -0
  33. package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -1
  34. package/dist/src/connection-manager/dialer/dial-request.d.ts +1 -1
  35. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  36. package/dist/src/connection-manager/dialer/index.d.ts +9 -9
  37. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  38. package/dist/src/connection-manager/dialer/index.js +11 -11
  39. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  40. package/dist/src/connection-manager/index.d.ts +24 -20
  41. package/dist/src/connection-manager/index.d.ts.map +1 -1
  42. package/dist/src/connection-manager/index.js +209 -82
  43. package/dist/src/connection-manager/index.js.map +1 -1
  44. package/dist/src/constants.d.ts +4 -0
  45. package/dist/src/constants.d.ts.map +1 -1
  46. package/dist/src/constants.js +4 -0
  47. package/dist/src/constants.js.map +1 -1
  48. package/dist/src/content-routing/index.d.ts +4 -4
  49. package/dist/src/content-routing/index.d.ts.map +1 -1
  50. package/dist/src/content-routing/utils.d.ts +2 -2
  51. package/dist/src/content-routing/utils.d.ts.map +1 -1
  52. package/dist/src/dht/dht-content-routing.d.ts +3 -3
  53. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  54. package/dist/src/dht/dht-peer-routing.d.ts +5 -5
  55. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  56. package/dist/src/dht/dummy-dht.d.ts +3 -3
  57. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  58. package/dist/src/dht/dummy-dht.js +1 -1
  59. package/dist/src/dht/dummy-dht.js.map +1 -1
  60. package/dist/src/errors.d.ts +4 -1
  61. package/dist/src/errors.d.ts.map +1 -1
  62. package/dist/src/errors.js +3 -0
  63. package/dist/src/errors.js.map +1 -1
  64. package/dist/src/fetch/index.d.ts +11 -4
  65. package/dist/src/fetch/index.d.ts.map +1 -1
  66. package/dist/src/fetch/index.js +82 -41
  67. package/dist/src/fetch/index.js.map +1 -1
  68. package/dist/src/get-peer.d.ts +2 -2
  69. package/dist/src/get-peer.d.ts.map +1 -1
  70. package/dist/src/get-peer.js +1 -1
  71. package/dist/src/get-peer.js.map +1 -1
  72. package/dist/src/identify/index.d.ts +8 -8
  73. package/dist/src/identify/index.d.ts.map +1 -1
  74. package/dist/src/identify/index.js +48 -33
  75. package/dist/src/identify/index.js.map +1 -1
  76. package/dist/src/index.d.ts +18 -17
  77. package/dist/src/index.d.ts.map +1 -1
  78. package/dist/src/index.js.map +1 -1
  79. package/dist/src/insecure/index.d.ts +2 -2
  80. package/dist/src/insecure/index.d.ts.map +1 -1
  81. package/dist/src/insecure/index.js +1 -1
  82. package/dist/src/insecure/index.js.map +1 -1
  83. package/dist/src/keychain/index.d.ts +2 -2
  84. package/dist/src/keychain/index.d.ts.map +1 -1
  85. package/dist/src/libp2p.d.ts +14 -14
  86. package/dist/src/libp2p.d.ts.map +1 -1
  87. package/dist/src/libp2p.js +17 -17
  88. package/dist/src/libp2p.js.map +1 -1
  89. package/dist/src/metrics/index.d.ts +3 -3
  90. package/dist/src/metrics/index.d.ts.map +1 -1
  91. package/dist/src/metrics/index.js +6 -2
  92. package/dist/src/metrics/index.js.map +1 -1
  93. package/dist/src/metrics/moving-average.d.ts +1 -1
  94. package/dist/src/metrics/moving-average.d.ts.map +1 -1
  95. package/dist/src/metrics/stats.d.ts +1 -1
  96. package/dist/src/metrics/stats.d.ts.map +1 -1
  97. package/dist/src/nat-manager.d.ts +1 -1
  98. package/dist/src/nat-manager.d.ts.map +1 -1
  99. package/dist/src/peer-record-updater.d.ts +1 -1
  100. package/dist/src/peer-record-updater.d.ts.map +1 -1
  101. package/dist/src/peer-routing.d.ts +4 -4
  102. package/dist/src/peer-routing.d.ts.map +1 -1
  103. package/dist/src/ping/index.d.ts +10 -3
  104. package/dist/src/ping/index.d.ts.map +1 -1
  105. package/dist/src/ping/index.js +31 -8
  106. package/dist/src/ping/index.js.map +1 -1
  107. package/dist/src/pnet/index.d.ts +1 -2
  108. package/dist/src/pnet/index.d.ts.map +1 -1
  109. package/dist/src/pnet/index.js.map +1 -1
  110. package/dist/src/pubsub/dummy-pubsub.d.ts +2 -2
  111. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  112. package/dist/src/registrar.d.ts +10 -8
  113. package/dist/src/registrar.d.ts.map +1 -1
  114. package/dist/src/registrar.js +63 -44
  115. package/dist/src/registrar.js.map +1 -1
  116. package/dist/src/transport-manager.d.ts +3 -3
  117. package/dist/src/transport-manager.d.ts.map +1 -1
  118. package/dist/src/upgrader.d.ts +12 -6
  119. package/dist/src/upgrader.d.ts.map +1 -1
  120. package/dist/src/upgrader.js +164 -65
  121. package/dist/src/upgrader.js.map +1 -1
  122. package/dist/src/version.d.ts.map +1 -1
  123. package/dist/src/version.js.map +1 -1
  124. package/package.json +56 -32
  125. package/src/address-manager/index.ts +2 -2
  126. package/src/circuit/auto-relay.ts +4 -4
  127. package/src/circuit/circuit/hop.ts +15 -8
  128. package/src/circuit/circuit/stop.ts +8 -4
  129. package/src/circuit/circuit/stream-handler.ts +1 -1
  130. package/src/circuit/index.ts +4 -13
  131. package/src/circuit/listener.ts +3 -3
  132. package/src/circuit/transport.ts +90 -63
  133. package/src/config.ts +27 -4
  134. package/src/connection-manager/auto-dialler.ts +1 -1
  135. package/src/connection-manager/dialer/auto-dialer.ts +8 -2
  136. package/src/connection-manager/dialer/dial-request.ts +1 -1
  137. package/src/connection-manager/dialer/index.ts +15 -15
  138. package/src/connection-manager/index.ts +267 -97
  139. package/src/constants.ts +5 -0
  140. package/src/content-routing/index.ts +2 -2
  141. package/src/content-routing/utils.ts +2 -2
  142. package/src/dht/dht-content-routing.ts +2 -2
  143. package/src/dht/dht-peer-routing.ts +3 -3
  144. package/src/dht/dummy-dht.ts +3 -3
  145. package/src/errors.ts +4 -1
  146. package/src/fetch/index.ts +120 -53
  147. package/src/get-peer.ts +3 -3
  148. package/src/identify/index.ts +60 -42
  149. package/src/index.ts +18 -17
  150. package/src/insecure/index.ts +3 -3
  151. package/src/keychain/index.ts +2 -2
  152. package/src/libp2p.ts +34 -29
  153. package/src/metrics/index.ts +9 -5
  154. package/src/metrics/moving-average.ts +1 -1
  155. package/src/metrics/stats.ts +1 -1
  156. package/src/nat-manager.ts +1 -1
  157. package/src/peer-record-updater.ts +1 -1
  158. package/src/peer-routing.ts +4 -4
  159. package/src/ping/index.ts +47 -14
  160. package/src/pnet/index.ts +1 -2
  161. package/src/pubsub/dummy-pubsub.ts +2 -2
  162. package/src/registrar.ts +79 -55
  163. package/src/transport-manager.ts +3 -3
  164. package/src/upgrader.ts +204 -76
  165. package/src/version.ts +1 -2
@@ -3,8 +3,9 @@ import { CircuitRelay as CircuitPB } from '../pb/index.js'
3
3
  import { RELAY_CODEC } from '../multicodec.js'
4
4
  import { StreamHandler } from './stream-handler.js'
5
5
  import { validateAddrs } from './utils.js'
6
- import type { Connection } from '@libp2p/interfaces/connection'
6
+ import type { Connection } from '@libp2p/interface-connection'
7
7
  import type { Duplex } from 'it-stream-types'
8
+ import type { AbortOptions } from '@libp2p/interfaces'
8
9
 
9
10
  const log = logger('libp2p:circuit:stop')
10
11
 
@@ -42,7 +43,7 @@ export function handleStop (options: HandleStopOptions): Duplex<Uint8Array> | un
42
43
  return streamHandler.rest()
43
44
  }
44
45
 
45
- export interface StopOptions {
46
+ export interface StopOptions extends AbortOptions {
46
47
  connection: Connection
47
48
  request: CircuitPB
48
49
  }
@@ -53,10 +54,13 @@ export interface StopOptions {
53
54
  export async function stop (options: StopOptions) {
54
55
  const {
55
56
  connection,
56
- request
57
+ request,
58
+ signal
57
59
  } = options
58
60
 
59
- const { stream } = await connection.newStream([RELAY_CODEC])
61
+ const stream = await connection.newStream(RELAY_CODEC, {
62
+ signal
63
+ })
60
64
  log('starting stop request to %p', connection.remotePeer)
61
65
  const streamHandler = new StreamHandler({ stream })
62
66
 
@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
2
2
  import * as lp from 'it-length-prefixed'
3
3
  import { Handshake, handshake } from 'it-handshake'
4
4
  import { CircuitRelay } from '../pb/index.js'
5
- import type { Stream } from '@libp2p/interfaces/connection'
5
+ import type { Stream } from '@libp2p/interface-connection'
6
6
  import type { Source } from 'it-stream-types'
7
7
 
8
8
  const log = logger('libp2p:circuit:stream-handler')
@@ -10,9 +10,10 @@ import { namespaceToCid } from './utils.js'
10
10
  import {
11
11
  RELAY_RENDEZVOUS_NS
12
12
  } from './constants.js'
13
- import type { AddressSorter } from '@libp2p/interfaces/peer-store'
13
+ import type { AddressSorter } from '@libp2p/interface-peer-store'
14
14
  import type { Startable } from '@libp2p/interfaces/startable'
15
- import type { Components } from '@libp2p/interfaces/components'
15
+ import type { Components } from '@libp2p/components'
16
+ import type { RelayConfig } from '../index.js'
16
17
 
17
18
  const log = logger('libp2p:relay')
18
19
 
@@ -22,11 +23,6 @@ export interface RelayAdvertiseConfig {
22
23
  ttl?: number
23
24
  }
24
25
 
25
- export interface HopConfig {
26
- enabled?: boolean
27
- active?: boolean
28
- }
29
-
30
26
  export interface AutoRelayConfig {
31
27
  enabled?: boolean
32
28
 
@@ -36,13 +32,8 @@ export interface AutoRelayConfig {
36
32
  maxListeners: number
37
33
  }
38
34
 
39
- export interface RelayInit {
35
+ export interface RelayInit extends RelayConfig {
40
36
  addressSorter?: AddressSorter
41
- maxListeners?: number
42
- onError?: (error: Error, msg?: string) => void
43
- hop: HopConfig
44
- advertise: RelayAdvertiseConfig
45
- autoRelay: AutoRelayConfig
46
37
  }
47
38
 
48
39
  export class Relay implements Startable {
@@ -1,7 +1,7 @@
1
1
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
2
- import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
3
- import type { PeerStore } from '@libp2p/interfaces/peer-store'
4
- import type { Listener } from '@libp2p/interfaces/transport'
2
+ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
3
+ import type { PeerStore } from '@libp2p/interface-peer-store'
4
+ import type { Listener } from '@libp2p/interface-transport'
5
5
  import { peerIdFromString } from '@libp2p/peer-id'
6
6
  import { Multiaddr } from '@multiformats/multiaddr'
7
7
 
@@ -10,19 +10,28 @@ import { createListener } from './listener.js'
10
10
  import { handleCanHop, handleHop, hop } from './circuit/hop.js'
11
11
  import { handleStop } from './circuit/stop.js'
12
12
  import { StreamHandler } from './circuit/stream-handler.js'
13
- import { symbol } from '@libp2p/interfaces/transport'
13
+ import { symbol } from '@libp2p/interface-transport'
14
14
  import { peerIdFromString } from '@libp2p/peer-id'
15
- import { Components, Initializable } from '@libp2p/interfaces/components'
15
+ import { Components, Initializable } from '@libp2p/components'
16
16
  import type { AbortOptions } from '@libp2p/interfaces'
17
- import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
18
- import type { Listener, Transport, CreateListenerOptions, ConnectionHandler } from '@libp2p/interfaces/transport'
19
- import type { Connection } from '@libp2p/interfaces/connection'
17
+ import type { IncomingStreamData } from '@libp2p/interface-registrar'
18
+ import type { Listener, Transport, CreateListenerOptions, ConnectionHandler } from '@libp2p/interface-transport'
19
+ import type { Connection } from '@libp2p/interface-connection'
20
+ import type { RelayConfig } from '../index.js'
21
+ import { abortableDuplex } from 'abortable-iterator'
22
+ import { TimeoutController } from 'timeout-abort-controller'
23
+ import { setMaxListeners } from 'events'
20
24
 
21
25
  const log = logger('libp2p:circuit')
22
26
 
23
27
  export class Circuit implements Transport, Initializable {
24
28
  private handler?: ConnectionHandler
25
29
  private components: Components = new Components()
30
+ private readonly _init: RelayConfig
31
+
32
+ constructor (init: RelayConfig) {
33
+ this._init = init
34
+ }
26
35
 
27
36
  init (components: Components): void {
28
37
  this.components = components
@@ -54,49 +63,25 @@ export class Circuit implements Transport, Initializable {
54
63
 
55
64
  async _onProtocol (data: IncomingStreamData) {
56
65
  const { connection, stream } = data
57
- const streamHandler = new StreamHandler({ stream })
58
- const request = await streamHandler.read()
59
-
60
- if (request == null) {
61
- log('request was invalid, could not read from stream')
62
- streamHandler.write({
63
- type: CircuitPB.Type.STATUS,
64
- code: CircuitPB.Status.MALFORMED_MESSAGE
65
- })
66
- streamHandler.close()
67
- return
68
- }
66
+ const controller = new TimeoutController(this._init.hop.timeout)
69
67
 
70
- let virtualConnection
68
+ try {
69
+ // fails on node < 15.4
70
+ setMaxListeners?.(Infinity, controller.signal)
71
+ } catch {}
71
72
 
72
- switch (request.type) {
73
- case CircuitPB.Type.CAN_HOP: {
74
- log('received CAN_HOP request from %p', connection.remotePeer)
75
- await handleCanHop({ circuit: this, connection, streamHandler })
76
- break
77
- }
78
- case CircuitPB.Type.HOP: {
79
- log('received HOP request from %p', connection.remotePeer)
80
- virtualConnection = await handleHop({
81
- connection,
82
- request,
83
- streamHandler,
84
- circuit: this,
85
- connectionManager: this.components.getConnectionManager()
86
- })
87
- break
88
- }
89
- case CircuitPB.Type.STOP: {
90
- log('received STOP request from %p', connection.remotePeer)
91
- virtualConnection = await handleStop({
92
- connection,
93
- request,
94
- streamHandler
95
- })
96
- break
97
- }
98
- default: {
99
- log('Request of type %s not supported', request.type)
73
+ try {
74
+ const source = abortableDuplex(stream, controller.signal)
75
+ const streamHandler = new StreamHandler({
76
+ stream: {
77
+ ...stream,
78
+ ...source
79
+ }
80
+ })
81
+ const request = await streamHandler.read()
82
+
83
+ if (request == null) {
84
+ log('request was invalid, could not read from stream')
100
85
  streamHandler.write({
101
86
  type: CircuitPB.Type.STATUS,
102
87
  code: CircuitPB.Status.MALFORMED_MESSAGE
@@ -104,27 +89,68 @@ export class Circuit implements Transport, Initializable {
104
89
  streamHandler.close()
105
90
  return
106
91
  }
107
- }
108
92
 
109
- if (virtualConnection != null) {
110
- // @ts-expect-error dst peer will not be undefined
111
- const remoteAddr = new Multiaddr(request.dstPeer.addrs[0])
112
- // @ts-expect-error dst peer will not be undefined
113
- const localAddr = new Multiaddr(request.srcPeer.addrs[0])
114
- const maConn = streamToMaConnection({
115
- stream: virtualConnection,
116
- remoteAddr,
117
- localAddr
118
- })
119
- const type = request.type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
120
- log('new %s connection %s', type, maConn.remoteAddr)
93
+ let virtualConnection
94
+
95
+ switch (request.type) {
96
+ case CircuitPB.Type.CAN_HOP: {
97
+ log('received CAN_HOP request from %p', connection.remotePeer)
98
+ await handleCanHop({ circuit: this, connection, streamHandler })
99
+ break
100
+ }
101
+ case CircuitPB.Type.HOP: {
102
+ log('received HOP request from %p', connection.remotePeer)
103
+ virtualConnection = await handleHop({
104
+ connection,
105
+ request,
106
+ streamHandler,
107
+ circuit: this,
108
+ connectionManager: this.components.getConnectionManager()
109
+ })
110
+ break
111
+ }
112
+ case CircuitPB.Type.STOP: {
113
+ log('received STOP request from %p', connection.remotePeer)
114
+ virtualConnection = await handleStop({
115
+ connection,
116
+ request,
117
+ streamHandler
118
+ })
119
+ break
120
+ }
121
+ default: {
122
+ log('Request of type %s not supported', request.type)
123
+ streamHandler.write({
124
+ type: CircuitPB.Type.STATUS,
125
+ code: CircuitPB.Status.MALFORMED_MESSAGE
126
+ })
127
+ streamHandler.close()
128
+ return
129
+ }
130
+ }
121
131
 
122
- const conn = await this.components.getUpgrader().upgradeInbound(maConn)
123
- log('%s connection %s upgraded', type, maConn.remoteAddr)
132
+ if (virtualConnection != null) {
133
+ // @ts-expect-error dst peer will not be undefined
134
+ const remoteAddr = new Multiaddr(request.dstPeer.addrs[0])
135
+ // @ts-expect-error dst peer will not be undefined
136
+ const localAddr = new Multiaddr(request.srcPeer.addrs[0])
137
+ const maConn = streamToMaConnection({
138
+ stream: virtualConnection,
139
+ remoteAddr,
140
+ localAddr
141
+ })
142
+ const type = request.type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
143
+ log('new %s connection %s', type, maConn.remoteAddr)
144
+
145
+ const conn = await this.components.getUpgrader().upgradeInbound(maConn)
146
+ log('%s connection %s upgraded', type, maConn.remoteAddr)
124
147
 
125
- if (this.handler != null) {
126
- this.handler(conn)
148
+ if (this.handler != null) {
149
+ this.handler(conn)
150
+ }
127
151
  }
152
+ } finally {
153
+ controller.clear()
128
154
  }
129
155
  }
130
156
 
@@ -160,6 +186,7 @@ export class Circuit implements Transport, Initializable {
160
186
 
161
187
  try {
162
188
  const virtualConnection = await hop({
189
+ ...options,
163
190
  connection: relayConnection,
164
191
  request: {
165
192
  type: CircuitPB.Type.HOP,
package/src/config.ts CHANGED
@@ -10,6 +10,7 @@ import type { Libp2pInit } from './index.js'
10
10
  import { codes, messages } from './errors.js'
11
11
  import errCode from 'err-code'
12
12
  import type { RecursivePartial } from '@libp2p/interfaces'
13
+ import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'
13
14
 
14
15
  const DefaultConfig: Partial<Libp2pInit> = {
15
16
  addresses: {
@@ -26,6 +27,7 @@ const DefaultConfig: Partial<Libp2pInit> = {
26
27
  maxParallelDials: Constants.MAX_PARALLEL_DIALS,
27
28
  maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
28
29
  dialTimeout: Constants.DIAL_TIMEOUT,
30
+ inboundUpgradeTimeout: Constants.INBOUND_UPGRADE_TIMEOUT,
29
31
  resolvers: {
30
32
  dnsaddr: dnsaddrResolver
31
33
  },
@@ -67,7 +69,8 @@ const DefaultConfig: Partial<Libp2pInit> = {
67
69
  },
68
70
  hop: {
69
71
  enabled: false,
70
- active: false
72
+ active: false,
73
+ timeout: 30000
71
74
  },
72
75
  autoRelay: {
73
76
  enabled: false,
@@ -79,13 +82,24 @@ const DefaultConfig: Partial<Libp2pInit> = {
79
82
  host: {
80
83
  agentVersion: AGENT_VERSION
81
84
  },
82
- timeout: 30000
85
+ // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L48
86
+ timeout: 60000,
87
+ maxInboundStreams: 1,
88
+ maxOutboundStreams: 1,
89
+ maxPushIncomingStreams: 1,
90
+ maxPushOutgoingStreams: 1
83
91
  },
84
92
  ping: {
85
- protocolPrefix: 'ipfs'
93
+ protocolPrefix: 'ipfs',
94
+ maxInboundStreams: 1,
95
+ maxOutboundStreams: 1,
96
+ timeout: 10000
86
97
  },
87
98
  fetch: {
88
- protocolPrefix: 'libp2p'
99
+ protocolPrefix: 'libp2p',
100
+ maxInboundStreams: 1,
101
+ maxOutboundStreams: 1,
102
+ timeout: 10000
89
103
  }
90
104
  }
91
105
 
@@ -104,5 +118,14 @@ export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit
104
118
  throw errCode(new Error(messages.ERR_PROTECTOR_REQUIRED), codes.ERR_PROTECTOR_REQUIRED)
105
119
  }
106
120
 
121
+ // Append user agent version to default AGENT_VERSION depending on the environment
122
+ if (resultingOptions.identify.host.agentVersion === AGENT_VERSION) {
123
+ if (isNode || isElectronMain) {
124
+ resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.process.version}`
125
+ } else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
126
+ resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`
127
+ }
128
+ }
129
+
107
130
  return resultingOptions
108
131
  }
@@ -7,7 +7,7 @@ import { pipe } from 'it-pipe'
7
7
  import filter from 'it-filter'
8
8
  import sort from 'it-sort'
9
9
  import type { Startable } from '@libp2p/interfaces/startable'
10
- import type { Components } from '@libp2p/interfaces/components'
10
+ import type { Components } from '@libp2p/components'
11
11
 
12
12
  const log = logger('libp2p:connection-manager:auto-dialler')
13
13
 
@@ -1,7 +1,8 @@
1
- import type { PeerInfo } from '@libp2p/interfaces/peer-info'
1
+ import type { PeerInfo } from '@libp2p/interface-peer-info'
2
2
  import { logger } from '@libp2p/logger'
3
- import type { Components } from '@libp2p/interfaces/components'
3
+ import type { Components } from '@libp2p/components'
4
4
  import { TimeoutController } from 'timeout-abort-controller'
5
+ import { setMaxListeners } from 'events'
5
6
 
6
7
  const log = logger('libp2p:dialer:auto-dialer')
7
8
 
@@ -44,6 +45,11 @@ export class AutoDialer {
44
45
 
45
46
  const controller = new TimeoutController(this.dialTimeout)
46
47
 
48
+ try {
49
+ // fails on node < 15.4
50
+ setMaxListeners?.(Infinity, controller.signal)
51
+ } catch {}
52
+
47
53
  void this.components.getConnectionManager().openConnection(peer.id, {
48
54
  signal: controller.signal
49
55
  })
@@ -5,7 +5,7 @@ import { setMaxListeners } from 'events'
5
5
  import { codes } from '../../errors.js'
6
6
  import { logger } from '@libp2p/logger'
7
7
  import type { Multiaddr } from '@multiformats/multiaddr'
8
- import type { Connection } from '@libp2p/interfaces/connection'
8
+ import type { Connection } from '@libp2p/interface-connection'
9
9
  import type { AbortOptions } from '@libp2p/interfaces'
10
10
  import type { Dialer } from './index.js'
11
11
 
@@ -18,16 +18,16 @@ import {
18
18
  MAX_PER_PEER_DIALS,
19
19
  MAX_ADDRS_TO_DIAL
20
20
  } from '../../constants.js'
21
- import type { Connection } from '@libp2p/interfaces/connection'
21
+ import type { Connection } from '@libp2p/interface-connection'
22
22
  import type { AbortOptions } from '@libp2p/interfaces'
23
23
  import type { Startable } from '@libp2p/interfaces/startable'
24
- import type { PeerId } from '@libp2p/interfaces/peer-id'
24
+ import type { PeerId } from '@libp2p/interface-peer-id'
25
25
  import { getPeer } from '../../get-peer.js'
26
26
  import sort from 'it-sort'
27
- import { Components, Initializable } from '@libp2p/interfaces/components'
27
+ import { Components, Initializable } from '@libp2p/components'
28
28
  import map from 'it-map'
29
- import type { AddressSorter } from '@libp2p/interfaces/peer-store'
30
- import type { ComponentMetricsTracker } from '@libp2p/interfaces/metrics'
29
+ import type { AddressSorter } from '@libp2p/interface-peer-store'
30
+ import type { ComponentMetricsTracker } from '@libp2p/interface-metrics'
31
31
 
32
32
  const log = logger('libp2p:dialer')
33
33
 
@@ -177,7 +177,7 @@ export class Dialer implements Startable, Initializable {
177
177
 
178
178
  log('creating dial target for %p', id)
179
179
 
180
- const dialTarget = await this._createCancellableDialTarget(id)
180
+ const dialTarget = await this._createCancellableDialTarget(id, options)
181
181
 
182
182
  if (dialTarget.addrs.length === 0) {
183
183
  throw errCode(new Error('The dial request has no valid addresses'), codes.ERR_NO_VALID_ADDRESSES)
@@ -207,7 +207,7 @@ export class Dialer implements Startable, Initializable {
207
207
  * The dial to the first address that is successfully able to upgrade a connection
208
208
  * will be used.
209
209
  */
210
- async _createCancellableDialTarget (peer: PeerId): Promise<DialTarget> {
210
+ async _createCancellableDialTarget (peer: PeerId, options: AbortOptions): Promise<DialTarget> {
211
211
  // Make dial target promise cancellable
212
212
  const id = `${(parseInt(String(Math.random() * 1e9), 10)).toString()}${Date.now()}`
213
213
  const cancellablePromise = new Promise<DialTarget>((resolve, reject) => {
@@ -216,7 +216,7 @@ export class Dialer implements Startable, Initializable {
216
216
 
217
217
  try {
218
218
  const dialTarget = await Promise.race([
219
- this._createDialTarget(peer),
219
+ this._createDialTarget(peer, options),
220
220
  cancellablePromise
221
221
  ])
222
222
 
@@ -232,7 +232,7 @@ export class Dialer implements Startable, Initializable {
232
232
  * If a multiaddr is received it should be the first address attempted.
233
233
  * Multiaddrs not supported by the available transports will be filtered out.
234
234
  */
235
- async _createDialTarget (peer: PeerId): Promise<DialTarget> {
235
+ async _createDialTarget (peer: PeerId, options: AbortOptions): Promise<DialTarget> {
236
236
  const knownAddrs = await pipe(
237
237
  await this.components.getPeerStore().addressBook.get(peer),
238
238
  (source) => filter(source, async (address) => {
@@ -253,7 +253,7 @@ export class Dialer implements Startable, Initializable {
253
253
 
254
254
  const addrs: Multiaddr[] = []
255
255
  for (const a of knownAddrs) {
256
- const resolvedAddrs = await this._resolve(a)
256
+ const resolvedAddrs = await this._resolve(a, options)
257
257
  resolvedAddrs.forEach(ra => addrs.push(ra))
258
258
  }
259
259
 
@@ -341,7 +341,7 @@ export class Dialer implements Startable, Initializable {
341
341
  /**
342
342
  * Resolve multiaddr recursively
343
343
  */
344
- async _resolve (ma: Multiaddr): Promise<Multiaddr[]> {
344
+ async _resolve (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
345
345
  // TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
346
346
  // Now only supporting resolve for dnsaddr
347
347
  const resolvableProto = ma.protoNames().includes('dnsaddr')
@@ -351,9 +351,9 @@ export class Dialer implements Startable, Initializable {
351
351
  return [ma]
352
352
  }
353
353
 
354
- const resolvedMultiaddrs = await this._resolveRecord(ma)
354
+ const resolvedMultiaddrs = await this._resolveRecord(ma, options)
355
355
  const recursiveMultiaddrs = await Promise.all(resolvedMultiaddrs.map(async (nm) => {
356
- return await this._resolve(nm)
356
+ return await this._resolve(nm, options)
357
357
  }))
358
358
 
359
359
  const addrs = recursiveMultiaddrs.flat()
@@ -368,10 +368,10 @@ export class Dialer implements Startable, Initializable {
368
368
  /**
369
369
  * Resolve a given multiaddr. If this fails, an empty array will be returned
370
370
  */
371
- async _resolveRecord (ma: Multiaddr): Promise<Multiaddr[]> {
371
+ async _resolveRecord (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
372
372
  try {
373
373
  ma = new Multiaddr(ma.toString()) // Use current multiaddr module
374
- const multiaddrs = await ma.resolve()
374
+ const multiaddrs = await ma.resolve(options)
375
375
  return multiaddrs
376
376
  } catch (err) {
377
377
  log.error(`multiaddr ${ma.toString()} could not be resolved`, err)