@persistica/flux-mesh 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/{main.js → index.js} +4 -4
- package/package.json +5 -7
- package/src/_classes/web-rtc-client-interface.class.d.ts +20 -0
- package/src/_routes/auth/network-authority.post.route.d.ts +7 -0
- package/src/_routes/auth/network-client.post.route.d.ts +12 -0
- package/src/_routes/options.route.d.ts +6 -0
- package/src/auth/auth.d.ts +22 -0
- package/src/business-logic/channels/channel-manager.class.d.ts +69 -0
- package/src/connected-client-socket.types.d.ts +21 -0
- package/src/index.d.ts +1 -0
- package/src/main.d.ts +25 -0
- package/src/orchestrators/facilitate-webrtc-connection.class.d.ts +2 -0
- package/src/register/network-client-manager.class.d.ts +38 -0
- package/src/register/register-network-authority.class.d.ts +34 -0
- package/src/routing/addressing.utils.d.ts +9 -0
- package/src/routing/global-channel/global-channel-pubsub.class.d.ts +34 -0
- package/src/routing/incomming-message-router.class.d.ts +9 -0
- package/src/routing/outgoing-message-router.class.d.ts +19 -0
- package/src/routing/process-message-router.class.d.ts +28 -0
- package/src/routing/redis/hash/network-agent.redis.sorted-set.d.ts +26 -0
- package/src/routing/redis/network-agent.redis.d.ts +43 -0
- package/src/routing/redis/redis-connection.class.d.ts +65 -0
- package/src/routing/rpc/core/global-rpc-client.class.d.ts +57 -0
- package/src/test.spec.d.ts +1 -0
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@persistica/flux-mesh",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Persistica Flux Mesh",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [],
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"exports": "./index.js",
|
|
11
|
+
"main": "./index.js",
|
|
12
|
+
"types": "./src/index.d.ts",
|
|
12
13
|
"repository": {
|
|
13
14
|
"type": "git",
|
|
14
15
|
"url": "git+https://github.com/rujorgensen/persistica.git"
|
|
@@ -18,8 +19,5 @@
|
|
|
18
19
|
"bugs": {
|
|
19
20
|
"url": "https://github.com/rujorgensen/persistica/issues"
|
|
20
21
|
},
|
|
21
|
-
"homepage": "https://github.com/rujorgensen/persistica#readme"
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"alpinejs": "^3.14.9"
|
|
24
|
-
}
|
|
22
|
+
"homepage": "https://github.com/rujorgensen/persistica#readme"
|
|
25
23
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { TProcessAddress } from '@flux/shared/types';
|
|
2
|
+
import { GlobalRPCClient2 } from '../routing/rpc/core/global-rpc-client.class';
|
|
3
|
+
import { type TRPCResponseCallbackFunction, RPCClient } from '@flux/shared/ws';
|
|
4
|
+
export declare class WebRTCClient extends RPCClient<'createOffer' | 'acceptOffer' | 'acceptAnswer' | 'answerAcceptedByInitiator'> {
|
|
5
|
+
private readonly _originProcessAddress;
|
|
6
|
+
private readonly _sendToRPCServer;
|
|
7
|
+
private readonly _handleResponseMessage;
|
|
8
|
+
constructor(_originProcessAddress: TProcessAddress, _sendToRPCServer: (data: any, // ! Use 'unknown' or Bun.BufferSource (does not work)
|
|
9
|
+
compress?: boolean | undefined) => number, _handleResponseMessage: (fn: TRPCResponseCallbackFunction) => void);
|
|
10
|
+
createOffer(): Promise<unknown>;
|
|
11
|
+
acceptOfferAndCreateAnswer(offer: unknown): Promise<unknown>;
|
|
12
|
+
acceptAnswer(answer: unknown): Promise<boolean>;
|
|
13
|
+
answerWasAccepted(answerAccepted: boolean): Promise<boolean>;
|
|
14
|
+
}
|
|
15
|
+
export declare class GlobalWebRTCClient extends GlobalRPCClient2<'createOffer' | 'acceptOffer' | 'acceptAnswer' | 'answerAcceptedByInitiator'> {
|
|
16
|
+
createOffer(): Promise<unknown>;
|
|
17
|
+
acceptOfferAndCreateAnswer(offer: unknown): Promise<unknown>;
|
|
18
|
+
acceptAnswer(answer: unknown): Promise<boolean>;
|
|
19
|
+
answerWasAccepted(answerAccepted: boolean): Promise<boolean>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BunRequest } from 'bun';
|
|
2
|
+
import type { GlobalRPCClient } from '../../routing/rpc/core/global-rpc-client.class';
|
|
3
|
+
import type { NetworkAuthorityManager } from '../../register/register-network-authority.class';
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param { BunRequest } request
|
|
7
|
+
* @param { NetworkAuthorityManager } networkAuthorityManager
|
|
8
|
+
* @param { GlobalRPCClient<'authorize'> } globalRPCClient
|
|
9
|
+
*
|
|
10
|
+
* @returns { void }
|
|
11
|
+
*/
|
|
12
|
+
export declare const authorizeNetworkAgent: (request: BunRequest, networkAuthorityManager: NetworkAuthorityManager, globalRPCClient: GlobalRPCClient<"authorize">) => Promise<Response>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TNetworkId_S } from '@flux/shared/types';
|
|
2
|
+
import type { TFluxClientUID } from '@flux/shared/utils';
|
|
3
|
+
export type TTokenPayload = {
|
|
4
|
+
networkId: TNetworkId_S;
|
|
5
|
+
claim?: string;
|
|
6
|
+
isAuthority?: boolean;
|
|
7
|
+
machineUID?: TFluxClientUID;
|
|
8
|
+
agentUID?: string;
|
|
9
|
+
};
|
|
10
|
+
export type TTokenPayloadJWT = string & {
|
|
11
|
+
__brand: 'TokenPayloadJWT';
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Generates JWT string from a token payload.
|
|
15
|
+
*
|
|
16
|
+
* @param { TTokenPayload } payload
|
|
17
|
+
* @param { number } expiresIn
|
|
18
|
+
*
|
|
19
|
+
* @returns { TTokenPayloadJWT }
|
|
20
|
+
*/
|
|
21
|
+
export declare const generateToken: (payload: TTokenPayload, expiresIn?: number) => string;
|
|
22
|
+
export declare const verifyTokenOrThrow: (token: unknown) => TTokenPayload;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type TAddress, type TChannelName, type TNetworkId_S } from '@flux/shared/types';
|
|
2
|
+
import type { GlobalChannelPubsub } from '../../routing/global-channel/global-channel-pubsub.class';
|
|
3
|
+
export declare class NetworkChannelManager {
|
|
4
|
+
private readonly _globalChannelPubsub;
|
|
5
|
+
private readonly channelUsageCount;
|
|
6
|
+
private readonly redisConnection;
|
|
7
|
+
private readonly networkChannelHash;
|
|
8
|
+
constructor(_globalChannelPubsub: GlobalChannelPubsub);
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a channel can have more members.
|
|
11
|
+
*
|
|
12
|
+
* ! TODO Don't query the database repeatedly, implement local synced cache.
|
|
13
|
+
*
|
|
14
|
+
* @param { TNetworkId_S } networkId
|
|
15
|
+
* @param { TChannelName } channelName
|
|
16
|
+
*
|
|
17
|
+
* @returns { Promise<boolean> }
|
|
18
|
+
*/
|
|
19
|
+
canHaveMembers(networkId: TNetworkId_S, channelName: TChannelName): Promise<boolean>;
|
|
20
|
+
/**
|
|
21
|
+
* Joins a network channel.
|
|
22
|
+
*
|
|
23
|
+
* @param { TNetworkId_S } networkId
|
|
24
|
+
* @param { TChannelName } channelName
|
|
25
|
+
* @param { TAddress } clientAddress
|
|
26
|
+
*
|
|
27
|
+
* @returns { void }
|
|
28
|
+
*/
|
|
29
|
+
joinNetworkChannel(networkId: TNetworkId_S, channelName: TChannelName, clientAddress: TAddress): void;
|
|
30
|
+
/**
|
|
31
|
+
* Leaves a network channel.
|
|
32
|
+
*
|
|
33
|
+
* @param { TNetworkId_S } networkId
|
|
34
|
+
* @param { TChannelName } channelName
|
|
35
|
+
* @param { TAddress } clientAddress
|
|
36
|
+
*
|
|
37
|
+
* @returns { Promise<void> }
|
|
38
|
+
*/
|
|
39
|
+
leaveNetworkChannel(networkId: TNetworkId_S, channelName: TChannelName, clientAddress: TAddress): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Leaves all network channels.
|
|
42
|
+
*
|
|
43
|
+
* @param { TNetworkId_S } networkId
|
|
44
|
+
* @param { TAddress } clientAddress
|
|
45
|
+
* @param { Set<TChannelName> } channelNames
|
|
46
|
+
*
|
|
47
|
+
* @returns { void }
|
|
48
|
+
*/
|
|
49
|
+
leaveAllNetworkChannels(networkId: TNetworkId_S, clientAddress: TAddress, channelNames: Set<TChannelName>): void;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a network channel if it does not exist.
|
|
52
|
+
*
|
|
53
|
+
* @param { TNetworkId_S } networkId
|
|
54
|
+
* @param { TChannelName } channelName
|
|
55
|
+
*
|
|
56
|
+
* @returns { void }
|
|
57
|
+
*/
|
|
58
|
+
private createNetworkChannelIfNotExist;
|
|
59
|
+
/**
|
|
60
|
+
* Increases the usage count of a channel.
|
|
61
|
+
*
|
|
62
|
+
* @param { TNetworkId_S } networkId
|
|
63
|
+
* @param { TChannelName } channelName
|
|
64
|
+
* @param { number } usage
|
|
65
|
+
*
|
|
66
|
+
* @returns { void }
|
|
67
|
+
*/
|
|
68
|
+
increaseUsageCount(networkId: TNetworkId_S, channelName: TChannelName, usage: number): void;
|
|
69
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TAddress, TAgentOwnUId, TChannelName, TClientId, TNetworkId_S } from '@flux/shared/types';
|
|
2
|
+
import type { TFluxClientUID } from '@flux/shared/utils';
|
|
3
|
+
import type { WebRTCClient } from './_classes/web-rtc-client-interface.class';
|
|
4
|
+
import type { RPCClient } from '@flux/shared/ws';
|
|
5
|
+
export type TConnectedClientSocket = Bun.ServerWebSocket<{
|
|
6
|
+
ip: Bun.SocketAddress | null;
|
|
7
|
+
id: TClientId;
|
|
8
|
+
uid?: TAgentOwnUId;
|
|
9
|
+
address: TAddress;
|
|
10
|
+
networkId: TNetworkId_S;
|
|
11
|
+
isAuthority?: boolean;
|
|
12
|
+
rtcClient?: WebRTCClient;
|
|
13
|
+
claim?: string;
|
|
14
|
+
rpcClient: RPCClient<'channel'>;
|
|
15
|
+
channelNames: Set<TChannelName>;
|
|
16
|
+
machineUID?: TFluxClientUID;
|
|
17
|
+
throughput: {
|
|
18
|
+
bytes: number;
|
|
19
|
+
packets: number;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FluxMeshServer, } from './main';
|
package/src/main.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
var meshLoadCount: number | null;
|
|
3
|
+
}
|
|
4
|
+
type TOptions = {
|
|
5
|
+
port?: number;
|
|
6
|
+
redisConnectionString?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare class FluxMeshServer {
|
|
9
|
+
private readonly optionsOrPort?;
|
|
10
|
+
private readonly redisConnection;
|
|
11
|
+
private readonly onReadyListeners;
|
|
12
|
+
private readonly bunServer;
|
|
13
|
+
private readonly globalChannelPubsub;
|
|
14
|
+
private readonly channelManager;
|
|
15
|
+
private readonly options;
|
|
16
|
+
constructor(optionsOrPort?: (TOptions | number) | undefined);
|
|
17
|
+
onReady(fn: () => void): void;
|
|
18
|
+
/**
|
|
19
|
+
* Gracefully shuts down the server.
|
|
20
|
+
*
|
|
21
|
+
* @returns { Promise<void> }
|
|
22
|
+
*/
|
|
23
|
+
stop(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { NetworkAgentRedisService } from '@flux/mesh/store/redis/network-agent';
|
|
2
|
+
import { NetworkUsageRedisCacheService } from '@flux/mesh/store/redis/network-usage';
|
|
3
|
+
import type { TAddress, TClientId, TAgentOwnUId, TNetworkId_S } from '@flux/shared/types';
|
|
4
|
+
import type { TFluxClientUID } from '@flux/shared/utils';
|
|
5
|
+
export declare class NetworkAgentManager {
|
|
6
|
+
readonly networkAgentRedisService: NetworkAgentRedisService;
|
|
7
|
+
readonly networkUsageRedisCacheService: NetworkUsageRedisCacheService;
|
|
8
|
+
private readonly redisConnection;
|
|
9
|
+
private readonly cache;
|
|
10
|
+
private readonly timers;
|
|
11
|
+
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Register an agent.
|
|
14
|
+
*
|
|
15
|
+
* @param { TNetworkId_S } networkId
|
|
16
|
+
* @param { TClientId } id
|
|
17
|
+
* @param { Bun.SocketAddress | null } ip
|
|
18
|
+
* @param { TAddress } address
|
|
19
|
+
* @param { TAgentOwnUId } [uid]
|
|
20
|
+
*
|
|
21
|
+
* @returns { void }
|
|
22
|
+
*/
|
|
23
|
+
registerAgent(networkId: TNetworkId_S, id: TClientId, ip: Bun.SocketAddress | null, address: TAddress, throughput: {
|
|
24
|
+
bytes: number;
|
|
25
|
+
packets: number;
|
|
26
|
+
}, uid?: TAgentOwnUId, machineUID?: TFluxClientUID): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Unregisters a network agent UID and associated data from the Redis hash.
|
|
29
|
+
*
|
|
30
|
+
* @param { TNetworkId_S } networkId
|
|
31
|
+
* @param { TClientId } clientId
|
|
32
|
+
* @param { TAgentOwnUId } clientOwnUId
|
|
33
|
+
*
|
|
34
|
+
* @returns { void }
|
|
35
|
+
*/
|
|
36
|
+
unregisterNetworkAgent(networkId: TNetworkId_S, clientId: TClientId, clientOwnUId?: TAgentOwnUId): void;
|
|
37
|
+
resolveNetworkClientAddressByUid(networkId: TNetworkId_S, clientOwnUId: TAgentOwnUId): Promise<TAddress>;
|
|
38
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type TAddress, type TClientId, type TNetworkId_S } from '@flux/shared/types';
|
|
2
|
+
import type { TFluxClientUID } from '@flux/shared/utils';
|
|
3
|
+
export declare class NetworkAuthorityManager {
|
|
4
|
+
private readonly redisConnection;
|
|
5
|
+
private readonly cache;
|
|
6
|
+
register(networkId: TNetworkId_S, socketId: TClientId, machineUID?: TFluxClientUID): Promise<void>;
|
|
7
|
+
unregister(networkId: TNetworkId_S, networkAuthorityAddress: TAddress): void;
|
|
8
|
+
/**
|
|
9
|
+
* Used for cleanup, in case of discovering an idle authority.
|
|
10
|
+
*
|
|
11
|
+
* @param { TNetworkId_S } networkId
|
|
12
|
+
* @param { TAddress } networkAuthorityAddress
|
|
13
|
+
*
|
|
14
|
+
* @returns { void }
|
|
15
|
+
*/
|
|
16
|
+
unregisterGlobal(networkId: TNetworkId_S, networkAuthorityAddress: TAddress): void;
|
|
17
|
+
/**
|
|
18
|
+
* Resolves a random network authority address for a given network ID.
|
|
19
|
+
*
|
|
20
|
+
* @param { TNetworkId_S } networkId
|
|
21
|
+
*
|
|
22
|
+
* @returns { Promise<TAddress> }
|
|
23
|
+
*/
|
|
24
|
+
resolveNetworkAuthorityAddressOrThrow(networkId: TNetworkId_S): Promise<TAddress>;
|
|
25
|
+
/**
|
|
26
|
+
* Removes a client.
|
|
27
|
+
*
|
|
28
|
+
* @param { TNetworkId_S } networkId
|
|
29
|
+
* @param { TAddress } networkAuthorityAddress
|
|
30
|
+
*
|
|
31
|
+
* @returns { void }
|
|
32
|
+
*/
|
|
33
|
+
removeUnresponsiveClient(networkId: TNetworkId_S, networkAuthorityAddress: TAddress): void;
|
|
34
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { TMachineAddress, TProcessId, TProcessAddress } from '@flux/shared/types';
|
|
2
|
+
export declare const readProcessId: () => TProcessId;
|
|
3
|
+
export declare const readMachineAddress: () => TMachineAddress;
|
|
4
|
+
/**
|
|
5
|
+
* Returns the process address.
|
|
6
|
+
*
|
|
7
|
+
* @returns { TProcessAddress }
|
|
8
|
+
*/
|
|
9
|
+
export declare const readProcessAddress: () => TProcessAddress;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 1. Detects global events and publishes them to all clients subscribed to the websocket channel system.
|
|
3
|
+
*
|
|
4
|
+
* 2. Sends messages to all nodes in the cluster, and when reached, publishes locally
|
|
5
|
+
* on the server channel to everyone subscribed.
|
|
6
|
+
*/
|
|
7
|
+
import type { TProcessAddress } from '@flux/shared/types';
|
|
8
|
+
import type { TConnectedClientSocket } from '../../connected-client-socket.types';
|
|
9
|
+
import type { RedisConnection } from '../redis/redis-connection.class';
|
|
10
|
+
export type TGlobalChannel = string & {
|
|
11
|
+
__brand: 'global-channel';
|
|
12
|
+
};
|
|
13
|
+
export declare class GlobalChannelPubsub {
|
|
14
|
+
private readonly redisConnection;
|
|
15
|
+
private readonly bunServer;
|
|
16
|
+
private readonly processAddress;
|
|
17
|
+
constructor(redisConnection: RedisConnection, bunServer: Bun.Server, processAddress: TProcessAddress);
|
|
18
|
+
/**
|
|
19
|
+
* Subscribes to the websocket channel event and publishes it on the local process.
|
|
20
|
+
*
|
|
21
|
+
* @returns { void }
|
|
22
|
+
*/
|
|
23
|
+
private subscribeWebsocketChannelEvent;
|
|
24
|
+
/**
|
|
25
|
+
* Publishes a message to the given channel.
|
|
26
|
+
*
|
|
27
|
+
* @param { string } channel
|
|
28
|
+
* @param { string } message
|
|
29
|
+
* @param { TConnectedClientSocket } [ws]
|
|
30
|
+
*
|
|
31
|
+
* @returns { Promise<void> }
|
|
32
|
+
*/
|
|
33
|
+
publish(channel: string, message: string, ws?: TConnectedClientSocket): Promise<void>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route messages to the correct destination.
|
|
3
|
+
*/
|
|
4
|
+
import type { TClientId } from '@flux/shared/types';
|
|
5
|
+
export declare class IncommingMessageRouter {
|
|
6
|
+
private readonly onLocal;
|
|
7
|
+
private readonly redisConnection;
|
|
8
|
+
constructor(onLocal: (clientId: TClientId, onMessage: (message: string) => void) => void);
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route messages to the correct destination.
|
|
3
|
+
*/
|
|
4
|
+
import { type TAddress, type TClientId } from '@flux/shared/types';
|
|
5
|
+
export declare class OutgoingMessageRouter {
|
|
6
|
+
private readonly passToLocalClientOrThrowUnknownClient;
|
|
7
|
+
private readonly processId;
|
|
8
|
+
private readonly machineAddress;
|
|
9
|
+
private readonly redisConnection;
|
|
10
|
+
constructor(passToLocalClientOrThrowUnknownClient: (clientId: TClientId, message: string) => void);
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param address
|
|
14
|
+
* @param message
|
|
15
|
+
*
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
message(address: TAddress, message: string): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route messages to the correct process if necessary.
|
|
3
|
+
*/
|
|
4
|
+
import { type TCallbackFunction, type TProcessAddress } from '@flux/shared/types';
|
|
5
|
+
export declare class ProcessMessageRouter {
|
|
6
|
+
private readonly machineAddress;
|
|
7
|
+
private readonly processAddress;
|
|
8
|
+
private readonly processId;
|
|
9
|
+
private readonly redisConnection;
|
|
10
|
+
private readonly localCallbacks;
|
|
11
|
+
/**
|
|
12
|
+
* Sends a message to a process.
|
|
13
|
+
*
|
|
14
|
+
* @param { TProcessAddress } address
|
|
15
|
+
* @param { string } message
|
|
16
|
+
*
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
message(address: TProcessAddress, message: string): void;
|
|
20
|
+
/**
|
|
21
|
+
* Subscribe to mesaages for local process.
|
|
22
|
+
*
|
|
23
|
+
* @param message
|
|
24
|
+
*
|
|
25
|
+
* @returns { void }
|
|
26
|
+
*/
|
|
27
|
+
subscribe(onMessage: TCallbackFunction): void;
|
|
28
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { RedisClient } from 'bun';
|
|
2
|
+
import type { TClientId, TNetworkId_S } from '@flux/shared/types';
|
|
3
|
+
export declare class NetworkAgentRedisSortedSet {
|
|
4
|
+
private readonly client;
|
|
5
|
+
private readonly processId;
|
|
6
|
+
private readonly machineAddress;
|
|
7
|
+
constructor(client: RedisClient);
|
|
8
|
+
/**
|
|
9
|
+
* Registers a network agent in the sorted set.
|
|
10
|
+
*
|
|
11
|
+
* @param { TNetworkId_S } networkId
|
|
12
|
+
* @param { TClientId } socketId
|
|
13
|
+
*
|
|
14
|
+
* @returns { Promise<void> }
|
|
15
|
+
*/
|
|
16
|
+
registerAgent(networkId: TNetworkId_S, socketId: TClientId): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Unregisters a network agent from the sorted set.
|
|
19
|
+
*
|
|
20
|
+
* @param { TNetworkId_S } networkId
|
|
21
|
+
* @param { TClientId } socketId
|
|
22
|
+
*
|
|
23
|
+
* @returns { Promise<number> }
|
|
24
|
+
*/
|
|
25
|
+
unregisterAgent(networkId: TNetworkId_S, socketId: TClientId): Promise<number>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Register information on a specific network agent.
|
|
3
|
+
*/
|
|
4
|
+
import type { RedisClient } from 'bun';
|
|
5
|
+
import type { TAddress, TClientId, TAgentOwnUId, TNetworkId_S } from '@flux/shared/types';
|
|
6
|
+
export declare class NetworkAgentRedis {
|
|
7
|
+
private readonly client;
|
|
8
|
+
private readonly networkAgentRedisSortedSet;
|
|
9
|
+
private readonly cashedDataUsage;
|
|
10
|
+
constructor(client: RedisClient);
|
|
11
|
+
/**
|
|
12
|
+
* Registers a network agent in the sorted set.
|
|
13
|
+
*
|
|
14
|
+
* @param { TNetworkId_S } networkId
|
|
15
|
+
* @param { TClientId } socketId
|
|
16
|
+
*
|
|
17
|
+
* @returns { Promise<void> }
|
|
18
|
+
*/
|
|
19
|
+
registerNetworkAgent(networkId: TNetworkId_S, clientId: TAddress, socketId: TClientId, uid: TAgentOwnUId): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Unregisters a network agent from the sorted set.
|
|
22
|
+
*
|
|
23
|
+
* @param { TNetworkId_S } networkId
|
|
24
|
+
* @param { TClientId } socketId
|
|
25
|
+
*
|
|
26
|
+
* @returns { Promise<number> }
|
|
27
|
+
*/
|
|
28
|
+
unregisterNetworkAgent(networkId: TNetworkId_S, socketId: TClientId): Promise<number>;
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @param socketId
|
|
32
|
+
* @param usage
|
|
33
|
+
*
|
|
34
|
+
* @returns { void }
|
|
35
|
+
*/
|
|
36
|
+
registerDataUsage(networkId: TNetworkId_S, socketId: TClientId, usage: number): void;
|
|
37
|
+
/**
|
|
38
|
+
* Pushes the data usage to the Redis server.
|
|
39
|
+
*
|
|
40
|
+
* @returns { Promise<void> }
|
|
41
|
+
*/
|
|
42
|
+
private pushDataUsage;
|
|
43
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { TAddress, TClientId, TProcessAddress } from '@flux/shared/types';
|
|
2
|
+
import { NetworkAuthorityRedisSortedSet } from '@flux/mesh/store/redis/network-authority';
|
|
3
|
+
import { NetworkAgentRedisService } from '@flux/mesh/store/redis/network-agent';
|
|
4
|
+
import type { TGlobalChannel } from '../global-channel/global-channel-pubsub.class';
|
|
5
|
+
/**
|
|
6
|
+
* Singleton function to get the Redis connection
|
|
7
|
+
*
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare const getMeshRedisConnection: (connectionString?: string) => RedisConnection;
|
|
11
|
+
export type MessageCallback = (message: string, channel?: string) => unknown;
|
|
12
|
+
export declare class RedisConnection {
|
|
13
|
+
private readonly _optionsOrURL;
|
|
14
|
+
private readonly cacheClient;
|
|
15
|
+
private readonly pubSub;
|
|
16
|
+
readonly networkAuthoritySet: NetworkAuthorityRedisSortedSet;
|
|
17
|
+
readonly networkAgentRedisService: NetworkAgentRedisService;
|
|
18
|
+
readonly hash: {
|
|
19
|
+
sadd: (key: Bun.RedisClient.KeyLike, member: string) => Promise<number>;
|
|
20
|
+
hmset: (key: Bun.RedisClient.KeyLike, fieldValues: string[]) => Promise<string>;
|
|
21
|
+
smembers: (key: Bun.RedisClient.KeyLike) => Promise<string[]>;
|
|
22
|
+
srem: (key: Bun.RedisClient.KeyLike, member: string) => Promise<number>;
|
|
23
|
+
scard: (key: Bun.RedisClient.KeyLike) => Promise<number>;
|
|
24
|
+
del: (...keys: Bun.RedisClient.KeyLike[]) => Promise<number>;
|
|
25
|
+
hincrby: (key: Bun.RedisClient.KeyLike, field: string, increment: string | number) => Promise<number>;
|
|
26
|
+
hmget: (key: Bun.RedisClient.KeyLike, fields: string[]) => Promise<Array<string | null>>;
|
|
27
|
+
expire: (key: Bun.RedisClient.KeyLike, seconds: number) => Promise<number>;
|
|
28
|
+
};
|
|
29
|
+
constructor(_optionsOrURL: string | {
|
|
30
|
+
name?: string;
|
|
31
|
+
url: string;
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param { TProcessAddress } skipProcessAddress
|
|
36
|
+
* @param { TGlobalChannel } channel
|
|
37
|
+
* @param { string } message
|
|
38
|
+
*
|
|
39
|
+
* @returns { Promise<void> }
|
|
40
|
+
*/
|
|
41
|
+
publishWebsocketChannelEvent(skipProcessAddress: TProcessAddress, channel: TGlobalChannel, message: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @returns { void }
|
|
45
|
+
*/
|
|
46
|
+
subscribeWebsocketChannelEvent(callback: (channel: TGlobalChannel, skipProcessAddress: TProcessAddress, message: string) => unknown): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* @param { TAddress | TProcessAddress } address
|
|
50
|
+
* @param { string } message
|
|
51
|
+
*
|
|
52
|
+
* @returns { Promise<void> }
|
|
53
|
+
*/
|
|
54
|
+
directPublish(address: TAddress | TProcessAddress, message: string): Promise<void>;
|
|
55
|
+
subscribe(channelId: TProcessAddress | TClientId, callback: MessageCallback): void;
|
|
56
|
+
unsubscribe(channelId: string, callback: MessageCallback): void;
|
|
57
|
+
setConnected(address: string): Promise<void>;
|
|
58
|
+
setDisconnected(_address: string): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Disconnects the Redis cache and pub/sub.
|
|
61
|
+
*
|
|
62
|
+
* @returns { void }
|
|
63
|
+
*/
|
|
64
|
+
disconnect(): void;
|
|
65
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The class to handling the *calling* side of the RPC.
|
|
3
|
+
*/
|
|
4
|
+
import { type TAddress } from '@flux/shared/types';
|
|
5
|
+
import type { OutgoingMessageRouter } from '../../outgoing-message-router.class';
|
|
6
|
+
import type { ProcessMessageRouter } from '../../process-message-router.class';
|
|
7
|
+
export declare class GlobalRPCClient<TMethods> {
|
|
8
|
+
private readonly _outgoingMessageRouter;
|
|
9
|
+
private readonly _processMessageRouter;
|
|
10
|
+
private requestId;
|
|
11
|
+
private readonly pendingRequests;
|
|
12
|
+
private readonly processAddress;
|
|
13
|
+
constructor(_outgoingMessageRouter: OutgoingMessageRouter, _processMessageRouter: ProcessMessageRouter);
|
|
14
|
+
/**
|
|
15
|
+
* Calls a function on the other side.
|
|
16
|
+
*
|
|
17
|
+
* @param { TAddress } rpcServerClientAddress
|
|
18
|
+
* @param { TMethods } method
|
|
19
|
+
*
|
|
20
|
+
* @returns { Promise<any> }
|
|
21
|
+
*/
|
|
22
|
+
call(rpcServerClientAddress: TAddress, method: TMethods, ...params: any): Promise<any>;
|
|
23
|
+
/**
|
|
24
|
+
* Handles the response from the server.
|
|
25
|
+
*
|
|
26
|
+
* @param { RPCResponse } response
|
|
27
|
+
*
|
|
28
|
+
* @returns { boolean }
|
|
29
|
+
*/
|
|
30
|
+
private handleResponseMessage;
|
|
31
|
+
}
|
|
32
|
+
export declare class GlobalRPCClient2<TMethods> {
|
|
33
|
+
private readonly _rpcServerClientAddress;
|
|
34
|
+
private readonly _outgoingMessageRouter;
|
|
35
|
+
private readonly _processMessageRouter;
|
|
36
|
+
private requestId;
|
|
37
|
+
private readonly pendingRequests;
|
|
38
|
+
private readonly processAddress;
|
|
39
|
+
constructor(_rpcServerClientAddress: TAddress, _outgoingMessageRouter: OutgoingMessageRouter, _processMessageRouter: ProcessMessageRouter);
|
|
40
|
+
/**
|
|
41
|
+
* Calls a function on the other side.
|
|
42
|
+
*
|
|
43
|
+
* @param { TAddress } rpcServerClientAddress
|
|
44
|
+
* @param { TMethods } method
|
|
45
|
+
*
|
|
46
|
+
* @returns { Promise<any> }
|
|
47
|
+
*/
|
|
48
|
+
call(method: TMethods, ...params: any): Promise<any>;
|
|
49
|
+
/**
|
|
50
|
+
* Handles the response from the server.
|
|
51
|
+
*
|
|
52
|
+
* @param { RPCResponse } response
|
|
53
|
+
*
|
|
54
|
+
* @returns { boolean }
|
|
55
|
+
*/
|
|
56
|
+
private handleResponseMessage;
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|