@syncbridge/common 0.4.0

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 (87) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +1 -0
  3. package/classes/base-element.d.ts +46 -0
  4. package/classes/base-element.js +80 -0
  5. package/classes/component-base.d.ts +26 -0
  6. package/classes/component-base.js +12 -0
  7. package/classes/frame-stream.d.ts +34 -0
  8. package/classes/frame-stream.js +140 -0
  9. package/classes/processor-base.d.ts +15 -0
  10. package/classes/processor-base.js +8 -0
  11. package/classes/runnable.d.ts +86 -0
  12. package/classes/runnable.js +228 -0
  13. package/classes/sb-error.d.ts +11 -0
  14. package/classes/sb-error.js +54 -0
  15. package/classes/stack-executor.d.ts +10 -0
  16. package/classes/stack-executor.js +37 -0
  17. package/constants.d.ts +6 -0
  18. package/constants.js +17 -0
  19. package/decorators/component.decorator.d.ts +15 -0
  20. package/decorators/component.decorator.js +28 -0
  21. package/decorators/decorator-helpers.d.ts +2 -0
  22. package/decorators/decorator-helpers.js +13 -0
  23. package/decorators/define-variable.decorator.d.ts +14 -0
  24. package/decorators/define-variable.decorator.js +57 -0
  25. package/decorators/finalize-element-metadata.d.ts +4 -0
  26. package/decorators/finalize-element-metadata.js +30 -0
  27. package/decorators/processor-events.decorator.d.ts +5 -0
  28. package/decorators/processor-events.decorator.js +21 -0
  29. package/decorators/processor.decorator.d.ts +31 -0
  30. package/decorators/processor.decorator.js +42 -0
  31. package/decorators/use-component.decorator.d.ts +9 -0
  32. package/decorators/use-component.decorator.js +24 -0
  33. package/decorators/use-variables.decorator.d.ts +5 -0
  34. package/decorators/use-variables.decorator.js +20 -0
  35. package/index.d.ts +20 -0
  36. package/index.js +20 -0
  37. package/interfaces/extension-package.interface.d.ts +6 -0
  38. package/interfaces/extension-package.interface.js +1 -0
  39. package/interfaces/index.d.ts +2 -0
  40. package/interfaces/index.js +2 -0
  41. package/interfaces/logger.interface.d.ts +17 -0
  42. package/interfaces/logger.interface.js +1 -0
  43. package/models/enums/log-level.d.ts +9 -0
  44. package/models/enums/log-level.js +15 -0
  45. package/models/enums/service-status.d.ts +8 -0
  46. package/models/enums/service-status.js +14 -0
  47. package/models/enums/variable-format.enum.d.ts +9 -0
  48. package/models/enums/variable-format.enum.js +15 -0
  49. package/models/enums/variable-type.enum.d.ts +8 -0
  50. package/models/enums/variable-type.enum.js +14 -0
  51. package/models/index.d.ts +13 -0
  52. package/models/index.js +13 -0
  53. package/models/metadata/author-metadata.d.ts +8 -0
  54. package/models/metadata/author-metadata.js +48 -0
  55. package/models/metadata/component-metadata.d.ts +8 -0
  56. package/models/metadata/component-metadata.js +25 -0
  57. package/models/metadata/element-component-metadata.d.ts +19 -0
  58. package/models/metadata/element-component-metadata.js +70 -0
  59. package/models/metadata/element-metadata.d.ts +18 -0
  60. package/models/metadata/element-metadata.js +81 -0
  61. package/models/metadata/package-metadata.d.ts +7 -0
  62. package/models/metadata/package-metadata.js +27 -0
  63. package/models/metadata/processor-metadata.d.ts +5 -0
  64. package/models/metadata/processor-metadata.js +14 -0
  65. package/models/metadata/variable-metadata.d.ts +31 -0
  66. package/models/metadata/variable-metadata.js +138 -0
  67. package/models/profile/log-options.d.ts +16 -0
  68. package/models/profile/log-options.js +56 -0
  69. package/models/profile/profile-component.d.ts +23 -0
  70. package/models/profile/profile-component.js +77 -0
  71. package/models/profile/profile.d.ts +25 -0
  72. package/models/profile/profile.js +115 -0
  73. package/models-document.d.ts +3 -0
  74. package/models-document.js +15 -0
  75. package/package.json +33 -0
  76. package/processor-factory.d.ts +13 -0
  77. package/processor-factory.js +106 -0
  78. package/registry.d.ts +32 -0
  79. package/registry.js +205 -0
  80. package/utils/encrypt-helpers.d.ts +2 -0
  81. package/utils/encrypt-helpers.js +43 -0
  82. package/utils/make-extension-package.d.ts +3 -0
  83. package/utils/make-extension-package.js +13 -0
  84. package/utils/metadata-utils.d.ts +7 -0
  85. package/utils/metadata-utils.js +99 -0
  86. package/utils/profile-utils.d.ts +21 -0
  87. package/utils/profile-utils.js +222 -0
@@ -0,0 +1,138 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { ApiField, ComplexType } from '@opra/common';
3
+ import { VariableFormat } from '../enums/variable-format.enum.js';
4
+ import { VariableType } from '../enums/variable-type.enum.js';
5
+ /**
6
+ *
7
+ */
8
+ let VariableMetadataRecord = class VariableMetadataRecord {
9
+ };
10
+ VariableMetadataRecord = __decorate([
11
+ ComplexType({
12
+ description: 'User defined variables',
13
+ additionalFields: () => VariableMetadata,
14
+ })
15
+ ], VariableMetadataRecord);
16
+ export { VariableMetadataRecord };
17
+ /**
18
+ *
19
+ */
20
+ let VariableMetadata = class VariableMetadata {
21
+ ignored;
22
+ hotPlug;
23
+ };
24
+ __decorate([
25
+ ApiField({
26
+ description: 'Determine if the variable can be changed while worker running',
27
+ }),
28
+ __metadata("design:type", Boolean)
29
+ ], VariableMetadata.prototype, "hotPlug", void 0);
30
+ __decorate([
31
+ ApiField({
32
+ description: 'Label of the variable',
33
+ }),
34
+ __metadata("design:type", String)
35
+ ], VariableMetadata.prototype, "label", void 0);
36
+ __decorate([
37
+ ApiField({
38
+ description: 'Processor description',
39
+ }),
40
+ __metadata("design:type", String)
41
+ ], VariableMetadata.prototype, "description", void 0);
42
+ __decorate([
43
+ ApiField({
44
+ description: 'Examples of the variable value.',
45
+ type: 'string',
46
+ isArray: true,
47
+ }),
48
+ __metadata("design:type", Array)
49
+ ], VariableMetadata.prototype, "examples", void 0);
50
+ __decorate([
51
+ ApiField({
52
+ description: 'Choices of the variable',
53
+ type: 'string',
54
+ isArray: true,
55
+ }),
56
+ __metadata("design:type", Array)
57
+ ], VariableMetadata.prototype, "choice", void 0);
58
+ __decorate([
59
+ ApiField({
60
+ description: 'Data type of the variable',
61
+ type: VariableType,
62
+ }),
63
+ __metadata("design:type", String)
64
+ ], VariableMetadata.prototype, "type", void 0);
65
+ __decorate([
66
+ ApiField({
67
+ description: 'Nested variables. Available if type is object',
68
+ }),
69
+ __metadata("design:type", VariableMetadataRecord)
70
+ ], VariableMetadata.prototype, "variables", void 0);
71
+ __decorate([
72
+ ApiField({
73
+ description: 'Format of the variable. Available if type is string',
74
+ type: VariableFormat,
75
+ }),
76
+ __metadata("design:type", String)
77
+ ], VariableMetadata.prototype, "format", void 0);
78
+ __decorate([
79
+ ApiField({
80
+ description: 'List of values. Available if type is enum',
81
+ type: 'string',
82
+ }),
83
+ __metadata("design:type", Array)
84
+ ], VariableMetadata.prototype, "enumValues", void 0);
85
+ __decorate([
86
+ ApiField({
87
+ description: 'Determine if the variable is an array or not',
88
+ }),
89
+ __metadata("design:type", Boolean)
90
+ ], VariableMetadata.prototype, "isArray", void 0);
91
+ __decorate([
92
+ ApiField({
93
+ description: 'Minimum number of items in the array. Available if isArray is true',
94
+ }),
95
+ __metadata("design:type", Number)
96
+ ], VariableMetadata.prototype, "minOccurs", void 0);
97
+ __decorate([
98
+ ApiField({
99
+ description: 'Maximum number of items in the array. Available if isArray is true',
100
+ }),
101
+ __metadata("design:type", Number)
102
+ ], VariableMetadata.prototype, "maxOccurs", void 0);
103
+ __decorate([
104
+ ApiField({
105
+ description: 'Determine if the variable is required or not',
106
+ }),
107
+ __metadata("design:type", Boolean)
108
+ ], VariableMetadata.prototype, "required", void 0);
109
+ __decorate([
110
+ ApiField({
111
+ description: 'Default value of the variable',
112
+ }),
113
+ __metadata("design:type", Object)
114
+ ], VariableMetadata.prototype, "default", void 0);
115
+ __decorate([
116
+ ApiField({
117
+ description: 'Regex pattern for the variable value. Available if type is string',
118
+ }),
119
+ __metadata("design:type", String)
120
+ ], VariableMetadata.prototype, "pattern", void 0);
121
+ __decorate([
122
+ ApiField({
123
+ description: 'Minimum value for the variable',
124
+ }),
125
+ __metadata("design:type", Object)
126
+ ], VariableMetadata.prototype, "minValue", void 0);
127
+ __decorate([
128
+ ApiField({
129
+ description: 'Maximum value for the variable',
130
+ }),
131
+ __metadata("design:type", Object)
132
+ ], VariableMetadata.prototype, "maxValue", void 0);
133
+ VariableMetadata = __decorate([
134
+ ComplexType({
135
+ description: 'User defined variable definition',
136
+ })
137
+ ], VariableMetadata);
138
+ export { VariableMetadata };
@@ -0,0 +1,16 @@
1
+ import { LogLevel } from '../enums/log-level.js';
2
+ /**
3
+ * LogOptions
4
+ */
5
+ export declare class LogOptions {
6
+ constructor(init?: Partial<LogOptions>);
7
+ level?: LogLevel;
8
+ errorStack?: boolean;
9
+ }
10
+ /**
11
+ * ProfileSettings
12
+ */
13
+ export declare class ProfileLogOptions extends LogOptions {
14
+ constructor(init?: Partial<ProfileLogOptions>);
15
+ terminal?: boolean;
16
+ }
@@ -0,0 +1,56 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { ApiField, ComplexType } from '@opra/common';
3
+ import { LogLevel } from '../enums/log-level.js';
4
+ /**
5
+ * LogOptions
6
+ */
7
+ let LogOptions = class LogOptions {
8
+ constructor(init) {
9
+ if (init)
10
+ Object.assign(this, init);
11
+ }
12
+ level;
13
+ errorStack;
14
+ };
15
+ __decorate([
16
+ ApiField({
17
+ label: 'Level',
18
+ description: 'Determines the log level',
19
+ default: LogLevel.info,
20
+ }),
21
+ __metadata("design:type", String)
22
+ ], LogOptions.prototype, "level", void 0);
23
+ __decorate([
24
+ ApiField({
25
+ label: 'Error Stack',
26
+ description: 'Determines if error stack will be logged or not',
27
+ }),
28
+ __metadata("design:type", Boolean)
29
+ ], LogOptions.prototype, "errorStack", void 0);
30
+ LogOptions = __decorate([
31
+ ComplexType(),
32
+ __metadata("design:paramtypes", [Object])
33
+ ], LogOptions);
34
+ export { LogOptions };
35
+ /**
36
+ * ProfileSettings
37
+ */
38
+ let ProfileLogOptions = class ProfileLogOptions extends LogOptions {
39
+ constructor(init) {
40
+ super(init);
41
+ }
42
+ terminal;
43
+ };
44
+ __decorate([
45
+ ApiField({
46
+ label: 'Terminal',
47
+ description: 'Determines if logs will be piped to application terminal',
48
+ default: true,
49
+ }),
50
+ __metadata("design:type", Boolean)
51
+ ], ProfileLogOptions.prototype, "terminal", void 0);
52
+ ProfileLogOptions = __decorate([
53
+ ComplexType(),
54
+ __metadata("design:paramtypes", [Object])
55
+ ], ProfileLogOptions);
56
+ export { ProfileLogOptions };
@@ -0,0 +1,23 @@
1
+ import { LogOptions } from './log-options.js';
2
+ /**
3
+ * WorkerComponentRecord
4
+ */
5
+ export declare class WorkerComponentRecord {
6
+ [key: string]: ProfileComponent;
7
+ }
8
+ /**
9
+ * ProfileComponentSettings
10
+ */
11
+ export declare class ProfileComponentSettings {
12
+ constructor(init?: Partial<ProfileComponentSettings>);
13
+ logs?: LogOptions;
14
+ }
15
+ /**
16
+ * ProfileComponent
17
+ */
18
+ export declare class ProfileComponent {
19
+ className: string;
20
+ values?: Record<string, any>;
21
+ components?: WorkerComponentRecord;
22
+ settings?: ProfileComponentSettings;
23
+ }
@@ -0,0 +1,77 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { ApiField, ComplexType } from '@opra/common';
3
+ import { LogOptions } from './log-options.js';
4
+ /**
5
+ * WorkerComponentRecord
6
+ */
7
+ let WorkerComponentRecord = class WorkerComponentRecord {
8
+ };
9
+ WorkerComponentRecord = __decorate([
10
+ ComplexType({
11
+ description: 'Container for profile components',
12
+ additionalFields: () => ProfileComponent,
13
+ })
14
+ ], WorkerComponentRecord);
15
+ export { WorkerComponentRecord };
16
+ /**
17
+ * ProfileComponentSettings
18
+ */
19
+ let ProfileComponentSettings = class ProfileComponentSettings {
20
+ constructor(init) {
21
+ if (init)
22
+ Object.assign(this, init);
23
+ }
24
+ logs;
25
+ };
26
+ __decorate([
27
+ ApiField({
28
+ label: 'Logs',
29
+ description: 'Log settings of the component',
30
+ }),
31
+ __metadata("design:type", LogOptions)
32
+ ], ProfileComponentSettings.prototype, "logs", void 0);
33
+ ProfileComponentSettings = __decorate([
34
+ ComplexType(),
35
+ __metadata("design:paramtypes", [Object])
36
+ ], ProfileComponentSettings);
37
+ export { ProfileComponentSettings };
38
+ /**
39
+ * ProfileComponent
40
+ */
41
+ let ProfileComponent = class ProfileComponent {
42
+ };
43
+ __decorate([
44
+ ApiField({
45
+ description: 'Class name of the component',
46
+ examples: ['TcpClient', 'SerialClient'],
47
+ }),
48
+ __metadata("design:type", String)
49
+ ], ProfileComponent.prototype, "className", void 0);
50
+ __decorate([
51
+ ApiField({
52
+ label: 'Values',
53
+ description: 'Values of user defined variables',
54
+ type: 'object',
55
+ }),
56
+ __metadata("design:type", Object)
57
+ ], ProfileComponent.prototype, "values", void 0);
58
+ __decorate([
59
+ ApiField({
60
+ label: 'Components',
61
+ description: 'Sub components',
62
+ }),
63
+ __metadata("design:type", WorkerComponentRecord)
64
+ ], ProfileComponent.prototype, "components", void 0);
65
+ __decorate([
66
+ ApiField({
67
+ label: 'Settings',
68
+ description: 'Settings of the component',
69
+ }),
70
+ __metadata("design:type", ProfileComponentSettings)
71
+ ], ProfileComponent.prototype, "settings", void 0);
72
+ ProfileComponent = __decorate([
73
+ ComplexType({
74
+ description: 'User defined variable definition',
75
+ })
76
+ ], ProfileComponent);
77
+ export { ProfileComponent };
@@ -0,0 +1,25 @@
1
+ import { ProfileLogOptions } from './log-options.js';
2
+ import { WorkerComponentRecord } from './profile-component.js';
3
+ /**
4
+ * ProfileSettings
5
+ */
6
+ export declare class ProfileSettings {
7
+ constructor(init?: Partial<ProfileSettings>);
8
+ autostart?: boolean;
9
+ logs?: ProfileLogOptions;
10
+ }
11
+ /**
12
+ * Profile
13
+ */
14
+ export declare class Profile {
15
+ constructor(init?: Partial<Profile>);
16
+ id: string;
17
+ displayName: string;
18
+ processorClass: string;
19
+ group?: string;
20
+ description?: string;
21
+ iconUrl?: string;
22
+ values?: Record<string, any>;
23
+ components?: WorkerComponentRecord;
24
+ settings?: ProfileSettings;
25
+ }
@@ -0,0 +1,115 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { ApiField, ComplexType } from '@opra/common';
3
+ import { ProfileLogOptions } from './log-options.js';
4
+ import { WorkerComponentRecord } from './profile-component.js';
5
+ /**
6
+ * ProfileSettings
7
+ */
8
+ let ProfileSettings = class ProfileSettings {
9
+ constructor(init) {
10
+ if (init)
11
+ Object.assign(this, init);
12
+ }
13
+ autostart;
14
+ logs;
15
+ };
16
+ __decorate([
17
+ ApiField({
18
+ label: 'Auto Start',
19
+ description: 'Determines if the processor will be auto started or not',
20
+ }),
21
+ __metadata("design:type", Boolean)
22
+ ], ProfileSettings.prototype, "autostart", void 0);
23
+ __decorate([
24
+ ApiField({
25
+ label: 'Logs',
26
+ description: 'Log settings of the processor',
27
+ }),
28
+ __metadata("design:type", ProfileLogOptions)
29
+ ], ProfileSettings.prototype, "logs", void 0);
30
+ ProfileSettings = __decorate([
31
+ ComplexType(),
32
+ __metadata("design:paramtypes", [Object])
33
+ ], ProfileSettings);
34
+ export { ProfileSettings };
35
+ /**
36
+ * Profile
37
+ */
38
+ let Profile = class Profile {
39
+ constructor(init) {
40
+ if (init)
41
+ Object.assign(this, init);
42
+ }
43
+ };
44
+ __decorate([
45
+ ApiField({
46
+ label: 'Identity',
47
+ description: 'Uniquely identifies the profile',
48
+ required: true,
49
+ }),
50
+ __metadata("design:type", String)
51
+ ], Profile.prototype, "id", void 0);
52
+ __decorate([
53
+ ApiField({
54
+ label: 'Display Name',
55
+ description: 'Human friendly name of the profile to be displayed in the UI',
56
+ required: true,
57
+ }),
58
+ __metadata("design:type", String)
59
+ ], Profile.prototype, "displayName", void 0);
60
+ __decorate([
61
+ ApiField({
62
+ label: 'Processor Class',
63
+ description: 'Class name of the processor',
64
+ required: true,
65
+ }),
66
+ __metadata("design:type", String)
67
+ ], Profile.prototype, "processorClass", void 0);
68
+ __decorate([
69
+ ApiField({
70
+ label: 'Group',
71
+ description: 'Group of the profile. Profiles are grouped by this value',
72
+ }),
73
+ __metadata("design:type", String)
74
+ ], Profile.prototype, "group", void 0);
75
+ __decorate([
76
+ ApiField({
77
+ label: 'Description',
78
+ description: 'Long description',
79
+ }),
80
+ __metadata("design:type", String)
81
+ ], Profile.prototype, "description", void 0);
82
+ __decorate([
83
+ ApiField({
84
+ label: 'Icon',
85
+ description: 'Icon url',
86
+ }),
87
+ __metadata("design:type", String)
88
+ ], Profile.prototype, "iconUrl", void 0);
89
+ __decorate([
90
+ ApiField({
91
+ label: 'Values',
92
+ description: 'Values of user defined variables',
93
+ type: 'object',
94
+ }),
95
+ __metadata("design:type", Object)
96
+ ], Profile.prototype, "values", void 0);
97
+ __decorate([
98
+ ApiField({
99
+ label: 'Components',
100
+ description: 'Components of the processor',
101
+ }),
102
+ __metadata("design:type", WorkerComponentRecord)
103
+ ], Profile.prototype, "components", void 0);
104
+ __decorate([
105
+ ApiField({
106
+ label: 'Settings',
107
+ description: 'Settings of the processor',
108
+ }),
109
+ __metadata("design:type", ProfileSettings)
110
+ ], Profile.prototype, "settings", void 0);
111
+ Profile = __decorate([
112
+ ComplexType(),
113
+ __metadata("design:paramtypes", [Object])
114
+ ], Profile);
115
+ export { Profile };
@@ -0,0 +1,3 @@
1
+ import { ApiDocument } from '@opra/common';
2
+ export declare function initializeModelsDocument(): Promise<void>;
3
+ export declare function getModelsDocument(): ApiDocument;
@@ -0,0 +1,15 @@
1
+ import { ApiDocumentFactory } from '@opra/common';
2
+ import * as models from './models/index.js';
3
+ let doc;
4
+ export async function initializeModelsDocument() {
5
+ doc =
6
+ doc ||
7
+ (await ApiDocumentFactory.createDocument({
8
+ types: [...Object.values(models)],
9
+ }));
10
+ }
11
+ export function getModelsDocument() {
12
+ if (!doc)
13
+ throw new Error('You must call initializeModelsDocument() first');
14
+ return doc;
15
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@syncbridge/common",
3
+ "version": "0.4.0",
4
+ "description": "SyncBridge Common utilities",
5
+ "author": "Panates Inc",
6
+ "license": "UNLICENSED",
7
+ "dependencies": {
8
+ "@jsopen/objects": "^2.0.2",
9
+ "node-events-async": "^1.5.0",
10
+ "reflect-metadata": "^0.2.2",
11
+ "ts-gems": "^3.11.3",
12
+ "valgen": "^5.19.4"
13
+ },
14
+ "peerDependencies": {
15
+ "@opra/common": ">=1.17.1 <2"
16
+ },
17
+ "type": "module",
18
+ "module": "./index.js",
19
+ "types": "./index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./index.d.ts",
23
+ "default": "./index.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "engines": {
28
+ "node": ">=20.0"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ }
33
+ }
@@ -0,0 +1,13 @@
1
+ import { ProcessorBase } from './classes/processor-base.js';
2
+ import { ILogger } from './interfaces/index.js';
3
+ import { Profile } from './models/index.js';
4
+ export declare namespace ProcessorFactory {
5
+ const METADATA_KEY: unique symbol;
6
+ const PROFILE_KEY: unique symbol;
7
+ interface ProcessorOptions {
8
+ logger: ILogger;
9
+ dataDirectory?: string;
10
+ }
11
+ function createProcessor<T extends ProcessorBase>(profile: Profile, options?: ProcessorOptions): Promise<T>;
12
+ function updateProcessor(processor: ProcessorBase, profile: Profile, options?: ProcessorOptions): Promise<void>;
13
+ }
@@ -0,0 +1,106 @@
1
+ import { StackExecutor } from './classes/stack-executor.js';
2
+ import { initializeModelsDocument } from './models-document.js';
3
+ import { ExtensionRegistry } from './registry.js';
4
+ import { materializeMetadata } from './utils/metadata-utils.js';
5
+ export var ProcessorFactory;
6
+ (function (ProcessorFactory) {
7
+ ProcessorFactory.METADATA_KEY = Symbol('METADATA');
8
+ ProcessorFactory.PROFILE_KEY = Symbol('PROFILE');
9
+ async function createProcessor(profile, options) {
10
+ await initializeModelsDocument();
11
+ const reg = ExtensionRegistry.getProcessor(profile.processorClass);
12
+ const processor = new reg.ctor({
13
+ name: profile.displayName,
14
+ logger: options?.logger,
15
+ values: profile.values || {},
16
+ dataDirectory: options?.dataDirectory,
17
+ });
18
+ await _configureProcessor(processor, profile);
19
+ return processor;
20
+ }
21
+ ProcessorFactory.createProcessor = createProcessor;
22
+ async function updateProcessor(processor, profile, options) {
23
+ if (options?.logger)
24
+ processor.logger = options.logger;
25
+ if (options?.dataDirectory)
26
+ processor.dataDirectory = options.dataDirectory;
27
+ return _configureProcessor(processor, profile);
28
+ }
29
+ ProcessorFactory.updateProcessor = updateProcessor;
30
+ async function _configureProcessor(processor, profile) {
31
+ const processorMetadata = materializeMetadata(profile);
32
+ const stackExecutor = new StackExecutor();
33
+ function configureComponents(owner, curPath, newMetadata, newProfile, oldMetadata, oldProfile) {
34
+ if (!newMetadata.components)
35
+ return;
36
+ // Stack: components
37
+ stackExecutor.execute('components', () => {
38
+ for (const [key, childMetadata] of Object.entries(newMetadata.components)) {
39
+ // Stack: components/componentName
40
+ stackExecutor.execute(key, () => {
41
+ const oldChildMetadata = oldMetadata?.components?.[key];
42
+ const oldChildProfile = oldProfile?.components?.[key];
43
+ const childProfile = newProfile.components[key];
44
+ const componentPath = curPath ? curPath + '/' + key : key;
45
+ let componentInstance = owner.components?.[key];
46
+ /** Create component only if processor is stopped */
47
+ if (processor.stopped) {
48
+ const compReg = ExtensionRegistry.getComponent(childMetadata.className);
49
+ /** Create the component instance */
50
+ const compClass = compReg.ctor;
51
+ componentInstance = new compClass({
52
+ processor: processor,
53
+ parent: owner,
54
+ name: key,
55
+ path: componentPath,
56
+ values: childProfile.values || {},
57
+ });
58
+ owner.components = owner.components || {};
59
+ owner.components[key] = componentInstance;
60
+ if (childMetadata.propertyKey)
61
+ owner[childMetadata.propertyKey] = componentInstance;
62
+ }
63
+ else if (oldMetadata?.className &&
64
+ oldMetadata?.className !== newMetadata.className) {
65
+ throw new Error('Can not change component class while processor running. You should stop it first.');
66
+ }
67
+ else {
68
+ // todo update only hot-plug values
69
+ componentInstance.values = childProfile.values || {};
70
+ }
71
+ /** Create the child logger */
72
+ componentInstance.logger = owner.logger?.child({
73
+ component: componentPath,
74
+ });
75
+ if (childProfile.settings?.logs?.level) {
76
+ componentInstance.logger.level =
77
+ childProfile.settings?.logs?.level;
78
+ }
79
+ /** Initialize sub components */
80
+ if (childMetadata.components) {
81
+ configureComponents(componentInstance, componentPath, childMetadata, childProfile, oldChildMetadata, oldChildProfile);
82
+ }
83
+ });
84
+ }
85
+ });
86
+ }
87
+ configureComponents(processor, '', processorMetadata, profile, processor[ProcessorFactory.METADATA_KEY], processor[ProcessorFactory.PROFILE_KEY]);
88
+ processor[ProcessorFactory.METADATA_KEY] = processorMetadata;
89
+ processor[ProcessorFactory.PROFILE_KEY] = profile;
90
+ // processor.on('error', err => this.logger.error(err));
91
+ // processor.on('status-change', args => {
92
+ // this.messageHandler.emit('status-change', args);
93
+ // });
94
+ // processor.on(
95
+ // 'component-status-change',
96
+ // (component, status, statusMessage) => {
97
+ // this.messageHandler.emit('component-status-change', {
98
+ // componentName: component.name,
99
+ // path: component.path,
100
+ // status,
101
+ // statusMessage,
102
+ // });
103
+ // },
104
+ // );
105
+ }
106
+ })(ProcessorFactory || (ProcessorFactory = {}));
package/registry.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { Type } from 'ts-gems';
2
+ import type { ComponentBase } from './classes/component-base.js';
3
+ import type { ProcessorBase } from './classes/processor-base.js';
4
+ import type { ComponentMetadata, ProcessorMetadata } from './models/index.js';
5
+ export declare namespace ExtensionRegistry {
6
+ const packages: Map<string, ExtensionPackageRecord>;
7
+ const components: Map<string, ComponentRecord>;
8
+ const processors: Map<string, ProcessorRecord>;
9
+ function setResolver(resolver: (name: string) => string): void;
10
+ function getProcessor(className: string): ExtensionRegistry.ProcessorRecord;
11
+ function getComponent(className: string): ExtensionRegistry.ComponentRecord;
12
+ function registerPackage(...specifiers: string[]): Promise<void>;
13
+ function registerComponent(ctor: Type, packageName: string): Promise<void>;
14
+ function registerProcessor(ctor: Type, packageName: string): Promise<void>;
15
+ }
16
+ export declare namespace ExtensionRegistry {
17
+ interface ExtensionPackageRecord {
18
+ name: string;
19
+ description?: string;
20
+ version: string;
21
+ }
22
+ interface ComponentRecord {
23
+ package: string;
24
+ ctor: Type<ComponentBase>;
25
+ metadata: ComponentMetadata;
26
+ }
27
+ interface ProcessorRecord {
28
+ package: string;
29
+ ctor: Type<ProcessorBase>;
30
+ metadata: ProcessorMetadata;
31
+ }
32
+ }