thor-io.vnext 2.0.6 → 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 (178) hide show
  1. package/.gitattributes +17 -17
  2. package/{index.d.ts → build/index.d.ts} +11 -8
  3. package/build/index.js +47 -0
  4. package/build/src/Connection/ClientInfo.d.ts +27 -0
  5. package/{src → 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/build/src/Interfaces/IInterceptor.js +2 -0
  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 -35
  96. package/licence +21 -21
  97. package/package.json +48 -49
  98. package/readme.md +32 -71
  99. package/src/Connection/ClientInfo.ts +34 -37
  100. package/src/Connection/Connection.ts +229 -278
  101. package/src/Connection/IClientInfo.ts +22 -0
  102. package/src/Controller/ControllerBase.ts +561 -458
  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 -18
  126. package/typedoc.json +6 -0
  127. package/index.js +0 -40
  128. package/src/Connection/ClientInfo.d.ts +0 -6
  129. package/src/Connection/Connection.d.ts +0 -24
  130. package/src/Connection/Connection.js +0 -129
  131. package/src/Controller/ControllerBase.d.ts +0 -43
  132. package/src/Controller/ControllerBase.js +0 -331
  133. package/src/Controllers/BrokerController/Broker.d.ts +0 -21
  134. package/src/Controllers/BrokerController/Broker.ts +0 -153
  135. package/src/Controllers/BrokerController/Models/PeerConnection.d.ts +0 -5
  136. package/src/Controllers/BrokerController/Models/PeerConnection.js +0 -9
  137. package/src/Controllers/BrokerController/Models/Signal.js +0 -10
  138. package/src/Decorators/CanInvoke.d.ts +0 -2
  139. package/src/Decorators/CanInvoke.js +0 -9
  140. package/src/Decorators/CanSet.d.ts +0 -1
  141. package/src/Decorators/CanSet.js +0 -8
  142. package/src/Decorators/ControllerProperties.d.ts +0 -1
  143. package/src/Decorators/ControllerProperties.js +0 -10
  144. package/src/Interfaces/IInterceptor.d.ts +0 -2
  145. package/src/Interfaces/IInterceptor.ts +0 -2
  146. package/src/Interfaces/ITransport.d.ts +0 -18
  147. package/src/Interfaces/ITransportMessage.d.ts +0 -8
  148. package/src/Messages/BufferMessage.d.ts +0 -10
  149. package/src/Messages/ErrorMessage.d.ts +0 -4
  150. package/src/Messages/ErrorMessage.js +0 -8
  151. package/src/Messages/PipeMessage.d.ts +0 -12
  152. package/src/Messages/PipeMessage.js +0 -20
  153. package/src/Messages/TextMessage.d.ts +0 -15
  154. package/src/Messages/TextMessage.js +0 -50
  155. package/src/Messages/WebSocketMessage.d.ts +0 -10
  156. package/src/Messages/WebSocketMessage.js +0 -15
  157. package/src/Plugin.d.ts +0 -6
  158. package/src/Plugin.js +0 -11
  159. package/src/Plugin.ts +0 -37
  160. package/src/Subscription.d.ts +0 -5
  161. package/src/Subscription.js +0 -9
  162. package/src/ThorIO.d.ts +0 -19
  163. package/src/ThorIO.js +0 -67
  164. package/src/ThorIO.ts +0 -153
  165. package/src/Transports/BufferMessageTransport.d.ts +0 -21
  166. package/src/Transports/BufferMessageTransport.js +0 -31
  167. package/src/Transports/PipeMessageTransport.d.ts +0 -20
  168. package/src/Transports/PipeMessageTransport.js +0 -33
  169. package/src/Transports/WebSocketMessageTransport.d.ts +0 -20
  170. package/src/Transports/WebSocketMessageTransport.js +0 -31
  171. package/src/Utils/BufferUtils.d.ts +0 -5
  172. package/src/Utils/BufferUtils.js +0 -29
  173. package/src/Utils/StringUtils.d.ts +0 -4
  174. package/src/Utils/StringUtils.js +0 -13
  175. /package/{src/Interfaces/IInterceptor.js → build/src/Connection/IClientInfo.js} +0 -0
  176. /package/{src → build/src}/Controllers/BrokerController/Models/Signal.d.ts +0 -0
  177. /package/{src → build/src}/Interfaces/ITransport.js +0 -0
  178. /package/{src → build/src}/Interfaces/ITransportMessage.js +0 -0
@@ -1,278 +1,229 @@
1
- import { Plugin } from "../Plugin";
2
- import { TextMessage } from "../Messages/TextMessage";
3
- import { ITransport } from "../Interfaces/ITransport";
4
- import { ITransportMessage } from "../Interfaces/ITransportMessage";
5
- import { ControllerBase } from "../Controller/ControllerBase";
6
- import { ClientInfo } from "./ClientInfo";
7
-
8
- /**
9
- *
10
- *
11
- * @export
12
- * @class Connection
13
- */
14
- export class Connection {
15
- /**
16
- *
17
- *
18
- * @type {Array<any>}
19
- * @memberOf Connection
20
- */
21
- public errors: Array<any>;
22
- /**
23
- *
24
- *
25
- * @type {number}
26
- * @memberOf Connection
27
- */
28
- public pingPongInterval: number;
29
- /**
30
- *
31
- *
32
- * @type {Array<Controller>}
33
- * @memberOf Connection
34
- */
35
- //public controllerInstances: Array<ControllerBase>;
36
- public controllerInstances: Map<string, ControllerBase>;
37
- /**
38
- *
39
- *
40
- * @type {ClientInfo}
41
- * @memberOf Connection
42
- */
43
- public clientInfo: ClientInfo;
44
- /**
45
- *
46
- *
47
- * @private
48
- * @param {ControllerBase} controller
49
- * @param {string} method
50
- * @param {string} data
51
- * @param {*} [buffer]
52
- *
53
- * @memberOf Connection
54
- */
55
- private methodInvoker(
56
- controller: ControllerBase,
57
- method: string,
58
- data: string,
59
- buffer?: any
60
- ) {
61
- try {
62
- if (!controller.canInvokeMethod(method))
63
- throw "method '" + method + "' cant be invoked.";
64
- if (typeof controller[method] === "function") {
65
- controller[method].apply(controller, [
66
- JSON.parse(data),
67
- method,
68
- controller.alias,
69
- buffer,
70
- ]);
71
- } else {
72
- let prop = method;
73
- let propValue = JSON.parse(data);
74
- if (typeof controller[prop] === typeof propValue)
75
- controller[prop] = propValue;
76
- }
77
- } catch (ex) {
78
- controller.invokeError(ex);
79
- }
80
- }
81
- /**
82
- *
83
- *
84
- * @readonly
85
- * @type {string}
86
- * @memberOf Connection
87
- */
88
- get id(): string {
89
- return this.transport.id;
90
- }
91
- /**
92
- * Creates an instance of Connection.
93
- *
94
- * @param {ITransport} transport
95
- * @param {Array<Connection>} connections
96
- * @param {Array<Plugin<ControllerBase>>} controllers
97
- *
98
- * @memberOf Connection
99
- */
100
- constructor(
101
- public transport: ITransport,
102
- public connections: Map<string, Connection>,
103
- private controllers: Array<Plugin<ControllerBase>>
104
- ) {
105
- this.connections = connections;
106
- //this.controllerInstances = new Array<ControllerBase>();
107
- this.controllerInstances = new Map<string, ControllerBase>();
108
- this.errors = new Array<any>();
109
- if (transport) {
110
- /**
111
- *
112
- *
113
- * @param {ITransportMessage} event
114
- */
115
- this.transport.onMessage = (event: ITransportMessage) => {
116
- try {
117
- if (!event.binary) {
118
- let message = event.toMessage();
119
- let controller = this.locateController(message.C);
120
- if (controller)
121
- this.methodInvoker(controller, message.T, message.D);
122
- } else {
123
- let message = TextMessage.fromArrayBuffer(event.data);
124
- let controller = this.locateController(message.C);
125
- if (controller)
126
- this.methodInvoker(controller, message.T, message.D, message.B);
127
- }
128
- } catch (error) {
129
- this.addError(error);
130
- }
131
- };
132
- }
133
- }
134
- /**
135
- *
136
- *
137
- * @private
138
- * @param {*} error
139
- *
140
- * @memberOf Connection
141
- */
142
- private addError(error: any) {
143
- this.errors.push(error);
144
- }
145
- /**
146
- *
147
- *
148
- * @param {string} alias
149
- * @returns {boolean}
150
- *
151
- * @memberOf Connection
152
- */
153
- hasController(alias: string): boolean {
154
- /**
155
- *
156
- *
157
- * @param {ControllerBase} pre
158
- * @returns
159
- */
160
- // let match = this.controllerInstances.filter((pre: ControllerBase) => {
161
- // return pre.alias == alias;
162
- // });
163
- // return match.length >= 0;
164
- return this.controllerInstances.has(alias);
165
- }
166
- /**
167
- *
168
- *
169
- * @param {string} alias
170
- *
171
- * @memberOf Connection
172
- */
173
- removeController(alias: string): boolean {
174
- // let index = this.controllerInstances.indexOf(this.getController(alias));
175
- // if (index > -1)
176
- // this.controllerInstances.splice(index, 1);
177
- return this.controllerInstances.delete(alias);
178
- }
179
- /**
180
- *
181
- *
182
- * @param {string} alias
183
- * @returns {ControllerBase}
184
- *
185
- * @memberOf Connection
186
- */
187
- getController(alias: string): ControllerBase {
188
- try {
189
- let match = this.controllerInstances.get(alias);
190
- if (!match) throw `cannot locate the requested controller ${alias}`;
191
- return match;
192
- } catch (error) {
193
- this.addError(error);
194
- return;
195
- }
196
- }
197
- /**
198
- *
199
- *
200
- * @private
201
- * @param {ControllerBase} controller
202
- * @returns {ControllerBase}
203
- *
204
- * @memberOf Connection
205
- */
206
- private addControllerInstance(controller: ControllerBase): ControllerBase {
207
- this.controllerInstances.set(controller.alias, controller);
208
- return controller;
209
- }
210
- /**
211
- *
212
- *
213
- * @private
214
- *
215
- * @memberOf Connection
216
- */
217
- private registerSealdController() {
218
- throw "not yet implemented";
219
- }
220
-
221
- public resolveController(alias: string): Plugin<ControllerBase> {
222
- try {
223
- let resolvedController = this.controllers.find(
224
- (resolve: Plugin<ControllerBase>) => {
225
- return (
226
- resolve.alias === alias &&
227
- Reflect.getMetadata("seald", resolve.instance) === false
228
- );
229
- }
230
- );
231
- return resolvedController;
232
- } catch {
233
- throw `Cannot resolve ${alias},controller unknown.`;
234
- }
235
- }
236
- /**
237
- *
238
- *
239
- * @param {string} alias
240
- * @returns {ControllerBase}
241
- *
242
- * @memberOf Connection
243
- */
244
- locateController(alias: string): ControllerBase {
245
- try {
246
- let match = this.getController(alias);
247
- if (match) {
248
- return match;
249
- } else {
250
- // let resolvedController = this.controllers.find((resolve: Plugin<ControllerBase>) => {
251
- // return resolve.alias === alias && Reflect.getMetadata("seald", resolve.instance) === false;
252
- // }).instance;
253
- let resolved = this.resolveController(alias);
254
- let controllerInstance = new resolved.instance(this);
255
- this.addControllerInstance(controllerInstance);
256
- controllerInstance.invoke(
257
- new ClientInfo(this.id, controllerInstance.alias),
258
- "___open",
259
- controllerInstance.alias
260
- );
261
- if (controllerInstance.onopen) controllerInstance.onopen();
262
- this.transport.onClose = (e: any) => {
263
- if (controllerInstance.onclose) controllerInstance.onclose();
264
- };
265
-
266
- return controllerInstance;
267
- }
268
- } catch (error) {
269
- this.transport.close(
270
- 1011,
271
- "Cannot locate the specified controller,it may be seald or the the alias in unknown '" +
272
- alias +
273
- "'. connection closed"
274
- );
275
- return null;
276
- }
277
- }
278
- }
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
+ }