libp2p 2.7.2 → 2.7.3

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 (45) hide show
  1. package/README.md +3 -3
  2. package/dist/index.min.js +15 -15
  3. package/dist/src/address-manager/index.js +8 -8
  4. package/dist/src/address-manager/index.js.map +1 -1
  5. package/dist/src/config/connection-gater.browser.d.ts +1 -1
  6. package/dist/src/config/connection-gater.browser.js +1 -1
  7. package/dist/src/connection-manager/address-sorter.js +4 -4
  8. package/dist/src/connection-manager/constants.defaults.d.ts +4 -4
  9. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  10. package/dist/src/connection-manager/constants.defaults.js +4 -4
  11. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  12. package/dist/src/connection-manager/dial-queue.js +4 -4
  13. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  14. package/dist/src/connection-manager/index.d.ts +8 -6
  15. package/dist/src/connection-manager/index.d.ts.map +1 -1
  16. package/dist/src/connection-manager/index.js.map +1 -1
  17. package/dist/src/errors.d.ts +3 -0
  18. package/dist/src/errors.d.ts.map +1 -1
  19. package/dist/src/errors.js +6 -0
  20. package/dist/src/errors.js.map +1 -1
  21. package/dist/src/index.d.ts +1 -1
  22. package/dist/src/libp2p.d.ts.map +1 -1
  23. package/dist/src/libp2p.js +2 -3
  24. package/dist/src/libp2p.js.map +1 -1
  25. package/dist/src/random-walk.js +2 -2
  26. package/dist/src/random-walk.js.map +1 -1
  27. package/dist/src/upgrader.d.ts +5 -11
  28. package/dist/src/upgrader.d.ts.map +1 -1
  29. package/dist/src/upgrader.js +25 -18
  30. package/dist/src/upgrader.js.map +1 -1
  31. package/dist/src/version.d.ts +1 -1
  32. package/dist/src/version.js +1 -1
  33. package/package.json +11 -11
  34. package/src/address-manager/index.ts +8 -8
  35. package/src/config/connection-gater.browser.ts +1 -1
  36. package/src/connection-manager/address-sorter.ts +4 -4
  37. package/src/connection-manager/constants.defaults.ts +4 -4
  38. package/src/connection-manager/dial-queue.ts +4 -4
  39. package/src/connection-manager/index.ts +8 -6
  40. package/src/errors.ts +7 -0
  41. package/src/index.ts +1 -1
  42. package/src/libp2p.ts +2 -3
  43. package/src/random-walk.ts +2 -2
  44. package/src/upgrader.ts +30 -28
  45. package/src/version.ts +1 -1
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)
@@ -413,7 +415,7 @@ export class DefaultUpgrader implements Upgrader {
413
415
  }
414
416
 
415
417
  // after the handshake the returned stream can have early data so override
416
- // the souce/sink
418
+ // the source/sink
417
419
  muxedStream.source = stream.source
418
420
  muxedStream.sink = stream.sink
419
421
  muxedStream.protocol = protocol
@@ -434,7 +436,7 @@ export class DefaultUpgrader implements Upgrader {
434
436
  }
435
437
 
436
438
  // If a protocol stream has been successfully negotiated and is to be passed to the application,
437
- // the peerstore should ensure that the peer is registered with that protocol
439
+ // the peer store should ensure that the peer is registered with that protocol
438
440
  await this.components.peerStore.merge(remotePeer, {
439
441
  protocols: [protocol]
440
442
  })
@@ -499,13 +501,13 @@ export class DefaultUpgrader implements Upgrader {
499
501
  }
500
502
 
501
503
  // If a protocol stream has been successfully negotiated and is to be passed to the application,
502
- // the peerstore should ensure that the peer is registered with that protocol
504
+ // the peer store should ensure that the peer is registered with that protocol
503
505
  await this.components.peerStore.merge(remotePeer, {
504
506
  protocols: [protocol]
505
507
  })
506
508
 
507
509
  // after the handshake the returned stream can have early data so override
508
- // the souce/sink
510
+ // the source/sink
509
511
  muxedStream.source = stream.source
510
512
  muxedStream.sink = stream.sink
511
513
  muxedStream.protocol = protocol
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '2.7.2'
1
+ export const version = '2.7.3'
2
2
  export const name = 'js-libp2p'