libp2p 0.45.8 → 0.45.9-3dfc236e

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.
@@ -6,6 +6,7 @@ import { RecordEnvelope } from '@libp2p/peer-record'
6
6
  import { type Multiaddr, multiaddr } from '@multiformats/multiaddr'
7
7
  import { pbStream, type ProtobufStream } from 'it-pb-stream'
8
8
  import pDefer from 'p-defer'
9
+ import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
9
10
  import {
10
11
  CIRCUIT_PROTO_CODE,
11
12
  DEFAULT_HOP_TIMEOUT,
@@ -59,6 +60,12 @@ export interface CircuitRelayServerInit {
59
60
  * The maximum number of simultaneous HOP outbound streams that can be open at once
60
61
  */
61
62
  maxOutboundHopStreams?: number
63
+
64
+ /**
65
+ * The maximum number of simultaneous STOP outbound streams that can be open at
66
+ * once. (default: 300)
67
+ */
68
+ maxOutboundStopStreams?: number
62
69
  }
63
70
 
64
71
  export interface HopProtocolOptions {
@@ -87,6 +94,10 @@ export interface RelayServerEvents {
87
94
  'relay:advert:error': CustomEvent<Error>
88
95
  }
89
96
 
97
+ const defaults = {
98
+ maxOutboundStopStreams: MAX_CONNECTIONS
99
+ }
100
+
90
101
  class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Startable, CircuitRelayService {
91
102
  private readonly registrar: Registrar
92
103
  private readonly peerStore: PeerStore
@@ -101,6 +112,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
101
112
  private readonly shutdownController: AbortController
102
113
  private readonly maxInboundHopStreams?: number
103
114
  private readonly maxOutboundHopStreams?: number
115
+ private readonly maxOutboundStopStreams: number
104
116
 
105
117
  /**
106
118
  * Creates an instance of Relay
@@ -119,6 +131,7 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
119
131
  this.shutdownController = new AbortController()
120
132
  this.maxInboundHopStreams = init.maxInboundHopStreams
121
133
  this.maxOutboundHopStreams = init.maxOutboundHopStreams
134
+ this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
122
135
 
123
136
  try {
124
137
  // fails on node < 15.4
@@ -390,7 +403,9 @@ class CircuitRelayServer extends EventEmitter<RelayServerEvents> implements Star
390
403
  request
391
404
  }: StopOptions): Promise<Stream | undefined> {
392
405
  log('starting circuit relay v2 stop request to %s', connection.remotePeer)
393
- const stream = await connection.newStream([RELAY_V2_STOP_CODEC])
406
+ const stream = await connection.newStream([RELAY_V2_STOP_CODEC], {
407
+ maxOutboundStreams: this.maxOutboundStopStreams
408
+ })
394
409
  const pbstr = pbStream(stream)
395
410
  const stopstr = pbstr.pb(StopMessage)
396
411
  stopstr.write(request)
@@ -82,14 +82,15 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
82
82
 
83
83
  /**
84
84
  * The maximum number of simultaneous STOP outbound streams that can be open at
85
- * once. STOP streams are opened by the relay server so this setting is
86
- * effectively ignored. (default: 32)
85
+ * once. If this transport is used along with the relay server these settings
86
+ * should be set to the same value (default: 300)
87
87
  */
88
88
  maxOutboundStopStreams?: number
89
89
  }
90
90
 
91
91
  const defaults = {
92
- maxInboundStopStreams: MAX_CONNECTIONS
92
+ maxInboundStopStreams: MAX_CONNECTIONS,
93
+ maxOutboundStopStreams: MAX_CONNECTIONS
93
94
  }
94
95
 
95
96
  class CircuitRelayTransport implements Transport {
@@ -115,7 +116,7 @@ class CircuitRelayTransport implements Transport {
115
116
  this.addressManager = components.addressManager
116
117
  this.connectionGater = components.connectionGater
117
118
  this.maxInboundStopStreams = init.maxInboundStopStreams ?? defaults.maxInboundStopStreams
118
- this.maxOutboundStopStreams = init.maxOutboundStopStreams
119
+ this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
119
120
 
120
121
  if (init.discoverRelays != null && init.discoverRelays > 0) {
121
122
  this.discovery = new RelayDiscovery(components)
package/src/upgrader.ts CHANGED
@@ -9,7 +9,7 @@ import { createConnection } from './connection/index.js'
9
9
  import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
10
10
  import { codes } from './errors.js'
11
11
  import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
12
- import type { MultiaddrConnection, Connection, Stream, ConnectionProtector } from '@libp2p/interface-connection'
12
+ import type { MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions } from '@libp2p/interface-connection'
13
13
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
14
14
  import type { ConnectionGater } from '@libp2p/interface-connection-gater'
15
15
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
@@ -70,17 +70,20 @@ function findIncomingStreamLimit (protocol: string, registrar: Registrar): numbe
70
70
  return DEFAULT_MAX_INBOUND_STREAMS
71
71
  }
72
72
 
73
- function findOutgoingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
73
+ function findOutgoingStreamLimit (protocol: string, registrar: Registrar, options: NewStreamOptions = {}): number {
74
74
  try {
75
75
  const { options } = registrar.getHandler(protocol)
76
- return options.maxOutboundStreams
76
+
77
+ if (options.maxOutboundStreams != null) {
78
+ return options.maxOutboundStreams
79
+ }
77
80
  } catch (err: any) {
78
81
  if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
79
82
  throw err
80
83
  }
81
84
  }
82
85
 
83
- return DEFAULT_MAX_OUTBOUND_STREAMS
86
+ return options.maxOutboundStreams ?? DEFAULT_MAX_OUTBOUND_STREAMS
84
87
  }
85
88
 
86
89
  function countStreams (protocol: string, direction: 'inbound' | 'outbound', connection: Connection): number {
@@ -428,7 +431,7 @@ export class DefaultUpgrader implements Upgrader {
428
431
  }
429
432
  })
430
433
 
431
- newStream = async (protocols: string[], options: AbortOptions = {}): Promise<Stream> => {
434
+ newStream = async (protocols: string[], options: NewStreamOptions = {}): Promise<Stream> => {
432
435
  if (muxer == null) {
433
436
  throw new CodeError('Stream is not multiplexed', codes.ERR_MUXER_UNAVAILABLE)
434
437
  }
@@ -450,10 +453,10 @@ export class DefaultUpgrader implements Upgrader {
450
453
 
451
454
  const { stream, protocol } = await mss.select(muxedStream, protocols, options)
452
455
 
453
- const outgoingLimit = findOutgoingStreamLimit(protocol, this.components.registrar)
456
+ const outgoingLimit = findOutgoingStreamLimit(protocol, this.components.registrar, options)
454
457
  const streamCount = countStreams(protocol, 'outbound', connection)
455
458
 
456
- if (streamCount === outgoingLimit) {
459
+ if (streamCount >= outgoingLimit) {
457
460
  const err = new CodeError(`Too many outbound protocol streams for protocol "${protocol}" - limit ${outgoingLimit}`, codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS)
458
461
  muxedStream.abort(err)
459
462
 
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.45.8'
1
+ export const version = '0.45.9-3dfc236e'
2
2
  export const name = 'libp2p'