@persistica/flux-mesh 0.0.12 → 0.0.16

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.
Files changed (36) hide show
  1. package/index.js +271 -4
  2. package/package.json +11 -5
  3. package/src/_classes/web-rtc-client-interface.class.d.ts +52 -0
  4. package/src/_managers/agent.manager.d.ts +15 -0
  5. package/src/_managers/authority.manager.d.ts +15 -0
  6. package/src/_managers/global/global-client.manager.d.ts +22 -0
  7. package/src/_managers/local/local-agent.manager.d.ts +12 -0
  8. package/src/_managers/local/local-authority.manager.d.ts +12 -0
  9. package/src/_routes/auth/network-authority.post.route.d.ts +10 -2
  10. package/src/_routes/auth/network-client.post.route.d.ts +7 -6
  11. package/src/_routes/npc-interact.get.route.d.ts +6 -0
  12. package/src/_routes/options.route.d.ts +1 -1
  13. package/src/auth/auth.d.ts +10 -3
  14. package/src/business-logic/channels/channel-manager.class.d.ts +11 -36
  15. package/src/business-logic/npc/npc-dialogue-manager.class.d.ts +7 -0
  16. package/src/business-logic/npc/npc-dialogue-script.class.d.ts +7 -0
  17. package/src/business-logic/npc/npc-dialogue-script.class.spec.d.ts +1 -0
  18. package/src/business-logic/npc/npc-dialogue.types.d.ts +6 -0
  19. package/src/business-logic/npc/npc-interaction-tracker.class.d.ts +5 -0
  20. package/src/business-logic/npc/npc-interaction-tracker.class.spec.d.ts +1 -0
  21. package/src/index.d.ts +2 -0
  22. package/src/main.d.ts +3 -3
  23. package/src/orchestrators/facilitate-webrtc-connection.class.d.ts +3 -0
  24. package/src/register/{network-client-manager.class.d.ts → network-agent-redis-cache.class.d.ts} +20 -10
  25. package/src/register/network-authority-redis-cache.class.d.ts +49 -0
  26. package/src/routing/addressing.utils.d.ts +11 -1
  27. package/src/routing/global-channel/global-channel-pubsub.class.d.ts +6 -6
  28. package/src/routing/outgoing-message-router.class.d.ts +3 -4
  29. package/src/routing/process-message-router.class.d.ts +4 -8
  30. package/src/routing/redis/_utils/split-message.utils.d.ts +11 -0
  31. package/src/routing/redis/_utils/split-message.utils.spec.d.ts +1 -0
  32. package/src/routing/redis/hash/network-agent.redis.sorted-set.d.ts +5 -5
  33. package/src/routing/redis/network-agent.redis.d.ts +37 -13
  34. package/src/routing/redis/redis-connection.class.d.ts +69 -20
  35. package/src/routing/rpc/core/global-rpc-client.class.d.ts +9 -12
  36. package/src/register/register-network-authority.class.d.ts +0 -34
@@ -8,8 +8,8 @@ export declare class NetworkAgentRedisSortedSet {
8
8
  /**
9
9
  * Registers a network agent in the sorted set.
10
10
  *
11
- * @param { TNetworkId_S } networkId
12
- * @param { TClientId } socketId
11
+ * @param { TNetworkId_S } networkId - The network ID
12
+ * @param { TClientId } socketId - The socket/client ID
13
13
  *
14
14
  * @returns { Promise<void> }
15
15
  */
@@ -17,10 +17,10 @@ export declare class NetworkAgentRedisSortedSet {
17
17
  /**
18
18
  * Unregisters a network agent from the sorted set.
19
19
  *
20
- * @param { TNetworkId_S } networkId
21
- * @param { TClientId } socketId
20
+ * @param { TNetworkId_S } networkId - The network ID
21
+ * @param { TClientId } socketId - The socket/client ID
22
22
  *
23
- * @returns { Promise<number> }
23
+ * @returns { Promise<number> } The number of elements removed
24
24
  */
25
25
  unregisterAgent(networkId: TNetworkId_S, socketId: TClientId): Promise<number>;
26
26
  }
@@ -2,7 +2,7 @@
2
2
  * Register information on a specific network agent.
3
3
  */
4
4
  import type { RedisClient } from 'bun';
5
- import type { TAddress, TClientId, TAgentOwnUId, TNetworkId_S } from '@flux/shared/types';
5
+ import type { TClientId, TNetworkId_S } from '@flux/shared/types';
6
6
  export declare class NetworkAgentRedis {
7
7
  private readonly client;
8
8
  private readonly networkAgentRedisSortedSet;
@@ -11,29 +11,53 @@ export declare class NetworkAgentRedis {
11
11
  /**
12
12
  * Registers a network agent in the sorted set.
13
13
  *
14
- * @param { TNetworkId_S } networkId
15
- * @param { TClientId } socketId
14
+ * @param { TNetworkId_S } networkId - The network ID
15
+ * @param { TAddress } address - The client address
16
+ * @param { TClientId } clientId - The socket ID
17
+ * @param { TAgentOwnUId } uid - The agent UID
16
18
  *
17
19
  * @returns { Promise<void> }
20
+ public async registerNetworkAgent(
21
+ networkId: TNetworkId_S,
22
+ address: TAddress,
23
+ clientId: TClientId,
24
+ uid: TAgentOwnUId,
25
+ ): Promise<void> {
26
+ const key: string = `networks/${networkId}/agents/${clientId}`;
27
+
28
+ await this.client.hset(key, {
29
+ 'data-usage': '0',
30
+ 'address': address,
31
+ 'name': uid,
32
+ 'registerAt': new Date().toISOString(),
33
+ });
34
+
35
+ await this.client.expire(key, 500);
36
+
37
+ await this.networkAgentRedisSortedSet
38
+ .registerAgent(
39
+ networkId,
40
+ clientId,
41
+ );
42
+ }
18
43
  */
19
- registerNetworkAgent(networkId: TNetworkId_S, clientId: TAddress, socketId: TClientId, uid: TAgentOwnUId): Promise<void>;
20
44
  /**
21
45
  * Unregisters a network agent from the sorted set.
22
46
  *
23
- * @param { TNetworkId_S } networkId
24
- * @param { TClientId } socketId
47
+ * @param { TNetworkId_S } networkId - The network ID
48
+ * @param { TClientId } clientId - The socket ID
25
49
  *
26
- * @returns { Promise<number> }
50
+ * @returns { Promise<number> } The number of elements removed
27
51
  */
28
- unregisterNetworkAgent(networkId: TNetworkId_S, socketId: TClientId): Promise<number>;
52
+ unregisterNetworkAgent(networkId: TNetworkId_S, clientId: TClientId): Promise<number>;
29
53
  /**
54
+ * Caches the data usage for a network agent to be pushed periodically.
30
55
  *
31
- * @param socketId
32
- * @param usage
33
- *
34
- * @returns { void }
56
+ * @param { TNetworkId_S } networkId - The network ID
57
+ * @param { TClientId } clientId - The socket ID
58
+ * @param { number } usage - The data usage in bytes
35
59
  */
36
- registerDataUsage(networkId: TNetworkId_S, socketId: TClientId, usage: number): void;
60
+ registerDataUsage(networkId: TNetworkId_S, clientId: TClientId, usage: number): void;
37
61
  /**
38
62
  * Pushes the data usage to the Redis server.
39
63
  *
@@ -1,11 +1,13 @@
1
- import type { TAddress, TClientId, TProcessAddress } from '@flux/shared/types';
1
+ import type { TAddress, TClientId, TNetworkId_S, TNetworkToken_S, TProcessAddress } from '@flux/shared/types';
2
2
  import { NetworkAuthorityRedisSortedSet } from '@flux/mesh/store/redis/network-authority';
3
3
  import { NetworkAgentRedisService } from '@flux/mesh/store/redis/network-agent';
4
4
  import type { TGlobalChannel } from '../global-channel/global-channel-pubsub.class';
5
5
  /**
6
- * Singleton function to get the Redis connection
6
+ * Singleton function to get the Redis connection.
7
7
  *
8
- * @returns
8
+ * @param { string } [connectionString] - Optional connection string override
9
+ *
10
+ * @returns { RedisConnection } The singleton Redis connection
9
11
  */
10
12
  export declare const getMeshRedisConnection: (connectionString?: string) => RedisConnection;
11
13
  export type MessageCallback = (message: string, channel?: string) => unknown;
@@ -16,14 +18,20 @@ export declare class RedisConnection {
16
18
  readonly networkAuthoritySet: NetworkAuthorityRedisSortedSet;
17
19
  readonly networkAgentRedisService: NetworkAgentRedisService;
18
20
  readonly hash: {
19
- sadd: (key: Bun.RedisClient.KeyLike, member: string) => Promise<number>;
20
- hmset: (key: Bun.RedisClient.KeyLike, fieldValues: string[]) => Promise<string>;
21
+ sadd: (key: Bun.RedisClient.KeyLike, ...members: string[]) => Promise<number>;
22
+ hset: {
23
+ (key: Bun.RedisClient.KeyLike, fields: Record<string | number, Bun.RedisClient.KeyLike | number>): Promise<number>;
24
+ (key: Bun.RedisClient.KeyLike, field: Bun.RedisClient.KeyLike, value: Bun.RedisClient.KeyLike, ...rest: Bun.RedisClient.KeyLike[]): Promise<number>;
25
+ };
21
26
  smembers: (key: Bun.RedisClient.KeyLike) => Promise<string[]>;
22
- srem: (key: Bun.RedisClient.KeyLike, member: string) => Promise<number>;
27
+ srem: (key: Bun.RedisClient.KeyLike, ...members: string[]) => Promise<number>;
23
28
  scard: (key: Bun.RedisClient.KeyLike) => Promise<number>;
24
29
  del: (...keys: Bun.RedisClient.KeyLike[]) => Promise<number>;
25
30
  hincrby: (key: Bun.RedisClient.KeyLike, field: string, increment: string | number) => Promise<number>;
26
- hmget: (key: Bun.RedisClient.KeyLike, fields: string[]) => Promise<Array<string | null>>;
31
+ hmget: {
32
+ (key: Bun.RedisClient.KeyLike, ...fields: string[]): Promise<Array<string | null>>;
33
+ (key: Bun.RedisClient.KeyLike, fields: string[]): Promise<Array<string | null>>;
34
+ };
27
35
  expire: (key: Bun.RedisClient.KeyLike, seconds: number) => Promise<number>;
28
36
  };
29
37
  constructor(_optionsOrURL: string | {
@@ -31,35 +39,76 @@ export declare class RedisConnection {
31
39
  url: string;
32
40
  });
33
41
  /**
34
- *
35
- * @param { TProcessAddress } skipProcessAddress
36
- * @param { TGlobalChannel } channel
37
- * @param { string } message
38
- *
39
- * @returns { Promise<void> }
42
+ * Publishes a websocket channel event to all processes, except the one specified.
40
43
  */
41
44
  publishWebsocketChannelEvent(skipProcessAddress: TProcessAddress, channel: TGlobalChannel, message: string): Promise<void>;
42
45
  /**
43
- *
44
- * @returns { void }
46
+ * Subscribes to websocket channel events from all processes.
45
47
  */
46
48
  subscribeWebsocketChannelEvent(callback: (channel: TGlobalChannel, skipProcessAddress: TProcessAddress, message: string) => unknown): Promise<void>;
47
49
  /**
50
+ * Publishes the current set of valid token values for a network.
51
+ * Called by the portal whenever tokens are created, rotated, or deleted.
52
+ */
53
+ publishNetworkTokenEvent(networkId: TNetworkId_S, tokens: TNetworkToken_S[]): Promise<void>;
54
+ /**
55
+ * Subscribes to network token update events.
56
+ * The mesh calls this to keep its local token cache in sync.
57
+ */
58
+ subscribeToNetworkTokenEvents(callback: (networkId: TNetworkId_S, tokens: TNetworkToken_S[]) => void): void;
59
+ /**
60
+ * Persists the current set of valid token values for a network into a
61
+ * Redis Set so that mesh processes can bootstrap their local caches after
62
+ * a cold start (i.e. before any pub/sub event has been received).
48
63
  *
49
- * @param { TAddress | TProcessAddress } address
50
- * @param { string } message
64
+ * The key format is: `:flux:network-tokens/{networkId}/values`.
51
65
  *
52
- * @returns { Promise<void> }
66
+ * Called by the portal on every token mutation alongside
67
+ * {@link publishNetworkTokenEvent}.
68
+ */
69
+ setNetworkTokenValues(networkId: TNetworkId_S, tokens: TNetworkToken_S[]): Promise<void>;
70
+ /**
71
+ * Reads the set of valid token values for a network from the persistent
72
+ * Redis Set written by the portal.
73
+ *
74
+ * Used by {@link NetworkTokenCache} for cold-start bootstrapping.
75
+ */
76
+ getNetworkTokenValues(networkId: TNetworkId_S): Promise<TNetworkToken_S[]>;
77
+ publishCustom(subChannel: 'kick-client', destinationProcessAddress: TProcessAddress, message: string): Promise<void>;
78
+ subscribeToCustom(subChannel: 'kick-client', destinationProcessAddress: TProcessAddress, callback: (data: string) => void): void;
79
+ /**
80
+ * Publishes a message directly to an address.
53
81
  */
54
82
  directPublish(address: TAddress | TProcessAddress, message: string): Promise<void>;
83
+ /**
84
+ * Subscribes to messages on a Redis channel.
85
+ */
55
86
  subscribe(channelId: TProcessAddress | TClientId, callback: MessageCallback): void;
87
+ /**
88
+ * Unsubscribes from messages on a Redis channel.
89
+ */
56
90
  unsubscribe(channelId: string, callback: MessageCallback): void;
91
+ /**
92
+ * Subscribes to data packets published on a specific network channel.
93
+ * Invokes the callback with the raw data string from each packet.
94
+ */
95
+ subscribeToNetworkChannel(networkId: string, channelName: string, callback: (clientId: TClientId, data: string) => void): void;
96
+ /**
97
+ * Unsubscribes a callback from data packets on a specific network channel.
98
+ */
99
+ unsubscribeFromNetworkChannel(networkId: string, channelName: string, callback: (clientId: TClientId, data: string) => void): void;
100
+ /**
101
+ * Marks the given address as connected in Redis.
102
+ */
57
103
  setConnected(address: string): Promise<void>;
104
+ /**
105
+ * Marks the given address as disconnected in Redis.
106
+ */
58
107
  setDisconnected(_address: string): Promise<void>;
59
108
  /**
60
109
  * Disconnects the Redis cache and pub/sub.
61
110
  *
62
- * @returns { void }
111
+ * @returns { Promise<void> }
63
112
  */
64
- disconnect(): void;
113
+ disconnect(): Promise<void>;
65
114
  }
@@ -14,18 +14,17 @@ export declare class GlobalRPCClient<TMethods> {
14
14
  /**
15
15
  * Calls a function on the other side.
16
16
  *
17
- * @param { TAddress } rpcServerClientAddress
18
- * @param { TMethods } method
17
+ * @param { TAddress } rpcServerClientAddress - The address of the RPC server client
18
+ * @param { TMethods } method - The method name to call
19
+ * @param { any[] } params - The parameters to pass
19
20
  *
20
- * @returns { Promise<any> }
21
+ * @returns { Promise<any> } The result from the remote method
21
22
  */
22
23
  call(rpcServerClientAddress: TAddress, method: TMethods, ...params: any): Promise<any>;
23
24
  /**
24
25
  * Handles the response from the server.
25
26
  *
26
- * @param { RPCResponse } response
27
- *
28
- * @returns { boolean }
27
+ * @param { RPCResponse } response - The response from the RPC server
29
28
  */
30
29
  private handleResponseMessage;
31
30
  }
@@ -40,18 +39,16 @@ export declare class GlobalRPCClient2<TMethods> {
40
39
  /**
41
40
  * Calls a function on the other side.
42
41
  *
43
- * @param { TAddress } rpcServerClientAddress
44
- * @param { TMethods } method
42
+ * @param { TMethods } method - The method name to call
43
+ * @param { any[] } params - The parameters to pass
45
44
  *
46
- * @returns { Promise<any> }
45
+ * @returns { Promise<any> } The result from the remote method
47
46
  */
48
47
  call(method: TMethods, ...params: any): Promise<any>;
49
48
  /**
50
49
  * Handles the response from the server.
51
50
  *
52
- * @param { RPCResponse } response
53
- *
54
- * @returns { boolean }
51
+ * @param { RPCResponse } response - The response from the RPC server
55
52
  */
56
53
  private handleResponseMessage;
57
54
  }
@@ -1,34 +0,0 @@
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
- }