Skip to content

Commit ed6f3a3

Browse files
committed
fix: general logic fixes and clean up
- logic fixes for early stream ending. - fixing up tests. - fixed up remote info for stream. - fixed problem with process being held open and handling socket errors. - propagating all events to the top level client or server. - cleaning up comments and commentary. - fixed up benchmark.
1 parent 242859c commit ed6f3a3

17 files changed

+950
-737
lines changed

benches/stream_1KB.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ async function main() {
1616
),
1717
]);
1818
// Setting up initial state
19-
const crypto = {
20-
key: await testsUtils.generateKey(),
21-
ops: {
22-
sign: testsUtils.sign,
23-
verify: testsUtils.verify,
24-
randomBytes: testsUtils.randomBytes,
25-
},
26-
};
27-
2819
const data1KiB = Buffer.alloc(1024, 0xf0);
2920
const host = '127.0.0.1' as Host;
3021
const certChainPem = await fs.promises.readFile(
@@ -36,14 +27,18 @@ async function main() {
3627

3728
const quicServer = new QUICServer({
3829
config: {
39-
tlsConfig: {
40-
privKeyPem: privKeyPem.toString(),
41-
certChainPem: certChainPem.toString(),
42-
},
30+
key: privKeyPem.toString(),
31+
cert: certChainPem.toString(),
4332
verifyPeer: false,
33+
keepAliveIntervalTime: 1000,
34+
},
35+
crypto: {
36+
key: await testsUtils.generateKeyHMAC(),
37+
ops: {
38+
sign: testsUtils.signHMAC,
39+
verify: testsUtils.verifyHMAC,
40+
},
4441
},
45-
keepaliveIntervalTime: 1000,
46-
crypto,
4742
logger,
4843
});
4944
quicServer.addEventListener(
@@ -80,7 +75,11 @@ async function main() {
8075
host,
8176
port: quicServer.port,
8277
localHost: host,
83-
crypto,
78+
crypto: {
79+
ops: {
80+
randomBytes: testsUtils.randomBytes,
81+
},
82+
},
8483
logger,
8584
});
8685

src/QUICClient.ts

+20-19
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import QUICConnection from './QUICConnection';
2828
import QUICConnectionId from './QUICConnectionId';
2929

3030
/**
31-
* You must provide a error handler `addEventListener('error')`.
32-
* Otherwise errors will just be ignored.
31+
* You must provide an error handler `addEventListener('error')`.
32+
* Otherwise, errors will just be ignored.
3333
*
3434
* Use the same event names.
35-
* However it needs to bubble up.
35+
* However, it needs to bubble up.
3636
* And the right target needs to be used.
3737
*
3838
* Events:
@@ -63,7 +63,7 @@ class QUICClient extends EventTarget {
6363
* @param opts
6464
* @param opts.host - peer host where `0.0.0.0` becomes `127.0.0.1` and `::` becomes `::1`
6565
* @param opts.port
66-
* @param opts.localHost - defaults to `::` (dualstack)
66+
* @param opts.localHost - defaults to `::` (dual-stack)
6767
* @param opts.localPort - defaults 0
6868
* @param opts.crypto - client only needs the ability to generate random bytes
6969
* @param opts.config - optional config
@@ -140,7 +140,7 @@ class QUICClient extends EventTarget {
140140
await crypto.ops.randomBytes(scidBuffer);
141141
const scid = new QUICConnectionId(scidBuffer);
142142
let [host_] = await utils.resolveHost(host, resolveHostname);
143-
// If the target host is in fact an zero IP, it cannot be used
143+
// If the target host is in fact a zero IP, it cannot be used
144144
// as a target host, so we need to resolve it to a non-zero IP
145145
// in this case, 0.0.0.0 is resolved to 127.0.0.1 and :: and ::0 is
146146
// resolved to ::1
@@ -227,7 +227,7 @@ class QUICClient extends EventTarget {
227227
try {
228228
await Promise.race([connectionProm, socketErrorP]);
229229
} catch (e) {
230-
// In case the `connection.start` is on-going, we need to abort it
230+
// In case the `connection.start` is ongoing, we need to abort it
231231
abortController.abort(e);
232232
if (!isSocketShared) {
233233
// Stop is idempotent
@@ -254,13 +254,13 @@ class QUICClient extends EventTarget {
254254
/**
255255
* This must not throw any exceptions.
256256
*/
257-
protected handleQUICSocketEvents = async (e: events.QUICSocketEvent) => {
258-
if (e instanceof events.QUICSocketErrorEvent) {
257+
protected handleQUICSocketEvents = async (event: events.QUICSocketEvent) => {
258+
if (event instanceof events.QUICSocketErrorEvent) {
259259
// QUIC socket errors are re-emitted but a destroy takes place
260260
this.dispatchEvent(
261261
new events.QUICClientErrorEvent({
262262
detail: new errors.ErrorQUICClient('Socket error', {
263-
cause: e.detail,
263+
cause: event.detail,
264264
}),
265265
}),
266266
);
@@ -276,7 +276,7 @@ class QUICClient extends EventTarget {
276276
}),
277277
);
278278
}
279-
} else if (e instanceof events.QUICSocketStopEvent) {
279+
} else if (event instanceof events.QUICSocketStopEvent) {
280280
// If a QUIC socket stopped, we immediately destroy
281281
// However, the stop will have its own constraints
282282
try {
@@ -292,21 +292,21 @@ class QUICClient extends EventTarget {
292292
);
293293
}
294294
} else {
295-
this.dispatchEvent(e);
295+
this.dispatchEvent(event);
296296
}
297297
};
298298

299299
/**
300300
* This must not throw any exceptions.
301301
*/
302302
protected handleQUICConnectionEvents = async (
303-
e: events.QUICConnectionEvent,
303+
event: events.QUICConnectionEvent,
304304
) => {
305-
if (e instanceof events.QUICConnectionErrorEvent) {
305+
if (event instanceof events.QUICConnectionErrorEvent) {
306306
this.dispatchEvent(
307307
new events.QUICClientErrorEvent({
308308
detail: new errors.ErrorQUICClient('Connection error', {
309-
cause: e.detail,
309+
cause: event.detail,
310310
}),
311311
}),
312312
);
@@ -322,7 +322,7 @@ class QUICClient extends EventTarget {
322322
}),
323323
);
324324
}
325-
} else if (e instanceof events.QUICConnectionStopEvent) {
325+
} else if (event instanceof events.QUICConnectionStopEvent) {
326326
try {
327327
// Force destroy means don't destroy gracefully
328328
await this.destroy({
@@ -335,13 +335,14 @@ class QUICClient extends EventTarget {
335335
}),
336336
);
337337
}
338-
}
339-
if (e instanceof events.QUICConnectionStreamEvent) {
338+
} else if (event instanceof events.QUICConnectionStreamEvent) {
340339
this.dispatchEvent(
341-
new events.QUICConnectionStreamEvent({ detail: e.detail }),
340+
new events.QUICConnectionStreamEvent({ detail: event.detail }),
342341
);
342+
} else if (event instanceof events.QUICStreamDestroyEvent) {
343+
this.dispatchEvent(new events.QUICStreamDestroyEvent());
343344
} else {
344-
throw Error('TMP MUST RETHROW EVENTS');
345+
utils.never();
345346
}
346347
};
347348

0 commit comments

Comments
 (0)