@@ -6,10 +6,9 @@ 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'
109import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
1110import { codes } from '../../errors.js'
12- import { CIRCUIT_PROTO_CODE , DEFAULT_STOP_TIMEOUT , RELAY_V2_HOP_CODEC , RELAY_V2_STOP_CODEC } from '../constants.js'
11+ import { CIRCUIT_PROTO_CODE , RELAY_V2_HOP_CODEC , RELAY_V2_STOP_CODEC } from '../constants.js'
1312import { StopMessage , HopMessage , Status } from '../pb/index.js'
1413import { RelayDiscovery , type RelayDiscoveryComponents } from './discovery.js'
1514import { createListener } from './listener.js'
@@ -101,6 +100,12 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
101100 reservationCompletionTimeout ?: number
102101}
103102
103+ const defaults = {
104+ maxInboundStopStreams : MAX_CONNECTIONS ,
105+ maxOutboundStopStreams : MAX_CONNECTIONS ,
106+ stopTimeout : 30000
107+ }
108+
104109class CircuitRelayTransport implements Transport {
105110 private readonly discovery ?: RelayDiscovery
106111 private readonly registrar : Registrar
@@ -111,31 +116,24 @@ class CircuitRelayTransport implements Transport {
111116 private readonly addressManager : AddressManager
112117 private readonly connectionGater : ConnectionGater
113118 private readonly reservationStore : ReservationStore
114- private readonly maxInboundStopStreams ? : number
119+ private readonly maxInboundStopStreams : number
115120 private readonly maxOutboundStopStreams ?: number
116- private readonly stopTimeout ? : number
121+ private readonly stopTimeout : number
117122 private started : boolean
118123
119124 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-
127125 this . registrar = components . registrar
128126 this . peerStore = components . peerStore
129127 this . connectionManager = components . connectionManager
130128 this . peerId = components . peerId
131129 this . upgrader = components . upgrader
132130 this . addressManager = components . addressManager
133131 this . connectionGater = components . connectionGater
134- this . maxInboundStopStreams = validatedConfig . maxInboundStopStreams
135- this . maxOutboundStopStreams = validatedConfig . maxOutboundStopStreams
136- this . stopTimeout = validatedConfig . stopTimeout
132+ this . maxInboundStopStreams = init . maxInboundStopStreams ?? defaults . maxInboundStopStreams
133+ this . maxOutboundStopStreams = init . maxOutboundStopStreams ?? defaults . maxOutboundStopStreams
134+ this . stopTimeout = init . stopTimeout ?? defaults . stopTimeout
137135
138- if ( validatedConfig . discoverRelays > 0 ) {
136+ if ( init . discoverRelays != null && init . discoverRelays > 0 ) {
139137 this . discovery = new RelayDiscovery ( components )
140138 this . discovery . addEventListener ( 'relay:discover' , ( evt ) => {
141139 this . reservationStore . addRelay ( evt . detail , 'discovered' )
@@ -323,7 +321,7 @@ class CircuitRelayTransport implements Transport {
323321 * An incoming STOP request means a remote peer wants to dial us via a relay
324322 */
325323 async onStop ( { connection, stream } : IncomingStreamData ) : Promise < void > {
326- const signal = AbortSignal . timeout ( this . stopTimeout ?? DEFAULT_STOP_TIMEOUT )
324+ const signal = AbortSignal . timeout ( this . stopTimeout )
327325 const pbstr = pbStream ( stream ) . pb ( StopMessage )
328326 const request = await pbstr . read ( {
329327 signal
0 commit comments