Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import type { PolykeyWorkerManager } from './workers/types.js';
import type { TLSConfig } from './network/types.js';
import type { NodeAddress, NodeId, SeedNodes } from './nodes/types.js';
import type { AgentClientManifest } from './nodes/agent/callers/index.js';
import path from 'node:path';
import process from 'process';
import Logger from '@matrixai/logger';
Expand Down Expand Up @@ -45,6 +46,7 @@ import * as workersUtils from './workers/utils.js';
import * as clientMiddleware from './client/middleware.js';
import clientServerManifest from './client/handlers/index.js';
import agentServerManifest from './nodes/agent/handlers/index.js';
import manifestClient from './nodes/agent/callers/index.js';

interface PolykeyAgent extends createDestroyStartStop.CreateDestroyStartStop {}
@createDestroyStartStop.CreateDestroyStartStop(
Expand Down Expand Up @@ -177,7 +179,9 @@ class PolykeyAgent {
let gestaltGraph: GestaltGraph | undefined;
let identitiesManager: IdentitiesManager | undefined;
let nodeGraph: NodeGraph | undefined;
let nodeConnectionManager: NodeConnectionManager | undefined;
let nodeConnectionManager:
| NodeConnectionManager<AgentClientManifest>
| undefined;
let nodeManager: NodeManager | undefined;
let discovery: Discovery | undefined;
let notificationsManager: NotificationsManager | undefined;
Expand Down Expand Up @@ -286,6 +290,7 @@ class PolykeyAgent {
nodeConnectionManager = new NodeConnectionManager({
keyRing,
tlsConfig,
rpcClientManifest: manifestClient,
connectionFindConcurrencyLimit:
optionsDefaulted.nodes.connectionFindConcurrencyLimit,
connectionFindLocalTimeoutTime:
Expand Down Expand Up @@ -469,7 +474,7 @@ class PolykeyAgent {
public readonly gestaltGraph: GestaltGraph;
public readonly nodeGraph: NodeGraph;
public readonly taskManager: TaskManager;
public readonly nodeConnectionManager: NodeConnectionManager;
public readonly nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
public readonly nodeManager: NodeManager;
public readonly discovery: Discovery;
public readonly vaultManager: VaultManager;
Expand Down Expand Up @@ -540,7 +545,7 @@ class PolykeyAgent {
gestaltGraph: GestaltGraph;
nodeGraph: NodeGraph;
taskManager: TaskManager;
nodeConnectionManager: NodeConnectionManager;
nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
nodeManager: NodeManager;
discovery: Discovery;
vaultManager: VaultManager;
Expand Down
7 changes: 4 additions & 3 deletions src/audit/Audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { AuditEventId } from '../ids/types.js';
import type NodeConnectionManager from '../nodes/NodeConnectionManager.js';
import type Discovery from '../discovery/Discovery.js';
import type { AbstractEvent } from '@matrixai/events';
import type { AgentClientManifest } from '../nodes/agent/callers/index.js';
import Logger from '@matrixai/logger';
import { IdInternal } from '@matrixai/id';
import { createDestroyStartStop } from '@matrixai/async-init';
Expand Down Expand Up @@ -45,7 +46,7 @@ class Audit {
fresh = false,
}: {
db: DB;
nodeConnectionManager: NodeConnectionManager;
nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
discovery: Discovery;
logger?: Logger;
fresh?: boolean;
Expand All @@ -59,7 +60,7 @@ class Audit {

protected logger: Logger;
protected db: DB;
protected nodeConnectionManager: NodeConnectionManager;
protected nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
protected discovery: Discovery;

protected eventHandlerMap: Map<
Expand All @@ -86,7 +87,7 @@ class Audit {
logger,
}: {
db: DB;
nodeConnectionManager: NodeConnectionManager;
nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
discovery: Discovery;
logger: Logger;
}) {
Expand Down
5 changes: 3 additions & 2 deletions src/client/handlers/NodesListConnections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import type {
import type NodeConnectionManager from '../../nodes/NodeConnectionManager.js';
import type { ContextTimed } from '@matrixai/contexts';
import type { JSONValue } from '@matrixai/rpc';
import type { AgentClientManifest } from '../../nodes/agent/callers/index.js';
import { ServerHandler } from '@matrixai/rpc';
import * as nodesUtils from '../../nodes/utils.js';

class NodesListConnections extends ServerHandler<
{
nodeConnectionManager: NodeConnectionManager;
nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
},
ClientRPCRequestParams,
ClientRPCResponseResult<NodeConnectionMessage>
Expand All @@ -25,7 +26,7 @@ class NodesListConnections extends ServerHandler<
const {
nodeConnectionManager,
}: {
nodeConnectionManager: NodeConnectionManager;
nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
} = this.container;
const connections = nodeConnectionManager.listConnections();
for (const connection of connections) {
Expand Down
3 changes: 2 additions & 1 deletion src/client/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type NodeGraph from '../../nodes/NodeGraph.js';
import type VaultManager from '../../vaults/VaultManager.js';
import type PolykeyAgent from '../../PolykeyAgent.js';
import type { FileSystem } from '../../types.js';
import type { AgentClientManifest } from '../../nodes/agent/callers/index.js';
import AgentLockAll from './AgentLockAll.js';
import AgentStatus from './AgentStatus.js';
import AgentStop from './AgentStop.js';
Expand Down Expand Up @@ -108,7 +109,7 @@ const serverManifest = (container: {
audit: Audit;
notificationsManager: NotificationsManager;
nodeManager: NodeManager;
nodeConnectionManager: NodeConnectionManager;
nodeConnectionManager: NodeConnectionManager<AgentClientManifest>;
nodeGraph: NodeGraph;
vaultManager: VaultManager;
fs: FileSystem;
Expand Down
41 changes: 23 additions & 18 deletions src/nodes/NodeConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { X509Certificate } from '@peculiar/x509';
import type { ContextTimed, ContextTimedInput } from '@matrixai/contexts';
import type { PromiseCancellable } from '@matrixai/async-cancellable';
import type { QUICSocket, QUICConnection } from '@matrixai/quic';
import type { ClientManifest } from '@matrixai/rpc';
import type { Host, Hostname, Port, TLSConfig } from '../network/types.js';
import type { Certificate } from '../keys/types.js';
import type { NodeId } from './types.js';
Expand Down Expand Up @@ -31,12 +32,14 @@ type AgentClientManifest = typeof agentClientManifest;
/**
* Encapsulates the unidirectional client-side connection of one node to another.
*/
interface NodeConnection extends createDestroy.CreateDestroy {}
// eslint-disable-next-line
interface NodeConnection<Manifest extends ClientManifest>
extends createDestroy.CreateDestroy {}
@createDestroy.CreateDestroy({
eventDestroy: nodesEvents.EventNodeConnectionDestroy,
eventDestroyed: nodesEvents.EventNodeConnectionDestroyed,
})
class NodeConnection {
class NodeConnection<Manifest extends ClientManifest> {
/**
* Hostname is defined if the target's host was resolved from this hostname
* Undefined if a Host was directly provided
Expand All @@ -60,7 +63,7 @@ class NodeConnection {
public readonly quicClient: QUICClient | undefined;
public readonly quicConnection: QUICConnection;
public readonly connectionId: string;
public readonly rpcClient: RPCClient<AgentClientManifest>;
public readonly rpcClient: RPCClient<Manifest>;

/**
* Dispatches a `EventNodeConnectionClose` in response to any `NodeConnection`
Expand Down Expand Up @@ -161,7 +164,7 @@ class NodeConnection {
}
};

static createNodeConnection(
static createNodeConnection<Manifest extends ClientManifest>(
{
targetNodeIds,
targetHost,
Expand All @@ -186,16 +189,18 @@ class NodeConnection {
connectionInitialMaxStreamsBidi?: number;
connectionInitialMaxStreamsUni?: number;
quicSocket?: QUICSocket;
manifest: AgentClientManifest;
manifest: Manifest;
logger?: Logger;
},
ctx?: Partial<ContextTimedInput>,
): PromiseCancellable<NodeConnection>;
): PromiseCancellable<NodeConnection<Manifest>>;
@decorators.timedCancellable(
true,
config.defaultsSystem.nodesConnectionConnectTimeoutTime,
)
static async createNodeConnection(
static async createNodeConnection<
Manifest extends ClientManifest = AgentClientManifest,
>(
{
targetNodeIds,
targetHost,
Expand All @@ -218,7 +223,7 @@ class NodeConnection {
targetPort: Port;
targetHostname?: Hostname;
tlsConfig: TLSConfig;
manifest: AgentClientManifest;
manifest: Manifest;
connectionKeepAliveIntervalTime?: number;
connectionKeepAliveTimeoutTime?: number;
connectionInitialMaxStreamsBidi?: number;
Expand All @@ -227,7 +232,7 @@ class NodeConnection {
logger?: Logger;
},
@decorators.context ctx: ContextTimed,
): Promise<NodeConnection> {
): Promise<NodeConnection<Manifest>> {
logger.info(`Creating forward ${this.name}`);
// Checking if attempting to connect to a wildcard IP
if (networkUtils.isHostWildcard(targetHost)) {
Expand Down Expand Up @@ -295,7 +300,7 @@ class NodeConnection {
quicEvents.EventQUICConnectionStream.name,
throwFunction,
);
const rpcClient = new RPCClient<AgentClientManifest>({
const rpcClient = new RPCClient<Manifest>({
manifest,
middlewareFactory: rpcUtilsMiddleware.defaultClientMiddlewareWrapper(),
streamFactory: async () => quicConnection.newStream(),
Expand All @@ -318,7 +323,7 @@ class NodeConnection {
quicConnection.remoteHost
}:${quicConnection.remotePort}]`,
);
const nodeConnection = new this({
const nodeConnection = new this<Manifest>({
validatedNodeId,
nodeId,
host: targetHost,
Expand Down Expand Up @@ -368,7 +373,7 @@ class NodeConnection {
return nodeConnection;
}

static createNodeConnectionReverse({
static createNodeConnectionReverse<Manifest extends ClientManifest>({
certChain,
nodeId,
quicConnection,
Expand All @@ -378,12 +383,12 @@ class NodeConnection {
certChain: Array<Certificate>;
nodeId: NodeId;
quicConnection: QUICConnection;
manifest: AgentClientManifest;
manifest: Manifest;
logger?: Logger;
}): NodeConnection {
}): NodeConnection<Manifest> {
logger.info(`Creating reverse ${this.name}`);
// Creating RPCClient
const rpcClient = new RPCClient<AgentClientManifest>({
const rpcClient = new RPCClient<Manifest>({
manifest,
middlewareFactory: rpcUtilsMiddleware.defaultClientMiddlewareWrapper(),
streamFactory: async (_ctx) => {
Expand All @@ -393,7 +398,7 @@ class NodeConnection {
logger: logger.getChild(RPCClient.name),
});
// Creating NodeConnection
const nodeConnection = new this({
const nodeConnection = new this<Manifest>({
validatedNodeId: nodeId,
nodeId: nodeId,
localHost: quicConnection.localHost as unknown as Host,
Expand Down Expand Up @@ -467,7 +472,7 @@ class NodeConnection {
quicClient?: QUICClient;
quicConnection: QUICConnection;
connectionId: string;
rpcClient: RPCClient<AgentClientManifest>;
rpcClient: RPCClient<Manifest>;
logger: Logger;
}) {
this.validatedNodeId = validatedNodeId;
Expand Down Expand Up @@ -544,7 +549,7 @@ class NodeConnection {
/**
* Gets RPCClient for this node connection
*/
public getClient(): RPCClient<AgentClientManifest> {
public getClient(): RPCClient<Manifest> {
return this.rpcClient;
}
}
Expand Down
Loading