@twin.org/engine-server 0.0.1-next.3 → 0.0.1-next.31

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.
@@ -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 engineModels = require('@twin.org/engine-models');
13
- var entityStorageService = require('@twin.org/entity-storage-service');
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,41 @@ 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
- const defaults = this._engineCore.getDefaultTypes();
313
- this._loggingConnectorType = coreConfig.silent ? undefined : defaults.loggingConnector;
314
- if (!core.Is.arrayValue(this._config.restRouteProcessor)) {
315
- this._config.restRouteProcessor = [];
54
+ if (!core.Is.arrayValue(coreConfig.types.restRouteProcessor)) {
55
+ coreConfig.types.restRouteProcessor = [];
56
+ if (!coreConfig.silent) {
57
+ coreConfig.types.restRouteProcessor.push({
58
+ type: engineServerTypes.RestRouteProcessorType.Logging,
59
+ options: {
60
+ config: {
61
+ includeBody: coreConfig.debug
62
+ }
63
+ }
64
+ });
65
+ }
66
+ coreConfig.types.restRouteProcessor.push({
67
+ type: engineServerTypes.RestRouteProcessorType.RestRoute,
68
+ options: {
69
+ config: {
70
+ includeErrorStack: coreConfig.debug
71
+ }
72
+ }
73
+ });
74
+ }
75
+ if (!core.Is.arrayValue(coreConfig.types.socketRouteProcessor)) {
76
+ coreConfig.types.socketRouteProcessor = [];
316
77
  if (!coreConfig.silent) {
317
- this._config.restRouteProcessor.push({
318
- type: engineModels.RestRouteProcessorType.Logging,
78
+ coreConfig.types.socketRouteProcessor.push({
79
+ type: engineServerTypes.SocketRouteProcessorType.Logging,
319
80
  options: {
320
81
  config: {
321
82
  includeBody: coreConfig.debug
@@ -323,8 +84,8 @@ class EngineServer {
323
84
  }
324
85
  });
325
86
  }
326
- this._config.restRouteProcessor.push({
327
- type: engineModels.RestRouteProcessorType.RestRoute,
87
+ coreConfig.types.socketRouteProcessor.push({
88
+ type: engineServerTypes.SocketRouteProcessorType.SocketRoute,
328
89
  options: {
329
90
  config: {
330
91
  includeErrorStack: coreConfig.debug
@@ -340,14 +101,16 @@ class EngineServer {
340
101
  * Add a REST route generator.
341
102
  * @param type The type to add the generator for.
342
103
  * @param typeConfig The type config.
343
- * @param generator The generator to add.
104
+ * @param module The module containing the generator.
105
+ * @param method The method to call on the module.
344
106
  */
345
- addRestRouteGenerator(type, typeConfig, generator) {
107
+ addRestRouteGenerator(type, typeConfig, module, method) {
346
108
  if (!core.Is.empty(typeConfig)) {
347
109
  this._restRouteGenerators.push({
348
110
  type,
349
111
  typeConfig,
350
- generator
112
+ module,
113
+ method
351
114
  });
352
115
  }
353
116
  }
@@ -355,24 +118,29 @@ class EngineServer {
355
118
  * Add a socket route generator.
356
119
  * @param type The type to add the generator for.
357
120
  * @param typeConfig The type config.
358
- * @param generator The generator to add.
121
+ * @param module The module containing the generator.
122
+ * @param method The method to call on the module.
359
123
  */
360
- addSocketRouteGenerator(type, typeConfig, generator) {
124
+ addSocketRouteGenerator(type, typeConfig, module, method) {
361
125
  if (!core.Is.empty(typeConfig)) {
362
126
  this._socketRouteGenerators.push({
363
127
  type,
364
128
  typeConfig,
365
- generator
129
+ module,
130
+ method
366
131
  });
367
132
  }
368
133
  }
369
134
  /**
370
135
  * Start the engine server.
371
- * @returns Nothing.
136
+ * @returns True if the start was successful.
372
137
  */
373
138
  async start() {
374
- await this._engineCore.start();
375
- await this.startWebServer();
139
+ const canContinue = await this._engineCore.start();
140
+ if (canContinue) {
141
+ await this.startWebServer();
142
+ }
143
+ return canContinue;
376
144
  }
377
145
  /**
378
146
  * Stop the engine server.
@@ -390,16 +158,19 @@ class EngineServer {
390
158
  * @internal
391
159
  */
392
160
  async startWebServer() {
393
- const restRoutes = this.buildRestRoutes();
394
- const socketRoutes = this.buildSocketRoutes();
161
+ const restRoutes = await this.buildRestRoutes();
162
+ const socketRoutes = await this.buildSocketRoutes();
395
163
  const restRouteProcessors = apiModels.RestRouteProcessorFactory.names().map(n => apiModels.RestRouteProcessorFactory.get(n));
396
164
  const socketRouteProcessors = apiModels.SocketRouteProcessorFactory.names().map(n => apiModels.SocketRouteProcessorFactory.get(n));
397
165
  const mimeTypeProcessors = apiModels.MimeTypeProcessorFactory.names().map(n => apiModels.MimeTypeProcessorFactory.get(n));
166
+ const coreConfig = this._engineCore.getConfig();
167
+ const defaults = this._engineCore.getDefaultTypes();
168
+ this._loggingConnectorType = coreConfig.silent ? undefined : defaults.loggingConnector;
398
169
  this._webServer = new apiServerFastify.FastifyWebServer({
399
170
  loggingConnectorType: this._loggingConnectorType,
400
171
  mimeTypeProcessors
401
172
  });
402
- await this._webServer.build(restRouteProcessors, restRoutes, socketRouteProcessors, socketRoutes, this._config.web);
173
+ await this._webServer.build(restRouteProcessors, restRoutes, socketRouteProcessors, socketRoutes, coreConfig.web);
403
174
  await this._webServer.start();
404
175
  }
405
176
  /**
@@ -407,10 +178,10 @@ class EngineServer {
407
178
  * @returns The REST routes for the application.
408
179
  * @internal
409
180
  */
410
- buildRestRoutes() {
181
+ async buildRestRoutes() {
411
182
  const routes = [];
412
- for (const { type, typeConfig, generator } of this._restRouteGenerators) {
413
- this.initialiseRestTypeRoute(routes, type, typeConfig, generator);
183
+ for (const { type, typeConfig, module, method } of this._restRouteGenerators) {
184
+ await this.initialiseRestTypeRoute(routes, type, typeConfig, module, method);
414
185
  }
415
186
  return routes;
416
187
  }
@@ -419,10 +190,10 @@ class EngineServer {
419
190
  * @returns The socket routes for the application.
420
191
  * @internal
421
192
  */
422
- buildSocketRoutes() {
193
+ async buildSocketRoutes() {
423
194
  const routes = [];
424
- for (const { type, typeConfig, generator } of this._socketRouteGenerators) {
425
- this.initialiseSocketTypeRoute(routes, type, typeConfig, generator);
195
+ for (const { type, typeConfig, module, method } of this._socketRouteGenerators) {
196
+ await this.initialiseSocketTypeRoute(routes, type, typeConfig, module, method);
426
197
  }
427
198
  return routes;
428
199
  }
@@ -434,9 +205,10 @@ class EngineServer {
434
205
  * @param generateRoutes The function to generate the routes.
435
206
  * @internal
436
207
  */
437
- initialiseRestTypeRoute(routes, typeKey, typeConfig, generateRoutes) {
208
+ async initialiseRestTypeRoute(routes, typeKey, typeConfig, module, method) {
438
209
  if (core.Is.arrayValue(typeConfig)) {
439
210
  const defaultEngineTypes = this._engineCore.getDefaultTypes();
211
+ const generateRoutes = await modules.ModuleHelper.getModuleEntry(module, method);
440
212
  for (let i = 0; i < typeConfig.length; i++) {
441
213
  const restPath = typeConfig[i].restPath;
442
214
  if (core.Is.string(restPath)) {
@@ -453,12 +225,14 @@ class EngineServer {
453
225
  * @param routes The routes to add to.
454
226
  * @param typeKey The key for the default types.
455
227
  * @param typeConfig The type config.
456
- * @param generateRoutes The function to generate the routes.
228
+ * @param module The module containing the generator.
229
+ * @param method The method to call on the module.
457
230
  * @internal
458
231
  */
459
- initialiseSocketTypeRoute(routes, typeKey, typeConfig, generateRoutes) {
232
+ async initialiseSocketTypeRoute(routes, typeKey, typeConfig, module, method) {
460
233
  if (core.Is.arrayValue(typeConfig)) {
461
234
  const defaultEngineTypes = this._engineCore.getDefaultTypes();
235
+ const generateRoutes = await modules.ModuleHelper.getModuleEntry(module, method);
462
236
  for (let i = 0; i < typeConfig.length; i++) {
463
237
  const socketPath = typeConfig[i].socketPath;
464
238
  if (core.Is.string(socketPath)) {
@@ -475,39 +249,222 @@ class EngineServer {
475
249
  * @internal
476
250
  */
477
251
  addServerTypeInitialisers() {
478
- this._engineCore.addTypeInitialiser("authenticationComponent", this._config.authenticationComponent, initialiseAuthenticationComponent);
479
- this._engineCore.addTypeInitialiser("informationComponent", this._config.informationComponent, initialiseInformationComponent);
480
- this._engineCore.addTypeInitialiser("restRouteProcessor", this._config.restRouteProcessor, initialiseRestRouteProcessorComponent);
481
- this._engineCore.addTypeInitialiser("socketRouteProcessor", this._config.socketRouteProcessor, initialiseSocketRouteProcessorComponent);
482
- this._engineCore.addTypeInitialiser("mimeTypeProcessor", this._config.mimeTypeProcessor, initialiseMimeTypeProcessorComponent);
252
+ const coreConfig = this._engineCore.getConfig();
253
+ this._engineCore.addTypeInitialiser("authenticationComponent", coreConfig.types.authenticationComponent, "@twin.org/engine-server-types", "initialiseAuthenticationComponent");
254
+ this._engineCore.addTypeInitialiser("informationComponent", coreConfig.types.informationComponent, "@twin.org/engine-server-types", "initialiseInformationComponent");
255
+ this._engineCore.addTypeInitialiser("restRouteProcessor", coreConfig.types.restRouteProcessor, "@twin.org/engine-server-types", "initialiseRestRouteProcessorComponent");
256
+ this._engineCore.addTypeInitialiser("socketRouteProcessor", coreConfig.types.socketRouteProcessor, "@twin.org/engine-server-types", "initialiseSocketRouteProcessorComponent");
257
+ this._engineCore.addTypeInitialiser("mimeTypeProcessor", coreConfig.types.mimeTypeProcessor, "@twin.org/engine-server-types", "initialiseMimeTypeProcessorComponent");
483
258
  }
484
259
  /**
485
260
  * Add the server REST route generators.
486
261
  * @internal
487
262
  */
488
263
  addServerRestRouteGenerators() {
489
- this.addRestRouteGenerator("informationComponent", this._config.informationComponent, apiService.generateRestRoutesInformation);
490
- this.addRestRouteGenerator("authenticationComponent", this._config.authenticationComponent, apiAuthEntityStorageService.generateRestRoutesAuthentication);
491
264
  const coreConfig = this._engineCore.getConfig();
492
- this.addRestRouteGenerator("loggingComponent", coreConfig.loggingComponent, blobStorageService.generateRestRoutesBlobStorage);
493
- this.addRestRouteGenerator("telemetryComponent", coreConfig.telemetryComponent, telemetryService.generateRestRoutesTelemetry);
494
- this.addRestRouteGenerator("blobStorageComponent", coreConfig.blobStorageComponent, loggingService.generateRestRoutesLogging);
495
- this.addRestRouteGenerator("identityComponent", coreConfig.identityComponent, identityService.generateRestRoutesIdentity);
496
- this.addRestRouteGenerator("identityProfileComponent", coreConfig.identityProfileComponent, identityService.generateRestRoutesIdentityProfile);
497
- this.addRestRouteGenerator("nftComponent", coreConfig.nftComponent, nftService.generateRestRoutesNft);
498
- this.addRestRouteGenerator("attestationComponent", coreConfig.attestationComponent, attestationService.generateRestRoutesAttestation);
499
- this.addRestRouteGenerator("auditableItemGraphComponent", coreConfig.auditableItemGraphComponent, auditableItemGraphService.generateRestRoutesAuditableItemGraph);
500
- this.addRestRouteGenerator("auditableItemStreamComponent", coreConfig.auditableItemStreamComponent, auditableItemStreamService.generateRestRoutesAuditableItemStream);
501
- this.addRestRouteGenerator("entityStorageComponent", coreConfig.entityStorageComponent, entityStorageService.generateRestRoutesEntityStorage);
265
+ this.addRestRouteGenerator("informationComponent", coreConfig.types.informationComponent, "@twin.org/api-service", "generateRestRoutesInformation");
266
+ this.addRestRouteGenerator("authenticationComponent", coreConfig.types.authenticationComponent, "@twin.org/api-auth-entity-storage-service", "generateRestRoutesAuthentication");
267
+ this.addRestRouteGenerator("loggingComponent", coreConfig.types.loggingComponent, "@twin.org/logging-service", "generateRestRoutesLogging");
268
+ this.addRestRouteGenerator("telemetryComponent", coreConfig.types.telemetryComponent, "@twin.org/telemetry-service", "generateRestRoutesTelemetry");
269
+ this.addRestRouteGenerator("blobStorageComponent", coreConfig.types.blobStorageComponent, "@twin.org/blob-storage-service", "generateRestRoutesBlobStorage");
270
+ this.addRestRouteGenerator("identityComponent", coreConfig.types.identityComponent, "@twin.org/identity-service", "generateRestRoutesIdentity");
271
+ this.addRestRouteGenerator("identityProfileComponent", coreConfig.types.identityProfileComponent, "@twin.org/identity-service", "generateRestRoutesIdentityProfile");
272
+ this.addRestRouteGenerator("nftComponent", coreConfig.types.nftComponent, "@twin.org/nft-service", "generateRestRoutesNft");
273
+ this.addRestRouteGenerator("attestationComponent", coreConfig.types.attestationComponent, "@twin.org/attestation-service", "generateRestRoutesAttestation");
274
+ this.addRestRouteGenerator("immutableProofComponent", coreConfig.types.immutableProofComponent, "@twin.org/immutable-proof-service", "generateRestRoutesImmutableProof");
275
+ this.addRestRouteGenerator("auditableItemGraphComponent", coreConfig.types.auditableItemGraphComponent, "@twin.org/auditable-item-graph-service", "generateRestRoutesAuditableItemGraph");
276
+ this.addRestRouteGenerator("auditableItemStreamComponent", coreConfig.types.auditableItemStreamComponent, "@twin.org/auditable-item-stream-service", "generateRestRoutesAuditableItemStream");
277
+ this.addRestRouteGenerator("entityStorageComponent", coreConfig.types.entityStorageComponent, "@twin.org/entity-storage-service", "generateRestRoutesEntityStorage");
502
278
  }
503
279
  /**
504
280
  * Add the server socket route generators.
505
281
  * @internal
506
282
  */
507
283
  addServerSocketRouteGenerators() {
508
- // const coreConfig = this._engineCore.getConfig();
509
- // this.addSocketRouteGenerator("eventBusComponent", coreConfig.eventBusComponent, generateSocketRoutesEventBus);
284
+ const coreConfig = this._engineCore.getConfig();
285
+ this.addSocketRouteGenerator("eventBusComponent", coreConfig.types.eventBusComponent, "@twin.org/event-bus-service", "generateSocketRoutesEventBus");
286
+ }
287
+ }
288
+
289
+ /**
290
+ * Handles the configuration of the server.
291
+ * @param envVars The environment variables.
292
+ * @param coreEngineConfig The core engine config.
293
+ * @param serverInfo The server information.
294
+ * @returns The the config for the core and the server.
295
+ */
296
+ function buildEngineServerConfiguration(envVars, coreEngineConfig, serverInfo) {
297
+ envVars.authSigningKeyId ??= "auth-signing";
298
+ const webServerOptions = {
299
+ port: core.Coerce.number(envVars.port),
300
+ host: core.Coerce.string(envVars.host),
301
+ methods: core.Is.stringValue(envVars.httpMethods)
302
+ ? envVars.httpMethods.split(",")
303
+ : undefined,
304
+ allowedHeaders: core.Is.stringValue(envVars.httpAllowedHeaders)
305
+ ? envVars.httpAllowedHeaders.split(",")
306
+ : undefined,
307
+ exposedHeaders: core.Is.stringValue(envVars.httpExposedHeaders)
308
+ ? envVars.httpExposedHeaders.split(",")
309
+ : undefined,
310
+ corsOrigins: core.Is.stringValue(envVars.corsOrigins) ? envVars.corsOrigins.split(",") : undefined
311
+ };
312
+ const authProcessorType = envVars.authProcessorType;
313
+ const serverConfig = {
314
+ ...coreEngineConfig,
315
+ web: webServerOptions,
316
+ types: {
317
+ ...coreEngineConfig.types,
318
+ informationComponent: [
319
+ {
320
+ type: engineServerTypes.InformationComponentType.Service,
321
+ options: {
322
+ config: {
323
+ serverInfo
324
+ }
325
+ }
326
+ }
327
+ ]
328
+ }
329
+ };
330
+ serverConfig.types.restRouteProcessor ??= [];
331
+ serverConfig.types.socketRouteProcessor ??= [];
332
+ serverConfig.types.restRouteProcessor.push({
333
+ type: engineServerTypes.RestRouteProcessorType.NodeIdentity
334
+ });
335
+ serverConfig.types.socketRouteProcessor.push({
336
+ type: engineServerTypes.SocketRouteProcessorType.NodeIdentity
337
+ });
338
+ if (!coreEngineConfig.silent) {
339
+ serverConfig.types.restRouteProcessor.push({
340
+ type: engineServerTypes.RestRouteProcessorType.Logging,
341
+ options: {
342
+ config: {
343
+ includeBody: coreEngineConfig.debug
344
+ }
345
+ }
346
+ });
347
+ serverConfig.types.socketRouteProcessor.push({
348
+ type: engineServerTypes.SocketRouteProcessorType.Logging,
349
+ options: {
350
+ config: {
351
+ includeBody: coreEngineConfig.debug
352
+ }
353
+ }
354
+ });
355
+ }
356
+ serverConfig.types.restRouteProcessor.push({
357
+ type: engineServerTypes.RestRouteProcessorType.RestRoute,
358
+ options: {
359
+ config: {
360
+ includeErrorStack: coreEngineConfig.debug
361
+ }
362
+ }
363
+ });
364
+ serverConfig.types.socketRouteProcessor.push({
365
+ type: engineServerTypes.SocketRouteProcessorType.SocketRoute,
366
+ options: {
367
+ config: {
368
+ includeErrorStack: coreEngineConfig.debug
369
+ }
370
+ }
371
+ });
372
+ if (authProcessorType === engineServerTypes.AuthenticationComponentType.EntityStorage) {
373
+ serverConfig.types.authenticationComponent ??= [];
374
+ serverConfig.types.authenticationComponent.push({
375
+ type: engineServerTypes.AuthenticationComponentType.EntityStorage,
376
+ options: {
377
+ config: {
378
+ signingKeyName: envVars.authSigningKeyId
379
+ }
380
+ }
381
+ });
382
+ serverConfig.types.restRouteProcessor.push({
383
+ type: engineServerTypes.RestRouteProcessorType.AuthHeader,
384
+ options: {
385
+ config: {
386
+ signingKeyName: envVars.authSigningKeyId
387
+ }
388
+ }
389
+ });
390
+ serverConfig.types.socketRouteProcessor.push({
391
+ type: engineServerTypes.SocketRouteProcessorType.AuthHeader,
392
+ options: {
393
+ config: {
394
+ signingKeyName: envVars.authSigningKeyId
395
+ }
396
+ }
397
+ });
398
+ }
399
+ addRestPaths(coreEngineConfig, serverConfig);
400
+ addSocketPaths(coreEngineConfig);
401
+ return serverConfig;
402
+ }
403
+ /**
404
+ * Adds the rest paths to the server config.
405
+ * @param coreEngineConfig The core engine config.
406
+ * @param serverConfig The server config.
407
+ */
408
+ function addRestPaths(coreEngineConfig, serverConfig) {
409
+ if (core.Is.arrayValue(serverConfig.types.informationComponent)) {
410
+ serverConfig.types.informationComponent[0].restPath = "";
411
+ }
412
+ if (core.Is.arrayValue(serverConfig.types.authenticationComponent) &&
413
+ !core.Is.stringValue(serverConfig.types.authenticationComponent[0].restPath)) {
414
+ serverConfig.types.authenticationComponent[0].restPath = "authentication";
415
+ }
416
+ if (core.Is.arrayValue(coreEngineConfig.types.blobStorageComponent) &&
417
+ !core.Is.stringValue(coreEngineConfig.types.blobStorageComponent[0].restPath)) {
418
+ coreEngineConfig.types.blobStorageComponent[0].restPath = "blob";
419
+ }
420
+ if (core.Is.arrayValue(coreEngineConfig.types.loggingComponent) &&
421
+ !core.Is.stringValue(coreEngineConfig.types.loggingComponent[0].restPath)) {
422
+ coreEngineConfig.types.loggingComponent[0].restPath = "logging";
423
+ }
424
+ if (core.Is.arrayValue(coreEngineConfig.types.telemetryComponent) &&
425
+ !core.Is.stringValue(coreEngineConfig.types.telemetryComponent[0].restPath)) {
426
+ coreEngineConfig.types.telemetryComponent[0].restPath = "telemetry";
427
+ }
428
+ if (core.Is.arrayValue(coreEngineConfig.types.identityComponent) &&
429
+ !core.Is.stringValue(coreEngineConfig.types.identityComponent[0].restPath)) {
430
+ coreEngineConfig.types.identityComponent[0].restPath = "identity";
431
+ }
432
+ if (core.Is.arrayValue(coreEngineConfig.types.identityProfileComponent) &&
433
+ !core.Is.stringValue(coreEngineConfig.types.identityProfileComponent[0].restPath)) {
434
+ coreEngineConfig.types.identityProfileComponent[0].restPath = "identity/profile";
435
+ }
436
+ if (core.Is.arrayValue(coreEngineConfig.types.nftComponent) &&
437
+ !core.Is.stringValue(coreEngineConfig.types.nftComponent[0].restPath)) {
438
+ coreEngineConfig.types.nftComponent[0].restPath = "nft";
439
+ }
440
+ if (core.Is.arrayValue(coreEngineConfig.types.immutableProofComponent) &&
441
+ !core.Is.stringValue(coreEngineConfig.types.immutableProofComponent[0].restPath)) {
442
+ coreEngineConfig.types.immutableProofComponent[0].restPath = "immutable-proof";
443
+ }
444
+ if (core.Is.arrayValue(coreEngineConfig.types.attestationComponent) &&
445
+ !core.Is.stringValue(coreEngineConfig.types.attestationComponent[0].restPath)) {
446
+ coreEngineConfig.types.attestationComponent[0].restPath = "attestation";
447
+ }
448
+ if (core.Is.arrayValue(coreEngineConfig.types.auditableItemGraphComponent) &&
449
+ !core.Is.stringValue(coreEngineConfig.types.auditableItemGraphComponent[0].restPath)) {
450
+ coreEngineConfig.types.auditableItemGraphComponent[0].restPath = "aig";
451
+ }
452
+ if (core.Is.arrayValue(coreEngineConfig.types.auditableItemStreamComponent) &&
453
+ !core.Is.stringValue(coreEngineConfig.types.auditableItemStreamComponent[0].restPath)) {
454
+ coreEngineConfig.types.auditableItemStreamComponent[0].restPath = "ais";
455
+ }
456
+ }
457
+ /**
458
+ * Adds the socket paths to the server config.
459
+ * @param coreEngineConfig The core engine config.
460
+ * @param serverConfig The server config.
461
+ */
462
+ function addSocketPaths(coreEngineConfig, serverConfig) {
463
+ if (core.Is.arrayValue(coreEngineConfig.types.eventBusComponent) &&
464
+ !core.Is.stringValue(coreEngineConfig.types.eventBusComponent[0].socketPath)) {
465
+ coreEngineConfig.types.eventBusComponent[0].socketPath = "event-bus";
510
466
  }
511
467
  }
512
468
 
513
469
  exports.EngineServer = EngineServer;
470
+ exports.buildEngineServerConfiguration = buildEngineServerConfiguration;