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