@soundworks/core 4.0.0-alpha.2 → 4.0.0-alpha.21
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 +0 -1
- package/.jsdoc +57 -0
- package/CHANGELOG.md +33 -0
- package/README.md +17 -51
- package/package.json +24 -31
- package/src/client/Client.js +13 -7
- package/src/client/Context.js +12 -13
- package/src/client/ContextManager.js +1 -1
- package/src/client/PluginManager.js +1 -1
- package/src/client/SharedStateCollection.js +38 -0
- package/src/client/Socket.js +80 -44
- package/src/common/BasePlugin.js +31 -21
- package/src/common/BasePluginManager.js +34 -19
- package/src/common/BaseSharedState.js +304 -79
- package/src/common/BaseSharedStateCollection.js +324 -0
- package/src/common/BaseStateManager.js +353 -49
- package/src/common/BatchedTransport.js +73 -0
- package/src/common/ParameterBag.js +92 -30
- package/src/common/PromiseStore.js +53 -0
- package/src/common/SharedStatePrivate.js +87 -46
- package/src/common/constants.js +14 -1
- package/src/common/sockets-utils.js +3 -4
- package/src/server/Client.js +7 -2
- package/src/server/ContextManager.js +4 -4
- package/src/server/PluginManager.js +1 -1
- package/src/server/Server.js +263 -43
- package/src/server/SharedState.js +0 -1
- package/src/server/SharedStateCollection.js +39 -0
- package/src/server/Socket.js +55 -49
- package/src/server/Sockets.js +33 -11
- package/src/server/StateManager.js +187 -100
- package/src/server/audit-network-latency.worker.js +16 -4
- package/src/server/crypto.js +37 -0
- package/src/common/promise-store.js +0 -25
- package/src/common/utils.js +0 -22
- package/tsconfig.json +0 -21
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// NOTICE: we use this syntax so that the server can be bundled to cjs
|
|
2
|
+
// with esbuild, so we can ship a cjs server bundle into Max.
|
|
3
|
+
// Should move back to regular esm module once Max has fixed its loader
|
|
4
|
+
export default `
|
|
5
|
+
const { parentPort } = require('node:worker_threads');
|
|
4
6
|
|
|
5
7
|
let stack = [];
|
|
6
8
|
let averageLatencyWindow = 5;
|
|
@@ -9,12 +11,21 @@ let averageLatencyPeriod = 2;
|
|
|
9
11
|
let intervalId = null;
|
|
10
12
|
let meanLatency = 0;
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
let getTime;
|
|
15
|
+
let inited = new Promise(async (resolve) => {
|
|
16
|
+
module = await import('@ircam/sc-utils');
|
|
17
|
+
getTime = module.getTime;
|
|
18
|
+
resolve(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
parentPort.on('message', async msg => {
|
|
13
22
|
switch (msg.type) {
|
|
14
23
|
case 'config': {
|
|
15
24
|
averageLatencyWindow = msg.value.averageLatencyWindow;
|
|
16
25
|
averageLatencyPeriod = msg.value.averageLatencyPeriod;
|
|
17
26
|
|
|
27
|
+
await inited;
|
|
28
|
+
|
|
18
29
|
// launch compute in its own loop so that the number of computation is
|
|
19
30
|
// decoupled from the number of connected clients
|
|
20
31
|
clearInterval(intervalId);
|
|
@@ -55,3 +66,4 @@ function computeAverageLatency() {
|
|
|
55
66
|
});
|
|
56
67
|
}
|
|
57
68
|
}
|
|
69
|
+
`;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
|
|
4
|
+
// adapted from https://dev.to/jobizil/encrypt-and-decrypt-data-in-nodejs-using-aes-256-cbc-2l6d
|
|
5
|
+
// generate a new key and init vector each time the server restart, we don't need
|
|
6
|
+
// any persistency here
|
|
7
|
+
const secretKey = uuidv4();
|
|
8
|
+
const secretIv = uuidv4();
|
|
9
|
+
const encryptionMethod = 'aes-256-cbc';
|
|
10
|
+
|
|
11
|
+
const key = crypto
|
|
12
|
+
.createHash('sha512')
|
|
13
|
+
.update(secretKey)
|
|
14
|
+
.digest('hex')
|
|
15
|
+
.substring(0, 32);
|
|
16
|
+
|
|
17
|
+
const encryptionIV = crypto
|
|
18
|
+
.createHash('sha512')
|
|
19
|
+
.update(secretIv)
|
|
20
|
+
.digest('hex')
|
|
21
|
+
.substring(0, 16);
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export function encryptData(obj) {
|
|
25
|
+
const data = JSON.stringify(obj);
|
|
26
|
+
const cipher = crypto.createCipheriv(encryptionMethod, key, encryptionIV);
|
|
27
|
+
|
|
28
|
+
return Buffer.from(cipher.update(data, 'utf8', 'hex') + cipher.final('hex')).toString('base64');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function decryptData(encryptedData) {
|
|
32
|
+
const buff = Buffer.from(encryptedData, 'base64');
|
|
33
|
+
const decipher = crypto.createDecipheriv(encryptionMethod, key, encryptionIV);
|
|
34
|
+
|
|
35
|
+
const decrypted = `${decipher.update(buff.toString('utf8'), 'hex', 'utf8')}${decipher.final('utf8')}`;
|
|
36
|
+
return JSON.parse(decrypted);
|
|
37
|
+
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { idGenerator } from './utils.js';
|
|
2
|
-
|
|
3
|
-
const generateRequestId = idGenerator();
|
|
4
|
-
const requestPromises = new Map();
|
|
5
|
-
|
|
6
|
-
export function storeRequestPromise(resolve, reject) {
|
|
7
|
-
const reqId = generateRequestId.next().value;
|
|
8
|
-
requestPromises.set(reqId, { resolve, reject });
|
|
9
|
-
|
|
10
|
-
return reqId;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function resolveRequest(reqId, data) {
|
|
14
|
-
const { resolve } = requestPromises.get(reqId);
|
|
15
|
-
requestPromises.delete(reqId);
|
|
16
|
-
|
|
17
|
-
resolve(data);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function rejectRequest(reqId, data) {
|
|
21
|
-
const { reject } = requestPromises.get(reqId);
|
|
22
|
-
requestPromises.delete(reqId);
|
|
23
|
-
|
|
24
|
-
reject(data);
|
|
25
|
-
}
|
package/src/common/utils.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// https://stackoverflow.com/questions/17575790/environment-detection-node-js-or-browser
|
|
2
|
-
export const isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
|
|
3
|
-
|
|
4
|
-
export function* idGenerator() {
|
|
5
|
-
for (let i = 0; true; i++) {
|
|
6
|
-
if (i === Number.MAX_SAFE_INTEGER) {
|
|
7
|
-
i = 0;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
yield i;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function isString(val) {
|
|
15
|
-
return (typeof val === 'string' || val instanceof String);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function isFunction(func) {
|
|
19
|
-
return Object.prototype.toString.call(func) == '[object Function]' ||
|
|
20
|
-
Object.prototype.toString.call(func) == '[object AsyncFunction]';
|
|
21
|
-
}
|
|
22
|
-
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"include": ["src/**/*.js"],
|
|
3
|
-
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
// Tells TypeScript to read JS files, as
|
|
6
|
-
// normally they are ignored as source files
|
|
7
|
-
"allowJs": true,
|
|
8
|
-
// Generate d.ts files
|
|
9
|
-
"declaration": true,
|
|
10
|
-
// This compiler run should
|
|
11
|
-
// only output d.ts files
|
|
12
|
-
"emitDeclarationOnly": true,
|
|
13
|
-
// Types should go into this directory.
|
|
14
|
-
// Removing this would place the .d.ts files
|
|
15
|
-
// next to the .js files
|
|
16
|
-
"outDir": "types",
|
|
17
|
-
// go to js file when using IDE functions like
|
|
18
|
-
// "Go to Definition" in VSCode
|
|
19
|
-
"declarationMap": true
|
|
20
|
-
}
|
|
21
|
-
}
|