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