@@ -9,7 +9,7 @@ import { pbStream } from 'it-protobuf-stream'
9
9
import { number , object } from 'yup'
10
10
import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
11
11
import { codes } from '../../errors.js'
12
- 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'
13
13
import { StopMessage , HopMessage , Status } from '../pb/index.js'
14
14
import { RelayDiscovery , type RelayDiscoveryComponents } from './discovery.js'
15
15
import { createListener } from './listener.js'
@@ -92,7 +92,7 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
92
92
* must finish the initial protocol negotiation within this timeout in ms
93
93
* (default: 30000)
94
94
*/
95
- stopTimeout : number
95
+ stopTimeout ? : number
96
96
97
97
/**
98
98
* When creating a reservation it must complete within this number of ms
@@ -113,7 +113,7 @@ class CircuitRelayTransport implements Transport {
113
113
private readonly reservationStore : ReservationStore
114
114
private readonly maxInboundStopStreams ?: number
115
115
private readonly maxOutboundStopStreams ?: number
116
- private readonly stopTimeout : number
116
+ private readonly stopTimeout ? : number
117
117
private started : boolean
118
118
119
119
constructor ( components : CircuitRelayTransportComponents , init : CircuitRelayTransportInit ) {
@@ -256,7 +256,7 @@ class CircuitRelayTransport implements Transport {
256
256
try {
257
257
const pbstr = pbStream ( stream )
258
258
const hopstr = pbstr . pb ( HopMessage )
259
- hopstr . write ( {
259
+ await hopstr . write ( {
260
260
type : HopMessage . Type . CONNECT ,
261
261
peer : {
262
262
id : destinationPeer . toBytes ( ) ,
@@ -315,7 +315,7 @@ class CircuitRelayTransport implements Transport {
315
315
* An incoming STOP request means a remote peer wants to dial us via a relay
316
316
*/
317
317
async onStop ( { connection, stream } : IncomingStreamData ) : Promise < void > {
318
- const signal = AbortSignal . timeout ( this . stopTimeout )
318
+ const signal = AbortSignal . timeout ( this . stopTimeout ?? DEFAULT_STOP_TIMEOUT )
319
319
const pbstr = pbStream ( stream ) . pb ( StopMessage )
320
320
const request = await pbstr . read ( {
321
321
signal
@@ -325,7 +325,7 @@ class CircuitRelayTransport implements Transport {
325
325
326
326
if ( request ?. type === undefined ) {
327
327
log . error ( 'type was missing from circuit v2 stop protocol request from %s' , connection . remotePeer )
328
- pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . MALFORMED_MESSAGE } , {
328
+ await pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . MALFORMED_MESSAGE } , {
329
329
signal
330
330
} )
331
331
await stream . close ( )
@@ -335,7 +335,7 @@ class CircuitRelayTransport implements Transport {
335
335
// Validate the STOP request has the required input
336
336
if ( request . type !== StopMessage . Type . CONNECT ) {
337
337
log . error ( 'invalid stop connect request via peer %p' , connection . remotePeer )
338
- pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . UNEXPECTED_MESSAGE } , {
338
+ await pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . UNEXPECTED_MESSAGE } , {
339
339
signal
340
340
} )
341
341
await stream . close ( )
@@ -344,7 +344,7 @@ class CircuitRelayTransport implements Transport {
344
344
345
345
if ( ! isValidStop ( request ) ) {
346
346
log . error ( 'invalid stop connect request via peer %p' , connection . remotePeer )
347
- pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . MALFORMED_MESSAGE } , {
347
+ await pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . MALFORMED_MESSAGE } , {
348
348
signal
349
349
} )
350
350
await stream . close ( )
@@ -355,15 +355,15 @@ class CircuitRelayTransport implements Transport {
355
355
356
356
if ( ( await this . connectionGater . denyInboundRelayedConnection ?.( connection . remotePeer , remotePeerId ) ) === true ) {
357
357
log . error ( 'connection gater denied inbound relayed connection from %p' , connection . remotePeer )
358
- pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . PERMISSION_DENIED } , {
358
+ await pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . PERMISSION_DENIED } , {
359
359
signal
360
360
} )
361
361
await stream . close ( )
362
362
return
363
363
}
364
364
365
365
log . trace ( 'sending success response to %p' , connection . remotePeer )
366
- pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . OK } , {
366
+ await pbstr . write ( { type : StopMessage . Type . STATUS , status : Status . OK } , {
367
367
signal
368
368
} )
369
369
@@ -386,7 +386,7 @@ export function circuitRelayTransport (init?: CircuitRelayTransportInit): (compo
386
386
discoverRelays : number ( ) . min ( 0 ) . integer ( ) . default ( 0 ) ,
387
387
maxInboundStopStreams : number ( ) . min ( 0 ) . integer ( ) . default ( MAX_CONNECTIONS ) ,
388
388
maxOutboundStopStreams : number ( ) . min ( 0 ) . integer ( ) . default ( MAX_CONNECTIONS ) ,
389
- stopTimeout : number ( ) . min ( 0 ) . integer ( ) . default ( 30000 )
389
+ stopTimeout : number ( ) . min ( 0 ) . integer ( ) . default ( DEFAULT_STOP_TIMEOUT )
390
390
} ) . validateSync ( init )
391
391
392
392
return ( components ) => {
0 commit comments