@soundworks/core 3.6.0 → 4.0.0-alpha.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.
- package/.eslintrc +16 -0
- package/LICENSE +1 -1
- package/README.md +32 -30
- package/package.json +57 -26
- package/src/client/Client.js +391 -0
- package/src/client/Context.js +225 -0
- package/src/client/ContextManager.js +111 -0
- package/src/client/Plugin.js +66 -0
- package/src/client/PluginManager.js +123 -0
- package/src/client/SharedState.js +84 -0
- package/src/client/Socket.js +389 -0
- package/src/client/StateManager.js +67 -0
- package/src/client/index.js +77 -0
- package/src/common/BasePlugin.js +163 -0
- package/src/common/BasePluginManager.js +227 -0
- package/src/common/BaseSharedState.js +473 -0
- package/src/common/BaseStateManager.js +200 -0
- package/{common → src/common}/ParameterBag.js +82 -122
- package/{common → src/common}/SharedStatePrivate.js +56 -64
- package/src/common/constants.js +51 -0
- package/src/common/logger.js +154 -0
- package/src/common/promise-store.js +25 -0
- package/src/common/sockets-utils.js +73 -0
- package/src/common/utils.js +22 -0
- package/src/server/Client.js +50 -0
- package/src/server/Context.js +99 -0
- package/src/server/ContextManager.js +266 -0
- package/src/server/Plugin.js +77 -0
- package/src/server/PluginManager.js +150 -0
- package/src/server/Server.js +849 -0
- package/src/server/SharedState.js +86 -0
- package/{server → src/server}/Socket.js +119 -94
- package/{server → src/server}/Sockets.js +73 -61
- package/{common/SharedStateManagerServer.js → src/server/StateManager.js} +183 -131
- package/src/server/audit-network-latency.worker.js +57 -0
- package/src/server/audit-schema.js +42 -0
- package/src/server/index.js +45 -0
- package/tsconfig.json +21 -0
- package/types/client/Client.d.ts +194 -0
- package/types/client/Client.d.ts.map +1 -0
- package/types/client/Context.d.ts +139 -0
- package/types/client/Context.d.ts.map +1 -0
- package/types/client/ContextManager.d.ts +45 -0
- package/types/client/ContextManager.d.ts.map +1 -0
- package/types/client/Plugin.d.ts +53 -0
- package/types/client/Plugin.d.ts.map +1 -0
- package/types/client/PluginManager.d.ts +96 -0
- package/types/client/PluginManager.d.ts.map +1 -0
- package/types/client/SharedState.d.ts +85 -0
- package/types/client/SharedState.d.ts.map +1 -0
- package/types/client/Socket.d.ts +138 -0
- package/types/client/Socket.d.ts.map +1 -0
- package/types/client/StateManager.d.ts +72 -0
- package/types/client/StateManager.d.ts.map +1 -0
- package/types/client/index.d.ts +11 -0
- package/types/client/index.d.ts.map +1 -0
- package/types/common/BasePlugin.d.ts +145 -0
- package/types/common/BasePlugin.d.ts.map +1 -0
- package/types/common/BasePluginManager.d.ts +88 -0
- package/types/common/BasePluginManager.d.ts.map +1 -0
- package/types/common/BaseSharedState.d.ts +173 -0
- package/types/common/BaseSharedState.d.ts.map +1 -0
- package/types/common/BaseStateManager.d.ts +70 -0
- package/types/common/BaseStateManager.d.ts.map +1 -0
- package/types/common/ParameterBag.d.ts +143 -0
- package/types/common/ParameterBag.d.ts.map +1 -0
- package/types/common/SharedStatePrivate.d.ts +20 -0
- package/types/common/SharedStatePrivate.d.ts.map +1 -0
- package/types/common/constants.d.ts +35 -0
- package/types/common/constants.d.ts.map +1 -0
- package/types/common/logger.d.ts +16 -0
- package/types/common/logger.d.ts.map +1 -0
- package/types/common/promise-store.d.ts +4 -0
- package/types/common/promise-store.d.ts.map +1 -0
- package/types/common/sockets-utils.d.ts +9 -0
- package/types/common/sockets-utils.d.ts.map +1 -0
- package/types/common/utils.d.ts +5 -0
- package/types/common/utils.d.ts.map +1 -0
- package/types/server/Client.d.ts +40 -0
- package/types/server/Client.d.ts.map +1 -0
- package/types/server/Context.d.ts +59 -0
- package/types/server/Context.d.ts.map +1 -0
- package/types/server/ContextManager.d.ts +69 -0
- package/types/server/ContextManager.d.ts.map +1 -0
- package/types/server/Plugin.d.ts +70 -0
- package/types/server/Plugin.d.ts.map +1 -0
- package/types/server/PluginManager.d.ts +91 -0
- package/types/server/PluginManager.d.ts.map +1 -0
- package/types/server/Server.d.ts +223 -0
- package/types/server/Server.d.ts.map +1 -0
- package/types/server/SharedState.d.ts +86 -0
- package/types/server/SharedState.d.ts.map +1 -0
- package/types/server/Socket.d.ts +128 -0
- package/types/server/Socket.d.ts.map +1 -0
- package/types/server/Sockets.d.ts +73 -0
- package/types/server/Sockets.d.ts.map +1 -0
- package/types/server/StateManager.d.ts +190 -0
- package/types/server/StateManager.d.ts.map +1 -0
- package/types/server/audit-network-latency.worker.d.ts +2 -0
- package/types/server/audit-network-latency.worker.d.ts.map +1 -0
- package/types/server/audit-schema.d.ts +27 -0
- package/types/server/audit-schema.d.ts.map +1 -0
- package/types/server/index.d.ts +7 -0
- package/types/server/index.d.ts.map +1 -0
- package/client/AbstractExperience.js +0 -54
- package/client/AbstractPlugin.js +0 -230
- package/client/Client.js +0 -190
- package/client/PluginManager.js +0 -267
- package/client/Socket.js +0 -338
- package/client/index.js +0 -48
- package/client.js +0 -28
- package/common/SharedState.js +0 -480
- package/common/SharedStateManagerClient.js +0 -194
- package/common/Signal.js +0 -40
- package/common/SignalAll.js +0 -44
- package/common/shared-state-utils.js +0 -90
- package/common/sockets-encoder-decoder.js +0 -73
- package/server/AbstractExperience.js +0 -126
- package/server/AbstractPlugin.js +0 -211
- package/server/Client.js +0 -70
- package/server/PluginManager.js +0 -177
- package/server/Server.js +0 -746
- package/server/index.js +0 -49
- package/server/utils/logger.js +0 -134
- package/server.js +0 -28
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* The `PluginManager` allows to register and retrieve `soundworks` plugins.
|
|
24
|
+
*
|
|
25
|
+
* Plugins should always be registered both client-side and server-side,
|
|
26
|
+
* and before {@link client.Client#start} or {@link server.Server#start}
|
|
27
|
+
* are called for proper initialization.
|
|
28
|
+
*
|
|
29
|
+
* In some sitautions, you might want to register the same plugin factory several times
|
|
30
|
+
* using different ids (e.g. for watching several parts of the file system, etc.).
|
|
31
|
+
*
|
|
32
|
+
* Refer to the plugins' documentation for more precise examples, and the specific API
|
|
33
|
+
* they expose. See [https://soundworks.dev/guide/ecosystem](https://soundworks.dev/guide/ecosystem)
|
|
34
|
+
* for more informations on the available plugins.
|
|
35
|
+
*
|
|
36
|
+
* ```
|
|
37
|
+
* import { Server } from '@soundworks/core/server.js';
|
|
38
|
+
* import platformPlugin from '@soundworks/plugin-sync/server.js';
|
|
39
|
+
*
|
|
40
|
+
* const server = new Server(config);
|
|
41
|
+
* // register the plugin before `server.start()`
|
|
42
|
+
* server.pluginManager.register('sync', pluginSync);
|
|
43
|
+
*
|
|
44
|
+
* await server.start();
|
|
45
|
+
*
|
|
46
|
+
* const sync = await server.pluginManager.get('sync');
|
|
47
|
+
*
|
|
48
|
+
* setInterval(() => {
|
|
49
|
+
* // log the estimated global synced clock alongside the local clock.
|
|
50
|
+
* console.log(sync.getSyncTime(), sync.getLocalTime());
|
|
51
|
+
* }, 1000);
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @memberof server
|
|
55
|
+
* @extends BasePluginManager
|
|
56
|
+
* @inheritdoc
|
|
57
|
+
* @hideconstructor
|
|
58
|
+
*/
|
|
59
|
+
declare class PluginManager extends BasePluginManager {
|
|
60
|
+
register(id: any, factory?: any, options?: {}, deps?: any[]): void;
|
|
61
|
+
/**
|
|
62
|
+
* Retrieve an fully started instance of a registered plugin.
|
|
63
|
+
*
|
|
64
|
+
* Be aware that the `get` method resolves only when the plugin is fully 'started',
|
|
65
|
+
* which is what we want 99.99% of the time. As such, and to prevent the application
|
|
66
|
+
* to be stuck in some kind of Promise dead lock, this method will throw if
|
|
67
|
+
* used before `server.init()` (or `server.start()`).
|
|
68
|
+
*
|
|
69
|
+
* To handle the remaining 0.01% cases and access plugin instances during their
|
|
70
|
+
* initialization (e.g. to display initialization screen, etc.), you should rely
|
|
71
|
+
* on the `onStateChange` method.
|
|
72
|
+
*
|
|
73
|
+
* _Note: the async API is designed to enable the dynamic creation of plugins (hopefully
|
|
74
|
+
* without brealing changes) in a future release._
|
|
75
|
+
*
|
|
76
|
+
* @param {server.Plugin#id} id - Id of the plugin as defined when registered.
|
|
77
|
+
* @returns {server.Plugin}
|
|
78
|
+
* @see {@link server.PluginManager#onStateChange}
|
|
79
|
+
*
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
private get;
|
|
83
|
+
/** private */
|
|
84
|
+
checkRegisteredPlugins(registeredPlugins: any): void;
|
|
85
|
+
/** @private */
|
|
86
|
+
private addClient;
|
|
87
|
+
/** @private */
|
|
88
|
+
private removeClient;
|
|
89
|
+
}
|
|
90
|
+
import BasePluginManager from "../common/BasePluginManager.js";
|
|
91
|
+
//# sourceMappingURL=PluginManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PluginManager.d.ts","sourceRoot":"","sources":["../../src/server/PluginManager.js"],"names":[],"mappings":";;;;;4BAQW,MAAM,aACN,gBAAc,IAAI;;AAL7B;;;;;;;;GAQG;AAEH;;;;GAIG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH;IASE,mEAQC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,YAMC;IAID,cAAc;IACd,qDAYC;IAED,eAAe;IACf,kBAUC;IAED,eAAe;IACf,qBAUC;CACF"}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
export default Server;
|
|
2
|
+
/**
|
|
3
|
+
* Server side entry point for a `soundworks` application.
|
|
4
|
+
*
|
|
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
|
+
*
|
|
9
|
+
* @memberof server
|
|
10
|
+
*
|
|
11
|
+
* @param {server.ServerConfig} config
|
|
12
|
+
* @example
|
|
13
|
+
* import { Server } from '@soundworks/core/server';
|
|
14
|
+
* const server = new Server({
|
|
15
|
+
* app: {
|
|
16
|
+
* name: 'my-example-app',
|
|
17
|
+
* clients: { myClient: { target: 'node' } },
|
|
18
|
+
* },
|
|
19
|
+
* env: {
|
|
20
|
+
* port: 8888,
|
|
21
|
+
* },
|
|
22
|
+
* });
|
|
23
|
+
* await server.init();
|
|
24
|
+
* await server.start();
|
|
25
|
+
*/
|
|
26
|
+
declare class Server {
|
|
27
|
+
constructor(config: any);
|
|
28
|
+
/**
|
|
29
|
+
* Configuration informations.
|
|
30
|
+
* Defaults to:
|
|
31
|
+
* ```
|
|
32
|
+
* {
|
|
33
|
+
* env: {
|
|
34
|
+
* type: 'development',
|
|
35
|
+
* port: 8000,
|
|
36
|
+
* subfolder: '',
|
|
37
|
+
* useHttps: false,
|
|
38
|
+
* httpsInfos: null,
|
|
39
|
+
* },
|
|
40
|
+
* app: {
|
|
41
|
+
* name: 'soundworks',
|
|
42
|
+
* clients: {},
|
|
43
|
+
* },
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
config: any;
|
|
48
|
+
/**
|
|
49
|
+
* Router. Internally use polka.
|
|
50
|
+
* (cf. {@link https://github.com/lukeed/polka})
|
|
51
|
+
*/
|
|
52
|
+
router: any;
|
|
53
|
+
/**
|
|
54
|
+
* Http(s) server instance. The node `http` or `https` module instance
|
|
55
|
+
* (cf. {@link https://nodejs.org/api/http.html})
|
|
56
|
+
*/
|
|
57
|
+
httpServer: any;
|
|
58
|
+
/**
|
|
59
|
+
* Key / value storage with Promise based Map API
|
|
60
|
+
* basically a wrapper around kvey (cf. {@link https://github.com/lukechilds/keyv})
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
private db;
|
|
64
|
+
/**
|
|
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}
|
|
69
|
+
*/
|
|
70
|
+
sockets: server.Sockets;
|
|
71
|
+
/**
|
|
72
|
+
* The {@link server.PluginManager} instance.
|
|
73
|
+
* @see {@link server.PluginManager}
|
|
74
|
+
* @type {server.PluginManager}
|
|
75
|
+
*/
|
|
76
|
+
pluginManager: server.PluginManager;
|
|
77
|
+
/**
|
|
78
|
+
* The {@link server.StateManager} instance.
|
|
79
|
+
* @see {@link server.StateManager}
|
|
80
|
+
* @type {server.StateManager}
|
|
81
|
+
*/
|
|
82
|
+
stateManager: server.StateManager;
|
|
83
|
+
/**
|
|
84
|
+
* The {@link server.ContextManager} instance.
|
|
85
|
+
*
|
|
86
|
+
*/
|
|
87
|
+
contextManager: ContextManager;
|
|
88
|
+
/**
|
|
89
|
+
* If https is required, will contain informations about the certificates
|
|
90
|
+
* (self-signed, validity dates, etc.)
|
|
91
|
+
*/
|
|
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
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Current status of the server ['idle', 'inited', 'started']
|
|
119
|
+
*/
|
|
120
|
+
status: string;
|
|
121
|
+
/** @private */
|
|
122
|
+
private _applicationTemplateConfig;
|
|
123
|
+
/** @private */
|
|
124
|
+
private _onStatusChangeCallbacks;
|
|
125
|
+
/** @private */
|
|
126
|
+
private _auditState;
|
|
127
|
+
/**
|
|
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.
|
|
131
|
+
*
|
|
132
|
+
* 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
|
|
137
|
+
*
|
|
138
|
+
* After `await server.init()`, you can safely use the StateManager, as well
|
|
139
|
+
* as any registered Plugins.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* const server = new Server(config);
|
|
143
|
+
* await server.init();
|
|
144
|
+
* await server.start();
|
|
145
|
+
* // or implicitly called by start
|
|
146
|
+
* const server = new Server(config);
|
|
147
|
+
* await server.start(); // init is called implicitely
|
|
148
|
+
*/
|
|
149
|
+
init(): Promise<void>;
|
|
150
|
+
/**
|
|
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.
|
|
154
|
+
*
|
|
155
|
+
* 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
|
|
160
|
+
*
|
|
161
|
+
* After `await server.start()` the server is ready to accept incoming connexions
|
|
162
|
+
*/
|
|
163
|
+
start(): Promise<any>;
|
|
164
|
+
/**
|
|
165
|
+
* Stop the server, close all existing WebSocket connections.
|
|
166
|
+
* Mainly usefull for test.
|
|
167
|
+
*/
|
|
168
|
+
stop(): Promise<void>;
|
|
169
|
+
/**
|
|
170
|
+
* Open the route for a given client.
|
|
171
|
+
* @private
|
|
172
|
+
*/
|
|
173
|
+
private _openClientRoute;
|
|
174
|
+
/**
|
|
175
|
+
* Socket connection callback.
|
|
176
|
+
* @private
|
|
177
|
+
*/
|
|
178
|
+
private _onSocketConnection;
|
|
179
|
+
/**
|
|
180
|
+
* Create namespaced databases for core and plugins
|
|
181
|
+
* (kind of experimental API do not expose in doc for now)
|
|
182
|
+
*
|
|
183
|
+
* @note - introduced in v3.1.0-beta.1
|
|
184
|
+
* @note - used by core and plugin-audio-streams
|
|
185
|
+
* @private
|
|
186
|
+
*/
|
|
187
|
+
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
|
+
/**
|
|
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 :)
|
|
213
|
+
*/
|
|
214
|
+
setCustomTemplateConfig(options: any): void;
|
|
215
|
+
/**
|
|
216
|
+
* Get the global audit state of the application.
|
|
217
|
+
*
|
|
218
|
+
* @throws Will throw if called before `server.init()`
|
|
219
|
+
*/
|
|
220
|
+
getAuditState(): Promise<any>;
|
|
221
|
+
}
|
|
222
|
+
import ContextManager from "./ContextManager.js";
|
|
223
|
+
//# sourceMappingURL=Server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Server.d.ts","sourceRoot":"","sources":["../../src/server/Server.js"],"names":[],"mappings":";AA2EA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH;IACE,yBA2IC;IAvIC;;;;;;;;;;;;;;;;;;OAkBG;IACH,YAA+C;IAuC/C;;;OAGG;IACH,YAAuB;IAIvB;;;OAGG;IACH,gBAAsB;IAEtB;;;;OAIG;IACH,WAAyC;IAEzC;;;;;OAKG;IACH,wBAA4B;IAE5B;;;;OAIG;IACH,oCAA4C;IAE5C;;;;OAIG;IACH,kCAAsC;IAEtC;;;OAGG;IACH,+BAA8C;IAE9C;;;OAGG;IACH;;;;;;;;;;;;;;;;;;;;;;;;MAAsB;IAEtB;;OAEG;IACH,eAAoB;IAEpB,eAAe;IACf,mCAIC;IAED,eAAe;IACf,iCAAyC;IACzC,eAAe;IACf,oBAAuB;IASzB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,sBAwMC;IAED;;;;;;;;;;;;OAYG;IACH,sBAoFC;IAGD;;;OAGG;IACH,sBAgBC;IAED;;;OAGG;IACH,yBAgEC;IAED;;;OAGG;IACH,4BAmEC;IAED;;;;;;;OAOG;IACH,2BAmBC;IAED,6CAIC;IAED,eAAe;IACf,wBAgBC;IAED;;;;;;;;;;;;;;OAcG;IACH,iCAsBC;IAED;;;;;;OAMG;IACH,4CAEC;IAED;;;;OAIG;IACH,8BAMC;CACF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export default SharedState;
|
|
2
|
+
export namespace server {
|
|
3
|
+
/**
|
|
4
|
+
* ~onUpdateCallback
|
|
5
|
+
*/
|
|
6
|
+
type SharedState = (newValues: any, oldValues: any, context?: Mixed) => any;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* @callback server.SharedState~onUpdateCallback
|
|
10
|
+
* @param {Object} newValues - Key / value pairs of the updates that have been
|
|
11
|
+
* applied to the state.
|
|
12
|
+
* @param {Object} oldValues - Key / value pairs of the updated params before
|
|
13
|
+
* the updates has been applied to the state.
|
|
14
|
+
* @param {Mixed} [context=null] - Optionnal context object that has been passed
|
|
15
|
+
* with the values updates in the `set` call.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Delete the registered {@link server.SharedState~onUpdateCallback}.
|
|
19
|
+
*
|
|
20
|
+
* @callback server.SharedState~deleteOnUpdateCallback
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* The `SharedState` is one of the most important and versatile abstraction provided
|
|
24
|
+
* by `soundworks`. It represents a set of parameters that are synchronized accross
|
|
25
|
+
* every nodes of the application (clients and server) that declared some interest
|
|
26
|
+
* to the shared state.
|
|
27
|
+
*
|
|
28
|
+
* A `SharedState` is created according to a "schema" (in the sense of a database
|
|
29
|
+
* schema) that must be declared and registered server-side. Any number of `SharedState`s
|
|
30
|
+
* can be created from a single schema.
|
|
31
|
+
*
|
|
32
|
+
* A shared state can be created both by the clients or by the server (in which case
|
|
33
|
+
* it is generally considered as a global state of the application). Similarly any
|
|
34
|
+
* node of the application (clients or server) can declare interest and "attach" to
|
|
35
|
+
* a state created by another node. All node attached to a state can modify its values
|
|
36
|
+
* and/or react to the modifications applied by other nodes.
|
|
37
|
+
*
|
|
38
|
+
* Tutorial: {@link https://soundworks.dev/guide/state-manager.html}
|
|
39
|
+
*
|
|
40
|
+
* ```
|
|
41
|
+
* // server-side
|
|
42
|
+
* import { Server } from '@soundworks/server/index.js';
|
|
43
|
+
*
|
|
44
|
+
* const server = new Server(config);
|
|
45
|
+
* // declare and register the schema of a shared state.
|
|
46
|
+
* server.stateManager.registerSchema('some-global-state', {
|
|
47
|
+
* myRandom: {
|
|
48
|
+
* type: 'float',
|
|
49
|
+
* default: 0,
|
|
50
|
+
* }
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* await server.start();
|
|
54
|
+
*
|
|
55
|
+
* // create a global state server-side
|
|
56
|
+
* const globalState = await server.stateManager.create('some-global-state');
|
|
57
|
+
* // listen and react to the changes made by the clients
|
|
58
|
+
* globalState.onUpdate(updates => console.log(updates));
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* ```
|
|
62
|
+
* // client-side
|
|
63
|
+
* import { Client } from '@soundworks/client.index.js';
|
|
64
|
+
*
|
|
65
|
+
* const client = new Client(config);
|
|
66
|
+
* await client.start();
|
|
67
|
+
*
|
|
68
|
+
* // attach to the global state created by the server
|
|
69
|
+
* const globalState = await client.stateManager.attach('some-global-state');
|
|
70
|
+
*
|
|
71
|
+
* // update the value of a `myRandom` parameter every seconds
|
|
72
|
+
* setInterval(() => {
|
|
73
|
+
* globalState.set({ myRandom: Math.random() });
|
|
74
|
+
* }, 1000);
|
|
75
|
+
* ``
|
|
76
|
+
*
|
|
77
|
+
* @memberof server
|
|
78
|
+
* @extends BaseSharedState
|
|
79
|
+
* @inheritdoc
|
|
80
|
+
* @hideconstructor
|
|
81
|
+
* @see {server.StateManager}
|
|
82
|
+
*/
|
|
83
|
+
declare class SharedState extends BaseSharedState {
|
|
84
|
+
}
|
|
85
|
+
import BaseSharedState from "../common/BaseSharedState.js";
|
|
86
|
+
//# sourceMappingURL=SharedState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SharedState.d.ts","sourceRoot":"","sources":["../../src/server/SharedState.js"],"names":[],"mappings":";;;;;;;AAIA;;;;;;;;GAQG;AAEH;;;;GAIG;AAGH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH;CAA4C"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
export default Socket;
|
|
2
|
+
/**
|
|
3
|
+
* Simple wrapper with simple pubsub system built on top of `ws` sockets.
|
|
4
|
+
* The abstraction contains two different socket:
|
|
5
|
+
* - one configured for string (JSON compatible) messages
|
|
6
|
+
* - one configured with `binaryType=arraybuffer` for streaming data more
|
|
7
|
+
* efficiently.
|
|
8
|
+
*
|
|
9
|
+
* @see https://github.com/websockets/ws
|
|
10
|
+
*
|
|
11
|
+
* @memberof server
|
|
12
|
+
*/
|
|
13
|
+
declare class Socket {
|
|
14
|
+
constructor(ws: any, binaryWs: any, rooms: any, sockets: any, options?: {});
|
|
15
|
+
/**
|
|
16
|
+
* Reference to the sockets object, is mainly dedicated to allow
|
|
17
|
+
* broadcasting from a given socket instance.
|
|
18
|
+
* @type {server.Sockets}
|
|
19
|
+
* @example
|
|
20
|
+
* socket.sockets.broadcast('my-room', this, 'update-value', 1);
|
|
21
|
+
*/
|
|
22
|
+
sockets: server.Sockets;
|
|
23
|
+
/**
|
|
24
|
+
* `ws` socket instance configured with `binaryType=blob` (string)
|
|
25
|
+
* @private
|
|
26
|
+
* @type {Object}
|
|
27
|
+
*/
|
|
28
|
+
private ws;
|
|
29
|
+
/**
|
|
30
|
+
* `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
|
|
31
|
+
* @private
|
|
32
|
+
* @type {Object}
|
|
33
|
+
*/
|
|
34
|
+
private binaryWs;
|
|
35
|
+
/**
|
|
36
|
+
* `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
|
|
37
|
+
* @private
|
|
38
|
+
* @type {Map}
|
|
39
|
+
*/
|
|
40
|
+
private rooms;
|
|
41
|
+
/**
|
|
42
|
+
* Configuration object
|
|
43
|
+
* @type {Object}
|
|
44
|
+
*/
|
|
45
|
+
config: any;
|
|
46
|
+
_stringListeners: Map<any, any>;
|
|
47
|
+
_binaryListeners: Map<any, any>;
|
|
48
|
+
_isAlive: boolean;
|
|
49
|
+
_intervalId: NodeJS.Timeout;
|
|
50
|
+
/**
|
|
51
|
+
* Called when the string socket closes (aka client reload).
|
|
52
|
+
*/
|
|
53
|
+
/** @private */
|
|
54
|
+
private terminate;
|
|
55
|
+
/** @private */
|
|
56
|
+
private _emit;
|
|
57
|
+
/** @private */
|
|
58
|
+
private _addListener;
|
|
59
|
+
/** @private */
|
|
60
|
+
private _removeListener;
|
|
61
|
+
/** @private */
|
|
62
|
+
private _removeAllListeners;
|
|
63
|
+
/**
|
|
64
|
+
* Add the socket to a room
|
|
65
|
+
* @param {String} roomId - Id of the room
|
|
66
|
+
*/
|
|
67
|
+
addToRoom(roomId: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* Remove the socket from a room
|
|
70
|
+
* @param {String} roomId - Id of the room
|
|
71
|
+
*/
|
|
72
|
+
removeFromRoom(roomId: string): void;
|
|
73
|
+
/**
|
|
74
|
+
* Sends JSON compatible messages on a given channel
|
|
75
|
+
*
|
|
76
|
+
* @param {String} channel - Channel of the message
|
|
77
|
+
* @param {...*} args - Arguments of the message (as many as needed, of any type)
|
|
78
|
+
*/
|
|
79
|
+
send(channel: string, ...args: any[]): void;
|
|
80
|
+
/**
|
|
81
|
+
* Listen JSON compatible messages on a given channel
|
|
82
|
+
*
|
|
83
|
+
* @param {String} channel - Channel of the message
|
|
84
|
+
* @param {Function} callback - Callback to execute when a message is received
|
|
85
|
+
*/
|
|
86
|
+
addListener(channel: string, callback: Function): void;
|
|
87
|
+
/**
|
|
88
|
+
* Remove a listener from JSON compatible messages on a given channel
|
|
89
|
+
*
|
|
90
|
+
* @param {String} channel - Channel of the message
|
|
91
|
+
* @param {Function} callback - Callback to cancel
|
|
92
|
+
*/
|
|
93
|
+
removeListener(channel: string, callback: Function): void;
|
|
94
|
+
/**
|
|
95
|
+
* Remove all listeners from JSON compatible messages on a given channel
|
|
96
|
+
*
|
|
97
|
+
* @param {String} channel - Channel of the message
|
|
98
|
+
*/
|
|
99
|
+
removeAllListeners(channel: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* Sends binary messages on a given channel
|
|
102
|
+
*
|
|
103
|
+
* @param {String} channel - Channel of the message
|
|
104
|
+
* @param {TypedArray} typedArray - Data to send
|
|
105
|
+
*/
|
|
106
|
+
sendBinary(channel: string, typedArray: TypedArray): void;
|
|
107
|
+
/**
|
|
108
|
+
* Listen binary messages on a given channel
|
|
109
|
+
*
|
|
110
|
+
* @param {String} channel - Channel of the message
|
|
111
|
+
* @param {Function} callback - Callback to execute when a message is received
|
|
112
|
+
*/
|
|
113
|
+
addBinaryListener(channel: string, callback: Function): void;
|
|
114
|
+
/**
|
|
115
|
+
* Remove a listener from binary compatible messages on a given channel
|
|
116
|
+
*
|
|
117
|
+
* @param {String} channel - Channel of the message
|
|
118
|
+
* @param {Function} callback - Callback to cancel
|
|
119
|
+
*/
|
|
120
|
+
removeBinaryListener(channel: string, callback: Function): void;
|
|
121
|
+
/**
|
|
122
|
+
* Remove all listeners from binary compatible messages on a given channel
|
|
123
|
+
*
|
|
124
|
+
* @param {String} channel - Channel of the message
|
|
125
|
+
*/
|
|
126
|
+
removeAllBinaryListeners(channel: string): void;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=Socket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Socket.d.ts","sourceRoot":"","sources":["../../src/server/Socket.js"],"names":[],"mappings":";AAcA;;;;;;;;;;GAUG;AACH;IACE,4EA2HC;IAzHC;;;;;;OAMG;IACH,wBAAsB;IAEtB;;;;OAIG;IACH,WAAY;IAEZ;;;;OAIG;IACH,iBAAwB;IAExB;;;;OAIG;IACH,cAAkB;IAElB;;;OAGG;IACH,YAGC;IAED,gCAAiC;IACjC,gCAAiC;IAoDjC,kBAAoB;IAiBpB,4BAW4B;IAG9B;;OAEG;IACH,eAAe;IACf,kBAmCC;IAED,eAAe;IACf,cAOC;IAED,eAAe;IACf,qBAOC;IAED,eAAe;IACf,wBASC;IAED,eAAe;IACf,4BAIC;IAED;;;OAGG;IACH,gCAOC;IAED;;;OAGG;IACH,qCAKC;IAED;;;;;OAKG;IACH,4CAcC;IAED;;;;;OAKG;IACH,uDAEC;IAED;;;;;OAKG;IACH,0DAEC;IAED;;;;OAIG;IACH,0CAEC;IAED;;;;;OAKG;IACH,0DAQC;IAED;;;;;OAKG;IACH,6DAEC;IAED;;;;;OAKG;IACH,gEAEC;IAED;;;;OAIG;IACH,gDAEC;CACF"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export default Sockets;
|
|
2
|
+
/**
|
|
3
|
+
* Websocket server that creates and host all {@link server.Socket} instance.
|
|
4
|
+
* Most of the time, you shouldn't have to use this class instance directely, but
|
|
5
|
+
* it could be usefull in some situations, for broadcasting messages, creating rooms, etc.
|
|
6
|
+
*
|
|
7
|
+
* @memberof server
|
|
8
|
+
*/
|
|
9
|
+
declare class Sockets {
|
|
10
|
+
/** @private */
|
|
11
|
+
private _wss;
|
|
12
|
+
/** @private */
|
|
13
|
+
private _rooms;
|
|
14
|
+
/** @private */
|
|
15
|
+
private _initializationCache;
|
|
16
|
+
/** @private */
|
|
17
|
+
private _latencyStatsWorker;
|
|
18
|
+
/** @private */
|
|
19
|
+
private _auditState;
|
|
20
|
+
/**
|
|
21
|
+
* Initialize sockets, all sockets are added by default added to two rooms:
|
|
22
|
+
* - to the room corresponding to the client `role`
|
|
23
|
+
* - to the '*' that holds all connected sockets
|
|
24
|
+
*
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
private start;
|
|
28
|
+
/** @protected */
|
|
29
|
+
protected terminate(): void;
|
|
30
|
+
/** @private */
|
|
31
|
+
private _broadcast;
|
|
32
|
+
/**
|
|
33
|
+
* Add a socket to a room
|
|
34
|
+
*
|
|
35
|
+
* @param {server.Socket} socket - Socket to add to the room.
|
|
36
|
+
* @param {String} roomId - Id of the room
|
|
37
|
+
*/
|
|
38
|
+
addToRoom(socket: server.Socket, roomId: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Remove a socket from a room
|
|
41
|
+
*
|
|
42
|
+
* @param {server.Socket} socket - Socket to remove from the room.
|
|
43
|
+
* @param {String} [roomId=null] - Id of the room
|
|
44
|
+
*/
|
|
45
|
+
removeFromRoom(socket: server.Socket, roomId?: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* Send a string message to all client of given room(s). If no room
|
|
48
|
+
* not specified, the message is sent to all clients
|
|
49
|
+
*
|
|
50
|
+
* @param {String|Array} roomsIds - Ids of the rooms that must receive
|
|
51
|
+
* the message. If null the message is sent to all clients
|
|
52
|
+
* @param {server.Socket} excludeSocket - Optionnal
|
|
53
|
+
* socket to ignore when broadcasting the message, typically the client
|
|
54
|
+
* at the origin of the message
|
|
55
|
+
* @param {String} channel - Channel of the message
|
|
56
|
+
* @param {...*} args - Arguments of the message (as many as needed, of any type)
|
|
57
|
+
*/
|
|
58
|
+
broadcast(roomIds: any, excludeSocket: server.Socket, channel: string, ...args: any[]): void;
|
|
59
|
+
/**
|
|
60
|
+
* Send a binary message (TypedArray) to all client of given room(s). If no room
|
|
61
|
+
* not specified, the message is sent to all clients
|
|
62
|
+
*
|
|
63
|
+
* @param {String|Array} roomsIds - Ids of the rooms that must receive
|
|
64
|
+
* the message. If null the message is sent to all clients
|
|
65
|
+
* @param {server.Socket} excludeSocket - Optionnal
|
|
66
|
+
* socket to ignore when broadcasting the message, typically the client
|
|
67
|
+
* at the origin of the message
|
|
68
|
+
* @param {String} channel - Channel of the message
|
|
69
|
+
* @param {...*} args - Arguments of the message (as many as needed, of any type)
|
|
70
|
+
*/
|
|
71
|
+
broadcastBinary(roomIds: any, excludeSocket: server.Socket, channel: string, typedArray: any): void;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=Sockets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Sockets.d.ts","sourceRoot":"","sources":["../../src/server/Sockets.js"],"names":[],"mappings":";AAYA;;;;;;GAMG;AACH;IAEI,eAAe;IACf,aAAgB;IAChB,eAAe;IACf,eAAuB;IAGvB,eAAe;IACf,6BAAqC;IAErC,eAAe;IACf,4BAA+B;IAC/B,eAAe;IACf,oBAAuB;IAIzB;;;;;;OAMG;IACH,cAwDC;IAED,iBAAiB;IACjB,4BAMC;IAED,eAAe;IACf,mBA8BC;IAED;;;;;OAKG;IACH,uDAEC;IAED;;;;;OAKG;IACH,6DAEC;IAED;;;;;;;;;;;OAWG;IACH,6FAEC;IAED;;;;;;;;;;;OAWG;IACH,oGAEC;CACF"}
|