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

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.
@@ -34,7 +34,7 @@ function initialiseAuthenticationComponent(engineCore$1, context, instanceConfig
34
34
  const type = instanceConfig.type;
35
35
  let component;
36
36
  let instanceType;
37
- if (type === engineModels.AuthenticationComponentType.AuthEntityStorage) {
37
+ if (type === engineModels.AuthenticationComponentType.EntityStorage) {
38
38
  apiAuthEntityStorageService.initSchema();
39
39
  engineCore.initialiseEntityStorageConnector(engineCore$1, context, instanceConfig.options?.userEntityStorageType, "AuthenticationUser");
40
40
  component = new apiAuthEntityStorageService.EntityStorageAuthenticationService({
@@ -511,4 +511,117 @@ class EngineServer {
511
511
  }
512
512
  }
513
513
 
514
+ /**
515
+ * Handles the configuration of the server.
516
+ * @param coreEngine The core engine.
517
+ * @param envVars The environment variables.
518
+ * @param serverInfo The server information.
519
+ * @returns The the config for the core and the server.
520
+ */
521
+ function buildEngineServerConfiguration(coreEngine, envVars, serverInfo) {
522
+ envVars.adminUsername ??= "admin@node";
523
+ envVars.authSigningKeyId ??= "auth-signing";
524
+ const webServerOptions = {
525
+ port: core.Coerce.number(envVars.port),
526
+ host: core.Coerce.string(envVars.host),
527
+ methods: core.Is.stringValue(envVars.httpMethods)
528
+ ? envVars.httpMethods.split(",")
529
+ : undefined,
530
+ allowedHeaders: core.Is.stringValue(envVars.httpAllowedHeaders)
531
+ ? envVars.httpAllowedHeaders.split(",")
532
+ : undefined,
533
+ exposedHeaders: core.Is.stringValue(envVars.httpExposedHeaders)
534
+ ? envVars.httpExposedHeaders.split(",")
535
+ : undefined,
536
+ corsOrigins: core.Is.stringValue(envVars.corsOrigins) ? envVars.corsOrigins.split(",") : undefined
537
+ };
538
+ const coreConfig = coreEngine.getConfig();
539
+ const authProcessorType = envVars.authProcessorType;
540
+ const serverConfig = {
541
+ web: webServerOptions,
542
+ informationComponent: [
543
+ {
544
+ type: engineModels.InformationComponentType.Service,
545
+ options: {
546
+ config: {
547
+ serverInfo
548
+ }
549
+ }
550
+ }
551
+ ],
552
+ restRouteProcessor: []
553
+ };
554
+ serverConfig.restRouteProcessor ??= [];
555
+ serverConfig.socketRouteProcessor ??= [];
556
+ if (!coreConfig.silent) {
557
+ serverConfig.restRouteProcessor.push({
558
+ type: engineModels.RestRouteProcessorType.Logging,
559
+ options: {
560
+ config: {
561
+ includeBody: coreConfig.debug
562
+ }
563
+ }
564
+ });
565
+ serverConfig.socketRouteProcessor.push({
566
+ type: engineModels.SocketRouteProcessorType.Logging,
567
+ options: {
568
+ config: {
569
+ includeBody: coreConfig.debug
570
+ }
571
+ }
572
+ });
573
+ }
574
+ serverConfig.restRouteProcessor.push({
575
+ type: engineModels.RestRouteProcessorType.NodeIdentity
576
+ });
577
+ serverConfig.socketRouteProcessor.push({
578
+ type: engineModels.SocketRouteProcessorType.NodeIdentity
579
+ });
580
+ serverConfig.restRouteProcessor.push({
581
+ type: engineModels.RestRouteProcessorType.RestRoute,
582
+ options: {
583
+ config: {
584
+ includeErrorStack: coreConfig.debug
585
+ }
586
+ }
587
+ });
588
+ serverConfig.socketRouteProcessor.push({
589
+ type: engineModels.SocketRouteProcessorType.SocketRoute,
590
+ options: {
591
+ config: {
592
+ includeErrorStack: coreConfig.debug
593
+ }
594
+ }
595
+ });
596
+ if (authProcessorType === engineModels.AuthenticationComponentType.EntityStorage) {
597
+ serverConfig.authenticationComponent ??= [];
598
+ serverConfig.authenticationComponent.push({
599
+ type: engineModels.AuthenticationComponentType.EntityStorage,
600
+ options: {
601
+ config: {
602
+ signingKeyName: envVars.authSigningKeyId
603
+ }
604
+ }
605
+ });
606
+ serverConfig.restRouteProcessor.push({
607
+ type: engineModels.RestRouteProcessorType.AuthHeader,
608
+ options: {
609
+ config: {
610
+ signingKeyName: envVars.authSigningKeyId
611
+ }
612
+ }
613
+ });
614
+ serverConfig.socketRouteProcessor.push({
615
+ type: engineModels.SocketRouteProcessorType.AuthHeader,
616
+ options: {
617
+ config: {
618
+ signingKeyName: envVars.authSigningKeyId
619
+ }
620
+ }
621
+ });
622
+ }
623
+ return serverConfig;
624
+ }
625
+
514
626
  exports.EngineServer = EngineServer;
627
+ exports.buildEngineServerConfiguration = buildEngineServerConfiguration;
@@ -6,7 +6,7 @@ import { generateRestRoutesAttestation } from '@twin.org/attestation-service';
6
6
  import { generateRestRoutesAuditableItemGraph } from '@twin.org/auditable-item-graph-service';
7
7
  import { generateRestRoutesAuditableItemStream } from '@twin.org/auditable-item-stream-service';
8
8
  import { generateRestRoutesBlobStorage } from '@twin.org/blob-storage-service';
9
- import { I18n, GeneralError, ComponentFactory, Guards, Is } from '@twin.org/core';
9
+ import { I18n, GeneralError, ComponentFactory, Guards, Is, Coerce } from '@twin.org/core';
10
10
  import { AuthenticationComponentType, InformationComponentType, MimeTypeProcessorType, RestRouteProcessorType, SocketRouteProcessorType } from '@twin.org/engine-models';
11
11
  import { generateRestRoutesEntityStorage } from '@twin.org/entity-storage-service';
12
12
  import { generateRestRoutesIdentity, generateRestRoutesIdentityProfile } from '@twin.org/identity-service';
@@ -32,7 +32,7 @@ function initialiseAuthenticationComponent(engineCore, context, instanceConfig,
32
32
  const type = instanceConfig.type;
33
33
  let component;
34
34
  let instanceType;
35
- if (type === AuthenticationComponentType.AuthEntityStorage) {
35
+ if (type === AuthenticationComponentType.EntityStorage) {
36
36
  initSchema();
37
37
  initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.userEntityStorageType, "AuthenticationUser");
38
38
  component = new EntityStorageAuthenticationService({
@@ -509,4 +509,116 @@ class EngineServer {
509
509
  }
510
510
  }
511
511
 
512
- export { EngineServer };
512
+ /**
513
+ * Handles the configuration of the server.
514
+ * @param coreEngine The core engine.
515
+ * @param envVars The environment variables.
516
+ * @param serverInfo The server information.
517
+ * @returns The the config for the core and the server.
518
+ */
519
+ function buildEngineServerConfiguration(coreEngine, envVars, serverInfo) {
520
+ envVars.adminUsername ??= "admin@node";
521
+ envVars.authSigningKeyId ??= "auth-signing";
522
+ const webServerOptions = {
523
+ port: Coerce.number(envVars.port),
524
+ host: Coerce.string(envVars.host),
525
+ methods: Is.stringValue(envVars.httpMethods)
526
+ ? envVars.httpMethods.split(",")
527
+ : undefined,
528
+ allowedHeaders: Is.stringValue(envVars.httpAllowedHeaders)
529
+ ? envVars.httpAllowedHeaders.split(",")
530
+ : undefined,
531
+ exposedHeaders: Is.stringValue(envVars.httpExposedHeaders)
532
+ ? envVars.httpExposedHeaders.split(",")
533
+ : undefined,
534
+ corsOrigins: Is.stringValue(envVars.corsOrigins) ? envVars.corsOrigins.split(",") : undefined
535
+ };
536
+ const coreConfig = coreEngine.getConfig();
537
+ const authProcessorType = envVars.authProcessorType;
538
+ const serverConfig = {
539
+ web: webServerOptions,
540
+ informationComponent: [
541
+ {
542
+ type: InformationComponentType.Service,
543
+ options: {
544
+ config: {
545
+ serverInfo
546
+ }
547
+ }
548
+ }
549
+ ],
550
+ restRouteProcessor: []
551
+ };
552
+ serverConfig.restRouteProcessor ??= [];
553
+ serverConfig.socketRouteProcessor ??= [];
554
+ if (!coreConfig.silent) {
555
+ serverConfig.restRouteProcessor.push({
556
+ type: RestRouteProcessorType.Logging,
557
+ options: {
558
+ config: {
559
+ includeBody: coreConfig.debug
560
+ }
561
+ }
562
+ });
563
+ serverConfig.socketRouteProcessor.push({
564
+ type: SocketRouteProcessorType.Logging,
565
+ options: {
566
+ config: {
567
+ includeBody: coreConfig.debug
568
+ }
569
+ }
570
+ });
571
+ }
572
+ serverConfig.restRouteProcessor.push({
573
+ type: RestRouteProcessorType.NodeIdentity
574
+ });
575
+ serverConfig.socketRouteProcessor.push({
576
+ type: SocketRouteProcessorType.NodeIdentity
577
+ });
578
+ serverConfig.restRouteProcessor.push({
579
+ type: RestRouteProcessorType.RestRoute,
580
+ options: {
581
+ config: {
582
+ includeErrorStack: coreConfig.debug
583
+ }
584
+ }
585
+ });
586
+ serverConfig.socketRouteProcessor.push({
587
+ type: SocketRouteProcessorType.SocketRoute,
588
+ options: {
589
+ config: {
590
+ includeErrorStack: coreConfig.debug
591
+ }
592
+ }
593
+ });
594
+ if (authProcessorType === AuthenticationComponentType.EntityStorage) {
595
+ serverConfig.authenticationComponent ??= [];
596
+ serverConfig.authenticationComponent.push({
597
+ type: AuthenticationComponentType.EntityStorage,
598
+ options: {
599
+ config: {
600
+ signingKeyName: envVars.authSigningKeyId
601
+ }
602
+ }
603
+ });
604
+ serverConfig.restRouteProcessor.push({
605
+ type: RestRouteProcessorType.AuthHeader,
606
+ options: {
607
+ config: {
608
+ signingKeyName: envVars.authSigningKeyId
609
+ }
610
+ }
611
+ });
612
+ serverConfig.socketRouteProcessor.push({
613
+ type: SocketRouteProcessorType.AuthHeader,
614
+ options: {
615
+ config: {
616
+ signingKeyName: envVars.authSigningKeyId
617
+ }
618
+ }
619
+ });
620
+ }
621
+ return serverConfig;
622
+ }
623
+
624
+ export { EngineServer, buildEngineServerConfiguration };
@@ -1 +1,3 @@
1
1
  export * from "./engineServer";
2
+ export * from "./models/IEngineServerEnvironmentVariables";
3
+ export * from "./utils/engineServerEnvBuilder";
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The engine server environment variables.
3
+ */
4
+ export interface IEngineServerEnvironmentVariables {
5
+ /**
6
+ * The name of the admin user.
7
+ */
8
+ adminUsername: string;
9
+ /**
10
+ * The port to serve the API from.
11
+ */
12
+ port: string;
13
+ /**
14
+ * The host to serve the API from.
15
+ */
16
+ host: string;
17
+ /**
18
+ * The CORS origins to allow, defaults to *.
19
+ */
20
+ corsOrigins: string;
21
+ /**
22
+ * The CORS methods to allow, defaults to GET, POST, PUT, DELETE, OPTIONS.
23
+ */
24
+ httpMethods: string;
25
+ /**
26
+ * The CORS headers to allow.
27
+ */
28
+ httpAllowedHeaders: string;
29
+ /**
30
+ * The CORS headers to expose.
31
+ */
32
+ httpExposedHeaders: string;
33
+ /**
34
+ * The type of auth processor to use on the API: entity-storage.
35
+ */
36
+ authProcessorType: string;
37
+ /**
38
+ * The id of the key in the vault to use for signing in auth operations.
39
+ */
40
+ authSigningKeyId: string;
41
+ }
@@ -0,0 +1,11 @@
1
+ import type { IServerInfo } from "@twin.org/api-models";
2
+ import { type IEngineCore, type IEngineServerConfig } from "@twin.org/engine-models";
3
+ import type { IEngineServerEnvironmentVariables } from "../models/IEngineServerEnvironmentVariables";
4
+ /**
5
+ * Handles the configuration of the server.
6
+ * @param coreEngine The core engine.
7
+ * @param envVars The environment variables.
8
+ * @param serverInfo The server information.
9
+ * @returns The the config for the core and the server.
10
+ */
11
+ export declare function buildEngineServerConfiguration(coreEngine: IEngineCore, envVars: IEngineServerEnvironmentVariables, serverInfo: IServerInfo): IEngineServerConfig;
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/engine-server - Changelog
2
2
 
3
- ## v0.0.1-next.4
3
+ ## v0.0.1-next.6
4
4
 
5
5
  - Initial Release
@@ -0,0 +1,25 @@
1
+ # Function: buildEngineServerConfiguration()
2
+
3
+ > **buildEngineServerConfiguration**(`coreEngine`, `envVars`, `serverInfo`): `IEngineServerConfig`
4
+
5
+ Handles the configuration of the server.
6
+
7
+ ## Parameters
8
+
9
+ • **coreEngine**: `IEngineCore`\<`IEngineState`\>
10
+
11
+ The core engine.
12
+
13
+ • **envVars**: [`IEngineServerEnvironmentVariables`](../interfaces/IEngineServerEnvironmentVariables.md)
14
+
15
+ The environment variables.
16
+
17
+ • **serverInfo**: `IServerInfo`
18
+
19
+ The server information.
20
+
21
+ ## Returns
22
+
23
+ `IEngineServerConfig`
24
+
25
+ The the config for the core and the server.
@@ -3,3 +3,11 @@
3
3
  ## Classes
4
4
 
5
5
  - [EngineServer](classes/EngineServer.md)
6
+
7
+ ## Interfaces
8
+
9
+ - [IEngineServerEnvironmentVariables](interfaces/IEngineServerEnvironmentVariables.md)
10
+
11
+ ## Functions
12
+
13
+ - [buildEngineServerConfiguration](functions/buildEngineServerConfiguration.md)
@@ -0,0 +1,75 @@
1
+ # Interface: IEngineServerEnvironmentVariables
2
+
3
+ The engine server environment variables.
4
+
5
+ ## Properties
6
+
7
+ ### adminUsername
8
+
9
+ > **adminUsername**: `string`
10
+
11
+ The name of the admin user.
12
+
13
+ ***
14
+
15
+ ### port
16
+
17
+ > **port**: `string`
18
+
19
+ The port to serve the API from.
20
+
21
+ ***
22
+
23
+ ### host
24
+
25
+ > **host**: `string`
26
+
27
+ The host to serve the API from.
28
+
29
+ ***
30
+
31
+ ### corsOrigins
32
+
33
+ > **corsOrigins**: `string`
34
+
35
+ The CORS origins to allow, defaults to *.
36
+
37
+ ***
38
+
39
+ ### httpMethods
40
+
41
+ > **httpMethods**: `string`
42
+
43
+ The CORS methods to allow, defaults to GET, POST, PUT, DELETE, OPTIONS.
44
+
45
+ ***
46
+
47
+ ### httpAllowedHeaders
48
+
49
+ > **httpAllowedHeaders**: `string`
50
+
51
+ The CORS headers to allow.
52
+
53
+ ***
54
+
55
+ ### httpExposedHeaders
56
+
57
+ > **httpExposedHeaders**: `string`
58
+
59
+ The CORS headers to expose.
60
+
61
+ ***
62
+
63
+ ### authProcessorType
64
+
65
+ > **authProcessorType**: `string`
66
+
67
+ The type of auth processor to use on the API: entity-storage.
68
+
69
+ ***
70
+
71
+ ### authSigningKeyId
72
+
73
+ > **authSigningKeyId**: `string`
74
+
75
+ The id of the key in the vault to use for signing in auth operations.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/engine-server",
3
- "version": "0.0.1-next.4",
3
+ "version": "0.0.1-next.6",
4
4
  "description": "Engine implementation for a server.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -43,8 +43,8 @@
43
43
  "@twin.org/crypto": "next",
44
44
  "@twin.org/data-core": "next",
45
45
  "@twin.org/data-schema-org": "next",
46
- "@twin.org/engine-core": "0.0.1-next.4",
47
- "@twin.org/engine-models": "0.0.1-next.4",
46
+ "@twin.org/engine-core": "0.0.1-next.6",
47
+ "@twin.org/engine-models": "0.0.1-next.6",
48
48
  "@twin.org/entity": "next",
49
49
  "@twin.org/entity-storage-connector-cosmosdb": "next",
50
50
  "@twin.org/entity-storage-connector-dynamodb": "next",