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.
- package/.gitattributes +17 -17
- package/{index.d.ts → build/index.d.ts} +11 -8
- package/build/index.js +47 -0
- package/build/src/Connection/ClientInfo.d.ts +27 -0
- package/{src → build/src}/Connection/ClientInfo.js +7 -0
- package/build/src/Connection/Connection.d.ts +117 -0
- package/build/src/Connection/Connection.js +202 -0
- package/build/src/Connection/IClientInfo.d.ts +20 -0
- package/build/src/Controller/ControllerBase.d.ts +134 -0
- package/build/src/Controller/ControllerBase.js +543 -0
- package/build/src/Controller/Subscription.d.ts +27 -0
- package/build/src/Controller/Subscription.js +22 -0
- package/build/src/Controllers/BrokerController/Broker.js +145 -0
- package/build/src/Controllers/BrokerController/BrokerController.d.ts +61 -0
- package/build/src/Controllers/BrokerController/BrokerController.js +115 -0
- package/{src/Controllers/BrokerController/Broker.js → build/src/Controllers/BrokerController/BrokerrController.js} +74 -85
- package/build/src/Controllers/BrokerController/Models/PeerConnection.d.ts +7 -0
- package/build/src/Controllers/BrokerController/Models/PeerConnection.js +14 -0
- package/build/src/Controllers/BrokerController/Models/Signal.js +11 -0
- package/build/src/Decorators/CanInvoke.d.ts +10 -0
- package/build/src/Decorators/CanInvoke.js +22 -0
- package/build/src/Decorators/CanSet.d.ts +10 -0
- package/build/src/Decorators/CanSet.js +20 -0
- package/build/src/Decorators/ControllerProperties.d.ts +10 -0
- package/build/src/Decorators/ControllerProperties.js +20 -0
- package/build/src/Interfaces/IInterceptor.js +2 -0
- package/build/src/Interfaces/ITransport.d.ts +94 -0
- package/build/src/Interfaces/ITransportMessage.d.ts +41 -0
- package/build/src/Messages/BufferMessage.d.ts +38 -0
- package/{src → build/src}/Messages/BufferMessage.js +32 -2
- package/build/src/Messages/ErrorMessage.d.ts +29 -0
- package/build/src/Messages/ErrorMessage.js +22 -0
- package/build/src/Messages/PipeMessage.d.ts +46 -0
- package/build/src/Messages/PipeMessage.js +41 -0
- package/build/src/Messages/TextMessage.d.ts +88 -0
- package/build/src/Messages/TextMessage.js +94 -0
- package/build/src/Messages/WebSocketMessage.d.ts +34 -0
- package/build/src/Messages/WebSocketMessage.js +41 -0
- package/build/src/Plugin.js +32 -0
- package/build/src/Server/Plugin.d.ts +37 -0
- package/build/src/Server/Plugin.js +32 -0
- package/build/src/Server/ThorIOServer.d.ts +68 -0
- package/build/src/Server/ThorIOServer.js +131 -0
- package/build/src/Subscription.js +22 -0
- package/build/src/ThorIO.js +142 -0
- package/build/src/ThorIOServer.d.ts +64 -0
- package/build/src/ThorIOServer.js +131 -0
- package/build/src/Transports/BufferMessageTransport.d.ts +78 -0
- package/build/src/Transports/BufferMessageTransport.js +85 -0
- package/build/src/Transports/PipeMessageTransport.d.ts +79 -0
- package/build/src/Transports/PipeMessageTransport.js +90 -0
- package/build/src/Transports/WebSocketMessageTransport.d.ts +87 -0
- package/build/src/Transports/WebSocketMessageTransport.js +93 -0
- package/build/src/Utils/BufferUtils.d.ts +32 -0
- package/build/src/Utils/BufferUtils.js +57 -0
- package/build/src/Utils/StringUtils.d.ts +15 -0
- package/build/src/Utils/StringUtils.js +31 -0
- package/build/src/test.js +5 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +43 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1610 -0
- package/docs/classes/BrokerController.html +164 -0
- package/docs/classes/BufferMessage.html +11 -0
- package/docs/classes/BufferMessageTransport.html +31 -0
- package/docs/classes/BufferUtils.html +16 -0
- package/docs/classes/ClientInfo.html +11 -0
- package/docs/classes/Connection.html +42 -0
- package/docs/classes/ControllerBase.html +144 -0
- package/docs/classes/ErrorMessage.html +13 -0
- package/docs/classes/PeerConnection.html +4 -0
- package/docs/classes/PipeMessage.html +17 -0
- package/docs/classes/PipeMessageTransport.html +33 -0
- package/docs/classes/Plugin.html +13 -0
- package/docs/classes/Signal.html +5 -0
- package/docs/classes/StringUtils.html +9 -0
- package/docs/classes/Subscription.html +11 -0
- package/docs/classes/TextMessage.html +37 -0
- package/docs/classes/ThorIOServer.html +25 -0
- package/docs/classes/WebSocketMessage.html +18 -0
- package/docs/classes/WebSocketMessageTransport.html +35 -0
- package/docs/functions/CanInvoke.html +5 -0
- package/docs/functions/CanSet.html +5 -0
- package/docs/functions/ControllerProperties.html +5 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +24 -0
- package/docs/interfaces/ITransport.html +46 -0
- package/docs/interfaces/ITransportMessage.html +21 -0
- package/docs/modules.html +1 -0
- package/index.ts +32 -35
- package/licence +21 -21
- package/package.json +48 -49
- package/readme.md +32 -71
- package/src/Connection/ClientInfo.ts +34 -37
- package/src/Connection/Connection.ts +229 -278
- package/src/Connection/IClientInfo.ts +22 -0
- package/src/Controller/ControllerBase.ts +561 -458
- package/src/{Subscription.ts → Controller/Subscription.ts} +32 -34
- package/src/Controllers/BrokerController/BrokerController.ts +121 -0
- package/src/Controllers/BrokerController/Models/PeerConnection.ts +12 -34
- package/src/Controllers/BrokerController/Models/Signal.ts +11 -43
- package/src/Decorators/CanInvoke.ts +21 -13
- package/src/Decorators/CanSet.ts +19 -12
- package/src/Decorators/ControllerProperties.ts +19 -16
- package/src/Interfaces/ITransport.ts +107 -23
- package/src/Interfaces/ITransportMessage.ts +45 -41
- package/src/Messages/BufferMessage.ts +70 -69
- package/src/Messages/ErrorMessage.ts +34 -6
- package/src/Messages/PipeMessage.ts +56 -62
- package/src/Messages/TextMessage.ts +139 -93
- package/src/Messages/WebSocketMessage.ts +39 -41
- package/src/Server/Plugin.ts +56 -0
- package/src/Server/ThorIOServer.ts +121 -0
- package/src/ThorIOServer.ts +117 -0
- package/src/Transports/BufferMessageTransport.ts +109 -100
- package/src/Transports/PipeMessageTransport.ts +112 -104
- package/src/Transports/WebSocketMessageTransport.ts +122 -110
- package/src/Utils/BufferUtils.ts +55 -61
- package/src/Utils/StringUtils.ts +28 -17
- package/tsconfig.json +109 -18
- package/typedoc.json +6 -0
- package/index.js +0 -40
- package/src/Connection/ClientInfo.d.ts +0 -6
- package/src/Connection/Connection.d.ts +0 -24
- package/src/Connection/Connection.js +0 -129
- package/src/Controller/ControllerBase.d.ts +0 -43
- package/src/Controller/ControllerBase.js +0 -331
- package/src/Controllers/BrokerController/Broker.d.ts +0 -21
- package/src/Controllers/BrokerController/Broker.ts +0 -153
- package/src/Controllers/BrokerController/Models/PeerConnection.d.ts +0 -5
- package/src/Controllers/BrokerController/Models/PeerConnection.js +0 -9
- package/src/Controllers/BrokerController/Models/Signal.js +0 -10
- package/src/Decorators/CanInvoke.d.ts +0 -2
- package/src/Decorators/CanInvoke.js +0 -9
- package/src/Decorators/CanSet.d.ts +0 -1
- package/src/Decorators/CanSet.js +0 -8
- package/src/Decorators/ControllerProperties.d.ts +0 -1
- package/src/Decorators/ControllerProperties.js +0 -10
- package/src/Interfaces/IInterceptor.d.ts +0 -2
- package/src/Interfaces/IInterceptor.ts +0 -2
- package/src/Interfaces/ITransport.d.ts +0 -18
- package/src/Interfaces/ITransportMessage.d.ts +0 -8
- package/src/Messages/BufferMessage.d.ts +0 -10
- package/src/Messages/ErrorMessage.d.ts +0 -4
- package/src/Messages/ErrorMessage.js +0 -8
- package/src/Messages/PipeMessage.d.ts +0 -12
- package/src/Messages/PipeMessage.js +0 -20
- package/src/Messages/TextMessage.d.ts +0 -15
- package/src/Messages/TextMessage.js +0 -50
- package/src/Messages/WebSocketMessage.d.ts +0 -10
- package/src/Messages/WebSocketMessage.js +0 -15
- package/src/Plugin.d.ts +0 -6
- package/src/Plugin.js +0 -11
- package/src/Plugin.ts +0 -37
- package/src/Subscription.d.ts +0 -5
- package/src/Subscription.js +0 -9
- package/src/ThorIO.d.ts +0 -19
- package/src/ThorIO.js +0 -67
- package/src/ThorIO.ts +0 -153
- package/src/Transports/BufferMessageTransport.d.ts +0 -21
- package/src/Transports/BufferMessageTransport.js +0 -31
- package/src/Transports/PipeMessageTransport.d.ts +0 -20
- package/src/Transports/PipeMessageTransport.js +0 -33
- package/src/Transports/WebSocketMessageTransport.d.ts +0 -20
- package/src/Transports/WebSocketMessageTransport.js +0 -31
- package/src/Utils/BufferUtils.d.ts +0 -5
- package/src/Utils/BufferUtils.js +0 -29
- package/src/Utils/StringUtils.d.ts +0 -4
- package/src/Utils/StringUtils.js +0 -13
- /package/{src/Interfaces/IInterceptor.js → build/src/Connection/IClientInfo.js} +0 -0
- /package/{src → build/src}/Controllers/BrokerController/Models/Signal.d.ts +0 -0
- /package/{src → build/src}/Interfaces/ITransport.js +0 -0
- /package/{src → build/src}/Interfaces/ITransportMessage.js +0 -0
|
@@ -1,278 +1,229 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { ClientInfo } from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
* @
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*
|
|
26
|
-
* @
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
*
|
|
33
|
-
* @
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
public
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
* @
|
|
41
|
-
* @
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
controller
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @param {ITransport} transport
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
*
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
*
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
this.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
*
|
|
215
|
-
* @
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
+
}
|