thor-io.vnext 2.0.6 → 3.1.0-beta.2

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
@@ -0,0 +1,543 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.ControllerBase = void 0;
10
+ const url_1 = require("url");
11
+ const CanInvoke_1 = require("../Decorators/CanInvoke");
12
+ const CanSet_1 = require("../Decorators/CanSet");
13
+ const ErrorMessage_1 = require("../Messages/ErrorMessage");
14
+ const TextMessage_1 = require("../Messages/TextMessage");
15
+ const Subscription_1 = require("./Subscription");
16
+ /**
17
+ * ControllerBase
18
+ *
19
+ * Base class for all controllers.
20
+ */
21
+ class ControllerBase {
22
+ /**
23
+ * Invokes a method on the controller.
24
+ * @param {string} method The name of the method to invoke.
25
+ * @param {any} data The data to pass to the method.
26
+ * @param {Buffer} [buffer] Optional buffer data.
27
+ */
28
+ invokeMethod(method, data, buffer) {
29
+ const controller = this;
30
+ const json = JSON.parse(data);
31
+ if (typeof controller[method] === "function") {
32
+ controller[method].apply(controller, [
33
+ json,
34
+ method,
35
+ controller.alias,
36
+ buffer,
37
+ ]);
38
+ }
39
+ }
40
+ /**
41
+ * Sets a property on the controller.
42
+ * @param {string} propertyName The name of the property to set.
43
+ * @param {any} propertyValue The value to set the property to.
44
+ */
45
+ setProperty(proprtyName, propertyValue) {
46
+ const controller = this;
47
+ if (typeof controller[proprtyName] === typeof propertyValue)
48
+ controller[proprtyName] = propertyValue;
49
+ }
50
+ /**
51
+ * Gets a property from the controller.
52
+ * @param {string} propertyName The name of the property to get.
53
+ * @param {string} topic The topic to use for the response.
54
+ */
55
+ getProperty(proprtyName, topic) {
56
+ const propertyValue = Reflect.getOwnMetadata(proprtyName, this);
57
+ this.invoke(propertyValue, topic);
58
+ }
59
+ /**
60
+ * Gets the name of the controller.
61
+ * @returns {string} The alias of the controller.
62
+ */
63
+ getName() {
64
+ return this.alias;
65
+ }
66
+ /**
67
+ * Creates a new instance of ControllerBase.
68
+ *
69
+ * @param {Connection} connection The connection associated with the controller.
70
+ */
71
+ constructor(connection) {
72
+ this.connection = connection;
73
+ this.subscriptions = [];
74
+ this.alias = Reflect.getMetadata("alias", this.constructor);
75
+ this.heartbeatInterval = Reflect.getMetadata("heartbeatInterval", this.constructor);
76
+ if (this.heartbeatInterval >= 1000)
77
+ this.enableHeartbeat();
78
+ }
79
+ /**
80
+ * Enables the heartbeat mechanism.
81
+ */
82
+ enableHeartbeat() {
83
+ this.connection.transport.addEventListener("pong", () => {
84
+ this.lastPong = new Date();
85
+ });
86
+ this.interval = setInterval(() => {
87
+ this.lastPing = new Date();
88
+ if (this.connection.transport.readyState === 1)
89
+ this.connection.transport.ping();
90
+ }, this.heartbeatInterval);
91
+ }
92
+ /**
93
+ * Disables the heartbeat mechanism.
94
+ */
95
+ disbaleHeartbeat() {
96
+ clearInterval(this.interval);
97
+ }
98
+ /**
99
+ * Checks if a specific method can be invoked on the controller.
100
+ *
101
+ * @param {string} method The name of the method.
102
+ * @returns {boolean} True if the method can be invoked, false otherwise.
103
+ */
104
+ canInvokeMethod(method) {
105
+ return Reflect.getMetadata("canInvoke", this, method);
106
+ }
107
+ /**
108
+ * Checks if a specific property can be set on the controller.
109
+ *
110
+ * @param {string} property The name of the property.
111
+ * @returns {boolean} True if the property can be set, false otherwise.
112
+ */
113
+ canSetProperty(property) {
114
+ return Reflect.getMetadata("canSet", this, property);
115
+ }
116
+ /**
117
+ * Checks if a specific property can be gotten from the controller.
118
+ *
119
+ * @param {string} property The name of the property.
120
+ * @returns {boolean} True if the property can be gotten, false otherwise.
121
+ */
122
+ canGetProperty(property) {
123
+ return Reflect.getMetadata("canGet", this, property);
124
+ }
125
+ /**
126
+ * Finds controllers on other connections that match the given predicate.
127
+ *
128
+ * @template T
129
+ * @param {string} alias The alias of the controller to find.
130
+ * @param {Function} predicate The predicate function to filter controllers.
131
+ * @returns {ControllerBase[] | undefined} An array of controllers that match the predicate, or undefined if no controllers were found.
132
+ */
133
+ findOn(alias, predicate) {
134
+ let connections = this.getConnections(alias).map((p) => {
135
+ return p.tryGetController(alias);
136
+ });
137
+ const result = connections.filter(predicate).filter((c) => c !== undefined);
138
+ return result.length > 0 ? result : undefined;
139
+ }
140
+ /**
141
+ * Gets a list of connections.
142
+ *
143
+ * @param {string} alias Optional alias to filter connections by.
144
+ * @returns {Connection[]} An array of connections.
145
+ */
146
+ getConnections(alias) {
147
+ if (!alias) {
148
+ return Array.from(this.connection.connections.values());
149
+ }
150
+ else {
151
+ return Array.from(this.connection.connections.values())
152
+ .map((conn) => {
153
+ if (conn.hasController(this.alias)) {
154
+ return conn;
155
+ }
156
+ })
157
+ .filter((conn) => conn !== undefined);
158
+ }
159
+ }
160
+ /**
161
+ * Called when the connection is opened.
162
+ */
163
+ onopen() { }
164
+ /**
165
+ * Called when the connection is closed.
166
+ */
167
+ onclose() { }
168
+ /**
169
+ * Finds elements in an array that match a predicate and applies a selector function.
170
+ *
171
+ * @template T
172
+ * @template U
173
+ * @param {T[]} array The array to search.
174
+ * @param {Function} predicate The predicate function to filter elements.
175
+ * @param {Function} selector The selector function to apply to the filtered elements.
176
+ * @returns {U[]} An array of selected elements.
177
+ */
178
+ find(array, predicate, selector = (x) => x) {
179
+ return array.filter(predicate).map(selector);
180
+ }
181
+ /**
182
+ * Invokes an error message.
183
+ *
184
+ * @param {any} ex The error object.
185
+ */
186
+ invokeError(ex) {
187
+ let errorMessage = new ErrorMessage_1.ErrorMessage(ex.message);
188
+ this.invoke(errorMessage, "___error", this.alias);
189
+ }
190
+ /**
191
+ * Invokes a method on controllers on other connections.
192
+ *
193
+ * @param {any} data The data to send.
194
+ * @param {string} topic The topic of the message.
195
+ * @param {string} [controller] The optional controller alias.
196
+ * @param {any} [buffer] Optional buffer data.
197
+ */
198
+ invokeToOthers(data, topic, controller, buffer) {
199
+ this.getConnections().filter((pre) => pre.id !== this.connection.id)
200
+ .forEach((connection) => {
201
+ const targetController = connection.tryGetController(controller || this.alias);
202
+ if (targetController) {
203
+ targetController.invoke(data, topic, controller || this.alias, buffer);
204
+ }
205
+ else {
206
+ // Handle the case where the controller is not found for this connection
207
+ console.warn(`Controller '${controller || this.alias}' not found on connection '${connection.id}'`);
208
+ }
209
+ });
210
+ }
211
+ /**
212
+ * Invokes a method on controllers on all connections.
213
+ *
214
+ * @param {any} data The data to send.
215
+ * @param {string} topic The topic of the message.
216
+ * @param {string} [controller] The optional controller alias.
217
+ * @param {any} [buffer] Optional buffer data.
218
+ */
219
+ invokeToAll(data, topic, controller, buffer) {
220
+ this.getConnections().forEach((connection) => {
221
+ const targetController = connection.tryGetController(controller || this.alias);
222
+ if (targetController) {
223
+ targetController.invoke(data, topic, controller || this.alias, buffer);
224
+ }
225
+ });
226
+ }
227
+ /**
228
+ * Invokes a method on controllers that match the given predicate.
229
+ *
230
+ * @param {Function} predicate The predicate function to filter controllers.
231
+ * @param {any} data The data to send.
232
+ * @param {string} topic The topic of the message.
233
+ * @param {string} [controller] The optional controller alias.
234
+ * @param {any} [buffer] Optional buffer data.
235
+ */
236
+ invokeTo(predicate, data, topic, controller, buffer) {
237
+ const connections = this.findOn(controller || this.alias, predicate);
238
+ if (connections) {
239
+ connections.forEach((ctrl) => {
240
+ ctrl.invoke(data, topic, controller || this.alias, buffer);
241
+ });
242
+ }
243
+ }
244
+ /**
245
+ * Invokes a method on the current connection.
246
+ *
247
+ * @param {any} data The data to send.
248
+ * @param {string} topic The topic of the message.
249
+ * @param {string} [controller] The optional controller alias.
250
+ * @param {Buffer} [buffer] Optional buffer data.
251
+ * @returns {ControllerBase} The current controller instance.
252
+ */
253
+ invoke(data, topic, controller, buffer) {
254
+ let msg = new TextMessage_1.TextMessage(topic, data, controller || this.alias, buffer);
255
+ if (this.connection.transport)
256
+ this.connection.transport.send(!msg.isBinary ? msg.toString() : msg.toArrayBuffer());
257
+ return this;
258
+ }
259
+ /**
260
+ * Publishes a message to subscribers.
261
+ *
262
+ * @param {any} data The data to send.
263
+ * @param {string} topic The topic of the message.
264
+ * @param {string} [controller] The optional controller alias.
265
+ * @returns {ControllerBase} The current controller instance.
266
+ */
267
+ publish(data, topic, controller) {
268
+ if (!this.hasSubscription(topic))
269
+ return this;
270
+ return this.invoke(data, topic, controller || this.alias);
271
+ }
272
+ /**
273
+ * Publishes a message to all connections.
274
+ *
275
+ * @param {any} data The data to send.
276
+ * @param {string} topic The topic of the message.
277
+ * @param {string} [controller] The optional controller alias.
278
+ */
279
+ publishToAll(data, topic, controller) {
280
+ let msg = new TextMessage_1.TextMessage(topic, data, this.alias);
281
+ this.getConnections().forEach((connection) => {
282
+ let ctrl = connection.tryGetController(controller || this.alias);
283
+ if (ctrl) {
284
+ if (ctrl.getSubscription(topic)) {
285
+ connection.transport.send(msg.toString());
286
+ }
287
+ }
288
+ else {
289
+ // Handle the case where the controller is not found for this connection
290
+ console.warn(`Controller '${controller || this.alias}' not found on connection '${connection.id}'`);
291
+ }
292
+ });
293
+ }
294
+ /**
295
+ * Checks if the controller has a subscription for the given topic.
296
+ *
297
+ * @param {string} topic The topic to check.
298
+ * @returns {boolean} True if the controller has a subscription for the topic, false otherwise.
299
+ */
300
+ hasSubscription(topic) {
301
+ /**
302
+ *
303
+ *
304
+ * @param {Subscription} pre
305
+ * @returns
306
+ */
307
+ let p = this.subscriptions.filter((pre) => {
308
+ return pre.topic === topic;
309
+ });
310
+ return !(p.length === 0);
311
+ }
312
+ /**
313
+ * Adds a subscription for the given topic.
314
+ *
315
+ * @param {string} topic The topic to subscribe to.
316
+ * @returns {Subscription} The created subscription.
317
+ */
318
+ addSubscription(topic) {
319
+ let subscription = new Subscription_1.Subscription(topic, this.alias);
320
+ return this.___subscribe(subscription, topic, this.alias);
321
+ }
322
+ /**
323
+ * Removes a subscription for the given topic.
324
+ *
325
+ * @param {string} topic The topic to unsubscribe from.
326
+ */
327
+ removeSubscription(topic) {
328
+ const subscription = this.getSubscription(topic);
329
+ if (!subscription)
330
+ return;
331
+ return this.___unsubscribe(subscription);
332
+ }
333
+ /**
334
+ * Gets the subscription for the given topic.
335
+ *
336
+ * @param {string} topic The topic to find the subscription for.
337
+ * @returns {Subscription | undefined} The subscription for the given topic, or undefined if no subscription is found.
338
+ */
339
+ getSubscription(topic) {
340
+ let subscription = this.subscriptions.find((pre) => {
341
+ return pre.topic === topic;
342
+ });
343
+ return subscription;
344
+ }
345
+ /**
346
+ * Connects the controller.
347
+ *
348
+ * **Note:** This method is marked as @CanInvoke(true), but its purpose is unclear.
349
+ * Consider reviewing and potentially removing this method.
350
+ */
351
+ ___connect() {
352
+ // todo: remove this method
353
+ }
354
+ /**
355
+ * Closes the connection associated with the controller.
356
+ */
357
+ ___close() {
358
+ this.connection.tryRemoveControllerInstance(this.alias);
359
+ this.invoke({}, " ___close", this.alias);
360
+ }
361
+ /**
362
+ * Subscribes the controller to a specific topic.
363
+ *
364
+ * @param {Subscription} subscription The subscription object.
365
+ * @param {string} topic The topic to subscribe to.
366
+ * @param {string} controller The controller alias.
367
+ * @returns {Subscription} The created or existing subscription.
368
+ */
369
+ ___subscribe(subscription, topic, controller) {
370
+ if (this.hasSubscription(subscription.topic)) {
371
+ return this.getSubscription(topic);
372
+ }
373
+ this.subscriptions.push(subscription);
374
+ return subscription;
375
+ }
376
+ /**
377
+ * Unsubscribes the controller from a specific topic.
378
+ *
379
+ * @param {Subscription} subscription The subscription to unsubscribe.
380
+ * @returns {boolean} True if the subscription was successfully unsubscribed, false otherwise.
381
+ */
382
+ ___unsubscribe(subscription) {
383
+ let index = this.subscriptions.indexOf(subscription);
384
+ if (index >= 0) {
385
+ let result = this.subscriptions.splice(index, 1);
386
+ return true;
387
+ }
388
+ else
389
+ return false;
390
+ }
391
+ /**
392
+ * Gets the query parameters from the request.
393
+ *
394
+ * @returns {URLSearchParams} The URLSearchParams object containing the query parameters.
395
+ */
396
+ get queryParameters() {
397
+ return new url_1.URLSearchParams(this.request.url.replace("/?", ""));
398
+ }
399
+ /**
400
+ * Gets the headers from the request.
401
+ *
402
+ * @returns {Map<string, string>} A Map containing the request headers.
403
+ */
404
+ get headers() {
405
+ let headers = new Map();
406
+ try {
407
+ Object.keys(this.connection.transport.request["headers"]).forEach(k => {
408
+ headers.set(k, this.connection.transport.request["headers"][k]);
409
+ });
410
+ }
411
+ catch (_a) {
412
+ return headers;
413
+ }
414
+ return headers;
415
+ }
416
+ /**
417
+ * Gets the underlying request object.
418
+ *
419
+ * @returns {any} The request object.
420
+ */
421
+ get request() {
422
+ return this.connection.transport.request;
423
+ }
424
+ /**
425
+ * Creates a new instance of the controller.
426
+ * @param {Connection} connection The connection associated with the controller.
427
+ * @returns {ControllerBase} The new controller instance.
428
+ */
429
+ instance(connection) {
430
+ console.log(`Create a new Instance of ${this.alias} for ${connection.id}`);
431
+ return new ControllerBase(connection);
432
+ }
433
+ }
434
+ exports.ControllerBase = ControllerBase;
435
+ __decorate([
436
+ (0, CanInvoke_1.CanInvoke)(false)
437
+ ], ControllerBase.prototype, "invokeMethod", null);
438
+ __decorate([
439
+ (0, CanInvoke_1.CanInvoke)(false)
440
+ ], ControllerBase.prototype, "setProperty", null);
441
+ __decorate([
442
+ (0, CanInvoke_1.CanInvoke)(false)
443
+ ], ControllerBase.prototype, "getProperty", null);
444
+ __decorate([
445
+ (0, CanInvoke_1.CanInvoke)(false)
446
+ ], ControllerBase.prototype, "getName", null);
447
+ __decorate([
448
+ (0, CanSet_1.CanSetGet)(false)
449
+ ], ControllerBase.prototype, "alias", void 0);
450
+ __decorate([
451
+ (0, CanSet_1.CanSetGet)(false)
452
+ ], ControllerBase.prototype, "subscriptions", void 0);
453
+ __decorate([
454
+ (0, CanSet_1.CanSetGet)(false)
455
+ ], ControllerBase.prototype, "connection", void 0);
456
+ __decorate([
457
+ (0, CanSet_1.CanSetGet)(false)
458
+ ], ControllerBase.prototype, "lastPong", void 0);
459
+ __decorate([
460
+ (0, CanSet_1.CanSetGet)(false)
461
+ /**
462
+ * Timestamp of the last ping sent.
463
+ * @type {Date | undefined}
464
+ */
465
+ ], ControllerBase.prototype, "lastPing", void 0);
466
+ __decorate([
467
+ (0, CanSet_1.CanSetGet)(false)
468
+ ], ControllerBase.prototype, "heartbeatInterval", void 0);
469
+ __decorate([
470
+ (0, CanInvoke_1.CanInvoke)(false)
471
+ ], ControllerBase.prototype, "enableHeartbeat", null);
472
+ __decorate([
473
+ (0, CanInvoke_1.CanInvoke)(false)
474
+ ], ControllerBase.prototype, "disbaleHeartbeat", null);
475
+ __decorate([
476
+ (0, CanInvoke_1.CanInvoke)(false)
477
+ ], ControllerBase.prototype, "canInvokeMethod", null);
478
+ __decorate([
479
+ (0, CanInvoke_1.CanInvoke)(false)
480
+ ], ControllerBase.prototype, "canSetProperty", null);
481
+ __decorate([
482
+ (0, CanInvoke_1.CanInvoke)(false)
483
+ ], ControllerBase.prototype, "canGetProperty", null);
484
+ __decorate([
485
+ (0, CanInvoke_1.CanInvoke)(false)
486
+ ], ControllerBase.prototype, "findOn", null);
487
+ __decorate([
488
+ (0, CanInvoke_1.CanInvoke)(false)
489
+ ], ControllerBase.prototype, "getConnections", null);
490
+ __decorate([
491
+ (0, CanInvoke_1.CanInvoke)(false)
492
+ ], ControllerBase.prototype, "onopen", null);
493
+ __decorate([
494
+ (0, CanInvoke_1.CanInvoke)(false)
495
+ ], ControllerBase.prototype, "onclose", null);
496
+ __decorate([
497
+ (0, CanInvoke_1.CanInvoke)(false)
498
+ ], ControllerBase.prototype, "find", null);
499
+ __decorate([
500
+ (0, CanInvoke_1.CanInvoke)(false)
501
+ ], ControllerBase.prototype, "invokeError", null);
502
+ __decorate([
503
+ (0, CanInvoke_1.CanInvoke)(false)
504
+ ], ControllerBase.prototype, "invokeToOthers", null);
505
+ __decorate([
506
+ (0, CanInvoke_1.CanInvoke)(false)
507
+ ], ControllerBase.prototype, "invokeToAll", null);
508
+ __decorate([
509
+ (0, CanInvoke_1.CanInvoke)(false)
510
+ ], ControllerBase.prototype, "invokeTo", null);
511
+ __decorate([
512
+ (0, CanInvoke_1.CanInvoke)(false)
513
+ ], ControllerBase.prototype, "invoke", null);
514
+ __decorate([
515
+ (0, CanInvoke_1.CanInvoke)(false)
516
+ ], ControllerBase.prototype, "publish", null);
517
+ __decorate([
518
+ (0, CanInvoke_1.CanInvoke)(false)
519
+ ], ControllerBase.prototype, "publishToAll", null);
520
+ __decorate([
521
+ (0, CanInvoke_1.CanInvoke)(false)
522
+ ], ControllerBase.prototype, "hasSubscription", null);
523
+ __decorate([
524
+ (0, CanInvoke_1.CanInvoke)(false)
525
+ ], ControllerBase.prototype, "addSubscription", null);
526
+ __decorate([
527
+ (0, CanInvoke_1.CanInvoke)(false)
528
+ ], ControllerBase.prototype, "removeSubscription", null);
529
+ __decorate([
530
+ (0, CanInvoke_1.CanInvoke)(false)
531
+ ], ControllerBase.prototype, "getSubscription", null);
532
+ __decorate([
533
+ (0, CanInvoke_1.CanInvoke)(true)
534
+ ], ControllerBase.prototype, "___connect", null);
535
+ __decorate([
536
+ (0, CanInvoke_1.CanInvoke)(true)
537
+ ], ControllerBase.prototype, "___close", null);
538
+ __decorate([
539
+ (0, CanInvoke_1.CanInvoke)(true)
540
+ ], ControllerBase.prototype, "___subscribe", null);
541
+ __decorate([
542
+ (0, CanInvoke_1.CanInvoke)(true)
543
+ ], ControllerBase.prototype, "___unsubscribe", null);
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Represents a subscription to a specific topic with an associated controller.
3
+ *
4
+ * @export
5
+ * @class Subscription
6
+ */
7
+ export declare class Subscription {
8
+ /**
9
+ * The topic of the subscription.
10
+ *
11
+ * @type {string}
12
+ */
13
+ topic: string;
14
+ /**
15
+ * The controller associated with the subscription.
16
+ *
17
+ * @type {string}
18
+ */
19
+ controller: string;
20
+ /**
21
+ * Creates an instance of Subscription.
22
+ *
23
+ * @param {string} topic - The topic of the subscription.
24
+ * @param {string} controller - The controller associated with the subscription.
25
+ */
26
+ constructor(topic: string, controller: string);
27
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Subscription = void 0;
4
+ /**
5
+ * Represents a subscription to a specific topic with an associated controller.
6
+ *
7
+ * @export
8
+ * @class Subscription
9
+ */
10
+ class Subscription {
11
+ /**
12
+ * Creates an instance of Subscription.
13
+ *
14
+ * @param {string} topic - The topic of the subscription.
15
+ * @param {string} controller - The controller associated with the subscription.
16
+ */
17
+ constructor(topic, controller) {
18
+ this.topic = topic;
19
+ this.controller = controller;
20
+ }
21
+ }
22
+ exports.Subscription = Subscription;