@soundworks/core 4.0.0-alpha.27 → 4.0.0-alpha.29

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 (128) hide show
  1. package/.jsdoc +3 -1
  2. package/CHANGELOG.md +15 -5
  3. package/package.json +5 -5
  4. package/src/client/Client.js +78 -83
  5. package/src/client/{Context.js → ClientContext.js} +48 -38
  6. package/src/client/ClientContextManager.js +115 -0
  7. package/src/client/{Plugin.js → ClientPlugin.js} +22 -18
  8. package/src/client/{PluginManager.js → ClientPluginManager.js} +39 -35
  9. package/src/client/{Socket.js → ClientSocket.js} +54 -65
  10. package/src/client/{StateManager.js → ClientStateManager.js} +7 -8
  11. package/src/client/index.js +17 -41
  12. package/src/common/BasePlugin.js +56 -51
  13. package/src/common/BasePluginManager.js +175 -139
  14. package/src/common/BaseStateManager.js +316 -120
  15. package/src/common/BatchedTransport.js +24 -24
  16. package/src/common/ParameterBag.js +18 -6
  17. package/src/common/PromiseStore.js +1 -0
  18. package/src/common/{BaseSharedState.js → SharedState.js} +271 -199
  19. package/src/common/{BaseSharedStateCollection.js → SharedStateCollection.js} +92 -61
  20. package/src/common/logger.js +5 -0
  21. package/src/common/shared-state-types.js +76 -0
  22. package/src/common/version.js +1 -1
  23. package/src/server/Server.js +391 -354
  24. package/src/server/ServerClient.js +73 -0
  25. package/src/server/{Context.js → ServerContext.js} +85 -66
  26. package/src/server/{ContextManager.js → ServerContextManager.js} +98 -85
  27. package/src/server/{Plugin.js → ServerPlugin.js} +10 -24
  28. package/src/server/{PluginManager.js → ServerPluginManager.js} +76 -69
  29. package/src/server/{Socket.js → ServerSocket.js} +19 -18
  30. package/src/server/{Sockets.js → ServerSockets.js} +36 -27
  31. package/src/server/ServerStateManager.js +515 -0
  32. package/src/{common → server}/SharedStatePrivate.js +61 -26
  33. package/src/server/audit-network-latency.worker.js +9 -7
  34. package/src/server/audit-schema.js +1 -1
  35. package/src/server/crypto.js +2 -0
  36. package/src/server/index.js +18 -21
  37. package/types/client/Client.d.ts +74 -95
  38. package/types/client/{Context.d.ts → ClientContext.d.ts} +25 -26
  39. package/types/client/{ContextManager.d.ts → ClientContextManager.d.ts} +19 -22
  40. package/types/client/{Plugin.d.ts → ClientPlugin.d.ts} +17 -20
  41. package/types/client/{PluginManager.d.ts → ClientPluginManager.d.ts} +13 -36
  42. package/types/client/ClientSocket.d.ts +74 -0
  43. package/types/client/{StateManager.d.ts → ClientStateManager.d.ts} +11 -15
  44. package/types/client/index.d.ts +26 -2
  45. package/types/common/BasePlugin.d.ts +52 -46
  46. package/types/common/BasePluginManager.d.ts +68 -44
  47. package/types/common/BaseStateManager.d.ts +207 -97
  48. package/types/common/BatchedTransport.d.ts +1 -5
  49. package/types/common/ParameterBag.d.ts +16 -18
  50. package/types/common/PromiseStore.d.ts +1 -1
  51. package/types/common/SharedState.d.ts +297 -0
  52. package/types/common/{BaseSharedStateCollection.d.ts → SharedStateCollection.d.ts} +34 -18
  53. package/types/common/constants.d.ts +0 -2
  54. package/types/common/logger.d.ts +2 -1
  55. package/types/common/shared-state-types.d.ts +83 -0
  56. package/types/common/sockets-utils.d.ts +0 -5
  57. package/types/common/version.d.ts +1 -2
  58. package/types/server/Server.d.ts +78 -132
  59. package/types/server/ServerClient.d.ts +45 -0
  60. package/types/server/{Context.d.ts → ServerContext.d.ts} +53 -43
  61. package/types/server/ServerContextManager.d.ts +80 -0
  62. package/types/server/{Plugin.d.ts → ServerPlugin.d.ts} +19 -33
  63. package/types/server/{PluginManager.d.ts → ServerPluginManager.d.ts} +39 -37
  64. package/types/server/ServerSocket.d.ts +85 -0
  65. package/types/server/ServerSockets.d.ts +50 -0
  66. package/types/server/ServerStateManager.d.ts +189 -0
  67. package/types/server/SharedStatePrivate.d.ts +21 -0
  68. package/types/server/audit-network-latency.worker.d.ts +1 -2
  69. package/types/server/audit-schema.d.ts +0 -1
  70. package/types/server/crypto.d.ts +2 -1
  71. package/types/server/index.d.ts +24 -2
  72. package/src/client/ContextManager.js +0 -111
  73. package/src/client/SharedState.js +0 -84
  74. package/src/client/SharedStateCollection.js +0 -38
  75. package/src/server/Client.js +0 -55
  76. package/src/server/SharedState.js +0 -85
  77. package/src/server/SharedStateCollection.js +0 -39
  78. package/src/server/StateManager.js +0 -696
  79. package/types/client/Client.d.ts.map +0 -1
  80. package/types/client/Context.d.ts.map +0 -1
  81. package/types/client/ContextManager.d.ts.map +0 -1
  82. package/types/client/Plugin.d.ts.map +0 -1
  83. package/types/client/PluginManager.d.ts.map +0 -1
  84. package/types/client/SharedState.d.ts +0 -85
  85. package/types/client/SharedState.d.ts.map +0 -1
  86. package/types/client/SharedStateCollection.d.ts +0 -41
  87. package/types/client/SharedStateCollection.d.ts.map +0 -1
  88. package/types/client/Socket.d.ts +0 -149
  89. package/types/client/Socket.d.ts.map +0 -1
  90. package/types/client/StateManager.d.ts.map +0 -1
  91. package/types/client/index.d.ts.map +0 -1
  92. package/types/common/BasePlugin.d.ts.map +0 -1
  93. package/types/common/BasePluginManager.d.ts.map +0 -1
  94. package/types/common/BaseSharedState.d.ts +0 -244
  95. package/types/common/BaseSharedState.d.ts.map +0 -1
  96. package/types/common/BaseSharedStateCollection.d.ts.map +0 -1
  97. package/types/common/BaseStateManager.d.ts.map +0 -1
  98. package/types/common/BatchedTransport.d.ts.map +0 -1
  99. package/types/common/ParameterBag.d.ts.map +0 -1
  100. package/types/common/PromiseStore.d.ts.map +0 -1
  101. package/types/common/SharedStatePrivate.d.ts +0 -21
  102. package/types/common/SharedStatePrivate.d.ts.map +0 -1
  103. package/types/common/constants.d.ts.map +0 -1
  104. package/types/common/logger.d.ts.map +0 -1
  105. package/types/common/sockets-utils.d.ts.map +0 -1
  106. package/types/common/version.d.ts.map +0 -1
  107. package/types/server/Client.d.ts +0 -45
  108. package/types/server/Client.d.ts.map +0 -1
  109. package/types/server/Context.d.ts.map +0 -1
  110. package/types/server/ContextManager.d.ts +0 -69
  111. package/types/server/ContextManager.d.ts.map +0 -1
  112. package/types/server/Plugin.d.ts.map +0 -1
  113. package/types/server/PluginManager.d.ts.map +0 -1
  114. package/types/server/Server.d.ts.map +0 -1
  115. package/types/server/SharedState.d.ts +0 -86
  116. package/types/server/SharedState.d.ts.map +0 -1
  117. package/types/server/SharedStateCollection.d.ts +0 -41
  118. package/types/server/SharedStateCollection.d.ts.map +0 -1
  119. package/types/server/Socket.d.ts +0 -164
  120. package/types/server/Socket.d.ts.map +0 -1
  121. package/types/server/Sockets.d.ts +0 -80
  122. package/types/server/Sockets.d.ts.map +0 -1
  123. package/types/server/StateManager.d.ts +0 -405
  124. package/types/server/StateManager.d.ts.map +0 -1
  125. package/types/server/audit-network-latency.worker.d.ts.map +0 -1
  126. package/types/server/audit-schema.d.ts.map +0 -1
  127. package/types/server/crypto.d.ts.map +0 -1
  128. package/types/server/index.d.ts.map +0 -1
@@ -1,21 +1,4 @@
1
- export default Plugin;
2
- export namespace server {
3
- /**
4
- * ~onStateChangeCallback
5
- */
6
- type Plugin = (#state: server.Plugin) => any;
7
- }
8
- /**
9
- * Callback executed when the plugin state is updated.
10
- *
11
- * @callback server.Plugin~onStateChangeCallback
12
- * @param {server.Plugin#state} state - Current state of the plugin.
13
- */
14
- /**
15
- * Delete the registered {@link server.Plugin~onStateChangeCallback}.
16
- *
17
- * @callback server.Plugin~deleteOnStateChangeCallback
18
- */
1
+ export default ServerPlugin;
19
2
  /**
20
3
  * Base class to extend in order to create the server-side counterpart of a
21
4
  * `soundworks` plugin.
@@ -31,41 +14,44 @@ export namespace server {
31
14
  *
32
15
  * _Creating new plugins should be considered an advanced usage._
33
16
  *
34
- * @memberof server
35
17
  * @extends BasePlugin
36
18
  * @inheritdoc
37
19
  */
38
- declare class Plugin extends BasePlugin {
20
+ declare class ServerPlugin extends BasePlugin {
39
21
  /**
40
- * @param {server.Server} server - The soundworks server instance.
22
+ * @param {Server} server - The soundworks server instance.
41
23
  * @param {string} id - User defined id of the plugin as defined in
42
- * {@link server.PluginManager#register}.
24
+ * {@link ServerPluginManager#register}.
43
25
  */
44
- constructor(server: server.Server, id: string);
26
+ constructor(server: Server, id: string);
45
27
  /**
46
28
  * Instance of soundworks server.
47
- * @type {server.Server}
48
- * @see {@link server.Server}
29
+ * @type {Server}
30
+ * @see {@link Server}
31
+ */
32
+ get server(): Server;
33
+ /**
34
+ * Set of the clients registered in the plugin.
35
+ * @type {Set<ServerClient>}
36
+ * @see {@link ServerClient}
49
37
  */
50
- server: server.Server;
51
- /** @private */
52
- private clients;
38
+ get clients(): Set<ServerClient>;
53
39
  /**
54
40
  * Method called when a client (which registered the client-side plugin),
55
41
  * connects to the application. Override this method if you need to perform
56
42
  * some particular logic (e.g. creating a shared state) for each clients.
57
43
  *
58
- * @param {server.Client} client
44
+ * @param {ServerClient} client
59
45
  */
60
- addClient(client: server.Client): Promise<void>;
46
+ addClient(client: ServerClient): Promise<void>;
61
47
  /**
62
48
  * Method called when a client (which registered the client-side plugin),
63
49
  * disconnects from the application. Override this method if you need to perform
64
50
  * some particular logic (e.g. creating a shared state) for each clients.
65
51
  *
66
- * @param {server.Client} client
52
+ * @param {ServerClient} client
67
53
  */
68
- removeClient(client: server.Client): Promise<void>;
54
+ removeClient(client: ServerClient): Promise<void>;
55
+ #private;
69
56
  }
70
57
  import BasePlugin from '../common/BasePlugin.js';
71
- //# sourceMappingURL=Plugin.d.ts.map
@@ -1,29 +1,12 @@
1
- export default PluginManager;
2
- export namespace server {
3
- /**
4
- * ~onStateChangeCallback
5
- */
6
- type PluginManager = (: object, initiator: server.Plugin | null) => any;
7
- }
8
- /**
9
- * Callback executed when a plugin internal state is updated.
10
- *
11
- * @callback server.PluginManager~onStateChangeCallback
12
- * @param {object<server.Plugin#id, server.Plugin>} fullState - List of all plugins.
13
- * @param {server.Plugin|null} initiator - Plugin that initiated the update or `null`
14
- * if the change was initiated by the state manager (i.e. when the initialization
15
- * of the plugins starts).
16
- */
17
- /**
18
- * Delete the registered {@link server.PluginManager~onStateChangeCallback}.
19
- *
20
- * @callback server.PluginManager~deleteOnStateChangeCallback
21
- */
1
+ export const kServerPluginManagerCheckRegisteredPlugins: unique symbol;
2
+ export const kServerPluginManagerAddClient: unique symbol;
3
+ export const kServerPluginManagerRemoveClient: unique symbol;
4
+ export default ServerPluginManager;
22
5
  /**
23
6
  * The `PluginManager` allows to register and retrieve `soundworks` plugins.
24
7
  *
25
8
  * Plugins should always be registered both client-side and server-side,
26
- * and before {@link client.Client#start} or {@link server.Server#start}
9
+ * and before {@link Client#start} or {@link Server#start}
27
10
  * to be properly initialized.
28
11
  *
29
12
  * In some sitautions, you might want to register the same plugin factory several times
@@ -71,15 +54,37 @@ export namespace server {
71
54
  * }, 1000);
72
55
  * ```
73
56
  *
74
- * @memberof server
75
57
  * @extends BasePluginManager
76
58
  * @inheritdoc
77
59
  * @hideconstructor
78
60
  */
79
- declare class PluginManager extends BasePluginManager {
80
- register(id: any, factory?: any, options?: {}, deps?: any[]): void;
61
+ declare class ServerPluginManager extends BasePluginManager {
62
+ /**
63
+ * Register a plugin into the manager.
64
+ *
65
+ * _A plugin must always be registered both on client-side and on server-side_
66
+ *
67
+ * Refer to the plugin documentation to check its options and proper way of
68
+ * registering it.
69
+ *
70
+ * @param {string} id - Unique id of the plugin. Enables the registration of the
71
+ * same plugin factory under different ids.
72
+ * @param {Function} factory - Factory function that returns the Plugin class.
73
+ * @param {object} [options={}] - Options to configure the plugin.
74
+ * @param {array} [deps=[]] - List of plugins' names the plugin depends on, i.e.
75
+ * the plugin initialization will start only after the plugins it depends on are
76
+ * fully started themselves.
77
+ * @see {@link ClientPluginManager#register}
78
+ * @see {@link ServerPluginManager#register}
79
+ * @example
80
+ * // client-side
81
+ * client.pluginManager.register('user-defined-id', pluginFactory);
82
+ * // server-side
83
+ * server.pluginManager.register('user-defined-id', pluginFactory);
84
+ */
85
+ register(id: string, factory?: Function, options?: object, deps?: any[]): void;
81
86
  /**
82
- * Retrieve an fully started instance of a registered plugin.
87
+ * Retrieve a fully started instance of a registered plugin.
83
88
  *
84
89
  * Be aware that the `get` method resolves only when the plugin is fully 'started',
85
90
  * which is what we want 99.99% of the time. As such, and to prevent the application
@@ -93,19 +98,16 @@ declare class PluginManager extends BasePluginManager {
93
98
  * _Note: the async API is designed to enable the dynamic creation of plugins
94
99
  * (hopefully without brealing changes) in a future release._
95
100
  *
96
- * @param {server.Plugin#id} id - Id of the plugin as defined when registered.
97
- * @returns {server.Plugin}
98
- * @see {@link server.PluginManager#onStateChange}
99
- *
100
- * @private
101
+ * @param {ServerPlugin#id} id - Id of the plugin as defined when registered.
102
+ * @returns {ServerPlugin}
101
103
  */
102
- private get;
103
- /** private */
104
- checkRegisteredPlugins(registeredPlugins: any): void;
104
+ get(id: any): ServerPlugin;
105
+ /** @private */
106
+ private [kServerPluginManagerCheckRegisteredPlugins];
105
107
  /** @private */
106
- private addClient;
108
+ private [kServerPluginManagerAddClient];
107
109
  /** @private */
108
- private removeClient;
110
+ private [kServerPluginManagerRemoveClient];
109
111
  }
110
112
  import BasePluginManager from '../common/BasePluginManager.js';
111
- //# sourceMappingURL=PluginManager.d.ts.map
113
+ import ServerPlugin from './ServerPlugin.js';
@@ -0,0 +1,85 @@
1
+ export const kSocketClientId: unique symbol;
2
+ export const kSocketTerminate: unique symbol;
3
+ export default ServerSocket;
4
+ /**
5
+ * Simple publish / subscribe wrapper built on top of the
6
+ * [ws](https://github.com/websockets/ws) library.
7
+ *
8
+ * An instance of {@link ServerSocket} is automatically created per client
9
+ * when it connects (see {@link SeverClient#socket}).
10
+ *
11
+ * _Important: In most cases, you should consider using a {@link SharedState}
12
+ * rather than directly using the Socket instance._
13
+ *
14
+ * @hideconstructor
15
+ */
16
+ declare class ServerSocket {
17
+ constructor(ws: any, sockets: any);
18
+ /**
19
+ * Reference to the @link{ServerSockets} instance.
20
+ *
21
+ * Allows for broadcasting from a given socket instance.
22
+ *
23
+ * @type {ServerSockets}
24
+ * @example
25
+ * socket.sockets.broadcast('my-room', this, 'update-value', 1);
26
+ */
27
+ get sockets(): ServerSockets;
28
+ /**
29
+ * Reay state of the underlying socket instance.
30
+ *
31
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState}
32
+ * @type {number}
33
+ */
34
+ get readyState(): number;
35
+ /**
36
+ * Send messages with JSON compatible data types on a given channel.
37
+ *
38
+ * @param {string} channel - Channel name.
39
+ * @param {...*} args - Payload of the message. As many arguments as needed, of
40
+ * JSON compatible data types (i.e. string, number, boolean, object, array and null).
41
+ */
42
+ send(channel: string, ...args: any[]): void;
43
+ /**
44
+ * Listen messages with JSON compatible data types on a given channel.
45
+ *
46
+ * @param {string} channel - Channel name.
47
+ * @param {Function} callback - Callback to execute when a message is received.
48
+ * Arguments of the callback function will match the arguments sent using the
49
+ * {@link ServerSocket#send} method.
50
+ */
51
+ addListener(channel: string, callback: Function): void;
52
+ /**
53
+ * Remove a listener of messages with JSON compatible data types from a given channel.
54
+ *
55
+ * @param {string} channel - Channel name.
56
+ * @param {Function} callback - Callback to remove.
57
+ */
58
+ removeListener(channel: string, callback: Function): void;
59
+ /**
60
+ * Remove all listeners of messages with JSON compatible data types.
61
+ *
62
+ * @param {string} channel - Channel name.
63
+ */
64
+ removeAllListeners(channel?: string): void;
65
+ /**
66
+ * Add the socket to a room
67
+ * @param {string} roomId - Id of the room.
68
+ */
69
+ addToRoom(roomId: string): void;
70
+ /**
71
+ * Remove the socket from a room
72
+ * @param {string} roomId - Id of the room.
73
+ */
74
+ removeFromRoom(roomId: string): void;
75
+ /**
76
+ * Removes all listeners and immediately close the web socket.
77
+ *
78
+ * Is automatically called when socket is closed on the client side or when
79
+ * Server is stopped
80
+ *
81
+ * @private
82
+ */
83
+ private [kSocketTerminate];
84
+ #private;
85
+ }
@@ -0,0 +1,50 @@
1
+ export const kSocketsStart: unique symbol;
2
+ export const kSocketsStop: unique symbol;
3
+ export const kSocketsRemoveFromAllRooms: unique symbol;
4
+ export const kSocketsLatencyStatsWorker: unique symbol;
5
+ export const kSocketsDebugPreventHeartBeat: unique symbol;
6
+ export default ServerSockets;
7
+ /**
8
+ * Manage all {@link ServerSocket} instances.
9
+ *
10
+ * _Important: In most cases, you should consider using a {@link SharedState}
11
+ * rather than directly using the ServerSocket instance._
12
+ */
13
+ declare class ServerSockets {
14
+ constructor(server: any, config: any);
15
+ /**
16
+ * Add a socket to a room.
17
+ *
18
+ * _Note that in most cases, you should use a {@link SharedState} instead_
19
+ *
20
+ * @param {ServerSocket} socket - Socket to add to the room.
21
+ * @param {String} roomId - Id of the room.
22
+ */
23
+ addToRoom(socket: ServerSocket, roomId: string): void;
24
+ /**
25
+ * Remove a socket from a room.
26
+ *
27
+ * _Note that in most cases, you should use a {@link SharedState} instead_
28
+ *
29
+ * @param {ServerSocket} socket - Socket to remove from the room.
30
+ * @param {String} roomId - Id of the room.
31
+ */
32
+ removeFromRoom(socket: ServerSocket, roomId: string): void;
33
+ /**
34
+ * Send a message to all clients os given room(s). If no room is specified,
35
+ * the message is sent to all clients.
36
+ *
37
+ * _Note that in most cases, you should use a {@link SharedState} instead_
38
+ *
39
+ * @param {String|Array} roomsIds - Ids of the rooms that must receive
40
+ * the message. If `null` the message is sent to all clients.
41
+ * @param {ServerSocket} excludeSocket - Optionnal socket to ignore when
42
+ * broadcasting the message, typically the client at the origin of the message.
43
+ * @param {String} channel - Channel name.
44
+ * @param {...*} args - Payload of the message. As many arguments as needed, of
45
+ * JSON compatible data types (i.e. string, number, boolean, object, array and null).
46
+ */
47
+ broadcast(roomIds: any, excludeSocket: ServerSocket, channel: string, ...args: any[]): void;
48
+ #private;
49
+ }
50
+ import ServerSocket from './ServerSocket.js';
@@ -0,0 +1,189 @@
1
+ export const kServerStateManagerAddClient: unique symbol;
2
+ export const kServerStateManagerRemoveClient: unique symbol;
3
+ export const kServerStateManagerDeletePrivateState: unique symbol;
4
+ export const kServerStateManagerGetHooks: unique symbol;
5
+ export const kStateManagerClientsByNodeId: unique symbol;
6
+ export default ServerStateManager;
7
+ export type serverStateManagerUpdateHook = () => any;
8
+ /**
9
+ * @callback serverStateManagerUpdateHook
10
+ * @async
11
+ *
12
+ * @param {object} updates - Update object as given on a set callback, or
13
+ * result of the previous hook
14
+ * @param {object} currentValues - Current values of the state.
15
+ * @param {object} [context=null] - Optionnal context passed by the creator
16
+ * of the update.
17
+ *
18
+ * @returns {object} The "real" updates to be applied on the state.
19
+ */
20
+ /**
21
+ * The `StateManager` allows to create new {@link SharedState}s, or attach
22
+ * to {@link SharedState}s created by other nodes (clients or server). It
23
+ * can also track all the {@link SharedState}s created by other nodes.
24
+ *
25
+ * An instance of `StateManager` is automatically created by the `soundworks.Server`
26
+ * at initialization (cf. {@link Server#stateManager}).
27
+ *
28
+ * Compared to the {@link ClientStateManager}, the `ServerStateManager` can also
29
+ * register and delete schemas, as well as register update hook that are executed when
30
+ * a state is updated.
31
+ *
32
+ * See {@link Server#stateManager}
33
+ *
34
+ * Tutorial: {@link https://soundworks.dev/guide/state-manager.html}
35
+ *
36
+ * ```
37
+ * // server-side
38
+ * import { Server } from '@soundworks/server/index.js';
39
+ *
40
+ * const server = new Server(config);
41
+ * // declare and register the schema of a shared state.
42
+ * server.stateManager.registerSchema('some-global-state', {
43
+ * myRandom: {
44
+ * type: 'float',
45
+ * default: 0,
46
+ * }
47
+ * });
48
+ *
49
+ * await server.start();
50
+ *
51
+ * // create a global state server-side
52
+ * const globalState = await server.stateManager.create('some-global-state');
53
+ * // listen and react to the changes made by the clients
54
+ * globalState.onUpdate(updates => console.log(updates));
55
+ * ```
56
+ *
57
+ * ```
58
+ * // client-side
59
+ * import { Client } from '@soundworks/client.index.js';
60
+ *
61
+ * const client = new Client(config);
62
+ * await client.start();
63
+ *
64
+ * // attach to the global state created by the server
65
+ * const globalState = await client.stateManager.attach('some-global-state');
66
+ *
67
+ * // update the value of a `myRandom` parameter every seconds
68
+ * setInterval(() => {
69
+ * globalState.set({ myRandom: Math.random() });
70
+ * }, 1000);
71
+ * ```
72
+ *
73
+ * @extends BaseStateManager
74
+ * @inheritdoc
75
+ * @hideconstructor
76
+ */
77
+ declare class ServerStateManager extends BaseStateManager {
78
+ /**
79
+ * Define a class of data structure from which {@link SharedState} can be instanciated.
80
+ *
81
+ * _In a future revision, this method and its arguments will be renamed_
82
+ *
83
+ * @param {SharedStateClassName} schemaName - Name of the schema.
84
+ * @param {SharedStateSchema} schema - Data structure
85
+ * describing the states that will be created from this schema.
86
+ *
87
+ * @see {@link ServerStateManager#create}
88
+ * @see {@link ClientStateManager#create}
89
+ *
90
+ * @example
91
+ * server.stateManager.registerSchema('my-schema', {
92
+ * myBoolean: {
93
+ * type: 'boolean'
94
+ * default: false,
95
+ * },
96
+ * myFloat: {
97
+ * type: 'float'
98
+ * default: 0.1,
99
+ * min: -1,
100
+ * max: 1
101
+ * }
102
+ * })
103
+ */
104
+ registerSchema(schemaName: SharedStateClassName, schema: SharedStateSchema): void;
105
+ /**
106
+ * Delete a whole class of {@link ShareState}.
107
+ *
108
+ * All {@link SharedState} instance that belong to this class are deleted
109
+ * as well, triggering the `onDetach` and `onDelete` callbacks are called on
110
+ * the actual {@link SharedState} instances.
111
+ *
112
+ * _In a future revision, this method and its arguments will be renamed_
113
+ *
114
+ * @param {SharedStateClassName} schemaName - Name of the schema.
115
+ */
116
+ deleteSchema(schemaName: SharedStateClassName): void;
117
+ /**
118
+ * Register a function for a given schema (e.g. will be applied on all states
119
+ * created from this schema) that will be executed before the update values
120
+ * are propagated. For example, this could be used to implement a preset system
121
+ * where all the values of the state are updated from e.g. some data stored in
122
+ * filesystem while the consumer of the state only want to update the preset name.
123
+ *
124
+ * The hook is associated to every state of its kind (i.e. schemaName) and
125
+ * executed on every update (call of `set`). Note that the hooks are executed
126
+ * server-side regarless the node on which `set` has been called and before
127
+ * the "actual" update of the state (e.g. before the call of `onUpdate`).
128
+ *
129
+ * @param {string} schemaName - Kind of states on which applying the hook.
130
+ * @param {serverStateManagerUpdateHook} updateHook - Function
131
+ * called between the `set` call and the actual update.
132
+ *
133
+ * @returns {Fuction} deleteHook - Handler that deletes the hook when executed.
134
+ *
135
+ * @example
136
+ * server.stateManager.registerSchema('hooked', {
137
+ * value: { type: 'string', default: null, nullable: true },
138
+ * numUpdates: { type: 'integer', default: 0 },
139
+ * });
140
+ * server.stateManager.registerUpdateHook('hooked', updates => {
141
+ * return {
142
+ * ...updates
143
+ * numUpdates: currentValues.numUpdates + 1,
144
+ * };
145
+ * });
146
+ *
147
+ * const state = await server.stateManager.create('hooked');
148
+ *
149
+ * await state.set({ value: 'test' });
150
+ * const values = state.getValues();
151
+ * assert.deepEqual(result, { value: 'test', numUpdates: 1 });
152
+ */
153
+ registerUpdateHook(schemaName: string, updateHook: serverStateManagerUpdateHook): Fuction;
154
+ /** @private */
155
+ private [kServerStateManagerDeletePrivateState];
156
+ /** @private */
157
+ private [kServerStateManagerGetHooks];
158
+ /** @private */
159
+ private [kStateManagerInit];
160
+ /**
161
+ * Add a client to the manager.
162
+ *
163
+ * This is automatically handled by the {@link Server} when a client connects.
164
+ *
165
+ * @param {number} nodeId - Id of the client node, as given in
166
+ * {@link client.StateManager}
167
+ * @param {object} transport - Transport mecanism to communicate with the
168
+ * client. Must implement a basic EventEmitter API.
169
+ *
170
+ * @private
171
+ */
172
+ private [kServerStateManagerAddClient];
173
+ /**
174
+ * Remove a client from the manager. Clean all created or attached states.
175
+ *
176
+ * This is automatically handled by the {@link Server} when a client disconnects.
177
+ *
178
+ * @param {number} nodeId - Id of the client node, as given in
179
+ * {@link client.StateManager}
180
+ *
181
+ * @private
182
+ */
183
+ private [kServerStateManagerRemoveClient];
184
+ /** @private */
185
+ private [kStateManagerClientsByNodeId];
186
+ #private;
187
+ }
188
+ import BaseStateManager from '../common/BaseStateManager.js';
189
+ import { kStateManagerInit } from '../common/BaseStateManager.js';
@@ -0,0 +1,21 @@
1
+ export const kSharedStatePrivateAttachClient: unique symbol;
2
+ export const kSharedStatePrivateDetachClient: unique symbol;
3
+ export default SharedStatePrivate;
4
+ /**
5
+ * The "real" state, this instance is kept private by the server.StateManager.
6
+ * It can only be accessed through a SharedState proxy.
7
+ *
8
+ * @private
9
+ */
10
+ declare class SharedStatePrivate {
11
+ constructor(id: any, schemaName: any, schema: any, manager: any, initValues?: {});
12
+ get id(): any;
13
+ get schemaName(): any;
14
+ get creatorId(): any;
15
+ get creatorRemoteId(): any;
16
+ get attachedClients(): Map<any, any>;
17
+ get parameters(): any;
18
+ [kSharedStatePrivateAttachClient](remoteId: any, client: any, isOwner: any, filter: any): void;
19
+ [kSharedStatePrivateDetachClient](remoteId: any, client: any): void;
20
+ #private;
21
+ }
@@ -1,3 +1,2 @@
1
- declare const _default: "\nconst { parentPort } = require('node:worker_threads');\n\nlet stack = [];\nlet averageLatencyWindow = 5;\nlet averageLatencyPeriod = 2;\n\nlet intervalId = null;\nlet meanLatency = 0;\n\nlet getTime;\nlet inited = new Promise(async (resolve) => {\n module = await import('@ircam/sc-utils');\n getTime = module.getTime;\n resolve(true);\n});\n\nparentPort.on('message', async msg => {\n switch (msg.type) {\n case 'config': {\n averageLatencyWindow = msg.value.averageLatencyWindow;\n averageLatencyPeriod = msg.value.averageLatencyPeriod;\n\n await inited;\n\n // launch compute in its own loop so that the number of computation is\n // decoupled from the number of connected clients\n clearInterval(intervalId);\n intervalId = setInterval(computeAverageLatency, averageLatencyPeriod * 1000);\n break;\n }\n case 'add-measurement': {\n // compute latency considering ping and pong time are equal\n const entry = msg.value;\n entry.latency = (entry.pong - entry.ping) / 2;\n // add new measure to the stack\n stack.push(entry);\n }\n }\n});\n\nfunction computeAverageLatency() {\n const now = getTime();\n // remove old measurements from stack\n stack = stack.filter(entry => entry.pong >= now - averageLatencyWindow);\n\n // prevent dividing by zero, just return last computed value\n if (stack.length === 0) {\n parentPort.postMessage({\n type: 'computed-average-latency',\n value: meanLatency,\n });\n } else {\n // compute mean\n let sum = 0;\n stack.forEach(entry => sum += entry.latency);\n\n meanLatency = sum / stack.length;\n // send result to main thread\n parentPort.postMessage({\n type: 'computed-average-latency',\n value: meanLatency,\n });\n }\n}\n";
1
+ declare const _default: "\nconst { parentPort } = require('node:worker_threads');\nconst { hrtime } = require('node:process');\n\nlet stack = [];\nlet averageLatencyWindow = 5;\nlet averageLatencyPeriod = 2;\n\nlet intervalId = null;\nlet meanLatency = 0;\n\n// workaround that sc-utils is pure emascript module\n// 2024/09/06 - Just copy getTime implementation so that we don't even need the node_modules\nconst start = hrtime.bigint();\n\nfunction getTime() {\n const now = hrtime.bigint();\n const delta = now - start;\n return Number(delta) * 1e-9;\n}\n\nparentPort.on('message', async msg => {\n switch (msg.type) {\n case 'config': {\n averageLatencyWindow = msg.value.averageLatencyWindow;\n averageLatencyPeriod = msg.value.averageLatencyPeriod;\n\n // launch compute in its own loop so that the number of computation is\n // decoupled from the number of connected clients\n clearInterval(intervalId);\n intervalId = setInterval(computeAverageLatency, averageLatencyPeriod * 1000);\n break;\n }\n case 'add-measurement': {\n // compute latency considering ping and pong time are equal\n const entry = msg.value;\n entry.latency = (entry.pong - entry.ping) / 2;\n // add new measure to the stack\n stack.push(entry);\n }\n }\n});\n\nfunction computeAverageLatency() {\n const now = getTime();\n // remove old measurements from stack\n stack = stack.filter(entry => entry.pong >= now - averageLatencyWindow);\n\n // prevent dividing by zero, just return last computed value\n if (stack.length === 0) {\n parentPort.postMessage({\n type: 'computed-average-latency',\n value: meanLatency,\n });\n } else {\n // compute mean\n let sum = 0;\n stack.forEach(entry => sum += entry.latency);\n\n meanLatency = sum / stack.length;\n // send result to main thread\n parentPort.postMessage({\n type: 'computed-average-latency',\n value: meanLatency,\n });\n }\n}\n";
2
2
  export default _default;
3
- //# sourceMappingURL=audit-network-latency.worker.d.ts.map
@@ -24,4 +24,3 @@ declare namespace _default {
24
24
  }
25
25
  }
26
26
  export default _default;
27
- //# sourceMappingURL=audit-schema.d.ts.map
@@ -1,3 +1,4 @@
1
+ /** @private */
1
2
  export function encryptData(obj: any): string;
3
+ /** @private */
2
4
  export function decryptData(encryptedData: any): any;
3
- //# sourceMappingURL=crypto.d.ts.map
@@ -1,3 +1,25 @@
1
1
  export { default as Server } from "./Server.js";
2
- export { default as Context } from "./Context.js";
3
- //# sourceMappingURL=index.d.ts.map
2
+ export { default as ServerContext } from "./ServerContext.js";
3
+ /**
4
+ * Configuration object for the server.
5
+ */
6
+ export type ServerConfig = {
7
+ /**
8
+ * - Application configuration object.
9
+ */
10
+ app?: {
11
+ clients: object;
12
+ name?: string;
13
+ author?: string;
14
+ };
15
+ /**
16
+ * - Environment configuration object.
17
+ */
18
+ env?: {
19
+ useHttps: boolean;
20
+ serverAddress: string;
21
+ port: number;
22
+ httpsInfos?: obj;
23
+ subpath?: string;
24
+ };
25
+ };