@onify/flow-extensions 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.FlowScripts = FlowScripts;
7
+ exports.JavaScript = JavaScript;
8
+ exports.JavaScriptResource = JavaScriptResource;
9
+
10
+ var _path = require("path");
11
+
12
+ var _fs = require("fs");
13
+
14
+ var _vm = require("vm");
15
+
16
+ const kSyntaxError = Symbol.for('syntax error');
17
+ const kResources = Symbol.for('resources base');
18
+
19
+ class FlowScriptError extends Error {
20
+ constructor(fromErr) {
21
+ super(fromErr.message);
22
+ this.name = this.constructor.name;
23
+ this.message = fromErr.message;
24
+ Object.defineProperty(this, 'stack', {
25
+ get() {
26
+ return fromErr.stack && fromErr.stack.split('\n').slice(0, 7).join('\n');
27
+ }
28
+
29
+ });
30
+ Object.defineProperty(this, 'code', {
31
+ get() {
32
+ return 'EFLOW_SCRIPT';
33
+ }
34
+
35
+ });
36
+ }
37
+
38
+ toString() {
39
+ return '[FlowScriptError] ' + this.message + '\n' + this.stack;
40
+ }
41
+
42
+ }
43
+
44
+ class FlowSyntaxError extends Error {
45
+ constructor(fromErr) {
46
+ super(fromErr.message);
47
+ this.name = this.constructor.name;
48
+ this.message = fromErr.message;
49
+ Object.defineProperty(this, 'stack', {
50
+ get() {
51
+ return fromErr.stack && fromErr.stack.split('\n').slice(0, 6).join('\n');
52
+ }
53
+
54
+ });
55
+ Object.defineProperty(this, 'code', {
56
+ get() {
57
+ return 'EFLOW_SCRIPT';
58
+ }
59
+
60
+ });
61
+ }
62
+
63
+ toString() {
64
+ return '[FlowSyntaxError] ' + this.message + '\n' + this.stack;
65
+ }
66
+
67
+ }
68
+
69
+ function FlowScripts(flowName, resourceBase, runContext, timeout = 60000) {
70
+ this._name = flowName;
71
+ this._scripts = {};
72
+ this._timeout = timeout;
73
+ this._runContext = runContext;
74
+ this[kResources] = resourceBase;
75
+ }
76
+
77
+ FlowScripts.prototype.register = function register({
78
+ id,
79
+ type,
80
+ behaviour
81
+ }) {
82
+ let language, scriptBody, resource;
83
+
84
+ switch (type) {
85
+ case 'bpmn:SequenceFlow':
86
+ {
87
+ if (!behaviour.conditionExpression) return;
88
+ language = behaviour.conditionExpression.language;
89
+ scriptBody = behaviour.conditionExpression.body;
90
+ resource = behaviour.conditionExpression.resource;
91
+ break;
92
+ }
93
+
94
+ default:
95
+ {
96
+ language = behaviour.scriptFormat;
97
+ scriptBody = behaviour.script;
98
+ resource = behaviour.resource;
99
+ }
100
+ }
101
+
102
+ if (!language) language = 'javascript';
103
+ if (!['js', 'javascript'].includes(language.toLowerCase().trim())) return;
104
+ language = 'javascript';
105
+ const name = this._name;
106
+ const filename = `${name}/${type}/${id}`;
107
+
108
+ if (scriptBody) {
109
+ this._scripts[id] = new JavaScript(name, scriptBody, this._runContext, {
110
+ filename,
111
+ timeout: this._timeout
112
+ });
113
+ } else if (resource) {
114
+ this._scripts[id] = new JavaScriptResource(name, resource, this[kResources], this._runContext, {
115
+ filename,
116
+ timeout: this._timeout
117
+ });
118
+ }
119
+ };
120
+
121
+ FlowScripts.prototype.getScript = function getScript(scriptType, {
122
+ id
123
+ }) {
124
+ return this._scripts[id];
125
+ };
126
+
127
+ function JavaScript(flowName, scriptBody, runContext, options) {
128
+ this.flowName = flowName;
129
+ this._runContext = runContext;
130
+ this.timeout = options && options.timeout;
131
+
132
+ try {
133
+ this.script = new _vm.Script(scriptBody, options);
134
+ } catch (err) {
135
+ this[kSyntaxError] = new FlowSyntaxError(err);
136
+ }
137
+ }
138
+
139
+ JavaScript.prototype.execute = async function execute(executionContext, callback) {
140
+ let callbackCalled;
141
+ const syntaxError = this[kSyntaxError];
142
+ if (syntaxError) return next(syntaxError);
143
+
144
+ try {
145
+ await this.script.runInNewContext({ ...executionContext,
146
+ Date,
147
+ console: {
148
+ log: console.log // eslint-disable-line no-console
149
+
150
+ },
151
+ Buffer: {
152
+ from: Buffer.from
153
+ },
154
+ contextName: this.flowName,
155
+ ...this._runContext,
156
+ next
157
+ }, {
158
+ timeout: this.timeout
159
+ });
160
+ } catch (err) {
161
+ return next(new FlowScriptError(err));
162
+ }
163
+
164
+ async function next(err, ...args) {
165
+ if (callbackCalled) return;
166
+ callbackCalled = true;
167
+ if (err) return callback(err);
168
+ callback(null, ...args);
169
+ }
170
+ };
171
+
172
+ function JavaScriptResource(flowName, resource, resourceBase, runContext, options) {
173
+ this.flowName = flowName;
174
+ this.resource = resource;
175
+ this.options = options;
176
+ this._runContext = runContext;
177
+ this[kResources] = resourceBase;
178
+ }
179
+
180
+ JavaScriptResource.prototype.execute = async function execute(executionContext, callback) {
181
+ try {
182
+ var scriptBody = await _fs.promises.readFile((0, _path.join)(this[kResources], executionContext.resolveExpression(this.resource))); // eslint-disable-line no-var
183
+ } catch (err) {
184
+ const {
185
+ filename
186
+ } = this.options;
187
+ return callback(new Error(`${filename}: script resource ${this.resource} not found`));
188
+ }
189
+
190
+ const script = new JavaScript(this.flowName, scriptBody, this._runContext, { ...this.options,
191
+ filename: this.resource
192
+ });
193
+ return script.execute(executionContext, callback);
194
+ };
package/dist/src/IO.js ADDED
@@ -0,0 +1,234 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.InputOutput = exports.IOScript = exports.IOMap = exports.IOList = exports.IOBase = void 0;
7
+
8
+ class InputOutput {
9
+ constructor(parentId, behaviour, context) {
10
+ this.parentId = parentId;
11
+ this.context = context;
12
+ const {
13
+ inputParameters,
14
+ outputParameters
15
+ } = behaviour;
16
+ this.input = this._map(parentId, inputParameters, 'input', context);
17
+ this.output = this._map(parentId, outputParameters, 'output', context);
18
+ }
19
+
20
+ async getInput(activity, executionMessage) {
21
+ const input = this.input;
22
+ const values = await Promise.all(input.map(parm => parm.getValue(activity, executionMessage)));
23
+ return values.reduce((result, parm) => Object.assign(result, parm), {});
24
+ }
25
+
26
+ async getOutput(activity, executionMessage) {
27
+ const output = this.output;
28
+ const values = await Promise.all(output.map(parm => parm.getValue(activity, executionMessage)));
29
+ return values.reduce((result, parm) => Object.assign(result, parm), {});
30
+ }
31
+
32
+ _map(parentId, list, ioType, context) {
33
+ const mapped = [];
34
+ if (!list) return mapped;
35
+
36
+ for (const parm of list) {
37
+ const definition = parm.definition;
38
+ const type = definition && definition.$type;
39
+
40
+ switch (type) {
41
+ case 'camunda:Map':
42
+ {
43
+ mapped.push(new IOMap(parm));
44
+ break;
45
+ }
46
+
47
+ case 'camunda:List':
48
+ {
49
+ mapped.push(new IOList(parm));
50
+ break;
51
+ }
52
+
53
+ case 'camunda:Script':
54
+ {
55
+ const id = `${parentId}/${ioType}/${type}/${parm.name}`;
56
+ const {
57
+ scriptFormat,
58
+ value,
59
+ resource
60
+ } = definition;
61
+ if (!value && !resource) break;
62
+ context.environment.scripts.register({
63
+ id,
64
+ type: parm.$type,
65
+ behaviour: {
66
+ scriptFormat,
67
+ ...(value ? {
68
+ script: value
69
+ } : undefined),
70
+ ...(resource ? {
71
+ resource
72
+ } : undefined)
73
+ }
74
+ });
75
+ mapped.push(new IOScript(parm, id));
76
+ break;
77
+ }
78
+
79
+ default:
80
+ {
81
+ mapped.push(new IOBase(parm));
82
+ }
83
+ }
84
+ }
85
+
86
+ return mapped;
87
+ }
88
+
89
+ }
90
+
91
+ exports.InputOutput = InputOutput;
92
+
93
+ class IOBase {
94
+ constructor(parm) {
95
+ this.name = parm.name;
96
+ this.type = parm.definition && parm.definition.$type || 'string';
97
+ this.behaviour = parm;
98
+ }
99
+
100
+ getValue(activity, executionMessage) {
101
+ return {
102
+ [this.name]: activity.environment.resolveExpression(this.behaviour.value, executionMessage)
103
+ };
104
+ }
105
+
106
+ }
107
+
108
+ exports.IOBase = IOBase;
109
+
110
+ class IOMap extends IOBase {
111
+ constructor(parm) {
112
+ super(parm);
113
+ }
114
+
115
+ getValue(activity, executionMessage) {
116
+ const name = this.name;
117
+ const entries = this.behaviour.definition.entries;
118
+ if (!Array.isArray(entries)) return {
119
+ [name]: {}
120
+ };
121
+ const environment = activity.environment;
122
+ const result = {};
123
+
124
+ for (const {
125
+ key,
126
+ value
127
+ } of entries) {
128
+ if (!key) continue;
129
+ const val = environment.resolveExpression(value, executionMessage);
130
+
131
+ if (key in result) {
132
+ if (val === undefined) continue;
133
+ const current = result[key];
134
+
135
+ if (Array.isArray(current)) {
136
+ current.push(val);
137
+ } else {
138
+ const items = result[key] = [];
139
+ if (current !== undefined) items.push(current);
140
+ items.push(val);
141
+ }
142
+ } else {
143
+ result[key] = val;
144
+ }
145
+ }
146
+
147
+ return {
148
+ [name]: result
149
+ };
150
+ }
151
+
152
+ }
153
+
154
+ exports.IOMap = IOMap;
155
+
156
+ class IOList extends IOBase {
157
+ constructor(parm) {
158
+ super(parm);
159
+ }
160
+
161
+ getValue(activity, executionMessage) {
162
+ var _this$behaviour$defin;
163
+
164
+ const name = this.name;
165
+ const items = (_this$behaviour$defin = this.behaviour.definition) === null || _this$behaviour$defin === void 0 ? void 0 : _this$behaviour$defin.items;
166
+ const result = [];
167
+ if (!Array.isArray(items)) return {
168
+ [name]: result
169
+ };
170
+ const environment = activity.environment;
171
+
172
+ for (const item of items) {
173
+ const val = environment.resolveExpression(item.value, executionMessage);
174
+ if (val !== undefined) result.push(val);
175
+ }
176
+
177
+ return {
178
+ [name]: result
179
+ };
180
+ }
181
+
182
+ }
183
+
184
+ exports.IOList = IOList;
185
+
186
+ class IOScript extends IOBase {
187
+ constructor(parm, scriptId) {
188
+ super(parm);
189
+ this.id = scriptId;
190
+ this.script = true;
191
+ }
192
+
193
+ getValue(activity, executionMessage) {
194
+ const definition = this.behaviour.definition;
195
+ const name = this.name;
196
+ const environment = activity.environment;
197
+ const {
198
+ fields,
199
+ content,
200
+ properties,
201
+ ...rest
202
+ } = executionMessage;
203
+ const scope = {
204
+ id: this.scriptId,
205
+ type: this.type,
206
+ name,
207
+ fields,
208
+ content,
209
+ properties,
210
+ environment: activity.environment,
211
+ logger: activity.logger,
212
+ resolveExpression,
213
+ ...rest
214
+ };
215
+ const script = environment.scripts.getScript(definition.scriptFormat, {
216
+ id: this.id
217
+ });
218
+ return new Promise((resolve, reject) => {
219
+ return script.execute(scope, (err, result) => {
220
+ if (err) return reject(err);
221
+ resolve({
222
+ [name]: result
223
+ });
224
+ });
225
+ });
226
+
227
+ function resolveExpression(expression) {
228
+ return environment.resolveExpression(expression, scope);
229
+ }
230
+ }
231
+
232
+ }
233
+
234
+ exports.IOScript = IOScript;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _IOProperties = _interopRequireDefault(require("./IOProperties.js"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ class IOForm {
13
+ constructor(activity, behaviour) {
14
+ this.activity = activity;
15
+ this.behaviour = behaviour;
16
+ }
17
+
18
+ resolve(elementApi) {
19
+ const form = {};
20
+
21
+ for (const field of this.behaviour.fields) {
22
+ const f = form[field.id] = { ...field,
23
+ ...(field.label && {
24
+ defaultValue: elementApi.resolveExpression(field.label)
25
+ }),
26
+ ...(field.defaultValue && {
27
+ defaultValue: elementApi.resolveExpression(field.defaultValue)
28
+ })
29
+ };
30
+
31
+ if (f.properties) {
32
+ f.properties = new _IOProperties.default(this.activity, f.properties).resolve(elementApi);
33
+ }
34
+ }
35
+
36
+ return form;
37
+ }
38
+
39
+ }
40
+
41
+ exports.default = IOForm;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ class IOProperties {
9
+ constructor(activity, behaviour) {
10
+ this.activity = activity;
11
+ this.behaviour = behaviour;
12
+ }
13
+
14
+ resolve(elementApi) {
15
+ const properties = {};
16
+
17
+ for (const {
18
+ id,
19
+ name,
20
+ value
21
+ } of this.behaviour.values) {
22
+ properties[id || name] = elementApi.resolveExpression(value);
23
+ }
24
+
25
+ return properties;
26
+ }
27
+
28
+ }
29
+
30
+ exports.default = IOProperties;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = ServiceExpression;
7
+
8
+ function ServiceExpression(activity) {
9
+ if (!(this instanceof ServiceExpression)) return new ServiceExpression(activity);
10
+ this.activity = activity;
11
+ this.type = `${activity.type}:expression`;
12
+ this.expression = activity.behaviour.expression;
13
+ }
14
+
15
+ ServiceExpression.prototype.execute = function execute(executionMessage, callback) {
16
+ const serviceFn = this.activity.environment.resolveExpression(this.expression, executionMessage);
17
+ serviceFn.call(this.activity, executionMessage, (err, result) => {
18
+ callback(err, result);
19
+ });
20
+ };