@@ -5,7 +5,8 @@ import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
55import * as mafmt from '@multiformats/mafmt'
66import { multiaddr } from '@multiformats/multiaddr'
77import { pbStream } from 'it-protobuf-stream'
8- import { CIRCUIT_PROTO_CODE , ERR_HOP_REQUEST_FAILED , ERR_RELAYED_DIAL , MAX_CONNECTIONS , RELAY_V2_HOP_CODEC , RELAY_V2_STOP_CODEC } from '../constants.js'
8+ import { object , number } from 'yup'
9+ import { CIRCUIT_PROTO_CODE , DEFAULT_STOP_TIMEOUT , ERR_HOP_REQUEST_FAILED , ERR_RELAYED_DIAL , MAX_CONNECTIONS , RELAY_V2_HOP_CODEC , RELAY_V2_STOP_CODEC } from '../constants.js'
910import { StopMessage , HopMessage , Status } from '../pb/index.js'
1011import { RelayDiscovery } from './discovery.js'
1112import { createListener } from './listener.js'
@@ -46,11 +47,12 @@ interface ConnectOptions {
4647 disconnectOnFailure : boolean
4748}
4849
49- const defaults = {
50- maxInboundStopStreams : MAX_CONNECTIONS ,
51- maxOutboundStopStreams : MAX_CONNECTIONS ,
52- stopTimeout : 30000
53- }
50+ const configValidator = object ( {
51+ discoverRelays : number ( ) . min ( 0 ) . integer ( ) . default ( 0 ) ,
52+ maxInboundStopStreams : number ( ) . min ( 0 ) . integer ( ) . default ( MAX_CONNECTIONS ) ,
53+ maxOutboundStopStreams : number ( ) . min ( 0 ) . integer ( ) . default ( MAX_CONNECTIONS ) ,
54+ stopTimeout : number ( ) . min ( 0 ) . integer ( ) . default ( DEFAULT_STOP_TIMEOUT )
55+ } )
5456
5557export class CircuitRelayTransport implements Transport {
5658 private readonly discovery ?: RelayDiscovery
@@ -71,6 +73,8 @@ export class CircuitRelayTransport implements Transport {
7173 private readonly log : Logger
7274
7375 constructor ( components : CircuitRelayTransportComponents , init : CircuitRelayTransportInit ) {
76+ const config = configValidator . validateSync ( init )
77+
7478 this . log = components . logger . forComponent ( 'libp2p:circuit-relay:transport' )
7579 this . registrar = components . registrar
7680 this . peerStore = components . peerStore
@@ -81,11 +85,11 @@ export class CircuitRelayTransport implements Transport {
8185 this . upgrader = components . upgrader
8286 this . addressManager = components . addressManager
8387 this . connectionGater = components . connectionGater
84- this . maxInboundStopStreams = init . maxInboundStopStreams ?? defaults . maxInboundStopStreams
85- this . maxOutboundStopStreams = init . maxOutboundStopStreams ?? defaults . maxOutboundStopStreams
86- this . stopTimeout = init . stopTimeout ?? defaults . stopTimeout
88+ this . maxInboundStopStreams = config . maxInboundStopStreams
89+ this . maxOutboundStopStreams = config . maxOutboundStopStreams
90+ this . stopTimeout = config . stopTimeout
8791
88- if ( init . discoverRelays != null && init . discoverRelays > 0 ) {
92+ if ( config . discoverRelays > 0 ) {
8993 this . discovery = new RelayDiscovery ( components )
9094 this . discovery . addEventListener ( 'relay:discover' , ( evt ) => {
9195 this . reservationStore . addRelay ( evt . detail , 'discovered' )
0 commit comments