@twin.org/engine-server 0.0.1-next.2 → 0.0.1-next.20
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/dist/cjs/index.cjs +216 -306
- package/dist/esm/index.mjs +216 -307
- package/dist/types/engineServer.d.ts +12 -15
- package/dist/types/index.d.ts +2 -0
- package/dist/types/models/IEngineServerEnvironmentVariables.d.ts +37 -0
- package/dist/types/utils/engineServerEnvBuilder.d.ts +12 -0
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/EngineServer.md +25 -25
- package/docs/reference/functions/buildEngineServerConfiguration.md +25 -0
- package/docs/reference/index.md +8 -0
- package/docs/reference/interfaces/IEngineServerEnvironmentVariables.md +67 -0
- package/package.json +6 -66
- package/dist/types/components/authentication.d.ts +0 -11
- package/dist/types/components/information.d.ts +0 -11
- package/dist/types/components/mimeTypeProcessor.d.ts +0 -11
- package/dist/types/components/restRouteProcessor.d.ts +0 -11
- package/dist/types/components/socketRouteProcessor.d.ts +0 -11
package/dist/esm/index.mjs
CHANGED
|
@@ -1,261 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MimeTypeProcessorFactory, RestRouteProcessorFactory, SocketRouteProcessorFactory } from '@twin.org/api-models';
|
|
1
|
+
import { RestRouteProcessorFactory, SocketRouteProcessorFactory, MimeTypeProcessorFactory } from '@twin.org/api-models';
|
|
3
2
|
import { FastifyWebServer } from '@twin.org/api-server-fastify';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { generateRestRoutesAuditableItemStream } from '@twin.org/auditable-item-stream-service';
|
|
8
|
-
import { generateRestRoutesBlobStorage } from '@twin.org/blob-storage-service';
|
|
9
|
-
import { I18n, GeneralError, ComponentFactory, Guards, Is } from '@twin.org/core';
|
|
10
|
-
import { AuthenticationComponentType, InformationComponentType, MimeTypeProcessorType, RestRouteProcessorType, SocketRouteProcessorType } from '@twin.org/engine-models';
|
|
11
|
-
import { generateRestRoutesEntityStorage } from '@twin.org/entity-storage-service';
|
|
12
|
-
import { generateRestRoutesIdentity, generateRestRoutesIdentityProfile } from '@twin.org/identity-service';
|
|
13
|
-
import { generateRestRoutesLogging } from '@twin.org/logging-service';
|
|
14
|
-
import { generateRestRoutesNft } from '@twin.org/nft-service';
|
|
15
|
-
import { generateRestRoutesTelemetry } from '@twin.org/telemetry-service';
|
|
16
|
-
import { initialiseEntityStorageConnector } from '@twin.org/engine-core';
|
|
17
|
-
import { JwtMimeTypeProcessor, LoggingProcessor, NodeIdentityProcessor, StaticUserIdentityProcessor, RestRouteProcessor, SocketRouteProcessor } from '@twin.org/api-processors';
|
|
3
|
+
import { Guards, Is, Coerce } from '@twin.org/core';
|
|
4
|
+
import { RestRouteProcessorType, InformationComponentType, SocketRouteProcessorType, AuthenticationComponentType } from '@twin.org/engine-server-types';
|
|
5
|
+
import { ModuleHelper } from '@twin.org/modules';
|
|
18
6
|
|
|
19
|
-
/**
|
|
20
|
-
* Initialise the authentication.
|
|
21
|
-
* @param engineCore The engine core.
|
|
22
|
-
* @param context The context for the engine.
|
|
23
|
-
* @param instanceConfig The instance config.
|
|
24
|
-
* @param overrideInstanceType The instance type to override the default.
|
|
25
|
-
* @returns The name of the instance created.
|
|
26
|
-
* @throws GeneralError if the component type is unknown.
|
|
27
|
-
*/
|
|
28
|
-
function initialiseAuthenticationComponent(engineCore, context, instanceConfig, overrideInstanceType) {
|
|
29
|
-
engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
|
|
30
|
-
element: `Authentication Component: ${instanceConfig.type}`
|
|
31
|
-
}));
|
|
32
|
-
const type = instanceConfig.type;
|
|
33
|
-
let component;
|
|
34
|
-
let instanceType;
|
|
35
|
-
if (type === AuthenticationComponentType.AuthEntityStorage) {
|
|
36
|
-
initSchema();
|
|
37
|
-
initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.userEntityStorageType, "AuthenticationUser");
|
|
38
|
-
component = new EntityStorageAuthenticationService({
|
|
39
|
-
vaultConnectorType: context.defaultTypes.vaultConnector,
|
|
40
|
-
...instanceConfig.options
|
|
41
|
-
});
|
|
42
|
-
instanceType = EntityStorageAuthenticationService.NAMESPACE;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
throw new GeneralError("engineCore", "componentUnknownType", {
|
|
46
|
-
type,
|
|
47
|
-
componentType: "authenticationComponent"
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
const finalInstanceType = overrideInstanceType ?? instanceType;
|
|
51
|
-
context.componentInstances.push({
|
|
52
|
-
instanceType: finalInstanceType,
|
|
53
|
-
component
|
|
54
|
-
});
|
|
55
|
-
ComponentFactory.register(finalInstanceType, () => component);
|
|
56
|
-
return finalInstanceType;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Initialise the information component.
|
|
61
|
-
* @param engineCore The engine core.
|
|
62
|
-
* @param context The context for the engine.
|
|
63
|
-
* @param instanceConfig The instance config.
|
|
64
|
-
* @param overrideInstanceType The instance type to override the default.
|
|
65
|
-
* @returns The name of the instance created.
|
|
66
|
-
* @throws GeneralError if the component type is unknown.
|
|
67
|
-
*/
|
|
68
|
-
function initialiseInformationComponent(engineCore, context, instanceConfig, overrideInstanceType) {
|
|
69
|
-
engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
|
|
70
|
-
element: `Information Component: ${instanceConfig.type}`
|
|
71
|
-
}));
|
|
72
|
-
const type = instanceConfig.type;
|
|
73
|
-
let component;
|
|
74
|
-
let instanceType;
|
|
75
|
-
if (type === InformationComponentType.Service) {
|
|
76
|
-
component = new InformationService(instanceConfig.options);
|
|
77
|
-
instanceType = InformationService.NAMESPACE;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
throw new GeneralError("engineCore", "componentUnknownType", {
|
|
81
|
-
type,
|
|
82
|
-
componentType: "informationComponent"
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
const finalInstanceType = overrideInstanceType ?? instanceType;
|
|
86
|
-
context.componentInstances.push({
|
|
87
|
-
instanceType: finalInstanceType,
|
|
88
|
-
component
|
|
89
|
-
});
|
|
90
|
-
ComponentFactory.register(finalInstanceType, () => component);
|
|
91
|
-
return finalInstanceType;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Copyright 2024 IOTA Stiftung.
|
|
95
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
96
|
-
/**
|
|
97
|
-
* Initialise the mime type processor.
|
|
98
|
-
* @param engineCore The engine core.
|
|
99
|
-
* @param context The context for the engine.
|
|
100
|
-
* @param instanceConfig The instance config.
|
|
101
|
-
* @param overrideInstanceType The instance type to override the default.
|
|
102
|
-
* @returns The name of the instance created.
|
|
103
|
-
* @throws GeneralError if the component type is unknown.
|
|
104
|
-
*/
|
|
105
|
-
function initialiseMimeTypeProcessorComponent(engineCore, context, instanceConfig, overrideInstanceType) {
|
|
106
|
-
engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
|
|
107
|
-
element: `Mime Type Processor: ${instanceConfig.type}`
|
|
108
|
-
}));
|
|
109
|
-
const type = instanceConfig.type;
|
|
110
|
-
let component;
|
|
111
|
-
let instanceType;
|
|
112
|
-
if (type === MimeTypeProcessorType.Jwt) {
|
|
113
|
-
component = new JwtMimeTypeProcessor();
|
|
114
|
-
instanceType = JwtMimeTypeProcessor.NAMESPACE;
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
throw new GeneralError("engineCore", "componentUnknownType", {
|
|
118
|
-
type,
|
|
119
|
-
componentType: "mimeTypeProcessorComponent"
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
const finalInstanceType = overrideInstanceType ?? instanceType;
|
|
123
|
-
context.componentInstances.push({
|
|
124
|
-
instanceType: finalInstanceType,
|
|
125
|
-
component
|
|
126
|
-
});
|
|
127
|
-
MimeTypeProcessorFactory.register(finalInstanceType, () => component);
|
|
128
|
-
return finalInstanceType;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Copyright 2024 IOTA Stiftung.
|
|
132
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
133
|
-
/**
|
|
134
|
-
* Initialise the rest route processor.
|
|
135
|
-
* @param engineCore The engine core.
|
|
136
|
-
* @param context The context for the engine.
|
|
137
|
-
* @param instanceConfig The instance config.
|
|
138
|
-
* @param overrideInstanceType The instance type to override the default.
|
|
139
|
-
* @returns The name of the instance created.
|
|
140
|
-
* @throws GeneralError if the component type is unknown.
|
|
141
|
-
*/
|
|
142
|
-
function initialiseRestRouteProcessorComponent(engineCore, context, instanceConfig, overrideInstanceType) {
|
|
143
|
-
engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
|
|
144
|
-
element: `REST Route Processor: ${instanceConfig.type}`
|
|
145
|
-
}));
|
|
146
|
-
const type = instanceConfig.type;
|
|
147
|
-
let component;
|
|
148
|
-
let instanceType;
|
|
149
|
-
if (type === RestRouteProcessorType.AuthHeader) {
|
|
150
|
-
component = new AuthHeaderProcessor({
|
|
151
|
-
vaultConnectorType: context.defaultTypes.vaultConnector,
|
|
152
|
-
config: {
|
|
153
|
-
...instanceConfig.options?.config
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
instanceType = AuthHeaderProcessor.NAMESPACE;
|
|
157
|
-
}
|
|
158
|
-
else if (type === RestRouteProcessorType.Logging) {
|
|
159
|
-
component = new LoggingProcessor({
|
|
160
|
-
loggingConnectorType: context.defaultTypes.loggingConnector,
|
|
161
|
-
config: {
|
|
162
|
-
...instanceConfig.options?.config
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
instanceType = LoggingProcessor.NAMESPACE;
|
|
166
|
-
}
|
|
167
|
-
else if (type === RestRouteProcessorType.NodeIdentity) {
|
|
168
|
-
component = new NodeIdentityProcessor();
|
|
169
|
-
instanceType = NodeIdentityProcessor.NAMESPACE;
|
|
170
|
-
}
|
|
171
|
-
else if (type === RestRouteProcessorType.StaticUserIdentity) {
|
|
172
|
-
component = new StaticUserIdentityProcessor(instanceConfig.options);
|
|
173
|
-
instanceType = StaticUserIdentityProcessor.NAMESPACE;
|
|
174
|
-
}
|
|
175
|
-
else if (type === RestRouteProcessorType.RestRoute) {
|
|
176
|
-
component = new RestRouteProcessor(instanceConfig.options);
|
|
177
|
-
instanceType = RestRouteProcessor.NAMESPACE;
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
throw new GeneralError("engineCore", "componentUnknownType", {
|
|
181
|
-
type,
|
|
182
|
-
componentType: "restRouteProcessorComponent"
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
const finalInstanceType = overrideInstanceType ?? instanceType;
|
|
186
|
-
context.componentInstances.push({
|
|
187
|
-
instanceType: finalInstanceType,
|
|
188
|
-
component
|
|
189
|
-
});
|
|
190
|
-
RestRouteProcessorFactory.register(finalInstanceType, () => component);
|
|
191
|
-
return finalInstanceType;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Copyright 2024 IOTA Stiftung.
|
|
195
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
196
|
-
/**
|
|
197
|
-
* Initialise the socket route processor.
|
|
198
|
-
* @param engineCore The engine core.
|
|
199
|
-
* @param context The context for the engine.
|
|
200
|
-
* @param instanceConfig The instance config.
|
|
201
|
-
* @param overrideInstanceType The instance type to override the default.
|
|
202
|
-
* @returns The name of the instance created.
|
|
203
|
-
* @throws GeneralError if the component type is unknown.
|
|
204
|
-
*/
|
|
205
|
-
function initialiseSocketRouteProcessorComponent(engineCore, context, instanceConfig, overrideInstanceType) {
|
|
206
|
-
engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
|
|
207
|
-
element: `Socket Route Processor: ${instanceConfig.type}`
|
|
208
|
-
}));
|
|
209
|
-
const type = instanceConfig.type;
|
|
210
|
-
let component;
|
|
211
|
-
let instanceType;
|
|
212
|
-
if (type === SocketRouteProcessorType.AuthHeader) {
|
|
213
|
-
component = new AuthHeaderProcessor({
|
|
214
|
-
vaultConnectorType: context.defaultTypes.vaultConnector,
|
|
215
|
-
config: {
|
|
216
|
-
...instanceConfig.options?.config
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
instanceType = AuthHeaderProcessor.NAMESPACE;
|
|
220
|
-
}
|
|
221
|
-
else if (type === SocketRouteProcessorType.Logging) {
|
|
222
|
-
component = new LoggingProcessor({
|
|
223
|
-
loggingConnectorType: context.defaultTypes.loggingConnector,
|
|
224
|
-
config: {
|
|
225
|
-
...instanceConfig.options.config
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
instanceType = LoggingProcessor.NAMESPACE;
|
|
229
|
-
}
|
|
230
|
-
else if (type === SocketRouteProcessorType.NodeIdentity) {
|
|
231
|
-
component = new NodeIdentityProcessor();
|
|
232
|
-
instanceType = NodeIdentityProcessor.NAMESPACE;
|
|
233
|
-
}
|
|
234
|
-
else if (type === SocketRouteProcessorType.StaticUserIdentity) {
|
|
235
|
-
component = new StaticUserIdentityProcessor(instanceConfig.options);
|
|
236
|
-
instanceType = StaticUserIdentityProcessor.NAMESPACE;
|
|
237
|
-
}
|
|
238
|
-
else if (type === SocketRouteProcessorType.SocketRoute) {
|
|
239
|
-
component = new SocketRouteProcessor(instanceConfig.options);
|
|
240
|
-
instanceType = SocketRouteProcessor.NAMESPACE;
|
|
241
|
-
}
|
|
242
|
-
else {
|
|
243
|
-
throw new GeneralError("engineCore", "componentUnknownType", {
|
|
244
|
-
type,
|
|
245
|
-
componentType: "socketRouteProcessorComponent"
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
const finalInstanceType = overrideInstanceType ?? instanceType;
|
|
249
|
-
context.componentInstances.push({
|
|
250
|
-
instanceType: finalInstanceType,
|
|
251
|
-
component
|
|
252
|
-
});
|
|
253
|
-
SocketRouteProcessorFactory.register(finalInstanceType, () => component);
|
|
254
|
-
return finalInstanceType;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Copyright 2024 IOTA Stiftung.
|
|
258
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
259
7
|
/**
|
|
260
8
|
* Server for the engine.
|
|
261
9
|
*/
|
|
@@ -264,10 +12,6 @@ class EngineServer {
|
|
|
264
12
|
* Runtime name for the class.
|
|
265
13
|
*/
|
|
266
14
|
CLASS_NAME = "EngineServer";
|
|
267
|
-
/**
|
|
268
|
-
* The server config.
|
|
269
|
-
*/
|
|
270
|
-
_config;
|
|
271
15
|
/**
|
|
272
16
|
* The engine.
|
|
273
17
|
* @internal
|
|
@@ -296,23 +40,19 @@ class EngineServer {
|
|
|
296
40
|
/**
|
|
297
41
|
* Create a new instance of EngineServer.
|
|
298
42
|
* @param options The options for the engine.
|
|
299
|
-
* @param options.server The server options for the engine.
|
|
300
43
|
* @param options.engineCore The engine core to serve from.
|
|
301
44
|
*/
|
|
302
45
|
constructor(options) {
|
|
303
46
|
Guards.object(this.CLASS_NAME, "options", options);
|
|
304
47
|
Guards.object(this.CLASS_NAME, "options.engineCore", options.engineCore);
|
|
305
|
-
this._config = options?.server ?? {};
|
|
306
48
|
this._engineCore = options.engineCore;
|
|
307
49
|
this._restRouteGenerators = [];
|
|
308
50
|
this._socketRouteGenerators = [];
|
|
309
51
|
const coreConfig = this._engineCore.getConfig();
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (!Is.arrayValue(this._config.restRouteProcessor)) {
|
|
313
|
-
this._config.restRouteProcessor = [];
|
|
52
|
+
if (!Is.arrayValue(coreConfig.types.restRouteProcessor)) {
|
|
53
|
+
coreConfig.types.restRouteProcessor = [];
|
|
314
54
|
if (!coreConfig.silent) {
|
|
315
|
-
|
|
55
|
+
coreConfig.types.restRouteProcessor.push({
|
|
316
56
|
type: RestRouteProcessorType.Logging,
|
|
317
57
|
options: {
|
|
318
58
|
config: {
|
|
@@ -321,7 +61,7 @@ class EngineServer {
|
|
|
321
61
|
}
|
|
322
62
|
});
|
|
323
63
|
}
|
|
324
|
-
|
|
64
|
+
coreConfig.types.restRouteProcessor.push({
|
|
325
65
|
type: RestRouteProcessorType.RestRoute,
|
|
326
66
|
options: {
|
|
327
67
|
config: {
|
|
@@ -338,14 +78,16 @@ class EngineServer {
|
|
|
338
78
|
* Add a REST route generator.
|
|
339
79
|
* @param type The type to add the generator for.
|
|
340
80
|
* @param typeConfig The type config.
|
|
341
|
-
* @param
|
|
81
|
+
* @param module The module containing the generator.
|
|
82
|
+
* @param method The method to call on the module.
|
|
342
83
|
*/
|
|
343
|
-
addRestRouteGenerator(type, typeConfig,
|
|
84
|
+
addRestRouteGenerator(type, typeConfig, module, method) {
|
|
344
85
|
if (!Is.empty(typeConfig)) {
|
|
345
86
|
this._restRouteGenerators.push({
|
|
346
87
|
type,
|
|
347
88
|
typeConfig,
|
|
348
|
-
|
|
89
|
+
module,
|
|
90
|
+
method
|
|
349
91
|
});
|
|
350
92
|
}
|
|
351
93
|
}
|
|
@@ -353,24 +95,29 @@ class EngineServer {
|
|
|
353
95
|
* Add a socket route generator.
|
|
354
96
|
* @param type The type to add the generator for.
|
|
355
97
|
* @param typeConfig The type config.
|
|
356
|
-
* @param
|
|
98
|
+
* @param module The module containing the generator.
|
|
99
|
+
* @param method The method to call on the module.
|
|
357
100
|
*/
|
|
358
|
-
addSocketRouteGenerator(type, typeConfig,
|
|
101
|
+
addSocketRouteGenerator(type, typeConfig, module, method) {
|
|
359
102
|
if (!Is.empty(typeConfig)) {
|
|
360
103
|
this._socketRouteGenerators.push({
|
|
361
104
|
type,
|
|
362
105
|
typeConfig,
|
|
363
|
-
|
|
106
|
+
module,
|
|
107
|
+
method
|
|
364
108
|
});
|
|
365
109
|
}
|
|
366
110
|
}
|
|
367
111
|
/**
|
|
368
112
|
* Start the engine server.
|
|
369
|
-
* @returns
|
|
113
|
+
* @returns True if the start was successful.
|
|
370
114
|
*/
|
|
371
115
|
async start() {
|
|
372
|
-
await this._engineCore.start();
|
|
373
|
-
|
|
116
|
+
const canContinue = await this._engineCore.start();
|
|
117
|
+
if (canContinue) {
|
|
118
|
+
await this.startWebServer();
|
|
119
|
+
}
|
|
120
|
+
return canContinue;
|
|
374
121
|
}
|
|
375
122
|
/**
|
|
376
123
|
* Stop the engine server.
|
|
@@ -388,16 +135,19 @@ class EngineServer {
|
|
|
388
135
|
* @internal
|
|
389
136
|
*/
|
|
390
137
|
async startWebServer() {
|
|
391
|
-
const restRoutes = this.buildRestRoutes();
|
|
392
|
-
const socketRoutes = this.buildSocketRoutes();
|
|
138
|
+
const restRoutes = await this.buildRestRoutes();
|
|
139
|
+
const socketRoutes = await this.buildSocketRoutes();
|
|
393
140
|
const restRouteProcessors = RestRouteProcessorFactory.names().map(n => RestRouteProcessorFactory.get(n));
|
|
394
141
|
const socketRouteProcessors = SocketRouteProcessorFactory.names().map(n => SocketRouteProcessorFactory.get(n));
|
|
395
142
|
const mimeTypeProcessors = MimeTypeProcessorFactory.names().map(n => MimeTypeProcessorFactory.get(n));
|
|
143
|
+
const coreConfig = this._engineCore.getConfig();
|
|
144
|
+
const defaults = this._engineCore.getDefaultTypes();
|
|
145
|
+
this._loggingConnectorType = coreConfig.silent ? undefined : defaults.loggingConnector;
|
|
396
146
|
this._webServer = new FastifyWebServer({
|
|
397
147
|
loggingConnectorType: this._loggingConnectorType,
|
|
398
148
|
mimeTypeProcessors
|
|
399
149
|
});
|
|
400
|
-
await this._webServer.build(restRouteProcessors, restRoutes, socketRouteProcessors, socketRoutes,
|
|
150
|
+
await this._webServer.build(restRouteProcessors, restRoutes, socketRouteProcessors, socketRoutes, coreConfig.web);
|
|
401
151
|
await this._webServer.start();
|
|
402
152
|
}
|
|
403
153
|
/**
|
|
@@ -405,10 +155,10 @@ class EngineServer {
|
|
|
405
155
|
* @returns The REST routes for the application.
|
|
406
156
|
* @internal
|
|
407
157
|
*/
|
|
408
|
-
buildRestRoutes() {
|
|
158
|
+
async buildRestRoutes() {
|
|
409
159
|
const routes = [];
|
|
410
|
-
for (const { type, typeConfig,
|
|
411
|
-
this.initialiseRestTypeRoute(routes, type, typeConfig,
|
|
160
|
+
for (const { type, typeConfig, module, method } of this._restRouteGenerators) {
|
|
161
|
+
await this.initialiseRestTypeRoute(routes, type, typeConfig, module, method);
|
|
412
162
|
}
|
|
413
163
|
return routes;
|
|
414
164
|
}
|
|
@@ -417,10 +167,10 @@ class EngineServer {
|
|
|
417
167
|
* @returns The socket routes for the application.
|
|
418
168
|
* @internal
|
|
419
169
|
*/
|
|
420
|
-
buildSocketRoutes() {
|
|
170
|
+
async buildSocketRoutes() {
|
|
421
171
|
const routes = [];
|
|
422
|
-
for (const { type, typeConfig,
|
|
423
|
-
this.initialiseSocketTypeRoute(routes, type, typeConfig,
|
|
172
|
+
for (const { type, typeConfig, module, method } of this._socketRouteGenerators) {
|
|
173
|
+
await this.initialiseSocketTypeRoute(routes, type, typeConfig, module, method);
|
|
424
174
|
}
|
|
425
175
|
return routes;
|
|
426
176
|
}
|
|
@@ -432,9 +182,10 @@ class EngineServer {
|
|
|
432
182
|
* @param generateRoutes The function to generate the routes.
|
|
433
183
|
* @internal
|
|
434
184
|
*/
|
|
435
|
-
initialiseRestTypeRoute(routes, typeKey, typeConfig,
|
|
185
|
+
async initialiseRestTypeRoute(routes, typeKey, typeConfig, module, method) {
|
|
436
186
|
if (Is.arrayValue(typeConfig)) {
|
|
437
187
|
const defaultEngineTypes = this._engineCore.getDefaultTypes();
|
|
188
|
+
const generateRoutes = await ModuleHelper.getModuleEntry(module, method);
|
|
438
189
|
for (let i = 0; i < typeConfig.length; i++) {
|
|
439
190
|
const restPath = typeConfig[i].restPath;
|
|
440
191
|
if (Is.string(restPath)) {
|
|
@@ -451,12 +202,14 @@ class EngineServer {
|
|
|
451
202
|
* @param routes The routes to add to.
|
|
452
203
|
* @param typeKey The key for the default types.
|
|
453
204
|
* @param typeConfig The type config.
|
|
454
|
-
* @param
|
|
205
|
+
* @param module The module containing the generator.
|
|
206
|
+
* @param method The method to call on the module.
|
|
455
207
|
* @internal
|
|
456
208
|
*/
|
|
457
|
-
initialiseSocketTypeRoute(routes, typeKey, typeConfig,
|
|
209
|
+
async initialiseSocketTypeRoute(routes, typeKey, typeConfig, module, method) {
|
|
458
210
|
if (Is.arrayValue(typeConfig)) {
|
|
459
211
|
const defaultEngineTypes = this._engineCore.getDefaultTypes();
|
|
212
|
+
const generateRoutes = await ModuleHelper.getModuleEntry(module, method);
|
|
460
213
|
for (let i = 0; i < typeConfig.length; i++) {
|
|
461
214
|
const socketPath = typeConfig[i].socketPath;
|
|
462
215
|
if (Is.string(socketPath)) {
|
|
@@ -473,30 +226,31 @@ class EngineServer {
|
|
|
473
226
|
* @internal
|
|
474
227
|
*/
|
|
475
228
|
addServerTypeInitialisers() {
|
|
476
|
-
this._engineCore.
|
|
477
|
-
this._engineCore.addTypeInitialiser("
|
|
478
|
-
this._engineCore.addTypeInitialiser("
|
|
479
|
-
this._engineCore.addTypeInitialiser("
|
|
480
|
-
this._engineCore.addTypeInitialiser("
|
|
229
|
+
const coreConfig = this._engineCore.getConfig();
|
|
230
|
+
this._engineCore.addTypeInitialiser("authenticationComponent", coreConfig.types.authenticationComponent, "@twin.org/engine-server-types", "initialiseAuthenticationComponent");
|
|
231
|
+
this._engineCore.addTypeInitialiser("informationComponent", coreConfig.types.informationComponent, "@twin.org/engine-server-types", "initialiseInformationComponent");
|
|
232
|
+
this._engineCore.addTypeInitialiser("restRouteProcessor", coreConfig.types.restRouteProcessor, "@twin.org/engine-server-types", "initialiseRestRouteProcessorComponent");
|
|
233
|
+
this._engineCore.addTypeInitialiser("socketRouteProcessor", coreConfig.types.socketRouteProcessor, "@twin.org/engine-server-types", "initialiseSocketRouteProcessorComponent");
|
|
234
|
+
this._engineCore.addTypeInitialiser("mimeTypeProcessor", coreConfig.types.mimeTypeProcessor, "@twin.org/engine-server-types", "initialiseMimeTypeProcessorComponent");
|
|
481
235
|
}
|
|
482
236
|
/**
|
|
483
237
|
* Add the server REST route generators.
|
|
484
238
|
* @internal
|
|
485
239
|
*/
|
|
486
240
|
addServerRestRouteGenerators() {
|
|
487
|
-
this.addRestRouteGenerator("informationComponent", this._config.informationComponent, generateRestRoutesInformation);
|
|
488
|
-
this.addRestRouteGenerator("authenticationComponent", this._config.authenticationComponent, generateRestRoutesAuthentication);
|
|
489
241
|
const coreConfig = this._engineCore.getConfig();
|
|
490
|
-
this.addRestRouteGenerator("
|
|
491
|
-
this.addRestRouteGenerator("
|
|
492
|
-
this.addRestRouteGenerator("
|
|
493
|
-
this.addRestRouteGenerator("
|
|
494
|
-
this.addRestRouteGenerator("
|
|
495
|
-
this.addRestRouteGenerator("
|
|
496
|
-
this.addRestRouteGenerator("
|
|
497
|
-
this.addRestRouteGenerator("
|
|
498
|
-
this.addRestRouteGenerator("
|
|
499
|
-
this.addRestRouteGenerator("
|
|
242
|
+
this.addRestRouteGenerator("informationComponent", coreConfig.types.informationComponent, "@twin.org/api-service", "generateRestRoutesInformation");
|
|
243
|
+
this.addRestRouteGenerator("authenticationComponent", coreConfig.types.authenticationComponent, "@twin.org/api-auth-entity-storage-service", "generateRestRoutesAuthentication");
|
|
244
|
+
this.addRestRouteGenerator("loggingComponent", coreConfig.types.loggingComponent, "@twin.org/logging-service", "generateRestRoutesLogging");
|
|
245
|
+
this.addRestRouteGenerator("telemetryComponent", coreConfig.types.telemetryComponent, "@twin.org/telemetry-service", "generateRestRoutesTelemetry");
|
|
246
|
+
this.addRestRouteGenerator("blobStorageComponent", coreConfig.types.blobStorageComponent, "@twin.org/blob-storage-service", "generateRestRoutesBlobStorage");
|
|
247
|
+
this.addRestRouteGenerator("identityComponent", coreConfig.types.identityComponent, "@twin.org/identity-service", "generateRestRoutesIdentity");
|
|
248
|
+
this.addRestRouteGenerator("identityProfileComponent", coreConfig.types.identityProfileComponent, "@twin.org/identity-service", "generateRestRoutesIdentityProfile");
|
|
249
|
+
this.addRestRouteGenerator("nftComponent", coreConfig.types.nftComponent, "@twin.org/nft-service", "generateRestRoutesNft");
|
|
250
|
+
this.addRestRouteGenerator("attestationComponent", coreConfig.types.attestationComponent, "@twin.org/attestation-service", "generateRestRoutesAttestation");
|
|
251
|
+
this.addRestRouteGenerator("auditableItemGraphComponent", coreConfig.types.auditableItemGraphComponent, "@twin.org/auditable-item-graph-service", "generateRestRoutesAuditableItemGraph");
|
|
252
|
+
this.addRestRouteGenerator("auditableItemStreamComponent", coreConfig.types.auditableItemStreamComponent, "@twin.org/auditable-item-stream-service", "generateRestRoutesAuditableItemStream");
|
|
253
|
+
this.addRestRouteGenerator("entityStorageComponent", coreConfig.types.entityStorageComponent, "@twin.org/entity-storage-service", "generateRestRoutesEntityStorage");
|
|
500
254
|
}
|
|
501
255
|
/**
|
|
502
256
|
* Add the server socket route generators.
|
|
@@ -508,4 +262,159 @@ class EngineServer {
|
|
|
508
262
|
}
|
|
509
263
|
}
|
|
510
264
|
|
|
511
|
-
|
|
265
|
+
/**
|
|
266
|
+
* Handles the configuration of the server.
|
|
267
|
+
* @param envVars The environment variables.
|
|
268
|
+
* @param coreEngineConfig The core engine config.
|
|
269
|
+
* @param serverInfo The server information.
|
|
270
|
+
* @returns The the config for the core and the server.
|
|
271
|
+
*/
|
|
272
|
+
function buildEngineServerConfiguration(envVars, coreEngineConfig, serverInfo) {
|
|
273
|
+
envVars.authSigningKeyId ??= "auth-signing";
|
|
274
|
+
const webServerOptions = {
|
|
275
|
+
port: Coerce.number(envVars.port),
|
|
276
|
+
host: Coerce.string(envVars.host),
|
|
277
|
+
methods: Is.stringValue(envVars.httpMethods)
|
|
278
|
+
? envVars.httpMethods.split(",")
|
|
279
|
+
: undefined,
|
|
280
|
+
allowedHeaders: Is.stringValue(envVars.httpAllowedHeaders)
|
|
281
|
+
? envVars.httpAllowedHeaders.split(",")
|
|
282
|
+
: undefined,
|
|
283
|
+
exposedHeaders: Is.stringValue(envVars.httpExposedHeaders)
|
|
284
|
+
? envVars.httpExposedHeaders.split(",")
|
|
285
|
+
: undefined,
|
|
286
|
+
corsOrigins: Is.stringValue(envVars.corsOrigins) ? envVars.corsOrigins.split(",") : undefined
|
|
287
|
+
};
|
|
288
|
+
const authProcessorType = envVars.authProcessorType;
|
|
289
|
+
const serverConfig = {
|
|
290
|
+
...coreEngineConfig,
|
|
291
|
+
web: webServerOptions,
|
|
292
|
+
types: {
|
|
293
|
+
...coreEngineConfig.types,
|
|
294
|
+
informationComponent: [
|
|
295
|
+
{
|
|
296
|
+
type: InformationComponentType.Service,
|
|
297
|
+
options: {
|
|
298
|
+
config: {
|
|
299
|
+
serverInfo
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
],
|
|
304
|
+
restRouteProcessor: []
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
serverConfig.types.restRouteProcessor ??= [];
|
|
308
|
+
serverConfig.types.socketRouteProcessor ??= [];
|
|
309
|
+
serverConfig.types.restRouteProcessor.push({
|
|
310
|
+
type: RestRouteProcessorType.NodeIdentity
|
|
311
|
+
});
|
|
312
|
+
serverConfig.types.socketRouteProcessor.push({
|
|
313
|
+
type: SocketRouteProcessorType.NodeIdentity
|
|
314
|
+
});
|
|
315
|
+
if (!coreEngineConfig.silent) {
|
|
316
|
+
serverConfig.types.restRouteProcessor.push({
|
|
317
|
+
type: RestRouteProcessorType.Logging,
|
|
318
|
+
options: {
|
|
319
|
+
config: {
|
|
320
|
+
includeBody: coreEngineConfig.debug
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
serverConfig.types.socketRouteProcessor.push({
|
|
325
|
+
type: SocketRouteProcessorType.Logging,
|
|
326
|
+
options: {
|
|
327
|
+
config: {
|
|
328
|
+
includeBody: coreEngineConfig.debug
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
serverConfig.types.restRouteProcessor.push({
|
|
334
|
+
type: RestRouteProcessorType.RestRoute,
|
|
335
|
+
options: {
|
|
336
|
+
config: {
|
|
337
|
+
includeErrorStack: coreEngineConfig.debug
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
serverConfig.types.socketRouteProcessor.push({
|
|
342
|
+
type: SocketRouteProcessorType.SocketRoute,
|
|
343
|
+
options: {
|
|
344
|
+
config: {
|
|
345
|
+
includeErrorStack: coreEngineConfig.debug
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
if (authProcessorType === AuthenticationComponentType.EntityStorage) {
|
|
350
|
+
serverConfig.types.authenticationComponent ??= [];
|
|
351
|
+
serverConfig.types.authenticationComponent.push({
|
|
352
|
+
type: AuthenticationComponentType.EntityStorage,
|
|
353
|
+
options: {
|
|
354
|
+
config: {
|
|
355
|
+
signingKeyName: envVars.authSigningKeyId
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
serverConfig.types.restRouteProcessor.push({
|
|
360
|
+
type: RestRouteProcessorType.AuthHeader,
|
|
361
|
+
options: {
|
|
362
|
+
config: {
|
|
363
|
+
signingKeyName: envVars.authSigningKeyId
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
serverConfig.types.socketRouteProcessor.push({
|
|
368
|
+
type: SocketRouteProcessorType.AuthHeader,
|
|
369
|
+
options: {
|
|
370
|
+
config: {
|
|
371
|
+
signingKeyName: envVars.authSigningKeyId
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
addRestPaths(coreEngineConfig, serverConfig);
|
|
377
|
+
return serverConfig;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Adds the rest paths to the server config.
|
|
381
|
+
* @param coreEngineConfig The core engine config.
|
|
382
|
+
* @param serverConfig The server config.
|
|
383
|
+
*/
|
|
384
|
+
function addRestPaths(coreEngineConfig, serverConfig) {
|
|
385
|
+
if (Is.arrayValue(serverConfig.types.informationComponent)) {
|
|
386
|
+
serverConfig.types.informationComponent[0].restPath = "";
|
|
387
|
+
}
|
|
388
|
+
if (Is.arrayValue(serverConfig.types.authenticationComponent)) {
|
|
389
|
+
serverConfig.types.authenticationComponent[0].restPath = "authentication";
|
|
390
|
+
}
|
|
391
|
+
if (Is.arrayValue(coreEngineConfig.types.blobStorageComponent)) {
|
|
392
|
+
coreEngineConfig.types.blobStorageComponent[0].restPath = "blob";
|
|
393
|
+
}
|
|
394
|
+
if (Is.arrayValue(coreEngineConfig.types.loggingComponent)) {
|
|
395
|
+
coreEngineConfig.types.loggingComponent[0].restPath = "logging";
|
|
396
|
+
}
|
|
397
|
+
if (Is.arrayValue(coreEngineConfig.types.telemetryComponent)) {
|
|
398
|
+
coreEngineConfig.types.telemetryComponent[0].restPath = "telemetry";
|
|
399
|
+
}
|
|
400
|
+
if (Is.arrayValue(coreEngineConfig.types.identityComponent)) {
|
|
401
|
+
coreEngineConfig.types.identityComponent[0].restPath = "identity";
|
|
402
|
+
}
|
|
403
|
+
if (Is.arrayValue(coreEngineConfig.types.identityProfileComponent)) {
|
|
404
|
+
coreEngineConfig.types.identityProfileComponent[0].restPath = "identity/profile";
|
|
405
|
+
}
|
|
406
|
+
if (Is.arrayValue(coreEngineConfig.types.nftComponent)) {
|
|
407
|
+
coreEngineConfig.types.nftComponent[0].restPath = "nft";
|
|
408
|
+
}
|
|
409
|
+
if (Is.arrayValue(coreEngineConfig.types.attestationComponent)) {
|
|
410
|
+
coreEngineConfig.types.attestationComponent[0].restPath = "attestation";
|
|
411
|
+
}
|
|
412
|
+
if (Is.arrayValue(coreEngineConfig.types.auditableItemGraphComponent)) {
|
|
413
|
+
coreEngineConfig.types.auditableItemGraphComponent[0].restPath = "aig";
|
|
414
|
+
}
|
|
415
|
+
if (Is.arrayValue(coreEngineConfig.types.auditableItemStreamComponent)) {
|
|
416
|
+
coreEngineConfig.types.auditableItemStreamComponent[0].restPath = "ais";
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export { EngineServer, buildEngineServerConfiguration };
|