thor-io.vnext 2.0.5-beta.3 → 3.1.0-beta.1

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 (185) hide show
  1. package/.gitattributes +17 -17
  2. package/build/index.d.ts +24 -0
  3. package/build/index.js +47 -0
  4. package/build/src/Connection/ClientInfo.d.ts +27 -0
  5. package/{src/Client → build/src/Connection}/ClientInfo.js +7 -0
  6. package/build/src/Connection/Connection.d.ts +117 -0
  7. package/build/src/Connection/Connection.js +202 -0
  8. package/build/src/Connection/IClientInfo.d.ts +20 -0
  9. package/build/src/Controller/ControllerBase.d.ts +134 -0
  10. package/build/src/Controller/ControllerBase.js +543 -0
  11. package/build/src/Controller/Subscription.d.ts +27 -0
  12. package/build/src/Controller/Subscription.js +22 -0
  13. package/build/src/Controllers/BrokerController/Broker.js +145 -0
  14. package/build/src/Controllers/BrokerController/BrokerController.d.ts +61 -0
  15. package/build/src/Controllers/BrokerController/BrokerController.js +115 -0
  16. package/{src/Controllers/BrokerController/Broker.js → build/src/Controllers/BrokerController/BrokerrController.js} +74 -85
  17. package/build/src/Controllers/BrokerController/Models/PeerConnection.d.ts +7 -0
  18. package/build/src/Controllers/BrokerController/Models/PeerConnection.js +14 -0
  19. package/build/src/Controllers/BrokerController/Models/Signal.js +11 -0
  20. package/build/src/Decorators/CanInvoke.d.ts +10 -0
  21. package/build/src/Decorators/CanInvoke.js +22 -0
  22. package/build/src/Decorators/CanSet.d.ts +10 -0
  23. package/build/src/Decorators/CanSet.js +20 -0
  24. package/build/src/Decorators/ControllerProperties.d.ts +10 -0
  25. package/build/src/Decorators/ControllerProperties.js +20 -0
  26. package/{src/Controllers/BrokerController/Models/InstantMessage.js → build/src/Interfaces/IInterceptor.js} +2 -5
  27. package/build/src/Interfaces/ITransport.d.ts +94 -0
  28. package/build/src/Interfaces/ITransportMessage.d.ts +41 -0
  29. package/build/src/Messages/BufferMessage.d.ts +38 -0
  30. package/{src → build/src}/Messages/BufferMessage.js +32 -2
  31. package/build/src/Messages/ErrorMessage.d.ts +29 -0
  32. package/build/src/Messages/ErrorMessage.js +22 -0
  33. package/build/src/Messages/PipeMessage.d.ts +46 -0
  34. package/build/src/Messages/PipeMessage.js +41 -0
  35. package/build/src/Messages/TextMessage.d.ts +88 -0
  36. package/build/src/Messages/TextMessage.js +94 -0
  37. package/build/src/Messages/WebSocketMessage.d.ts +34 -0
  38. package/build/src/Messages/WebSocketMessage.js +41 -0
  39. package/build/src/Plugin.js +32 -0
  40. package/build/src/Server/Plugin.d.ts +37 -0
  41. package/build/src/Server/Plugin.js +32 -0
  42. package/build/src/Server/ThorIOServer.d.ts +68 -0
  43. package/build/src/Server/ThorIOServer.js +131 -0
  44. package/build/src/Subscription.js +22 -0
  45. package/build/src/ThorIO.js +142 -0
  46. package/build/src/ThorIOServer.d.ts +64 -0
  47. package/build/src/ThorIOServer.js +131 -0
  48. package/build/src/Transports/BufferMessageTransport.d.ts +78 -0
  49. package/build/src/Transports/BufferMessageTransport.js +85 -0
  50. package/build/src/Transports/PipeMessageTransport.d.ts +79 -0
  51. package/build/src/Transports/PipeMessageTransport.js +90 -0
  52. package/build/src/Transports/WebSocketMessageTransport.d.ts +87 -0
  53. package/build/src/Transports/WebSocketMessageTransport.js +93 -0
  54. package/build/src/Utils/BufferUtils.d.ts +32 -0
  55. package/build/src/Utils/BufferUtils.js +57 -0
  56. package/build/src/Utils/StringUtils.d.ts +15 -0
  57. package/build/src/Utils/StringUtils.js +31 -0
  58. package/build/src/test.js +5 -0
  59. package/docs/.nojekyll +1 -0
  60. package/docs/assets/hierarchy.js +1 -0
  61. package/docs/assets/highlight.css +43 -0
  62. package/docs/assets/icons.js +18 -0
  63. package/docs/assets/icons.svg +1 -0
  64. package/docs/assets/main.js +60 -0
  65. package/docs/assets/navigation.js +1 -0
  66. package/docs/assets/search.js +1 -0
  67. package/docs/assets/style.css +1610 -0
  68. package/docs/classes/BrokerController.html +164 -0
  69. package/docs/classes/BufferMessage.html +11 -0
  70. package/docs/classes/BufferMessageTransport.html +31 -0
  71. package/docs/classes/BufferUtils.html +16 -0
  72. package/docs/classes/ClientInfo.html +11 -0
  73. package/docs/classes/Connection.html +42 -0
  74. package/docs/classes/ControllerBase.html +144 -0
  75. package/docs/classes/ErrorMessage.html +13 -0
  76. package/docs/classes/PeerConnection.html +4 -0
  77. package/docs/classes/PipeMessage.html +17 -0
  78. package/docs/classes/PipeMessageTransport.html +33 -0
  79. package/docs/classes/Plugin.html +13 -0
  80. package/docs/classes/Signal.html +5 -0
  81. package/docs/classes/StringUtils.html +9 -0
  82. package/docs/classes/Subscription.html +11 -0
  83. package/docs/classes/TextMessage.html +37 -0
  84. package/docs/classes/ThorIOServer.html +25 -0
  85. package/docs/classes/WebSocketMessage.html +18 -0
  86. package/docs/classes/WebSocketMessageTransport.html +35 -0
  87. package/docs/functions/CanInvoke.html +5 -0
  88. package/docs/functions/CanSet.html +5 -0
  89. package/docs/functions/ControllerProperties.html +5 -0
  90. package/docs/hierarchy.html +1 -0
  91. package/docs/index.html +24 -0
  92. package/docs/interfaces/ITransport.html +46 -0
  93. package/docs/interfaces/ITransportMessage.html +21 -0
  94. package/docs/modules.html +1 -0
  95. package/index.ts +32 -37
  96. package/licence +21 -21
  97. package/package.json +48 -50
  98. package/readme.md +32 -1
  99. package/src/{Client → Connection}/ClientInfo.ts +34 -43
  100. package/src/Connection/Connection.ts +229 -0
  101. package/src/Connection/IClientInfo.ts +22 -0
  102. package/src/Controller/ControllerBase.ts +561 -457
  103. package/src/{Subscription.ts → Controller/Subscription.ts} +32 -34
  104. package/src/Controllers/BrokerController/BrokerController.ts +121 -0
  105. package/src/Controllers/BrokerController/Models/PeerConnection.ts +12 -34
  106. package/src/Controllers/BrokerController/Models/Signal.ts +11 -43
  107. package/src/Decorators/CanInvoke.ts +21 -13
  108. package/src/Decorators/CanSet.ts +19 -12
  109. package/src/Decorators/ControllerProperties.ts +19 -16
  110. package/src/Interfaces/ITransport.ts +107 -23
  111. package/src/Interfaces/ITransportMessage.ts +45 -41
  112. package/src/Messages/BufferMessage.ts +70 -69
  113. package/src/Messages/ErrorMessage.ts +34 -6
  114. package/src/Messages/PipeMessage.ts +56 -62
  115. package/src/Messages/TextMessage.ts +139 -93
  116. package/src/Messages/WebSocketMessage.ts +39 -41
  117. package/src/Server/Plugin.ts +56 -0
  118. package/src/Server/ThorIOServer.ts +121 -0
  119. package/src/ThorIOServer.ts +117 -0
  120. package/src/Transports/BufferMessageTransport.ts +109 -100
  121. package/src/Transports/PipeMessageTransport.ts +112 -104
  122. package/src/Transports/WebSocketMessageTransport.ts +122 -110
  123. package/src/Utils/BufferUtils.ts +55 -61
  124. package/src/Utils/StringUtils.ts +28 -17
  125. package/tsconfig.json +109 -19
  126. package/typedoc.json +6 -0
  127. package/index.d.ts +0 -24
  128. package/index.js +0 -46
  129. package/src/Client/ClientInfo.d.ts +0 -6
  130. package/src/Connection.d.ts +0 -24
  131. package/src/Connection.js +0 -121
  132. package/src/Connection.ts +0 -263
  133. package/src/Controller/ControllerBase.d.ts +0 -43
  134. package/src/Controller/ControllerBase.js +0 -331
  135. package/src/Controllers/BrokerController/Broker.d.ts +0 -21
  136. package/src/Controllers/BrokerController/Broker.ts +0 -153
  137. package/src/Controllers/BrokerController/Models/InstantMessage.d.ts +0 -3
  138. package/src/Controllers/BrokerController/Models/InstantMessage.ts +0 -15
  139. package/src/Controllers/BrokerController/Models/PeerConnection.d.ts +0 -5
  140. package/src/Controllers/BrokerController/Models/PeerConnection.js +0 -9
  141. package/src/Controllers/BrokerController/Models/Signal.js +0 -10
  142. package/src/Decorators/CanInvoke.d.ts +0 -2
  143. package/src/Decorators/CanInvoke.js +0 -9
  144. package/src/Decorators/CanSet.d.ts +0 -1
  145. package/src/Decorators/CanSet.js +0 -8
  146. package/src/Decorators/ControllerProperties.d.ts +0 -1
  147. package/src/Decorators/ControllerProperties.js +0 -10
  148. package/src/Interfaces/IInterceptor.d.ts +0 -2
  149. package/src/Interfaces/IInterceptor.ts +0 -2
  150. package/src/Interfaces/ITransport.d.ts +0 -18
  151. package/src/Interfaces/ITransportMessage.d.ts +0 -8
  152. package/src/Listener.d.ts +0 -5
  153. package/src/Listener.js +0 -9
  154. package/src/Listener.ts +0 -34
  155. package/src/Messages/BufferMessage.d.ts +0 -10
  156. package/src/Messages/ErrorMessage.d.ts +0 -4
  157. package/src/Messages/ErrorMessage.js +0 -8
  158. package/src/Messages/PipeMessage.d.ts +0 -12
  159. package/src/Messages/PipeMessage.js +0 -20
  160. package/src/Messages/TextMessage.d.ts +0 -15
  161. package/src/Messages/TextMessage.js +0 -50
  162. package/src/Messages/WebSocketMessage.d.ts +0 -10
  163. package/src/Messages/WebSocketMessage.js +0 -15
  164. package/src/Plugin.d.ts +0 -6
  165. package/src/Plugin.js +0 -11
  166. package/src/Plugin.ts +0 -37
  167. package/src/Subscription.d.ts +0 -5
  168. package/src/Subscription.js +0 -9
  169. package/src/ThorIO.d.ts +0 -18
  170. package/src/ThorIO.js +0 -65
  171. package/src/ThorIO.ts +0 -148
  172. package/src/Transports/BufferMessageTransport.d.ts +0 -21
  173. package/src/Transports/BufferMessageTransport.js +0 -31
  174. package/src/Transports/PipeMessageTransport.d.ts +0 -20
  175. package/src/Transports/PipeMessageTransport.js +0 -33
  176. package/src/Transports/WebSocketMessageTransport.d.ts +0 -20
  177. package/src/Transports/WebSocketMessageTransport.js +0 -31
  178. package/src/Utils/BufferUtils.d.ts +0 -5
  179. package/src/Utils/BufferUtils.js +0 -29
  180. package/src/Utils/StringUtils.d.ts +0 -4
  181. package/src/Utils/StringUtils.js +0 -13
  182. /package/{src/Interfaces/IInterceptor.js → build/src/Connection/IClientInfo.js} +0 -0
  183. /package/{src → build/src}/Controllers/BrokerController/Models/Signal.d.ts +0 -0
  184. /package/{src → build/src}/Interfaces/ITransport.js +0 -0
  185. /package/{src → build/src}/Interfaces/ITransportMessage.js +0 -0
package/package.json CHANGED
@@ -1,50 +1,48 @@
1
- {
2
- "name": "thor-io.vnext",
3
- "author": {
4
- "name": "Magnus Thor",
5
- "email": "magnus.thor74@gmail.com"
6
- },
7
- "contributors": [
8
- {
9
- "name": "Ricky Davies",
10
- "email": "rdavies@zen.co.uk"
11
- }
12
- ],
13
- "description": "thor-io.vnext represents the next generation of thor-io.Thor-IO is a fundament for stateful real-time communication.TCP/IP,WebSockets & WebRTC or whatever you need and desire.",
14
- "version": "2.0.5-beta.3",
15
- "license": "MIT",
16
- "main": "index.js",
17
- "typings": " typings/index",
18
- "repository": {
19
- "type": "git",
20
- "url": "https://github.com/MagnusThor/thor-io.vnext.git"
21
- },
22
- "keywords": [
23
- "unity",
24
- ".net",
25
- "realtime",
26
- "websockets",
27
- "WebRTC",
28
- "PubSub",
29
- "RPC",
30
- "Smart-RPC",
31
- "IoT",
32
- "NodeJS",
33
- "RTC",
34
- "Typescript"
35
- ],
36
- "dependencies": {
37
- "@types/node": "^10.17.26",
38
- "@types/reflect-metadata": "^0.1.0",
39
- "ws": "^7.3.0"
40
- },
41
- "devDependencies": {
42
- "@types/ws": "^7.2.5",
43
- "express": "^4.17.1",
44
- "express-ws": "^4.0.0",
45
- "thor-io.client-vnext": "^2.0.2-beta.2",
46
- "typescript": "^3.9.5",
47
- "typings": "^2.1.1",
48
- "webpack": "^4.43.0"
49
- }
50
- }
1
+ {
2
+ "name": "thor-io.vnext",
3
+ "author": {
4
+ "name": "Magnus Thor",
5
+ "email": "magnus.thor74@gmail.com"
6
+ },
7
+ "contributors": [
8
+ {
9
+ "name": "Ricky Davies",
10
+ "email": "rdavies@zen.co.uk"
11
+ }
12
+ ],
13
+ "description": "thor-io.vnext represents the next generation of thor-io.Thor-IO is a fundament for stateful real-time communication.TCP/IP,WebSockets & WebRTC or whatever you need and desire.",
14
+ "version": "3.1.0-beta.1",
15
+ "license": "MIT",
16
+ "main": "build/index.js",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/MagnusThor/thor-io.vnext.git"
20
+ },
21
+ "keywords": [
22
+ "unity",
23
+ ".net",
24
+ "realtime",
25
+ "websockets",
26
+ "WebRTC",
27
+ "PubSub",
28
+ "RPC",
29
+ "Smart-RPC",
30
+ "IoT",
31
+ "NodeJS",
32
+ "RTC",
33
+ "Typescript"
34
+ ],
35
+ "dependencies": {
36
+ "@types/node": "^10.17.26",
37
+ "@types/reflect-metadata": "^0.1.0",
38
+ "ws": "^7.3.0"
39
+ },
40
+ "devDependencies": {
41
+ "@types/ws": "^7.2.5",
42
+ "express": "^4.17.1",
43
+ "express-ws": "^4.0.0",
44
+ "typedoc": "^0.27.6",
45
+ "typings": "^0.6.10",
46
+ "webpack": "^5.97.1"
47
+ }
48
+ }
package/readme.md CHANGED
@@ -1 +1,32 @@
1
- See https://github.com/MagnusThor/thor-io.vnext/wiki for a brief documnentation.
1
+ **The Foundation for Next-Gen, Stateful Real-Time Communication**
2
+
3
+ thor-io.vnext empowers you to build cutting-edge, real-time applications with unparalleled flexibility and performance.
4
+
5
+ **Key Features:**
6
+
7
+ * **Rock-Solid Foundation:** Built for demanding applications, thor-io.vnext provides a robust framework for stateful real-time communication.
8
+ * **Unleash Your Creativity:** Seamlessly integrate with your preferred transport layer: TCP/IP, WebSockets, WebRTC, or any custom solution you envision.
9
+ * **Effortless Integration:** Easily plug into your existing Node.js ecosystem – works flawlessly with Express and other popular middleware.
10
+ * **WebRTC Signaling Made Easy:** Leverage built-in WebRTC signaling capabilities to simplify the development of peer-to-peer applications.
11
+
12
+ **What can you build with thor-io.vnext?**
13
+
14
+ * **Real-time Collaboration:** Power collaborative tools like shared editors, whiteboards, and online design platforms.
15
+ * **Immersive Experiences:** Create captivating online games, interactive simulations, and virtual/augmented reality applications.
16
+ * **High-Performance Applications:** Build robust applications that demand real-time data synchronization, such as financial trading platforms and IoT solutions.
17
+ * **And Much More!** The possibilities are limited only by your imagination.
18
+
19
+ **Getting Started:**
20
+
21
+ **Install**
22
+ ```bash
23
+ npm install thor-io.vnext
24
+ ```
25
+
26
+ **Documentation**
27
+
28
+ https://../doc
29
+
30
+
31
+
32
+ /Magnus Thor
@@ -1,43 +1,34 @@
1
- // todo: refactor this, implememt PI for a sticky session?
2
- /**
3
- *
4
- *
5
- * @export
6
- * @class ClientInfo
7
- */
8
- export class ClientInfo {
9
- /**
10
- *
11
- *
12
- * @type {string}
13
- * @memberOf ClientInfo
14
- */
15
- public CI: string;
16
- /**
17
- *
18
- *
19
- * @type {string}
20
- * @memberOf ClientInfo
21
- */
22
- public C: string;
23
- /**
24
- *
25
- *
26
- * @type {Date}
27
- * @memberOf ClientInfo
28
- */
29
- public TS: Date;
30
- /**
31
- * Creates an instance of ClientInfo.
32
- *
33
- * @param {string} ci
34
- * @param {string} controller
35
- *
36
- * @memberOf ClientInfo
37
- */
38
- constructor(ci: string, controller: string) {
39
- this.CI = ci;
40
- this.C = controller;
41
- this.TS = new Date();
42
- }
43
- }
1
+ export class ClientInfo {
2
+ /**
3
+ * The client identifier.
4
+ *
5
+ * @type {string}
6
+ */
7
+ public CI: string;
8
+
9
+ /**
10
+ * The controller identifier.
11
+ *
12
+ * @type {string}
13
+ */
14
+ public C: string;
15
+
16
+ /**
17
+ * The timestamp when the instance was created.
18
+ *
19
+ * @type {Date}
20
+ */
21
+ public TS: Date;
22
+
23
+ /**
24
+ * Creates an instance of ClientInfo.
25
+ *
26
+ * @param {string} ci - The client identifier.
27
+ * @param {string} controller - The controller identifier.
28
+ */
29
+ constructor(ci: string, controller: string) {
30
+ this.CI = ci;
31
+ this.C = controller;
32
+ this.TS = new Date();
33
+ }
34
+ }
@@ -0,0 +1,229 @@
1
+ import { ControllerBase } from '../Controller/ControllerBase';
2
+ import { ITransport } from '../Interfaces/ITransport';
3
+ import { ITransportMessage } from '../Interfaces/ITransportMessage';
4
+ import { TextMessage } from '../Messages/TextMessage';
5
+ import { Plugin } from '../Server/Plugin';
6
+ import { ClientInfo } from './ClientInfo';
7
+
8
+ export class Connection {
9
+
10
+ /**
11
+ * An array to store errors.
12
+ * @public
13
+ * @type {Array<any>}
14
+ */
15
+ public errors: Array<any> = [];
16
+
17
+ /**
18
+ * The ping pong interval.
19
+ * @public
20
+ * @type {number}
21
+ */
22
+ public pingPongInterval: number = 60;
23
+
24
+ /**
25
+ * A map to store the controller instances.
26
+ * @public
27
+ * @type {Map<string, ControllerBase>}
28
+ */
29
+ public controllerInstances: Map<string, ControllerBase> = new Map();
30
+
31
+ /**
32
+ * Client information.
33
+ * @public
34
+ * @type {ClientInfo | undefined}
35
+ */
36
+ public clientInfo: ClientInfo | undefined;
37
+
38
+ /**
39
+ * Tries to invoke a method or set/get a property on the controller.
40
+ * @private
41
+ * @param {ControllerBase} controller The controller instance.
42
+ * @param {string} methodOrProperty The method or property name.
43
+ * @param {string} data The data to be passed to the method or property.
44
+ * @param {Buffer} [buffer] An optional buffer.
45
+ */
46
+ private tryInvokeMethod(
47
+ controller: ControllerBase,
48
+ methodOrProperty: string,
49
+ data: string,
50
+ buffer?: Buffer
51
+ ) {
52
+ if (controller.canInvokeMethod(methodOrProperty)) {
53
+ controller.invokeMethod(methodOrProperty, data, buffer);
54
+ } else if (controller.canSetProperty(methodOrProperty)) {
55
+ controller.setProperty(methodOrProperty, JSON.parse(data));
56
+ } else if (controller.canGetProperty(methodOrProperty)) {
57
+ const { resultId } = JSON.parse(data) as { resultId: string };
58
+ controller.getProperty(methodOrProperty, resultId);
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Getter for the connection ID.
64
+ * @public
65
+ * @returns {string} The connection ID.
66
+ */
67
+ get id(): string {
68
+ return this.transport.id;
69
+ }
70
+
71
+ /**
72
+ * Constructor for the Connection class.
73
+ * @param {ITransport} transport The transport instance.
74
+ * @param {Map<string, Connection>} connections A map of connections.
75
+ * @param {Map<string, Plugin<ControllerBase>>} controllers A map of controllers.
76
+ * @constructor
77
+ */
78
+ constructor(
79
+ public transport: ITransport,
80
+ public connections: Map<string, Connection>,
81
+ private controllers: Map<string, Plugin<ControllerBase>>
82
+ ) {
83
+ try {
84
+ this.setupTransport(transport);
85
+
86
+ } catch (error) {
87
+ console.log('Error in Connection constructor', error);
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Sets up the transport event listeners.
93
+ * @private
94
+ * @param {ITransport} transport The transport instance.
95
+ */
96
+ private setupTransport(transport: ITransport) {
97
+ if (transport) {
98
+ this.transport.onMessage = (event: ITransportMessage) => {
99
+ try {
100
+ const message = event.isBinary ? TextMessage.fromArrayBuffer(event.data) : event.toMessage();
101
+ const controller = this.tryCreateControllerInstance(message.C);
102
+ if (controller) {
103
+ this.tryInvokeMethod(controller, message.T, message.D, message.B);
104
+ }
105
+ } catch (error) {
106
+ this.addError(error);
107
+ }
108
+ };
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Adds an error to the errors array.
114
+ * @private
115
+ * @param {any} error The error to be added.
116
+ */
117
+ private addError(error: any) {
118
+ this.errors.push(error);
119
+ }
120
+
121
+ /**
122
+ * Checks if a controller with the given alias exists.
123
+ * @public
124
+ * @param {string} alias The controller alias.
125
+ * @returns {boolean} True if the controller exists, false otherwise.
126
+ */
127
+ hasController(alias: string): boolean {
128
+ return this.controllerInstances.has(alias);
129
+ }
130
+
131
+ /**
132
+ * Removes a controller from the controllerInstances.
133
+ * @public
134
+ * @param {string} alias The alias of the controller to be removed.
135
+ * @returns {boolean} True if the controller was removed successfully, false otherwise.
136
+ */
137
+ tryRemoveControllerInstance(alias: string): boolean {
138
+ return this.controllerInstances.delete(alias);
139
+ }
140
+
141
+ /**
142
+ * Gets a controller instance from the controllerInstances map.
143
+ * @public
144
+ * @param {string} alias The alias of the controller to be retrieved.
145
+ * @returns {ControllerBase | undefined} The controller instance if found, otherwise undefined.
146
+ */
147
+ tryGetController(alias: string): ControllerBase | undefined {
148
+ try {
149
+ const match = this.controllerInstances.get(alias);
150
+ if (!match) throw new Error(`Cannot locate the requested controller ${alias}`);
151
+ return match;
152
+ } catch (error) {
153
+ this.addError(error);
154
+ return undefined;
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Adds a controller instance to the controllerInstances map.
160
+ * @private
161
+ * @param {ControllerBase} controller The controller instance to be added.
162
+ * @returns {ControllerBase} The added controller instance.
163
+ */
164
+ private addControllerInstance(controller: ControllerBase): ControllerBase {
165
+ if (!controller.alias) throw `Cannot add Controller instance`;
166
+ this.controllerInstances.set(controller.alias, controller);
167
+ return controller;
168
+ }
169
+
170
+ /**
171
+ * Finds and resolves a controller by alias.
172
+ * @public
173
+ * @param {string} alias The alias of the controller to be resolved.
174
+ * @returns {ControllerBase} The resolved controller instance.
175
+ * @throws {Error} If the controller cannot be resolved.
176
+ */
177
+ public tryResolveController(alias: string): ControllerBase {
178
+ const plugin = this.controllers.get(alias);
179
+ if (!plugin) {
180
+ throw new Error(`Cannot resolve ${alias}, controller unknown.`);
181
+ }
182
+ return plugin.getInstance();
183
+ }
184
+
185
+ /**
186
+ * Locates a controller by alias. If registered, returns it; otherwise, creates an instance.
187
+ * @public
188
+ * @param {string} alias The alias of the controller to be located.
189
+ * @returns {ControllerBase | undefined} The located controller instance, or undefined if not found.
190
+ */
191
+ tryCreateControllerInstance(alias: string): ControllerBase | undefined {
192
+ try {
193
+ let instancedController = this.tryGetController(alias);
194
+ if (instancedController) {
195
+ return instancedController;
196
+ } else {
197
+ const resolvedController = this.tryResolveController(alias);
198
+ const controllerInstance = new resolvedController(this);
199
+ this.addControllerInstance(controllerInstance);
200
+ this.initializeControllerInstance(controllerInstance);
201
+ return controllerInstance;
202
+ }
203
+ } catch (error) {
204
+
205
+ this.transport.close(
206
+ 1011,
207
+ `Cannot locate the specified controller, it may be sealed or the alias is unknown '${alias}'. Connection closed.`
208
+ );
209
+ return undefined;
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Initializes a newly created controller instance.
215
+ * @private
216
+ * @param {ControllerBase} controllerInstance The controller instance to initialize.
217
+ */
218
+ private initializeControllerInstance(controllerInstance: ControllerBase) {
219
+ controllerInstance.invoke(
220
+ new ClientInfo(this.id, controllerInstance.alias!),
221
+ '___open',
222
+ controllerInstance.alias!
223
+ );
224
+ if (controllerInstance.onopen) controllerInstance.onopen();
225
+ this.transport.onClose = (e: any) => {
226
+ if (controllerInstance.onclose) controllerInstance.onclose();
227
+ };
228
+ }
229
+ }
@@ -0,0 +1,22 @@
1
+ export interface IClientInfo {
2
+ /**
3
+ * The client identifier.
4
+ *
5
+ * @type {string}
6
+ */
7
+ CI: string;
8
+
9
+ /**
10
+ * The controller identifier.
11
+ *
12
+ * @type {string}
13
+ */
14
+ C: string;
15
+
16
+ /**
17
+ * The timestamp when the instance was created.
18
+ *
19
+ * @type {Date}
20
+ */
21
+ TS: Date;
22
+ }