@@ -10,6 +10,7 @@ import type {
10
10
StreamCodeToReason ,
11
11
StreamId ,
12
12
StreamReasonToCode ,
13
+ VerifyCallback ,
13
14
} from './types' ;
14
15
import type { Connection , ConnectionErrorCode , SendInfo } from './native/types' ;
15
16
import { Lock , LockBox , Monitor , RWLockWriter } from '@matrixai/async-locks' ;
@@ -30,9 +31,6 @@ import * as utils from './utils';
30
31
import { never } from './utils' ;
31
32
import * as errors from './errors' ;
32
33
33
- // FIXME
34
- type VerifyCallback = ( certs : Array < string > ) => void ;
35
-
36
34
/**
37
35
* Think of this as equivalent to `net.Socket`.
38
36
* Errors here are emitted to the connection only.
@@ -361,58 +359,63 @@ class QUICConnection extends EventTarget {
361
359
public async start ( @context ctx : ContextTimed ) : Promise < void > {
362
360
this . logger . info ( `Start ${ this . constructor . name } ` ) ;
363
361
ctx . signal . throwIfAborted ( ) ;
364
- ctx . signal . addEventListener ( 'abort' , ( r ) => {
362
+ const abortHandler = ( r ) => {
365
363
this . rejectEstablishedP ( r ) ;
366
364
this . rejectSecureEstablishedP ( r ) ;
367
365
368
366
// Is this actually true?
369
367
// Technically the connection is closed
370
368
this . rejectClosedP ( r ) ;
371
- } ) ;
369
+ } ;
370
+ ctx . signal . addEventListener ( 'abort' , abortHandler ) ;
372
371
// Set the connection up
373
372
this . socket . connectionMap . set ( this . connectionId , this ) ;
374
373
// Waits for the first short packet after establishment
375
374
// This ensures that TLS has been established and verified on both sides
376
375
await this . send ( ) ;
377
- await this . secureEstablishedP . catch ( ( e ) => {
378
- this . socket . connectionMap . delete ( this . connectionId ) ;
376
+ await this . secureEstablishedP
377
+ . catch ( ( e ) => {
378
+ this . socket . connectionMap . delete ( this . connectionId ) ;
379
379
380
- if ( this . conn . isTimedOut ( ) ) {
381
- // We don't dispatch an event here, it was already done in the timeout.
382
- throw new errors . ErrorQUICConnectionStartTimeOut ( ) ;
383
- }
380
+ if ( this . conn . isTimedOut ( ) ) {
381
+ // We don't dispatch an event here, it was already done in the timeout.
382
+ throw new errors . ErrorQUICConnectionStartTimeOut ( ) ;
383
+ }
384
384
385
- // Emit error if local error
386
- const localError = this . conn . localError ( ) ;
387
- if ( localError != null ) {
388
- const message = `connection start failed with localError ${ Buffer . from (
389
- localError . reason ,
390
- ) . toString ( ) } (${ localError . errorCode } )`;
391
- this . logger . info ( message ) ;
392
- throw new errors . ErrorQUICConnectionInternal ( message , {
393
- data : {
394
- type : 'local' ,
395
- ...localError ,
396
- } ,
397
- } ) ;
398
- }
399
- // Emit error if peer error
400
- const peerError = this . conn . peerError ( ) ;
401
- if ( peerError != null ) {
402
- const message = `Connection start failed with peerError ${ Buffer . from (
403
- peerError . reason ,
404
- ) . toString ( ) } (${ peerError . errorCode } )`;
405
- this . logger . info ( message ) ;
406
- throw new errors . ErrorQUICConnectionInternal ( message , {
407
- data : {
408
- type : 'local' ,
409
- ...peerError ,
410
- } ,
411
- } ) ;
412
- }
413
- // Throw the default error if none of the above were true, this shouldn't really happen
414
- throw e ;
415
- } ) ;
385
+ // Emit error if local error
386
+ const localError = this . conn . localError ( ) ;
387
+ if ( localError != null ) {
388
+ const message = `connection start failed with localError ${ Buffer . from (
389
+ localError . reason ,
390
+ ) . toString ( ) } (${ localError . errorCode } )`;
391
+ this . logger . info ( message ) ;
392
+ throw new errors . ErrorQUICConnectionInternal ( message , {
393
+ data : {
394
+ type : 'local' ,
395
+ ...localError ,
396
+ } ,
397
+ } ) ;
398
+ }
399
+ // Emit error if peer error
400
+ const peerError = this . conn . peerError ( ) ;
401
+ if ( peerError != null ) {
402
+ const message = `Connection start failed with peerError ${ Buffer . from (
403
+ peerError . reason ,
404
+ ) . toString ( ) } (${ peerError . errorCode } )`;
405
+ this . logger . info ( message ) ;
406
+ throw new errors . ErrorQUICConnectionInternal ( message , {
407
+ data : {
408
+ type : 'local' ,
409
+ ...peerError ,
410
+ } ,
411
+ } ) ;
412
+ }
413
+ // Throw the default error if none of the above were true, this shouldn't really happen
414
+ throw e ;
415
+ } )
416
+ . finally ( ( ) => {
417
+ ctx . signal . removeEventListener ( 'abort' , abortHandler ) ;
418
+ } ) ;
416
419
this . logger . warn ( 'secured' ) ;
417
420
// After this is done
418
421
// We need to established the keep alive interval time
@@ -937,7 +940,7 @@ class QUICConnection extends EventTarget {
937
940
const timeout = this . conn . timeout ( ) ;
938
941
// If this is `null`, then technically there's nothing to do
939
942
if ( timeout == null ) return ;
940
- // Allow an extra 1ms for the delay to fully complete so we can avoid a repeated 0ms delay
943
+ // Allow an extra 1ms for the delay to fully complete, so we can avoid a repeated 0ms delay
941
944
this . connTimeOutTimer = new Timer ( {
942
945
delay : timeout + 1 ,
943
946
handler : connTimeOutHandler ,
@@ -948,14 +951,18 @@ class QUICConnection extends EventTarget {
948
951
// If this is `null` there's nothing to do
949
952
if ( timeout == null ) return ;
950
953
// If there was an existing timer, we cancel it and set a new one
951
- if ( this . connTimeOutTimer != null ) {
952
- this . connTimeOutTimer . cancel ( ) ;
954
+ if (
955
+ this . connTimeOutTimer != null &&
956
+ this . connTimeOutTimer . status === null
957
+ ) {
958
+ this . connTimeOutTimer . reset ( timeout ) ;
959
+ } else {
960
+ this . logger . debug ( `timeout created with delay ${ timeout } ` ) ;
961
+ this . connTimeOutTimer = new Timer ( {
962
+ delay : timeout + 1 ,
963
+ handler : connTimeOutHandler ,
964
+ } ) ;
953
965
}
954
- this . logger . debug ( `timeout created with delay ${ timeout } ` ) ;
955
- this . connTimeOutTimer = new Timer ( {
956
- delay : timeout ,
957
- handler : connTimeOutHandler ,
958
- } ) ;
959
966
}
960
967
961
968
/**
0 commit comments