@twin.org/engine-core 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.
- package/dist/{esm/index.mjs → es/engineCore.js} +205 -266
- package/dist/es/engineCore.js.map +1 -0
- package/dist/es/index.js +8 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IEngineCoreOptions.js +2 -0
- package/dist/es/models/IEngineCoreOptions.js.map +1 -0
- package/dist/es/storage/fileStateStorage.js +92 -0
- package/dist/es/storage/fileStateStorage.js.map +1 -0
- package/dist/es/storage/memoryStateStorage.js +55 -0
- package/dist/es/storage/memoryStateStorage.js.map +1 -0
- package/dist/es/utils/engineModuleHelper.js +46 -0
- package/dist/es/utils/engineModuleHelper.js.map +1 -0
- package/dist/types/engineCore.d.ts +51 -7
- package/dist/types/index.d.ts +5 -4
- package/dist/types/models/IEngineCoreOptions.d.ts +0 -5
- package/dist/types/storage/fileStateStorage.d.ts +1 -1
- package/dist/types/storage/memoryStateStorage.d.ts +1 -1
- package/dist/types/utils/engineModuleHelper.d.ts +17 -0
- package/docs/changelog.md +290 -0
- package/docs/reference/classes/EngineCore.md +156 -14
- package/docs/reference/classes/EngineModuleHelper.md +55 -0
- package/docs/reference/classes/FileStateStorage.md +1 -1
- package/docs/reference/classes/MemoryStateStorage.md +1 -1
- package/docs/reference/index.md +1 -0
- package/docs/reference/interfaces/IEngineCoreOptions.md +0 -14
- package/locales/en.json +10 -10
- package/package.json +23 -9
- package/dist/cjs/index.cjs +0 -649
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engineCore.js","sourceRoot":"","sources":["../../src/engineCore.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAC;AACrE,OAAO,EACN,SAAS,EACT,gBAAgB,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,MAAM,IAAI,YAAY,CAAC,UAAU,CAAC,UAAU,EAAE,iBAAiB,EAAE;4BAChE,SAAS,EAAE,QAAQ,CAAC,SAAS,CAAC,SAAS,EAAE;4BACzC,YAAY,EAAE,QAAQ,CAAC,YAAY;yBACnC,CAAC,CAAC;oBACJ,CAAC;gBACF,CAAC;YACF,CAAC;YACD,8CAA8C;YAC9C,MAAM,eAAe,GAAG,IAAI,CAAC,gBAAgB,CAAC;YAC9C,IAAI,EAAE,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;gBAClC,MAAM,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YACvD,CAAC;YAED,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,GAAG,YAA6B,oBAAoB,CAAC,CAAC,CAAC;QAC9F,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { isMainThread } from \"node:worker_threads\";\nimport { ContextIdStore, type IContextIds } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tComponentFactory,\n\tErrorHelper,\n\tGeneralError,\n\tGuards,\n\tI18n,\n\ttype IError,\n\tIs\n} from \"@twin.org/core\";\nimport type {\n\tEngineTypeInitialiser,\n\tIEngineCore,\n\tIEngineCoreClone,\n\tIEngineCoreConfig,\n\tIEngineCoreContext,\n\tIEngineCoreTypeConfig,\n\tIEngineState,\n\tIEngineStateStorage\n} from \"@twin.org/engine-models\";\nimport { EntitySchemaFactory, type IEntitySchema } from \"@twin.org/entity\";\nimport { ConsoleLoggingConnector } from \"@twin.org/logging-connector-console\";\nimport {\n\ttype ILoggingComponent,\n\tLoggingConnectorFactory,\n\tSilentLoggingConnector\n} from \"@twin.org/logging-models\";\nimport { LoggingService } from \"@twin.org/logging-service\";\nimport { ModuleHelper } from \"@twin.org/modules\";\nimport { nameof, nameofCamelCase } from \"@twin.org/nameof\";\nimport type { IEngineCoreOptions } from \"./models/IEngineCoreOptions.js\";\nimport { MemoryStateStorage } from \"./storage/memoryStateStorage.js\";\n\n/**\n * Core for the engine.\n */\nexport class EngineCore<\n\tC extends IEngineCoreConfig = IEngineCoreConfig,\n\tS extends IEngineState = IEngineState\n> implements IEngineCore<C, S>\n{\n\t/**\n\t * Name for the engine logger component, used for direct console logging.\n\t */\n\tpublic static readonly LOGGING_COMPONENT_TYPE_NAME: string = \"engine-logging-service\";\n\n\t/**\n\t * Name for the engine logger connector, used for direct console logging.\n\t */\n\tpublic static readonly LOGGING_CONNECTOR_TYPE_NAME: string = \"engine-logging-connector\";\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<EngineCore>();\n\n\t/**\n\t * The core context.\n\t */\n\tprotected _context: IEngineCoreContext<C, S>;\n\n\t/**\n\t * The context ID keys.\n\t */\n\tprotected readonly _contextIdKeys: string[];\n\n\t/**\n\t * The context IDs.\n\t */\n\tprotected _contextIds?: IContextIds;\n\n\t/**\n\t * The state storage interface.\n\t * @internal\n\t */\n\tprivate _stateStorage?: IEngineStateStorage<S>;\n\n\t/**\n\t * The logging component for the engine.\n\t * @internal\n\t */\n\tprivate _engineLoggingComponent?: ILoggingComponent;\n\n\t/**\n\t * Skip the bootstrap process.\n\t * @internal\n\t */\n\tprivate _skipBootstrap?: boolean;\n\n\t/**\n\t * The type initialisers.\n\t * @internal\n\t */\n\tprivate _typeInitialisers: {\n\t\ttype: string;\n\t\tmodule: string;\n\t\tmethod: string;\n\t}[];\n\n\t/**\n\t * Is the engine started.\n\t * @internal\n\t */\n\tprivate _isStarted: boolean;\n\n\t/**\n\t * Is the engine a clone.\n\t * @internal\n\t */\n\tprivate _isClone: boolean;\n\n\t/**\n\t * Add type initialisers to the engine.\n\t * @internal\n\t */\n\tprivate readonly _populateTypeInitialisers?: (\n\t\tengineCore: IEngineCore<C, S>,\n\t\tcontext: IEngineCoreContext<C, S>\n\t) => void;\n\n\t/**\n\t * Method for bootstrapping any data for the engine.\n\t * @internal\n\t */\n\tprivate readonly _customBootstrap?: (\n\t\tengineCore: IEngineCore<C, S>,\n\t\tcontext: IEngineCoreContext<C, S>\n\t) => Promise<void>;\n\n\t/**\n\t * Create a new instance of EngineCore.\n\t * @param options The options for the engine.\n\t */\n\tconstructor(options?: IEngineCoreOptions<C, S>) {\n\t\toptions = options ?? {};\n\t\toptions.config = options.config ?? ({} as C);\n\t\toptions.config.debug = options.config.debug ?? false;\n\t\toptions.config.silent = options.config.silent ?? false;\n\t\toptions.config.types ??= {};\n\n\t\tthis._skipBootstrap = options.skipBootstrap ?? false;\n\t\tthis._populateTypeInitialisers = options.populateTypeInitialisers;\n\t\tthis._customBootstrap = options.customBootstrap;\n\t\tthis._typeInitialisers = [];\n\t\tthis._contextIdKeys = [];\n\n\t\tthis._context = {\n\t\t\tconfig: options.config,\n\t\t\tregisteredInstances: {},\n\t\t\tcomponentInstances: [],\n\t\t\tstate: {} as S,\n\t\t\tstateDirty: false\n\t\t};\n\t\tthis._stateStorage = options.stateStorage;\n\t\tthis._isStarted = false;\n\t\tthis._isClone = false;\n\n\t\tif (Is.function(this._populateTypeInitialisers)) {\n\t\t\tthis._populateTypeInitialisers(this, this._context);\n\t\t}\n\t}\n\n\t/**\n\t * Add a type initialiser.\n\t * @param type The type to add the initialiser for.\n\t * @param module The name of the module which contains the initialiser method.\n\t * @param method The name of the method to call.\n\t */\n\tpublic addTypeInitialiser(type: string, module: string, method: string): void {\n\t\tGuards.stringValue(EngineCore.CLASS_NAME, nameof(type), type);\n\t\tGuards.stringValue(EngineCore.CLASS_NAME, nameof(module), module);\n\t\tGuards.stringValue(EngineCore.CLASS_NAME, nameof(method), method);\n\n\t\tconst currentIndex = this._typeInitialisers.findIndex(t => t.type === type);\n\t\tif (currentIndex >= 0) {\n\t\t\tthis._typeInitialisers[currentIndex].module = module;\n\t\t\tthis._typeInitialisers[currentIndex].method = method;\n\t\t} else {\n\t\t\tthis._typeInitialisers.push({\n\t\t\t\ttype,\n\t\t\t\tmodule,\n\t\t\t\tmethod\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Get the type config for a specific type.\n\t * @param type The type to get the config for.\n\t * @returns The type config or undefined if not found.\n\t */\n\tpublic getTypeConfig(type: string): IEngineCoreTypeConfig[] | undefined {\n\t\tGuards.stringValue(EngineCore.CLASS_NAME, nameof(type), type);\n\t\treturn this._context.config.types?.[type];\n\t}\n\n\t/**\n\t * Add a context ID key to the engine.\n\t * @param key The context ID key.\n\t */\n\tpublic addContextIdKey(key: string): void {\n\t\tif (!this._contextIdKeys.includes(key)) {\n\t\t\tthis._contextIdKeys.push(key);\n\t\t}\n\t}\n\n\t/**\n\t * Get the context ID keys for the engine.\n\t * @returns The context IDs keys.\n\t */\n\tpublic getContextIdKeys(): string[] {\n\t\treturn this._contextIdKeys;\n\t}\n\n\t/**\n\t * Add a context ID to the engine.\n\t * @param key The context ID key.\n\t * @param value The context ID value.\n\t */\n\tpublic addContextId(key: string, value: string): void {\n\t\tthis._contextIds ??= {};\n\t\tthis._contextIds[key] = value;\n\t}\n\n\t/**\n\t * Get the context IDs for the engine.\n\t * @returns The context IDs or undefined if none are set.\n\t */\n\tpublic getContextIds(): IContextIds | undefined {\n\t\treturn this._contextIds;\n\t}\n\n\t/**\n\t * Start the engine core.\n\t * @returns True if the start was successful.\n\t */\n\tpublic async start(): Promise<boolean> {\n\t\tif (this._isStarted) {\n\t\t\treturn false;\n\t\t}\n\n\t\tthis.setupEngineLogger();\n\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.starting`));\n\n\t\tif (this._context.config.debug) {\n\t\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.debuggingEnabled`));\n\t\t}\n\n\t\tlet canContinue;\n\t\ttry {\n\t\t\tcanContinue = await this.stateLoad();\n\n\t\t\tif (canContinue) {\n\t\t\t\tfor (const { type, module, method } of this._typeInitialisers) {\n\t\t\t\t\tawait this.initialiseTypeConfig(type, module, method);\n\t\t\t\t}\n\n\t\t\t\tawait this.bootstrap();\n\n\t\t\t\tawait this.logInfo(\n\t\t\t\t\tI18n.formatMessage(`${nameofCamelCase<EngineCore>()}.componentsStarting`)\n\t\t\t\t);\n\n\t\t\t\tawait ContextIdStore.run(this._contextIds ?? {}, async () => {\n\t\t\t\t\tfor (const instance of this._context.componentInstances) {\n\t\t\t\t\t\tif (!instance.started) {\n\t\t\t\t\t\t\tconst startMethod = instance.component.start?.bind(instance.component);\n\t\t\t\t\t\t\tif (Is.function(startMethod)) {\n\t\t\t\t\t\t\t\tawait this.logInfo(\n\t\t\t\t\t\t\t\t\tI18n.formatMessage(`${nameofCamelCase<EngineCore>()}.componentStarting`, {\n\t\t\t\t\t\t\t\t\t\tclassName: instance.component.className(),\n\t\t\t\t\t\t\t\t\t\tinstanceType: instance.instanceType\n\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tawait startMethod(EngineCore.LOGGING_COMPONENT_TYPE_NAME);\n\n\t\t\t\t\t\t\t\t\tinstance.started = true;\n\t\t\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\t\t\tawait this.logError(\n\t\t\t\t\t\t\t\t\t\tnew GeneralError(\n\t\t\t\t\t\t\t\t\t\t\tEngineCore.CLASS_NAME,\n\t\t\t\t\t\t\t\t\t\t\t\"componentStartFailed\",\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\tclassName: instance.component.className(),\n\t\t\t\t\t\t\t\t\t\t\t\tinstanceType: instance.instanceType\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tBaseError.fromError(err)\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\t\tthrow err;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tawait this.logInfo(\n\t\t\t\t\tI18n.formatMessage(`${nameofCamelCase<EngineCore>()}.componentsComplete`)\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.started`));\n\t\t\tthis._isStarted = true;\n\t\t} catch (err) {\n\t\t\tcanContinue = false;\n\t\t\tawait this.logError(BaseError.fromError(err));\n\t\t} finally {\n\t\t\tif (!(await this.stateSave())) {\n\t\t\t\tcanContinue = false;\n\t\t\t}\n\t\t}\n\n\t\tif (!canContinue) {\n\t\t\tawait this.stop();\n\t\t}\n\n\t\treturn canContinue;\n\t}\n\n\t/**\n\t * Stop the engine core.\n\t * @returns Nothing.\n\t */\n\tpublic async stop(): Promise<void> {\n\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.stopping`));\n\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.componentsStopping`));\n\n\t\tawait ContextIdStore.run(this._contextIds ?? {}, async () => {\n\t\t\tfor (const instance of this._context.componentInstances) {\n\t\t\t\tif (instance.started) {\n\t\t\t\t\tinstance.started = false;\n\t\t\t\t\tconst stopMethod = instance.component.stop?.bind(instance.component);\n\t\t\t\t\tif (Is.function(stopMethod)) {\n\t\t\t\t\t\tawait this.logInfo(\n\t\t\t\t\t\t\tI18n.formatMessage(`${nameofCamelCase<EngineCore>()}.componentStopping`, {\n\t\t\t\t\t\t\t\tclassName: instance.component.className(),\n\t\t\t\t\t\t\t\tinstanceType: instance.instanceType\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tawait stopMethod(EngineCore.LOGGING_COMPONENT_TYPE_NAME);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tawait this.logError(\n\t\t\t\t\t\t\t\tnew GeneralError(\n\t\t\t\t\t\t\t\t\tEngineCore.CLASS_NAME,\n\t\t\t\t\t\t\t\t\t\"componentStopFailed\",\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tclassName: instance.component.className(),\n\t\t\t\t\t\t\t\t\t\tinstanceType: instance.instanceType\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\tBaseError.fromError(err)\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tawait this.stateSave();\n\n\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.componentsStopped`));\n\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.stopped`));\n\t}\n\n\t/**\n\t * Is the engine started.\n\t * @returns True if the engine is started.\n\t */\n\tpublic isStarted(): boolean {\n\t\treturn this._isStarted;\n\t}\n\n\t/**\n\t * Is this the primary engine instance.\n\t * @returns True if the engine is the primary instance.\n\t */\n\tpublic isPrimary(): boolean {\n\t\treturn isMainThread && !this._isClone;\n\t}\n\n\t/**\n\t * Is this engine instance a clone.\n\t * @returns True if the engine instance is a clone.\n\t */\n\tpublic isClone(): boolean {\n\t\treturn this._isClone;\n\t}\n\n\t/**\n\t * Log info.\n\t * @param message The message to log.\n\t */\n\tpublic async logInfo(message: string): Promise<void> {\n\t\tawait this._engineLoggingComponent?.log({\n\t\t\tsource: EngineCore.CLASS_NAME,\n\t\t\tlevel: \"info\",\n\t\t\tmessage\n\t\t});\n\t}\n\n\t/**\n\t * Log error.\n\t * @param error The error to log.\n\t */\n\tpublic async logError(error: IError): Promise<void> {\n\t\tconst formattedErrors = ErrorHelper.localizeErrors(error);\n\t\tfor (const formattedError of formattedErrors) {\n\t\t\tlet message = Is.stringValue(formattedError.source)\n\t\t\t\t? `${formattedError.source}: ${formattedError.message}`\n\t\t\t\t: formattedError.message;\n\t\t\tif (this._context.config.debug && Is.stringValue(formattedError.stack)) {\n\t\t\t\tmessage += `\\n${formattedError.stack}`;\n\t\t\t}\n\t\t\tawait this._engineLoggingComponent?.log({\n\t\t\t\tsource: EngineCore.CLASS_NAME,\n\t\t\t\tlevel: \"error\",\n\t\t\t\tmessage\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Get the config for the engine.\n\t * @returns The config for the engine.\n\t */\n\tpublic getConfig(): C {\n\t\treturn this._context.config;\n\t}\n\n\t/**\n\t * Get the state of the engine.\n\t * @returns The state of the engine.\n\t */\n\tpublic getState(): S {\n\t\treturn this._context.state;\n\t}\n\n\t/**\n\t * Get all the registered instances.\n\t * @returns The registered instances.\n\t */\n\tpublic getRegisteredInstances(): {\n\t\t[name: string]: {\n\t\t\ttype: string;\n\t\t\tisDefault?: boolean;\n\t\t\tfeatures?: string[];\n\t\t}[];\n\t} {\n\t\treturn this._context.registeredInstances;\n\t}\n\n\t/**\n\t * Get the registered instance type for the component/connector.\n\t * @param componentConnectorType The type of the component/connector.\n\t * @param features The requested features of the component, if not specified the default entry will be retrieved.\n\t * @returns The instance type matching the criteria if one is registered.\n\t * @throws If a matching instance was not found.\n\t */\n\tpublic getRegisteredInstanceType(componentConnectorType: string, features?: string[]): string {\n\t\tGuards.stringValue(\n\t\t\tEngineCore.CLASS_NAME,\n\t\t\tnameof(componentConnectorType),\n\t\t\tcomponentConnectorType\n\t\t);\n\n\t\tconst registeredType = this.getRegisteredInstanceTypeOptional(componentConnectorType, features);\n\n\t\tif (!Is.stringValue(registeredType)) {\n\t\t\tthrow new GeneralError(EngineCore.CLASS_NAME, \"instanceTypeNotFound\", {\n\t\t\t\ttype: componentConnectorType,\n\t\t\t\tfeatures: (features ?? [\"default\"]).join(\",\")\n\t\t\t});\n\t\t}\n\n\t\treturn registeredType;\n\t}\n\n\t/**\n\t * Get the registered instance type for the component/connector if it exists.\n\t * @param componentConnectorType The type of the component/connector.\n\t * @param features The requested features of the component, if not specified the default entry will be retrieved.\n\t * @returns The instance type matching the criteria if one is registered.\n\t */\n\tpublic getRegisteredInstanceTypeOptional(\n\t\tcomponentConnectorType: string,\n\t\tfeatures?: string[]\n\t): string | undefined {\n\t\tlet registeredType: string | undefined;\n\n\t\tconst registeredTypes = this._context.registeredInstances[componentConnectorType];\n\t\tif (Is.arrayValue(registeredTypes)) {\n\t\t\tif (Is.arrayValue(features)) {\n\t\t\t\tregisteredType = registeredTypes.find(t =>\n\t\t\t\t\tt.features?.every(f => features.includes(f))\n\t\t\t\t)?.type;\n\t\t\t} else {\n\t\t\t\t// First look for the default entry\n\t\t\t\tregisteredType = registeredTypes.find(t => t.isDefault)?.type;\n\n\t\t\t\t// Can't find a default so just use the first entry\n\t\t\t\tif (!Is.stringValue(registeredType)) {\n\t\t\t\t\tregisteredType = registeredTypes[0]?.type;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn registeredType;\n\t}\n\n\t/**\n\t * Get the data required to create a clone of the engine.\n\t * @returns The clone data.\n\t */\n\tpublic getCloneData(): IEngineCoreClone<C, S> {\n\t\tconst entitySchemas: {\n\t\t\t[schema: string]: IEntitySchema;\n\t\t} = {};\n\n\t\tconst entitySchemaNames = EntitySchemaFactory.names();\n\t\tfor (const schemaName of entitySchemaNames) {\n\t\t\tentitySchemas[schemaName] = EntitySchemaFactory.get(schemaName);\n\t\t}\n\n\t\tconst cloneData: IEngineCoreClone<C, S> = {\n\t\t\tconfig: this._context.config,\n\t\t\tstate: this._context.state,\n\t\t\ttypeInitialisers: this._typeInitialisers,\n\t\t\tentitySchemas,\n\t\t\tcontextIdKeys: this._contextIdKeys\n\t\t};\n\n\t\treturn cloneData;\n\t}\n\n\t/**\n\t * Populate the engine from the clone data.\n\t * @param cloneData The clone data to populate from.\n\t * @param silent Should the clone be silent.\n\t */\n\tpublic populateClone(cloneData: IEngineCoreClone<C, S>, silent?: boolean): void {\n\t\tGuards.object(EngineCore.CLASS_NAME, nameof(cloneData), cloneData);\n\t\tGuards.object(EngineCore.CLASS_NAME, nameof(cloneData.config), cloneData.config);\n\t\tGuards.object(EngineCore.CLASS_NAME, nameof(cloneData.state), cloneData.state);\n\t\tGuards.array(\n\t\t\tEngineCore.CLASS_NAME,\n\t\t\tnameof(cloneData.typeInitialisers),\n\t\t\tcloneData.typeInitialisers\n\t\t);\n\n\t\tthis._skipBootstrap = true;\n\t\tthis._isClone = true;\n\n\t\tif (silent ?? false) {\n\t\t\tcloneData.config.silent = true;\n\t\t}\n\n\t\tthis._context = {\n\t\t\tconfig: cloneData.config,\n\t\t\tregisteredInstances: {},\n\t\t\tcomponentInstances: [],\n\t\t\tstate: {} as S,\n\t\t\tstateDirty: false\n\t\t};\n\n\t\tthis._typeInitialisers = cloneData.typeInitialisers;\n\t\tthis._contextIdKeys.push(...cloneData.contextIdKeys);\n\n\t\tfor (const schemaName of Object.keys(cloneData.entitySchemas)) {\n\t\t\tEntitySchemaFactory.register(schemaName, () => cloneData.entitySchemas[schemaName]);\n\t\t}\n\n\t\tthis._stateStorage = new MemoryStateStorage(true, cloneData.state);\n\t\tthis._isStarted = false;\n\t}\n\n\t/**\n\t * Initialise the types from connector.\n\t * @param typeKey The key for the default types.\n\t * @param instanceMethod The function to initialise the instance.\n\t * @internal\n\t */\n\tprivate async initialiseTypeConfig(\n\t\ttypeKey: string,\n\t\tmodule: string,\n\t\tmethod: string\n\t): Promise<void> {\n\t\tconst typeConfig: IEngineCoreTypeConfig[] | undefined = this._context.config.types?.[typeKey];\n\n\t\tif (Is.arrayValue(typeConfig)) {\n\t\t\tconst instanceMethod = await ModuleHelper.getModuleEntry<EngineTypeInitialiser>(\n\t\t\t\tmodule,\n\t\t\t\tmethod\n\t\t\t);\n\n\t\t\tfor (let i = 0; i < typeConfig.length; i++) {\n\t\t\t\tawait this.logInfo(\n\t\t\t\t\tI18n.formatMessage(\"engineCore.configuring\", {\n\t\t\t\t\t\tcomponentType: typeKey,\n\t\t\t\t\t\tconfigType: typeConfig[i].type\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\t\tconst result = await instanceMethod(this, this._context, typeConfig[i]);\n\n\t\t\t\tif (Is.stringValue(result.instanceType) && Is.object(result.component)) {\n\t\t\t\t\tconst finalInstanceType = typeConfig[i].overrideInstanceType ?? result.instanceType;\n\t\t\t\t\tthis._context.componentInstances.push({\n\t\t\t\t\t\tinstanceType: finalInstanceType,\n\t\t\t\t\t\tcomponent: result.component,\n\t\t\t\t\t\tstarted: false\n\t\t\t\t\t});\n\n\t\t\t\t\tresult.factory?.register(finalInstanceType, () => result.component);\n\n\t\t\t\t\tthis._context.registeredInstances[typeKey] ??= [];\n\n\t\t\t\t\tthis._context.registeredInstances[typeKey].push({\n\t\t\t\t\t\ttype: finalInstanceType,\n\t\t\t\t\t\tisDefault: typeConfig[i].isDefault,\n\t\t\t\t\t\tfeatures: typeConfig[i].features\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tthrow new GeneralError(\"engineCore\", \"componentUnknownType\", {\n\t\t\t\t\t\ttype: typeConfig[i].type,\n\t\t\t\t\t\tcomponentType: typeKey\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Setup the engine logger.\n\t * @internal\n\t */\n\tprivate setupEngineLogger(): void {\n\t\tconst silent = this._context.config.silent ?? false;\n\t\tconst engineLoggerConnector = silent\n\t\t\t? new SilentLoggingConnector()\n\t\t\t: new ConsoleLoggingConnector({\n\t\t\t\t\tconfig: {\n\t\t\t\t\t\ttranslateMessages: true,\n\t\t\t\t\t\thideGroups: true\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\tthis._context.componentInstances.push({\n\t\t\tinstanceType: EngineCore.LOGGING_CONNECTOR_TYPE_NAME,\n\t\t\tcomponent: engineLoggerConnector,\n\t\t\tstarted: false\n\t\t});\n\n\t\tLoggingConnectorFactory.register(\n\t\t\tEngineCore.LOGGING_CONNECTOR_TYPE_NAME,\n\t\t\t() => engineLoggerConnector\n\t\t);\n\n\t\tthis._context.registeredInstances.loggingConnector = [\n\t\t\t{\n\t\t\t\ttype: EngineCore.LOGGING_CONNECTOR_TYPE_NAME\n\t\t\t}\n\t\t];\n\n\t\tconst engineLoggerComponent = new LoggingService({\n\t\t\tloggingConnectorType: EngineCore.LOGGING_CONNECTOR_TYPE_NAME\n\t\t});\n\t\tthis._engineLoggingComponent = engineLoggerComponent;\n\n\t\tComponentFactory.register(EngineCore.LOGGING_COMPONENT_TYPE_NAME, () => engineLoggerComponent);\n\t\tthis._context.registeredInstances.loggingComponent = [\n\t\t\t{\n\t\t\t\ttype: EngineCore.LOGGING_COMPONENT_TYPE_NAME\n\t\t\t}\n\t\t];\n\t}\n\n\t/**\n\t * Load the state.\n\t * @returns True if the state was loaded and can continue.\n\t * @internal\n\t */\n\tprivate async stateLoad(): Promise<boolean> {\n\t\tif (this._stateStorage) {\n\t\t\ttry {\n\t\t\t\tthis._context.state = ((await this._stateStorage.load(this)) ?? {}) as S;\n\t\t\t\tthis._context.stateDirty = false;\n\n\t\t\t\treturn true;\n\t\t\t} catch (err) {\n\t\t\t\tawait this.logError(BaseError.fromError(err));\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\n\t/**\n\t * Save the state.\n\t * @returns True if the state was saved.\n\t * @internal\n\t */\n\tprivate async stateSave(): Promise<boolean> {\n\t\tif (this._stateStorage && !Is.empty(this._context.state) && this._context.stateDirty) {\n\t\t\ttry {\n\t\t\t\tawait this._stateStorage.save(this, this._context.state);\n\t\t\t\tthis._context.stateDirty = false;\n\t\t\t\treturn true;\n\t\t\t} catch (err) {\n\t\t\t\tawait this.logError(BaseError.fromError(err));\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\n\t/**\n\t * Bootstrap the engine.\n\t * @internal\n\t */\n\tprivate async bootstrap(): Promise<void> {\n\t\tif (!this._skipBootstrap) {\n\t\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.bootstrapStarted`));\n\n\t\t\t// First bootstrap the components.\n\t\t\tfor (const instance of this._context.componentInstances) {\n\t\t\t\tconst bootstrapMethod = instance.component.bootstrap?.bind(instance.component);\n\t\t\t\tif (Is.function(bootstrapMethod)) {\n\t\t\t\t\tawait this.logInfo(\n\t\t\t\t\t\tI18n.formatMessage(`${nameofCamelCase<EngineCore>()}.bootstrapping`, {\n\t\t\t\t\t\t\tclassName: instance.component.className(),\n\t\t\t\t\t\t\tinstanceType: instance.instanceType\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\n\t\t\t\t\tconst bootstrapSuccess = await bootstrapMethod(EngineCore.LOGGING_COMPONENT_TYPE_NAME);\n\n\t\t\t\t\t// If the bootstrap method failed then throw an error\n\t\t\t\t\tif (!bootstrapSuccess) {\n\t\t\t\t\t\tthrow new GeneralError(EngineCore.CLASS_NAME, \"bootstrapFailed\", {\n\t\t\t\t\t\t\tclassName: instance.component.className(),\n\t\t\t\t\t\t\tinstanceType: instance.instanceType\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Now perform any custom bootstrap operations\n\t\t\tconst customBootstrap = this._customBootstrap;\n\t\t\tif (Is.function(customBootstrap)) {\n\t\t\t\tawait customBootstrap.call(this, this, this._context);\n\t\t\t}\n\n\t\t\tawait this.logInfo(I18n.formatMessage(`${nameofCamelCase<EngineCore>()}.bootstrapComplete`));\n\t\t}\n\t}\n}\n"]}
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
export * from "./engineCore.js";
|
|
4
|
+
export * from "./models/IEngineCoreOptions.js";
|
|
5
|
+
export * from "./storage/fileStateStorage.js";
|
|
6
|
+
export * from "./storage/memoryStateStorage.js";
|
|
7
|
+
export * from "./utils/engineModuleHelper.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./engineCore.js\";\nexport * from \"./models/IEngineCoreOptions.js\";\nexport * from \"./storage/fileStateStorage.js\";\nexport * from \"./storage/memoryStateStorage.js\";\nexport * from \"./utils/engineModuleHelper.js\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IEngineCoreOptions.js","sourceRoot":"","sources":["../../../src/models/IEngineCoreOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type {\n\tIEngineCore,\n\tIEngineCoreConfig,\n\tIEngineCoreContext,\n\tIEngineState,\n\tIEngineStateStorage\n} from \"@twin.org/engine-models\";\n\n/**\n * The options for creating engine core.\n */\nexport interface IEngineCoreOptions<\n\tC extends IEngineCoreConfig = IEngineCoreConfig,\n\tS extends IEngineState = IEngineState\n> {\n\t/**\n\t * The engine core config.\n\t */\n\tconfig?: C;\n\n\t/**\n\t * The state storage component.\n\t */\n\tstateStorage?: IEngineStateStorage<S>;\n\n\t/**\n\t * Skip the bootstrap process, useful for additional engine instances.\n\t */\n\tskipBootstrap?: boolean;\n\n\t/**\n\t * Populate the type initialisers for the engine.\n\t */\n\tpopulateTypeInitialisers?: (\n\t\tengineCore: IEngineCore<C, S>,\n\t\tcontext: IEngineCoreContext<C, S>\n\t) => void;\n\n\t/**\n\t * Custom bootstrap method for the engine.\n\t */\n\tcustomBootstrap?: (\n\t\tengineCore: IEngineCore<C, S>,\n\t\tcontext: IEngineCoreContext<C, S>\n\t) => Promise<void>;\n}\n"]}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { BaseError, GeneralError, Guards, I18n } from "@twin.org/core";
|
|
6
|
+
/**
|
|
7
|
+
* Store state in a file.
|
|
8
|
+
*/
|
|
9
|
+
export class FileStateStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
static CLASS_NAME = "FileStateStorage";
|
|
14
|
+
/**
|
|
15
|
+
* The filename to store the state.
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
_filename;
|
|
19
|
+
/**
|
|
20
|
+
* Readonly mode state file is not updated.
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
_readonlyMode;
|
|
24
|
+
/**
|
|
25
|
+
* Create a new instance of FileStateStorage.
|
|
26
|
+
* @param filename The filename to store the state.
|
|
27
|
+
* @param readonlyMode Whether the file is in read-only mode.
|
|
28
|
+
*/
|
|
29
|
+
constructor(filename, readonlyMode = false) {
|
|
30
|
+
Guards.stringValue(FileStateStorage.CLASS_NAME, "filename", filename);
|
|
31
|
+
this._filename = filename;
|
|
32
|
+
this._readonlyMode = readonlyMode;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Method for loading the state.
|
|
36
|
+
* @param engineCore The engine core to load the state for.
|
|
37
|
+
* @returns The state of the engine or undefined if it doesn't exist.
|
|
38
|
+
*/
|
|
39
|
+
async load(engineCore) {
|
|
40
|
+
try {
|
|
41
|
+
engineCore.logInfo(I18n.formatMessage(`${"fileStateStorage"}.loading`, {
|
|
42
|
+
filename: this._filename
|
|
43
|
+
}));
|
|
44
|
+
if (await this.fileExists(this._filename)) {
|
|
45
|
+
const content = await readFile(this._filename, "utf8");
|
|
46
|
+
return JSON.parse(content.toString());
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
throw new GeneralError(FileStateStorage.CLASS_NAME, "failedLoading", { filename: this._filename }, BaseError.fromError(err));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Method for saving the state.
|
|
55
|
+
* @param engineCore The engine core to save the state for.
|
|
56
|
+
* @param state The state of the engine to save.
|
|
57
|
+
* @returns Nothing.
|
|
58
|
+
*/
|
|
59
|
+
async save(engineCore, state) {
|
|
60
|
+
if (!this._readonlyMode) {
|
|
61
|
+
try {
|
|
62
|
+
engineCore.logInfo(I18n.formatMessage(`${"fileStateStorage"}.saving`, {
|
|
63
|
+
filename: this._filename
|
|
64
|
+
}));
|
|
65
|
+
try {
|
|
66
|
+
await mkdir(path.dirname(this._filename), { recursive: true });
|
|
67
|
+
}
|
|
68
|
+
catch { }
|
|
69
|
+
await writeFile(this._filename, JSON.stringify(state, undefined, "\t"), "utf8");
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
throw new GeneralError(FileStateStorage.CLASS_NAME, "failedSaving", { filename: this._filename }, BaseError.fromError(err));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Does the specified file exist.
|
|
78
|
+
* @param filename The filename to check for existence.
|
|
79
|
+
* @returns True if the file exists.
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
async fileExists(filename) {
|
|
83
|
+
try {
|
|
84
|
+
const stats = await stat(filename);
|
|
85
|
+
return stats.isFile();
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=fileStateStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file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ourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { mkdir, readFile, stat, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { BaseError, GeneralError, Guards, I18n } from \"@twin.org/core\";\nimport type { IEngineCore, IEngineState, IEngineStateStorage } from \"@twin.org/engine-models\";\nimport { nameof, nameofCamelCase } from \"@twin.org/nameof\";\n\n/**\n * Store state in a file.\n */\nexport class FileStateStorage<S extends IEngineState = IEngineState>\n\timplements IEngineStateStorage<S>\n{\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<FileStateStorage>();\n\n\t/**\n\t * The filename to store the state.\n\t * @internal\n\t */\n\tprivate readonly _filename: string;\n\n\t/**\n\t * Readonly mode state file is not updated.\n\t * @internal\n\t */\n\tprivate readonly _readonlyMode: boolean;\n\n\t/**\n\t * Create a new instance of FileStateStorage.\n\t * @param filename The filename to store the state.\n\t * @param readonlyMode Whether the file is in read-only mode.\n\t */\n\tconstructor(filename: string, readonlyMode: boolean = false) {\n\t\tGuards.stringValue(FileStateStorage.CLASS_NAME, nameof(filename), filename);\n\t\tthis._filename = filename;\n\t\tthis._readonlyMode = readonlyMode;\n\t}\n\n\t/**\n\t * Method for loading the state.\n\t * @param engineCore The engine core to load the state for.\n\t * @returns The state of the engine or undefined if it doesn't exist.\n\t */\n\tpublic async load(engineCore: IEngineCore): Promise<S | undefined> {\n\t\ttry {\n\t\t\tengineCore.logInfo(\n\t\t\t\tI18n.formatMessage(`${nameofCamelCase<FileStateStorage>()}.loading`, {\n\t\t\t\t\tfilename: this._filename\n\t\t\t\t})\n\t\t\t);\n\t\t\tif (await this.fileExists(this._filename)) {\n\t\t\t\tconst content = await readFile(this._filename, \"utf8\");\n\t\t\t\treturn JSON.parse(content.toString()) as S;\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tFileStateStorage.CLASS_NAME,\n\t\t\t\t\"failedLoading\",\n\t\t\t\t{ filename: this._filename },\n\t\t\t\tBaseError.fromError(err)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Method for saving the state.\n\t * @param engineCore The engine core to save the state for.\n\t * @param state The state of the engine to save.\n\t * @returns Nothing.\n\t */\n\tpublic async save(engineCore: IEngineCore, state: S): Promise<void> {\n\t\tif (!this._readonlyMode) {\n\t\t\ttry {\n\t\t\t\tengineCore.logInfo(\n\t\t\t\t\tI18n.formatMessage(`${nameofCamelCase<FileStateStorage>()}.saving`, {\n\t\t\t\t\t\tfilename: this._filename\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\t\ttry {\n\t\t\t\t\tawait mkdir(path.dirname(this._filename), { recursive: true });\n\t\t\t\t} catch {}\n\t\t\t\tawait writeFile(this._filename, JSON.stringify(state, undefined, \"\\t\"), \"utf8\");\n\t\t\t} catch (err) {\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tFileStateStorage.CLASS_NAME,\n\t\t\t\t\t\"failedSaving\",\n\t\t\t\t\t{ filename: this._filename },\n\t\t\t\t\tBaseError.fromError(err)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Does the specified file exist.\n\t * @param filename The filename to check for existence.\n\t * @returns True if the file exists.\n\t * @internal\n\t */\n\tprivate async fileExists(filename: string): Promise<boolean> {\n\t\ttry {\n\t\t\tconst stats = await stat(filename);\n\t\t\treturn stats.isFile();\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n}\n"]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { I18n } from "@twin.org/core";
|
|
4
|
+
/**
|
|
5
|
+
* Store state in memory.
|
|
6
|
+
*/
|
|
7
|
+
export class MemoryStateStorage {
|
|
8
|
+
/**
|
|
9
|
+
* Runtime name for the class.
|
|
10
|
+
*/
|
|
11
|
+
static CLASS_NAME = "MemoryStateStorage";
|
|
12
|
+
/**
|
|
13
|
+
* Readonly mode state file is not updated.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
_readonlyMode;
|
|
17
|
+
/**
|
|
18
|
+
* The state object.
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
_engineState;
|
|
22
|
+
/**
|
|
23
|
+
* Create a new instance of MemoryStateStorage.
|
|
24
|
+
* @param readonlyMode Whether the file is in read-only mode.
|
|
25
|
+
* @param state The initial state.
|
|
26
|
+
*/
|
|
27
|
+
constructor(readonlyMode = false, state) {
|
|
28
|
+
this._readonlyMode = readonlyMode;
|
|
29
|
+
this._engineState = state;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Method for loading the state.
|
|
33
|
+
* @param engineCore The engine core to load the state for.
|
|
34
|
+
* @returns The state of the engine or undefined if it doesn't exist.
|
|
35
|
+
*/
|
|
36
|
+
async load(engineCore) {
|
|
37
|
+
engineCore.logInfo(I18n.formatMessage(`${"memoryStateStorage"}.loading`, {
|
|
38
|
+
filename: this._engineState
|
|
39
|
+
}));
|
|
40
|
+
return this._engineState;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Method for saving the state.
|
|
44
|
+
* @param engineCore The engine core to save the state for.
|
|
45
|
+
* @param state The state of the engine to save.
|
|
46
|
+
* @returns Nothing.
|
|
47
|
+
*/
|
|
48
|
+
async save(engineCore, state) {
|
|
49
|
+
if (!this._readonlyMode) {
|
|
50
|
+
engineCore.logInfo(I18n.formatMessage(`${"memoryStateStorage"}.saving`));
|
|
51
|
+
this._engineState = state;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=memoryStateStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoryStateStorage.js","sourceRoot":"","sources":["../../../src/storage/memoryStateStorage.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAItC;;GAEG;AACH,MAAM,OAAO,kBAAkB;IAG9B;;OAEG;IACI,MAAM,CAAU,UAAU,wBAAwC;IAEzE;;;OAGG;IACc,aAAa,CAAU;IAExC;;;OAGG;IACK,YAAY,CAAK;IAEzB;;;;OAIG;IACH,YAAY,eAAwB,KAAK,EAAE,KAAS;QACnD,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,IAAI,CAAC,UAAuB;QACxC,UAAU,CAAC,OAAO,CACjB,IAAI,CAAC,aAAa,CAAC,GAAG,oBAAqC,UAAU,EAAE;YACtE,QAAQ,EAAE,IAAI,CAAC,YAAY;SAC3B,CAAC,CACF,CAAC;QACF,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,IAAI,CAAC,UAAuB,EAAE,KAAQ;QAClD,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;YACzB,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,GAAG,oBAAqC,SAAS,CAAC,CAAC,CAAC;YAC1F,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC3B,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { I18n } from \"@twin.org/core\";\nimport type { IEngineCore, IEngineState, IEngineStateStorage } from \"@twin.org/engine-models\";\nimport { nameof, nameofCamelCase } from \"@twin.org/nameof\";\n\n/**\n * Store state in memory.\n */\nexport class MemoryStateStorage<S extends IEngineState = IEngineState>\n\timplements IEngineStateStorage<S>\n{\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MemoryStateStorage>();\n\n\t/**\n\t * Readonly mode state file is not updated.\n\t * @internal\n\t */\n\tprivate readonly _readonlyMode: boolean;\n\n\t/**\n\t * The state object.\n\t * @internal\n\t */\n\tprivate _engineState?: S;\n\n\t/**\n\t * Create a new instance of MemoryStateStorage.\n\t * @param readonlyMode Whether the file is in read-only mode.\n\t * @param state The initial state.\n\t */\n\tconstructor(readonlyMode: boolean = false, state?: S) {\n\t\tthis._readonlyMode = readonlyMode;\n\t\tthis._engineState = state;\n\t}\n\n\t/**\n\t * Method for loading the state.\n\t * @param engineCore The engine core to load the state for.\n\t * @returns The state of the engine or undefined if it doesn't exist.\n\t */\n\tpublic async load(engineCore: IEngineCore): Promise<S | undefined> {\n\t\tengineCore.logInfo(\n\t\t\tI18n.formatMessage(`${nameofCamelCase<MemoryStateStorage>()}.loading`, {\n\t\t\t\tfilename: this._engineState\n\t\t\t})\n\t\t);\n\t\treturn this._engineState;\n\t}\n\n\t/**\n\t * Method for saving the state.\n\t * @param engineCore The engine core to save the state for.\n\t * @param state The state of the engine to save.\n\t * @returns Nothing.\n\t */\n\tpublic async save(engineCore: IEngineCore, state: S): Promise<void> {\n\t\tif (!this._readonlyMode) {\n\t\t\tengineCore.logInfo(I18n.formatMessage(`${nameofCamelCase<MemoryStateStorage>()}.saving`));\n\t\t\tthis._engineState = state;\n\t\t}\n\t}\n}\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { GeneralError, Is } from "@twin.org/core";
|
|
4
|
+
import { ModuleHelper } from "@twin.org/modules";
|
|
5
|
+
/**
|
|
6
|
+
* Helper class for engine modules.
|
|
7
|
+
*/
|
|
8
|
+
export class EngineModuleHelper {
|
|
9
|
+
/**
|
|
10
|
+
* Runtime name for the class.
|
|
11
|
+
*/
|
|
12
|
+
static CLASS_NAME = "EngineModuleHelper";
|
|
13
|
+
/**
|
|
14
|
+
* Loads an engine component and constructs it with the relevant dependencies and configuration.
|
|
15
|
+
* @param engineCore The engine core.
|
|
16
|
+
* @param engineModuleConfig The configuration for the module.
|
|
17
|
+
* @returns The instantiated component.
|
|
18
|
+
*/
|
|
19
|
+
static async loadComponent(engineCore, engineModuleConfig) {
|
|
20
|
+
const moduleClass = await ModuleHelper.getModuleEntry(engineModuleConfig.moduleName, engineModuleConfig.className);
|
|
21
|
+
const isClass = Is.class(moduleClass);
|
|
22
|
+
if (!isClass) {
|
|
23
|
+
throw new GeneralError(EngineModuleHelper.CLASS_NAME, "moduleNotClass", {
|
|
24
|
+
moduleName: engineModuleConfig.moduleName,
|
|
25
|
+
className: engineModuleConfig.className
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const constructorOptions = {};
|
|
29
|
+
if (Is.arrayValue(engineModuleConfig.dependencies)) {
|
|
30
|
+
for (const dependency of engineModuleConfig.dependencies) {
|
|
31
|
+
if (dependency.isOptional ?? false) {
|
|
32
|
+
constructorOptions[dependency.propertyName] = engineCore.getRegisteredInstanceType(dependency.componentName, dependency.features);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
constructorOptions[dependency.propertyName] =
|
|
36
|
+
engineCore.getRegisteredInstanceTypeOptional(dependency.componentName, dependency.features);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (Is.object(engineModuleConfig.config)) {
|
|
41
|
+
constructorOptions.config = engineModuleConfig.config;
|
|
42
|
+
}
|
|
43
|
+
return new moduleClass(constructorOptions);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=engineModuleHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engineModuleHelper.js","sourceRoot":"","sources":["../../../src/utils/engineModuleHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,EAAE,YAAY,EAAE,M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kB,CAAC,MAAM,GAAG,kBAAkB,CAAC,MAAM,CAAC;QACvD,CAAC;QAED,OAAO,IAAI,WAAW,CAAC,kBAAkB,CAAM,CAAC;IACjD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Is } from \"@twin.org/core\";\nimport type { IEngineCore, IEngineModuleConfig } from \"@twin.org/engine-models\";\nimport { ModuleHelper } from \"@twin.org/modules\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Helper class for engine modules.\n */\nexport class EngineModuleHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<EngineModuleHelper>();\n\n\t/**\n\t * Loads an engine component and constructs it with the relevant dependencies and configuration.\n\t * @param engineCore The engine core.\n\t * @param engineModuleConfig The configuration for the module.\n\t * @returns The instantiated component.\n\t */\n\tpublic static async loadComponent<T>(\n\t\tengineCore: IEngineCore,\n\t\tengineModuleConfig: IEngineModuleConfig\n\t): Promise<T> {\n\t\tconst moduleClass = await ModuleHelper.getModuleEntry(\n\t\t\tengineModuleConfig.moduleName,\n\t\t\tengineModuleConfig.className\n\t\t);\n\n\t\tconst isClass = Is.class(moduleClass);\n\t\tif (!isClass) {\n\t\t\tthrow new GeneralError(EngineModuleHelper.CLASS_NAME, \"moduleNotClass\", {\n\t\t\t\tmoduleName: engineModuleConfig.moduleName,\n\t\t\t\tclassName: engineModuleConfig.className\n\t\t\t});\n\t\t}\n\n\t\tconst constructorOptions: { [key: string]: unknown } = {};\n\n\t\tif (Is.arrayValue(engineModuleConfig.dependencies)) {\n\t\t\tfor (const dependency of engineModuleConfig.dependencies) {\n\t\t\t\tif (dependency.isOptional ?? false) {\n\t\t\t\t\tconstructorOptions[dependency.propertyName] = engineCore.getRegisteredInstanceType(\n\t\t\t\t\t\tdependency.componentName,\n\t\t\t\t\t\tdependency.features\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconstructorOptions[dependency.propertyName] =\n\t\t\t\t\t\tengineCore.getRegisteredInstanceTypeOptional(\n\t\t\t\t\t\t\tdependency.componentName,\n\t\t\t\t\t\t\tdependency.features\n\t\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (Is.object(engineModuleConfig.config)) {\n\t\t\tconstructorOptions.config = engineModuleConfig.config;\n\t\t}\n\n\t\treturn new moduleClass(constructorOptions) as T;\n\t}\n}\n"]}
|
|
@@ -1,18 +1,35 @@
|
|
|
1
|
+
import { type IContextIds } from "@twin.org/context";
|
|
1
2
|
import { type IError } from "@twin.org/core";
|
|
2
3
|
import type { IEngineCore, IEngineCoreClone, IEngineCoreConfig, IEngineCoreContext, IEngineCoreTypeConfig, IEngineState } from "@twin.org/engine-models";
|
|
3
|
-
import type { IEngineCoreOptions } from "./models/IEngineCoreOptions";
|
|
4
|
+
import type { IEngineCoreOptions } from "./models/IEngineCoreOptions.js";
|
|
4
5
|
/**
|
|
5
6
|
* Core for the engine.
|
|
6
7
|
*/
|
|
7
8
|
export declare class EngineCore<C extends IEngineCoreConfig = IEngineCoreConfig, S extends IEngineState = IEngineState> implements IEngineCore<C, S> {
|
|
8
9
|
/**
|
|
9
|
-
* Name for the engine logger.
|
|
10
|
+
* Name for the engine logger component, used for direct console logging.
|
|
10
11
|
*/
|
|
11
|
-
static readonly
|
|
12
|
+
static readonly LOGGING_COMPONENT_TYPE_NAME: string;
|
|
13
|
+
/**
|
|
14
|
+
* Name for the engine logger connector, used for direct console logging.
|
|
15
|
+
*/
|
|
16
|
+
static readonly LOGGING_CONNECTOR_TYPE_NAME: string;
|
|
17
|
+
/**
|
|
18
|
+
* Runtime name for the class.
|
|
19
|
+
*/
|
|
20
|
+
static readonly CLASS_NAME: string;
|
|
12
21
|
/**
|
|
13
22
|
* The core context.
|
|
14
23
|
*/
|
|
15
24
|
protected _context: IEngineCoreContext<C, S>;
|
|
25
|
+
/**
|
|
26
|
+
* The context ID keys.
|
|
27
|
+
*/
|
|
28
|
+
protected readonly _contextIdKeys: string[];
|
|
29
|
+
/**
|
|
30
|
+
* The context IDs.
|
|
31
|
+
*/
|
|
32
|
+
protected _contextIds?: IContextIds;
|
|
16
33
|
/**
|
|
17
34
|
* Create a new instance of EngineCore.
|
|
18
35
|
* @param options The options for the engine.
|
|
@@ -21,11 +38,37 @@ export declare class EngineCore<C extends IEngineCoreConfig = IEngineCoreConfig,
|
|
|
21
38
|
/**
|
|
22
39
|
* Add a type initialiser.
|
|
23
40
|
* @param type The type to add the initialiser for.
|
|
24
|
-
* @param typeConfig The type config.
|
|
25
41
|
* @param module The name of the module which contains the initialiser method.
|
|
26
42
|
* @param method The name of the method to call.
|
|
27
43
|
*/
|
|
28
|
-
addTypeInitialiser(type: string,
|
|
44
|
+
addTypeInitialiser(type: string, module: string, method: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* Get the type config for a specific type.
|
|
47
|
+
* @param type The type to get the config for.
|
|
48
|
+
* @returns The type config or undefined if not found.
|
|
49
|
+
*/
|
|
50
|
+
getTypeConfig(type: string): IEngineCoreTypeConfig[] | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Add a context ID key to the engine.
|
|
53
|
+
* @param key The context ID key.
|
|
54
|
+
*/
|
|
55
|
+
addContextIdKey(key: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* Get the context ID keys for the engine.
|
|
58
|
+
* @returns The context IDs keys.
|
|
59
|
+
*/
|
|
60
|
+
getContextIdKeys(): string[];
|
|
61
|
+
/**
|
|
62
|
+
* Add a context ID to the engine.
|
|
63
|
+
* @param key The context ID key.
|
|
64
|
+
* @param value The context ID value.
|
|
65
|
+
*/
|
|
66
|
+
addContextId(key: string, value: string): void;
|
|
67
|
+
/**
|
|
68
|
+
* Get the context IDs for the engine.
|
|
69
|
+
* @returns The context IDs or undefined if none are set.
|
|
70
|
+
*/
|
|
71
|
+
getContextIds(): IContextIds | undefined;
|
|
29
72
|
/**
|
|
30
73
|
* Start the engine core.
|
|
31
74
|
* @returns True if the start was successful.
|
|
@@ -55,12 +98,12 @@ export declare class EngineCore<C extends IEngineCoreConfig = IEngineCoreConfig,
|
|
|
55
98
|
* Log info.
|
|
56
99
|
* @param message The message to log.
|
|
57
100
|
*/
|
|
58
|
-
logInfo(message: string): void
|
|
101
|
+
logInfo(message: string): Promise<void>;
|
|
59
102
|
/**
|
|
60
103
|
* Log error.
|
|
61
104
|
* @param error The error to log.
|
|
62
105
|
*/
|
|
63
|
-
logError(error: IError): void
|
|
106
|
+
logError(error: IError): Promise<void>;
|
|
64
107
|
/**
|
|
65
108
|
* Get the config for the engine.
|
|
66
109
|
* @returns The config for the engine.
|
|
@@ -78,6 +121,7 @@ export declare class EngineCore<C extends IEngineCoreConfig = IEngineCoreConfig,
|
|
|
78
121
|
getRegisteredInstances(): {
|
|
79
122
|
[name: string]: {
|
|
80
123
|
type: string;
|
|
124
|
+
isDefault?: boolean;
|
|
81
125
|
features?: string[];
|
|
82
126
|
}[];
|
|
83
127
|
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export * from "./engineCore";
|
|
2
|
-
export * from "./models/IEngineCoreOptions";
|
|
3
|
-
export * from "./storage/fileStateStorage";
|
|
4
|
-
export * from "./storage/memoryStateStorage";
|
|
1
|
+
export * from "./engineCore.js";
|
|
2
|
+
export * from "./models/IEngineCoreOptions.js";
|
|
3
|
+
export * from "./storage/fileStateStorage.js";
|
|
4
|
+
export * from "./storage/memoryStateStorage.js";
|
|
5
|
+
export * from "./utils/engineModuleHelper.js";
|
|
@@ -23,9 +23,4 @@ export interface IEngineCoreOptions<C extends IEngineCoreConfig = IEngineCoreCon
|
|
|
23
23
|
* Custom bootstrap method for the engine.
|
|
24
24
|
*/
|
|
25
25
|
customBootstrap?: (engineCore: IEngineCore<C, S>, context: IEngineCoreContext<C, S>) => Promise<void>;
|
|
26
|
-
/**
|
|
27
|
-
* The name of the logger to use in the engine.
|
|
28
|
-
* @default engine
|
|
29
|
-
*/
|
|
30
|
-
loggerTypeName?: string;
|
|
31
26
|
}
|
|
@@ -6,7 +6,7 @@ export declare class FileStateStorage<S extends IEngineState = IEngineState> imp
|
|
|
6
6
|
/**
|
|
7
7
|
* Runtime name for the class.
|
|
8
8
|
*/
|
|
9
|
-
readonly CLASS_NAME: string;
|
|
9
|
+
static readonly CLASS_NAME: string;
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of FileStateStorage.
|
|
12
12
|
* @param filename The filename to store the state.
|
|
@@ -6,7 +6,7 @@ export declare class MemoryStateStorage<S extends IEngineState = IEngineState> i
|
|
|
6
6
|
/**
|
|
7
7
|
* Runtime name for the class.
|
|
8
8
|
*/
|
|
9
|
-
readonly CLASS_NAME: string;
|
|
9
|
+
static readonly CLASS_NAME: string;
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of MemoryStateStorage.
|
|
12
12
|
* @param readonlyMode Whether the file is in read-only mode.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { IEngineCore, IEngineModuleConfig } from "@twin.org/engine-models";
|
|
2
|
+
/**
|
|
3
|
+
* Helper class for engine modules.
|
|
4
|
+
*/
|
|
5
|
+
export declare class EngineModuleHelper {
|
|
6
|
+
/**
|
|
7
|
+
* Runtime name for the class.
|
|
8
|
+
*/
|
|
9
|
+
static readonly CLASS_NAME: string;
|
|
10
|
+
/**
|
|
11
|
+
* Loads an engine component and constructs it with the relevant dependencies and configuration.
|
|
12
|
+
* @param engineCore The engine core.
|
|
13
|
+
* @param engineModuleConfig The configuration for the module.
|
|
14
|
+
* @returns The instantiated component.
|
|
15
|
+
*/
|
|
16
|
+
static loadComponent<T>(engineCore: IEngineCore, engineModuleConfig: IEngineModuleConfig): Promise<T>;
|
|
17
|
+
}
|