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