bpmn-engine 25.0.0 → 26.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/lib/index.cjs +84 -4
- package/package.json +32 -16
- package/src/index.js +82 -2
- package/types/index.d.ts +153 -0
- package/types/interfaces.d.ts +80 -0
- package/types/bpmn-engine.d.ts +0 -334
package/lib/index.cjs
CHANGED
|
@@ -8,7 +8,7 @@ var node_url = require('node:url');
|
|
|
8
8
|
var BpmnModdle = require('bpmn-moddle');
|
|
9
9
|
var Elements = require('bpmn-elements');
|
|
10
10
|
var smqp = require('smqp');
|
|
11
|
-
var
|
|
11
|
+
var moddleContextSerializer = require('moddle-context-serializer');
|
|
12
12
|
var Debug = require('debug');
|
|
13
13
|
var node_vm = require('node:vm');
|
|
14
14
|
|
|
@@ -175,6 +175,10 @@ const kState = Symbol.for('state');
|
|
|
175
175
|
const kStopped = Symbol.for('stopped');
|
|
176
176
|
const kTypeResolver = Symbol.for('type resolver');
|
|
177
177
|
|
|
178
|
+
/**
|
|
179
|
+
* BPMN 2.0 execution engine.
|
|
180
|
+
* @param {import('types').BpmnEngineOptions} [options]
|
|
181
|
+
*/
|
|
178
182
|
function Engine(options) {
|
|
179
183
|
if (!(this instanceof Engine)) return new Engine(options);
|
|
180
184
|
|
|
@@ -188,7 +192,7 @@ function Engine(options) {
|
|
|
188
192
|
|
|
189
193
|
this.logger = opts.Logger('engine');
|
|
190
194
|
|
|
191
|
-
this[kTypeResolver] =
|
|
195
|
+
this[kTypeResolver] = moddleContextSerializer.TypeResolver(
|
|
192
196
|
{
|
|
193
197
|
...Elements__namespace,
|
|
194
198
|
...opts.elements,
|
|
@@ -260,6 +264,12 @@ Object.defineProperties(Engine.prototype, {
|
|
|
260
264
|
},
|
|
261
265
|
});
|
|
262
266
|
|
|
267
|
+
/**
|
|
268
|
+
* Execute the loaded definitions.
|
|
269
|
+
* @param {import('types').BpmnEngineExecuteOptions | ((err: Error, execution?: Execution) => void)} [optionsOrCallback]
|
|
270
|
+
* @param {(err: Error, execution?: Execution) => void} [callback]
|
|
271
|
+
* @returns {Promise<Execution>}
|
|
272
|
+
*/
|
|
263
273
|
Engine.prototype.execute = async function execute(...args) {
|
|
264
274
|
const [executeOptions, callback] = getOptionsAndCallback(...args);
|
|
265
275
|
try {
|
|
@@ -273,12 +283,19 @@ Engine.prototype.execute = async function execute(...args) {
|
|
|
273
283
|
return execution._execute(executeOptions, callback);
|
|
274
284
|
};
|
|
275
285
|
|
|
286
|
+
/** @returns {Promise<void>} */
|
|
276
287
|
Engine.prototype.stop = function stop() {
|
|
277
288
|
const execution = this.execution;
|
|
278
289
|
if (!execution) return;
|
|
279
290
|
return execution.stop();
|
|
280
291
|
};
|
|
281
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Recover engine from saved state.
|
|
295
|
+
* @param {import('types').BpmnEngineExecutionState | null} savedState
|
|
296
|
+
* @param {import('types').BpmnEngineOptions} [recoverOptions]
|
|
297
|
+
* @returns {Engine}
|
|
298
|
+
*/
|
|
282
299
|
Engine.prototype.recover = function recover(savedState, recoverOptions) {
|
|
283
300
|
if (this[kExecution]?.isRunning) {
|
|
284
301
|
throw new Error('cannot recover a running engine');
|
|
@@ -306,7 +323,7 @@ Engine.prototype.recover = function recover(savedState, recoverOptions) {
|
|
|
306
323
|
const typeResolver = this[kTypeResolver];
|
|
307
324
|
const loadedDefinitions = (this[kLoadedDefinitions] = savedState.definitions.map((dState) => {
|
|
308
325
|
let source;
|
|
309
|
-
if (dState.source) source =
|
|
326
|
+
if (dState.source) source = moddleContextSerializer.deserialize(JSON.parse(dState.source), typeResolver);
|
|
310
327
|
else source = preSources.find((s) => s.id === dState.id);
|
|
311
328
|
|
|
312
329
|
pendingSources.add(source);
|
|
@@ -324,6 +341,12 @@ Engine.prototype.recover = function recover(savedState, recoverOptions) {
|
|
|
324
341
|
return this;
|
|
325
342
|
};
|
|
326
343
|
|
|
344
|
+
/**
|
|
345
|
+
* Resume execution from a previously recovered state.
|
|
346
|
+
* @param {import('types').BpmnEngineExecuteOptions | ((err: Error, execution?: Execution) => void)} [optionsOrCallback]
|
|
347
|
+
* @param {(err: Error, execution?: Execution) => void} [callback]
|
|
348
|
+
* @returns {Promise<Execution>}
|
|
349
|
+
*/
|
|
327
350
|
Engine.prototype.resume = async function resume(...args) {
|
|
328
351
|
const [resumeOptions, callback] = getOptionsAndCallback(...args);
|
|
329
352
|
|
|
@@ -345,6 +368,10 @@ Engine.prototype.resume = async function resume(...args) {
|
|
|
345
368
|
return execution._resume(resumeOptions, callback);
|
|
346
369
|
};
|
|
347
370
|
|
|
371
|
+
/**
|
|
372
|
+
* Add a pre-serialized source context to the engine.
|
|
373
|
+
* @param {{ sourceContext: import('types').SerializableContext }} [options]
|
|
374
|
+
*/
|
|
348
375
|
Engine.prototype.addSource = function addSource(options) {
|
|
349
376
|
if (!options?.sourceContext) return;
|
|
350
377
|
const loadedDefinitions = this[kLoadedDefinitions];
|
|
@@ -352,16 +379,25 @@ Engine.prototype.addSource = function addSource(options) {
|
|
|
352
379
|
this[kPendingSources].add(options.sourceContext);
|
|
353
380
|
};
|
|
354
381
|
|
|
382
|
+
/**
|
|
383
|
+
* @param {import('types').BpmnEngineExecuteOptions} [executeOptions]
|
|
384
|
+
* @returns {Promise<import('bpmn-elements').Definition[]>}
|
|
385
|
+
*/
|
|
355
386
|
Engine.prototype.getDefinitions = function getDefinitions(executeOptions) {
|
|
356
387
|
const loadedDefinitions = this[kLoadedDefinitions];
|
|
357
388
|
if (loadedDefinitions?.length) return Promise.resolve(loadedDefinitions);
|
|
358
389
|
return this._loadDefinitions(executeOptions);
|
|
359
390
|
};
|
|
360
391
|
|
|
392
|
+
/**
|
|
393
|
+
* @param {string} id
|
|
394
|
+
* @returns {Promise<import('bpmn-elements').Definition>}
|
|
395
|
+
*/
|
|
361
396
|
Engine.prototype.getDefinitionById = async function getDefinitionById(id) {
|
|
362
397
|
return (await this.getDefinitions()).find((d) => d.id === id);
|
|
363
398
|
};
|
|
364
399
|
|
|
400
|
+
/** @returns {Promise<import('types').BpmnEngineExecutionState>} */
|
|
365
401
|
Engine.prototype.getState = async function getState() {
|
|
366
402
|
const execution = this.execution;
|
|
367
403
|
if (execution) return execution.getState();
|
|
@@ -370,6 +406,11 @@ Engine.prototype.getState = async function getState() {
|
|
|
370
406
|
return new Execution(this, definitions, this.options).getState();
|
|
371
407
|
};
|
|
372
408
|
|
|
409
|
+
/**
|
|
410
|
+
* @template R
|
|
411
|
+
* @param {import('types').BpmnEngineEvent} eventName
|
|
412
|
+
* @returns {Promise<R>}
|
|
413
|
+
*/
|
|
373
414
|
Engine.prototype.waitFor = function waitFor(eventName) {
|
|
374
415
|
const self = this;
|
|
375
416
|
return new Promise((resolve, reject) => {
|
|
@@ -387,12 +428,14 @@ Engine.prototype.waitFor = function waitFor(eventName) {
|
|
|
387
428
|
});
|
|
388
429
|
};
|
|
389
430
|
|
|
431
|
+
/** @internal */
|
|
390
432
|
Engine.prototype._loadDefinitions = async function loadDefinitions(executeOptions) {
|
|
391
433
|
const runSources = await Promise.all(this[kPendingSources]);
|
|
392
434
|
const loadedDefinitions = (this[kLoadedDefinitions] = runSources.map((source) => this._loadDefinition(source, executeOptions)));
|
|
393
435
|
return loadedDefinitions;
|
|
394
436
|
};
|
|
395
437
|
|
|
438
|
+
/** @internal */
|
|
396
439
|
Engine.prototype._loadDefinition = function loadDefinition(serializedContext, executeOptions) {
|
|
397
440
|
const environment = this.environment;
|
|
398
441
|
const context = new Elements__namespace.Context(
|
|
@@ -415,20 +458,29 @@ Engine.prototype._loadDefinition = function loadDefinition(serializedContext, ex
|
|
|
415
458
|
return new Elements__namespace.Definition(context);
|
|
416
459
|
};
|
|
417
460
|
|
|
461
|
+
/** @internal */
|
|
418
462
|
Engine.prototype._serializeSource = async function serializeSource(source) {
|
|
419
463
|
const moddleContext = await this._getModdleContext(source);
|
|
420
464
|
return this._serializeModdleContext(moddleContext);
|
|
421
465
|
};
|
|
422
466
|
|
|
467
|
+
/** @internal */
|
|
423
468
|
Engine.prototype._serializeModdleContext = function serializeModdleContext(moddleContext) {
|
|
424
|
-
return
|
|
469
|
+
return moddleContextSerializer.Serializer(moddleContext, this[kTypeResolver], this.options.extendFn);
|
|
425
470
|
};
|
|
426
471
|
|
|
472
|
+
/** @internal */
|
|
427
473
|
Engine.prototype._getModdleContext = function getModdleContext(source) {
|
|
428
474
|
const bpmnModdle = new BpmnModdle(this.options.moddleOptions);
|
|
429
475
|
return bpmnModdle.fromXML(Buffer.isBuffer(source) ? source.toString() : source.trim());
|
|
430
476
|
};
|
|
431
477
|
|
|
478
|
+
/**
|
|
479
|
+
* @param {Engine} engine
|
|
480
|
+
* @param {any[]} definitions
|
|
481
|
+
* @param {import('types').BpmnEngineExecuteOptions} [options]
|
|
482
|
+
* @param {boolean} [isRecovered]
|
|
483
|
+
*/
|
|
432
484
|
function Execution(engine, definitions, options, isRecovered = false) {
|
|
433
485
|
this.name = engine.name;
|
|
434
486
|
this.options = options;
|
|
@@ -478,6 +530,7 @@ Object.defineProperties(Execution.prototype, {
|
|
|
478
530
|
},
|
|
479
531
|
});
|
|
480
532
|
|
|
533
|
+
/** @internal */
|
|
481
534
|
Execution.prototype._execute = function execute(executeOptions, callback) {
|
|
482
535
|
this._setup(executeOptions);
|
|
483
536
|
this[kStopped] = false;
|
|
@@ -499,6 +552,7 @@ Execution.prototype._execute = function execute(executeOptions, callback) {
|
|
|
499
552
|
return this;
|
|
500
553
|
};
|
|
501
554
|
|
|
555
|
+
/** @internal */
|
|
502
556
|
Execution.prototype._resume = function resume(resumeOptions, callback) {
|
|
503
557
|
this._setup(resumeOptions);
|
|
504
558
|
|
|
@@ -512,6 +566,7 @@ Execution.prototype._resume = function resume(resumeOptions, callback) {
|
|
|
512
566
|
return this;
|
|
513
567
|
};
|
|
514
568
|
|
|
569
|
+
/** @internal */
|
|
515
570
|
Execution.prototype._addConsumerCallbacks = function addConsumerCallbacks(callback) {
|
|
516
571
|
if (!callback) return;
|
|
517
572
|
|
|
@@ -562,6 +617,7 @@ Execution.prototype._addConsumerCallbacks = function addConsumerCallbacks(callba
|
|
|
562
617
|
}
|
|
563
618
|
};
|
|
564
619
|
|
|
620
|
+
/** @returns {Promise<any>} */
|
|
565
621
|
Execution.prototype.stop = async function stop() {
|
|
566
622
|
const engine = this[kEngine];
|
|
567
623
|
const prom = engine.waitFor('stop');
|
|
@@ -579,6 +635,7 @@ Execution.prototype.stop = async function stop() {
|
|
|
579
635
|
return result;
|
|
580
636
|
};
|
|
581
637
|
|
|
638
|
+
/** @internal */
|
|
582
639
|
Execution.prototype._setup = function setup(setupOptions) {
|
|
583
640
|
const listener = setupOptions?.listener || this.options.listener;
|
|
584
641
|
if (listener && typeof listener.emit !== 'function') throw new Error('listener.emit is not a function');
|
|
@@ -598,6 +655,7 @@ Execution.prototype._setup = function setup(setupOptions) {
|
|
|
598
655
|
}
|
|
599
656
|
};
|
|
600
657
|
|
|
658
|
+
/** @internal */
|
|
601
659
|
Execution.prototype._onChildMessage = function onChildMessage(routingKey, message, owner) {
|
|
602
660
|
const { environment: ownerEnvironment } = owner;
|
|
603
661
|
const listener = ownerEnvironment.options?.listener;
|
|
@@ -667,6 +725,7 @@ Execution.prototype._onChildMessage = function onChildMessage(routingKey, messag
|
|
|
667
725
|
}
|
|
668
726
|
};
|
|
669
727
|
|
|
728
|
+
/** @internal */
|
|
670
729
|
Execution.prototype._complete = function complete(eventType, content, messageProperties) {
|
|
671
730
|
const timers = this.environment.timers;
|
|
672
731
|
timers.executing.slice().forEach((ref) => timers.clearTimeout(ref));
|
|
@@ -674,11 +733,13 @@ Execution.prototype._complete = function complete(eventType, content, messagePro
|
|
|
674
733
|
return eventType !== 'error' && this[kEngine].emit(eventType, this);
|
|
675
734
|
};
|
|
676
735
|
|
|
736
|
+
/** @internal */
|
|
677
737
|
Execution.prototype._teardownDefinition = function teardownDefinition(definition) {
|
|
678
738
|
this[kExecuting].delete(definition);
|
|
679
739
|
definition.broker.cancel('_engine_definition');
|
|
680
740
|
};
|
|
681
741
|
|
|
742
|
+
/** @internal */
|
|
682
743
|
Execution.prototype._saveOutput = function saveOutput(output) {
|
|
683
744
|
if (!output || typeof output !== 'object') return;
|
|
684
745
|
|
|
@@ -694,6 +755,7 @@ Execution.prototype._saveOutput = function saveOutput(output) {
|
|
|
694
755
|
}
|
|
695
756
|
};
|
|
696
757
|
|
|
758
|
+
/** @returns {import('types').BpmnEngineExecutionState} */
|
|
697
759
|
Execution.prototype.getState = function getState() {
|
|
698
760
|
const definitions = [];
|
|
699
761
|
for (const definition of this.definitions) {
|
|
@@ -713,6 +775,10 @@ Execution.prototype.getState = function getState() {
|
|
|
713
775
|
};
|
|
714
776
|
};
|
|
715
777
|
|
|
778
|
+
/**
|
|
779
|
+
* @param {string} activityId
|
|
780
|
+
* @returns {any}
|
|
781
|
+
*/
|
|
716
782
|
Execution.prototype.getActivityById = function getActivityById(activityId) {
|
|
717
783
|
for (const definition of this.definitions) {
|
|
718
784
|
const activity = definition.getActivityById(activityId);
|
|
@@ -720,6 +786,7 @@ Execution.prototype.getActivityById = function getActivityById(activityId) {
|
|
|
720
786
|
}
|
|
721
787
|
};
|
|
722
788
|
|
|
789
|
+
/** @returns {any[]} */
|
|
723
790
|
Execution.prototype.getPostponed = function getPostponed() {
|
|
724
791
|
const definitions = this.stopped ? this.definitions : this[kExecuting];
|
|
725
792
|
let result = [];
|
|
@@ -729,6 +796,10 @@ Execution.prototype.getPostponed = function getPostponed() {
|
|
|
729
796
|
return result;
|
|
730
797
|
};
|
|
731
798
|
|
|
799
|
+
/**
|
|
800
|
+
* @param {import('types').BpmnMessage} [payload]
|
|
801
|
+
* @param {{ ignoreSameDefinition?: boolean }} [signalOptions]
|
|
802
|
+
*/
|
|
732
803
|
Execution.prototype.signal = function signal(payload, signalOptions) {
|
|
733
804
|
for (const definition of this[kExecuting]) {
|
|
734
805
|
if (signalOptions?.ignoreSameDefinition && payload?.parent?.id === definition.id) continue;
|
|
@@ -736,26 +807,35 @@ Execution.prototype.signal = function signal(payload, signalOptions) {
|
|
|
736
807
|
}
|
|
737
808
|
};
|
|
738
809
|
|
|
810
|
+
/** @param {import('types').BpmnMessage} [payload] */
|
|
739
811
|
Execution.prototype.cancelActivity = function cancelActivity(payload) {
|
|
740
812
|
for (const definition of this[kExecuting]) {
|
|
741
813
|
definition.cancelActivity(payload);
|
|
742
814
|
}
|
|
743
815
|
};
|
|
744
816
|
|
|
817
|
+
/**
|
|
818
|
+
* @template T
|
|
819
|
+
* @param {import('types').BpmnEngineEvent} eventName
|
|
820
|
+
* @returns {Promise<T>}
|
|
821
|
+
*/
|
|
745
822
|
Execution.prototype.waitFor = function waitFor(...args) {
|
|
746
823
|
return this[kEngine].waitFor(...args);
|
|
747
824
|
};
|
|
748
825
|
|
|
826
|
+
/** @internal */
|
|
749
827
|
Execution.prototype._onBrokerReturn = function onBrokerReturn(message) {
|
|
750
828
|
if (message.properties.type === 'error') {
|
|
751
829
|
this[kEngine].emit('error', message.content);
|
|
752
830
|
}
|
|
753
831
|
};
|
|
754
832
|
|
|
833
|
+
/** @internal */
|
|
755
834
|
Execution.prototype._debug = function debug(msg) {
|
|
756
835
|
this[kEngine].logger.debug(`<${this.name}> ${msg}`);
|
|
757
836
|
};
|
|
758
837
|
|
|
838
|
+
/** @internal */
|
|
759
839
|
Execution.prototype._getActivityStatus = function getActivityStatus() {
|
|
760
840
|
let status = 'idle';
|
|
761
841
|
const executing = this[kExecuting];
|
package/package.json
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bpmn-engine",
|
|
3
3
|
"description": "BPMN 2.0 execution engine. Open source javascript workflow engine.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "26.0.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.js",
|
|
7
7
|
"main": "./lib/index.cjs",
|
|
8
|
-
"types": "./types/
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
9
|
"sideEffects": false,
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./types/index.d.ts",
|
|
13
|
+
"require": "./lib/index.cjs",
|
|
14
|
+
"import": "./src/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
10
17
|
"repository": {
|
|
11
18
|
"type": "git",
|
|
12
19
|
"url": "git://github.com/paed01/bpmn-engine.git"
|
|
@@ -50,31 +57,40 @@
|
|
|
50
57
|
"url": "https://github.com/paed01/bpmn-engine/master/LICENSE"
|
|
51
58
|
}
|
|
52
59
|
],
|
|
60
|
+
"overrides": {
|
|
61
|
+
"c8": {
|
|
62
|
+
"yargs": "^18.0.0"
|
|
63
|
+
},
|
|
64
|
+
"mocha": {
|
|
65
|
+
"yargs": "^18.0.0"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
53
68
|
"devDependencies": {
|
|
54
69
|
"@bonniernews/hot-bev": "^0.4.0",
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@types/
|
|
70
|
+
"@eslint/js": "^10.0.1",
|
|
71
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
72
|
+
"@types/bpmn-moddle": "^10.0.0",
|
|
73
|
+
"@types/node": "^20.19.40",
|
|
58
74
|
"bent": "^7.3.12",
|
|
59
|
-
"c8": "^
|
|
75
|
+
"c8": "^11.0.0",
|
|
60
76
|
"camunda-bpmn-moddle": "^7.0.1",
|
|
61
|
-
"chai": "^
|
|
62
|
-
"chronokinesis": "^
|
|
63
|
-
"eslint": "^
|
|
64
|
-
"
|
|
77
|
+
"chai": "^6.2.0",
|
|
78
|
+
"chronokinesis": "^8.0.0",
|
|
79
|
+
"eslint": "^10.5.0",
|
|
80
|
+
"globals": "^17.6.0",
|
|
65
81
|
"mocha": "^11.0.1",
|
|
66
82
|
"mocha-cakes-2": "^3.3.0",
|
|
67
83
|
"prettier": "^3.2.5",
|
|
68
84
|
"rollup": "^4.10.0",
|
|
69
|
-
"texample": "^
|
|
85
|
+
"texample": "^1.0.2"
|
|
70
86
|
},
|
|
71
87
|
"dependencies": {
|
|
72
|
-
"bpmn-elements": "^
|
|
73
|
-
"bpmn-moddle": "^9.0.
|
|
74
|
-
"debug": "^4.3
|
|
75
|
-
"moddle-context-serializer": "^
|
|
88
|
+
"bpmn-elements": "^18.0.0",
|
|
89
|
+
"bpmn-moddle": "^9.0.4",
|
|
90
|
+
"debug": "^4.4.3",
|
|
91
|
+
"moddle-context-serializer": "^6.0.0"
|
|
76
92
|
},
|
|
77
93
|
"peerDependencies": {
|
|
78
|
-
"smqp": "
|
|
94
|
+
"smqp": "^13.0.1"
|
|
79
95
|
}
|
|
80
96
|
}
|
package/src/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
5
5
|
import BpmnModdle from 'bpmn-moddle';
|
|
6
6
|
import * as Elements from 'bpmn-elements';
|
|
7
7
|
import { Broker } from 'smqp';
|
|
8
|
-
import
|
|
8
|
+
import { Serializer, deserialize, TypeResolver } from 'moddle-context-serializer';
|
|
9
9
|
|
|
10
10
|
import DebugLogger from './Logger.js';
|
|
11
11
|
import JavaScripts from './JavaScripts.js';
|
|
@@ -29,6 +29,10 @@ const kTypeResolver = Symbol.for('type resolver');
|
|
|
29
29
|
export default Engine;
|
|
30
30
|
export { JavaScripts };
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* BPMN 2.0 execution engine.
|
|
34
|
+
* @param {import('types').BpmnEngineOptions} [options]
|
|
35
|
+
*/
|
|
32
36
|
export function Engine(options) {
|
|
33
37
|
if (!(this instanceof Engine)) return new Engine(options);
|
|
34
38
|
|
|
@@ -114,6 +118,12 @@ Object.defineProperties(Engine.prototype, {
|
|
|
114
118
|
},
|
|
115
119
|
});
|
|
116
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Execute the loaded definitions.
|
|
123
|
+
* @param {import('types').BpmnEngineExecuteOptions | ((err: Error, execution?: Execution) => void)} [optionsOrCallback]
|
|
124
|
+
* @param {(err: Error, execution?: Execution) => void} [callback]
|
|
125
|
+
* @returns {Promise<Execution>}
|
|
126
|
+
*/
|
|
117
127
|
Engine.prototype.execute = async function execute(...args) {
|
|
118
128
|
const [executeOptions, callback] = getOptionsAndCallback(...args);
|
|
119
129
|
try {
|
|
@@ -127,12 +137,19 @@ Engine.prototype.execute = async function execute(...args) {
|
|
|
127
137
|
return execution._execute(executeOptions, callback);
|
|
128
138
|
};
|
|
129
139
|
|
|
140
|
+
/** @returns {Promise<void>} */
|
|
130
141
|
Engine.prototype.stop = function stop() {
|
|
131
142
|
const execution = this.execution;
|
|
132
143
|
if (!execution) return;
|
|
133
144
|
return execution.stop();
|
|
134
145
|
};
|
|
135
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Recover engine from saved state.
|
|
149
|
+
* @param {import('types').BpmnEngineExecutionState | null} savedState
|
|
150
|
+
* @param {import('types').BpmnEngineOptions} [recoverOptions]
|
|
151
|
+
* @returns {Engine}
|
|
152
|
+
*/
|
|
136
153
|
Engine.prototype.recover = function recover(savedState, recoverOptions) {
|
|
137
154
|
if (this[kExecution]?.isRunning) {
|
|
138
155
|
throw new Error('cannot recover a running engine');
|
|
@@ -178,6 +195,12 @@ Engine.prototype.recover = function recover(savedState, recoverOptions) {
|
|
|
178
195
|
return this;
|
|
179
196
|
};
|
|
180
197
|
|
|
198
|
+
/**
|
|
199
|
+
* Resume execution from a previously recovered state.
|
|
200
|
+
* @param {import('types').BpmnEngineExecuteOptions | ((err: Error, execution?: Execution) => void)} [optionsOrCallback]
|
|
201
|
+
* @param {(err: Error, execution?: Execution) => void} [callback]
|
|
202
|
+
* @returns {Promise<Execution>}
|
|
203
|
+
*/
|
|
181
204
|
Engine.prototype.resume = async function resume(...args) {
|
|
182
205
|
const [resumeOptions, callback] = getOptionsAndCallback(...args);
|
|
183
206
|
|
|
@@ -199,6 +222,10 @@ Engine.prototype.resume = async function resume(...args) {
|
|
|
199
222
|
return execution._resume(resumeOptions, callback);
|
|
200
223
|
};
|
|
201
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Add a pre-serialized source context to the engine.
|
|
227
|
+
* @param {{ sourceContext: import('types').SerializableContext }} [options]
|
|
228
|
+
*/
|
|
202
229
|
Engine.prototype.addSource = function addSource(options) {
|
|
203
230
|
if (!options?.sourceContext) return;
|
|
204
231
|
const loadedDefinitions = this[kLoadedDefinitions];
|
|
@@ -206,16 +233,25 @@ Engine.prototype.addSource = function addSource(options) {
|
|
|
206
233
|
this[kPendingSources].add(options.sourceContext);
|
|
207
234
|
};
|
|
208
235
|
|
|
236
|
+
/**
|
|
237
|
+
* @param {import('types').BpmnEngineExecuteOptions} [executeOptions]
|
|
238
|
+
* @returns {Promise<import('bpmn-elements').Definition[]>}
|
|
239
|
+
*/
|
|
209
240
|
Engine.prototype.getDefinitions = function getDefinitions(executeOptions) {
|
|
210
241
|
const loadedDefinitions = this[kLoadedDefinitions];
|
|
211
242
|
if (loadedDefinitions?.length) return Promise.resolve(loadedDefinitions);
|
|
212
243
|
return this._loadDefinitions(executeOptions);
|
|
213
244
|
};
|
|
214
245
|
|
|
246
|
+
/**
|
|
247
|
+
* @param {string} id
|
|
248
|
+
* @returns {Promise<import('bpmn-elements').Definition>}
|
|
249
|
+
*/
|
|
215
250
|
Engine.prototype.getDefinitionById = async function getDefinitionById(id) {
|
|
216
251
|
return (await this.getDefinitions()).find((d) => d.id === id);
|
|
217
252
|
};
|
|
218
253
|
|
|
254
|
+
/** @returns {Promise<import('types').BpmnEngineExecutionState>} */
|
|
219
255
|
Engine.prototype.getState = async function getState() {
|
|
220
256
|
const execution = this.execution;
|
|
221
257
|
if (execution) return execution.getState();
|
|
@@ -224,6 +260,11 @@ Engine.prototype.getState = async function getState() {
|
|
|
224
260
|
return new Execution(this, definitions, this.options).getState();
|
|
225
261
|
};
|
|
226
262
|
|
|
263
|
+
/**
|
|
264
|
+
* @template R
|
|
265
|
+
* @param {import('types').BpmnEngineEvent} eventName
|
|
266
|
+
* @returns {Promise<R>}
|
|
267
|
+
*/
|
|
227
268
|
Engine.prototype.waitFor = function waitFor(eventName) {
|
|
228
269
|
const self = this;
|
|
229
270
|
return new Promise((resolve, reject) => {
|
|
@@ -241,12 +282,14 @@ Engine.prototype.waitFor = function waitFor(eventName) {
|
|
|
241
282
|
});
|
|
242
283
|
};
|
|
243
284
|
|
|
285
|
+
/** @internal */
|
|
244
286
|
Engine.prototype._loadDefinitions = async function loadDefinitions(executeOptions) {
|
|
245
287
|
const runSources = await Promise.all(this[kPendingSources]);
|
|
246
288
|
const loadedDefinitions = (this[kLoadedDefinitions] = runSources.map((source) => this._loadDefinition(source, executeOptions)));
|
|
247
289
|
return loadedDefinitions;
|
|
248
290
|
};
|
|
249
291
|
|
|
292
|
+
/** @internal */
|
|
250
293
|
Engine.prototype._loadDefinition = function loadDefinition(serializedContext, executeOptions) {
|
|
251
294
|
const environment = this.environment;
|
|
252
295
|
const context = new Elements.Context(
|
|
@@ -269,20 +312,29 @@ Engine.prototype._loadDefinition = function loadDefinition(serializedContext, ex
|
|
|
269
312
|
return new Elements.Definition(context);
|
|
270
313
|
};
|
|
271
314
|
|
|
315
|
+
/** @internal */
|
|
272
316
|
Engine.prototype._serializeSource = async function serializeSource(source) {
|
|
273
317
|
const moddleContext = await this._getModdleContext(source);
|
|
274
318
|
return this._serializeModdleContext(moddleContext);
|
|
275
319
|
};
|
|
276
320
|
|
|
321
|
+
/** @internal */
|
|
277
322
|
Engine.prototype._serializeModdleContext = function serializeModdleContext(moddleContext) {
|
|
278
|
-
return
|
|
323
|
+
return Serializer(moddleContext, this[kTypeResolver], this.options.extendFn);
|
|
279
324
|
};
|
|
280
325
|
|
|
326
|
+
/** @internal */
|
|
281
327
|
Engine.prototype._getModdleContext = function getModdleContext(source) {
|
|
282
328
|
const bpmnModdle = new BpmnModdle(this.options.moddleOptions);
|
|
283
329
|
return bpmnModdle.fromXML(Buffer.isBuffer(source) ? source.toString() : source.trim());
|
|
284
330
|
};
|
|
285
331
|
|
|
332
|
+
/**
|
|
333
|
+
* @param {Engine} engine
|
|
334
|
+
* @param {any[]} definitions
|
|
335
|
+
* @param {import('types').BpmnEngineExecuteOptions} [options]
|
|
336
|
+
* @param {boolean} [isRecovered]
|
|
337
|
+
*/
|
|
286
338
|
export function Execution(engine, definitions, options, isRecovered = false) {
|
|
287
339
|
this.name = engine.name;
|
|
288
340
|
this.options = options;
|
|
@@ -332,6 +384,7 @@ Object.defineProperties(Execution.prototype, {
|
|
|
332
384
|
},
|
|
333
385
|
});
|
|
334
386
|
|
|
387
|
+
/** @internal */
|
|
335
388
|
Execution.prototype._execute = function execute(executeOptions, callback) {
|
|
336
389
|
this._setup(executeOptions);
|
|
337
390
|
this[kStopped] = false;
|
|
@@ -353,6 +406,7 @@ Execution.prototype._execute = function execute(executeOptions, callback) {
|
|
|
353
406
|
return this;
|
|
354
407
|
};
|
|
355
408
|
|
|
409
|
+
/** @internal */
|
|
356
410
|
Execution.prototype._resume = function resume(resumeOptions, callback) {
|
|
357
411
|
this._setup(resumeOptions);
|
|
358
412
|
|
|
@@ -366,6 +420,7 @@ Execution.prototype._resume = function resume(resumeOptions, callback) {
|
|
|
366
420
|
return this;
|
|
367
421
|
};
|
|
368
422
|
|
|
423
|
+
/** @internal */
|
|
369
424
|
Execution.prototype._addConsumerCallbacks = function addConsumerCallbacks(callback) {
|
|
370
425
|
if (!callback) return;
|
|
371
426
|
|
|
@@ -416,6 +471,7 @@ Execution.prototype._addConsumerCallbacks = function addConsumerCallbacks(callba
|
|
|
416
471
|
}
|
|
417
472
|
};
|
|
418
473
|
|
|
474
|
+
/** @returns {Promise<any>} */
|
|
419
475
|
Execution.prototype.stop = async function stop() {
|
|
420
476
|
const engine = this[kEngine];
|
|
421
477
|
const prom = engine.waitFor('stop');
|
|
@@ -433,6 +489,7 @@ Execution.prototype.stop = async function stop() {
|
|
|
433
489
|
return result;
|
|
434
490
|
};
|
|
435
491
|
|
|
492
|
+
/** @internal */
|
|
436
493
|
Execution.prototype._setup = function setup(setupOptions) {
|
|
437
494
|
const listener = setupOptions?.listener || this.options.listener;
|
|
438
495
|
if (listener && typeof listener.emit !== 'function') throw new Error('listener.emit is not a function');
|
|
@@ -452,6 +509,7 @@ Execution.prototype._setup = function setup(setupOptions) {
|
|
|
452
509
|
}
|
|
453
510
|
};
|
|
454
511
|
|
|
512
|
+
/** @internal */
|
|
455
513
|
Execution.prototype._onChildMessage = function onChildMessage(routingKey, message, owner) {
|
|
456
514
|
const { environment: ownerEnvironment } = owner;
|
|
457
515
|
const listener = ownerEnvironment.options?.listener;
|
|
@@ -521,6 +579,7 @@ Execution.prototype._onChildMessage = function onChildMessage(routingKey, messag
|
|
|
521
579
|
}
|
|
522
580
|
};
|
|
523
581
|
|
|
582
|
+
/** @internal */
|
|
524
583
|
Execution.prototype._complete = function complete(eventType, content, messageProperties) {
|
|
525
584
|
const timers = this.environment.timers;
|
|
526
585
|
timers.executing.slice().forEach((ref) => timers.clearTimeout(ref));
|
|
@@ -528,11 +587,13 @@ Execution.prototype._complete = function complete(eventType, content, messagePro
|
|
|
528
587
|
return eventType !== 'error' && this[kEngine].emit(eventType, this);
|
|
529
588
|
};
|
|
530
589
|
|
|
590
|
+
/** @internal */
|
|
531
591
|
Execution.prototype._teardownDefinition = function teardownDefinition(definition) {
|
|
532
592
|
this[kExecuting].delete(definition);
|
|
533
593
|
definition.broker.cancel('_engine_definition');
|
|
534
594
|
};
|
|
535
595
|
|
|
596
|
+
/** @internal */
|
|
536
597
|
Execution.prototype._saveOutput = function saveOutput(output) {
|
|
537
598
|
if (!output || typeof output !== 'object') return;
|
|
538
599
|
|
|
@@ -548,6 +609,7 @@ Execution.prototype._saveOutput = function saveOutput(output) {
|
|
|
548
609
|
}
|
|
549
610
|
};
|
|
550
611
|
|
|
612
|
+
/** @returns {import('types').BpmnEngineExecutionState} */
|
|
551
613
|
Execution.prototype.getState = function getState() {
|
|
552
614
|
const definitions = [];
|
|
553
615
|
for (const definition of this.definitions) {
|
|
@@ -567,6 +629,10 @@ Execution.prototype.getState = function getState() {
|
|
|
567
629
|
};
|
|
568
630
|
};
|
|
569
631
|
|
|
632
|
+
/**
|
|
633
|
+
* @param {string} activityId
|
|
634
|
+
* @returns {any}
|
|
635
|
+
*/
|
|
570
636
|
Execution.prototype.getActivityById = function getActivityById(activityId) {
|
|
571
637
|
for (const definition of this.definitions) {
|
|
572
638
|
const activity = definition.getActivityById(activityId);
|
|
@@ -574,6 +640,7 @@ Execution.prototype.getActivityById = function getActivityById(activityId) {
|
|
|
574
640
|
}
|
|
575
641
|
};
|
|
576
642
|
|
|
643
|
+
/** @returns {any[]} */
|
|
577
644
|
Execution.prototype.getPostponed = function getPostponed() {
|
|
578
645
|
const definitions = this.stopped ? this.definitions : this[kExecuting];
|
|
579
646
|
let result = [];
|
|
@@ -583,6 +650,10 @@ Execution.prototype.getPostponed = function getPostponed() {
|
|
|
583
650
|
return result;
|
|
584
651
|
};
|
|
585
652
|
|
|
653
|
+
/**
|
|
654
|
+
* @param {import('types').BpmnMessage} [payload]
|
|
655
|
+
* @param {{ ignoreSameDefinition?: boolean }} [signalOptions]
|
|
656
|
+
*/
|
|
586
657
|
Execution.prototype.signal = function signal(payload, signalOptions) {
|
|
587
658
|
for (const definition of this[kExecuting]) {
|
|
588
659
|
if (signalOptions?.ignoreSameDefinition && payload?.parent?.id === definition.id) continue;
|
|
@@ -590,26 +661,35 @@ Execution.prototype.signal = function signal(payload, signalOptions) {
|
|
|
590
661
|
}
|
|
591
662
|
};
|
|
592
663
|
|
|
664
|
+
/** @param {import('types').BpmnMessage} [payload] */
|
|
593
665
|
Execution.prototype.cancelActivity = function cancelActivity(payload) {
|
|
594
666
|
for (const definition of this[kExecuting]) {
|
|
595
667
|
definition.cancelActivity(payload);
|
|
596
668
|
}
|
|
597
669
|
};
|
|
598
670
|
|
|
671
|
+
/**
|
|
672
|
+
* @template T
|
|
673
|
+
* @param {import('types').BpmnEngineEvent} eventName
|
|
674
|
+
* @returns {Promise<T>}
|
|
675
|
+
*/
|
|
599
676
|
Execution.prototype.waitFor = function waitFor(...args) {
|
|
600
677
|
return this[kEngine].waitFor(...args);
|
|
601
678
|
};
|
|
602
679
|
|
|
680
|
+
/** @internal */
|
|
603
681
|
Execution.prototype._onBrokerReturn = function onBrokerReturn(message) {
|
|
604
682
|
if (message.properties.type === 'error') {
|
|
605
683
|
this[kEngine].emit('error', message.content);
|
|
606
684
|
}
|
|
607
685
|
};
|
|
608
686
|
|
|
687
|
+
/** @internal */
|
|
609
688
|
Execution.prototype._debug = function debug(msg) {
|
|
610
689
|
this[kEngine].logger.debug(`<${this.name}> ${msg}`);
|
|
611
690
|
};
|
|
612
691
|
|
|
692
|
+
/** @internal */
|
|
613
693
|
Execution.prototype._getActivityStatus = function getActivityStatus() {
|
|
614
694
|
let status = 'idle';
|
|
615
695
|
const executing = this[kExecuting];
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
declare module 'bpmn-engine' {
|
|
2
|
+
import type { EventEmitter } from 'node:events';
|
|
3
|
+
import type { Broker } from 'smqp';
|
|
4
|
+
import type { Definitions as BpmnModdleDefinitions } from 'bpmn-moddle';
|
|
5
|
+
import type { ExtendFn, SerializableContext, ResolverFn } from 'moddle-context-serializer';
|
|
6
|
+
import type {
|
|
7
|
+
ActivityStatus,
|
|
8
|
+
Definition,
|
|
9
|
+
ElementBase,
|
|
10
|
+
Environment,
|
|
11
|
+
EnvironmentOptions,
|
|
12
|
+
EnvironmentState,
|
|
13
|
+
IApi,
|
|
14
|
+
ILogger,
|
|
15
|
+
IScripts,
|
|
16
|
+
Script,
|
|
17
|
+
} from 'bpmn-elements';
|
|
18
|
+
|
|
19
|
+
export type BpmnEngineEvent = 'error' | 'stop' | 'end';
|
|
20
|
+
|
|
21
|
+
export type BpmnActivityEvent =
|
|
22
|
+
| 'activity.enter'
|
|
23
|
+
| 'activity.start'
|
|
24
|
+
| 'activity.wait'
|
|
25
|
+
| 'wait'
|
|
26
|
+
| 'activity.end'
|
|
27
|
+
| 'activity.leave'
|
|
28
|
+
| 'activity.stop'
|
|
29
|
+
| 'activity.throw'
|
|
30
|
+
| 'activity.error';
|
|
31
|
+
|
|
32
|
+
export type BpmnSequenceFlowEvent = 'flow.take' | 'flow.discard' | 'flow.looped';
|
|
33
|
+
|
|
34
|
+
export type BpmnEngineRunningStatus = 'idle' | 'running' | 'stopped' | 'error';
|
|
35
|
+
|
|
36
|
+
export interface BpmnMessage {
|
|
37
|
+
id?: string;
|
|
38
|
+
executionId?: string;
|
|
39
|
+
[name: string]: any;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IListenerEmitter {
|
|
43
|
+
emit(eventName: string, ...args: any[]): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface BpmnEngineExecuteOptions extends EnvironmentOptions {
|
|
47
|
+
listener?: EventEmitter | IListenerEmitter;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface BpmnEngineOptions extends BpmnEngineExecuteOptions {
|
|
51
|
+
name?: string;
|
|
52
|
+
source?: string;
|
|
53
|
+
sourceContext?: SerializableContext;
|
|
54
|
+
elements?: Record<string, any>;
|
|
55
|
+
typeResolver?: ResolverFn;
|
|
56
|
+
extendFn?: ExtendFn;
|
|
57
|
+
moddleOptions?: any;
|
|
58
|
+
moddleContext?: BpmnModdleDefinitions;
|
|
59
|
+
Logger?: (scope: string) => ILogger;
|
|
60
|
+
scripts?: IScripts;
|
|
61
|
+
disableDummyScript?: boolean;
|
|
62
|
+
[x: string]: any;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface BpmnEngineDefinitionState {
|
|
66
|
+
state: string;
|
|
67
|
+
source?: string;
|
|
68
|
+
[x: string]: any;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface BpmnEngineExecutionState {
|
|
72
|
+
name: string;
|
|
73
|
+
engineVersion: string;
|
|
74
|
+
state: BpmnEngineRunningStatus;
|
|
75
|
+
stopped: boolean;
|
|
76
|
+
environment: EnvironmentState;
|
|
77
|
+
definitions: BpmnEngineDefinitionState[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function Engine(options?: BpmnEngineOptions): Engine;
|
|
81
|
+
export class Engine extends EventEmitter {
|
|
82
|
+
constructor(options?: BpmnEngineOptions);
|
|
83
|
+
options: BpmnEngineOptions;
|
|
84
|
+
readonly name: string;
|
|
85
|
+
readonly broker: Broker;
|
|
86
|
+
readonly logger: ILogger;
|
|
87
|
+
readonly environment: Environment;
|
|
88
|
+
readonly state: BpmnEngineRunningStatus;
|
|
89
|
+
readonly stopped: boolean;
|
|
90
|
+
readonly execution: Execution | null;
|
|
91
|
+
readonly activityStatus: ActivityStatus;
|
|
92
|
+
|
|
93
|
+
execute(): Promise<Execution>;
|
|
94
|
+
execute(options: BpmnEngineExecuteOptions): Promise<Execution>;
|
|
95
|
+
execute(options: BpmnEngineExecuteOptions, callback: (err: Error | null, execution?: Execution) => void): Promise<Execution>;
|
|
96
|
+
execute(callback: (err: Error | null, execution?: Execution) => void): Promise<Execution>;
|
|
97
|
+
|
|
98
|
+
getDefinitionById(id: string): Promise<Definition | undefined>;
|
|
99
|
+
getDefinitions(executeOptions?: BpmnEngineExecuteOptions): Promise<Definition[]>;
|
|
100
|
+
getState(): Promise<BpmnEngineExecutionState>;
|
|
101
|
+
|
|
102
|
+
recover(savedState: BpmnEngineExecutionState | null, recoverOptions?: BpmnEngineOptions): Engine;
|
|
103
|
+
|
|
104
|
+
resume(): Promise<Execution>;
|
|
105
|
+
resume(options: BpmnEngineExecuteOptions): Promise<Execution>;
|
|
106
|
+
resume(options: BpmnEngineExecuteOptions, callback: (err: Error | null, execution?: Execution) => void): Promise<Execution>;
|
|
107
|
+
resume(callback: (err: Error | null, execution?: Execution) => void): Promise<Execution>;
|
|
108
|
+
|
|
109
|
+
stop(): Promise<void> | undefined;
|
|
110
|
+
|
|
111
|
+
addSource(options?: { sourceContext: SerializableContext }): void;
|
|
112
|
+
|
|
113
|
+
waitFor<R>(eventName: BpmnEngineEvent): Promise<R>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function Execution(
|
|
117
|
+
engine: Engine,
|
|
118
|
+
definitions: Definition[],
|
|
119
|
+
options?: BpmnEngineExecuteOptions,
|
|
120
|
+
isRecovered?: boolean
|
|
121
|
+
): Execution;
|
|
122
|
+
export class Execution {
|
|
123
|
+
constructor(engine: Engine, definitions: Definition[], options?: BpmnEngineExecuteOptions, isRecovered?: boolean);
|
|
124
|
+
options: BpmnEngineExecuteOptions;
|
|
125
|
+
readonly name: string;
|
|
126
|
+
readonly definitions: Definition[];
|
|
127
|
+
readonly broker: Broker;
|
|
128
|
+
readonly environment: Environment;
|
|
129
|
+
readonly state: BpmnEngineRunningStatus;
|
|
130
|
+
readonly stopped: boolean;
|
|
131
|
+
readonly isRunning: boolean;
|
|
132
|
+
readonly activityStatus: ActivityStatus;
|
|
133
|
+
|
|
134
|
+
getActivityById(activityId: string): ElementBase | undefined;
|
|
135
|
+
getActivityById<R>(activityId: string): R | undefined;
|
|
136
|
+
getPostponed(): IApi<ElementBase>[];
|
|
137
|
+
getState(): BpmnEngineExecutionState;
|
|
138
|
+
stop(): Promise<void>;
|
|
139
|
+
|
|
140
|
+
signal(message?: BpmnMessage, options?: { ignoreSameDefinition?: boolean }): void;
|
|
141
|
+
cancelActivity(message?: BpmnMessage): void;
|
|
142
|
+
|
|
143
|
+
waitFor<R>(eventName: BpmnEngineEvent): Promise<R>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export class JavaScripts implements IScripts {
|
|
147
|
+
constructor(disableDummy?: boolean);
|
|
148
|
+
register(activity: any): Script | undefined;
|
|
149
|
+
getScript(language: string, identifier: { id: string; [x: string]: any }): Script;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export default Engine;
|
|
153
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { EventEmitter } from 'node:events';
|
|
2
|
+
import type { Definitions as BpmnModdleDefinitions } from 'bpmn-moddle';
|
|
3
|
+
import type { ExtendFn, SerializableContext, ResolverFn } from 'moddle-context-serializer';
|
|
4
|
+
import type { ActivityStatus, ElementBroker, EnvironmentOptions, EnvironmentState, IScripts, Script, ILogger } from 'bpmn-elements';
|
|
5
|
+
|
|
6
|
+
export type { SerializableContext };
|
|
7
|
+
|
|
8
|
+
export type BpmnEngineEvent = 'error' | 'stop' | 'end';
|
|
9
|
+
|
|
10
|
+
export type BpmnActivityEvent =
|
|
11
|
+
| 'activity.enter'
|
|
12
|
+
| 'activity.start'
|
|
13
|
+
| 'activity.wait'
|
|
14
|
+
| 'wait'
|
|
15
|
+
| 'activity.end'
|
|
16
|
+
| 'activity.leave'
|
|
17
|
+
| 'activity.stop'
|
|
18
|
+
| 'activity.throw'
|
|
19
|
+
| 'activity.error';
|
|
20
|
+
|
|
21
|
+
export type BpmnSequenceFlowEvent = 'flow.take' | 'flow.discard' | 'flow.looped';
|
|
22
|
+
|
|
23
|
+
export type BpmnEngineRunningStatus = 'idle' | 'running' | 'stopped' | 'error';
|
|
24
|
+
|
|
25
|
+
export interface BpmnMessage {
|
|
26
|
+
id?: string;
|
|
27
|
+
executionId?: string;
|
|
28
|
+
[name: string]: any;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface IListenerEmitter {
|
|
32
|
+
emit(eventName: string, ...args: any[]): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface BpmnEngineExecuteOptions extends EnvironmentOptions {
|
|
36
|
+
listener?: EventEmitter | IListenerEmitter;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface BpmnEngineOptions extends BpmnEngineExecuteOptions {
|
|
40
|
+
name?: string;
|
|
41
|
+
source?: string;
|
|
42
|
+
sourceContext?: SerializableContext;
|
|
43
|
+
elements?: Record<string, any>;
|
|
44
|
+
typeResolver?: ResolverFn;
|
|
45
|
+
extendFn?: ExtendFn;
|
|
46
|
+
moddleOptions?: any;
|
|
47
|
+
moddleContext?: BpmnModdleDefinitions;
|
|
48
|
+
Logger?: (scope: string) => ILogger;
|
|
49
|
+
scripts?: IScripts;
|
|
50
|
+
disableDummyScript?: boolean;
|
|
51
|
+
[x: string]: any;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface BpmnEngineDefinitionState {
|
|
55
|
+
state: string;
|
|
56
|
+
source?: string;
|
|
57
|
+
[x: string]: any;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface BpmnEngineExecutionState {
|
|
61
|
+
name: string;
|
|
62
|
+
engineVersion: string;
|
|
63
|
+
state: BpmnEngineRunningStatus;
|
|
64
|
+
stopped: boolean;
|
|
65
|
+
environment: EnvironmentState;
|
|
66
|
+
definitions: BpmnEngineDefinitionState[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type {
|
|
70
|
+
ActivityStatus,
|
|
71
|
+
BpmnModdleDefinitions,
|
|
72
|
+
ElementBroker,
|
|
73
|
+
EnvironmentOptions,
|
|
74
|
+
EnvironmentState,
|
|
75
|
+
ExtendFn,
|
|
76
|
+
ILogger,
|
|
77
|
+
IScripts,
|
|
78
|
+
ResolverFn,
|
|
79
|
+
Script,
|
|
80
|
+
};
|
package/types/bpmn-engine.d.ts
DELETED
|
@@ -1,334 +0,0 @@
|
|
|
1
|
-
// Author : Saeed Tabrizi
|
|
2
|
-
// Refactored by : Pål Edman
|
|
3
|
-
|
|
4
|
-
import { EventEmitter } from 'node:events';
|
|
5
|
-
import { Definitions as BpmnModdleDefinitions } from 'bpmn-moddle';
|
|
6
|
-
import { extendFn, SerializableContext, TypeResolver } from 'moddle-context-serializer';
|
|
7
|
-
import {
|
|
8
|
-
ActivityStatus,
|
|
9
|
-
ElementBroker,
|
|
10
|
-
EnvironmentOptions,
|
|
11
|
-
Definition,
|
|
12
|
-
DefinitionState,
|
|
13
|
-
Environment,
|
|
14
|
-
Api,
|
|
15
|
-
ElementBase,
|
|
16
|
-
ILogger,
|
|
17
|
-
EnvironmentState,
|
|
18
|
-
IScripts,
|
|
19
|
-
Script,
|
|
20
|
-
} from 'bpmn-elements';
|
|
21
|
-
|
|
22
|
-
declare module 'bpmn-engine' {
|
|
23
|
-
/**
|
|
24
|
-
* Engine emits the following events:
|
|
25
|
-
|
|
26
|
-
error: An non-recoverable error has occurred
|
|
27
|
-
stop: Executions was stopped
|
|
28
|
-
end: Execution completed
|
|
29
|
-
*/
|
|
30
|
-
export type BpmnEngineEvent = 'error' | 'stop' | 'end';
|
|
31
|
-
/**
|
|
32
|
-
* Each activity and flow emits events when changing state.
|
|
33
|
-
|
|
34
|
-
activity.enter: An activity is entered
|
|
35
|
-
activity.start: An activity is started
|
|
36
|
-
activity.wait: The activity is postponed for some reason, e.g. a user task is waiting to be signaled or a message is expected
|
|
37
|
-
wait: Same as above
|
|
38
|
-
activity.end: An activity has ended successfully
|
|
39
|
-
activity.leave: The execution left the activity
|
|
40
|
-
activity.stop: Activity run was stopped
|
|
41
|
-
activity.throw: An recoverable error was thrown
|
|
42
|
-
activity.error: An non-recoverable error has occurred
|
|
43
|
-
*/
|
|
44
|
-
export type BpmnActivityEvent =
|
|
45
|
-
| 'activity.enter'
|
|
46
|
-
| 'activity.start'
|
|
47
|
-
| 'activity.wait'
|
|
48
|
-
| 'wait'
|
|
49
|
-
| 'activity.end'
|
|
50
|
-
| 'activity.leave'
|
|
51
|
-
| 'activity.stop'
|
|
52
|
-
| 'activity.throw'
|
|
53
|
-
| 'activity.error';
|
|
54
|
-
/**
|
|
55
|
-
* Sequence flow events
|
|
56
|
-
flow.take: The sequence flow was taken
|
|
57
|
-
flow.discard: The sequence flow was discarded
|
|
58
|
-
flow.looped: The sequence is looped
|
|
59
|
-
*/
|
|
60
|
-
export type BpmnSequenceFlowEvent = 'flow.take' | 'flow.discard' | 'flow.looped';
|
|
61
|
-
|
|
62
|
-
export interface BpmnMessage {
|
|
63
|
-
id?: string;
|
|
64
|
-
executionId?: string;
|
|
65
|
-
[name: string]: any;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface IListenerEmitter {
|
|
69
|
-
emit(eventName: string, ...args: any[]): void;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export interface BpmnEngineExecuteOptions extends EnvironmentOptions {
|
|
73
|
-
listener?: EventEmitter | IListenerEmitter;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface BpmnEngineOptions extends BpmnEngineExecuteOptions {
|
|
77
|
-
/**
|
|
78
|
-
* optional name of engine,
|
|
79
|
-
*/
|
|
80
|
-
name?: string;
|
|
81
|
-
/**
|
|
82
|
-
* optional BPMN 2.0 definition source as string
|
|
83
|
-
*/
|
|
84
|
-
source?: string;
|
|
85
|
-
/**
|
|
86
|
-
* optional serialized context supplied by moddle-context-serializer
|
|
87
|
-
*/
|
|
88
|
-
sourceContext?: SerializableContext;
|
|
89
|
-
/**
|
|
90
|
-
* optional override of BPMN Elements behaviors, defaults to bpmn-elements
|
|
91
|
-
*/
|
|
92
|
-
elements?: Record<string, any>;
|
|
93
|
-
/**
|
|
94
|
-
* Passed to moddle-context-serializer as TypeResolver
|
|
95
|
-
*/
|
|
96
|
-
typeResolver?: typeof TypeResolver;
|
|
97
|
-
/**
|
|
98
|
-
* Passed to moddle-context-serializer as extendFn
|
|
99
|
-
*/
|
|
100
|
-
extendFn?: extendFn;
|
|
101
|
-
/**
|
|
102
|
-
* optional bpmn-moddle options to be passed to bpmn-moddle
|
|
103
|
-
*/
|
|
104
|
-
moddleOptions?: any;
|
|
105
|
-
moddleContext?: BpmnModdleDefinitions;
|
|
106
|
-
[x: string]: any;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const enum BpmnEngineRunningStatus {
|
|
110
|
-
/**
|
|
111
|
-
* Engine is not executing
|
|
112
|
-
*/
|
|
113
|
-
Idle = 'idle',
|
|
114
|
-
/**
|
|
115
|
-
* Engine is executing
|
|
116
|
-
*/
|
|
117
|
-
Running = 'running',
|
|
118
|
-
/**
|
|
119
|
-
* Engine execution is stopped
|
|
120
|
-
*/
|
|
121
|
-
Stopped = 'stopped',
|
|
122
|
-
/**
|
|
123
|
-
* Engine execution has errored
|
|
124
|
-
*/
|
|
125
|
-
Error = 'error',
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export function Engine(options?: BpmnEngineOptions): Engine;
|
|
129
|
-
export class Engine extends EventEmitter {
|
|
130
|
-
constructor(options?: BpmnEngineOptions);
|
|
131
|
-
/**
|
|
132
|
-
* engine name
|
|
133
|
-
*/
|
|
134
|
-
readonly name: string;
|
|
135
|
-
/**
|
|
136
|
-
* engine broker
|
|
137
|
-
*/
|
|
138
|
-
broker: ElementBroker<Engine>;
|
|
139
|
-
/**
|
|
140
|
-
* engine state
|
|
141
|
-
*/
|
|
142
|
-
readonly state: BpmnEngineRunningStatus;
|
|
143
|
-
/**
|
|
144
|
-
* boolean stopped
|
|
145
|
-
*/
|
|
146
|
-
readonly stopped: boolean;
|
|
147
|
-
/**
|
|
148
|
-
* current engine execution
|
|
149
|
-
*/
|
|
150
|
-
readonly execution: Execution;
|
|
151
|
-
/**
|
|
152
|
-
* engine environment
|
|
153
|
-
*/
|
|
154
|
-
readonly environment: Environment;
|
|
155
|
-
/**
|
|
156
|
-
* engine logger
|
|
157
|
-
*/
|
|
158
|
-
readonly logger: ILogger;
|
|
159
|
-
get activityStatus(): ActivityStatus;
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* execute definition
|
|
163
|
-
* @param options Optional object with options to override the initial engine options
|
|
164
|
-
* @param cb Callback called before the throw
|
|
165
|
-
* @summary Execute options overrides the initial options passed to the engine before executing the definition.
|
|
166
|
-
*/
|
|
167
|
-
execute(): Promise<Execution>;
|
|
168
|
-
execute(options: BpmnEngineExecuteOptions): Promise<Execution>;
|
|
169
|
-
execute(options: BpmnEngineExecuteOptions, cb: (err: Error, execution?: Execution) => void): Promise<Execution>;
|
|
170
|
-
execute(cb: (err: Error) => void): Promise<Execution>;
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* get definition by id
|
|
174
|
-
* @param id id of definition
|
|
175
|
-
*/
|
|
176
|
-
getDefinitionById(id: string): Promise<Definition>;
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* get all definitions
|
|
180
|
-
*/
|
|
181
|
-
getDefinitions(): Promise<Definition[]>;
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* get execution serialized state
|
|
185
|
-
*/
|
|
186
|
-
getState(): Promise<BpmnEngineExecutionState>;
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Recover engine from state
|
|
190
|
-
* @param state engine state
|
|
191
|
-
* @param recoverOptions optional object with options that will completely override the options passed to the engine at init
|
|
192
|
-
*/
|
|
193
|
-
recover(state: any, recoverOptions?: BpmnEngineOptions): Engine;
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Resume execution function with previously saved engine state.
|
|
197
|
-
* @param options
|
|
198
|
-
* @param callback
|
|
199
|
-
*/
|
|
200
|
-
resume(options?: BpmnEngineExecuteOptions, callback?: (err: Error, execution?: Execution) => void): Promise<Execution>;
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Stop execution. The instance is terminated.
|
|
204
|
-
*/
|
|
205
|
-
stop(): Promise<void>;
|
|
206
|
-
|
|
207
|
-
waitFor<R>(eventName: BpmnEngineEvent): Promise<R>;
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Add definition source by source context.
|
|
211
|
-
* @param options
|
|
212
|
-
*/
|
|
213
|
-
addSource(options?: { sourceContext: any }): void;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
interface BpmnEngineDefinitionState extends DefinitionState {
|
|
217
|
-
/**
|
|
218
|
-
* Serialized moddle-context-serializer context
|
|
219
|
-
*/
|
|
220
|
-
source?: string;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export interface BpmnEngineExecutionState {
|
|
224
|
-
name: string;
|
|
225
|
-
engineVersion: string;
|
|
226
|
-
state: BpmnEngineRunningStatus;
|
|
227
|
-
stopped: boolean;
|
|
228
|
-
environment: EnvironmentState;
|
|
229
|
-
definitions: BpmnEngineDefinitionState[];
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export interface Execution {
|
|
233
|
-
/**
|
|
234
|
-
* engine name
|
|
235
|
-
*
|
|
236
|
-
* @type {string}
|
|
237
|
-
* @
|
|
238
|
-
*/
|
|
239
|
-
readonly name: string;
|
|
240
|
-
/**
|
|
241
|
-
* state of execution, i.e running or idle
|
|
242
|
-
*
|
|
243
|
-
* @type {("running"| "idle")}
|
|
244
|
-
*
|
|
245
|
-
*/
|
|
246
|
-
readonly state: 'running' | 'idle';
|
|
247
|
-
/**
|
|
248
|
-
* is the execution stopped
|
|
249
|
-
*
|
|
250
|
-
* @type {boolean}
|
|
251
|
-
*
|
|
252
|
-
*/
|
|
253
|
-
readonly stopped: boolean;
|
|
254
|
-
/**
|
|
255
|
-
* engine environment
|
|
256
|
-
*
|
|
257
|
-
* @type {Environment}
|
|
258
|
-
*
|
|
259
|
-
*/
|
|
260
|
-
readonly environment: Environment;
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* executing definitions
|
|
264
|
-
*
|
|
265
|
-
* @type {Definition}
|
|
266
|
-
*
|
|
267
|
-
*/
|
|
268
|
-
readonly definitions: Definition[];
|
|
269
|
-
get activityStatus(): ActivityStatus;
|
|
270
|
-
/**
|
|
271
|
-
* Are any definition running
|
|
272
|
-
*/
|
|
273
|
-
get isRunning(): boolean;
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Get activity/element by id. Loops the definitions and returns the first found activity with id.
|
|
277
|
-
* @param activityId Activity or element id
|
|
278
|
-
*/
|
|
279
|
-
getActivityById(activityId: string): ElementBase;
|
|
280
|
-
getActivityById<R>(activityId: string): R;
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* get execution serializable state
|
|
284
|
-
*
|
|
285
|
-
* @returns {BpmnEngineExecutionState}
|
|
286
|
-
*
|
|
287
|
-
*/
|
|
288
|
-
getState(): BpmnEngineExecutionState;
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* stop execution
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*/
|
|
295
|
-
stop(): Promise<void>;
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* get activities in a postponed state
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*/
|
|
302
|
-
getPostponed(): Api<ElementBase>[];
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* send signal to execution, distributed to all definitions
|
|
306
|
-
* Delegate a signal message to all interested parties, usually MessageEventDefinition, SignalEventDefinition, SignalTask (user, manual), ReceiveTask, or a StartEvent that has a form.
|
|
307
|
-
* @param message {BpmnMessage}
|
|
308
|
-
* @param options
|
|
309
|
-
*/
|
|
310
|
-
signal(message?: BpmnMessage, options?: { ignoreSameDefinition?: boolean }): void;
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* send cancel activity to execution, distributed to all definitions
|
|
314
|
-
* Delegate a cancel message to all interested parties, perhaps a stalled TimerEventDefinition.
|
|
315
|
-
* @param message
|
|
316
|
-
*/
|
|
317
|
-
cancelActivity(message?: BpmnMessage): void;
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
*
|
|
321
|
-
* @param event
|
|
322
|
-
*/
|
|
323
|
-
waitFor<T>(event: BpmnEngineEvent): Promise<T>;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
export class JavaScripts implements IScripts {
|
|
327
|
-
/**
|
|
328
|
-
* @param disableDummy Disable returning dummy scripts if script is not found. Dummy script will immediately call next function without error or return value
|
|
329
|
-
*/
|
|
330
|
-
constructor(disableDummy?: boolean);
|
|
331
|
-
register(activity: any): Script | undefined;
|
|
332
|
-
getScript(language: string, identifier: { id: string; [x: string]: any }): Script;
|
|
333
|
-
}
|
|
334
|
-
}
|