@onify/flow-extensions 8.2.2 → 8.3.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.
@@ -3,12 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.FlowScriptError = exports.FlowResourceError = void 0;
6
7
  exports.FlowScripts = FlowScripts;
8
+ exports.FlowSyntaxError = void 0;
7
9
  exports.JavaScript = JavaScript;
8
10
  exports.JavaScriptResource = JavaScriptResource;
9
- var _path = require("path");
10
- var _fs = require("fs");
11
- var _vm = require("vm");
11
+ var _nodePath = require("node:path");
12
+ var _nodeFs = require("node:fs");
13
+ var _nodeVm = require("node:vm");
12
14
  const kSyntaxError = Symbol.for('syntax error');
13
15
  const kResources = Symbol.for('resources base');
14
16
  class FlowScriptError extends Error {
@@ -28,9 +30,10 @@ class FlowScriptError extends Error {
28
30
  });
29
31
  }
30
32
  toString() {
31
- return '[FlowScriptError] ' + this.message + '\n' + this.stack;
33
+ return '[' + this.name + '] ' + this.message + '\n' + this.stack;
32
34
  }
33
35
  }
36
+ exports.FlowScriptError = FlowScriptError;
34
37
  class FlowSyntaxError extends Error {
35
38
  constructor(fromErr) {
36
39
  super(fromErr.message);
@@ -48,14 +51,23 @@ class FlowSyntaxError extends Error {
48
51
  });
49
52
  }
50
53
  toString() {
51
- return '[FlowSyntaxError] ' + this.message + '\n' + this.stack;
54
+ return '[' + this.name + '] ' + this.message + '\n' + this.stack;
52
55
  }
53
56
  }
57
+ exports.FlowSyntaxError = FlowSyntaxError;
58
+ class FlowResourceError extends FlowScriptError {
59
+ constructor(fromErr, filename) {
60
+ super(fromErr);
61
+ this.filename = filename;
62
+ this.inner = fromErr;
63
+ }
64
+ }
65
+ exports.FlowResourceError = FlowResourceError;
54
66
  function FlowScripts(flowName, resourceBase, runContext, timeout = 60000) {
55
- this._name = flowName;
56
- this._scripts = {};
57
- this._timeout = timeout;
58
- this._runContext = runContext;
67
+ this.flowName = flowName;
68
+ this.scripts = new Map();
69
+ this.timeout = timeout;
70
+ this.runContext = runContext;
59
71
  this[kResources] = resourceBase;
60
72
  }
61
73
  FlowScripts.prototype.register = function register({
@@ -83,31 +95,40 @@ FlowScripts.prototype.register = function register({
83
95
  if (!language) return;
84
96
  if (!['js', 'javascript'].includes(language.toLowerCase().trim())) return;
85
97
  language = 'javascript';
86
- const name = this._name;
87
- const filename = `${name}/${type}/${id}`;
98
+ const flowName = this.flowName;
99
+ const filename = `${flowName}/${type}/${id}`;
88
100
  if (scriptBody) {
89
- this._scripts[id] = new JavaScript(name, scriptBody, this._runContext, {
101
+ this.scripts.set(id, new JavaScript(flowName, scriptBody, this.runContext, {
90
102
  filename,
91
- timeout: this._timeout
92
- });
103
+ timeout: this.timeout
104
+ }));
93
105
  } else if (resource) {
94
- this._scripts[id] = new JavaScriptResource(name, resource, this[kResources], this._runContext, {
106
+ this.scripts.set(id, new JavaScriptResource(flowName, resource, this[kResources], this.runContext, {
95
107
  filename,
96
- timeout: this._timeout
97
- });
108
+ timeout: this.timeout
109
+ }));
98
110
  }
99
111
  };
100
112
  FlowScripts.prototype.getScript = function getScript(scriptType, {
101
113
  id
102
114
  }) {
103
- return this._scripts[id];
115
+ return this.scripts.get(id);
104
116
  };
117
+
118
+ /**
119
+ * Java script
120
+ * @param {string} flowName
121
+ * @param {string|Buffer} scriptBody
122
+ * @param {any} [runContext]
123
+ * @param {import('node:vm').ScriptOptions} options
124
+ */
105
125
  function JavaScript(flowName, scriptBody, runContext, options) {
106
126
  this.flowName = flowName;
107
- this._runContext = runContext;
127
+ this.options = options;
128
+ this.runContext = runContext;
108
129
  this.timeout = options?.timeout;
109
130
  try {
110
- this.script = new _vm.Script(scriptBody, options);
131
+ this.script = new _nodeVm.Script(scriptBody, options);
111
132
  } catch (err) {
112
133
  this[kSyntaxError] = new FlowSyntaxError(err);
113
134
  }
@@ -127,7 +148,7 @@ JavaScript.prototype.execute = async function execute(executionContext, callback
127
148
  from: Buffer.from
128
149
  },
129
150
  contextName: this.flowName,
130
- ...this._runContext,
151
+ ...this.runContext,
131
152
  next
132
153
  }, {
133
154
  timeout: this.timeout
@@ -145,25 +166,37 @@ JavaScript.prototype.execute = async function execute(executionContext, callback
145
166
  function JavaScriptResource(flowName, resource, resourceBase, runContext, options) {
146
167
  this.flowName = flowName;
147
168
  this.resource = resource;
169
+ this.runContext = runContext;
148
170
  this.options = options;
149
- this._runContext = runContext;
150
- this[kResources] = resourceBase;
171
+ this.timeout = options?.timeout;
172
+ this.resourceBase = resourceBase;
151
173
  }
174
+
175
+ /**
176
+ * Get javascript resource content
177
+ * @param {string} resourceBase Resource base
178
+ * @param {*} resource Resource name or path
179
+ * @returns {Promise<string|Buffer} Resource content
180
+ */
181
+ JavaScriptResource.prototype.getResourceContent = function getResourceContent(resourceBase, resource) {
182
+ return _nodeFs.promises.readFile((0, _nodePath.join)(resourceBase, resource));
183
+ };
152
184
  JavaScriptResource.prototype.execute = async function execute(executionContext, callback) {
153
185
  let resource;
154
186
  try {
155
187
  resource = executionContext.resolveExpression(this.resource);
156
- var scriptBody = await _fs.promises.readFile((0, _path.join)(this[kResources], resource)); // eslint-disable-line no-var
188
+ var scriptBody = await this.getResourceContent(this.resourceBase, resource); // eslint-disable-line no-var
189
+ if (!scriptBody) throw new TypeError(`${this.options.filename}: script resource ${resource || this.resource} is empty`);
157
190
  } catch (err) {
158
191
  if (err instanceof SyntaxError) {
159
192
  return callback(err);
160
193
  }
161
- const {
162
- filename
163
- } = this.options;
164
- return callback(new Error(`${filename}: script resource ${resource || this.resource} not found`));
194
+ return callback(new FlowResourceError(err, this.options.filename));
195
+ }
196
+ if (!scriptBody) {
197
+ throw new FlowResourceError();
165
198
  }
166
- const script = new JavaScript(this.flowName, scriptBody, this._runContext, {
199
+ const script = new JavaScript(this.flowName, scriptBody, this.runContext, {
167
200
  ...this.options,
168
201
  filename: `${this.options.filename}/${resource}`
169
202
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onify/flow-extensions",
3
- "version": "8.2.2",
3
+ "version": "8.3.1",
4
4
  "description": "Onify Flow extensions",
5
5
  "type": "module",
6
6
  "module": "src/index.js",
@@ -50,9 +50,9 @@
50
50
  "@babel/core": "^7.23.9",
51
51
  "@babel/preset-env": "^7.23.9",
52
52
  "@babel/register": "^7.23.7",
53
- "bpmn-elements": "^15.0.3",
53
+ "bpmn-elements": "^16.2.0",
54
54
  "bpmn-elements-8-1": "npm:bpmn-elements@8.1",
55
- "bpmn-engine": "^21.1.0",
55
+ "bpmn-engine": "^23.0.1",
56
56
  "bpmn-engine-14": "npm:bpmn-engine@14",
57
57
  "bpmn-moddle": "^9.0.1",
58
58
  "c8": "^10.1.2",
@@ -66,13 +66,13 @@
66
66
  "moddle-context-serializer": "^4.1.1",
67
67
  "nock": "^13.5.4",
68
68
  "prettier": "^3.2.4",
69
- "texample": "^0.0.5"
69
+ "texample": "^0.0.6"
70
70
  },
71
71
  "peerDependencies": {
72
+ "@0dep/piso": ">=2",
72
73
  "bpmn-elements": ">=14"
73
74
  },
74
75
  "dependencies": {
75
- "@0dep/piso": "^2.0.0",
76
76
  "cron-parser": "^4.9.0"
77
77
  },
78
78
  "files": [
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare module '@onify/flow-extensions' {
2
- import { SequenceFlow, TimerEventDefinition, ElementBase, IExtension, ContextInstance } from 'bpmn-elements';
2
+ import { SequenceFlow, TimerEventDefinition, ElementBase, IExtension, ContextInstance, SequenceFlow } from 'bpmn-elements';
3
3
  import { extendFn as extendFunction } from 'moddle-context-serializer';
4
4
 
5
5
  export class OnifySequenceFlow extends SequenceFlow {}
@@ -11,6 +11,48 @@ declare module '@onify/flow-extensions' {
11
11
  }
12
12
 
13
13
  declare module '@onify/flow-extensions/FlowScripts' {
14
- import { IScripts } from 'bpmn-elements';
15
- export var FlowScripts: IScripts;
14
+ import { IScripts, Script, ExecutionScope } from 'bpmn-elements';
15
+ import { SerializableElement } from 'moddle-context-serializer';
16
+ import { ScriptOptions } from 'node:vm';
17
+
18
+ declare type registerArgument = SerializableElement | { id: string; type: string; behavior: any };
19
+
20
+ export interface FlowScriptOptions extends ScriptOptions {
21
+ timeout?: number;
22
+ }
23
+
24
+ export class FlowScripts implements IScripts {
25
+ /**
26
+ * @param flowName Flow name
27
+ * @param resourceBase External resource base
28
+ * @param runContext Optional script globals
29
+ * @param timeout Optional execution timeout in milliseconds, default 60000
30
+ */
31
+ constructor(flowName: string, resourceBase: string, runContext?: any, timeout?: number);
32
+ flowName: string;
33
+ timeout: number;
34
+ /** Registered scripts */
35
+ get scripts(): Map<string, Script | JavaScriptResource>;
36
+ /** Registered scripts */
37
+ register(element: registerArgument): Script | undefined;
38
+ getScript(language: string, identifier: { id: string; [x: string]: any }): Script;
39
+ }
40
+
41
+ export class JavaScript implements Script {
42
+ constructor(flowName: string, scriptBody: string | Buffer, runContext: any, options?: FlowScriptOptions);
43
+ get flowName(): string;
44
+ get runContext(): unknown;
45
+ get options(): FlowScriptOptions;
46
+ execute(executionContext: ExecutionScope, callback: CallableFunction): void;
47
+ }
48
+
49
+ export class JavaScriptResource extends JavaScript {
50
+ constructor(flowName: string, resource: string, resourceBase: string, runContext?: unknown, options?: FlowScriptOptions);
51
+ /**
52
+ * Get javascript resource content
53
+ * @param resourceBase Resource base
54
+ * @param resource Resolved resource name or path
55
+ */
56
+ getResourceContent(resourceBase: string, resource: string): Promise<string | Buffer>;
57
+ }
16
58
  }