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