Skip to content

Commit b1ea509

Browse files
committed
fix: small fixes
[ci skip]
1 parent 110034d commit b1ea509

File tree

4 files changed

+64
-53
lines changed

4 files changed

+64
-53
lines changed

src/QUICClient.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class QUICClient extends EventTarget {
7272
* @param opts.reasonToCode - optional reason to code map
7373
* @param opts.codeToReason - optional code to reason map
7474
* @param opts.logger - optional logger
75+
* @param ctx
7576
*/
7677
public static createQUICClient(
7778
opts: {
@@ -216,9 +217,10 @@ class QUICClient extends EventTarget {
216217
),
217218
});
218219
const abortController = new AbortController();
219-
ctx.signal.addEventListener('abort', (r) => {
220+
const abortHandler = (r) => {
220221
abortController.abort(r);
221-
});
222+
};
223+
ctx.signal.addEventListener('abort', abortHandler);
222224
try {
223225
await Promise.race([
224226
connection.start({ ...ctx, signal: abortController.signal }),
@@ -234,6 +236,7 @@ class QUICClient extends EventTarget {
234236
throw e;
235237
} finally {
236238
socket.removeEventListener('socketError', handleQUICSocketError);
239+
ctx.signal.removeEventListener('abort', abortHandler);
237240
}
238241
address = utils.buildAddress(host_, port);
239242
const client = new this({

src/QUICConnection.ts

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
StreamCodeToReason,
1111
StreamId,
1212
StreamReasonToCode,
13+
VerifyCallback,
1314
} from './types';
1415
import type { Connection, ConnectionErrorCode, SendInfo } from './native/types';
1516
import { Lock, LockBox, Monitor, RWLockWriter } from '@matrixai/async-locks';
@@ -30,9 +31,6 @@ import * as utils from './utils';
3031
import { never } from './utils';
3132
import * 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
/**

src/QUICServer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class QUICServer extends EventTarget {
350350
await connection.start(); // TODO: pass ctx
351351
} catch (e) {
352352
// Ignoring any errors here as a failure to connect
353+
// FIXME: should we emit a connection error here?
353354
return;
354355
}
355356
this.dispatchEvent(

tests/QUICClient.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as testsUtils from './utils';
1414
import { sleep } from './utils';
1515

1616
describe(QUICClient.name, () => {
17-
const logger = new Logger(`${QUICClient.name} Test`, LogLevel.DEBUG, [
17+
const logger = new Logger(`${QUICClient.name} Test`, LogLevel.WARN, [
1818
new StreamHandler(
1919
formatting.format`${formatting.level}:${formatting.keys}:${formatting.msg}`,
2020
),

0 commit comments

Comments
 (0)