@twin.org/engine-server 0.0.1-next.4 → 0.0.1-next.41

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