@twin.org/engine-server 0.0.2-next.9 → 0.0.3-next.2

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,427 +0,0 @@
1
- 'use strict';
2
-
3
- var apiModels = require('@twin.org/api-models');
4
- var apiServerFastify = require('@twin.org/api-server-fastify');
5
- var core = require('@twin.org/core');
6
- var engineServerTypes = require('@twin.org/engine-server-types');
7
- var modules = require('@twin.org/modules');
8
-
9
- /**
10
- * Server for the engine.
11
- */
12
- class EngineServer {
13
- /**
14
- * Runtime name for the class.
15
- */
16
- CLASS_NAME = "EngineServer";
17
- /**
18
- * The engine.
19
- * @internal
20
- */
21
- _engineCore;
22
- /**
23
- * The REST route generators.
24
- * @internal
25
- */
26
- _restRouteGenerators;
27
- /**
28
- * The socket route generators.
29
- * @internal
30
- */
31
- _socketRouteGenerators;
32
- /**
33
- * The web server.
34
- * @internal
35
- */
36
- _webServer;
37
- /**
38
- * The logging component type.
39
- * @internal
40
- */
41
- _loggingComponentType;
42
- /**
43
- * The REST routes for the application.
44
- * @internal
45
- */
46
- _restRoutes;
47
- /**
48
- * The Socket routes for the application.
49
- * @internal
50
- */
51
- _socketRoutes;
52
- /**
53
- * Create a new instance of EngineServer.
54
- * @param options The options for the engine.
55
- * @param options.engineCore The engine core to serve from.
56
- */
57
- constructor(options) {
58
- core.Guards.object(this.CLASS_NAME, "options", options);
59
- core.Guards.object(this.CLASS_NAME, "options.engineCore", options.engineCore);
60
- this._engineCore = options.engineCore;
61
- this._restRouteGenerators = [];
62
- this._socketRouteGenerators = [];
63
- this._restRoutes = [];
64
- this._socketRoutes = [];
65
- const coreConfig = this._engineCore.getConfig();
66
- if (!core.Is.arrayValue(coreConfig.types.restRouteProcessor)) {
67
- coreConfig.types.restRouteProcessor = [];
68
- if (!coreConfig.silent) {
69
- coreConfig.types.restRouteProcessor.push({
70
- type: engineServerTypes.RestRouteProcessorType.Logging,
71
- options: {
72
- config: {
73
- includeBody: coreConfig.debug
74
- }
75
- }
76
- });
77
- }
78
- coreConfig.types.restRouteProcessor.push({
79
- type: engineServerTypes.RestRouteProcessorType.RestRoute,
80
- options: {
81
- config: {
82
- includeErrorStack: coreConfig.debug
83
- }
84
- }
85
- });
86
- }
87
- if (!core.Is.arrayValue(coreConfig.types.socketRouteProcessor)) {
88
- coreConfig.types.socketRouteProcessor = [];
89
- if (!coreConfig.silent) {
90
- coreConfig.types.socketRouteProcessor.push({
91
- type: engineServerTypes.SocketRouteProcessorType.Logging,
92
- options: {
93
- config: {
94
- includeBody: coreConfig.debug
95
- }
96
- }
97
- });
98
- }
99
- coreConfig.types.socketRouteProcessor.push({
100
- type: engineServerTypes.SocketRouteProcessorType.SocketRoute,
101
- options: {
102
- config: {
103
- includeErrorStack: coreConfig.debug
104
- }
105
- }
106
- });
107
- }
108
- this.addServerTypeInitialisers();
109
- this.addServerRestRouteGenerators();
110
- this.addServerSocketRouteGenerators();
111
- }
112
- /**
113
- * Add a REST route generator.
114
- * @param type The type to add the generator for.
115
- * @param typeConfig The type config.
116
- * @param module The module containing the generator.
117
- * @param method The method to call on the module.
118
- */
119
- addRestRouteGenerator(type, typeConfig, module, method) {
120
- if (!core.Is.empty(typeConfig)) {
121
- this._restRouteGenerators.push({
122
- type,
123
- typeConfig,
124
- module,
125
- method
126
- });
127
- }
128
- }
129
- /**
130
- * Add a socket route generator.
131
- * @param type The type to add the generator for.
132
- * @param typeConfig The type config.
133
- * @param module The module containing the generator.
134
- * @param method The method to call on the module.
135
- */
136
- addSocketRouteGenerator(type, typeConfig, module, method) {
137
- if (!core.Is.empty(typeConfig)) {
138
- this._socketRouteGenerators.push({
139
- type,
140
- typeConfig,
141
- module,
142
- method
143
- });
144
- }
145
- }
146
- /**
147
- * Get the built REST routes.
148
- * @returns The REST routes.
149
- */
150
- getRestRoutes() {
151
- return this._restRoutes;
152
- }
153
- /**
154
- * Get the built socket routes.
155
- * @returns The socket routes.
156
- */
157
- getSocketRoutes() {
158
- return this._socketRoutes;
159
- }
160
- /**
161
- * Start the engine server.
162
- * @returns True if the start was successful.
163
- */
164
- async start() {
165
- const canContinue = await this._engineCore.start();
166
- if (canContinue) {
167
- await this.startWebServer();
168
- }
169
- return canContinue;
170
- }
171
- /**
172
- * Stop the engine server.
173
- * @returns Nothing.
174
- */
175
- async stop() {
176
- if (this._webServer) {
177
- await this._webServer.stop();
178
- this._webServer = undefined;
179
- }
180
- await this._engineCore.stop();
181
- }
182
- /**
183
- * Starts the web server.
184
- * @internal
185
- */
186
- async startWebServer() {
187
- this._restRoutes = await this.buildRestRoutes();
188
- this._socketRoutes = await this.buildSocketRoutes();
189
- const restRouteProcessors = apiModels.RestRouteProcessorFactory.names().map(n => apiModels.RestRouteProcessorFactory.get(n));
190
- const socketRouteProcessors = apiModels.SocketRouteProcessorFactory.names().map(n => apiModels.SocketRouteProcessorFactory.get(n));
191
- const mimeTypeProcessors = apiModels.MimeTypeProcessorFactory.names().map(n => apiModels.MimeTypeProcessorFactory.get(n));
192
- const coreConfig = this._engineCore.getConfig();
193
- this._loggingComponentType = coreConfig.silent
194
- ? undefined
195
- : this._engineCore.getRegisteredInstanceType("loggingComponent");
196
- this._webServer = new apiServerFastify.FastifyWebServer({
197
- loggingComponentType: this._loggingComponentType,
198
- mimeTypeProcessors
199
- });
200
- await this._webServer.build(restRouteProcessors, this._restRoutes, socketRouteProcessors, this._socketRoutes, coreConfig.web);
201
- await this._webServer.start();
202
- }
203
- /**
204
- * The REST routes for the application.
205
- * @returns The REST routes for the application.
206
- * @internal
207
- */
208
- async buildRestRoutes() {
209
- const routes = [];
210
- for (const { type, typeConfig, module, method } of this._restRouteGenerators) {
211
- await this.initialiseRestTypeRoute(routes, type, typeConfig, module, method);
212
- }
213
- return routes;
214
- }
215
- /**
216
- * The socket routes for the application.
217
- * @returns The socket routes for the application.
218
- * @internal
219
- */
220
- async buildSocketRoutes() {
221
- const routes = [];
222
- for (const { type, typeConfig, module, method } of this._socketRouteGenerators) {
223
- await this.initialiseSocketTypeRoute(routes, type, typeConfig, module, method);
224
- }
225
- return routes;
226
- }
227
- /**
228
- * Initialise the rest routes from connector.
229
- * @param routes The routes to add to.
230
- * @param typeKey The key for the default types.
231
- * @param typeConfig The type config.
232
- * @param generateRoutes The function to generate the routes.
233
- * @internal
234
- */
235
- async initialiseRestTypeRoute(routes, typeKey, typeConfig, module, method) {
236
- if (core.Is.arrayValue(typeConfig)) {
237
- const generateRoutes = await modules.ModuleHelper.getModuleEntry(module, method);
238
- for (let i = 0; i < typeConfig.length; i++) {
239
- const restPath = typeConfig[i].restPath;
240
- if (core.Is.string(restPath)) {
241
- const serviceType = typeConfig[i].overrideInstanceType ??
242
- this._engineCore.getRegisteredInstanceType(typeKey);
243
- if (core.Is.stringValue(serviceType)) {
244
- const generatedRoutes = generateRoutes(restPath, serviceType);
245
- for (const route of generatedRoutes) {
246
- // Don't strip trailing slashes from the root path.
247
- if (core.Is.stringValue(route.path) && route.path.length > 1) {
248
- route.path = core.StringHelper.trimTrailingSlashes(route.path);
249
- }
250
- }
251
- routes.push(...generatedRoutes);
252
- }
253
- }
254
- }
255
- }
256
- }
257
- /**
258
- * Initialise the socket routes from connector.
259
- * @param routes The routes to add to.
260
- * @param typeKey The key for the default types.
261
- * @param typeConfig The type config.
262
- * @param module The module containing the generator.
263
- * @param method The method to call on the module.
264
- * @internal
265
- */
266
- async initialiseSocketTypeRoute(routes, typeKey, typeConfig, module, method) {
267
- if (core.Is.arrayValue(typeConfig)) {
268
- const generateRoutes = await modules.ModuleHelper.getModuleEntry(module, method);
269
- for (let i = 0; i < typeConfig.length; i++) {
270
- const socketPath = typeConfig[i].socketPath;
271
- if (core.Is.string(socketPath)) {
272
- const serviceType = typeConfig[i].overrideInstanceType ??
273
- this._engineCore.getRegisteredInstanceType(typeKey);
274
- if (core.Is.stringValue(serviceType)) {
275
- routes.push(...generateRoutes(socketPath, serviceType));
276
- }
277
- }
278
- }
279
- }
280
- }
281
- /**
282
- * Add the server type initializers.
283
- * @internal
284
- */
285
- addServerTypeInitialisers() {
286
- const coreConfig = this._engineCore.getConfig();
287
- this._engineCore.addTypeInitialiser("authenticationAdminComponent", coreConfig.types.authenticationAdminComponent, "@twin.org/engine-server-types", "initialiseAuthenticationAdminComponent");
288
- this._engineCore.addTypeInitialiser("authenticationComponent", coreConfig.types.authenticationComponent, "@twin.org/engine-server-types", "initialiseAuthenticationComponent");
289
- this._engineCore.addTypeInitialiser("informationComponent", coreConfig.types.informationComponent, "@twin.org/engine-server-types", "initialiseInformationComponent");
290
- this._engineCore.addTypeInitialiser("restRouteProcessor", coreConfig.types.restRouteProcessor, "@twin.org/engine-server-types", "initialiseRestRouteProcessorComponent");
291
- this._engineCore.addTypeInitialiser("socketRouteProcessor", coreConfig.types.socketRouteProcessor, "@twin.org/engine-server-types", "initialiseSocketRouteProcessorComponent");
292
- this._engineCore.addTypeInitialiser("mimeTypeProcessor", coreConfig.types.mimeTypeProcessor, "@twin.org/engine-server-types", "initialiseMimeTypeProcessorComponent");
293
- }
294
- /**
295
- * Add the server REST route generators.
296
- * @internal
297
- */
298
- addServerRestRouteGenerators() {
299
- const coreConfig = this._engineCore.getConfig();
300
- this.addRestRouteGenerator("informationComponent", coreConfig.types.informationComponent, "@twin.org/api-service", "generateRestRoutesInformation");
301
- this.addRestRouteGenerator("authenticationComponent", coreConfig.types.authenticationComponent, "@twin.org/api-auth-entity-storage-service", "generateRestRoutesAuthentication");
302
- this.addRestRouteGenerator("loggingComponent", coreConfig.types.loggingComponent, "@twin.org/logging-service", "generateRestRoutesLogging");
303
- this.addRestRouteGenerator("telemetryComponent", coreConfig.types.telemetryComponent, "@twin.org/telemetry-service", "generateRestRoutesTelemetry");
304
- this.addRestRouteGenerator("blobStorageComponent", coreConfig.types.blobStorageComponent, "@twin.org/blob-storage-service", "generateRestRoutesBlobStorage");
305
- this.addRestRouteGenerator("identityComponent", coreConfig.types.identityComponent, "@twin.org/identity-service", "generateRestRoutesIdentity");
306
- this.addRestRouteGenerator("identityResolverComponent", coreConfig.types.identityResolverComponent, "@twin.org/identity-service", "generateRestRoutesIdentityResolver");
307
- this.addRestRouteGenerator("identityProfileComponent", coreConfig.types.identityProfileComponent, "@twin.org/identity-service", "generateRestRoutesIdentityProfile");
308
- this.addRestRouteGenerator("nftComponent", coreConfig.types.nftComponent, "@twin.org/nft-service", "generateRestRoutesNft");
309
- this.addRestRouteGenerator("verifiableStorageComponent", coreConfig.types.verifiableStorageComponent, "@twin.org/verifiable-storage-service", "generateRestRoutesVerifiableStorage");
310
- this.addRestRouteGenerator("attestationComponent", coreConfig.types.attestationComponent, "@twin.org/attestation-service", "generateRestRoutesAttestation");
311
- this.addRestRouteGenerator("immutableProofComponent", coreConfig.types.immutableProofComponent, "@twin.org/immutable-proof-service", "generateRestRoutesImmutableProof");
312
- this.addRestRouteGenerator("auditableItemGraphComponent", coreConfig.types.auditableItemGraphComponent, "@twin.org/auditable-item-graph-service", "generateRestRoutesAuditableItemGraph");
313
- this.addRestRouteGenerator("auditableItemStreamComponent", coreConfig.types.auditableItemStreamComponent, "@twin.org/auditable-item-stream-service", "generateRestRoutesAuditableItemStream");
314
- this.addRestRouteGenerator("entityStorageComponent", coreConfig.types.entityStorageComponent, "@twin.org/entity-storage-service", "generateRestRoutesEntityStorage");
315
- this.addRestRouteGenerator("dataProcessingComponent", coreConfig.types.dataProcessingComponent, "@twin.org/data-processing-service", "generateRestRoutesDataProcessing");
316
- this.addRestRouteGenerator("documentManagementComponent", coreConfig.types.documentManagementComponent, "@twin.org/document-management-service", "generateRestRoutesDocumentManagement");
317
- this.addRestRouteGenerator("rightsManagementComponent", coreConfig.types.rightsManagementComponent, "@twin.org/rights-management-service", "generateRestRoutesRightsManagement");
318
- this.addRestRouteGenerator("synchronisedStorageComponent", coreConfig.types.synchronisedStorageComponent, "@twin.org/synchronised-storage-service", "generateRestRoutesSynchronisedStorage");
319
- this.addRestRouteGenerator("federatedCatalogueComponent", coreConfig.types.federatedCatalogueComponent, "@twin.org/federated-catalogue-service", "generateRestRoutesFederatedCatalogue");
320
- }
321
- /**
322
- * Add the server socket route generators.
323
- * @internal
324
- */
325
- addServerSocketRouteGenerators() {
326
- const coreConfig = this._engineCore.getConfig();
327
- this.addSocketRouteGenerator("eventBusComponent", coreConfig.types.eventBusComponent, "@twin.org/event-bus-service", "generateSocketRoutesEventBus");
328
- }
329
- }
330
-
331
- // Copyright 2024 IOTA Stiftung.
332
- // SPDX-License-Identifier: Apache-2.0.
333
- /**
334
- * Adds the rest paths to the server config.
335
- * @param serverConfig The server config.
336
- */
337
- function addDefaultRestPaths(serverConfig) {
338
- if (core.Is.arrayValue(serverConfig.types.informationComponent)) {
339
- serverConfig.types.informationComponent[0].restPath = "";
340
- }
341
- if (core.Is.arrayValue(serverConfig.types.authenticationComponent) &&
342
- !core.Is.stringValue(serverConfig.types.authenticationComponent[0].restPath)) {
343
- serverConfig.types.authenticationComponent[0].restPath = "/authentication";
344
- }
345
- if (core.Is.arrayValue(serverConfig.types.blobStorageComponent) &&
346
- !core.Is.stringValue(serverConfig.types.blobStorageComponent[0].restPath)) {
347
- serverConfig.types.blobStorageComponent[0].restPath = "/blob";
348
- }
349
- if (core.Is.arrayValue(serverConfig.types.loggingComponent) &&
350
- !core.Is.stringValue(serverConfig.types.loggingComponent[0].restPath)) {
351
- serverConfig.types.loggingComponent[0].restPath = "/logging";
352
- }
353
- if (core.Is.arrayValue(serverConfig.types.telemetryComponent) &&
354
- !core.Is.stringValue(serverConfig.types.telemetryComponent[0].restPath)) {
355
- serverConfig.types.telemetryComponent[0].restPath = "/telemetry";
356
- }
357
- if (core.Is.arrayValue(serverConfig.types.identityComponent) &&
358
- !core.Is.stringValue(serverConfig.types.identityComponent[0].restPath)) {
359
- serverConfig.types.identityComponent[0].restPath = "/identity";
360
- }
361
- if (core.Is.arrayValue(serverConfig.types.identityResolverComponent) &&
362
- !core.Is.stringValue(serverConfig.types.identityResolverComponent[0].restPath)) {
363
- serverConfig.types.identityResolverComponent[0].restPath = "/identity";
364
- }
365
- if (core.Is.arrayValue(serverConfig.types.identityProfileComponent) &&
366
- !core.Is.stringValue(serverConfig.types.identityProfileComponent[0].restPath)) {
367
- serverConfig.types.identityProfileComponent[0].restPath = "/identity/profile";
368
- }
369
- if (core.Is.arrayValue(serverConfig.types.nftComponent) &&
370
- !core.Is.stringValue(serverConfig.types.nftComponent[0].restPath)) {
371
- serverConfig.types.nftComponent[0].restPath = "/nft";
372
- }
373
- if (core.Is.arrayValue(serverConfig.types.verifiableStorageComponent) &&
374
- !core.Is.stringValue(serverConfig.types.verifiableStorageComponent[0].restPath)) {
375
- serverConfig.types.verifiableStorageComponent[0].restPath = "/verifiable";
376
- }
377
- if (core.Is.arrayValue(serverConfig.types.immutableProofComponent) &&
378
- !core.Is.stringValue(serverConfig.types.immutableProofComponent[0].restPath)) {
379
- serverConfig.types.immutableProofComponent[0].restPath = "/immutable-proof";
380
- }
381
- if (core.Is.arrayValue(serverConfig.types.attestationComponent) &&
382
- !core.Is.stringValue(serverConfig.types.attestationComponent[0].restPath)) {
383
- serverConfig.types.attestationComponent[0].restPath = "/attestation";
384
- }
385
- if (core.Is.arrayValue(serverConfig.types.auditableItemGraphComponent) &&
386
- !core.Is.stringValue(serverConfig.types.auditableItemGraphComponent[0].restPath)) {
387
- serverConfig.types.auditableItemGraphComponent[0].restPath = "/aig";
388
- }
389
- if (core.Is.arrayValue(serverConfig.types.auditableItemStreamComponent) &&
390
- !core.Is.stringValue(serverConfig.types.auditableItemStreamComponent[0].restPath)) {
391
- serverConfig.types.auditableItemStreamComponent[0].restPath = "/ais";
392
- }
393
- if (core.Is.arrayValue(serverConfig.types.dataProcessingComponent) &&
394
- !core.Is.stringValue(serverConfig.types.dataProcessingComponent[0].restPath)) {
395
- serverConfig.types.dataProcessingComponent[0].restPath = "/data-processing";
396
- }
397
- if (core.Is.arrayValue(serverConfig.types.documentManagementComponent) &&
398
- !core.Is.stringValue(serverConfig.types.documentManagementComponent[0].restPath)) {
399
- serverConfig.types.documentManagementComponent[0].restPath = "/documents";
400
- }
401
- if (core.Is.arrayValue(serverConfig.types.rightsManagementComponent) &&
402
- !core.Is.stringValue(serverConfig.types.rightsManagementComponent[0].restPath)) {
403
- serverConfig.types.rightsManagementComponent[0].restPath = "/rights-management";
404
- }
405
- if (core.Is.arrayValue(serverConfig.types.synchronisedStorageComponent) &&
406
- !core.Is.stringValue(serverConfig.types.synchronisedStorageComponent[0].restPath)) {
407
- serverConfig.types.synchronisedStorageComponent[0].restPath = "/synchronised-storage";
408
- }
409
- if (core.Is.arrayValue(serverConfig.types.federatedCatalogueComponent) &&
410
- !core.Is.stringValue(serverConfig.types.federatedCatalogueComponent[0].restPath)) {
411
- serverConfig.types.federatedCatalogueComponent[0].restPath = "/federated-catalogue";
412
- }
413
- }
414
- /**
415
- * Adds the socket paths to the server config.
416
- * @param serverConfig The server config.
417
- */
418
- function addDefaultSocketPaths(serverConfig) {
419
- if (core.Is.arrayValue(serverConfig.types.eventBusComponent) &&
420
- !core.Is.stringValue(serverConfig.types.eventBusComponent[0].socketPath)) {
421
- serverConfig.types.eventBusComponent[0].socketPath = "event-bus";
422
- }
423
- }
424
-
425
- exports.EngineServer = EngineServer;
426
- exports.addDefaultRestPaths = addDefaultRestPaths;
427
- exports.addDefaultSocketPaths = addDefaultSocketPaths;