@syncbridge/common 0.12.2 → 0.12.4
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/constants.js +1 -1
- package/package.json +2 -2
- package/processor-factory.js +10 -12
- package/utils/decode-profile.d.ts +13 -4
- package/utils/decode-profile.js +58 -33
- package/utils/materialize-metadata.d.ts +8 -2
- package/utils/materialize-metadata.js +16 -8
package/constants.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncbridge/common",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.4",
|
|
4
4
|
"description": "SyncBridge Common utilities",
|
|
5
5
|
"author": "Panates Inc",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"reflect-metadata": "^0.2.2",
|
|
13
13
|
"semver": "^7.8.0",
|
|
14
14
|
"ts-gems": "^3.12.0",
|
|
15
|
-
"valgen": "^6.
|
|
15
|
+
"valgen": "^6.2.0"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
18
|
"@opra/common": ">=1.17.1 <2"
|
package/processor-factory.js
CHANGED
|
@@ -7,8 +7,7 @@ import { StackExecutor } from './classes/stack-executor.js';
|
|
|
7
7
|
import { ElementComponentMetadataRecord, ProcessorMetadata, Profile, ProfileComponentRecord, VariableMetadataRecord, } from './models/index.js';
|
|
8
8
|
import { initializeModelsDocument } from './models-document.js';
|
|
9
9
|
import { ExtensionRegistry } from './registry/extension-registry.js';
|
|
10
|
-
import {
|
|
11
|
-
import { materializeMetadataSilent } from './utils/materialize-metadata.js';
|
|
10
|
+
import { decodeProfileSilent } from './utils/decode-profile.js';
|
|
12
11
|
export var ProcessorFactory;
|
|
13
12
|
(function (ProcessorFactory) {
|
|
14
13
|
ProcessorFactory.METADATA_KEY = Symbol('METADATA');
|
|
@@ -122,32 +121,31 @@ export var ProcessorFactory;
|
|
|
122
121
|
}
|
|
123
122
|
ProcessorFactory.updateProcessorSilent = updateProcessorSilent;
|
|
124
123
|
async function _configureProcessor(ctx) {
|
|
125
|
-
|
|
124
|
+
/* Decode profile */
|
|
125
|
+
const r1 = decodeProfileSilent(ctx.profile);
|
|
126
126
|
if (!r1.result) {
|
|
127
127
|
ctx.stackExecutor.issues.push(...r1.issues);
|
|
128
128
|
return false;
|
|
129
129
|
}
|
|
130
|
-
|
|
131
|
-
const profile = decodeProfile(r1.metadata, ctx.profile);
|
|
132
|
-
ctx.profile = profile;
|
|
130
|
+
ctx.profile = r1.profile;
|
|
133
131
|
const oldMetadata = ctx.processor?.[ProcessorFactory.METADATA_KEY];
|
|
134
132
|
const oldProfile = ctx.processor?.[ProcessorFactory.PROFILE_KEY];
|
|
135
133
|
if (!ctx.processor) {
|
|
136
134
|
/* Create new processor instance */
|
|
137
|
-
const reg = ExtensionRegistry.getProcessor(profile.processorUri, profile.processorVersion);
|
|
135
|
+
const reg = ExtensionRegistry.getProcessor(r1.profile.processorUri, r1.profile.processorVersion);
|
|
138
136
|
const processorClass = await reg.getClass();
|
|
139
137
|
/* Create processor instance */
|
|
140
138
|
ctx.processor = new processorClass({
|
|
141
|
-
name: profile.displayName,
|
|
139
|
+
name: r1.profile.displayName,
|
|
142
140
|
logger: ctx.options?.logger,
|
|
143
|
-
values: profile.values ? clone(profile.values) : {},
|
|
141
|
+
values: r1.profile.values ? clone(r1.profile.values) : {},
|
|
144
142
|
dataDirectory: ctx.options?.dataDirectory,
|
|
145
143
|
});
|
|
146
144
|
}
|
|
147
145
|
else {
|
|
148
146
|
/* Update values */
|
|
149
147
|
if (!ctx.stackExecutor.execute('values', () => {
|
|
150
|
-
_patchValues(ctx, ctx.processor, ctx.processor.values, '', profile.values, r1.metadata.variables || {}, oldProfile?.values, oldMetadata.variables);
|
|
148
|
+
_patchValues(ctx, ctx.processor, ctx.processor.values, '', r1.profile.values, r1.metadata.variables || {}, oldProfile?.values, oldMetadata.variables);
|
|
151
149
|
})) {
|
|
152
150
|
return false;
|
|
153
151
|
}
|
|
@@ -157,7 +155,7 @@ export var ProcessorFactory;
|
|
|
157
155
|
owner: ctx.processor,
|
|
158
156
|
curPath: '',
|
|
159
157
|
newMetadataComponents: r1.metadata.components,
|
|
160
|
-
newProfileComponents: profile.components,
|
|
158
|
+
newProfileComponents: r1.profile.components,
|
|
161
159
|
oldMetadataComponents: oldMetadata?.components,
|
|
162
160
|
oldProfileComponents: oldProfile?.components,
|
|
163
161
|
};
|
|
@@ -165,7 +163,7 @@ export var ProcessorFactory;
|
|
|
165
163
|
return false;
|
|
166
164
|
}
|
|
167
165
|
ctx.processor[ProcessorFactory.METADATA_KEY] = r1.metadata;
|
|
168
|
-
ctx.processor[ProcessorFactory.PROFILE_KEY] = profile;
|
|
166
|
+
ctx.processor[ProcessorFactory.PROFILE_KEY] = r1.profile;
|
|
169
167
|
return true;
|
|
170
168
|
}
|
|
171
169
|
function _validateComponents(ctx) {
|
|
@@ -7,19 +7,28 @@ export interface CodecOptions {
|
|
|
7
7
|
/**
|
|
8
8
|
* Decodes profile
|
|
9
9
|
*/
|
|
10
|
-
export declare function decodeProfile(
|
|
11
|
-
export declare function decodeProfileSilent(
|
|
10
|
+
export declare function decodeProfile(profile: Profile, options?: CodecOptions): Profile;
|
|
11
|
+
export declare function decodeProfileSilent(profile: Profile, options?: CodecOptions): {
|
|
12
|
+
result: true;
|
|
12
13
|
profile: Profile;
|
|
14
|
+
metadata: ProcessorMetadata;
|
|
13
15
|
issues?: StackExecutor.Issue[];
|
|
16
|
+
} | {
|
|
17
|
+
result: false;
|
|
18
|
+
issues: StackExecutor.Issue[];
|
|
14
19
|
};
|
|
15
20
|
/**
|
|
16
21
|
* Encodes profile
|
|
17
22
|
*/
|
|
18
|
-
export declare function encodeProfile(
|
|
23
|
+
export declare function encodeProfile(rawProfile: Profile, options?: CodecOptions): Profile;
|
|
19
24
|
/**
|
|
20
25
|
* Encodes profile
|
|
21
26
|
*/
|
|
22
|
-
export declare function encodeProfileSilent(
|
|
27
|
+
export declare function encodeProfileSilent(profile: Profile, options?: CodecOptions): {
|
|
28
|
+
result: true;
|
|
23
29
|
profile: Profile;
|
|
24
30
|
issues?: StackExecutor.Issue[];
|
|
31
|
+
} | {
|
|
32
|
+
result: false;
|
|
33
|
+
issues: StackExecutor.Issue[];
|
|
25
34
|
};
|
package/utils/decode-profile.js
CHANGED
|
@@ -4,82 +4,102 @@ import { StackExecutor } from '../classes/stack-executor.js';
|
|
|
4
4
|
import { ElementComponentMetadata, ProcessorMetadata, Profile, ProfileComponent, VariableMetadataRecord, VariableType, } from '../models/index.js';
|
|
5
5
|
import { getModelsDocument } from '../models-document.js';
|
|
6
6
|
import { secretDecryptor, secretEncryptor } from './encrypt-helpers.js';
|
|
7
|
+
import { materializeMetadataSilent } from './materialize-metadata.js';
|
|
7
8
|
/**
|
|
8
9
|
* Decodes profile
|
|
9
10
|
*/
|
|
10
|
-
export function decodeProfile(
|
|
11
|
-
const
|
|
12
|
-
if (
|
|
11
|
+
export function decodeProfile(profile, options) {
|
|
12
|
+
const r1 = decodeProfileSilent(profile, options);
|
|
13
|
+
if (!r1.result) {
|
|
13
14
|
const msg = 'Worker profile decoding error.\n ' +
|
|
14
|
-
issues
|
|
15
|
+
r1.issues
|
|
15
16
|
.map(issue => `- ${issue.message}. Location: /${issue.location}`)
|
|
16
17
|
.join('\n ');
|
|
17
|
-
issues.forEach(issue => {
|
|
18
|
+
r1.issues.forEach(issue => {
|
|
18
19
|
if (issue instanceof SbValidationError)
|
|
19
20
|
delete issue.stack;
|
|
20
21
|
});
|
|
21
22
|
throw new SbValidationError(msg, {
|
|
22
|
-
workerId:
|
|
23
|
-
issues,
|
|
23
|
+
workerId: profile.id,
|
|
24
|
+
issues: r1.issues,
|
|
24
25
|
});
|
|
25
26
|
}
|
|
26
|
-
return
|
|
27
|
+
return r1.profile;
|
|
27
28
|
}
|
|
28
|
-
export function decodeProfileSilent(
|
|
29
|
+
export function decodeProfileSilent(profile, options) {
|
|
29
30
|
const modelsDocument = getModelsDocument();
|
|
31
|
+
const r1 = materializeMetadataSilent(profile);
|
|
32
|
+
if (!r1.result) {
|
|
33
|
+
return { result: false, issues: r1.issues };
|
|
34
|
+
}
|
|
30
35
|
const t = modelsDocument.node.getComplexType(Profile);
|
|
31
36
|
const fn = t.generateCodec('decode');
|
|
32
|
-
const out = fn(
|
|
37
|
+
const out = fn(profile);
|
|
33
38
|
const stackExecutor = new StackExecutor();
|
|
34
|
-
_codecProcessElement(stackExecutor, 'decode',
|
|
35
|
-
|
|
39
|
+
_codecProcessElement(stackExecutor, 'decode', r1.metadata, out, options);
|
|
40
|
+
if (stackExecutor.issues.find(i => !(i.severity === 'warning' || i.severity === 'info'))) {
|
|
41
|
+
return { result: false, issues: stackExecutor.issues };
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
result: true,
|
|
45
|
+
profile: out,
|
|
46
|
+
metadata: r1.metadata,
|
|
47
|
+
issues: stackExecutor.issues,
|
|
48
|
+
};
|
|
36
49
|
}
|
|
37
50
|
/**
|
|
38
51
|
* Encodes profile
|
|
39
52
|
*/
|
|
40
|
-
export function encodeProfile(
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
53
|
+
export function encodeProfile(rawProfile, options) {
|
|
54
|
+
const r1 = encodeProfileSilent(rawProfile, options);
|
|
55
|
+
if (!r1.result) {
|
|
43
56
|
const msg = 'Worker profile encoding error.\n ' +
|
|
44
|
-
issues
|
|
57
|
+
r1.issues
|
|
45
58
|
.map(issue => `- ${issue.message}. Location: /${issue.location}`)
|
|
46
59
|
.join('\n ');
|
|
47
|
-
issues.forEach(issue => {
|
|
60
|
+
r1.issues.forEach(issue => {
|
|
48
61
|
if (issue instanceof SbValidationError)
|
|
49
62
|
delete issue.stack;
|
|
50
63
|
});
|
|
51
64
|
throw new SbValidationError(msg, {
|
|
52
65
|
workerId: rawProfile.id,
|
|
53
|
-
issues,
|
|
66
|
+
issues: r1.issues,
|
|
54
67
|
});
|
|
55
68
|
}
|
|
56
|
-
return
|
|
69
|
+
return r1.profile;
|
|
57
70
|
}
|
|
58
71
|
/**
|
|
59
72
|
* Encodes profile
|
|
60
73
|
*/
|
|
61
|
-
export function encodeProfileSilent(
|
|
74
|
+
export function encodeProfileSilent(profile, options) {
|
|
62
75
|
const modelsDocument = getModelsDocument();
|
|
76
|
+
const r1 = materializeMetadataSilent(profile);
|
|
77
|
+
if (!r1.result) {
|
|
78
|
+
return { result: false, issues: r1.issues };
|
|
79
|
+
}
|
|
63
80
|
const t = modelsDocument.node.getComplexType(Profile);
|
|
64
81
|
const fn = t.generateCodec('encode', { coerce: true });
|
|
65
82
|
const out = fn({
|
|
66
|
-
id:
|
|
67
|
-
processorUri:
|
|
68
|
-
displayName:
|
|
69
|
-
group:
|
|
70
|
-
description:
|
|
71
|
-
iconUrl:
|
|
83
|
+
id: profile.id,
|
|
84
|
+
processorUri: profile.processorUri,
|
|
85
|
+
displayName: profile.displayName,
|
|
86
|
+
group: profile.group,
|
|
87
|
+
description: profile.description,
|
|
88
|
+
iconUrl: profile.iconUrl,
|
|
72
89
|
settings: {
|
|
73
|
-
autostart:
|
|
74
|
-
logs:
|
|
75
|
-
...
|
|
90
|
+
autostart: profile.settings?.autostart ?? false,
|
|
91
|
+
logs: profile.settings?.logs ?? {},
|
|
92
|
+
...profile.settings,
|
|
76
93
|
},
|
|
77
|
-
values:
|
|
78
|
-
...
|
|
94
|
+
values: profile.values,
|
|
95
|
+
...profile,
|
|
79
96
|
});
|
|
80
97
|
const stackExecutor = new StackExecutor();
|
|
81
|
-
_codecProcessElement(stackExecutor, 'encode', metadata, out, options);
|
|
82
|
-
|
|
98
|
+
_codecProcessElement(stackExecutor, 'encode', r1.metadata, out, options);
|
|
99
|
+
if (stackExecutor.issues.find(i => !(i.severity === 'warning' || i.severity === 'info'))) {
|
|
100
|
+
return { result: false, issues: stackExecutor.issues };
|
|
101
|
+
}
|
|
102
|
+
return { result: true, profile: out, issues: stackExecutor.issues };
|
|
83
103
|
}
|
|
84
104
|
/**
|
|
85
105
|
*
|
|
@@ -130,6 +150,11 @@ function _decodeVariables(stackExecutor, codec, metadata, profile, options) {
|
|
|
130
150
|
if (defVariable == null)
|
|
131
151
|
return;
|
|
132
152
|
const fns = [];
|
|
153
|
+
if (defVariable.ignored) {
|
|
154
|
+
if (defVariable.default === undefined)
|
|
155
|
+
return;
|
|
156
|
+
fns.push(vg.fixed(defVariable.default));
|
|
157
|
+
}
|
|
133
158
|
switch (defVariable.type) {
|
|
134
159
|
case VariableType.String: {
|
|
135
160
|
switch (defVariable.format) {
|
|
@@ -3,11 +3,17 @@ import { ProcessorMetadata, Profile } from '../models/index.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* Materializes metadata from profile.
|
|
5
5
|
*/
|
|
6
|
-
export declare function materializeMetadata(profile: Profile,
|
|
6
|
+
export declare function materializeMetadata(profile: Profile, options?: {
|
|
7
|
+
returnUndefinedComponentsAsAbstract?: boolean;
|
|
8
|
+
removeIgnoredVariables?: boolean;
|
|
9
|
+
}): ProcessorMetadata;
|
|
7
10
|
/**
|
|
8
11
|
*
|
|
9
12
|
*/
|
|
10
|
-
export declare function materializeMetadataSilent(profile: Profile,
|
|
13
|
+
export declare function materializeMetadataSilent(profile: Profile, options?: {
|
|
14
|
+
returnUndefinedComponentsAsAbstract?: boolean;
|
|
15
|
+
removeIgnoredVariables?: boolean;
|
|
16
|
+
}): {
|
|
11
17
|
result: true;
|
|
12
18
|
metadata: ProcessorMetadata;
|
|
13
19
|
issues: StackExecutor.Issue[];
|
|
@@ -6,8 +6,8 @@ import { ExtensionRegistry } from '../registry/extension-registry.js';
|
|
|
6
6
|
/**
|
|
7
7
|
* Materializes metadata from profile.
|
|
8
8
|
*/
|
|
9
|
-
export function materializeMetadata(profile,
|
|
10
|
-
const r = materializeMetadataSilent(profile,
|
|
9
|
+
export function materializeMetadata(profile, options) {
|
|
10
|
+
const r = materializeMetadataSilent(profile, options);
|
|
11
11
|
if (!r.result) {
|
|
12
12
|
const msg = 'Worker profile validation error.\n ' +
|
|
13
13
|
r.issues
|
|
@@ -27,7 +27,7 @@ export function materializeMetadata(profile, returnUndefinedComponentsAsAbstract
|
|
|
27
27
|
/**
|
|
28
28
|
*
|
|
29
29
|
*/
|
|
30
|
-
export function materializeMetadataSilent(profile,
|
|
30
|
+
export function materializeMetadataSilent(profile, options) {
|
|
31
31
|
const stackExecutor = new StackExecutor();
|
|
32
32
|
const processorRec = ExtensionRegistry.getProcessor(profile.processorUri);
|
|
33
33
|
const metadata = omitUndefined(clone({
|
|
@@ -42,9 +42,17 @@ export function materializeMetadataSilent(profile, returnUndefinedComponentsAsAb
|
|
|
42
42
|
components: processorRec.metadata.components,
|
|
43
43
|
...processorRec.metadata,
|
|
44
44
|
}, { deep: 'full' }));
|
|
45
|
+
if (metadata.variables) {
|
|
46
|
+
/* Remove ignored variables */
|
|
47
|
+
for (const [k, variable] of Object.entries(metadata.variables)) {
|
|
48
|
+
if (variable.ignored && options?.removeIgnoredVariables)
|
|
49
|
+
delete metadata.variables[k];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/* Materialize components */
|
|
45
53
|
if (metadata.components) {
|
|
46
54
|
stackExecutor.execute('components', () => {
|
|
47
|
-
metadata.components = _materializeComponents(stackExecutor, metadata.components, profile,
|
|
55
|
+
metadata.components = _materializeComponents(stackExecutor, metadata.components, profile, options);
|
|
48
56
|
});
|
|
49
57
|
}
|
|
50
58
|
if (stackExecutor.issues?.length)
|
|
@@ -55,7 +63,7 @@ export function materializeMetadataSilent(profile, returnUndefinedComponentsAsAb
|
|
|
55
63
|
*
|
|
56
64
|
* @private
|
|
57
65
|
*/
|
|
58
|
-
function _materializeComponents(stackExecutor, srcMetadata, profile,
|
|
66
|
+
function _materializeComponents(stackExecutor, srcMetadata, profile, options) {
|
|
59
67
|
const out = {};
|
|
60
68
|
for (const [componentName, metadataComponent,] of Object.entries(srcMetadata)) {
|
|
61
69
|
stackExecutor.execute(componentName, () => {
|
|
@@ -63,7 +71,7 @@ function _materializeComponents(stackExecutor, srcMetadata, profile, returnUndef
|
|
|
63
71
|
let materializedComponent;
|
|
64
72
|
const profileComponent = profile?.components?.[componentName];
|
|
65
73
|
if (!profileComponent) {
|
|
66
|
-
if (!returnUndefinedComponentsAsAbstract) {
|
|
74
|
+
if (!options?.returnUndefinedComponentsAsAbstract) {
|
|
67
75
|
if (metadataComponent.required)
|
|
68
76
|
throw new SbValidationError(`Component "${componentName}" should be configured`);
|
|
69
77
|
return;
|
|
@@ -96,12 +104,12 @@ function _materializeComponents(stackExecutor, srcMetadata, profile, returnUndef
|
|
|
96
104
|
out[componentName] = materializedComponent;
|
|
97
105
|
/** Remove ignored variables */
|
|
98
106
|
for (const [k, variable] of Object.entries(materializedComponent.variables)) {
|
|
99
|
-
if (variable.ignored)
|
|
107
|
+
if (variable.ignored && options?.removeIgnoredVariables)
|
|
100
108
|
delete materializedComponent.variables[k];
|
|
101
109
|
}
|
|
102
110
|
if (reg.metadata.components) {
|
|
103
111
|
stackExecutor.execute('components', () => {
|
|
104
|
-
materializedComponent.components = _materializeComponents(stackExecutor, reg.metadata.components, profileComponent,
|
|
112
|
+
materializedComponent.components = _materializeComponents(stackExecutor, reg.metadata.components, profileComponent, options);
|
|
105
113
|
});
|
|
106
114
|
}
|
|
107
115
|
}
|