@@ -2,6 +2,7 @@ import type { X509Certificate } from '@peculiar/x509';
22import type { ContextTimed , ContextTimedInput } from '@matrixai/contexts' ;
33import type { PromiseCancellable } from '@matrixai/async-cancellable' ;
44import type { QUICSocket , QUICConnection } from '@matrixai/quic' ;
5+ import type { ClientManifest } from '@matrixai/rpc' ;
56import type { Host , Hostname , Port , TLSConfig } from '../network/types.js' ;
67import type { Certificate } from '../keys/types.js' ;
78import type { NodeId } from './types.js' ;
@@ -31,12 +32,14 @@ type AgentClientManifest = typeof agentClientManifest;
3132/**
3233 * Encapsulates the unidirectional client-side connection of one node to another.
3334 */
34- interface NodeConnection extends createDestroy . CreateDestroy { }
35+ // eslint-disable-next-line
36+ interface NodeConnection < Manifest extends ClientManifest >
37+ extends createDestroy . CreateDestroy { }
3538@createDestroy . CreateDestroy ( {
3639 eventDestroy : nodesEvents . EventNodeConnectionDestroy ,
3740 eventDestroyed : nodesEvents . EventNodeConnectionDestroyed ,
3841} )
39- class NodeConnection {
42+ class NodeConnection < Manifest extends ClientManifest > {
4043 /**
4144 * Hostname is defined if the target's host was resolved from this hostname
4245 * Undefined if a Host was directly provided
@@ -60,7 +63,7 @@ class NodeConnection {
6063 public readonly quicClient : QUICClient | undefined ;
6164 public readonly quicConnection : QUICConnection ;
6265 public readonly connectionId : string ;
63- public readonly rpcClient : RPCClient < AgentClientManifest > ;
66+ public readonly rpcClient : RPCClient < Manifest > ;
6467
6568 /**
6669 * Dispatches a `EventNodeConnectionClose` in response to any `NodeConnection`
@@ -161,7 +164,7 @@ class NodeConnection {
161164 }
162165 } ;
163166
164- static createNodeConnection (
167+ static createNodeConnection < Manifest extends ClientManifest > (
165168 {
166169 targetNodeIds,
167170 targetHost,
@@ -186,16 +189,18 @@ class NodeConnection {
186189 connectionInitialMaxStreamsBidi ?: number ;
187190 connectionInitialMaxStreamsUni ?: number ;
188191 quicSocket ?: QUICSocket ;
189- manifest : AgentClientManifest ;
192+ manifest : Manifest ;
190193 logger ?: Logger ;
191194 } ,
192195 ctx ?: Partial < ContextTimedInput > ,
193- ) : PromiseCancellable < NodeConnection > ;
196+ ) : PromiseCancellable < NodeConnection < Manifest > > ;
194197 @decorators . timedCancellable (
195198 true ,
196199 config . defaultsSystem . nodesConnectionConnectTimeoutTime ,
197200 )
198- static async createNodeConnection (
201+ static async createNodeConnection <
202+ Manifest extends ClientManifest = AgentClientManifest ,
203+ > (
199204 {
200205 targetNodeIds,
201206 targetHost,
@@ -218,7 +223,7 @@ class NodeConnection {
218223 targetPort : Port ;
219224 targetHostname ?: Hostname ;
220225 tlsConfig : TLSConfig ;
221- manifest : AgentClientManifest ;
226+ manifest : Manifest ;
222227 connectionKeepAliveIntervalTime ?: number ;
223228 connectionKeepAliveTimeoutTime ?: number ;
224229 connectionInitialMaxStreamsBidi ?: number ;
@@ -227,7 +232,7 @@ class NodeConnection {
227232 logger ?: Logger ;
228233 } ,
229234 @decorators . context ctx : ContextTimed ,
230- ) : Promise < NodeConnection > {
235+ ) : Promise < NodeConnection < Manifest > > {
231236 logger . info ( `Creating forward ${ this . name } ` ) ;
232237 // Checking if attempting to connect to a wildcard IP
233238 if ( networkUtils . isHostWildcard ( targetHost ) ) {
@@ -295,7 +300,7 @@ class NodeConnection {
295300 quicEvents . EventQUICConnectionStream . name ,
296301 throwFunction ,
297302 ) ;
298- const rpcClient = new RPCClient < AgentClientManifest > ( {
303+ const rpcClient = new RPCClient < Manifest > ( {
299304 manifest,
300305 middlewareFactory : rpcUtilsMiddleware . defaultClientMiddlewareWrapper ( ) ,
301306 streamFactory : async ( ) => quicConnection . newStream ( ) ,
@@ -318,7 +323,7 @@ class NodeConnection {
318323 quicConnection . remoteHost
319324 } :${ quicConnection . remotePort } ]`,
320325 ) ;
321- const nodeConnection = new this ( {
326+ const nodeConnection = new this < Manifest > ( {
322327 validatedNodeId,
323328 nodeId,
324329 host : targetHost ,
@@ -368,7 +373,7 @@ class NodeConnection {
368373 return nodeConnection ;
369374 }
370375
371- static createNodeConnectionReverse ( {
376+ static createNodeConnectionReverse < Manifest extends ClientManifest > ( {
372377 certChain,
373378 nodeId,
374379 quicConnection,
@@ -378,12 +383,12 @@ class NodeConnection {
378383 certChain : Array < Certificate > ;
379384 nodeId : NodeId ;
380385 quicConnection : QUICConnection ;
381- manifest : AgentClientManifest ;
386+ manifest : Manifest ;
382387 logger ?: Logger ;
383- } ) : NodeConnection {
388+ } ) : NodeConnection < Manifest > {
384389 logger . info ( `Creating reverse ${ this . name } ` ) ;
385390 // Creating RPCClient
386- const rpcClient = new RPCClient < AgentClientManifest > ( {
391+ const rpcClient = new RPCClient < Manifest > ( {
387392 manifest,
388393 middlewareFactory : rpcUtilsMiddleware . defaultClientMiddlewareWrapper ( ) ,
389394 streamFactory : async ( _ctx ) => {
@@ -393,7 +398,7 @@ class NodeConnection {
393398 logger : logger . getChild ( RPCClient . name ) ,
394399 } ) ;
395400 // Creating NodeConnection
396- const nodeConnection = new this ( {
401+ const nodeConnection = new this < Manifest > ( {
397402 validatedNodeId : nodeId ,
398403 nodeId : nodeId ,
399404 localHost : quicConnection . localHost as unknown as Host ,
@@ -467,7 +472,7 @@ class NodeConnection {
467472 quicClient ?: QUICClient ;
468473 quicConnection : QUICConnection ;
469474 connectionId : string ;
470- rpcClient : RPCClient < AgentClientManifest > ;
475+ rpcClient : RPCClient < Manifest > ;
471476 logger : Logger ;
472477 } ) {
473478 this . validatedNodeId = validatedNodeId ;
@@ -544,7 +549,7 @@ class NodeConnection {
544549 /**
545550 * Gets RPCClient for this node connection
546551 */
547- public getClient ( ) : RPCClient < AgentClientManifest > {
552+ public getClient ( ) : RPCClient < Manifest > {
548553 return this . rpcClient ;
549554 }
550555}
0 commit comments