@onify/flow-extensions 3.0.0 → 4.1.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/CHANGELOG.md +11 -0
- package/README.md +89 -0
- package/dist/index.js +15 -415
- package/dist/src/Connector.js +1 -1
- package/dist/src/Errors.js +12 -2
- package/dist/src/ExecutionListeners.js +11 -5
- package/dist/src/FlowScripts.js +1 -1
- package/dist/src/IO.js +75 -76
- package/dist/src/OnifyElementExtensions.js +224 -0
- package/dist/src/OnifyProcessExtensions.js +51 -0
- package/dist/src/OnifySequenceFlow.js +52 -0
- package/dist/src/ServiceExpression.js +1 -1
- package/dist/src/formatters.js +112 -0
- package/dist/src/getExtensions.js +62 -0
- package/index.js +8 -333
- package/package.json +13 -13
- package/src/Connector.js +1 -1
- package/src/Errors.js +8 -4
- package/src/ExecutionListeners.js +17 -11
- package/src/IO.js +66 -74
- package/src/OnifyElementExtensions.js +150 -0
- package/src/OnifyProcessExtensions.js +40 -0
- package/src/OnifySequenceFlow.js +45 -0
- package/src/ServiceExpression.js +1 -1
- package/src/formatters.js +97 -0
- package/src/getExtensions.js +57 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import {getExtensions} from './getExtensions.js';
|
|
2
|
+
|
|
3
|
+
export class OnifyElementExtensions {
|
|
4
|
+
constructor(activity, context) {
|
|
5
|
+
this.activity = activity;
|
|
6
|
+
this.context = context;
|
|
7
|
+
const {Service} = this.extensions = getExtensions(activity, context);
|
|
8
|
+
if (Service) {
|
|
9
|
+
activity.behaviour.Service = Service;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
activate(message) {
|
|
13
|
+
const activity = this.activity;
|
|
14
|
+
const broker = activity.broker;
|
|
15
|
+
const formatQ = activity.broker.getQueue('format-run-q');
|
|
16
|
+
const executionListeners = this.extensions.listeners;
|
|
17
|
+
|
|
18
|
+
if (message.fields.redelivered && message.fields.routingKey === 'run.start') {
|
|
19
|
+
activity.on('start', (elementApi) => {
|
|
20
|
+
this._formatOnEnter(broker, formatQ, elementApi);
|
|
21
|
+
}, {consumerTag: '_onify-extension-on-enter'});
|
|
22
|
+
} else {
|
|
23
|
+
activity.on('enter', (elementApi) => {
|
|
24
|
+
this._formatOnEnter(broker, formatQ, elementApi);
|
|
25
|
+
}, {consumerTag: '_onify-extension-on-enter'});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
activity.on('activity.execution.completed', async (elementApi) => {
|
|
29
|
+
if (activity.isSubProcess && activity.id !== elementApi.id) return;
|
|
30
|
+
|
|
31
|
+
formatQ.queueMessage({routingKey: 'run.end.format'}, {endRoutingKey: 'run.end.complete'}, {persistent: false});
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
var format = await this._onExecuted(elementApi);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return broker.publish('format', 'run.end.error', {error: err}, {persistent: false});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
broker.publish('format', 'run.end.complete', {...format}, {persistent: false});
|
|
40
|
+
}, {consumerTag: '_onify-extension-on-executed'});
|
|
41
|
+
|
|
42
|
+
if (executionListeners?.onStart) {
|
|
43
|
+
activity.on('start', async (elementApi) => {
|
|
44
|
+
if (activity.isSubProcess && activity.id !== elementApi.id) return;
|
|
45
|
+
|
|
46
|
+
formatQ.queueMessage({routingKey: 'run.listener.start'}, {endRoutingKey: 'run.listener.start.complete'}, {persistent: false});
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
var format = await executionListeners.execute('start', elementApi);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
return broker.publish('format', 'run.listener.start.error', {error: err}, {persistent: false});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
broker.publish('format', 'run.listener.start.complete', {...format}, {persistent: false});
|
|
55
|
+
}, {consumerTag: '_onify-extension-on-listenerstart'});
|
|
56
|
+
}
|
|
57
|
+
if (executionListeners?.onEnd) {
|
|
58
|
+
activity.on('end', async (elementApi) => {
|
|
59
|
+
if (activity.isSubProcess && activity.id !== elementApi.id) return;
|
|
60
|
+
|
|
61
|
+
formatQ.queueMessage({routingKey: 'run.listener.end'}, {endRoutingKey: 'run.listener.end.complete'}, {persistent: false});
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
var format = await executionListeners.execute('end', elementApi);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return broker.publish('format', 'run.listener.end.error', {error: err}, {persistent: false});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
broker.publish('format', 'run.listener.end.complete', {...format}, {persistent: false});
|
|
70
|
+
}, {consumerTag: '_onify-extension-on-listenerend'});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
deactivate() {
|
|
74
|
+
const broker = this.activity.broker;
|
|
75
|
+
broker.cancel('_onify-extension-on-enter');
|
|
76
|
+
broker.cancel('_onify-extension-on-executed');
|
|
77
|
+
broker.cancel('_onify-extension-on-listenerstart');
|
|
78
|
+
broker.cancel('_onify-extension-on-listenerend');
|
|
79
|
+
}
|
|
80
|
+
async _formatOnEnter(broker, formatQ, elementApi) {
|
|
81
|
+
if (this.activity.isSubProcess && this.activity.id !== elementApi.id) return;
|
|
82
|
+
|
|
83
|
+
formatQ.queueMessage({routingKey: 'run.enter.format'}, {endRoutingKey: 'run.enter.complete'}, {persistent: false});
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
var format = await this._onEnter(elementApi);
|
|
87
|
+
} catch (err) {
|
|
88
|
+
return broker.publish('format', 'run.enter.error', {error: err}, {persistent: false});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
broker.publish('format', 'run.enter.complete', format, {persistent: false});
|
|
92
|
+
}
|
|
93
|
+
async _onEnter(elementApi) {
|
|
94
|
+
const {format, properties, io, form} = this.extensions;
|
|
95
|
+
|
|
96
|
+
const result = {};
|
|
97
|
+
Object.assign(result, format.resolve(elementApi));
|
|
98
|
+
Object.assign(elementApi.content, result);
|
|
99
|
+
|
|
100
|
+
if (properties) {
|
|
101
|
+
Object.assign(result, {properties: properties.resolve(elementApi)});
|
|
102
|
+
Object.assign(elementApi.content, result);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (io) {
|
|
106
|
+
if (io.input?.length) {
|
|
107
|
+
const input = await io.getInput(this.activity, elementApi);
|
|
108
|
+
Object.assign(result, {input});
|
|
109
|
+
Object.assign(elementApi.content, result);
|
|
110
|
+
}
|
|
111
|
+
if (io.output?.length) {
|
|
112
|
+
result.assignToOutput = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (form) {
|
|
117
|
+
Object.assign(result, {form: form.resolve(elementApi)});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
async _onExecuted(elementApi) {
|
|
123
|
+
const {properties, io} = this.extensions;
|
|
124
|
+
|
|
125
|
+
const result = {};
|
|
126
|
+
if (io?.output.length) {
|
|
127
|
+
const output = await io.getOutput(this.activity, elementApi);
|
|
128
|
+
Object.assign(result, {output});
|
|
129
|
+
Object.assign(elementApi.content, {output});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (properties) {
|
|
133
|
+
const props = properties.resolve(elementApi);
|
|
134
|
+
Object.assign(result, {properties: props});
|
|
135
|
+
Object.assign(elementApi.content, {properties: props});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const {output, assignToOutput, resultVariable} = elementApi.content;
|
|
139
|
+
|
|
140
|
+
if (output !== undefined && output !== null) {
|
|
141
|
+
if (assignToOutput && typeof output === 'object') {
|
|
142
|
+
Object.assign(this.activity.environment.output, output);
|
|
143
|
+
} else if (resultVariable) {
|
|
144
|
+
elementApi.environment.output[resultVariable] = output;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {FormatError} from './Errors.js';
|
|
2
|
+
import {getExtensions} from './getExtensions.js';
|
|
3
|
+
|
|
4
|
+
export class OnifyProcessExtensions {
|
|
5
|
+
constructor(bp, context) {
|
|
6
|
+
this.process = bp;
|
|
7
|
+
this.context = context;
|
|
8
|
+
this.extensions = getExtensions(bp, context);
|
|
9
|
+
this._activate();
|
|
10
|
+
}
|
|
11
|
+
_activate() {
|
|
12
|
+
const bp = this.process;
|
|
13
|
+
bp.on('process.enter', (elementApi) => {
|
|
14
|
+
try {
|
|
15
|
+
const result = this._onEnter(elementApi);
|
|
16
|
+
const environment = elementApi.environment;
|
|
17
|
+
environment.assignVariables(result);
|
|
18
|
+
} catch (err) {
|
|
19
|
+
elementApi.broker.publish('event', 'process.error', {
|
|
20
|
+
...elementApi.content,
|
|
21
|
+
error: new FormatError(bp.id, err),
|
|
22
|
+
}, {mandatory: true, type: 'error'});
|
|
23
|
+
}
|
|
24
|
+
}, {consumerTag: '_onify-extension-on-enter'});
|
|
25
|
+
}
|
|
26
|
+
_onEnter(elementApi) {
|
|
27
|
+
const {format, properties} = this.extensions;
|
|
28
|
+
const result = {};
|
|
29
|
+
|
|
30
|
+
Object.assign(result, format.resolve(elementApi));
|
|
31
|
+
Object.assign(elementApi.content, result);
|
|
32
|
+
|
|
33
|
+
if (properties) {
|
|
34
|
+
Object.assign(result, properties.resolve(elementApi));
|
|
35
|
+
Object.assign(elementApi.content, result);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {SequenceFlow} from 'bpmn-elements';
|
|
2
|
+
import {getExtensions} from './getExtensions.js';
|
|
3
|
+
|
|
4
|
+
export class OnifySequenceFlow extends SequenceFlow {
|
|
5
|
+
constructor(flowDef, context) {
|
|
6
|
+
super(flowDef, context);
|
|
7
|
+
this.extensions = getExtensions(this, context);
|
|
8
|
+
this._activate();
|
|
9
|
+
}
|
|
10
|
+
_activate() {
|
|
11
|
+
if (!this.extensions.listeners?.onTake) return;
|
|
12
|
+
|
|
13
|
+
this.broker.subscribeTmp('event', 'flow.take', (_, msg) => {
|
|
14
|
+
this._executeListeners(msg);
|
|
15
|
+
}, {noAck: true, consumerTag: '_onify-execution-listener'});
|
|
16
|
+
}
|
|
17
|
+
async _executeListeners(message) {
|
|
18
|
+
try {
|
|
19
|
+
await this.extensions.listeners.execute('take', message);
|
|
20
|
+
} catch (err) {
|
|
21
|
+
this.logger.error(`<${this.id}> execution listener error: ${err}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
evaluate(fromMessage, callback) {
|
|
25
|
+
const properties = this.extensions.properties;
|
|
26
|
+
if (!properties) return super.evaluate(fromMessage, callback);
|
|
27
|
+
|
|
28
|
+
super.evaluate(fromMessage, (err, result) => {
|
|
29
|
+
if (err) return callback(err);
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
let overriddenResult = result ? {} : false;
|
|
33
|
+
if (result) {
|
|
34
|
+
overriddenResult = {
|
|
35
|
+
...(typeof result === 'object' && result),
|
|
36
|
+
properties: properties.resolve(this.getApi(fromMessage)),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return callback(err, overriddenResult);
|
|
40
|
+
} catch (formatErr) {
|
|
41
|
+
return callback(formatErr);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/ServiceExpression.js
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import cronParser from 'cron-parser';
|
|
2
|
+
|
|
3
|
+
const iso8601cycle = /^\s*(R\d+\/)?P\w+/i;
|
|
4
|
+
|
|
5
|
+
export class FormatActivity {
|
|
6
|
+
constructor(activity) {
|
|
7
|
+
this.activity = activity;
|
|
8
|
+
this.resultVariable = activity.behaviour.resultVariable;
|
|
9
|
+
|
|
10
|
+
let timeCycles;
|
|
11
|
+
if (activity.eventDefinitions) {
|
|
12
|
+
for (const ed of activity.eventDefinitions.filter((e) => e.type === 'bpmn:TimerEventDefinition')) {
|
|
13
|
+
if (!('timeCycle' in ed)) continue;
|
|
14
|
+
timeCycles = timeCycles || [];
|
|
15
|
+
timeCycles.push(ed.timeCycle);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
this.timeCycles = timeCycles;
|
|
19
|
+
}
|
|
20
|
+
resolve(elementApi) {
|
|
21
|
+
let user, groups, assigneeValue, description;
|
|
22
|
+
const activity = this.activity;
|
|
23
|
+
const {
|
|
24
|
+
documentation,
|
|
25
|
+
candidateUsers,
|
|
26
|
+
candidateGroups,
|
|
27
|
+
scheduledStart,
|
|
28
|
+
assignee,
|
|
29
|
+
} = activity.behaviour;
|
|
30
|
+
|
|
31
|
+
if (candidateUsers) user = resolveAndSplit(elementApi, candidateUsers);
|
|
32
|
+
if (candidateGroups) groups = resolveAndSplit(elementApi, candidateGroups);
|
|
33
|
+
if (assignee) assigneeValue = elementApi.resolveExpression(assignee);
|
|
34
|
+
if (documentation) description = documentation[0]?.text;
|
|
35
|
+
|
|
36
|
+
let expireAt;
|
|
37
|
+
const timeCycles = this.timeCycles;
|
|
38
|
+
if (timeCycles) {
|
|
39
|
+
for (const cycle of timeCycles) {
|
|
40
|
+
const cron = elementApi.resolveExpression(cycle);
|
|
41
|
+
if (!cron || iso8601cycle.test(cron)) continue;
|
|
42
|
+
|
|
43
|
+
const expireAtDt = cronParser.parseExpression(cron).next().toDate();
|
|
44
|
+
if (!expireAt || expireAtDt < expireAt) expireAt = expireAtDt;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
...(this.resultVariable && {resultVariable: this.resultVariable}),
|
|
50
|
+
...(scheduledStart && activity.parent.type === 'bpmn:Process' && {scheduledStart}),
|
|
51
|
+
...(user?.length && {candidateUsers: user}),
|
|
52
|
+
...(groups?.length && {candidateGroups: groups}),
|
|
53
|
+
...(!elementApi.content.description && description && {description: elementApi.resolveExpression(description)}),
|
|
54
|
+
...(expireAt && {expireAt}),
|
|
55
|
+
...(assigneeValue && {assignee: assigneeValue}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class FormatProcess {
|
|
61
|
+
constructor(bp) {
|
|
62
|
+
this.process = bp;
|
|
63
|
+
}
|
|
64
|
+
resolve(elementApi) {
|
|
65
|
+
let user, groups, description;
|
|
66
|
+
const bp = this.process;
|
|
67
|
+
const {
|
|
68
|
+
documentation,
|
|
69
|
+
candidateStarterUsers,
|
|
70
|
+
candidateStarterGroups,
|
|
71
|
+
} = bp.behaviour;
|
|
72
|
+
|
|
73
|
+
if (candidateStarterUsers) user = resolveAndSplit(elementApi, candidateStarterUsers);
|
|
74
|
+
if (candidateStarterGroups) groups = resolveAndSplit(elementApi, candidateStarterGroups);
|
|
75
|
+
if (documentation) description = documentation[0]?.text;
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
...(user?.length && {candidateStarterUsers: user}),
|
|
79
|
+
...(groups?.length && {candidateStarterGroups: groups}),
|
|
80
|
+
...(!elementApi.content.description && description && {description: elementApi.resolveExpression(description)}),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function resolveAndSplit(elementApi, str) {
|
|
86
|
+
if (Array.isArray(str)) return str.filter(Boolean);
|
|
87
|
+
if (typeof str !== 'string') return;
|
|
88
|
+
|
|
89
|
+
const resolved = elementApi.resolveExpression(str);
|
|
90
|
+
if (Array.isArray(resolved)) return resolved.filter(Boolean);
|
|
91
|
+
if (typeof resolved !== 'string') return;
|
|
92
|
+
|
|
93
|
+
return resolved
|
|
94
|
+
.split(',')
|
|
95
|
+
.map((g) => g.trim && g.trim().toLowerCase())
|
|
96
|
+
.filter(Boolean);
|
|
97
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {FormatActivity, FormatProcess} from './formatters.js';
|
|
2
|
+
import {InputOutput} from './IO.js';
|
|
3
|
+
import Connector from './Connector.js';
|
|
4
|
+
import ExecutionListeners from './ExecutionListeners.js';
|
|
5
|
+
import IOForm from './IOForm.js';
|
|
6
|
+
import IOProperties from './IOProperties.js';
|
|
7
|
+
import ServiceExpression from './ServiceExpression.js';
|
|
8
|
+
|
|
9
|
+
export function getExtensions(element, context) {
|
|
10
|
+
const result = {};
|
|
11
|
+
|
|
12
|
+
switch (element.type) {
|
|
13
|
+
case 'bpmn:SequenceFlow':
|
|
14
|
+
break;
|
|
15
|
+
case 'bpmn:Process':
|
|
16
|
+
result.format = new FormatProcess(element);
|
|
17
|
+
break;
|
|
18
|
+
default:
|
|
19
|
+
result.format = new FormatActivity(element);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let listenerPosition = 0;
|
|
23
|
+
const listeners = new ExecutionListeners(element, context);
|
|
24
|
+
|
|
25
|
+
const expression = element.behaviour.expression;
|
|
26
|
+
if (expression) result.Service = ServiceExpression;
|
|
27
|
+
|
|
28
|
+
const extensions = element.behaviour.extensionElements?.values;
|
|
29
|
+
if (extensions) {
|
|
30
|
+
for (const ext of extensions) {
|
|
31
|
+
switch (ext.$type) {
|
|
32
|
+
case 'camunda:Properties':
|
|
33
|
+
if (ext.values?.length) result.properties = new IOProperties(element, ext);
|
|
34
|
+
break;
|
|
35
|
+
case 'camunda:InputOutput':
|
|
36
|
+
result.io = new InputOutput(element.id, ext, context);
|
|
37
|
+
break;
|
|
38
|
+
case 'camunda:FormData':
|
|
39
|
+
if (ext.fields?.length) result.form = new IOForm(element, ext);
|
|
40
|
+
break;
|
|
41
|
+
case 'camunda:Connector': {
|
|
42
|
+
const {connectorId, inputOutput} = ext;
|
|
43
|
+
const io = inputOutput && new InputOutput(`${element.id}/${ext.$type.toLowerCase()}`, inputOutput, context);
|
|
44
|
+
result.Service = Connector.bind(Connector, connectorId, io);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case 'camunda:ExecutionListener':
|
|
48
|
+
listeners.add(ext, listenerPosition++);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (listeners.length) result.listeners = listeners;
|
|
55
|
+
|
|
56
|
+
return result;
|
|
57
|
+
}
|