@@ -6,9 +6,10 @@ import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
6
6
import * as mafmt from '@multiformats/mafmt'
7
7
import { multiaddr } from '@multiformats/multiaddr'
8
8
import { pbStream } from 'it-protobuf-stream'
9
+ import { number , object } from 'yup'
9
10
import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
10
11
import { codes } from '../../errors.js'
11
- import { CIRCUIT_PROTO_CODE , RELAY_V2_HOP_CODEC , RELAY_V2_STOP_CODEC } from '../constants.js'
12
+ import { CIRCUIT_PROTO_CODE , DEFAULT_STOP_TIMEOUT , RELAY_V2_HOP_CODEC , RELAY_V2_STOP_CODEC } from '../constants.js'
12
13
import { StopMessage , HopMessage , Status } from '../pb/index.js'
13
14
import { RelayDiscovery , type RelayDiscoveryComponents } from './discovery.js'
14
15
import { createListener } from './listener.js'
@@ -100,12 +101,6 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
100
101
reservationCompletionTimeout ?: number
101
102
}
102
103
103
- const defaults = {
104
- maxInboundStopStreams : MAX_CONNECTIONS ,
105
- maxOutboundStopStreams : MAX_CONNECTIONS ,
106
- stopTimeout : 30000
107
- }
108
-
109
104
class CircuitRelayTransport implements Transport {
110
105
private readonly discovery ?: RelayDiscovery
111
106
private readonly registrar : Registrar
@@ -116,24 +111,31 @@ class CircuitRelayTransport implements Transport {
116
111
private readonly addressManager : AddressManager
117
112
private readonly connectionGater : ConnectionGater
118
113
private readonly reservationStore : ReservationStore
119
- private readonly maxInboundStopStreams : number
114
+ private readonly maxInboundStopStreams ? : number
120
115
private readonly maxOutboundStopStreams ?: number
121
- private readonly stopTimeout : number
116
+ private readonly stopTimeout ? : number
122
117
private started : boolean
123
118
124
119
constructor ( components : CircuitRelayTransportComponents , init : CircuitRelayTransportInit ) {
120
+ const validatedConfig = object ( {
121
+ discoverRelays : number ( ) . min ( 0 ) . integer ( ) . default ( 0 ) ,
122
+ maxInboundStopStreams : number ( ) . min ( 0 ) . integer ( ) . default ( MAX_CONNECTIONS ) ,
123
+ maxOutboundStopStreams : number ( ) . min ( 0 ) . integer ( ) . default ( MAX_CONNECTIONS ) ,
124
+ stopTimeout : number ( ) . min ( 0 ) . integer ( ) . default ( DEFAULT_STOP_TIMEOUT )
125
+ } ) . validateSync ( init )
126
+
125
127
this . registrar = components . registrar
126
128
this . peerStore = components . peerStore
127
129
this . connectionManager = components . connectionManager
128
130
this . peerId = components . peerId
129
131
this . upgrader = components . upgrader
130
132
this . addressManager = components . addressManager
131
133
this . connectionGater = components . connectionGater
132
- this . maxInboundStopStreams = init . maxInboundStopStreams ?? defaults . maxInboundStopStreams
133
- this . maxOutboundStopStreams = init . maxOutboundStopStreams ?? defaults . maxOutboundStopStreams
134
- this . stopTimeout = init . stopTimeout ?? defaults . stopTimeout
134
+ this . maxInboundStopStreams = validatedConfig . maxInboundStopStreams
135
+ this . maxOutboundStopStreams = validatedConfig . maxOutboundStopStreams
136
+ this . stopTimeout = validatedConfig . stopTimeout
135
137
136
- if ( init . discoverRelays != null && init . discoverRelays > 0 ) {
138
+ if ( validatedConfig . discoverRelays > 0 ) {
137
139
this . discovery = new RelayDiscovery ( components )
138
140
this . discovery . addEventListener ( 'relay:discover' , ( evt ) => {
139
141
this . reservationStore . addRelay ( evt . detail , 'discovered' )
@@ -321,7 +323,7 @@ class CircuitRelayTransport implements Transport {
321
323
* An incoming STOP request means a remote peer wants to dial us via a relay
322
324
*/
323
325
async onStop ( { connection, stream } : IncomingStreamData ) : Promise < void > {
324
- const signal = AbortSignal . timeout ( this . stopTimeout )
326
+ const signal = AbortSignal . timeout ( this . stopTimeout ?? DEFAULT_STOP_TIMEOUT )
325
327
const pbstr = pbStream ( stream ) . pb ( StopMessage )
326
328
const request = await pbstr . read ( {
327
329
signal
0 commit comments