@twin.org/engine-core 0.0.1-next.1

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.
Files changed (35) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +21 -0
  3. package/dist/cjs/index.cjs +1662 -0
  4. package/dist/esm/index.mjs +1657 -0
  5. package/dist/types/components/attestation.d.ts +21 -0
  6. package/dist/types/components/auditableItemGraph.d.ts +11 -0
  7. package/dist/types/components/auditableItemStream.d.ts +11 -0
  8. package/dist/types/components/backgroundTask.d.ts +11 -0
  9. package/dist/types/components/blobStorage.d.ts +21 -0
  10. package/dist/types/components/entityStorage.d.ts +20 -0
  11. package/dist/types/components/faucet.d.ts +11 -0
  12. package/dist/types/components/identity.d.ts +21 -0
  13. package/dist/types/components/identityProfile.d.ts +21 -0
  14. package/dist/types/components/immutableProof.d.ts +11 -0
  15. package/dist/types/components/immutableStorage.d.ts +11 -0
  16. package/dist/types/components/logging.d.ts +21 -0
  17. package/dist/types/components/nft.d.ts +21 -0
  18. package/dist/types/components/telemetry.d.ts +21 -0
  19. package/dist/types/components/vault.d.ts +11 -0
  20. package/dist/types/components/wallet.d.ts +21 -0
  21. package/dist/types/engineCore.d.ts +65 -0
  22. package/dist/types/index.d.ts +5 -0
  23. package/dist/types/models/IEngineCoreOptions.d.ts +22 -0
  24. package/dist/types/storage/fileStateStorage.d.ts +29 -0
  25. package/dist/types/storage/memoryStateStorage.d.ts +28 -0
  26. package/docs/changelog.md +5 -0
  27. package/docs/examples.md +1 -0
  28. package/docs/reference/classes/EngineCore.md +209 -0
  29. package/docs/reference/classes/FileStateStorage.md +89 -0
  30. package/docs/reference/classes/MemoryStateStorage.md +85 -0
  31. package/docs/reference/functions/initialiseEntityStorageConnector.md +31 -0
  32. package/docs/reference/index.md +15 -0
  33. package/docs/reference/interfaces/IEngineCoreOptions.md +45 -0
  34. package/locales/en.json +42 -0
  35. package/package.json +102 -0
@@ -0,0 +1,1657 @@
1
+ import { StringHelper, Is, GeneralError, I18n, ComponentFactory, BaseError, ErrorHelper, Guards } from '@twin.org/core';
2
+ import { EntityStorageConnectorType, EntityStorageComponentType, AttestationConnectorType, AttestationComponentType, AuditableItemGraphComponentType, AuditableItemStreamComponentType, BackgroundTaskConnectorType, BlobStorageConnectorType, BlobStorageComponentType, FaucetConnectorType, IdentityConnectorType, IdentityComponentType, IdentityProfileConnectorType, IdentityProfileComponentType, ImmutableProofComponentType, ImmutableStorageConnectorType, LoggingConnectorType, LoggingComponentType, NftConnectorType, NftComponentType, TelemetryConnectorType, TelemetryComponentType, VaultConnectorType, WalletConnectorType } from '@twin.org/engine-models';
3
+ import { CosmosDbEntityStorageConnector } from '@twin.org/entity-storage-connector-cosmosdb';
4
+ import { DynamoDbEntityStorageConnector } from '@twin.org/entity-storage-connector-dynamodb';
5
+ import { FileEntityStorageConnector } from '@twin.org/entity-storage-connector-file';
6
+ import { FirestoreEntityStorageConnector } from '@twin.org/entity-storage-connector-gcp-firestore';
7
+ import { MemoryEntityStorageConnector } from '@twin.org/entity-storage-connector-memory';
8
+ import { ScyllaDBTableConnector } from '@twin.org/entity-storage-connector-scylladb';
9
+ import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
10
+ import { EntityStorageService } from '@twin.org/entity-storage-service';
11
+ import { ConsoleLoggingConnector } from '@twin.org/logging-connector-console';
12
+ import { MultiLoggingConnector, LoggingConnectorFactory } from '@twin.org/logging-models';
13
+ import { EntityStorageAttestationConnector } from '@twin.org/attestation-connector-entity-storage';
14
+ import { IotaAttestationConnector } from '@twin.org/attestation-connector-iota';
15
+ import { AttestationConnectorFactory } from '@twin.org/attestation-models';
16
+ import { AttestationService } from '@twin.org/attestation-service';
17
+ import { initSchema, AuditableItemGraphService } from '@twin.org/auditable-item-graph-service';
18
+ import { initSchema as initSchema$1, AuditableItemStreamService } from '@twin.org/auditable-item-stream-service';
19
+ import { initSchema as initSchema$2, EntityStorageBackgroundTaskConnector } from '@twin.org/background-task-connector-entity-storage';
20
+ import { BackgroundTaskConnectorFactory } from '@twin.org/background-task-models';
21
+ import { S3BlobStorageConnector } from '@twin.org/blob-storage-connector-aws-s3';
22
+ import { AzureBlobStorageConnector } from '@twin.org/blob-storage-connector-azure';
23
+ import { FileBlobStorageConnector } from '@twin.org/blob-storage-connector-file';
24
+ import { GcpBlobStorageConnector } from '@twin.org/blob-storage-connector-gcp';
25
+ import { IpfsBlobStorageConnector } from '@twin.org/blob-storage-connector-ipfs';
26
+ import { MemoryBlobStorageConnector } from '@twin.org/blob-storage-connector-memory';
27
+ import { BlobStorageConnectorFactory } from '@twin.org/blob-storage-models';
28
+ import { initSchema as initSchema$3, BlobStorageService } from '@twin.org/blob-storage-service';
29
+ import { EntityStorageFaucetConnector, EntityStorageWalletConnector, initSchema as initSchema$b } from '@twin.org/wallet-connector-entity-storage';
30
+ import { IotaFaucetConnector, IotaWalletConnector } from '@twin.org/wallet-connector-iota';
31
+ import { FaucetConnectorFactory, WalletConnectorFactory } from '@twin.org/wallet-models';
32
+ import { initSchema as initSchema$4, EntityStorageIdentityConnector, EntityStorageIdentityProfileConnector } from '@twin.org/identity-connector-entity-storage';
33
+ import { IotaIdentityConnector } from '@twin.org/identity-connector-iota';
34
+ import { IdentityConnectorFactory, IdentityProfileConnectorFactory } from '@twin.org/identity-models';
35
+ import { IdentityService, IdentityProfileService } from '@twin.org/identity-service';
36
+ import { initSchema as initSchema$5, ImmutableProofService } from '@twin.org/immutable-proof-service';
37
+ import { initSchema as initSchema$6, EntityStorageImmutableStorageConnector } from '@twin.org/immutable-storage-connector-entity-storage';
38
+ import { IotaImmutableStorageConnector } from '@twin.org/immutable-storage-connector-iota';
39
+ import { ImmutableStorageConnectorFactory } from '@twin.org/immutable-storage-models';
40
+ import { initSchema as initSchema$7, EntityStorageLoggingConnector } from '@twin.org/logging-connector-entity-storage';
41
+ import { LoggingService } from '@twin.org/logging-service';
42
+ import { initSchema as initSchema$8, EntityStorageNftConnector } from '@twin.org/nft-connector-entity-storage';
43
+ import { IotaNftConnector } from '@twin.org/nft-connector-iota';
44
+ import { NftConnectorFactory } from '@twin.org/nft-models';
45
+ import { NftService } from '@twin.org/nft-service';
46
+ import { initSchema as initSchema$9, EntityStorageTelemetryConnector } from '@twin.org/telemetry-connector-entity-storage';
47
+ import { TelemetryConnectorFactory } from '@twin.org/telemetry-models';
48
+ import { TelemetryService } from '@twin.org/telemetry-service';
49
+ import { initSchema as initSchema$a, EntityStorageVaultConnector } from '@twin.org/vault-connector-entity-storage';
50
+ import { HashicorpVaultConnector } from '@twin.org/vault-connector-hashicorp';
51
+ import { VaultConnectorFactory } from '@twin.org/vault-models';
52
+ import { readFile, writeFile, stat } from 'node:fs/promises';
53
+
54
+ // Copyright 2024 IOTA Stiftung.
55
+ // SPDX-License-Identifier: Apache-2.0.
56
+ /**
57
+ * Initialise the entity storage connector.
58
+ * @param engineCore The engine core.
59
+ * @param context The context for the engine.
60
+ * @param typeCustom Override the type of connector to use instead of default configuration.
61
+ * @param schema The schema for the entity storage.
62
+ * @throws GeneralError if the connector type is unknown.
63
+ */
64
+ function initialiseEntityStorageConnector(engineCore, context, typeCustom, schema) {
65
+ const instanceName = StringHelper.kebabCase(schema);
66
+ let entityStorageConfig;
67
+ if (Is.stringValue(typeCustom)) {
68
+ // A custom type has been specified, so look it up
69
+ entityStorageConfig = context.config.entityStorageConnector?.find(c => c.type === typeCustom || c.overrideInstanceType === typeCustom);
70
+ if (Is.empty(entityStorageConfig)) {
71
+ throw new GeneralError("engineCore", "entityStorageCustomMissing", {
72
+ typeCustom,
73
+ storageName: instanceName
74
+ });
75
+ }
76
+ }
77
+ else {
78
+ // The default entity storage method is either the one with the isDefault flag set
79
+ // or pick the first one if no default is set.
80
+ entityStorageConfig =
81
+ context.config.entityStorageConnector?.find(c => c.isDefault ?? false) ??
82
+ context.config.entityStorageConnector?.[0];
83
+ if (Is.empty(entityStorageConfig)) {
84
+ throw new GeneralError("engineCore", "entityStorageMissing", {
85
+ storageName: instanceName
86
+ });
87
+ }
88
+ }
89
+ const type = entityStorageConfig.type;
90
+ let entityStorageConnector;
91
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuringEntityStorage", {
92
+ element: "Entity Storage",
93
+ storageName: instanceName,
94
+ storageType: type
95
+ }));
96
+ if (type === EntityStorageConnectorType.Memory) {
97
+ entityStorageConnector = new MemoryEntityStorageConnector({
98
+ entitySchema: schema
99
+ });
100
+ }
101
+ else if (type === EntityStorageConnectorType.File) {
102
+ entityStorageConnector = new FileEntityStorageConnector({
103
+ entitySchema: schema,
104
+ ...entityStorageConfig.options
105
+ });
106
+ }
107
+ else if (type === EntityStorageConnectorType.AwsDynamoDb) {
108
+ entityStorageConnector = new DynamoDbEntityStorageConnector({
109
+ entitySchema: schema,
110
+ ...entityStorageConfig.options,
111
+ config: {
112
+ ...entityStorageConfig.options.config,
113
+ tableName: `${entityStorageConfig.options.tablePrefix ?? ""}${instanceName}`
114
+ }
115
+ });
116
+ }
117
+ else if (type === EntityStorageConnectorType.AzureCosmosDb) {
118
+ entityStorageConnector = new CosmosDbEntityStorageConnector({
119
+ entitySchema: schema,
120
+ ...entityStorageConfig.options,
121
+ config: {
122
+ ...entityStorageConfig.options.config,
123
+ containerId: `${entityStorageConfig.options.tablePrefix ?? ""}${instanceName}`
124
+ }
125
+ });
126
+ }
127
+ else if (type === EntityStorageConnectorType.GcpFirestoreDb) {
128
+ entityStorageConnector = new FirestoreEntityStorageConnector({
129
+ entitySchema: schema,
130
+ ...entityStorageConfig.options,
131
+ config: {
132
+ ...entityStorageConfig.options.config,
133
+ collectionName: `${entityStorageConfig.options.tablePrefix ?? ""}${instanceName}`
134
+ }
135
+ });
136
+ }
137
+ else if (type === EntityStorageConnectorType.ScyllaDb) {
138
+ entityStorageConnector = new ScyllaDBTableConnector({
139
+ entitySchema: schema,
140
+ ...entityStorageConfig.options,
141
+ config: {
142
+ ...entityStorageConfig.options.config,
143
+ tableName: `${entityStorageConfig.options.tablePrefix ?? ""}${instanceName}`
144
+ }
145
+ });
146
+ }
147
+ else {
148
+ throw new GeneralError("engineCore", "connectorUnknownType", {
149
+ type,
150
+ connectorType: "entityStorageConnector"
151
+ });
152
+ }
153
+ context.componentInstances.push({
154
+ instanceType: instanceName,
155
+ component: entityStorageConnector
156
+ });
157
+ EntityStorageConnectorFactory.register(instanceName, () => entityStorageConnector);
158
+ }
159
+ /**
160
+ * Initialise the entity storage connector.
161
+ * @param engineCore The engine core.
162
+ * @param context The context for the engine.
163
+ * @param instanceConfig The instance config.
164
+ * @param overrideInstanceType The instance type to override the default.
165
+ * @returns The name of the instance created.
166
+ * @throws GeneralError if the connector type is unknown.
167
+ */
168
+ function initialiseEntityStorageComponent(engineCore, context, instanceConfig, overrideInstanceType) {
169
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
170
+ element: `Entity Storage Component: ${instanceConfig.type}`
171
+ }));
172
+ const type = instanceConfig.type;
173
+ let connector;
174
+ let instanceType;
175
+ if (type === EntityStorageComponentType.Service) {
176
+ const kebabName = StringHelper.kebabCase(instanceConfig.options.entityStorageType);
177
+ // See if there is a custom entity storage for this type, otherwise just use the default one.
178
+ const hasCustom = context.config.entityStorageConnector?.some(c => c.type === kebabName || c.overrideInstanceType === kebabName);
179
+ initialiseEntityStorageConnector(engineCore, context, hasCustom ? kebabName : undefined, instanceConfig.options.entityStorageType);
180
+ connector = new EntityStorageService({
181
+ entityStorageType: kebabName,
182
+ config: {
183
+ ...instanceConfig.options.config
184
+ }
185
+ });
186
+ instanceType = StringHelper.kebabCase(instanceConfig.options.entityStorageType);
187
+ }
188
+ else {
189
+ throw new GeneralError("engineCore", "componentUnknownType", {
190
+ type,
191
+ serviceType: "entityStorageComponent"
192
+ });
193
+ }
194
+ const finalInstanceType = overrideInstanceType ?? instanceType;
195
+ context.componentInstances.push({
196
+ instanceType: finalInstanceType,
197
+ component: connector
198
+ });
199
+ ComponentFactory.register(finalInstanceType, () => connector);
200
+ return finalInstanceType;
201
+ }
202
+
203
+ // Copyright 2024 IOTA Stiftung.
204
+ // SPDX-License-Identifier: Apache-2.0.
205
+ /**
206
+ * Initialise the attestation connector.
207
+ * @param engineCore The engine core.
208
+ * @param context The context for the engine.
209
+ * @param instanceConfig The instance config.
210
+ * @param overrideInstanceType The instance type to override the default.
211
+ * @returns The name of the instance created.
212
+ * @throws GeneralError if the connector type is unknown.
213
+ */
214
+ function initialiseAttestationConnector(engineCore, context, instanceConfig, overrideInstanceType) {
215
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
216
+ element: `Attestation Connector: ${instanceConfig.type}`
217
+ }));
218
+ const type = instanceConfig.type;
219
+ let connector;
220
+ let instanceType;
221
+ if (type === AttestationConnectorType.Iota) {
222
+ connector = new IotaAttestationConnector({
223
+ identityConnectorType: context.defaultTypes.identityConnector,
224
+ nftConnectorType: context.defaultTypes.nftConnector,
225
+ ...instanceConfig.options
226
+ });
227
+ instanceType = IotaAttestationConnector.NAMESPACE;
228
+ }
229
+ else if (type === AttestationConnectorType.EntityStorage) {
230
+ connector = new EntityStorageAttestationConnector({
231
+ identityConnectorType: context.defaultTypes.identityConnector,
232
+ nftConnectorType: context.defaultTypes.nftConnector,
233
+ ...instanceConfig.options
234
+ });
235
+ instanceType = EntityStorageAttestationConnector.NAMESPACE;
236
+ }
237
+ else {
238
+ throw new GeneralError("engineCore", "connectorUnknownType", {
239
+ type,
240
+ connectorType: "attestationConnector"
241
+ });
242
+ }
243
+ const finalInstanceType = overrideInstanceType ?? instanceType;
244
+ context.componentInstances.push({
245
+ instanceType: finalInstanceType,
246
+ component: connector
247
+ });
248
+ AttestationConnectorFactory.register(finalInstanceType, () => connector);
249
+ return finalInstanceType;
250
+ }
251
+ /**
252
+ * Initialise the attestation component.
253
+ * @param engineCore The engine core.
254
+ * @param context The context for the engine.
255
+ * @param instanceConfig The instance config.
256
+ * @param overrideInstanceType The instance type to override the default.
257
+ * @returns The name of the instance created.
258
+ * @throws GeneralError if the component type is unknown.
259
+ */
260
+ function initialiseAttestationComponent(engineCore, context, instanceConfig, overrideInstanceType) {
261
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
262
+ element: `Attestation Component: ${instanceConfig.type}`
263
+ }));
264
+ const type = instanceConfig.type;
265
+ let component;
266
+ let instanceType;
267
+ if (type === AttestationComponentType.Service) {
268
+ component = new AttestationService({
269
+ walletConnectorType: context.defaultTypes.walletConnector,
270
+ ...instanceConfig.options
271
+ });
272
+ instanceType = AttestationService.NAMESPACE;
273
+ }
274
+ else {
275
+ throw new GeneralError("engineCore", "componentUnknownType", {
276
+ type,
277
+ componentType: "attestationComponent"
278
+ });
279
+ }
280
+ const finalInstanceType = overrideInstanceType ?? instanceType;
281
+ context.componentInstances.push({
282
+ instanceType: finalInstanceType,
283
+ component
284
+ });
285
+ ComponentFactory.register(finalInstanceType, () => component);
286
+ return finalInstanceType;
287
+ }
288
+
289
+ /**
290
+ * Initialise the auditable item graph component.
291
+ * @param engineCore The engine core.
292
+ * @param context The context for the engine.
293
+ * @param instanceConfig The instance config.
294
+ * @param overrideInstanceType The instance type to override the default.
295
+ * @returns The name of the instance created.
296
+ * @throws GeneralError if the component type is unknown.
297
+ */
298
+ function initialiseAuditableItemGraphComponent(engineCore, context, instanceConfig, overrideInstanceType) {
299
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
300
+ element: `Auditable Item Graph Component: ${instanceConfig.type}`
301
+ }));
302
+ const type = instanceConfig.type;
303
+ let component;
304
+ let instanceType;
305
+ if (type === AuditableItemGraphComponentType.Service) {
306
+ initSchema();
307
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.vertexEntityStorageType, "AuditableItemGraphVertex");
308
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.changesetEntityStorageType, "AuditableItemGraphChangeset");
309
+ component = new AuditableItemGraphService({
310
+ immutableProofComponentType: context.defaultTypes.immutableProofComponent,
311
+ ...instanceConfig.options
312
+ });
313
+ instanceType = AuditableItemGraphService.NAMESPACE;
314
+ }
315
+ else {
316
+ throw new GeneralError("engineCore", "componentUnknownType", {
317
+ type,
318
+ componentType: "auditableItemGraphComponent"
319
+ });
320
+ }
321
+ const finalInstanceType = overrideInstanceType ?? instanceType;
322
+ context.componentInstances.push({
323
+ instanceType: finalInstanceType,
324
+ component
325
+ });
326
+ ComponentFactory.register(finalInstanceType, () => component);
327
+ return finalInstanceType;
328
+ }
329
+
330
+ /**
331
+ * Initialise the auditable item stream component.
332
+ * @param engineCore The engine core.
333
+ * @param context The context for the engine.
334
+ * @param instanceConfig The instance config.
335
+ * @param overrideInstanceType The instance type to override the default.
336
+ * @returns The name of the instance created.
337
+ * @throws GeneralError if the component type is unknown.
338
+ */
339
+ function initialiseAuditableItemStreamComponent(engineCore, context, instanceConfig, overrideInstanceType) {
340
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
341
+ element: `Auditable Item Stream Component: ${instanceConfig.type}`
342
+ }));
343
+ const type = instanceConfig.type;
344
+ let component;
345
+ let instanceType;
346
+ if (type === AuditableItemStreamComponentType.Service) {
347
+ initSchema$1();
348
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.streamEntityStorageType, "AuditableItemStream");
349
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.streamEntryEntityStorageType, "AuditableItemStreamEntry");
350
+ component = new AuditableItemStreamService({
351
+ immutableProofComponentType: context.defaultTypes.immutableProofComponent,
352
+ ...instanceConfig.options
353
+ });
354
+ instanceType = AuditableItemStreamService.NAMESPACE;
355
+ }
356
+ else {
357
+ throw new GeneralError("engineCore", "componentUnknownType", {
358
+ type,
359
+ componentType: "auditableItemStreamComponent"
360
+ });
361
+ }
362
+ const finalInstanceType = overrideInstanceType ?? instanceType;
363
+ context.componentInstances.push({
364
+ instanceType: finalInstanceType,
365
+ component
366
+ });
367
+ ComponentFactory.register(finalInstanceType, () => component);
368
+ return finalInstanceType;
369
+ }
370
+
371
+ // Copyright 2024 IOTA Stiftung.
372
+ // SPDX-License-Identifier: Apache-2.0.
373
+ /**
374
+ * Initialise a background task connector.
375
+ * @param engineCore The engine core.
376
+ * @param context The context for the engine.
377
+ * @param instanceConfig The instance config.
378
+ * @param overrideInstanceType The instance type to override the default.
379
+ * @returns The name of the instance created.
380
+ * @throws GeneralError if the connector type is unknown.
381
+ */
382
+ function initialiseBackgroundTaskConnector(engineCore, context, instanceConfig, overrideInstanceType) {
383
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
384
+ element: `Background Task Connector: ${instanceConfig.type}`
385
+ }));
386
+ const type = instanceConfig.type;
387
+ let connector;
388
+ let instanceType;
389
+ if (type === BackgroundTaskConnectorType.EntityStorage) {
390
+ initSchema$2();
391
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.backgroundTaskEntityStorageType, "BackgroundTask");
392
+ connector = new EntityStorageBackgroundTaskConnector({
393
+ loggingConnectorType: context.defaultTypes.loggingConnector,
394
+ ...instanceConfig.options
395
+ });
396
+ instanceType = EntityStorageBackgroundTaskConnector.NAMESPACE;
397
+ }
398
+ else {
399
+ throw new GeneralError("engineCore", "connectorUnknownType", {
400
+ type,
401
+ connectorType: "backgroundTaskConnector"
402
+ });
403
+ }
404
+ const finalInstanceType = overrideInstanceType ?? instanceType;
405
+ context.componentInstances.push({ instanceType: finalInstanceType, component: connector });
406
+ BackgroundTaskConnectorFactory.register(finalInstanceType, () => connector);
407
+ return finalInstanceType;
408
+ }
409
+
410
+ // Copyright 2024 IOTA Stiftung.
411
+ // SPDX-License-Identifier: Apache-2.0.
412
+ /**
413
+ * Initialise the blob storage connector.
414
+ * @param engineCore The engine core.
415
+ * @param context The context for the engine.
416
+ * @param instanceConfig The instance config.
417
+ * @param overrideInstanceType The instance type to override the default.
418
+ * @returns The name of the instance created.
419
+ * @throws GeneralError if the connector type is unknown.
420
+ */
421
+ function initialiseBlobStorageConnector(engineCore, context, instanceConfig, overrideInstanceType) {
422
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
423
+ element: `Blob Storage Connector: ${instanceConfig.type}`
424
+ }));
425
+ const type = instanceConfig.type;
426
+ let connector;
427
+ let instanceType;
428
+ if (type === BlobStorageConnectorType.Ipfs) {
429
+ connector = new IpfsBlobStorageConnector(instanceConfig.options);
430
+ instanceType = IpfsBlobStorageConnector.NAMESPACE;
431
+ }
432
+ else if (type === BlobStorageConnectorType.File) {
433
+ connector = new FileBlobStorageConnector({
434
+ ...instanceConfig.options,
435
+ config: {
436
+ ...instanceConfig.options.config,
437
+ directory: `${instanceConfig.options.storagePrefix ?? ""}${instanceConfig.options.config.directory}`
438
+ }
439
+ });
440
+ instanceType = FileBlobStorageConnector.NAMESPACE;
441
+ }
442
+ else if (type === BlobStorageConnectorType.Memory) {
443
+ connector = new MemoryBlobStorageConnector();
444
+ instanceType = MemoryBlobStorageConnector.NAMESPACE;
445
+ }
446
+ else if (type === BlobStorageConnectorType.AwsS3) {
447
+ connector = new S3BlobStorageConnector({
448
+ ...instanceConfig.options,
449
+ config: {
450
+ ...instanceConfig.options.config,
451
+ bucketName: `${instanceConfig.options.storagePrefix ?? ""}${instanceConfig.options.config.bucketName}`
452
+ }
453
+ });
454
+ instanceType = S3BlobStorageConnector.NAMESPACE;
455
+ }
456
+ else if (type === BlobStorageConnectorType.GcpStorage) {
457
+ connector = new GcpBlobStorageConnector({
458
+ ...instanceConfig.options,
459
+ config: {
460
+ ...instanceConfig.options.config,
461
+ bucketName: `${instanceConfig.options.storagePrefix ?? ""}${instanceConfig.options.config.bucketName}`
462
+ }
463
+ });
464
+ instanceType = GcpBlobStorageConnector.NAMESPACE;
465
+ }
466
+ else if (type === BlobStorageConnectorType.AzureStorage) {
467
+ connector = new AzureBlobStorageConnector({
468
+ ...instanceConfig.options,
469
+ config: {
470
+ ...instanceConfig.options.config,
471
+ containerName: `${instanceConfig.options.storagePrefix ?? ""}${instanceConfig.options.config.containerName}`
472
+ }
473
+ });
474
+ instanceType = AzureBlobStorageConnector.NAMESPACE;
475
+ }
476
+ else {
477
+ throw new GeneralError("engineCore", "connectorUnknownType", {
478
+ type,
479
+ connectorType: "blobStorageConnector"
480
+ });
481
+ }
482
+ const finalInstanceType = overrideInstanceType ?? instanceType;
483
+ context.componentInstances.push({
484
+ instanceType: finalInstanceType,
485
+ component: connector
486
+ });
487
+ BlobStorageConnectorFactory.register(finalInstanceType, () => connector);
488
+ return finalInstanceType;
489
+ }
490
+ /**
491
+ * Initialise the blob storage component.
492
+ * @param engineCore The engine core.
493
+ * @param context The context for the engine.
494
+ * @param instanceConfig The instance config.
495
+ * @param overrideInstanceType The instance type to override the default.
496
+ * @returns The name of the instance created.
497
+ * @throws GeneralError if the component type is unknown.
498
+ */
499
+ function initialiseBlobStorageComponent(engineCore, context, instanceConfig, overrideInstanceType) {
500
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
501
+ element: `Blob Storage Component: ${instanceConfig.type}`
502
+ }));
503
+ const type = instanceConfig.type;
504
+ let component;
505
+ let instanceType;
506
+ if (type === BlobStorageComponentType.Service) {
507
+ initSchema$3();
508
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.entryEntityStorageType, "BlobStorageEntry");
509
+ component = new BlobStorageService({
510
+ vaultConnectorType: context.defaultTypes.vaultConnector,
511
+ ...instanceConfig.options
512
+ });
513
+ instanceType = BlobStorageService.NAMESPACE;
514
+ }
515
+ else {
516
+ throw new GeneralError("engineCore", "componentUnknownType", {
517
+ type,
518
+ componentType: "blobStorageComponent"
519
+ });
520
+ }
521
+ const finalInstanceType = overrideInstanceType ?? instanceType;
522
+ context.componentInstances.push({
523
+ instanceType: finalInstanceType,
524
+ component
525
+ });
526
+ ComponentFactory.register(finalInstanceType, () => component);
527
+ return finalInstanceType;
528
+ }
529
+
530
+ // Copyright 2024 IOTA Stiftung.
531
+ // SPDX-License-Identifier: Apache-2.0.
532
+ /**
533
+ * Initialise a faucet connector.
534
+ * @param engineCore The engine core.
535
+ * @param context The context for the engine.
536
+ * @param instanceConfig The instance config.
537
+ * @param overrideInstanceType The instance type to override the default.
538
+ * @returns The name of the instance created.
539
+ * @throws GeneralError if the connector type is unknown.
540
+ */
541
+ function initialiseFaucetConnector(engineCore, context, instanceConfig, overrideInstanceType) {
542
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
543
+ element: `Faucet Connector: ${instanceConfig.type}`
544
+ }));
545
+ const type = instanceConfig.type;
546
+ let connector;
547
+ let instanceType;
548
+ if (type === FaucetConnectorType.Iota) {
549
+ const dltConfig = context.config.dltConfig?.find(dlt => dlt.type === context.defaultTypes.dltConfig);
550
+ connector = new IotaFaucetConnector({
551
+ ...instanceConfig.options,
552
+ config: {
553
+ ...dltConfig?.options?.config,
554
+ ...instanceConfig.options.config
555
+ }
556
+ });
557
+ instanceType = IotaFaucetConnector.NAMESPACE;
558
+ }
559
+ else if (type === FaucetConnectorType.EntityStorage) {
560
+ connector = new EntityStorageFaucetConnector(instanceConfig.options);
561
+ instanceType = EntityStorageFaucetConnector.NAMESPACE;
562
+ }
563
+ else {
564
+ throw new GeneralError("engineCore", "connectorUnknownType", {
565
+ type,
566
+ connectorType: "faucetConnector"
567
+ });
568
+ }
569
+ const finalInstanceType = overrideInstanceType ?? instanceType;
570
+ context.componentInstances.push({
571
+ instanceType: finalInstanceType,
572
+ component: connector
573
+ });
574
+ FaucetConnectorFactory.register(finalInstanceType, () => connector);
575
+ return finalInstanceType;
576
+ }
577
+
578
+ // Copyright 2024 IOTA Stiftung.
579
+ // SPDX-License-Identifier: Apache-2.0.
580
+ /**
581
+ * Initialise the identity connector.
582
+ * @param engineCore The engine core.
583
+ * @param context The context for the engine.
584
+ * @param instanceConfig The instance config.
585
+ * @param overrideInstanceType The instance type to override the default.
586
+ * @returns The name of the instance created.
587
+ * @throws GeneralError if the connector type is unknown.
588
+ */
589
+ function initialiseIdentityConnector(engineCore, context, instanceConfig, overrideInstanceType) {
590
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
591
+ element: `Identity Connector: ${instanceConfig.type}`
592
+ }));
593
+ const type = instanceConfig.type;
594
+ let connector;
595
+ let instanceType;
596
+ if (type === IdentityConnectorType.Iota) {
597
+ const dltConfig = context.config.dltConfig?.find(dlt => dlt.type === context.defaultTypes.dltConfig);
598
+ connector = new IotaIdentityConnector({
599
+ ...instanceConfig.options,
600
+ config: {
601
+ ...dltConfig?.options?.config,
602
+ ...instanceConfig.options.config
603
+ }
604
+ });
605
+ instanceType = IotaIdentityConnector.NAMESPACE;
606
+ }
607
+ else if (type === IdentityConnectorType.EntityStorage) {
608
+ initSchema$4({ includeProfile: false });
609
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.didDocumentEntityStorageType, "IdentityDocument");
610
+ connector = new EntityStorageIdentityConnector({
611
+ vaultConnectorType: context.defaultTypes.vaultConnector,
612
+ ...instanceConfig.options
613
+ });
614
+ instanceType = EntityStorageIdentityConnector.NAMESPACE;
615
+ }
616
+ else {
617
+ throw new GeneralError("engineCore", "connectorUnknownType", {
618
+ type,
619
+ connectorType: "identityConnector"
620
+ });
621
+ }
622
+ const finalInstanceType = overrideInstanceType ?? instanceType;
623
+ context.componentInstances.push({ instanceType: finalInstanceType, component: connector });
624
+ IdentityConnectorFactory.register(finalInstanceType, () => connector);
625
+ return finalInstanceType;
626
+ }
627
+ /**
628
+ * Initialise the identity component.
629
+ * @param engineCore The engine core.
630
+ * @param context The context for the engine.
631
+ * @param instanceConfig The instance config.
632
+ * @param overrideInstanceType The instance type to override the default.
633
+ * @returns The name of the instance created.
634
+ * @throws GeneralError if the component type is unknown.
635
+ */
636
+ function initialiseIdentityComponent(engineCore, context, instanceConfig, overrideInstanceType) {
637
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
638
+ element: `Identity Component: ${instanceConfig.type}`
639
+ }));
640
+ const type = instanceConfig.type;
641
+ let component;
642
+ let instanceType;
643
+ if (type === IdentityComponentType.Service) {
644
+ component = new IdentityService(instanceConfig.options);
645
+ instanceType = IdentityService.NAMESPACE;
646
+ }
647
+ else {
648
+ throw new GeneralError("engineCore", "componentUnknownType", {
649
+ type,
650
+ componentType: "identityComponent"
651
+ });
652
+ }
653
+ const finalInstanceType = overrideInstanceType ?? instanceType;
654
+ context.componentInstances.push({ instanceType: finalInstanceType, component });
655
+ ComponentFactory.register(finalInstanceType, () => component);
656
+ return finalInstanceType;
657
+ }
658
+
659
+ // Copyright 2024 IOTA Stiftung.
660
+ // SPDX-License-Identifier: Apache-2.0.
661
+ /**
662
+ * Initialise the identity profile connector.
663
+ * @param engineCore The engine core.
664
+ * @param context The context for the engine.
665
+ * @param instanceConfig The instance config.
666
+ * @param overrideInstanceType The instance type to override the default.
667
+ * @returns The name of the instance created.
668
+ * @throws GeneralError if the connector type is unknown.
669
+ */
670
+ function initialiseIdentityProfileConnector(engineCore, context, instanceConfig, overrideInstanceType) {
671
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
672
+ element: `Identity Profile Connector: ${instanceConfig.type}`
673
+ }));
674
+ const type = instanceConfig.type;
675
+ let connector;
676
+ let instanceType;
677
+ if (type === IdentityProfileConnectorType.EntityStorage) {
678
+ initSchema$4({ includeDocument: false });
679
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.profileEntityStorageType, "IdentityProfile");
680
+ connector = new EntityStorageIdentityProfileConnector(instanceConfig.options);
681
+ instanceType = EntityStorageIdentityProfileConnector.NAMESPACE;
682
+ }
683
+ else {
684
+ throw new GeneralError("engineCore", "connectorUnknownType", {
685
+ type,
686
+ serviceType: "identityProfile"
687
+ });
688
+ }
689
+ const finalInstanceType = overrideInstanceType ?? instanceType;
690
+ context.componentInstances.push({
691
+ instanceType: finalInstanceType,
692
+ component: connector
693
+ });
694
+ IdentityProfileConnectorFactory.register(finalInstanceType, () => connector);
695
+ return finalInstanceType;
696
+ }
697
+ /**
698
+ * Initialise the identity profile component.
699
+ * @param engineCore The engine core.
700
+ * @param context The context for the engine.
701
+ * @param instanceConfig The instance config.
702
+ * @param overrideInstanceType The instance type to override the default.
703
+ * @returns The name of the instance created.
704
+ * @throws GeneralError if the component type is unknown.
705
+ */
706
+ function initialiseIdentityProfileComponent(engineCore, context, instanceConfig, overrideInstanceType) {
707
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
708
+ element: `Identity Profile Component: ${instanceConfig.type}`
709
+ }));
710
+ const type = instanceConfig.type;
711
+ let component;
712
+ let instanceType;
713
+ if (type === IdentityProfileComponentType.Service) {
714
+ component = new IdentityProfileService({
715
+ profileEntityConnectorType: context.defaultTypes.identityProfileConnector,
716
+ ...instanceConfig.options
717
+ });
718
+ instanceType = IdentityProfileService.NAMESPACE;
719
+ }
720
+ else {
721
+ throw new GeneralError("engineCore", "componentUnknownType", {
722
+ type,
723
+ componentType: "identityProfileComponent"
724
+ });
725
+ }
726
+ const finalInstanceType = overrideInstanceType ?? instanceType;
727
+ context.componentInstances.push({
728
+ instanceType: finalInstanceType,
729
+ component
730
+ });
731
+ ComponentFactory.register(finalInstanceType, () => component);
732
+ return finalInstanceType;
733
+ }
734
+
735
+ // Copyright 2024 IOTA Stiftung.
736
+ // SPDX-License-Identifier: Apache-2.0.
737
+ /**
738
+ * Initialise the immutable proof component.
739
+ * @param engineCore The engine core.
740
+ * @param context The context for the engine.
741
+ * @param instanceConfig The instance config.
742
+ * @param overrideInstanceType The instance type to override the default.
743
+ * @returns The name of the instance created.
744
+ * @throws GeneralError if the component type is unknown.
745
+ */
746
+ function initialiseImmutableProofComponent(engineCore, context, instanceConfig, overrideInstanceType) {
747
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
748
+ element: `Immutable Proof Component: ${instanceConfig.type}`
749
+ }));
750
+ const type = instanceConfig.type;
751
+ let component;
752
+ let instanceType;
753
+ if (type === ImmutableProofComponentType.Service) {
754
+ initSchema$5();
755
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.immutableProofEntityStorageType, "ImmutableProof");
756
+ component = new ImmutableProofService({
757
+ vaultConnectorType: context.defaultTypes.vaultConnector,
758
+ immutableStorageType: context.defaultTypes.immutableStorageConnector,
759
+ identityConnectorType: context.defaultTypes.identityConnector,
760
+ backgroundTaskConnectorType: context.defaultTypes.backgroundTaskConnector,
761
+ ...instanceConfig.options
762
+ });
763
+ instanceType = ImmutableProofService.NAMESPACE;
764
+ }
765
+ else {
766
+ throw new GeneralError("engineCore", "componentUnknownType", {
767
+ type,
768
+ componentType: "immutableProofComponent"
769
+ });
770
+ }
771
+ const finalInstanceType = overrideInstanceType ?? instanceType;
772
+ context.componentInstances.push({
773
+ instanceType: finalInstanceType,
774
+ component
775
+ });
776
+ ComponentFactory.register(finalInstanceType, () => component);
777
+ return finalInstanceType;
778
+ }
779
+
780
+ // Copyright 2024 IOTA Stiftung.
781
+ // SPDX-License-Identifier: Apache-2.0.
782
+ /**
783
+ * Initialise the immutable storage connector.
784
+ * @param engineCore The engine core.
785
+ * @param context The context for the engine.
786
+ * @param instanceConfig The instance config.
787
+ * @param overrideInstanceType The instance type to override the default.
788
+ * @returns The name of the instance created.
789
+ * @throws GeneralError if the connector type is unknown.
790
+ */
791
+ function initialiseImmutableStorageConnector(engineCore, context, instanceConfig, overrideInstanceType) {
792
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
793
+ element: `Immutable Storage Connector: ${instanceConfig.type}`
794
+ }));
795
+ const type = instanceConfig.type;
796
+ let connector;
797
+ let instanceType;
798
+ if (type === ImmutableStorageConnectorType.Iota) {
799
+ const dltConfig = context.config.dltConfig?.find(dlt => dlt.type === context.defaultTypes.dltConfig);
800
+ connector = new IotaImmutableStorageConnector({
801
+ ...instanceConfig.options,
802
+ config: {
803
+ ...dltConfig?.options?.config,
804
+ ...instanceConfig.options.config
805
+ }
806
+ });
807
+ instanceType = IotaImmutableStorageConnector.NAMESPACE;
808
+ }
809
+ else if (type === ImmutableStorageConnectorType.EntityStorage) {
810
+ initSchema$6();
811
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.immutableStorageEntityStorageType, "ImmutableItem");
812
+ connector = new EntityStorageImmutableStorageConnector(instanceConfig.options);
813
+ instanceType = EntityStorageImmutableStorageConnector.NAMESPACE;
814
+ }
815
+ else {
816
+ throw new GeneralError("engineCore", "connectorUnknownType", {
817
+ type,
818
+ connectorType: "immutableStorageConnector"
819
+ });
820
+ }
821
+ const finalInstanceType = overrideInstanceType ?? instanceType;
822
+ context.componentInstances.push({
823
+ instanceType: finalInstanceType,
824
+ component: connector
825
+ });
826
+ ImmutableStorageConnectorFactory.register(finalInstanceType, () => connector);
827
+ return finalInstanceType;
828
+ }
829
+
830
+ // Copyright 2024 IOTA Stiftung.
831
+ // SPDX-License-Identifier: Apache-2.0.
832
+ /**
833
+ * Initialise the logging connector.
834
+ * @param engineCore The engine core.
835
+ * @param context The engine core context.
836
+ * @param instanceConfig The instance config.
837
+ * @param overrideInstanceType The instance type to override the default.
838
+ * @returns The name of the instance created.
839
+ * @throws GeneralError if the connector type is unknown.
840
+ */
841
+ function initialiseLoggingConnector(engineCore, context, instanceConfig, overrideInstanceType) {
842
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
843
+ element: `Logging Connector: ${instanceConfig.type}`
844
+ }));
845
+ const type = instanceConfig.type;
846
+ let connector;
847
+ let instanceType;
848
+ if (type === LoggingConnectorType.Console) {
849
+ connector = new ConsoleLoggingConnector(instanceConfig.options);
850
+ instanceType = ConsoleLoggingConnector.NAMESPACE;
851
+ }
852
+ else if (type === LoggingConnectorType.EntityStorage) {
853
+ initSchema$7();
854
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.logEntryStorageConnectorType, "LogEntry");
855
+ connector = new EntityStorageLoggingConnector(instanceConfig.options);
856
+ instanceType = EntityStorageLoggingConnector.NAMESPACE;
857
+ }
858
+ else if (type === LoggingConnectorType.Multi) {
859
+ connector = new MultiLoggingConnector(instanceConfig.options);
860
+ instanceType = MultiLoggingConnector.NAMESPACE;
861
+ }
862
+ else {
863
+ throw new GeneralError("engineCore", "connectorUnknownType", {
864
+ type,
865
+ connectorType: "loggingConnector"
866
+ });
867
+ }
868
+ const finalInstanceType = overrideInstanceType ?? instanceType;
869
+ context.componentInstances.push({ instanceType: finalInstanceType, component: connector });
870
+ LoggingConnectorFactory.register(finalInstanceType, () => connector);
871
+ return finalInstanceType;
872
+ }
873
+ /**
874
+ * Initialise the logging component.
875
+ * @param engineCore The engine core.
876
+ * @param context The context for the engine.
877
+ * @param instanceConfig The instance config.
878
+ * @param overrideInstanceType The instance type to override the default.
879
+ * @returns The name of the instance created.
880
+ * @throws GeneralError if the component type is unknown.
881
+ */
882
+ function initialiseLoggingComponent(engineCore, context, instanceConfig, overrideInstanceType) {
883
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
884
+ element: `Logging Component: ${instanceConfig.type}`
885
+ }));
886
+ const type = instanceConfig.type;
887
+ let component;
888
+ let instanceType;
889
+ if (type === LoggingComponentType.Service) {
890
+ component = new LoggingService({
891
+ loggingConnectorType: context.defaultTypes.loggingConnector,
892
+ ...instanceConfig.options
893
+ });
894
+ instanceType = LoggingService.NAMESPACE;
895
+ }
896
+ else {
897
+ throw new GeneralError("engineCore", "componentUnknownType", {
898
+ type,
899
+ componentType: "loggingComponent"
900
+ });
901
+ }
902
+ const finalInstanceType = overrideInstanceType ?? instanceType;
903
+ context.componentInstances.push({ instanceType: finalInstanceType, component });
904
+ ComponentFactory.register(finalInstanceType, () => component);
905
+ return finalInstanceType;
906
+ }
907
+
908
+ // Copyright 2024 IOTA Stiftung.
909
+ // SPDX-License-Identifier: Apache-2.0.
910
+ /**
911
+ * Initialise the NFT connector.
912
+ * @param engineCore The engine core.
913
+ * @param context The context for the engine.
914
+ * @param instanceConfig The instance config.
915
+ * @param overrideInstanceType The instance type to override the default.
916
+ * @returns The name of the instance created.
917
+ * @throws GeneralError if the connector type is unknown.
918
+ */
919
+ function initialiseNftConnector(engineCore, context, instanceConfig, overrideInstanceType) {
920
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
921
+ element: `NFT Connector: ${instanceConfig.type}`
922
+ }));
923
+ const type = instanceConfig.type;
924
+ let connector;
925
+ let instanceType;
926
+ if (type === NftConnectorType.EntityStorage) {
927
+ initSchema$8();
928
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.nftEntityStorageType, "Nft");
929
+ connector = new EntityStorageNftConnector(instanceConfig.options);
930
+ instanceType = EntityStorageNftConnector.NAMESPACE;
931
+ }
932
+ else if (type === NftConnectorType.Iota) {
933
+ connector = new IotaNftConnector({
934
+ ...instanceConfig.options
935
+ });
936
+ instanceType = IotaNftConnector.NAMESPACE;
937
+ }
938
+ else {
939
+ throw new GeneralError("engineCore", "connectorUnknownType", {
940
+ type,
941
+ connectorType: "nftConnector"
942
+ });
943
+ }
944
+ const finalInstanceType = overrideInstanceType ?? instanceType;
945
+ context.componentInstances.push({
946
+ instanceType: finalInstanceType,
947
+ component: connector
948
+ });
949
+ NftConnectorFactory.register(finalInstanceType, () => connector);
950
+ return finalInstanceType;
951
+ }
952
+ /**
953
+ * Initialise the NFT component.
954
+ * @param engineCore The engine core.
955
+ * @param context The context for the engine.
956
+ * @param instanceConfig The instance config.
957
+ * @param overrideInstanceType The instance type to override the default.
958
+ * @returns The name of the instance created.
959
+ * @throws GeneralError if the component type is unknown.
960
+ */
961
+ function initialiseNftComponent(engineCore, context, instanceConfig, overrideInstanceType) {
962
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
963
+ element: `Nft Storage Component: ${instanceConfig.type}`
964
+ }));
965
+ const type = instanceConfig.type;
966
+ let component;
967
+ let instanceType;
968
+ if (type === NftComponentType.Service) {
969
+ component = new NftService(instanceConfig.options);
970
+ instanceType = NftService.NAMESPACE;
971
+ }
972
+ else {
973
+ throw new GeneralError("engineCore", "componentUnknownType", {
974
+ type,
975
+ componentType: "nftComponent"
976
+ });
977
+ }
978
+ const finalInstanceType = overrideInstanceType ?? instanceType;
979
+ context.componentInstances.push({
980
+ instanceType: finalInstanceType,
981
+ component
982
+ });
983
+ ComponentFactory.register(finalInstanceType, () => component);
984
+ return finalInstanceType;
985
+ }
986
+
987
+ // Copyright 2024 IOTA Stiftung.
988
+ // SPDX-License-Identifier: Apache-2.0.
989
+ /**
990
+ * Initialise a telemetry connector.
991
+ * @param engineCore The engine core.
992
+ * @param context The context for the engine.
993
+ * @param instanceConfig The instance config.
994
+ * @param overrideInstanceType The instance type to override the default.
995
+ * @returns The name of the instance created.
996
+ * @throws GeneralError if the connector type is unknown.
997
+ */
998
+ function initialiseTelemetryConnector(engineCore, context, instanceConfig, overrideInstanceType) {
999
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
1000
+ element: `Telemetry Connector: ${instanceConfig.type}`
1001
+ }));
1002
+ const type = instanceConfig.type;
1003
+ let connector;
1004
+ let instanceType;
1005
+ if (type === TelemetryConnectorType.EntityStorage) {
1006
+ initSchema$9();
1007
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.telemetryMetricStorageConnectorType, "TelemetryMetric");
1008
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.telemetryMetricValueStorageConnectorType, "TelemetryMetricValue");
1009
+ connector = new EntityStorageTelemetryConnector({
1010
+ loggingConnectorType: context.defaultTypes.loggingConnector,
1011
+ ...instanceConfig.options
1012
+ });
1013
+ instanceType = EntityStorageTelemetryConnector.NAMESPACE;
1014
+ }
1015
+ else {
1016
+ throw new GeneralError("engineCore", "connectorUnknownType", {
1017
+ type,
1018
+ connectorType: "telemetryConnector"
1019
+ });
1020
+ }
1021
+ const finalInstanceType = overrideInstanceType ?? instanceType;
1022
+ context.componentInstances.push({ instanceType: finalInstanceType, component: connector });
1023
+ TelemetryConnectorFactory.register(finalInstanceType, () => connector);
1024
+ return finalInstanceType;
1025
+ }
1026
+ /**
1027
+ * Initialise the telemetry component.
1028
+ * @param engineCore The engine core.
1029
+ * @param context The context for the engine.
1030
+ * @param instanceConfig The instance config.
1031
+ * @param overrideInstanceType The instance type to override the default.
1032
+ * @returns The name of the instance created.
1033
+ * @throws GeneralError if the component type is unknown.
1034
+ */
1035
+ function initialiseTelemetryComponent(engineCore, context, instanceConfig, overrideInstanceType) {
1036
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
1037
+ element: `Telemetry Component: ${instanceConfig.type}`
1038
+ }));
1039
+ const type = instanceConfig.type;
1040
+ let component;
1041
+ let instanceType;
1042
+ if (type === TelemetryComponentType.Service) {
1043
+ component = new TelemetryService({
1044
+ telemetryConnectorType: context.defaultTypes.telemetryConnector,
1045
+ ...instanceConfig.options
1046
+ });
1047
+ instanceType = TelemetryService.NAMESPACE;
1048
+ }
1049
+ else {
1050
+ throw new GeneralError("engineCore", "componentUnknownType", {
1051
+ type,
1052
+ componentType: "telemetryComponent"
1053
+ });
1054
+ }
1055
+ const finalInstanceType = overrideInstanceType ?? instanceType;
1056
+ context.componentInstances.push({ instanceType: finalInstanceType, component });
1057
+ ComponentFactory.register(finalInstanceType, () => component);
1058
+ return finalInstanceType;
1059
+ }
1060
+
1061
+ // Copyright 2024 IOTA Stiftung.
1062
+ // SPDX-License-Identifier: Apache-2.0.
1063
+ /**
1064
+ * Initialise the vault connector.
1065
+ * @param engineCore The engine core.
1066
+ * @param context The context for the engine.
1067
+ * @param instanceConfig The instance config.
1068
+ * @param overrideInstanceType The instance type to override the default.
1069
+ * @returns The name of the instance created.
1070
+ * @throws GeneralError if the connector type is unknown.
1071
+ */
1072
+ function initialiseVaultConnector(engineCore, context, instanceConfig, overrideInstanceType) {
1073
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
1074
+ element: `Vault Connector: ${instanceConfig.type}`
1075
+ }));
1076
+ const type = instanceConfig.type;
1077
+ let connector;
1078
+ let instanceType;
1079
+ if (type === VaultConnectorType.EntityStorage) {
1080
+ initSchema$a();
1081
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.vaultKeyEntityStorageType, "VaultKey");
1082
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.vaultSecretEntityStorageType, "VaultSecret");
1083
+ connector = new EntityStorageVaultConnector(instanceConfig.options);
1084
+ instanceType = EntityStorageVaultConnector.NAMESPACE;
1085
+ }
1086
+ else if (type === VaultConnectorType.Hashicorp) {
1087
+ connector = new HashicorpVaultConnector(instanceConfig.options);
1088
+ instanceType = HashicorpVaultConnector.NAMESPACE;
1089
+ }
1090
+ else {
1091
+ throw new GeneralError("engineCore", "connectorUnknownType", {
1092
+ type,
1093
+ connectorType: "vaultConnector"
1094
+ });
1095
+ }
1096
+ const finalInstanceType = overrideInstanceType ?? instanceType;
1097
+ context.componentInstances.push({ instanceType: finalInstanceType, component: connector });
1098
+ VaultConnectorFactory.register(finalInstanceType, () => connector);
1099
+ return finalInstanceType;
1100
+ }
1101
+
1102
+ // Copyright 2024 IOTA Stiftung.
1103
+ // SPDX-License-Identifier: Apache-2.0.
1104
+ /**
1105
+ * Initialise a wallet connector.
1106
+ * @param engineCore The engine core.
1107
+ * @param context The context for the node.
1108
+ * @param instanceConfig The instance config.
1109
+ * @param overrideInstanceType The instance type to override the default.
1110
+ * @returns The name of the instance created.
1111
+ * @throws GeneralError if the connector type is unknown.
1112
+ */
1113
+ function initialiseWalletConnector(engineCore, context, instanceConfig, overrideInstanceType) {
1114
+ engineCore.logInfo(I18n.formatMessage("engineCore.configuring", {
1115
+ element: `Wallet Connector: ${instanceConfig.type}`
1116
+ }));
1117
+ const type = instanceConfig.type;
1118
+ let connector;
1119
+ let instanceType;
1120
+ if (type === WalletConnectorType.Iota) {
1121
+ const dltConfig = context.config.dltConfig?.find(dlt => dlt.type === context.defaultTypes.dltConfig);
1122
+ connector = new IotaWalletConnector({
1123
+ ...instanceConfig.options,
1124
+ config: {
1125
+ ...dltConfig?.options?.config,
1126
+ ...instanceConfig.options.config
1127
+ }
1128
+ });
1129
+ instanceType = IotaWalletConnector.NAMESPACE;
1130
+ }
1131
+ else if (type === WalletConnectorType.EntityStorage) {
1132
+ connector = new EntityStorageWalletConnector({
1133
+ vaultConnectorType: context.defaultTypes.vaultConnector,
1134
+ faucetConnectorType: context.defaultTypes.faucetConnector,
1135
+ ...instanceConfig.options
1136
+ });
1137
+ instanceType = EntityStorageWalletConnector.NAMESPACE;
1138
+ }
1139
+ else {
1140
+ throw new GeneralError("engineCore", "connectorUnknownType", {
1141
+ type,
1142
+ connectorType: "walletConnector"
1143
+ });
1144
+ }
1145
+ const finalInstanceType = overrideInstanceType ?? instanceType;
1146
+ context.componentInstances.push({
1147
+ instanceType: finalInstanceType,
1148
+ component: connector
1149
+ });
1150
+ WalletConnectorFactory.register(finalInstanceType, () => connector);
1151
+ return finalInstanceType;
1152
+ }
1153
+ /**
1154
+ * Initialise the wallet storage.
1155
+ * @param engineCore The engine core.
1156
+ * @param context The context for the engine.
1157
+ * @param instanceConfig The instance config.
1158
+ * @param overrideInstanceType The instance type to override the default.
1159
+ * @returns Nothing.
1160
+ * @throws GeneralError if the connector type is unknown.
1161
+ */
1162
+ function initialiseWalletStorage(engineCore, context, instanceConfig, overrideInstanceType) {
1163
+ const type = instanceConfig.type;
1164
+ if (type === WalletConnectorType.Iota) ;
1165
+ else if (type === WalletConnectorType.EntityStorage) {
1166
+ initSchema$b();
1167
+ initialiseEntityStorageConnector(engineCore, context, instanceConfig.options?.walletAddressEntityStorageType, "WalletAddress");
1168
+ }
1169
+ else {
1170
+ throw new GeneralError("engineCore", "connectorUnknownType", {
1171
+ type,
1172
+ connectorType: "walletConnector"
1173
+ });
1174
+ }
1175
+ return undefined;
1176
+ }
1177
+
1178
+ // Copyright 2024 IOTA Stiftung.
1179
+ // SPDX-License-Identifier: Apache-2.0.
1180
+ /**
1181
+ * Core for the engine.
1182
+ */
1183
+ class EngineCore {
1184
+ /**
1185
+ * Name for the engine logger.
1186
+ */
1187
+ static LOGGER_TYPE_NAME = "engine";
1188
+ /**
1189
+ * Runtime name for the class.
1190
+ */
1191
+ CLASS_NAME = "EngineCore";
1192
+ /**
1193
+ * The core context.
1194
+ * @internal
1195
+ */
1196
+ _context;
1197
+ /**
1198
+ * The state storage interface.
1199
+ * @internal
1200
+ */
1201
+ _stateStorage;
1202
+ /**
1203
+ * The logging connector for the engine.
1204
+ * @internal
1205
+ */
1206
+ _engineLoggingConnector;
1207
+ /**
1208
+ * Skip the bootstrap process.
1209
+ * @internal
1210
+ */
1211
+ _skipBootstrap;
1212
+ /**
1213
+ * The type initialisers.
1214
+ * @internal
1215
+ */
1216
+ _typeInitialisers;
1217
+ /**
1218
+ * Method for bootstrapping any data for the engine.
1219
+ * @internal
1220
+ */
1221
+ _customBootstrap;
1222
+ /**
1223
+ * Create a new instance of EngineCore.
1224
+ * @param options The options for the engine.
1225
+ */
1226
+ constructor(options) {
1227
+ options = options ?? {};
1228
+ options.config = options.config ?? {};
1229
+ options.config.debug = options.config.debug ?? false;
1230
+ options.config.silent = options.config.silent ?? false;
1231
+ this._skipBootstrap = options.skipBootstrap ?? false;
1232
+ this._customBootstrap = options.customBootstrap;
1233
+ this._typeInitialisers = [];
1234
+ this._context = {
1235
+ config: options.config,
1236
+ defaultTypes: {},
1237
+ componentInstances: [],
1238
+ state: { bootstrappedComponents: [] },
1239
+ stateDirty: false
1240
+ };
1241
+ this._stateStorage = options.stateStorage;
1242
+ this.addCoreTypeInitialisers();
1243
+ }
1244
+ /**
1245
+ * Add a type initialiser.
1246
+ * @param type The type to add the initialiser for.
1247
+ * @param typeConfig The type config.
1248
+ * @param initialiser The initialiser to add.
1249
+ */
1250
+ addTypeInitialiser(type, typeConfig, initialiser) {
1251
+ if (!Is.empty(typeConfig)) {
1252
+ this._typeInitialisers.push({
1253
+ type,
1254
+ typeConfig,
1255
+ initialiser: initialiser
1256
+ });
1257
+ }
1258
+ }
1259
+ /**
1260
+ * Start the engine core.
1261
+ * @returns Nothing.
1262
+ */
1263
+ async start() {
1264
+ this.setupEngineLogger();
1265
+ this.logInfo(I18n.formatMessage("engineCore.starting"));
1266
+ if (this._context.config.debug) {
1267
+ this.logInfo(I18n.formatMessage("engineCore.debuggingEnabled"));
1268
+ }
1269
+ if (await this.stateLoad()) {
1270
+ for (const { type, typeConfig, initialiser } of this._typeInitialisers) {
1271
+ this.initialiseTypeConfig(type, typeConfig, initialiser);
1272
+ }
1273
+ const canContinue = await this.bootstrap();
1274
+ if (canContinue) {
1275
+ this.logInfo(I18n.formatMessage("engineCore.componentsStarting"));
1276
+ for (const instance of this._context.componentInstances) {
1277
+ if (Is.function(instance.component.start)) {
1278
+ this.logInfo(I18n.formatMessage("engineCore.componentStarting", { element: instance.instanceType }));
1279
+ await instance.component.start(this._context.state.nodeIdentity, EngineCore.LOGGER_TYPE_NAME);
1280
+ }
1281
+ }
1282
+ this.logInfo(I18n.formatMessage("engineCore.componentsComplete"));
1283
+ }
1284
+ }
1285
+ this.logInfo(I18n.formatMessage("engineCore.started"));
1286
+ }
1287
+ /**
1288
+ * Stop the engine core.
1289
+ * @returns Nothing.
1290
+ */
1291
+ async stop() {
1292
+ this.logInfo(I18n.formatMessage("engineCore.stopping"));
1293
+ this.logInfo(I18n.formatMessage("engineCore.componentsStopping"));
1294
+ for (const instance of this._context.componentInstances) {
1295
+ if (Is.function(instance.component.stop)) {
1296
+ this.logInfo(I18n.formatMessage("engineCore.componentStopping", { element: instance.instanceType }));
1297
+ try {
1298
+ await instance.component.stop(this._context.state.nodeIdentity, EngineCore.LOGGER_TYPE_NAME);
1299
+ }
1300
+ catch (err) {
1301
+ this.logError(new GeneralError(this.CLASS_NAME, "componentStopFailed", {
1302
+ component: instance.instanceType
1303
+ }, BaseError.fromError(err)));
1304
+ }
1305
+ }
1306
+ }
1307
+ this.logInfo(I18n.formatMessage("engineCore.componentsStopped"));
1308
+ this.logInfo(I18n.formatMessage("engineCore.stopped"));
1309
+ }
1310
+ /**
1311
+ * Log info.
1312
+ * @param message The message to log.
1313
+ */
1314
+ logInfo(message) {
1315
+ this._engineLoggingConnector?.log({
1316
+ source: this.CLASS_NAME,
1317
+ level: "info",
1318
+ message
1319
+ });
1320
+ }
1321
+ /**
1322
+ * Log error.
1323
+ * @param error The error to log.
1324
+ */
1325
+ logError(error) {
1326
+ const formattedErrors = ErrorHelper.localizeErrors(error);
1327
+ for (const formattedError of formattedErrors) {
1328
+ let message = Is.stringValue(formattedError.source)
1329
+ ? `${formattedError.source}: ${formattedError.message}`
1330
+ : formattedError.message;
1331
+ if (this._context.config.debug && Is.stringValue(formattedError.stack)) {
1332
+ message += `\n${formattedError.stack}`;
1333
+ }
1334
+ this._engineLoggingConnector?.log({
1335
+ source: this.CLASS_NAME,
1336
+ level: "error",
1337
+ message
1338
+ });
1339
+ }
1340
+ }
1341
+ /**
1342
+ * Get the config for the engine.
1343
+ * @returns The config for the engine.
1344
+ */
1345
+ getConfig() {
1346
+ return this._context.config;
1347
+ }
1348
+ /**
1349
+ * Get the state of the engine.
1350
+ * @returns The state of the engine.
1351
+ */
1352
+ getState() {
1353
+ return this._context.state;
1354
+ }
1355
+ /**
1356
+ * Get the types for the component.
1357
+ * @returns The default types.
1358
+ */
1359
+ getDefaultTypes() {
1360
+ return this._context.defaultTypes;
1361
+ }
1362
+ /**
1363
+ * Initialise the types from connector.
1364
+ * @param typeKey The key for the default types.
1365
+ * @param instanceMethod The function to initialise the instance.
1366
+ * @internal
1367
+ */
1368
+ initialiseTypeConfig(typeKey, typeConfig, instanceMethod) {
1369
+ if (Is.arrayValue(typeConfig)) {
1370
+ for (let i = 0; i < typeConfig.length; i++) {
1371
+ const instanceType = instanceMethod(this, this._context, typeConfig[i], typeConfig[i].overrideInstanceType);
1372
+ if (Is.stringValue(instanceType) &&
1373
+ (Is.empty(this._context.defaultTypes[typeKey]) || typeConfig[i].isDefault)) {
1374
+ this._context.defaultTypes[typeKey] = instanceType;
1375
+ }
1376
+ }
1377
+ }
1378
+ }
1379
+ /**
1380
+ * Setup the engine logger.
1381
+ * @internal
1382
+ */
1383
+ setupEngineLogger() {
1384
+ if (!this._context.config.silent) {
1385
+ const engineLogger = new ConsoleLoggingConnector({
1386
+ config: {
1387
+ translateMessages: true,
1388
+ hideGroups: true
1389
+ }
1390
+ });
1391
+ this._context.componentInstances.push({
1392
+ instanceType: EngineCore.LOGGER_TYPE_NAME,
1393
+ component: engineLogger
1394
+ });
1395
+ LoggingConnectorFactory.register(EngineCore.LOGGER_TYPE_NAME, () => engineLogger);
1396
+ this._engineLoggingConnector = engineLogger;
1397
+ this._context.defaultTypes.loggingConnector = EngineCore.LOGGER_TYPE_NAME;
1398
+ }
1399
+ }
1400
+ /**
1401
+ * Load the state.
1402
+ * @returns True if the state was loaded and can continue.
1403
+ * @internal
1404
+ */
1405
+ async stateLoad() {
1406
+ if (this._stateStorage) {
1407
+ try {
1408
+ this._context.state = (await this._stateStorage.load(this)) ?? {
1409
+ bootstrappedComponents: []
1410
+ };
1411
+ this._context.state.bootstrappedComponents ??= [];
1412
+ this._context.stateDirty = false;
1413
+ return true;
1414
+ }
1415
+ catch (err) {
1416
+ this.logError(BaseError.fromError(err));
1417
+ return false;
1418
+ }
1419
+ }
1420
+ return true;
1421
+ }
1422
+ /**
1423
+ * Save the state.
1424
+ * @internal
1425
+ */
1426
+ async stateSave() {
1427
+ if (this._stateStorage && !Is.empty(this._context.state) && this._context.stateDirty) {
1428
+ try {
1429
+ await this._stateStorage.save(this, this._context.state);
1430
+ this._context.stateDirty = false;
1431
+ }
1432
+ catch (err) {
1433
+ this.logError(BaseError.fromError(err));
1434
+ }
1435
+ }
1436
+ }
1437
+ /**
1438
+ * Bootstrap the engine.
1439
+ * @returns True if the engine can continue.
1440
+ * @internal
1441
+ */
1442
+ async bootstrap() {
1443
+ let canContinue = true;
1444
+ if (!this._skipBootstrap) {
1445
+ this.logInfo(I18n.formatMessage("engineCore.bootstrapStarted"));
1446
+ try {
1447
+ // First bootstrap the components.
1448
+ if (Is.arrayValue(this._context.state?.bootstrappedComponents)) {
1449
+ for (const instance of this._context.componentInstances) {
1450
+ if (Is.function(instance.component.bootstrap)) {
1451
+ const bootstrapName = `component-${instance.component.CLASS_NAME}-${instance.instanceType}`;
1452
+ if (!this._context.state.bootstrappedComponents.includes(bootstrapName)) {
1453
+ this.logInfo(I18n.formatMessage("engineCore.bootstrapping", {
1454
+ element: bootstrapName
1455
+ }));
1456
+ const bootstrapSuccess = await instance.component.bootstrap(EngineCore.LOGGER_TYPE_NAME);
1457
+ // If the bootstrap method failed then throw an error
1458
+ if (!bootstrapSuccess) {
1459
+ throw new GeneralError(this.CLASS_NAME, "bootstrapFailed", {
1460
+ component: `${instance.component.CLASS_NAME}:${instance.instanceType}`
1461
+ });
1462
+ }
1463
+ // Otherwise add the component to the bootstrapped list and set the state as dirty
1464
+ this._context.state.bootstrappedComponents.push(bootstrapName);
1465
+ this._context.stateDirty = true;
1466
+ }
1467
+ }
1468
+ }
1469
+ // Now perform any custom bootstrap operations
1470
+ if (canContinue && Is.function(this._customBootstrap)) {
1471
+ await this._customBootstrap(this, this._context);
1472
+ }
1473
+ }
1474
+ }
1475
+ catch (err) {
1476
+ canContinue = false;
1477
+ this.logError(BaseError.fromError(err));
1478
+ }
1479
+ finally {
1480
+ await this.stateSave();
1481
+ if (canContinue) {
1482
+ this.logInfo(I18n.formatMessage("engineCore.bootstrapComplete"));
1483
+ }
1484
+ }
1485
+ }
1486
+ return canContinue;
1487
+ }
1488
+ /**
1489
+ * Add the core type initializers.
1490
+ * @internal
1491
+ */
1492
+ addCoreTypeInitialisers() {
1493
+ this.addTypeInitialiser("loggingConnector", this._context.config.loggingConnector, initialiseLoggingConnector);
1494
+ this.addTypeInitialiser("loggingComponent", this._context.config.loggingComponent, initialiseLoggingComponent);
1495
+ this.addTypeInitialiser("backgroundTaskConnector", this._context.config.backgroundTaskConnector, initialiseBackgroundTaskConnector);
1496
+ this.addTypeInitialiser("telemetryConnector", this._context.config.telemetryConnector, initialiseTelemetryConnector);
1497
+ this.addTypeInitialiser("telemetryComponent", this._context.config.telemetryComponent, initialiseTelemetryComponent);
1498
+ this.addTypeInitialiser("entityStorageComponent", this._context.config.entityStorageComponent, initialiseEntityStorageComponent);
1499
+ this.addTypeInitialiser("vaultConnector", this._context.config.vaultConnector, initialiseVaultConnector);
1500
+ this.addTypeInitialiser("blobStorageConnector", this._context.config.blobStorageConnector, initialiseBlobStorageConnector);
1501
+ this.addTypeInitialiser("blobStorageComponent", this._context.config.blobStorageComponent, initialiseBlobStorageComponent);
1502
+ this.addTypeInitialiser("immutableStorageConnector", this._context.config.immutableStorageConnector, initialiseImmutableStorageConnector);
1503
+ this.addTypeInitialiser("walletConnector", this._context.config.walletConnector, initialiseWalletStorage);
1504
+ this.addTypeInitialiser("faucetConnector", this._context.config.faucetConnector, initialiseFaucetConnector);
1505
+ this.addTypeInitialiser("walletConnector", this._context.config.walletConnector, initialiseWalletConnector);
1506
+ this.addTypeInitialiser("identityConnector", this._context.config.identityConnector, initialiseIdentityConnector);
1507
+ this.addTypeInitialiser("identityComponent", this._context.config.identityComponent, initialiseIdentityComponent);
1508
+ this.addTypeInitialiser("identityProfileConnector", this._context.config.identityProfileConnector, initialiseIdentityProfileConnector);
1509
+ this.addTypeInitialiser("identityProfileComponent", this._context.config.identityProfileComponent, initialiseIdentityProfileComponent);
1510
+ this.addTypeInitialiser("nftConnector", this._context.config.nftConnector, initialiseNftConnector);
1511
+ this.addTypeInitialiser("nftComponent", this._context.config.nftComponent, initialiseNftComponent);
1512
+ this.addTypeInitialiser("immutableProofComponent", this._context.config.immutableProofComponent, initialiseImmutableProofComponent);
1513
+ this.addTypeInitialiser("attestationConnector", this._context.config.attestationConnector, initialiseAttestationConnector);
1514
+ this.addTypeInitialiser("attestationComponent", this._context.config.attestationComponent, initialiseAttestationComponent);
1515
+ this.addTypeInitialiser("auditableItemGraphComponent", this._context.config.auditableItemGraphComponent, initialiseAuditableItemGraphComponent);
1516
+ this.addTypeInitialiser("auditableItemStreamComponent", this._context.config.auditableItemStreamComponent, initialiseAuditableItemStreamComponent);
1517
+ }
1518
+ }
1519
+
1520
+ // Copyright 2024 IOTA Stiftung.
1521
+ // SPDX-License-Identifier: Apache-2.0.
1522
+ /**
1523
+ * Store state in a file.
1524
+ */
1525
+ class FileStateStorage {
1526
+ /**
1527
+ * Runtime name for the class.
1528
+ */
1529
+ CLASS_NAME = "FileStateStorage";
1530
+ /**
1531
+ * The filename to store the state.
1532
+ * @internal
1533
+ */
1534
+ _filename;
1535
+ /**
1536
+ * Readonly mode state file is not updated.
1537
+ * @internal
1538
+ */
1539
+ _readonlyMode;
1540
+ /**
1541
+ * Create a new instance of FileStateStorage.
1542
+ * @param filename The filename to store the state.
1543
+ * @param readonlyMode Whether the file is in read-only mode.
1544
+ */
1545
+ constructor(filename, readonlyMode = false) {
1546
+ Guards.stringValue(this.CLASS_NAME, "filename", filename);
1547
+ this._filename = filename;
1548
+ this._readonlyMode = readonlyMode;
1549
+ }
1550
+ /**
1551
+ * Method for loading the state.
1552
+ * @param engineCore The engine core to load the state for.
1553
+ * @returns The state of the engine or undefined if it doesn't exist.
1554
+ */
1555
+ async load(engineCore) {
1556
+ try {
1557
+ engineCore.logInfo(I18n.formatMessage(`${StringHelper.camelCase(this.CLASS_NAME)}.loading`, {
1558
+ filename: this._filename
1559
+ }));
1560
+ if (await this.fileExists(this._filename)) {
1561
+ const content = await readFile(this._filename, "utf8");
1562
+ return JSON.parse(content.toString());
1563
+ }
1564
+ }
1565
+ catch (err) {
1566
+ throw new GeneralError(this.CLASS_NAME, "loadingError", { filename: this._filename }, BaseError.fromError(err));
1567
+ }
1568
+ }
1569
+ /**
1570
+ * Method for saving the state.
1571
+ * @param engineCore The engine core to save the state for.
1572
+ * @param state The state of the engine to save.
1573
+ * @returns Nothing.
1574
+ */
1575
+ async save(engineCore, state) {
1576
+ if (!this._readonlyMode) {
1577
+ try {
1578
+ engineCore.logInfo(I18n.formatMessage(`${StringHelper.camelCase(this.CLASS_NAME)}.saving`, {
1579
+ filename: this._filename
1580
+ }));
1581
+ await writeFile(this._filename, JSON.stringify(state), "utf8");
1582
+ }
1583
+ catch (err) {
1584
+ throw new GeneralError(this.CLASS_NAME, "savingError", { filename: this._filename }, BaseError.fromError(err));
1585
+ }
1586
+ }
1587
+ }
1588
+ /**
1589
+ * Does the specified file exist.
1590
+ * @param filename The filename to check for existence.
1591
+ * @returns True if the file exists.
1592
+ * @internal
1593
+ */
1594
+ async fileExists(filename) {
1595
+ try {
1596
+ const stats = await stat(filename);
1597
+ return stats.isFile();
1598
+ }
1599
+ catch {
1600
+ return false;
1601
+ }
1602
+ }
1603
+ }
1604
+
1605
+ // Copyright 2024 IOTA Stiftung.
1606
+ // SPDX-License-Identifier: Apache-2.0.
1607
+ /**
1608
+ * Store state in memory.
1609
+ */
1610
+ class MemoryStateStorage {
1611
+ /**
1612
+ * Runtime name for the class.
1613
+ */
1614
+ CLASS_NAME = "MemoryStateStorage";
1615
+ /**
1616
+ * Readonly mode state file is not updated.
1617
+ * @internal
1618
+ */
1619
+ _readonlyMode;
1620
+ /**
1621
+ * The state object.
1622
+ * @internal
1623
+ */
1624
+ _engineState;
1625
+ /**
1626
+ * Create a new instance of MemoryStateStorage.
1627
+ * @param readonlyMode Whether the file is in read-only mode.
1628
+ */
1629
+ constructor(readonlyMode = false) {
1630
+ this._readonlyMode = readonlyMode;
1631
+ }
1632
+ /**
1633
+ * Method for loading the state.
1634
+ * @param engineCore The engine core to load the state for.
1635
+ * @returns The state of the engine or undefined if it doesn't exist.
1636
+ */
1637
+ async load(engineCore) {
1638
+ engineCore.logInfo(I18n.formatMessage(`${StringHelper.camelCase(this.CLASS_NAME)}.loading`, {
1639
+ filename: this._engineState
1640
+ }));
1641
+ return this._engineState;
1642
+ }
1643
+ /**
1644
+ * Method for saving the state.
1645
+ * @param engineCore The engine core to save the state for.
1646
+ * @param state The state of the engine to save.
1647
+ * @returns Nothing.
1648
+ */
1649
+ async save(engineCore, state) {
1650
+ if (!this._readonlyMode) {
1651
+ engineCore.logInfo(I18n.formatMessage(`${StringHelper.camelCase(this.CLASS_NAME)}.saving`));
1652
+ this._engineState = state;
1653
+ }
1654
+ }
1655
+ }
1656
+
1657
+ export { EngineCore, FileStateStorage, MemoryStateStorage, initialiseEntityStorageConnector };