@nanobpm/engine-wasm 0.4.0 → 0.5.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/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # @nanobpm/engine-wasm
2
+
3
+ The nanobpmn engine (`engine-core`) compiled to **WebAssembly** for in-browser
4
+ BPMN execution. Deploy a diagram, start instances, activate/complete jobs, drive
5
+ the virtual clock, and read live snapshots/traces — all client-side, with no
6
+ gateway. It is the substrate for the [Bojtos](https://github.com/nanobpm/bojtos)
7
+ demo framework (ADR 0043) and the console test-run panel.
8
+
9
+ The package ships **two independent engines** from one install, selectable by
10
+ **import path**:
11
+
12
+ | Import | Engine | Wire size (brotli) | Use it for |
13
+ | --- | --- | ---: | --- |
14
+ | `@nanobpm/engine-wasm` | **lean** — primary state only | ~252 KB | demos, the modeler, debuggers, anything that reads via `snapshot()` / `events()` |
15
+ | `@nanobpm/engine-wasm/readmodel` | **read-model** — lean surface **+** the gateway's C8-style REST read methods | ~561 KB | full-surface app testing in CI that wants gateway read parity |
16
+
17
+ ## Why two binaries (not a runtime toggle)
18
+
19
+ Each variant is its own `wasm-pack --target web` JS glue + `_bg.wasm`. A bundler
20
+ follows the static module graph, so it only emits the wasm for the subpath you
21
+ actually import — wasm **cannot** be tree-shaken out of a single fat build. So:
22
+
23
+ - Lean-only pages pay **zero** read-model weight (the +309 KB brotli never lands).
24
+ - You can `await import("@nanobpm/engine-wasm/readmodel")` to code-split the
25
+ heavy engine behind a runtime decision.
26
+ - You can even instantiate **both** on one page.
27
+
28
+ The read-model variant is the lean engine **plus** an in-memory wasm SQLite read
29
+ model (the same projection the gateway serves from), exposing the REST read
30
+ surface the lean engine lacks.
31
+
32
+ ## Install
33
+
34
+ ```sh
35
+ npm install @nanobpm/engine-wasm
36
+ ```
37
+
38
+ ## Usage — lean (default)
39
+
40
+ ```js
41
+ import init, { TestEngine } from "@nanobpm/engine-wasm";
42
+
43
+ await init(); // instantiate the wasm module
44
+ const engine = new TestEngine();
45
+
46
+ engine.deploy(bpmnXml); // deploy a BPMN diagram
47
+ const handle = engine.createInstance("my-process", "{}");
48
+ const snap = JSON.parse(engine.snapshot()); // live primary state
49
+ const trace = JSON.parse(engine.events()); // the event log
50
+ engine.reset(); // back to a clean engine
51
+ ```
52
+
53
+ The lean surface covers primary state and execution: `deploy`, `createInstance`,
54
+ `snapshot`, `events`, job ops (`activateJobs`, `completeJob`, `failJob`,
55
+ `updateRetries`, `throwError`), user-task ops (`completeUserTask`,
56
+ `assignUserTask`, …), messaging/signals, the virtual clock (`advanceTime`,
57
+ `tickNow`), and `reset`. It links **no** SQLite / read-model code.
58
+
59
+ ## Usage — read-model
60
+
61
+ ```js
62
+ import init, { TestEngine } from "@nanobpm/engine-wasm/readmodel";
63
+
64
+ await init();
65
+ const engine = new TestEngine();
66
+
67
+ engine.deploy(bpmnXml);
68
+ engine.createInstance("my-process", "{}");
69
+
70
+ // Same lean surface as above, PLUS the gateway's REST read channel:
71
+ const form = JSON.parse(engine.getFormByKey("2251799813685250"));
72
+ const open = JSON.parse(engine.searchUserTasks(JSON.stringify({ state: "CREATED" })));
73
+ const insts = JSON.parse(engine.searchProcessInstances("{}"));
74
+ const res = JSON.parse(engine.getResourceByKey("2251799813685251"));
75
+ const vars = JSON.parse(engine.searchVariables("{}"));
76
+ ```
77
+
78
+ ### Read methods (read-model subpath only)
79
+
80
+ Each returns the same JSON shapes the Camunda v2 REST surface returns; each
81
+ delegates to the in-memory read model, kept current after every command and
82
+ cleared by `reset()`.
83
+
84
+ - **`getFormByKey(formKey)`** → the latest deployed form for `formKey`
85
+ (`{ tenantId, formId, schema, version, formKey }`), or `null`.
86
+ Mirrors `GET /forms/{formKey}`.
87
+ - **`searchUserTasks(filterJson)`** → `{ items, page }`. Honours an optional
88
+ `{ state? }` filter (e.g. `"CREATED"`) through the read model.
89
+ Mirrors `POST /user-tasks/search`.
90
+ - **`searchProcessInstances(filterJson)`** → `{ items, page }`. Body is
91
+ shape-validated; filter/sort/page fields are not yet honoured (returns every
92
+ instance). Mirrors `POST /process-instances/search`.
93
+ - **`getResourceByKey(resourceKey)`** → the generic resource, or `null`.
94
+ Mirrors `GET /resources/{resourceKey}`.
95
+ - **`searchVariables(filterJson)`** → `{ items, page }`. Body is shape-validated;
96
+ long values are truncated with `isTruncated: true`.
97
+ Mirrors `POST /variables/search`.
98
+
99
+ ## Choosing a subpath
100
+
101
+ - **Reach for lean** for demos, the modeler, debuggers/adapters, and anything
102
+ that only inspects `snapshot()` / `events()`. Keeping these paths lean is the
103
+ whole point of the split — the +309 KB brotli must never ship in a demo page.
104
+ - **Reach for `/readmodel`** when you need gateway read parity — full-surface app
105
+ testing in CI — instead of hand-rolling shadow read stores in JS/TS.
106
+
107
+ ## References
108
+
109
+ - Epic [#796](https://github.com/Magikcraft/nano-bpm/issues/796) — the read
110
+ channel via a wasm SQLite read model (size table, toolchain note, downstream
111
+ consumers).
112
+ - ADR 0043 — Bojtos (the demo framework this engine backs).
@@ -83,11 +83,13 @@ export class TestEngine {
83
83
  correlateMessage(message_name: string, correlation_key: string, variables_json: string): string;
84
84
  /**
85
85
  * Start a new instance of `process_id`, seeding it with the given variables
86
- * (a JSON object string; pass `"{}"` or `""` for none). Returns the
87
- * post-run [`Snapshot`] with a top-level `created` field holding the new
88
- * instance key.
86
+ * (a JSON object string; pass `"{}"` or `""` for none). `version` selects a
87
+ * specific process **version** number (Zeebe by-id semantics); pass
88
+ * `undefined`/`null` (or a non-positive value) for the latest version.
89
+ * Returns the post-run [`Snapshot`] with a top-level `created` field holding
90
+ * the new instance key.
89
91
  */
90
- createInstance(process_id: string, variables_json: string): string;
92
+ createInstance(process_id: string, variables_json: string, version?: number | null): string;
91
93
  /**
92
94
  * Stop debugging, keeping the state the run produced. If the run is paused
93
95
  * mid-command, that in-flight command is first **finished normally** (its
@@ -143,6 +145,15 @@ export class TestEngine {
143
145
  * With no retries left this raises an incident (visible in the snapshot).
144
146
  */
145
147
  failJob(job_key: string, retries: number, message: string): string;
148
+ /**
149
+ * Migrate a running process instance to a target process definition (Zeebe
150
+ * "migrate process instance"): re-point every active element instance at the
151
+ * mapped element of `target_process_definition_key` and re-home the instance.
152
+ * `mapping_instructions_json` is a JSON array of
153
+ * `{ sourceElementId: string, targetElementId: string }`. Returns the
154
+ * snapshot.
155
+ */
156
+ migrate(instance_key: string, target_process_definition_key: string, mapping_instructions_json: string): string;
146
157
  /**
147
158
  * Modify a running process instance (Zeebe "modify process instance"): move
148
159
  * tokens by terminating existing element instances and/or activating new
@@ -242,7 +253,7 @@ export interface InitOutput {
242
253
  readonly testengine_completeJob: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
243
254
  readonly testengine_completeUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
244
255
  readonly testengine_correlateMessage: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
245
- readonly testengine_createInstance: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
256
+ readonly testengine_createInstance: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
246
257
  readonly testengine_debugClear: (a: number) => void;
247
258
  readonly testengine_debugCreateInstance: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
248
259
  readonly testengine_debugIsPaused: (a: number) => number;
@@ -251,6 +262,7 @@ export interface InitOutput {
251
262
  readonly testengine_deploy: (a: number, b: number, c: number, d: number) => void;
252
263
  readonly testengine_events: (a: number, b: number) => void;
253
264
  readonly testengine_failJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
265
+ readonly testengine_migrate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
254
266
  readonly testengine_modify: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
255
267
  readonly testengine_new: () => number;
256
268
  readonly testengine_now: (a: number) => number;
@@ -361,14 +361,17 @@ export class TestEngine {
361
361
  }
362
362
  /**
363
363
  * Start a new instance of `process_id`, seeding it with the given variables
364
- * (a JSON object string; pass `"{}"` or `""` for none). Returns the
365
- * post-run [`Snapshot`] with a top-level `created` field holding the new
366
- * instance key.
364
+ * (a JSON object string; pass `"{}"` or `""` for none). `version` selects a
365
+ * specific process **version** number (Zeebe by-id semantics); pass
366
+ * `undefined`/`null` (or a non-positive value) for the latest version.
367
+ * Returns the post-run [`Snapshot`] with a top-level `created` field holding
368
+ * the new instance key.
367
369
  * @param {string} process_id
368
370
  * @param {string} variables_json
371
+ * @param {number | null} [version]
369
372
  * @returns {string}
370
373
  */
371
- createInstance(process_id, variables_json) {
374
+ createInstance(process_id, variables_json, version) {
372
375
  let deferred4_0;
373
376
  let deferred4_1;
374
377
  try {
@@ -377,7 +380,7 @@ export class TestEngine {
377
380
  const len0 = WASM_VECTOR_LEN;
378
381
  const ptr1 = passStringToWasm0(variables_json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
379
382
  const len1 = WASM_VECTOR_LEN;
380
- wasm.testengine_createInstance(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
383
+ wasm.testengine_createInstance(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, isLikeNone(version) ? Number.MAX_SAFE_INTEGER : (version) >> 0);
381
384
  var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
382
385
  var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
383
386
  var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
@@ -624,6 +627,48 @@ export class TestEngine {
624
627
  wasm.__wbindgen_export3(deferred4_0, deferred4_1, 1);
625
628
  }
626
629
  }
630
+ /**
631
+ * Migrate a running process instance to a target process definition (Zeebe
632
+ * "migrate process instance"): re-point every active element instance at the
633
+ * mapped element of `target_process_definition_key` and re-home the instance.
634
+ * `mapping_instructions_json` is a JSON array of
635
+ * `{ sourceElementId: string, targetElementId: string }`. Returns the
636
+ * snapshot.
637
+ * @param {string} instance_key
638
+ * @param {string} target_process_definition_key
639
+ * @param {string} mapping_instructions_json
640
+ * @returns {string}
641
+ */
642
+ migrate(instance_key, target_process_definition_key, mapping_instructions_json) {
643
+ let deferred5_0;
644
+ let deferred5_1;
645
+ try {
646
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
647
+ const ptr0 = passStringToWasm0(instance_key, wasm.__wbindgen_export, wasm.__wbindgen_export2);
648
+ const len0 = WASM_VECTOR_LEN;
649
+ const ptr1 = passStringToWasm0(target_process_definition_key, wasm.__wbindgen_export, wasm.__wbindgen_export2);
650
+ const len1 = WASM_VECTOR_LEN;
651
+ const ptr2 = passStringToWasm0(mapping_instructions_json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
652
+ const len2 = WASM_VECTOR_LEN;
653
+ wasm.testengine_migrate(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
654
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
655
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
656
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
657
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
658
+ var ptr4 = r0;
659
+ var len4 = r1;
660
+ if (r3) {
661
+ ptr4 = 0; len4 = 0;
662
+ throw takeObject(r2);
663
+ }
664
+ deferred5_0 = ptr4;
665
+ deferred5_1 = len4;
666
+ return getStringFromWasm0(ptr4, len4);
667
+ } finally {
668
+ wasm.__wbindgen_add_to_stack_pointer(16);
669
+ wasm.__wbindgen_export3(deferred5_0, deferred5_1, 1);
670
+ }
671
+ }
627
672
  /**
628
673
  * Modify a running process instance (Zeebe "modify process instance"): move
629
674
  * tokens by terminating existing element instances and/or activating new
@@ -977,7 +1022,7 @@ if (Symbol.dispose) TestEngine.prototype[Symbol.dispose] = TestEngine.prototype.
977
1022
  function __wbg_get_imports() {
978
1023
  const import0 = {
979
1024
  __proto__: null,
980
- __wbg___wbindgen_throw_ea4887a5f8f9a9db: function(arg0, arg1) {
1025
+ __wbg___wbindgen_throw_bb96b2010945f0bc: function(arg0, arg1) {
981
1026
  throw new Error(getStringFromWasm0(arg0, arg1));
982
1027
  },
983
1028
  __wbindgen_cast_0000000000000001: function(arg0, arg1) {
@@ -1041,6 +1086,10 @@ heap.push(undefined, null, true, false);
1041
1086
 
1042
1087
  let heap_next = heap.length;
1043
1088
 
1089
+ function isLikeNone(x) {
1090
+ return x === undefined || x === null;
1091
+ }
1092
+
1044
1093
  function passStringToWasm0(arg, malloc, realloc) {
1045
1094
  if (realloc === undefined) {
1046
1095
  const buf = cachedTextEncoder.encode(arg);
@@ -1125,11 +1174,15 @@ function __wbg_finalize_init(instance, module) {
1125
1174
 
1126
1175
  async function __wbg_load(module, imports) {
1127
1176
  if (typeof Response === 'function' && module instanceof Response) {
1177
+ if (!module.ok) {
1178
+ throw new Error(`failed to fetch Wasm: ${module.status} ${module.statusText} fetching '${module.url}'`);
1179
+ }
1180
+
1128
1181
  if (typeof WebAssembly.instantiateStreaming === 'function') {
1129
1182
  try {
1130
1183
  return await WebAssembly.instantiateStreaming(module, imports);
1131
1184
  } catch (e) {
1132
- const validResponse = module.ok && expectedResponseType(module.type);
1185
+ const validResponse = expectedResponseType(module.type);
1133
1186
 
1134
1187
  if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
1135
1188
  console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
Binary file
@@ -11,7 +11,7 @@ export const testengine_completeAgentJob: (a: number, b: number, c: number, d: n
11
11
  export const testengine_completeJob: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
12
12
  export const testengine_completeUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
13
13
  export const testengine_correlateMessage: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
14
- export const testengine_createInstance: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
14
+ export const testengine_createInstance: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
15
15
  export const testengine_debugClear: (a: number) => void;
16
16
  export const testengine_debugCreateInstance: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
17
17
  export const testengine_debugIsPaused: (a: number) => number;
@@ -20,6 +20,7 @@ export const testengine_debugStep: (a: number, b: number) => void;
20
20
  export const testengine_deploy: (a: number, b: number, c: number, d: number) => void;
21
21
  export const testengine_events: (a: number, b: number) => void;
22
22
  export const testengine_failJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
23
+ export const testengine_migrate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
23
24
  export const testengine_modify: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
24
25
  export const testengine_new: () => number;
25
26
  export const testengine_now: (a: number) => number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nanobpm/engine-wasm",
3
- "version": "0.4.0",
4
- "description": "The nanobpmn engine (engine-core) compiled to WebAssembly for in-browser BPMN execution: deploy a diagram, start instances, activate/complete jobs, and read live snapshots/traces. The substrate for the Bojtos demo framework (ADR 0043) and the console test-run panel.",
3
+ "version": "0.5.0",
4
+ "description": "The nanobpmn engine (engine-core) compiled to WebAssembly for in-browser BPMN execution: deploy a diagram, start instances, activate/complete jobs, and read live snapshots/traces. Published as two subpath entrypoints — a lean baseline engine (default) and a read-model engine (the gateway's C8-style REST read surface). The substrate for the Bojtos demo framework (ADR 0043) and the console test-run panel.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
7
7
  "repository": {
@@ -9,21 +9,31 @@
9
9
  "url": "https://github.com/Magikcraft/nano-bpm",
10
10
  "directory": "engine-wasm/pkg"
11
11
  },
12
- "main": "./nanobpmn_engine.js",
13
- "module": "./nanobpmn_engine.js",
14
- "types": "./nanobpmn_engine.d.ts",
12
+ "main": "./lean/nanobpmn_engine.js",
13
+ "module": "./lean/nanobpmn_engine.js",
14
+ "types": "./lean/nanobpmn_engine.d.ts",
15
15
  "exports": {
16
16
  ".": {
17
- "types": "./nanobpmn_engine.d.ts",
18
- "default": "./nanobpmn_engine.js"
17
+ "types": "./lean/nanobpmn_engine.d.ts",
18
+ "default": "./lean/nanobpmn_engine.js"
19
19
  },
20
- "./nanobpmn_engine_bg.wasm": "./nanobpmn_engine_bg.wasm"
20
+ "./readmodel": {
21
+ "types": "./readmodel/nanobpmn_engine.d.ts",
22
+ "default": "./readmodel/nanobpmn_engine.js"
23
+ },
24
+ "./lean/nanobpmn_engine_bg.wasm": "./lean/nanobpmn_engine_bg.wasm",
25
+ "./readmodel/nanobpmn_engine_bg.wasm": "./readmodel/nanobpmn_engine_bg.wasm"
21
26
  },
22
27
  "files": [
23
- "nanobpmn_engine.js",
24
- "nanobpmn_engine.d.ts",
25
- "nanobpmn_engine_bg.wasm",
26
- "nanobpmn_engine_bg.wasm.d.ts"
28
+ "lean/nanobpmn_engine.js",
29
+ "lean/nanobpmn_engine.d.ts",
30
+ "lean/nanobpmn_engine_bg.wasm",
31
+ "lean/nanobpmn_engine_bg.wasm.d.ts",
32
+ "readmodel/nanobpmn_engine.js",
33
+ "readmodel/nanobpmn_engine.d.ts",
34
+ "readmodel/nanobpmn_engine_bg.wasm",
35
+ "readmodel/nanobpmn_engine_bg.wasm.d.ts",
36
+ "README.md"
27
37
  ],
28
38
  "sideEffects": [
29
39
  "./snippets/*"