@soundworks/core 4.0.0-alpha.21 → 4.0.0-alpha.23

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 (87) hide show
  1. package/CHANGELOG.md +14 -3
  2. package/package.json +15 -15
  3. package/src/client/Client.js +233 -188
  4. package/src/client/Socket.js +84 -247
  5. package/src/client/index.js +6 -0
  6. package/src/common/constants.js +0 -1
  7. package/src/common/logger.js +15 -0
  8. package/src/common/sockets-utils.js +0 -63
  9. package/src/common/version.js +2 -0
  10. package/src/server/Plugin.js +22 -10
  11. package/src/server/Server.js +37 -36
  12. package/src/server/Socket.js +126 -250
  13. package/src/server/Sockets.js +103 -127
  14. package/src/server/audit-network-latency.worker.js +4 -3
  15. package/src/server/index.js +6 -0
  16. package/types/client/Client.d.ts +41 -8
  17. package/types/client/Client.d.ts.map +1 -1
  18. package/types/client/Context.d.ts +40 -14
  19. package/types/client/Context.d.ts.map +1 -1
  20. package/types/client/Plugin.d.ts +42 -41
  21. package/types/client/Plugin.d.ts.map +1 -1
  22. package/types/client/PluginManager.d.ts +26 -11
  23. package/types/client/PluginManager.d.ts.map +1 -1
  24. package/types/client/SharedState.d.ts +1 -1
  25. package/types/client/SharedState.d.ts.map +1 -1
  26. package/types/client/SharedStateCollection.d.ts +41 -0
  27. package/types/client/SharedStateCollection.d.ts.map +1 -0
  28. package/types/client/Socket.d.ts +41 -30
  29. package/types/client/Socket.d.ts.map +1 -1
  30. package/types/client/StateManager.d.ts +1 -1
  31. package/types/client/StateManager.d.ts.map +1 -1
  32. package/types/client/index.d.ts +0 -8
  33. package/types/common/BasePlugin.d.ts +24 -19
  34. package/types/common/BasePlugin.d.ts.map +1 -1
  35. package/types/common/BasePluginManager.d.ts +17 -5
  36. package/types/common/BasePluginManager.d.ts.map +1 -1
  37. package/types/common/BaseSharedState.d.ts +100 -29
  38. package/types/common/BaseSharedState.d.ts.map +1 -1
  39. package/types/common/BaseSharedStateCollection.d.ts +183 -0
  40. package/types/common/BaseSharedStateCollection.d.ts.map +1 -0
  41. package/types/common/BaseStateManager.d.ts +83 -18
  42. package/types/common/BaseStateManager.d.ts.map +1 -1
  43. package/types/common/BatchedTransport.d.ts +21 -0
  44. package/types/common/BatchedTransport.d.ts.map +1 -0
  45. package/types/common/ParameterBag.d.ts +61 -40
  46. package/types/common/ParameterBag.d.ts.map +1 -1
  47. package/types/common/PromiseStore.d.ts +11 -0
  48. package/types/common/PromiseStore.d.ts.map +1 -0
  49. package/types/common/SharedStatePrivate.d.ts +4 -3
  50. package/types/common/SharedStatePrivate.d.ts.map +1 -1
  51. package/types/common/constants.d.ts +10 -1
  52. package/types/common/constants.d.ts.map +1 -1
  53. package/types/common/logger.d.ts +1 -1
  54. package/types/common/sockets-utils.d.ts.map +1 -1
  55. package/types/common/version.d.ts +3 -0
  56. package/types/common/version.d.ts.map +1 -0
  57. package/types/server/Client.d.ts +5 -0
  58. package/types/server/Client.d.ts.map +1 -1
  59. package/types/server/Context.d.ts +162 -22
  60. package/types/server/Context.d.ts.map +1 -1
  61. package/types/server/ContextManager.d.ts +4 -4
  62. package/types/server/ContextManager.d.ts.map +1 -1
  63. package/types/server/Plugin.d.ts +56 -55
  64. package/types/server/Plugin.d.ts.map +1 -1
  65. package/types/server/PluginManager.d.ts +26 -6
  66. package/types/server/PluginManager.d.ts.map +1 -1
  67. package/types/server/Server.d.ts +207 -70
  68. package/types/server/Server.d.ts.map +1 -1
  69. package/types/server/SharedState.d.ts +1 -1
  70. package/types/server/SharedState.d.ts.map +1 -1
  71. package/types/server/SharedStateCollection.d.ts +41 -0
  72. package/types/server/SharedStateCollection.d.ts.map +1 -0
  73. package/types/server/Socket.d.ts +93 -57
  74. package/types/server/Socket.d.ts.map +1 -1
  75. package/types/server/Sockets.d.ts +32 -25
  76. package/types/server/Sockets.d.ts.map +1 -1
  77. package/types/server/StateManager.d.ts +242 -27
  78. package/types/server/StateManager.d.ts.map +1 -1
  79. package/types/server/audit-network-latency.worker.d.ts +2 -1
  80. package/types/server/audit-schema.d.ts +8 -8
  81. package/types/server/crypto.d.ts +3 -0
  82. package/types/server/crypto.d.ts.map +1 -0
  83. package/types/server/index.d.ts +0 -4
  84. package/types/common/promise-store.d.ts +0 -4
  85. package/types/common/promise-store.d.ts.map +0 -1
  86. package/types/common/utils.d.ts +0 -5
  87. package/types/common/utils.d.ts.map +0 -1
@@ -1,128 +1,164 @@
1
1
  export default Socket;
2
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.
3
+ * The Socket class is a simple publish / subscribe wrapper built on top of the
4
+ * [ws](https://github.com/websockets/ws) library. An instance of {@link server.Socket}
5
+ * is automatically created per client when it connects (see {@link server.Client#socket}).
8
6
  *
9
- * @see https://github.com/websockets/ws
7
+ * _Important: In most cases, you should consider using a {@link client.SharedState}
8
+ * rather than directly using the sockets._
9
+ *
10
+ * The Socket class contains two different WebSockets:
11
+ * - a socket configured with `binaryType = 'blob'` for JSON compatible data
12
+ * types (i.e. string, number, boolean, object, array and null).
13
+ * - a socket configured with `binaryType= 'arraybuffer'` for efficient streaming
14
+ * of binary data.
10
15
  *
11
16
  * @memberof server
17
+ * @hideconstructor
12
18
  */
13
19
  declare class Socket {
14
- constructor(ws: any, binaryWs: any, rooms: any, sockets: any, options?: {});
20
+ constructor(ws: any, binaryWs: any, rooms: any, sockets: any);
21
+ /**
22
+ * `ws` socket instance configured with `binaryType=blob` (string)
23
+ *
24
+ * @type {object}
25
+ * @private
26
+ */
27
+ private ws;
28
+ /**
29
+ * `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
30
+ *
31
+ * @type {object}
32
+ * @private
33
+ */
34
+ private binaryWs;
15
35
  /**
16
36
  * Reference to the sockets object, is mainly dedicated to allow
17
37
  * broadcasting from a given socket instance.
38
+ *
18
39
  * @type {server.Sockets}
19
40
  * @example
20
41
  * socket.sockets.broadcast('my-room', this, 'update-value', 1);
21
42
  */
22
43
  sockets: server.Sockets;
23
44
  /**
24
- * `ws` socket instance configured with `binaryType=blob` (string)
45
+ * Reference to the rooms object
46
+ *
47
+ * @type {Map}
25
48
  * @private
26
- * @type {Object}
27
49
  */
28
- private ws;
50
+ private rooms;
51
+ /** @private */
52
+ private _stringListeners;
53
+ /** @private */
54
+ private _binaryListeners;
55
+ /** @private */
56
+ private _heartbeatId;
57
+ /** @private */
58
+ private _isAlive;
29
59
  /**
30
- * `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
60
+ * Removes all listeners and immediately close the two sockets. Is automatically
61
+ * called on `server.stop()`
62
+ *
31
63
  * @private
32
- * @type {Object}
33
64
  */
34
- private binaryWs;
65
+ private terminate;
35
66
  /**
36
- * `ws` socket instance configured with `binaryType=arraybuffer` (TypedArray)
67
+ * @param {boolean} binary - Emit to either the string or binary socket.
68
+ * @param {string} channel - Channel name.
69
+ * @param {...*} args - Content of the message.
37
70
  * @private
38
- * @type {Map}
39
71
  */
40
- private rooms;
72
+ private _emit;
41
73
  /**
42
- * Configuration object
43
- * @type {Object}
74
+ * @param {Function[]} listeners - List of listeners, either for the string or binary socket.
75
+ * @param {string} channel - Channel name.
76
+ * @param {Function} callback - The function to be added to the listeners.
77
+ * @private
44
78
  */
45
- config: any;
46
- _stringListeners: Map<any, any>;
47
- _binaryListeners: Map<any, any>;
48
- _isAlive: boolean;
49
- _intervalId: NodeJS.Timeout;
79
+ private _addListener;
50
80
  /**
51
- * Called when the string socket closes (aka client reload).
81
+ * @param {Function[]} listeners - List of listeners, either for the string or binary socket.
82
+ * @param {string} channel - Channel name.
83
+ * @param {Function} callback - The function to be removed from the listeners.
84
+ * @private
52
85
  */
53
- /** @private */
54
- private terminate;
55
- /** @private */
56
- private _emit;
57
- /** @private */
58
- private _addListener;
59
- /** @private */
60
86
  private _removeListener;
61
- /** @private */
87
+ /**
88
+ * @param {Function[]} listeners - List of listeners, either for the string or binary socket.
89
+ * @param {string} [channel=null] - Channel name of the listeners to remove. If null
90
+ * all the listeners are cleared.
91
+ * @private
92
+ */
62
93
  private _removeAllListeners;
63
94
  /**
64
95
  * Add the socket to a room
65
- * @param {String} roomId - Id of the room
96
+ *
97
+ * @param {string} roomId - Id of the room.
66
98
  */
67
99
  addToRoom(roomId: string): void;
68
100
  /**
69
101
  * Remove the socket from a room
70
- * @param {String} roomId - Id of the room
102
+ *
103
+ * @param {string} roomId - Id of the room.
71
104
  */
72
105
  removeFromRoom(roomId: string): void;
73
106
  /**
74
- * Sends JSON compatible messages on a given channel
107
+ * Send messages with JSON compatible data types on a given channel.
75
108
  *
76
- * @param {String} channel - Channel of the message
77
- * @param {...*} args - Arguments of the message (as many as needed, of any type)
109
+ * @param {string} channel - Channel name.
110
+ * @param {...*} args - Payload of the message. As many arguments as needed, of
111
+ * JSON compatible data types (i.e. string, number, boolean, object, array and null).
78
112
  */
79
113
  send(channel: string, ...args: any[]): void;
80
114
  /**
81
- * Listen JSON compatible messages on a given channel
115
+ * Listen messages with JSON compatible data types on a given channel.
82
116
  *
83
- * @param {String} channel - Channel of the message
84
- * @param {Function} callback - Callback to execute when a message is received
117
+ * @param {string} channel - Channel name.
118
+ * @param {Function} callback - Callback to execute when a message is received,
119
+ * arguments of the callback function will match the arguments sent using the
120
+ * {@link server.Socket#send} method.
85
121
  */
86
122
  addListener(channel: string, callback: Function): void;
87
123
  /**
88
- * Remove a listener from JSON compatible messages on a given channel
124
+ * Remove a listener of messages with JSON compatible data types from a given channel.
89
125
  *
90
- * @param {String} channel - Channel of the message
91
- * @param {Function} callback - Callback to cancel
126
+ * @param {string} channel - Channel name.
127
+ * @param {Function} callback - Callback to remove.
92
128
  */
93
129
  removeListener(channel: string, callback: Function): void;
94
130
  /**
95
- * Remove all listeners from JSON compatible messages on a given channel
131
+ * Remove all listeners of messages with JSON compatible data types.
96
132
  *
97
- * @param {String} channel - Channel of the message
133
+ * @param {string} channel - Channel name.
98
134
  */
99
- removeAllListeners(channel: string): void;
135
+ removeAllListeners(channel?: string): void;
100
136
  /**
101
- * Sends binary messages on a given channel
137
+ * Send binary messages on a given channel.
102
138
  *
103
- * @param {String} channel - Channel of the message
104
- * @param {TypedArray} typedArray - Data to send
139
+ * @param {string} channel - Channel name.
140
+ * @param {TypedArray} typedArray - Binary data to be sent.
105
141
  */
106
142
  sendBinary(channel: string, typedArray: TypedArray): void;
107
143
  /**
108
144
  * Listen binary messages on a given channel
109
145
  *
110
- * @param {String} channel - Channel of the message
111
- * @param {Function} callback - Callback to execute when a message is received
146
+ * @param {string} channel - Channel name.
147
+ * @param {Function} callback - Callback to execute when a message is received.
112
148
  */
113
149
  addBinaryListener(channel: string, callback: Function): void;
114
150
  /**
115
- * Remove a listener from binary compatible messages on a given channel
151
+ * Remove a listener of binary compatible messages from a given channel
116
152
  *
117
- * @param {String} channel - Channel of the message
118
- * @param {Function} callback - Callback to cancel
153
+ * @param {string} channel - Channel name.
154
+ * @param {Function} callback - Callback to remove.
119
155
  */
120
156
  removeBinaryListener(channel: string, callback: Function): void;
121
157
  /**
122
- * Remove all listeners from binary compatible messages on a given channel
158
+ * Remove all listeners of binary compatible messages on a given channel
123
159
  *
124
- * @param {String} channel - Channel of the message
160
+ * @param {string} channel - Channel of the message.
125
161
  */
126
- removeAllBinaryListeners(channel: string): void;
162
+ removeAllBinaryListeners(channel?: string): void;
127
163
  }
128
164
  //# sourceMappingURL=Socket.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"Socket.d.ts","sourceRoot":"","sources":["../../src/server/Socket.js"],"names":[],"mappings":";AAqBA;;;;;;;;;;;;;;;;GAgBG;AACH;IACE,8DAiIC;IAhIC;;;;;OAKG;IACH,WAAY;IAEZ;;;;;OAKG;IACH,iBAAwB;IAExB;;;;;;;OAOG;IACH,wBAAsB;IAEtB;;;;;OAKG;IACH,cAAkB;IAElB,eAAe;IACf,yBAAiC;IACjC,eAAe;IACf,yBAAiC;IACjC,eAAe;IACf,qBAAwB;IAIxB,eAAe;IACf,iBAAoB;IAsFtB;;;;;OAKG;IACH,kBAoCC;IAED;;;;;OAKG;IACH,cAOC;IAED;;;;;OAKG;IACH,qBAOC;IAED;;;;;OAKG;IACH,wBASC;IAED;;;;;OAKG;IACH,4BAMC;IAED;;;;OAIG;IACH,kBAFW,MAAM,QAShB;IAED;;;;OAIG;IACH,uBAFW,MAAM,QAOhB;IAED;;;;;;OAMG;IACH,cAJW,MAAM,wBAkBhB;IAED;;;;;;;OAOG;IACH,qBALW,MAAM,4BAOhB;IAED;;;;;OAKG;IACH,wBAHW,MAAM,4BAKhB;IAED;;;;OAIG;IACH,6BAFW,MAAM,QAIhB;IAED;;;;;OAKG;IACH,oBAHW,MAAM,gCAWhB;IAED;;;;;OAKG;IACH,2BAHW,MAAM,4BAKhB;IAED;;;;;OAKG;IACH,8BAHW,MAAM,4BAKhB;IAED;;;;OAIG;IACH,mCAFW,MAAM,QAIhB;CACF"}
@@ -1,6 +1,7 @@
1
1
  export default Sockets;
2
2
  /**
3
- * Websocket server that creates and host all {@link server.Socket} instance.
3
+ * Manager all {@link server.Socket} instances.
4
+ *
4
5
  * Most of the time, you shouldn't have to use this class instance directely, but
5
6
  * it could be usefull in some situations, for broadcasting messages, creating rooms, etc.
6
7
  *
@@ -17,57 +18,63 @@ declare class Sockets {
17
18
  private _latencyStatsWorker;
18
19
  /** @private */
19
20
  private _auditState;
21
+ /** @private */
22
+ private _DEBUG_PREVENT_HEARTBEAT;
20
23
  /**
21
- * Initialize sockets, all sockets are added by default added to two rooms:
24
+ * Initialize sockets, all sockets are added to two rooms by default:
22
25
  * - to the room corresponding to the client `role`
23
- * - to the '*' that holds all connected sockets
26
+ * - to the '*' room that holds all connected sockets
24
27
  *
25
28
  * @private
26
29
  */
27
30
  private start;
28
- /** @protected */
29
- protected terminate(): void;
31
+ /**
32
+ * Terminate all existing sockets
33
+ *
34
+ * @private
35
+ */
36
+ private terminate;
30
37
  /** @private */
31
38
  private _broadcast;
32
39
  /**
33
40
  * Add a socket to a room
34
41
  *
35
42
  * @param {server.Socket} socket - Socket to add to the room.
36
- * @param {String} roomId - Id of the room
43
+ * @param {String} roomId - Id of the room.
37
44
  */
38
45
  addToRoom(socket: server.Socket, roomId: string): void;
39
46
  /**
40
47
  * Remove a socket from a room
41
48
  *
42
49
  * @param {server.Socket} socket - Socket to remove from the room.
43
- * @param {String} [roomId=null] - Id of the room
50
+ * @param {String} roomId - Id of the room.
44
51
  */
45
- removeFromRoom(socket: server.Socket, roomId?: string): void;
52
+ removeFromRoom(socket: server.Socket, roomId: string): void;
46
53
  /**
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
54
+ * Send a message of JSON compatible data types to all client of given room(s).
55
+ * If no room is specified, the message is sent to all clients.
56
+ *
49
57
  *
50
58
  * @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)
59
+ * the message. If `null` the message is sent to all clients.
60
+ * @param {server.Socket} excludeSocket - Optionnal socket to ignore when
61
+ * broadcasting the message, typically the client at the origin of the message.
62
+ * @param {String} channel - Channel name.
63
+ * @param {...*} args - Payload of the message. As many arguments as needed, of
64
+ * JSON compatible data types (i.e. string, number, boolean, object, array and null).
57
65
  */
58
66
  broadcast(roomIds: any, excludeSocket: server.Socket, channel: string, ...args: any[]): void;
59
67
  /**
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
68
+ * Send a binary message to all client of given room(s). If no room is specified
69
+ * specified, the message is sent to all clients.
62
70
  *
63
71
  * @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)
72
+ * the message. If `null` the message is sent to all clients.
73
+ * @param {server.Socket} excludeSocket - Optionnal socket to ignore when
74
+ * broadcasting the message, typically the client at the origin of the message.
75
+ * @param {string} channel - Channel name.
76
+ * @param {TypedArray} typedArray - Binary data to be sent.
70
77
  */
71
- broadcastBinary(roomIds: any, excludeSocket: server.Socket, channel: string, typedArray: any): void;
78
+ broadcastBinary(roomIds: any, excludeSocket: server.Socket, channel: string, typedArray: TypedArray): void;
72
79
  }
73
80
  //# sourceMappingURL=Sockets.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"Sockets.d.ts","sourceRoot":"","sources":["../../src/server/Sockets.js"],"names":[],"mappings":";AAUA;;;;;;;GAOG;AACH;IAEI,eAAe;IACf,aAAgB;IAChB,eAAe;IACf,eAAuB;IAGvB,eAAe;IACf,6BAAqC;IAErC,eAAe;IACf,4BAA+B;IAC/B,eAAe;IACf,oBAAuB;IAEvB,eAAe;IACf,iCAAqC;IAGvC;;;;;;OAMG;IACH,cA8EC;IAED;;;;OAIG;IACH,kBAMC;IAED,eAAe;IACf,mBA8BC;IAED;;;;;OAKG;IACH,uDAEC;IAED;;;;;OAKG;IACH,4DAEC;IAED;;;;;;;;;;;;OAYG;IACH,6FAEC;IAED;;;;;;;;;;OAUG;IACH,qEAHW,MAAM,gCAKhB;CACF"}