@soundworks/core 4.0.0-alpha.9 → 5.0.0

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 (144) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/README.md +14 -29
  3. package/package.json +24 -42
  4. package/src/client/Client.js +306 -256
  5. package/src/client/{Context.js → ClientContext.js} +63 -54
  6. package/src/client/ClientContextManager.js +115 -0
  7. package/src/client/{Plugin.js → ClientPlugin.js} +23 -19
  8. package/src/client/{PluginManager.js → ClientPluginManager.js} +43 -40
  9. package/src/client/ClientSocket.js +270 -0
  10. package/src/client/{StateManager.js → ClientStateManager.js} +13 -13
  11. package/src/client/index.js +36 -39
  12. package/src/common/BasePlugin.js +63 -58
  13. package/src/common/BasePluginManager.js +191 -142
  14. package/src/common/BaseStateManager.js +635 -145
  15. package/src/common/BatchedTransport.js +73 -0
  16. package/src/common/ParameterBag.js +90 -93
  17. package/src/common/PromiseStore.js +54 -0
  18. package/src/common/SharedState.js +821 -0
  19. package/src/common/SharedStateCollection.js +492 -0
  20. package/src/common/constants.js +14 -2
  21. package/src/common/logger.js +64 -68
  22. package/src/common/shared-state-types.js +82 -0
  23. package/src/common/sockets-utils.js +0 -64
  24. package/src/common/version.js +2 -0
  25. package/src/server/Server.js +552 -824
  26. package/src/server/ServerClient.js +74 -0
  27. package/src/server/{Context.js → ServerContext.js} +96 -77
  28. package/src/server/{ContextManager.js → ServerContextManager.js} +107 -87
  29. package/src/server/{Plugin.js → ServerPlugin.js} +30 -32
  30. package/src/server/ServerPluginManager.js +177 -0
  31. package/src/server/ServerSocket.js +256 -0
  32. package/src/server/ServerSockets.js +150 -0
  33. package/src/server/ServerStateManager.js +706 -0
  34. package/src/server/SharedStatePrivate.js +277 -0
  35. package/src/server/audit-network-latency.worker.js +19 -4
  36. package/src/server/{audit-schema.js → audit-state-class-description.js} +3 -2
  37. package/src/server/create-http-server.js +127 -0
  38. package/src/server/crypto.js +8 -7
  39. package/src/server/index.js +60 -17
  40. package/types/client/Client.d.ts +95 -70
  41. package/types/client/{Context.d.ts → ClientContext.d.ts} +59 -34
  42. package/types/client/{ContextManager.d.ts → ClientContextManager.d.ts} +19 -22
  43. package/types/client/ClientPlugin.d.ts +51 -0
  44. package/types/client/ClientPluginManager.d.ts +112 -0
  45. package/types/client/ClientSocket.d.ts +76 -0
  46. package/types/client/{StateManager.d.ts → ClientStateManager.d.ts} +18 -21
  47. package/types/client/index.d.ts +33 -6
  48. package/types/common/BasePlugin.d.ts +64 -53
  49. package/types/common/BasePluginManager.d.ts +84 -39
  50. package/types/common/BaseStateManager.d.ts +227 -49
  51. package/types/common/BatchedTransport.d.ts +17 -0
  52. package/types/common/ParameterBag.d.ts +73 -76
  53. package/types/common/PromiseStore.d.ts +11 -0
  54. package/types/common/SharedState.d.ts +291 -0
  55. package/types/common/SharedStateCollection.d.ts +311 -0
  56. package/types/common/constants.d.ts +10 -3
  57. package/types/common/logger.d.ts +8 -6
  58. package/types/common/shared-state-types.d.ts +95 -0
  59. package/types/common/sockets-utils.d.ts +0 -5
  60. package/types/common/version.d.ts +2 -0
  61. package/types/server/Server.d.ts +215 -136
  62. package/types/server/ServerClient.d.ts +45 -0
  63. package/types/server/ServerContext.d.ts +209 -0
  64. package/types/server/ServerContextManager.d.ts +75 -0
  65. package/types/server/ServerPlugin.d.ts +57 -0
  66. package/types/server/ServerPluginManager.d.ts +115 -0
  67. package/types/server/ServerSocket.d.ts +69 -0
  68. package/types/server/ServerSockets.d.ts +42 -0
  69. package/types/server/ServerStateManager.d.ts +284 -0
  70. package/types/server/SharedStatePrivate.d.ts +23 -0
  71. package/types/server/audit-network-latency.worker.d.ts +2 -2
  72. package/types/server/{audit-schema.d.ts → audit-state-class-description.d.ts} +8 -9
  73. package/types/server/create-http-server.d.ts +5 -0
  74. package/types/server/crypto.d.ts +4 -0
  75. package/types/server/index.d.ts +51 -3
  76. package/.eslintrc +0 -15
  77. package/.github/workflows/gh-pages.yml +0 -31
  78. package/.github/workflows/unit-tests.yml +0 -18
  79. package/src/client/ContextManager.js +0 -111
  80. package/src/client/SharedState.js +0 -84
  81. package/src/client/SharedStateCollection.js +0 -35
  82. package/src/client/Socket.js +0 -402
  83. package/src/common/BaseSharedState.js +0 -583
  84. package/src/common/BaseSharedStateCollection.js +0 -179
  85. package/src/common/SharedStatePrivate.js +0 -192
  86. package/src/common/promise-store.js +0 -25
  87. package/src/server/Client.js +0 -55
  88. package/src/server/PluginManager.js +0 -170
  89. package/src/server/SharedState.js +0 -85
  90. package/src/server/SharedStateCollection.js +0 -36
  91. package/src/server/Socket.js +0 -396
  92. package/src/server/Sockets.js +0 -224
  93. package/src/server/StateManager.js +0 -620
  94. package/tsconfig.json +0 -21
  95. package/types/client/Client.d.ts.map +0 -1
  96. package/types/client/Context.d.ts.map +0 -1
  97. package/types/client/ContextManager.d.ts.map +0 -1
  98. package/types/client/Plugin.d.ts +0 -53
  99. package/types/client/Plugin.d.ts.map +0 -1
  100. package/types/client/PluginManager.d.ts +0 -96
  101. package/types/client/PluginManager.d.ts.map +0 -1
  102. package/types/client/SharedState.d.ts +0 -85
  103. package/types/client/SharedState.d.ts.map +0 -1
  104. package/types/client/Socket.d.ts +0 -138
  105. package/types/client/Socket.d.ts.map +0 -1
  106. package/types/client/StateManager.d.ts.map +0 -1
  107. package/types/client/index.d.ts.map +0 -1
  108. package/types/common/BasePlugin.d.ts.map +0 -1
  109. package/types/common/BasePluginManager.d.ts.map +0 -1
  110. package/types/common/BaseSharedState.d.ts +0 -173
  111. package/types/common/BaseSharedState.d.ts.map +0 -1
  112. package/types/common/BaseStateManager.d.ts.map +0 -1
  113. package/types/common/ParameterBag.d.ts.map +0 -1
  114. package/types/common/SharedStatePrivate.d.ts +0 -20
  115. package/types/common/SharedStatePrivate.d.ts.map +0 -1
  116. package/types/common/constants.d.ts.map +0 -1
  117. package/types/common/logger.d.ts.map +0 -1
  118. package/types/common/promise-store.d.ts +0 -4
  119. package/types/common/promise-store.d.ts.map +0 -1
  120. package/types/common/sockets-utils.d.ts.map +0 -1
  121. package/types/common/utils.d.ts +0 -5
  122. package/types/common/utils.d.ts.map +0 -1
  123. package/types/server/Client.d.ts +0 -40
  124. package/types/server/Client.d.ts.map +0 -1
  125. package/types/server/Context.d.ts +0 -59
  126. package/types/server/Context.d.ts.map +0 -1
  127. package/types/server/ContextManager.d.ts +0 -69
  128. package/types/server/ContextManager.d.ts.map +0 -1
  129. package/types/server/Plugin.d.ts +0 -70
  130. package/types/server/Plugin.d.ts.map +0 -1
  131. package/types/server/PluginManager.d.ts +0 -91
  132. package/types/server/PluginManager.d.ts.map +0 -1
  133. package/types/server/Server.d.ts.map +0 -1
  134. package/types/server/SharedState.d.ts +0 -86
  135. package/types/server/SharedState.d.ts.map +0 -1
  136. package/types/server/Socket.d.ts +0 -128
  137. package/types/server/Socket.d.ts.map +0 -1
  138. package/types/server/Sockets.d.ts +0 -73
  139. package/types/server/Sockets.d.ts.map +0 -1
  140. package/types/server/StateManager.d.ts +0 -190
  141. package/types/server/StateManager.d.ts.map +0 -1
  142. package/types/server/audit-network-latency.worker.d.ts.map +0 -1
  143. package/types/server/audit-schema.d.ts.map +0 -1
  144. package/types/server/index.d.ts.map +0 -1
@@ -0,0 +1,95 @@
1
+ /**
2
+ * User defined name for a class of {@link SharedState}
3
+ */
4
+ type SharedStateClassName = string;
5
+ /**
6
+ * Description of a {@link SharedState} data structure that describes the structure
7
+ * of a class of {@link SharedState} to be registered by {@link ServerStateManager#defineClass}
8
+ *
9
+ * A `SharedStateClassDescription` is the blueprint, or the definition from which
10
+ * shared states from a given class can be created.
11
+ *
12
+ * It consists of a set of key / value pairs where the key is the name of
13
+ * the parameter (cf. ${SharedStateParameterName}), and the value is an object
14
+ * describing the parameter (cf. ${SharedStateParameterDescription}).
15
+ */
16
+ type SharedStateClassDescription = any;
17
+ /**
18
+ * User defined name of a parameter in a class of {@link SharedState}
19
+ */
20
+ type SharedStateParameterName = string;
21
+ /**
22
+ * Description of a parameter in a class of {@link SharedState}
23
+ */
24
+ type SharedStateParameterDescription = {
25
+ /**
26
+ * - Type of the parameter.
27
+ */
28
+ type: "boolean" | "string" | "integer" | "float" | "enum" | "any";
29
+ /**
30
+ * - Default value of the parameter. Optional only if
31
+ * `nullable = true` or `event = true`
32
+ */
33
+ default: any;
34
+ /**
35
+ * - Defines if the parameter is nullable.
36
+ * When `true` the parameter `default` is set to `null`.
37
+ */
38
+ nullable?: boolean;
39
+ /**
40
+ * - Define if the parameter is a volatile, i.e.
41
+ * its value only exists on an update and is set back to `null` after propagation.
42
+ * When `true`, `nullable` is automatically set to `true` and `default` to `null`.
43
+ */
44
+ event?: boolean;
45
+ /**
46
+ * - When set to `false`, an update will
47
+ * trigger the propagation of a parameter even when its value didn't change.
48
+ * This option provides a sort of middle ground between the default behavior
49
+ * (e.g. where only changed values are propagated) and the behavior of the `event`
50
+ * option (which has no state per se). Hence, setting this options to `false` if
51
+ * `event=true` makes no sens.
52
+ */
53
+ filterChange?: boolean;
54
+ /**
55
+ * - When set to `true`, an update will
56
+ * trigger the update listeners immediately on the node that generated the update,
57
+ * before propagating the change on the network.
58
+ * This option is useful in cases the network would introduce a noticeable
59
+ * latency on the client.
60
+ * If for some reason the value is overridden server-side (e.g. in an `updateHook`)
61
+ * the listeners will be called again when the "real" value is received.
62
+ */
63
+ immediate?: boolean;
64
+ /**
65
+ * - When set to true, the parameter must be
66
+ * provided in the initialization values when the state is created.
67
+ */
68
+ required?: boolean;
69
+ /**
70
+ * - When set to true, the parameter is never
71
+ * propagated on the network (hence it is no longer a shared parameter :). This
72
+ * is useful to declare some common parameter (e.g. some interface state) that
73
+ * don't need to be shared but to stay in the shared state API paradigm.
74
+ */
75
+ local?: boolean;
76
+ /**
77
+ * - Minimum value of the parameter. Only applies
78
+ * for `integer` and `float` types.
79
+ */
80
+ min?: number;
81
+ /**
82
+ * - Maximum value of the parameter. Only applies
83
+ * for `integer` and `float` types.
84
+ */
85
+ max?: number;
86
+ /**
87
+ * - Possible values of the parameter. Only applies and
88
+ * mandatory for `enum` type.
89
+ */
90
+ list?: Array<any>;
91
+ /**
92
+ * - Optional metadata of the parameter.
93
+ */
94
+ metas?: object;
95
+ };
@@ -1,9 +1,4 @@
1
1
  /** @private */
2
- export function packBinaryMessage(channel: any, data: any): ArrayBufferLike;
3
- /** @private */
4
- export function unpackBinaryMessage(buffer: any): any[];
5
- /** @private */
6
2
  export function packStringMessage(channel: any, ...args: any[]): string;
7
3
  /** @private */
8
4
  export function unpackStringMessage(data: any): any;
9
- //# sourceMappingURL=sockets-utils.d.ts.map
@@ -0,0 +1,2 @@
1
+ declare const _default: "5.0.0";
2
+ export default _default;
@@ -1,142 +1,198 @@
1
+ export const kServerOnSocketConnection: unique symbol;
2
+ export const kServerIsValidConnectionToken: unique symbol;
3
+ export const kServerOnStatusChangeCallbacks: unique symbol;
1
4
  export default Server;
2
5
  /**
3
- * Server side entry point for a `soundworks` application.
6
+ * The `Server` class is the main entry point for soundworks server-side project.
4
7
  *
5
- * This object hosts configuration informations, as well as methods to
6
- * initialize and start the application. It is also responsible for creating
7
- * the static file (http) server as well as the socket server.
8
+ * The `Server` instance allows to access soundworks components such as {@link ServerStateManager},
9
+ * {@link ServerPluginManager}, {@link ServerSocket} or {@link ServerContextManager}.
10
+ * Its is also responsible for handling the initialization lifecycle of the different
11
+ * soundworks components.
8
12
  *
9
- * @memberof server
10
- *
11
- * @param {server.ServerConfig} config
12
- * @example
13
+ * ```
13
14
  * import { Server } from '@soundworks/core/server';
15
+ *
14
16
  * const server = new Server({
15
17
  * app: {
16
18
  * name: 'my-example-app',
17
- * clients: { myClient: { target: 'node' } },
19
+ * clients: {
20
+ * player: { runtime: 'browser', default: true },
21
+ * controller: { runtime: 'browser' },
22
+ * thing: { runtime: 'node' }
23
+ * },
18
24
  * },
19
25
  * env: {
20
- * port: 8888,
26
+ * port: 8000,
21
27
  * },
22
28
  * });
23
- * await server.init();
29
+ *
24
30
  * await server.start();
31
+ * ```
25
32
  */
26
33
  declare class Server {
27
- constructor(config: any);
28
34
  /**
29
- * Configuration informations.
30
- * Defaults to:
31
- * ```
35
+ * @param {ServerConfig} config - Configuration object for the server.
36
+ * @throws
37
+ * - If `config.app.clients` is empty.
38
+ * - If a `node` client is defined but `config.env.serverAddress` is not defined.
39
+ * - if `config.env.useHttps` is `true` and `config.env.httpsInfos` is not `null`
40
+ * (which generates self signed certificated), `config.env.httpsInfos.cert` and
41
+ * `config.env.httpsInfos.key` should point to valid cert files.
42
+ */
43
+ constructor(config: ServerConfig);
44
+ /**
45
+ * Given config object merged with the following defaults:
46
+ * @example
32
47
  * {
33
48
  * env: {
34
49
  * type: 'development',
35
50
  * port: 8000,
36
- * subfolder: '',
51
+ * serverAddress: null,
52
+ * baseUrl: '',
37
53
  * useHttps: false,
38
54
  * httpsInfos: null,
55
+ * crossOriginIsolated: true,
56
+ * verbose: true,
39
57
  * },
40
58
  * app: {
41
59
  * name: 'soundworks',
42
60
  * clients: {},
43
- * },
61
+ * }
44
62
  * }
45
- * ```
63
+ * @type {ServerConfig}
46
64
  */
47
- config: any;
65
+ get config(): ServerConfig;
48
66
  /**
49
- * Router. Internally use polka.
50
- * (cf. {@link https://github.com/lukeed/polka})
67
+ * Package version.
68
+ *
69
+ * @type {string}
51
70
  */
52
- router: any;
71
+ get version(): string;
53
72
  /**
54
- * Http(s) server instance. The node `http` or `https` module instance
55
- * (cf. {@link https://nodejs.org/api/http.html})
73
+ * Id of the server, a constant set to `-1`
74
+ * @type {number}
75
+ * @readonly
56
76
  */
57
- httpServer: any;
77
+ readonly get id(): number;
58
78
  /**
59
- * Key / value storage with Promise based Map API
60
- * basically a wrapper around kvey (cf. {@link https://github.com/lukechilds/keyv})
61
- * @private
79
+ * Status of the server.
80
+ *
81
+ * @type {'idle'|'inited'|'started'|'errored'}
82
+ */
83
+ get status(): "idle" | "inited" | "started" | "errored";
84
+ set router(router: any);
85
+ /**
86
+ * Instance of the router if any.
87
+ *
88
+ * The router can be used to open new route, for example to expose a directory
89
+ * of static assets (in default soundworks applications only the `public` is exposed).
90
+ *
91
+ * @example
92
+ * import { Server } from '@soundworks/core/server.js';
93
+ * import { loadConfig, configureHttpRouter } from '@soundworks/helpers/server.js';
94
+ *
95
+ * // create the server instance
96
+ * const server = new Server(loadConfig());
97
+ * // configure the express router provided by the helpers
98
+ * configureHttpRouter(server);
99
+ *
100
+ * // expose assets located in the `soundfiles` directory on the network
101
+ * server.router.use('/soundfiles', express.static('soundfiles')));
62
102
  */
63
- private db;
103
+ get router(): any;
64
104
  /**
65
- * The {@link server.Sockets} instance. A small wrapper around
66
- * [`ws`](https://github.com/websockets/ws) server.
67
- * @see {@link server.Sockets}
68
- * @type {server.Sockets}
105
+ * Instance of the Node.js `http.Server` or `https.Server`
106
+ *
107
+ * @see {@link https://nodejs.org/api/http.html#class-httpserver}
108
+ * @see {@link https://nodejs.org/api/https.html#class-httpsserver}
69
109
  */
70
- sockets: server.Sockets;
110
+ get httpServer(): any;
71
111
  /**
72
- * The {@link server.PluginManager} instance.
73
- * @see {@link server.PluginManager}
74
- * @type {server.PluginManager}
112
+ * Simple key / value filesystem database with Promise based Map API.
113
+ *
114
+ * Basically a tiny wrapper around the {@link https://github.com/lukechilds/keyv} package.
75
115
  */
76
- pluginManager: server.PluginManager;
116
+ get db(): any;
77
117
  /**
78
- * The {@link server.StateManager} instance.
79
- * @see {@link server.StateManager}
80
- * @type {server.StateManager}
118
+ * Instance of the {@link ServerSockets} class.
119
+ *
120
+ * @type {ServerSockets}
81
121
  */
82
- stateManager: server.StateManager;
122
+ get sockets(): ServerSockets;
83
123
  /**
84
- * The {@link server.ContextManager} instance.
124
+ * Instance of the {@link ServerPluginManager} class.
85
125
  *
126
+ * @type {ServerPluginManager}
86
127
  */
87
- contextManager: ContextManager;
128
+ get pluginManager(): ServerPluginManager<any>;
88
129
  /**
89
- * If https is required, will contain informations about the certificates
90
- * (self-signed, validity dates, etc.)
130
+ * Instance of the {@link ServerStateManager} class.
131
+ *
132
+ * @type {ServerStateManager}
91
133
  */
92
- httpsInfos: {
93
- selfSigned: boolean;
94
- CN: any;
95
- altNames: any;
96
- validFrom: any;
97
- validTo: any;
98
- isValid: boolean;
99
- daysRemaining: number;
100
- } | {
101
- selfSigned: boolean;
102
- CN?: undefined;
103
- altNames?: undefined;
104
- validFrom?: undefined;
105
- validTo?: undefined;
106
- isValid?: undefined;
107
- daysRemaining?: undefined;
108
- } | {
109
- selfSigned: boolean;
110
- CN?: undefined;
111
- altNames?: undefined;
112
- validFrom?: undefined;
113
- validTo?: undefined;
114
- isValid?: undefined;
115
- daysRemaining?: undefined;
116
- };
134
+ get stateManager(): ServerStateManager;
117
135
  /**
118
- * Current status of the server ['idle', 'inited', 'started']
136
+ * Instance of the {@link ServerContextManager} class.
137
+ *
138
+ * @type {ServerContextManager}
119
139
  */
120
- status: string;
121
- /** @private */
122
- private _applicationTemplateConfig;
123
- /** @private */
124
- private _onStatusChangeCallbacks;
125
- /** @private */
126
- private _auditState;
140
+ get contextManager(): ServerContextManager<any>;
127
141
  /**
128
- * Method to be called before `start` in the initialization lifecycle of the
129
- * soundworks server. Note that if `init()`` is not explicitely called, `start()`
130
- * will call it implicitely.
142
+ * Register a callback to execute when status change.
143
+ *
144
+ * Status are dispatched in the following order:
145
+ * - 'http-server-ready'
146
+ * - 'inited'
147
+ * - 'started'
148
+ * - 'stopped'
149
+ * during the lifecycle of the server. If an error occurs the 'errored' status is propagated.
150
+ *
151
+ * @param {function} callback
152
+ */
153
+ onStatusChange(callback: Function): () => boolean;
154
+ /**
155
+ * Attach and retrieve the global audit state of the application.
156
+ *
157
+ * The audit state is a {@link SharedState} instance that keeps track of
158
+ * global information about the application such as, the number of connected
159
+ * clients, network latency estimation, etc.
160
+ *
161
+ * The audit state is created by the server on start up.
162
+ *
163
+ * @returns {Promise<SharedState>}
164
+ * @throws Will throw if called before `server.init()`
165
+ *
166
+ * @example
167
+ * const auditState = await server.getAuditState();
168
+ * auditState.onUpdate(() => console.log(auditState.getValues()), true);
169
+ */
170
+ getAuditState(): Promise<SharedState>;
171
+ /**
172
+ * The `init` method is part of the initialization lifecycle of the `soundworks`
173
+ * server. Most of the time, the `init` method will be implicitly called by the
174
+ * {@link Server#start} method.
175
+ *
176
+ * In some situations you might want to call this method manually, in such cases
177
+ * the method should be called before the {@link Server#start} method.
131
178
  *
132
179
  * What it does:
133
- * - create the audit state
134
- * - prepapre http(s) server and routing according to the informations
135
- * declared in `config.app.clients`
136
- * - starts all registered plugins
180
+ * 1) Create the audit state
181
+ * 2) Create the HTTP(s) server
182
+ * 3) Initialize registered plugins
137
183
  *
138
- * After `await server.init()`, you can safely use the StateManager, as well
139
- * as any registered Plugins.
184
+ * Between steps 2 and 3, the 'http-server-ready' event status is dispatched so
185
+ * that consumer code can register its router before plugin initialization:
186
+ * ```js
187
+ * server.onStatusChange(status => {
188
+ * if (status === 'http-server-ready') {
189
+ * server.httpServer.on('request', router);
190
+ * }
191
+ * });
192
+ * ```
193
+ *
194
+ * After `await server.init()` is fulfilled, the {@link Server#stateManager}
195
+ * and all registered plugins can be safely used.
140
196
  *
141
197
  * @example
142
198
  * const server = new Server(config);
@@ -144,38 +200,77 @@ declare class Server {
144
200
  * await server.start();
145
201
  * // or implicitly called by start
146
202
  * const server = new Server(config);
147
- * await server.start(); // init is called implicitely
203
+ * await server.start(); // init is called implicitly
148
204
  */
149
205
  init(): Promise<void>;
150
206
  /**
151
- * Method to be called when `init` step is done in the initialization
152
- * lifecycle of the soundworks server. If `server.init()` has not been called
153
- * explicitely, `server.start()` will call it automatically.
207
+ * The `start` method is part of the initialization lifecycle of the `soundworks`
208
+ * server. The `start` method will implicitly call the {@link Server#init}
209
+ * method if it has not been called manually.
154
210
  *
155
211
  * What it does:
156
- * - starts all registered contexts (context are automatically registered
157
- * when instantiated)
158
- * - start the web socket server
159
- * - launch the HTTP server on given port
212
+ * - implicitly call {@link Server#init} if not done manually
213
+ * - launch the HTTP and WebSocket servers
214
+ * - start all created contexts. To this end, you will have to call `server.init`
215
+ * manually and instantiate the contexts between `server.init()` and `server.start()`
216
+ *
217
+ * After `await server.start()` the server is ready to accept incoming connections
160
218
  *
161
- * After `await server.start()` the server is ready to accept incoming connexions
219
+ * @example
220
+ * import { Server } from '@soundworks/core/server.js'
221
+ *
222
+ * const server = new Server(config);
223
+ * await server.start();
162
224
  */
163
225
  start(): Promise<any>;
164
226
  /**
165
- * Stop the server, close all existing WebSocket connections.
166
- * Mainly usefull for test.
227
+ * Stops all started contexts, plugins, close all the socket connections and
228
+ * the http(s) server.
229
+ *
230
+ * In most situations, you might not need to call this method. However, it can
231
+ * be useful for unit testing or similar situations where you want to create
232
+ * and delete several servers in the same process.
233
+ *
234
+ * @example
235
+ * import { Server } from '@soundworks/core/server.js'
236
+ *
237
+ * const server = new Server(config);
238
+ * await server.start();
239
+ *
240
+ * await new Promise(resolve => setTimeout(resolve, 1000));
241
+ * await server.stop();
167
242
  */
168
243
  stop(): Promise<void>;
244
+ onClientConnect(callback: any): () => boolean;
245
+ onClientDisconnect(callback: any): () => boolean;
246
+ /** @private */
247
+ private isProtectedClientRole;
169
248
  /**
170
- * Open the route for a given client.
171
- * @private
249
+ * Generate a token to secure client connection.
250
+ *
251
+ * The token should be passed to the client-side `Client` config object, it will
252
+ * be internally used to check the WebSocket connection and reject it if the
253
+ * token is invalid.
172
254
  */
173
- private _openClientRoute;
255
+ generateAuthToken(req: any): string;
174
256
  /**
175
- * Socket connection callback.
176
- * @private
257
+ * Check if the given client is trusted, i.e. config.env.type == 'production'
258
+ * and the client is protected behind a password.
259
+ *
260
+ * @param {ServerClient} client - Client to be tested
261
+ * @returns {boolean}
177
262
  */
178
- private _onSocketConnection;
263
+ isTrustedClient(client: ServerClient): boolean;
264
+ /**
265
+ * Check if the token from a client is trusted, i.e. config.env.type == 'production'
266
+ * and the client is protected behind a password.
267
+ *
268
+ * @param {number} clientId - Id of the client
269
+ * @param {string} clientIp - Ip of the client
270
+ * @param {string} token - Token to be tested
271
+ * @returns {boolean}
272
+ */
273
+ isTrustedToken(clientId: number, clientIp: string, token: string): boolean;
179
274
  /**
180
275
  * Create namespaced databases for core and plugins
181
276
  * (kind of experimental API do not expose in doc for now)
@@ -185,39 +280,23 @@ declare class Server {
185
280
  * @private
186
281
  */
187
282
  private createNamespacedDb;
188
- onStatusChange(callback: any): () => boolean;
189
- /** @private */
190
- private _dispatchStatus;
191
- /**
192
- * Configure the server to work out-of-the box with the soundworks-template
193
- * directory tree structure.
194
- *
195
- * - uses [template-literal](https://www.npmjs.com/package/template-literal) package
196
- * as html templateEngine
197
- * - define `.build/server/tmpl` as the directory in which html template can be
198
- * found
199
- * - define the `clientConfigFunction` function that return client compliant
200
- * config object to be injected in the html template.
201
- *
202
- * Also expose two public directory:
203
- * - the `public` directory which is exposed behind the root path
204
- * - the `./.build/public` directory which is exposed behind the `build` path
205
- */
206
- setDefaultTemplateConfig(): void;
207
283
  /**
208
- * Define your own template path, template engine, and clientConfig function.
209
- * This method is for very advanced use-cases and only be used if you know what
210
- * you are doing. As such its behavior could probably be improved a lot...
211
- *
212
- * If you end up using this, please contact me to explain your use-case :)
284
+ * @deprecated
213
285
  */
214
- setCustomTemplateConfig(options: any): void;
286
+ useDefaultApplicationTemplate(): void;
215
287
  /**
216
- * Get the global audit state of the application.
217
- *
218
- * @throws Will throw if called before `server.init()`
288
+ * Socket connection callback.
289
+ * @private
219
290
  */
220
- getAuditState(): Promise<any>;
291
+ private [kServerOnSocketConnection];
292
+ /** @private */
293
+ private [kServerIsValidConnectionToken];
294
+ /** @private */
295
+ private [kServerOnStatusChangeCallbacks];
296
+ #private;
221
297
  }
222
- import ContextManager from "./ContextManager.js";
223
- //# sourceMappingURL=Server.d.ts.map
298
+ import ServerSockets from './ServerSockets.js';
299
+ import ServerPluginManager from './ServerPluginManager.js';
300
+ import ServerStateManager from './ServerStateManager.js';
301
+ import ServerContextManager from './ServerContextManager.js';
302
+ import ServerClient from './ServerClient.js';
@@ -0,0 +1,45 @@
1
+ export const kServerClientToken: unique symbol;
2
+ export default ServerClient;
3
+ /**
4
+ * Server-side representation of a client.
5
+ *
6
+ * @hideconstructor
7
+ * @see {@link Client}
8
+ */
9
+ declare class ServerClient {
10
+ /**
11
+ * @param {String} role - Role of the client
12
+ * @param {ServerSocket} socket - Socket connection with the client
13
+ */
14
+ constructor(role: string, socket: ServerSocket);
15
+ /**
16
+ * Client role, as specified in client side config {@link Client}.
17
+ *
18
+ * @type {String}
19
+ */
20
+ get role(): string;
21
+ /**
22
+ * Session Id (incremented positive number).
23
+ *
24
+ * @type {Number}
25
+ */
26
+ get id(): number;
27
+ /**
28
+ * Unique session Id (uuidv4).
29
+ *
30
+ * @type {String}
31
+ */
32
+ get uuid(): string;
33
+ /**
34
+ * Socket connection with the remote {@link Client}.
35
+ *
36
+ * @type {ServerSocket}
37
+ */
38
+ get socket(): ServerSocket;
39
+ /**
40
+ * Set in `server[kServerOnSocketConnection]`
41
+ * @private
42
+ */
43
+ private [kServerClientToken];
44
+ #private;
45
+ }