@onify/flow-extensions 2.0.0 → 4.0.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.
package/dist/src/IO.js CHANGED
@@ -3,80 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.InputOutput = exports.IOScript = exports.IOMap = exports.IOList = exports.IOBase = void 0;
7
- class InputOutput {
8
- constructor(parentId, behaviour, context) {
9
- this.parentId = parentId;
10
- this.context = context;
11
- const {
12
- inputParameters,
13
- outputParameters
14
- } = behaviour;
15
- this.input = this._map(parentId, inputParameters, 'input', context);
16
- this.output = this._map(parentId, outputParameters, 'output', context);
17
- }
18
- async getInput(activity, executionMessage) {
19
- const input = this.input;
20
- const values = await Promise.all(input.map(parm => parm.getValue(activity, executionMessage)));
21
- return values.reduce((result, parm) => Object.assign(result, parm), {});
22
- }
23
- async getOutput(activity, executionMessage) {
24
- const output = this.output;
25
- const values = await Promise.all(output.map(parm => parm.getValue(activity, executionMessage)));
26
- return values.reduce((result, parm) => Object.assign(result, parm), {});
27
- }
28
- _map(parentId, list, ioType, context) {
29
- const mapped = [];
30
- if (!list) return mapped;
31
- for (const parm of list) {
32
- const definition = parm.definition;
33
- const type = definition && definition.$type;
34
- switch (type) {
35
- case 'camunda:Map':
36
- {
37
- mapped.push(new IOMap(parm));
38
- break;
39
- }
40
- case 'camunda:List':
41
- {
42
- mapped.push(new IOList(parm));
43
- break;
44
- }
45
- case 'camunda:Script':
46
- {
47
- const id = `${parentId}/${ioType}/${type}/${parm.name}`;
48
- const {
49
- scriptFormat,
50
- value,
51
- resource
52
- } = definition;
53
- if (!value && !resource) break;
54
- context.environment.scripts.register({
55
- id,
56
- type: parm.$type,
57
- behaviour: {
58
- scriptFormat,
59
- ...(value && {
60
- script: value
61
- }),
62
- ...(resource && {
63
- resource
64
- })
65
- }
66
- });
67
- mapped.push(new IOScript(parm, id));
68
- break;
69
- }
70
- default:
71
- {
72
- mapped.push(new IOBase(parm));
73
- }
74
- }
75
- }
76
- return mapped;
77
- }
78
- }
79
- exports.InputOutput = InputOutput;
6
+ exports.InputOutput = exports.IOScript = exports.IOList = exports.IOBase = void 0;
80
7
  class IOBase {
81
8
  constructor(parm) {
82
9
  this.name = parm.name;
@@ -127,7 +54,6 @@ class IOMap extends IOBase {
127
54
  };
128
55
  }
129
56
  }
130
- exports.IOMap = IOMap;
131
57
  class IOList extends IOBase {
132
58
  constructor(parm) {
133
59
  super(parm);
@@ -167,7 +93,7 @@ class IOScript extends IOBase {
167
93
  ...rest
168
94
  } = executionMessage;
169
95
  const scope = {
170
- id: this.scriptId,
96
+ id: this.id,
171
97
  type: this.type,
172
98
  name,
173
99
  fields,
@@ -194,4 +120,77 @@ class IOScript extends IOBase {
194
120
  }
195
121
  }
196
122
  }
197
- exports.IOScript = IOScript;
123
+ exports.IOScript = IOScript;
124
+ class InputOutput {
125
+ constructor(parentId, behaviour, context) {
126
+ this.parentId = parentId;
127
+ this.context = context;
128
+ const {
129
+ inputParameters,
130
+ outputParameters
131
+ } = behaviour;
132
+ this.input = this._map(parentId, inputParameters, 'input', context);
133
+ this.output = this._map(parentId, outputParameters, 'output', context);
134
+ }
135
+ async getInput(activity, executionMessage) {
136
+ const input = this.input;
137
+ const values = await Promise.all(input.map(parm => parm.getValue(activity, executionMessage)));
138
+ return values.reduce((result, parm) => Object.assign(result, parm), {});
139
+ }
140
+ async getOutput(activity, executionMessage) {
141
+ const output = this.output;
142
+ const values = await Promise.all(output.map(parm => parm.getValue(activity, executionMessage)));
143
+ return values.reduce((result, parm) => Object.assign(result, parm), {});
144
+ }
145
+ _map(parentId, list, ioType, context) {
146
+ const mapped = [];
147
+ if (!list) return mapped;
148
+ for (const parm of list) {
149
+ const definition = parm.definition;
150
+ const type = definition && definition.$type;
151
+ switch (type) {
152
+ case 'camunda:Map':
153
+ {
154
+ mapped.push(new IOMap(parm));
155
+ break;
156
+ }
157
+ case 'camunda:List':
158
+ {
159
+ mapped.push(new IOList(parm));
160
+ break;
161
+ }
162
+ case 'camunda:Script':
163
+ {
164
+ const id = `${parentId}/${ioType}/${type}/${parm.name}`;
165
+ const {
166
+ scriptFormat,
167
+ value,
168
+ resource
169
+ } = definition;
170
+ if (!value && !resource) break;
171
+ context.environment.scripts.register({
172
+ id,
173
+ type: parm.$type,
174
+ behaviour: {
175
+ scriptFormat,
176
+ ...(value && {
177
+ script: value
178
+ }),
179
+ ...(resource && {
180
+ resource
181
+ })
182
+ }
183
+ });
184
+ mapped.push(new IOScript(parm, id));
185
+ break;
186
+ }
187
+ default:
188
+ {
189
+ mapped.push(new IOBase(parm));
190
+ }
191
+ }
192
+ }
193
+ return mapped;
194
+ }
195
+ }
196
+ exports.InputOutput = InputOutput;
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.OnifyElementExtensions = void 0;
7
+ var _getExtensions = require("./getExtensions.js");
8
+ class OnifyElementExtensions {
9
+ constructor(activity, context) {
10
+ this.activity = activity;
11
+ this.context = context;
12
+ const {
13
+ Service
14
+ } = this.extensions = (0, _getExtensions.getExtensions)(activity, context);
15
+ if (Service) {
16
+ activity.behaviour.Service = Service;
17
+ }
18
+ }
19
+ activate(message) {
20
+ const activity = this.activity;
21
+ const broker = activity.broker;
22
+ const formatQ = activity.broker.getQueue('format-run-q');
23
+ if (message.fields.redelivered && message.fields.routingKey === 'run.start') {
24
+ activity.on('start', elementApi => {
25
+ this._formatOnEnter(broker, formatQ, elementApi);
26
+ }, {
27
+ consumerTag: '_onify-extension-on-enter'
28
+ });
29
+ } else {
30
+ activity.on('enter', elementApi => {
31
+ this._formatOnEnter(broker, formatQ, elementApi);
32
+ }, {
33
+ consumerTag: '_onify-extension-on-enter'
34
+ });
35
+ }
36
+ activity.on('activity.execution.completed', async elementApi => {
37
+ if (activity.isSubProcess && activity.id !== elementApi.id) return;
38
+ formatQ.queueMessage({
39
+ routingKey: 'run.end.format'
40
+ }, {
41
+ endRoutingKey: 'run.end.complete'
42
+ }, {
43
+ persistent: false
44
+ });
45
+ try {
46
+ var format = await this._onExecuted(elementApi);
47
+ } catch (err) {
48
+ return broker.publish('format', 'run.end.error', {
49
+ error: err
50
+ }, {
51
+ persistent: false
52
+ });
53
+ }
54
+ broker.publish('format', 'run.end.complete', {
55
+ ...format
56
+ }, {
57
+ persistent: false
58
+ });
59
+ }, {
60
+ consumerTag: '_onify-extension-on-executed'
61
+ });
62
+ const executionListeners = this.extensions.listeners;
63
+ if (executionListeners !== null && executionListeners !== void 0 && executionListeners.onStart) {
64
+ activity.on('start', async elementApi => {
65
+ if (activity.isSubProcess && activity.id !== elementApi.id) return;
66
+ formatQ.queueMessage({
67
+ routingKey: 'run.listener.start'
68
+ }, {
69
+ endRoutingKey: 'run.listener.start.complete'
70
+ }, {
71
+ persistent: false
72
+ });
73
+ try {
74
+ var format = await executionListeners.execute('start', elementApi);
75
+ } catch (err) {
76
+ return broker.publish('format', 'run.listener.start.error', {
77
+ error: err
78
+ }, {
79
+ persistent: false
80
+ });
81
+ }
82
+ broker.publish('format', 'run.listener.start.complete', {
83
+ ...format
84
+ }, {
85
+ persistent: false
86
+ });
87
+ }, {
88
+ consumerTag: '_onify-extension-on-listenerstart'
89
+ });
90
+ }
91
+ if (executionListeners !== null && executionListeners !== void 0 && executionListeners.onEnd) {
92
+ activity.on('end', async elementApi => {
93
+ if (activity.isSubProcess && activity.id !== elementApi.id) return;
94
+ formatQ.queueMessage({
95
+ routingKey: 'run.listener.end'
96
+ }, {
97
+ endRoutingKey: 'run.listener.end.complete'
98
+ }, {
99
+ persistent: false
100
+ });
101
+ try {
102
+ var format = await executionListeners.execute('end', elementApi);
103
+ } catch (err) {
104
+ return broker.publish('format', 'run.listener.end.error', {
105
+ error: err
106
+ }, {
107
+ persistent: false
108
+ });
109
+ }
110
+ broker.publish('format', 'run.listener.end.complete', {
111
+ ...format
112
+ }, {
113
+ persistent: false
114
+ });
115
+ }, {
116
+ consumerTag: '_onify-extension-on-listenerend'
117
+ });
118
+ }
119
+ }
120
+ deactivate() {
121
+ const broker = this.activity.broker;
122
+ broker.cancel('_onify-extension-on-enter');
123
+ broker.cancel('_onify-extension-on-executed');
124
+ broker.cancel('_onify-extension-on-listenerstart');
125
+ broker.cancel('_onify-extension-on-listenerend');
126
+ }
127
+ async _formatOnEnter(broker, formatQ, elementApi) {
128
+ if (this.activity.isSubProcess && this.activity.id !== elementApi.id) return;
129
+ formatQ.queueMessage({
130
+ routingKey: 'run.enter.format'
131
+ }, {
132
+ endRoutingKey: 'run.enter.complete'
133
+ }, {
134
+ persistent: false
135
+ });
136
+ try {
137
+ var format = await this._onEnter(elementApi);
138
+ } catch (err) {
139
+ return broker.publish('format', 'run.enter.error', {
140
+ error: err
141
+ }, {
142
+ persistent: false
143
+ });
144
+ }
145
+ broker.publish('format', 'run.enter.complete', format, {
146
+ persistent: false
147
+ });
148
+ }
149
+ async _onEnter(elementApi) {
150
+ const {
151
+ format,
152
+ properties,
153
+ io,
154
+ form
155
+ } = this.extensions;
156
+ const result = {};
157
+ Object.assign(result, format.resolve(elementApi));
158
+ Object.assign(elementApi.content, result);
159
+ if (properties) {
160
+ Object.assign(result, {
161
+ properties: properties.resolve(elementApi)
162
+ });
163
+ Object.assign(elementApi.content, result);
164
+ }
165
+ if (io) {
166
+ var _io$input, _io$output;
167
+ if ((_io$input = io.input) !== null && _io$input !== void 0 && _io$input.length) {
168
+ const input = await io.getInput(this.activity, elementApi);
169
+ Object.assign(result, {
170
+ input
171
+ });
172
+ Object.assign(elementApi.content, result);
173
+ }
174
+ if ((_io$output = io.output) !== null && _io$output !== void 0 && _io$output.length) {
175
+ result.assignToOutput = true;
176
+ }
177
+ }
178
+ if (form) {
179
+ Object.assign(result, {
180
+ form: form.resolve(elementApi)
181
+ });
182
+ }
183
+ return result;
184
+ }
185
+ async _onExecuted(elementApi) {
186
+ const {
187
+ properties,
188
+ io
189
+ } = this.extensions;
190
+ const result = {};
191
+ if (io !== null && io !== void 0 && io.output.length) {
192
+ const output = await io.getOutput(this.activity, elementApi);
193
+ Object.assign(result, {
194
+ output
195
+ });
196
+ Object.assign(elementApi.content, {
197
+ output
198
+ });
199
+ }
200
+ if (properties) {
201
+ const props = properties.resolve(elementApi);
202
+ Object.assign(result, {
203
+ properties: props
204
+ });
205
+ Object.assign(elementApi.content, {
206
+ properties: props
207
+ });
208
+ }
209
+ const {
210
+ output,
211
+ assignToOutput,
212
+ resultVariable
213
+ } = elementApi.content;
214
+ if (output === undefined || output === null) return result;
215
+ if (assignToOutput && typeof output === 'object') {
216
+ Object.assign(this.activity.environment.output, output);
217
+ } else if (resultVariable) {
218
+ elementApi.environment.output[resultVariable] = output;
219
+ }
220
+ return result;
221
+ }
222
+ }
223
+ exports.OnifyElementExtensions = OnifyElementExtensions;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.OnifyProcessExtensions = void 0;
7
+ var _Errors = require("./Errors.js");
8
+ var _getExtensions = require("./getExtensions.js");
9
+ class OnifyProcessExtensions {
10
+ constructor(bp, context) {
11
+ this.process = bp;
12
+ this.context = context;
13
+ this.extensions = (0, _getExtensions.getExtensions)(bp, context);
14
+ this._activate();
15
+ }
16
+ _activate() {
17
+ const bp = this.process;
18
+ bp.on('process.enter', elementApi => {
19
+ try {
20
+ const result = this._onEnter(elementApi);
21
+ const environment = elementApi.environment;
22
+ environment.assignVariables(result);
23
+ } catch (err) {
24
+ elementApi.broker.publish('event', 'process.error', {
25
+ ...elementApi.content,
26
+ error: new _Errors.FormatError(bp.id, err)
27
+ }, {
28
+ mandatory: true,
29
+ type: 'error'
30
+ });
31
+ }
32
+ }, {
33
+ consumerTag: '_onify-extension-on-enter'
34
+ });
35
+ }
36
+ _onEnter(elementApi) {
37
+ const {
38
+ format,
39
+ properties
40
+ } = this.extensions;
41
+ const result = {};
42
+ Object.assign(result, format.resolve(elementApi));
43
+ Object.assign(elementApi.content, result);
44
+ if (properties) {
45
+ Object.assign(result, properties.resolve(elementApi));
46
+ Object.assign(elementApi.content, result);
47
+ }
48
+ return result;
49
+ }
50
+ }
51
+ exports.OnifyProcessExtensions = OnifyProcessExtensions;
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = ServiceExpression;
7
- var _Errors = require("./Errors");
7
+ var _Errors = require("./Errors.js");
8
8
  function ServiceExpression(activity) {
9
9
  if (!(this instanceof ServiceExpression)) return new ServiceExpression(activity);
10
10
  this.activity = activity;
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.FormatProcess = exports.FormatActivity = void 0;
7
+ var _cronParser = _interopRequireDefault(require("cron-parser"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ const iso8601cycle = /^\s*(R\d+\/)?P\w+/i;
10
+ class FormatActivity {
11
+ constructor(activity) {
12
+ this.activity = activity;
13
+ this.resultVariable = activity.behaviour.resultVariable;
14
+ let timeCycles;
15
+ if (activity.eventDefinitions) {
16
+ for (const ed of activity.eventDefinitions.filter(e => e.type === 'bpmn:TimerEventDefinition')) {
17
+ if (!('timeCycle' in ed)) continue;
18
+ timeCycles = timeCycles || [];
19
+ timeCycles.push(ed.timeCycle);
20
+ }
21
+ }
22
+ this.timeCycles = timeCycles;
23
+ }
24
+ resolve(elementApi) {
25
+ var _documentation$, _user, _groups;
26
+ let user, groups, assigneeValue, description;
27
+ const activity = this.activity;
28
+ const {
29
+ documentation,
30
+ candidateUsers,
31
+ candidateGroups,
32
+ scheduledStart,
33
+ assignee
34
+ } = activity.behaviour;
35
+ if (candidateUsers) user = resolveAndSplit(elementApi, candidateUsers);
36
+ if (candidateGroups) groups = resolveAndSplit(elementApi, candidateGroups);
37
+ if (assignee) assigneeValue = elementApi.resolveExpression(assignee);
38
+ if (documentation) description = (_documentation$ = documentation[0]) === null || _documentation$ === void 0 ? void 0 : _documentation$.text;
39
+ let expireAt;
40
+ const timeCycles = this.timeCycles;
41
+ if (timeCycles) {
42
+ for (const cycle of timeCycles) {
43
+ const cron = elementApi.resolveExpression(cycle);
44
+ if (!cron || iso8601cycle.test(cron)) continue;
45
+ const expireAtDt = _cronParser.default.parseExpression(cron).next().toDate();
46
+ if (!expireAt || expireAtDt < expireAt) expireAt = expireAtDt;
47
+ }
48
+ }
49
+ return {
50
+ ...(this.resultVariable && {
51
+ resultVariable: this.resultVariable
52
+ }),
53
+ ...(scheduledStart && activity.parent.type === 'bpmn:Process' && {
54
+ scheduledStart
55
+ }),
56
+ ...(((_user = user) === null || _user === void 0 ? void 0 : _user.length) && {
57
+ candidateUsers: user
58
+ }),
59
+ ...(((_groups = groups) === null || _groups === void 0 ? void 0 : _groups.length) && {
60
+ candidateGroups: groups
61
+ }),
62
+ ...(!elementApi.content.description && description && {
63
+ description: elementApi.resolveExpression(description)
64
+ }),
65
+ ...(expireAt && {
66
+ expireAt
67
+ }),
68
+ ...(assigneeValue && {
69
+ assignee: assigneeValue
70
+ })
71
+ };
72
+ }
73
+ }
74
+ exports.FormatActivity = FormatActivity;
75
+ class FormatProcess {
76
+ constructor(bp) {
77
+ this.process = bp;
78
+ }
79
+ resolve(elementApi) {
80
+ var _documentation$2, _user2, _groups2;
81
+ let user, groups, description;
82
+ const bp = this.process;
83
+ const {
84
+ documentation,
85
+ candidateStarterUsers,
86
+ candidateStarterGroups
87
+ } = bp.behaviour;
88
+ if (candidateStarterUsers) user = resolveAndSplit(elementApi, candidateStarterUsers);
89
+ if (candidateStarterGroups) groups = resolveAndSplit(elementApi, candidateStarterGroups);
90
+ if (documentation) description = (_documentation$2 = documentation[0]) === null || _documentation$2 === void 0 ? void 0 : _documentation$2.text;
91
+ return {
92
+ ...(((_user2 = user) === null || _user2 === void 0 ? void 0 : _user2.length) && {
93
+ candidateStarterUsers: user
94
+ }),
95
+ ...(((_groups2 = groups) === null || _groups2 === void 0 ? void 0 : _groups2.length) && {
96
+ candidateStarterGroups: groups
97
+ }),
98
+ ...(!elementApi.content.description && description && {
99
+ description: elementApi.resolveExpression(description)
100
+ })
101
+ };
102
+ }
103
+ }
104
+ exports.FormatProcess = FormatProcess;
105
+ function resolveAndSplit(elementApi, str) {
106
+ if (Array.isArray(str)) return str.filter(Boolean);
107
+ if (typeof str !== 'string') return;
108
+ const resolved = elementApi.resolveExpression(str);
109
+ if (Array.isArray(resolved)) return resolved.filter(Boolean);
110
+ if (typeof resolved !== 'string') return;
111
+ return resolved.split(',').map(g => g.trim && g.trim().toLowerCase()).filter(Boolean);
112
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getExtensions = getExtensions;
7
+ var _formatters = require("./formatters.js");
8
+ var _IO = require("./IO.js");
9
+ var _Connector = _interopRequireDefault(require("./Connector.js"));
10
+ var _ExecutionListeners = _interopRequireDefault(require("./ExecutionListeners.js"));
11
+ var _IOForm = _interopRequireDefault(require("./IOForm.js"));
12
+ var _IOProperties = _interopRequireDefault(require("./IOProperties.js"));
13
+ var _ServiceExpression = _interopRequireDefault(require("./ServiceExpression.js"));
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ function getExtensions(element, context) {
16
+ var _element$behaviour$ex, _ext$values, _ext$fields;
17
+ const result = {
18
+ format: element.type === 'bpmn:Process' ? new _formatters.FormatProcess(element) : new _formatters.FormatActivity(element)
19
+ };
20
+ const expression = element.behaviour.expression;
21
+ if (expression) result.Service = _ServiceExpression.default;
22
+ const extensions = (_element$behaviour$ex = element.behaviour.extensionElements) === null || _element$behaviour$ex === void 0 ? void 0 : _element$behaviour$ex.values;
23
+ if (!extensions) return result;
24
+ const listeners = new _ExecutionListeners.default(element, context);
25
+ let listener = 0;
26
+ for (const ext of extensions) {
27
+ switch (ext.$type) {
28
+ case 'camunda:Properties':
29
+ if ((_ext$values = ext.values) !== null && _ext$values !== void 0 && _ext$values.length) result.properties = new _IOProperties.default(element, ext);
30
+ break;
31
+ case 'camunda:InputOutput':
32
+ result.io = new _IO.InputOutput(element.id, ext, context);
33
+ break;
34
+ case 'camunda:FormData':
35
+ if ((_ext$fields = ext.fields) !== null && _ext$fields !== void 0 && _ext$fields.length) result.form = new _IOForm.default(element, ext);
36
+ break;
37
+ case 'camunda:Connector':
38
+ {
39
+ const {
40
+ connectorId,
41
+ inputOutput
42
+ } = ext;
43
+ const io = inputOutput && new _IO.InputOutput(`${element.id}/${ext.$type.toLowerCase()}`, inputOutput, context);
44
+ result.Service = _Connector.default.bind(_Connector.default, connectorId, io);
45
+ break;
46
+ }
47
+ case 'camunda:ExecutionListener':
48
+ listeners.add(ext, listener++);
49
+ break;
50
+ }
51
+ }
52
+ if (listeners.length) result.listeners = listeners;
53
+ return result;
54
+ }