@@ -4,19 +4,15 @@ import type { NodeId } from './types';
4
4
import type { Host , Hostname , Port , TLSConfig } from '../network/types' ;
5
5
import type { Certificate , CertificatePEM } from '../keys/types' ;
6
6
import type { ClientManifest } from '../rpc/types' ;
7
- import type {
8
- QUICSocket ,
9
- ClientCrypto ,
10
- QUICConnection ,
11
- events as quicEvents ,
12
- } from '@matrixai/quic' ;
7
+ import type { QUICSocket , ClientCrypto , QUICConnection } from '@matrixai/quic' ;
13
8
import type { ContextTimedInput } from '@matrixai/contexts/dist/types' ;
14
9
import type { X509Certificate } from '@peculiar/x509' ;
15
10
import Logger from '@matrixai/logger' ;
16
11
import { CreateDestroy } from '@matrixai/async-init/dist/CreateDestroy' ;
17
12
import { timedCancellable , context } from '@matrixai/contexts/dist/decorators' ;
18
13
import { Evented } from '@matrixai/events' ;
19
- import { QUICClient } from '@matrixai/quic' ;
14
+ import { QUICClient , events as quicEvents } from '@matrixai/quic' ;
15
+ import { QUICClientEvent } from '@matrixai/quic/dist/events' ;
20
16
import * as nodesErrors from './errors' ;
21
17
import * as nodesEvents from './events' ;
22
18
import RPCClient from '../rpc/RPCClient' ;
@@ -31,6 +27,7 @@ import { never } from '../utils';
31
27
*/
32
28
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- False positive for M
33
29
interface NodeConnection < M extends ClientManifest > extends CreateDestroy { }
30
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars -- False positive for M
34
31
interface NodeConnection < M extends ClientManifest > extends Evented { }
35
32
@CreateDestroy ( )
36
33
@Evented ( )
@@ -196,33 +193,55 @@ class NodeConnection<M extends ClientManifest> {
196
193
} :${ quicConnection . remotePort } ]`,
197
194
) ,
198
195
} ) ;
199
- // Setting up stream handling
200
- const handleConnectionStream = (
201
- streamEvent : quicEvents . QUICConnectionStreamEvent ,
202
- ) => {
203
- const stream = streamEvent . detail ;
204
- nodeConnection . dispatchEvent (
205
- new nodesEvents . EventNodeStream ( { detail : stream } ) ,
206
- ) ;
207
- } ;
208
- // TODO: All quicConnection event handling should be done under a unified handler.
196
+ // TODO: remove this later based on testing
209
197
quicConnection . removeEventListener ( 'connectionStream' , throwFunction ) ;
210
- quicConnection . addEventListener ( 'connectionStream' , handleConnectionStream ) ;
198
+ // QUICClient events are...
199
+ // - QUICClientEvent,
200
+ // - QUICClientDestroyEvent,
201
+ // - QUICClientErrorEvent,
202
+ // QUICConnection events are...
203
+ // - QUICConnectionEvent,
204
+ // - QUICConnectionStreamEvent,
205
+ // - QUICConnectionStopEvent,
206
+ // - QUICConnectionErrorEvent,
207
+ // QUICStream events are...
208
+ // - QUICStreamEvent,
209
+ // - QUICStreamDestroyEvent,
210
+ const handleNodeConnectionEvents = ( e ) => {
211
+ if ( e instanceof quicEvents . QUICClientDestroyEvent ) {
212
+ // Trigger the nodeConnection destroying
213
+ void nodeConnection . destroy ( { force : false } ) ;
214
+ } else if ( e instanceof quicEvents . QUICConnectionStreamEvent ) {
215
+ const quicStream = e . detail ;
216
+ // Setting up stream handling
217
+ quicStream . addEventListener (
218
+ quicEvents . QUICStreamDestroyEvent . name ,
219
+ ( e ) => {
220
+ nodeConnection . dispatchEvent ( e . clone ( ) ) ;
221
+ } ,
222
+ { once : true } ,
223
+ ) ;
224
+ nodeConnection . dispatchEvent (
225
+ new nodesEvents . EventNodeStream ( { detail : quicStream } ) ,
226
+ ) ;
227
+ }
228
+ nodeConnection . dispatchEvent ( e . clone ( ) ) ;
229
+ } ;
230
+ // Setting up QUICConnection events
231
+ quicConnection . addEventListener ( handleNodeConnectionEvents ) ;
211
232
quicConnection . addEventListener (
212
- 'connectionStop' ,
233
+ quicEvents . QUICConnectionStopEvent . name ,
213
234
( ) => {
214
- quicConnection . removeEventListener (
215
- 'connectionStream' ,
216
- handleConnectionStream ,
217
- ) ;
235
+ quicConnection . removeEventListener ( handleNodeConnectionEvents ) ;
218
236
} ,
219
237
{ once : true } ,
220
238
) ;
239
+ // Setting up QUICClient events
240
+ quicClient . addEventListener ( handleNodeConnectionEvents ) ;
221
241
quicClient . addEventListener (
222
- 'clientDestroy' ,
242
+ quicEvents . QUICClientDestroyEvent . name ,
223
243
( ) => {
224
- // Trigger the nodeConnection destroying
225
- void nodeConnection . destroy ( { force : false } ) ;
244
+ quicClient . removeEventListener ( handleNodeConnectionEvents ) ;
226
245
} ,
227
246
{ once : true } ,
228
247
) ;
@@ -269,31 +288,41 @@ class NodeConnection<M extends ClientManifest> {
269
288
rpcClient,
270
289
logger,
271
290
} ) ;
272
- // Setting up stream handling
273
- // TODO: All quicConnection event handling should be done under a unified handler.
274
- const handleConnectionStream = (
275
- streamEvent : quicEvents . QUICConnectionStreamEvent ,
276
- ) => {
277
- nodeConnection . dispatchEvent (
278
- new nodesEvents . EventNodeStream ( { detail : streamEvent . detail } ) ,
279
- ) ;
291
+ // QUICClient events are...
292
+ // - QUICClientEvent,
293
+ // - QUICClientDestroyEvent,
294
+ // - QUICClientErrorEvent,
295
+ // QUICConnection events are...
296
+ // - QUICConnectionEvent,
297
+ // - QUICConnectionStreamEvent,
298
+ // - QUICConnectionStopEvent,
299
+ // - QUICConnectionErrorEvent,
300
+ const handleNodeConnectionEvents = ( e ) => {
301
+ if ( e instanceof quicEvents . QUICClientDestroyEvent ) {
302
+ // Trigger the nodeConnection destroying
303
+ void nodeConnection . destroy ( { force : false } ) ;
304
+ } else if ( e instanceof quicEvents . QUICConnectionStreamEvent ) {
305
+ const quicStream = e . detail ;
306
+ // Setting up stream handling
307
+ quicStream . addEventListener (
308
+ quicEvents . QUICStreamDestroyEvent . name ,
309
+ ( e ) => {
310
+ nodeConnection . dispatchEvent ( e . clone ( ) ) ;
311
+ } ,
312
+ { once : true } ,
313
+ ) ;
314
+ nodeConnection . dispatchEvent (
315
+ new nodesEvents . EventNodeStream ( { detail : quicStream } ) ,
316
+ ) ;
317
+ }
318
+ nodeConnection . dispatchEvent ( e . clone ( ) ) ;
280
319
} ;
281
- quicConnection . addEventListener ( 'connectionStream' , handleConnectionStream ) ;
320
+ // Setting up QUICConnection events
321
+ quicConnection . addEventListener ( handleNodeConnectionEvents ) ;
282
322
quicConnection . addEventListener (
283
- 'connectionStop' ,
323
+ quicEvents . QUICConnectionStopEvent . name ,
284
324
( ) => {
285
- quicConnection . removeEventListener (
286
- 'connectionStream' ,
287
- handleConnectionStream ,
288
- ) ;
289
- } ,
290
- { once : true } ,
291
- ) ;
292
- quicConnection . addEventListener (
293
- 'connectionStop' ,
294
- async ( ) => {
295
- // Trigger the nodeConnection destroying
296
- await nodeConnection . destroy ( { force : false } ) ;
325
+ quicConnection . removeEventListener ( handleNodeConnectionEvents ) ;
297
326
} ,
298
327
{ once : true } ,
299
328
) ;
0 commit comments