libp2p 2.7.2-5b084e968 → 2.7.2-90cca822b

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.
@@ -1,17 +1,17 @@
1
1
  /**
2
2
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#dialTimeout
3
3
  */
4
- export const DIAL_TIMEOUT = 5e3
4
+ export const DIAL_TIMEOUT = 10_000
5
5
 
6
6
  /**
7
- * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#upgradeTimeout
7
+ * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundUpgradeTimeout
8
8
  */
9
- export const UPGRADE_TIMEOUT = 3e3
9
+ export const INBOUND_UPGRADE_TIMEOUT = 10_000
10
10
 
11
11
  /**
12
12
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#protocolNegotiationTimeout
13
13
  */
14
- export const PROTOCOL_NEGOTIATION_TIMEOUT = 2e3
14
+ export const PROTOCOL_NEGOTIATION_TIMEOUT = 10_000
15
15
 
16
16
  /**
17
17
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#maxPeerAddrsToDial
@@ -59,7 +59,7 @@ export interface ConnectionManagerInit {
59
59
  * How long a dial attempt is allowed to take, including DNS resolution
60
60
  * of the multiaddr, opening a socket and upgrading it to a Connection.
61
61
  *
62
- * @default 5000
62
+ * @default 10_000
63
63
  */
64
64
  dialTimeout?: number
65
65
 
@@ -67,7 +67,7 @@ export interface ConnectionManagerInit {
67
67
  * When a new incoming connection is opened, the upgrade process (e.g.
68
68
  * protect, encrypt, multiplex etc) must complete within this number of ms.
69
69
  *
70
- * @default 3000
70
+ * @default 10_000
71
71
  */
72
72
  inboundUpgradeTimeout?: number
73
73
 
@@ -77,7 +77,7 @@ export interface ConnectionManagerInit {
77
77
  *
78
78
  * Does not apply if an abort signal is passed to the `.dial` method.
79
79
  *
80
- * @default 3000
80
+ * @deprecated This is handled by `dialTimeout`
81
81
  */
82
82
  outboundUpgradeTimeout?: number
83
83
 
@@ -92,16 +92,18 @@ export interface ConnectionManagerInit {
92
92
  /**
93
93
  * Outbound protocol negotiation must complete within this number of ms.
94
94
  *
95
- * Does not apply if an abort signal is passed to the `.dial` method.
95
+ * Does not apply if an abort signal is passed to the `.dial` or
96
+ * `.dialProtocol` method of the `ConnectionManager` or the `openStream`
97
+ * method of the `Connection`.
96
98
  *
97
- * @default 2000
99
+ * @default 10_000
98
100
  */
99
101
  outboundStreamProtocolNegotiationTimeout?: number
100
102
 
101
103
  /**
102
104
  * Inbound protocol negotiation must complete within this number of ms
103
105
  *
104
- * @default 2000
106
+ * @default 10_000
105
107
  */
106
108
  inboundStreamProtocolNegotiationTimeout?: number
107
109
 
package/src/libp2p.ts CHANGED
@@ -18,7 +18,7 @@ import { DefaultPeerRouting } from './peer-routing.js'
18
18
  import { RandomWalk } from './random-walk.js'
19
19
  import { Registrar } from './registrar.js'
20
20
  import { DefaultTransportManager } from './transport-manager.js'
21
- import { DefaultUpgrader } from './upgrader.js'
21
+ import { Upgrader } from './upgrader.js'
22
22
  import { userAgent } from './user-agent.js'
23
23
  import * as pkg from './version.js'
24
24
  import type { Components } from './components.js'
@@ -113,11 +113,10 @@ export class Libp2p<T extends ServiceMap = ServiceMap> extends TypedEventEmitter
113
113
  }
114
114
 
115
115
  // Set up the Upgrader
116
- this.components.upgrader = new DefaultUpgrader(this.components, {
116
+ this.components.upgrader = new Upgrader(this.components, {
117
117
  connectionEncrypters: (init.connectionEncrypters ?? []).map((fn, index) => this.configureComponent(`connection-encryption-${index}`, fn(this.components))),
118
118
  streamMuxers: (init.streamMuxers ?? []).map((fn, index) => this.configureComponent(`stream-muxers-${index}`, fn(this.components))),
119
119
  inboundUpgradeTimeout: init.connectionManager?.inboundUpgradeTimeout,
120
- outboundUpgradeTimeout: init.connectionManager?.outboundUpgradeTimeout,
121
120
  inboundStreamProtocolNegotiationTimeout: init.connectionManager?.inboundStreamProtocolNegotiationTimeout ?? init.connectionManager?.protocolNegotiationTimeout,
122
121
  outboundStreamProtocolNegotiationTimeout: init.connectionManager?.outboundStreamProtocolNegotiationTimeout ?? init.connectionManager?.protocolNegotiationTimeout
123
122
  })
package/src/upgrader.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import { InvalidMultiaddrError, TooManyInboundProtocolStreamsError, TooManyOutboundProtocolStreamsError, LimitedConnectionError, setMaxListeners, InvalidPeerIdError } from '@libp2p/interface'
2
2
  import * as mss from '@libp2p/multistream-select'
3
3
  import { peerIdFromString } from '@libp2p/peer-id'
4
+ import { anySignal } from 'any-signal'
4
5
  import { CustomProgressEvent } from 'progress-events'
5
6
  import { createConnection } from './connection/index.js'
6
- import { PROTOCOL_NEGOTIATION_TIMEOUT, UPGRADE_TIMEOUT } from './connection-manager/constants.js'
7
+ import { PROTOCOL_NEGOTIATION_TIMEOUT, INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
7
8
  import { ConnectionDeniedError, ConnectionInterceptedError, EncryptionFailedError, MuxerUnavailableError } from './errors.js'
8
9
  import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
9
- import type { Libp2pEvents, AbortOptions, ComponentLogger, MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions, ConnectionEncrypter, SecuredConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, PeerStore, StreamMuxer, StreamMuxerFactory, Upgrader, UpgraderOptions, ConnectionLimits, SecureConnectionOptions, CounterGroup } from '@libp2p/interface'
10
+ import type { Libp2pEvents, AbortOptions, ComponentLogger, MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions, ConnectionEncrypter, SecuredConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, PeerStore, StreamMuxer, StreamMuxerFactory, Upgrader as UpgraderInterface, UpgraderOptions, ConnectionLimits, SecureConnectionOptions, CounterGroup, ClearableSignal } from '@libp2p/interface'
10
11
  import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
11
12
 
12
13
  interface CreateConnectionOptions {
@@ -40,13 +41,6 @@ export interface UpgraderInit {
40
41
  */
41
42
  inboundUpgradeTimeout?: number
42
43
 
43
- /**
44
- * An amount of ms by which an outbound connection upgrade must complete
45
- *
46
- * @default 3000
47
- */
48
- outboundUpgradeTimeout?: number
49
-
50
44
  /**
51
45
  * When a new incoming stream is opened on a multiplexed connection, protocol
52
46
  * negotiation on that stream must complete within this many ms
@@ -120,12 +114,11 @@ export interface DefaultUpgraderComponents {
120
114
 
121
115
  type ConnectionDeniedType = keyof Pick<ConnectionGater, 'denyOutboundConnection' | 'denyInboundEncryptedConnection' | 'denyOutboundEncryptedConnection' | 'denyInboundUpgradedConnection' | 'denyOutboundUpgradedConnection'>
122
116
 
123
- export class DefaultUpgrader implements Upgrader {
117
+ export class Upgrader implements UpgraderInterface {
124
118
  private readonly components: DefaultUpgraderComponents
125
119
  private readonly connectionEncrypters: Map<string, ConnectionEncrypter>
126
120
  private readonly streamMuxers: Map<string, StreamMuxerFactory>
127
121
  private readonly inboundUpgradeTimeout: number
128
- private readonly outboundUpgradeTimeout: number
129
122
  private readonly inboundStreamProtocolNegotiationTimeout: number
130
123
  private readonly outboundStreamProtocolNegotiationTimeout: number
131
124
  private readonly events: TypedEventTarget<Libp2pEvents>
@@ -148,8 +141,7 @@ export class DefaultUpgrader implements Upgrader {
148
141
  this.streamMuxers.set(muxer.protocol, muxer)
149
142
  })
150
143
 
151
- this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ?? UPGRADE_TIMEOUT
152
- this.outboundUpgradeTimeout = init.outboundUpgradeTimeout ?? UPGRADE_TIMEOUT
144
+ this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ?? INBOUND_UPGRADE_TIMEOUT
153
145
  this.inboundStreamProtocolNegotiationTimeout = init.inboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
154
146
  this.outboundStreamProtocolNegotiationTimeout = init.outboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
155
147
  this.events = components.events
@@ -177,12 +169,25 @@ export class DefaultUpgrader implements Upgrader {
177
169
  }
178
170
  }
179
171
 
172
+ createInboundAbortSignal (signal: AbortSignal): ClearableSignal {
173
+ const output = anySignal([
174
+ AbortSignal.timeout(this.inboundUpgradeTimeout),
175
+ signal
176
+ ])
177
+ setMaxListeners(Infinity, output)
178
+
179
+ return output
180
+ }
181
+
180
182
  /**
181
183
  * Upgrades an inbound connection
182
184
  */
183
- async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderOptions = {}): Promise<void> {
185
+ async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderOptions): Promise<void> {
184
186
  let accepted = false
185
187
 
188
+ // always apply upgrade timeout for incoming upgrades
189
+ const signal = this.createInboundAbortSignal(opts.signal)
190
+
186
191
  try {
187
192
  this.metrics.dials?.increment({
188
193
  inbound: true
@@ -196,7 +201,10 @@ export class DefaultUpgrader implements Upgrader {
196
201
 
197
202
  await this.shouldBlockConnection('denyInboundConnection', maConn)
198
203
 
199
- await this._performUpgrade(maConn, 'inbound', opts)
204
+ await this._performUpgrade(maConn, 'inbound', {
205
+ ...opts,
206
+ signal
207
+ })
200
208
  } catch (err) {
201
209
  this.metrics.errors?.increment({
202
210
  inbound: true
@@ -204,6 +212,8 @@ export class DefaultUpgrader implements Upgrader {
204
212
 
205
213
  throw err
206
214
  } finally {
215
+ signal.clear()
216
+
207
217
  if (accepted) {
208
218
  this.components.connectionManager.afterUpgradeInbound()
209
219
  }
@@ -213,7 +223,7 @@ export class DefaultUpgrader implements Upgrader {
213
223
  /**
214
224
  * Upgrades an outbound connection
215
225
  */
216
- async upgradeOutbound (maConn: MultiaddrConnection, opts: UpgraderOptions = {}): Promise<Connection> {
226
+ async upgradeOutbound (maConn: MultiaddrConnection, opts: UpgraderOptions): Promise<Connection> {
217
227
  try {
218
228
  this.metrics.dials?.increment({
219
229
  outbound: true
@@ -251,14 +261,6 @@ export class DefaultUpgrader implements Upgrader {
251
261
  let muxerFactory: StreamMuxerFactory | undefined
252
262
  let cryptoProtocol
253
263
 
254
- if (opts.signal == null) {
255
- maConn.log('no abort signal was passed while trying to upgrade connection, falling back to default timeout')
256
-
257
- const upgradeTimeoutSignal = AbortSignal.timeout(direction === 'inbound' ? this.inboundUpgradeTimeout : this.outboundUpgradeTimeout)
258
- setMaxListeners(Infinity, upgradeTimeoutSignal)
259
- opts.signal = upgradeTimeoutSignal
260
- }
261
-
262
264
  this.components.metrics?.trackMultiaddrConnection(maConn)
263
265
 
264
266
  maConn.log.trace('starting the %s connection upgrade', direction)
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '2.7.2-5b084e968'
1
+ export const version = '2.7.2-90cca822b'
2
2
  export const name = 'js-libp2p'