@soundworks/core 4.0.0-alpha.13 → 4.0.0-alpha.15
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/CHANGELOG.md +4 -0
- package/README.md +13 -28
- package/package.json +17 -23
- package/src/client/Context.js +10 -11
- package/src/client/SharedStateCollection.js +5 -2
- package/src/client/Socket.js +0 -1
- package/src/common/BasePlugin.js +1 -1
- package/src/common/BasePluginManager.js +1 -1
- package/src/common/BaseSharedState.js +31 -27
- package/src/common/BaseSharedStateCollection.js +33 -10
- package/src/common/BaseStateManager.js +173 -53
- package/src/common/ParameterBag.js +11 -7
- package/src/common/PromiseStore.js +41 -0
- package/src/common/SharedStatePrivate.js +24 -27
- package/src/common/constants.js +2 -1
- package/src/common/sockets-utils.js +3 -4
- package/src/server/Server.js +2 -3
- package/src/server/SharedStateCollection.js +5 -2
- package/src/server/Socket.js +1 -1
- package/src/server/StateManager.js +34 -22
- package/src/server/audit-network-latency.worker.js +1 -1
- package/src/server/crypto.js +6 -7
- package/src/common/promise-store.js +0 -25
- package/tsconfig.json +0 -21
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import EventEmitter from 'node:events';
|
|
2
2
|
|
|
3
3
|
import { idGenerator, isString, isPlainObject } from '@ircam/sc-utils';
|
|
4
|
-
import clonedeep from 'lodash.
|
|
4
|
+
import clonedeep from 'lodash/cloneDeep.js';
|
|
5
5
|
|
|
6
6
|
import BaseStateManager from '../common/BaseStateManager.js';
|
|
7
7
|
import ParameterBag from '../common/ParameterBag.js';
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
ATTACH_ERROR,
|
|
18
18
|
OBSERVE_REQUEST,
|
|
19
19
|
OBSERVE_RESPONSE,
|
|
20
|
+
OBSERVE_ERROR,
|
|
20
21
|
OBSERVE_NOTIFICATION,
|
|
21
22
|
UNOBSERVE_NOTIFICATION,
|
|
22
23
|
DELETE_SCHEMA,
|
|
@@ -359,17 +360,21 @@ class StateManager extends BaseStateManager {
|
|
|
359
360
|
client.transport.emit(CREATE_RESPONSE, reqId, stateId, remoteId, schemaName, sendedSchema, currentValues);
|
|
360
361
|
|
|
361
362
|
this._observers.forEach(observer => {
|
|
362
|
-
|
|
363
|
+
// prevent propagating infos about internal states
|
|
364
|
+
if (!PRIVATE_STATES.includes(schemaName)) {
|
|
363
365
|
observer.transport.emit(OBSERVE_NOTIFICATION, schemaName, stateId, nodeId);
|
|
364
366
|
}
|
|
365
367
|
});
|
|
366
368
|
} catch (err) {
|
|
367
|
-
|
|
368
|
-
console.error(
|
|
369
|
+
const msg = `Cannot create state "${schemaName}", ${err.message}`;
|
|
370
|
+
console.error(msg);
|
|
371
|
+
|
|
372
|
+
client.transport.emit(CREATE_ERROR, reqId, msg);
|
|
369
373
|
}
|
|
370
374
|
} else {
|
|
371
375
|
const msg = `Cannot create state "${schemaName}", schema does not exists`;
|
|
372
376
|
console.error(msg);
|
|
377
|
+
|
|
373
378
|
client.transport.emit(CREATE_ERROR, reqId, msg);
|
|
374
379
|
}
|
|
375
380
|
});
|
|
@@ -410,36 +415,43 @@ class StateManager extends BaseStateManager {
|
|
|
410
415
|
client.transport.emit(ATTACH_RESPONSE, reqId, state.id, remoteId, schemaName, sendedSchema, currentValues);
|
|
411
416
|
} else {
|
|
412
417
|
const msg = `Cannot attach, no existing state for schema "${schemaName}" with stateId: "${stateId}"`;
|
|
413
|
-
client.transport.emit(ATTACH_ERROR, reqId, msg);
|
|
414
418
|
console.error(msg);
|
|
419
|
+
|
|
420
|
+
client.transport.emit(ATTACH_ERROR, reqId, msg);
|
|
415
421
|
}
|
|
416
422
|
} else {
|
|
417
423
|
const msg = `Cannot attach, schema "${schemaName}" does not exists`;
|
|
418
|
-
client.transport.emit(ATTACH_ERROR, reqId, msg);
|
|
419
424
|
console.error(msg);
|
|
425
|
+
|
|
426
|
+
client.transport.emit(ATTACH_ERROR, reqId, msg);
|
|
420
427
|
}
|
|
421
428
|
});
|
|
422
429
|
|
|
423
430
|
// ---------------------------------------------
|
|
424
431
|
// OBSERVE PEERS (be notified when a state is created, lazy)
|
|
425
432
|
// ---------------------------------------------
|
|
426
|
-
client.transport.addListener(OBSERVE_REQUEST, reqId => {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
this._serverStatesById.forEach(state => {
|
|
430
|
-
const { schemaName, id, _creatorId } = state;
|
|
431
|
-
// only track application states
|
|
432
|
-
// (e.g. do not propagate infos about audit state)
|
|
433
|
-
if (!PRIVATE_STATES.includes(schemaName) && _creatorId !== client.id) {
|
|
434
|
-
statesInfos.push([schemaName, id, _creatorId]);
|
|
435
|
-
}
|
|
436
|
-
});
|
|
433
|
+
client.transport.addListener(OBSERVE_REQUEST, (reqId, observedSchemaName) => {
|
|
434
|
+
if (observedSchemaName === null || this._schemas.has(observedSchemaName)) {
|
|
435
|
+
const statesInfos = [];
|
|
437
436
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
437
|
+
this._serverStatesById.forEach(state => {
|
|
438
|
+
const { schemaName, id, _creatorId } = state;
|
|
439
|
+
|
|
440
|
+
// prevent propagating infos about internal states
|
|
441
|
+
if (!PRIVATE_STATES.includes(schemaName)) {
|
|
442
|
+
statesInfos.push([schemaName, id, _creatorId]);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
441
445
|
|
|
442
|
-
|
|
446
|
+
// add client to observers first because if some synchronous server side
|
|
447
|
+
// callback throws, the client would never be added to the list
|
|
448
|
+
this._observers.add(client);
|
|
449
|
+
|
|
450
|
+
client.transport.emit(OBSERVE_RESPONSE, reqId, ...statesInfos);
|
|
451
|
+
} else {
|
|
452
|
+
const msg = `Cannot observe, schema "${observedSchemaName}" does not exists`;
|
|
453
|
+
client.transport.emit(OBSERVE_ERROR, reqId, msg);
|
|
454
|
+
}
|
|
443
455
|
});
|
|
444
456
|
|
|
445
457
|
client.transport.addListener(UNOBSERVE_NOTIFICATION, () => {
|
|
@@ -554,7 +566,7 @@ class StateManager extends BaseStateManager {
|
|
|
554
566
|
attached.transport.emit(`${DELETE_NOTIFICATION}-${state.id}-${remoteId}`);
|
|
555
567
|
}
|
|
556
568
|
|
|
557
|
-
this._serverStatesById.delete(
|
|
569
|
+
this._serverStatesById.delete(state.id);
|
|
558
570
|
}
|
|
559
571
|
}
|
|
560
572
|
|
package/src/server/crypto.js
CHANGED
|
@@ -23,16 +23,15 @@ const encryptionIV = crypto
|
|
|
23
23
|
|
|
24
24
|
export function encryptData(obj) {
|
|
25
25
|
const data = JSON.stringify(obj);
|
|
26
|
-
const cipher = crypto.createCipheriv(encryptionMethod, key, encryptionIV)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
).toString('base64')
|
|
26
|
+
const cipher = crypto.createCipheriv(encryptionMethod, key, encryptionIV);
|
|
27
|
+
|
|
28
|
+
return Buffer.from(cipher.update(data, 'utf8', 'hex') + cipher.final('hex')).toString('base64');
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
export function decryptData(encryptedData) {
|
|
33
|
-
const buff = Buffer.from(encryptedData, 'base64')
|
|
34
|
-
const decipher = crypto.createDecipheriv(encryptionMethod, key, encryptionIV)
|
|
32
|
+
const buff = Buffer.from(encryptedData, 'base64');
|
|
33
|
+
const decipher = crypto.createDecipheriv(encryptionMethod, key, encryptionIV);
|
|
35
34
|
|
|
36
|
-
const decrypted = `${decipher.update(buff.toString('utf8'), 'hex', 'utf8')}${decipher.final('utf8')}
|
|
35
|
+
const decrypted = `${decipher.update(buff.toString('utf8'), 'hex', 'utf8')}${decipher.final('utf8')}`;
|
|
37
36
|
return JSON.parse(decrypted);
|
|
38
37
|
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { idGenerator } from '@ircam/sc-utils';
|
|
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/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
|
-
}
|