@@ -6,9 +6,10 @@ import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
66import * as mafmt from '@multiformats/mafmt'
77import { multiaddr } from '@multiformats/multiaddr'
88import { pbStream } from 'it-protobuf-stream'
9+ import { number , object } from 'yup'
910import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
1011import { 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'
1213import { StopMessage , HopMessage , Status } from '../pb/index.js'
1314import { RelayDiscovery , type RelayDiscoveryComponents } from './discovery.js'
1415import { createListener } from './listener.js'
@@ -100,12 +101,6 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
100101 reservationCompletionTimeout ?: number
101102}
102103
103- const defaults = {
104- maxInboundStopStreams : MAX_CONNECTIONS ,
105- maxOutboundStopStreams : MAX_CONNECTIONS ,
106- stopTimeout : 30000
107- }
108-
109104class CircuitRelayTransport implements Transport {
110105 private readonly discovery ?: RelayDiscovery
111106 private readonly registrar : Registrar
@@ -116,24 +111,31 @@ class CircuitRelayTransport implements Transport {
116111 private readonly addressManager : AddressManager
117112 private readonly connectionGater : ConnectionGater
118113 private readonly reservationStore : ReservationStore
119- private readonly maxInboundStopStreams : number
114+ private readonly maxInboundStopStreams ? : number
120115 private readonly maxOutboundStopStreams ?: number
121- private readonly stopTimeout : number
116+ private readonly stopTimeout ? : number
122117 private started : boolean
123118
124119 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+
125127 this . registrar = components . registrar
126128 this . peerStore = components . peerStore
127129 this . connectionManager = components . connectionManager
128130 this . peerId = components . peerId
129131 this . upgrader = components . upgrader
130132 this . addressManager = components . addressManager
131133 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
135137
136- if ( init . discoverRelays != null && init . discoverRelays > 0 ) {
138+ if ( validatedConfig . discoverRelays > 0 ) {
137139 this . discovery = new RelayDiscovery ( components )
138140 this . discovery . addEventListener ( 'relay:discover' , ( evt ) => {
139141 this . reservationStore . addRelay ( evt . detail , 'discovered' )
@@ -321,7 +323,7 @@ class CircuitRelayTransport implements Transport {
321323 * An incoming STOP request means a remote peer wants to dial us via a relay
322324 */
323325 async onStop ( { connection, stream } : IncomingStreamData ) : Promise < void > {
324- const signal = AbortSignal . timeout ( this . stopTimeout )
326+ const signal = AbortSignal . timeout ( this . stopTimeout ?? DEFAULT_STOP_TIMEOUT )
325327 const pbstr = pbStream ( stream ) . pb ( StopMessage )
326328 const request = await pbstr . read ( {
327329 signal
0 commit comments