@@ -10,6 +10,7 @@ import type {
1010 StreamCodeToReason ,
1111 StreamId ,
1212 StreamReasonToCode ,
13+ VerifyCallback ,
1314} from './types' ;
1415import type { Connection , ConnectionErrorCode , SendInfo } from './native/types' ;
1516import { Lock , LockBox , Monitor , RWLockWriter } from '@matrixai/async-locks' ;
@@ -30,9 +31,6 @@ import * as utils from './utils';
3031import { never } from './utils' ;
3132import * as errors from './errors' ;
3233
33- // FIXME
34- type VerifyCallback = ( certs : Array < string > ) => void ;
35-
3634/**
3735 * Think of this as equivalent to `net.Socket`.
3836 * Errors here are emitted to the connection only.
@@ -361,58 +359,63 @@ class QUICConnection extends EventTarget {
361359 public async start ( @context ctx : ContextTimed ) : Promise < void > {
362360 this . logger . info ( `Start ${ this . constructor . name } ` ) ;
363361 ctx . signal . throwIfAborted ( ) ;
364- ctx . signal . addEventListener ( 'abort' , ( r ) => {
362+ const abortHandler = ( r ) => {
365363 this . rejectEstablishedP ( r ) ;
366364 this . rejectSecureEstablishedP ( r ) ;
367365
368366 // Is this actually true?
369367 // Technically the connection is closed
370368 this . rejectClosedP ( r ) ;
371- } ) ;
369+ } ;
370+ ctx . signal . addEventListener ( 'abort' , abortHandler ) ;
372371 // Set the connection up
373372 this . socket . connectionMap . set ( this . connectionId , this ) ;
374373 // Waits for the first short packet after establishment
375374 // This ensures that TLS has been established and verified on both sides
376375 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 ) ;
379379
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+ }
384384
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+ } ) ;
416419 this . logger . warn ( 'secured' ) ;
417420 // After this is done
418421 // We need to established the keep alive interval time
@@ -937,7 +940,7 @@ class QUICConnection extends EventTarget {
937940 const timeout = this . conn . timeout ( ) ;
938941 // If this is `null`, then technically there's nothing to do
939942 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
941944 this . connTimeOutTimer = new Timer ( {
942945 delay : timeout + 1 ,
943946 handler : connTimeOutHandler ,
@@ -948,14 +951,18 @@ class QUICConnection extends EventTarget {
948951 // If this is `null` there's nothing to do
949952 if ( timeout == null ) return ;
950953 // 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+ } ) ;
953965 }
954- this . logger . debug ( `timeout created with delay ${ timeout } ` ) ;
955- this . connTimeOutTimer = new Timer ( {
956- delay : timeout ,
957- handler : connTimeOutHandler ,
958- } ) ;
959966 }
960967
961968 /**
0 commit comments