@nanobpm/workflow 0.8.2 → 0.9.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.
Files changed (61) hide show
  1. package/dist/declarative.d.ts +172 -10
  2. package/dist/declarative.js +213 -355
  3. package/dist/nodes/branch.d.ts +11 -0
  4. package/dist/nodes/branch.js +40 -0
  5. package/dist/nodes/break.d.ts +8 -0
  6. package/dist/nodes/break.js +19 -0
  7. package/dist/nodes/continue.d.ts +8 -0
  8. package/dist/nodes/continue.js +18 -0
  9. package/dist/nodes/forEach.d.ts +24 -0
  10. package/dist/nodes/forEach.js +104 -0
  11. package/dist/nodes/index.d.ts +11 -0
  12. package/dist/nodes/index.js +18 -0
  13. package/dist/nodes/loop.d.ts +9 -0
  14. package/dist/nodes/loop.js +25 -0
  15. package/dist/nodes/parallel.d.ts +9 -0
  16. package/dist/nodes/parallel.js +40 -0
  17. package/dist/nodes/registry.d.ts +33 -0
  18. package/dist/nodes/registry.js +71 -0
  19. package/dist/nodes/run.d.ts +10 -0
  20. package/dist/nodes/run.js +21 -0
  21. package/dist/nodes/signal.d.ts +11 -0
  22. package/dist/nodes/signal.js +21 -0
  23. package/dist/nodes/switch.d.ts +11 -0
  24. package/dist/nodes/switch.js +51 -0
  25. package/dist/nodes/task.d.ts +11 -0
  26. package/dist/nodes/task.js +21 -0
  27. package/dist/nodes/timer.d.ts +9 -0
  28. package/dist/nodes/timer.js +34 -0
  29. package/dist/test-support/diff.d.ts +8 -0
  30. package/dist/test-support/diff.js +70 -0
  31. package/dist/test-support/index.d.ts +8 -0
  32. package/dist/test-support/index.js +49 -0
  33. package/dist/test-support/normalize.d.ts +19 -0
  34. package/dist/test-support/normalize.js +245 -0
  35. package/dist/test-support/parity.d.ts +28 -0
  36. package/dist/test-support/parity.js +61 -0
  37. package/dist/test-support/xml.d.ts +17 -0
  38. package/dist/test-support/xml.js +135 -0
  39. package/dist/types.d.ts +20 -56
  40. package/package.json +12 -4
  41. package/src/declarative.ts +327 -359
  42. package/src/nodes/README.md +123 -0
  43. package/src/nodes/branch.ts +48 -0
  44. package/src/nodes/break.ts +25 -0
  45. package/src/nodes/continue.ts +24 -0
  46. package/src/nodes/forEach.ts +137 -0
  47. package/src/nodes/index.ts +19 -0
  48. package/src/nodes/loop.ts +34 -0
  49. package/src/nodes/parallel.ts +48 -0
  50. package/src/nodes/registry.ts +105 -0
  51. package/src/nodes/run.ts +29 -0
  52. package/src/nodes/signal.ts +29 -0
  53. package/src/nodes/switch.ts +56 -0
  54. package/src/nodes/task.ts +29 -0
  55. package/src/nodes/timer.ts +42 -0
  56. package/src/test-support/diff.ts +69 -0
  57. package/src/test-support/index.ts +53 -0
  58. package/src/test-support/normalize.ts +274 -0
  59. package/src/test-support/parity.ts +85 -0
  60. package/src/test-support/xml.ts +140 -0
  61. package/src/types.ts +20 -30
@@ -0,0 +1,135 @@
1
+ // A dependency-free XML reader, just enough to parse a BPMN 2.0 document into a
2
+ // tree the normalizer can canonicalize. BPMN files this package emits (and the
3
+ // hand-authored goldens) are plain XML — elements, attributes, text — with no
4
+ // DTDs, processing instructions beyond the `<?xml …?>` prolog, or mixed-content
5
+ // subtleties that would need a full parser. We keep prefixed names verbatim
6
+ // (`bpmn:serviceTask`, `zeebe:taskDefinition`) so the normalizer can reason about
7
+ // namespaces by local name.
8
+ /** The local (un-prefixed) name of an element or attribute. */
9
+ export function localName(qualified) {
10
+ const i = qualified.indexOf(":");
11
+ return i === -1 ? qualified : qualified.slice(i + 1);
12
+ }
13
+ function decodeEntities(s) {
14
+ return s.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (m, body) => {
15
+ switch (body) {
16
+ case "lt":
17
+ return "<";
18
+ case "gt":
19
+ return ">";
20
+ case "amp":
21
+ return "&";
22
+ case "quot":
23
+ return '"';
24
+ case "apos":
25
+ return "'";
26
+ }
27
+ if (body[0] === "#") {
28
+ const code = body[1] === "x" || body[1] === "X" ? Number.parseInt(body.slice(2), 16) : Number.parseInt(body.slice(1), 10);
29
+ // Only decode a valid Unicode scalar value. `String.fromCodePoint` throws
30
+ // a `RangeError` for code points outside 0..0x10FFFF (e.g. `&#x110000;`),
31
+ // and surrogate halves (0xD800..0xDFFF) are not valid characters — treat
32
+ // any such reference as malformed and leave it verbatim rather than
33
+ // throwing an opaque exception out of `parseXml`.
34
+ const isScalar = Number.isInteger(code) && code >= 0 && code <= 0x10ffff && !(code >= 0xd800 && code <= 0xdfff);
35
+ return isScalar ? String.fromCodePoint(code) : m;
36
+ }
37
+ return m;
38
+ });
39
+ }
40
+ function parseAttrs(src) {
41
+ const attrs = {};
42
+ const re = /([^\s=/]+)\s*=\s*("([^"]*)"|'([^']*)')/g;
43
+ let m = re.exec(src);
44
+ while (m !== null) {
45
+ const value = m[3] !== undefined ? m[3] : (m[4] ?? "");
46
+ attrs[m[1]] = decodeEntities(value);
47
+ m = re.exec(src);
48
+ }
49
+ return attrs;
50
+ }
51
+ function appendText(el, chunk) {
52
+ if (chunk)
53
+ el.text = el.text ? `${el.text} ${chunk}` : chunk;
54
+ }
55
+ /** Parse an XML document into its root element. Throws on a malformed document
56
+ * (unbalanced tags, no root, or more than one top-level element). Comments,
57
+ * CDATA, the `<?xml?>` prolog and other processing instructions are handled;
58
+ * CDATA content contributes to `text`. */
59
+ export function parseXml(xml) {
60
+ const root = { name: "#root", attrs: {}, children: [], text: "" };
61
+ const stack = [root];
62
+ let i = 0;
63
+ const n = xml.length;
64
+ while (i < n) {
65
+ const lt = xml.indexOf("<", i);
66
+ if (lt === -1)
67
+ break;
68
+ if (lt > i)
69
+ appendText(stack[stack.length - 1], decodeEntities(xml.slice(i, lt)).trim());
70
+ if (xml.startsWith("<!--", lt)) {
71
+ const end = xml.indexOf("-->", lt + 4);
72
+ if (end === -1)
73
+ throw new Error("parseXml: unterminated comment");
74
+ i = end + 3;
75
+ continue;
76
+ }
77
+ if (xml.startsWith("<![CDATA[", lt)) {
78
+ const end = xml.indexOf("]]>", lt + 9);
79
+ if (end === -1)
80
+ throw new Error("parseXml: unterminated CDATA");
81
+ appendText(stack[stack.length - 1], xml.slice(lt + 9, end).trim());
82
+ i = end + 3;
83
+ continue;
84
+ }
85
+ if (xml.startsWith("<?", lt) || xml.startsWith("<!", lt)) {
86
+ const end = xml.indexOf(">", lt + 2);
87
+ if (end === -1)
88
+ throw new Error("parseXml: unterminated processing instruction / declaration");
89
+ i = end + 1;
90
+ continue;
91
+ }
92
+ const gt = xml.indexOf(">", lt);
93
+ if (gt === -1)
94
+ throw new Error("parseXml: unterminated tag");
95
+ let inner = xml.slice(lt + 1, gt);
96
+ if (inner.startsWith("/")) {
97
+ const closeName = inner.slice(1).trim();
98
+ if (stack.length <= 1)
99
+ throw new Error(`parseXml: unbalanced end tag </${closeName}>`);
100
+ const open = stack.pop();
101
+ if (open === undefined || open.name !== closeName) {
102
+ throw new Error(`parseXml: mismatched end tag </${closeName}> (expected </${open?.name ?? "#root"}>)`);
103
+ }
104
+ i = gt + 1;
105
+ continue;
106
+ }
107
+ const selfClosing = inner.endsWith("/");
108
+ if (selfClosing)
109
+ inner = inner.slice(0, -1);
110
+ const nameMatch = /^\s*(\S+)/.exec(inner);
111
+ if (!nameMatch)
112
+ throw new Error("parseXml: malformed start tag");
113
+ const name = nameMatch[1];
114
+ const attrs = parseAttrs(inner.slice(nameMatch[0].length));
115
+ const el = { name, attrs, children: [], text: "" };
116
+ stack[stack.length - 1].children.push(el);
117
+ if (!selfClosing)
118
+ stack.push(el);
119
+ i = gt + 1;
120
+ }
121
+ if (stack.length !== 1) {
122
+ const unclosed = stack
123
+ .slice(1)
124
+ .map((el) => `<${el.name}>`)
125
+ .join(", ");
126
+ throw new Error(`parseXml: unclosed tag(s): ${unclosed}`);
127
+ }
128
+ if (root.children.length === 0)
129
+ throw new Error("parseXml: no root element");
130
+ if (root.children.length > 1) {
131
+ const roots = root.children.map((el) => `<${el.name}>`).join(", ");
132
+ throw new Error(`parseXml: multiple root elements (${roots}) — a well-formed document has exactly one`);
133
+ }
134
+ return root.children[0];
135
+ }
package/dist/types.d.ts CHANGED
@@ -37,62 +37,26 @@ export type FlowContracts = Record<string, StepContract>;
37
37
  export type StepHandler<V extends JsonObject = JsonObject, R extends JsonObject = JsonObject> = {
38
38
  bivarianceHack(job: Job<V>): Promise<R | void> | R | void;
39
39
  }["bivarianceHack"];
40
- /** A node in a declarative flow tree. Leaf activities carry optional data
41
- * envelopes (lifted to `nano:shape` + `dataEnvelope` in the model); structural
42
- * combinators carry nested `FlowNode[]` bodies. */
43
- export type FlowNode = {
44
- kind: "run";
45
- name: string;
46
- envelopes?: NodeEnvelopes;
47
- } | {
48
- kind: "task";
49
- name: string;
50
- envelopes?: NodeEnvelopes;
51
- jobType?: string;
52
- } | {
53
- kind: "signal";
54
- name: string;
55
- correlationKey: string;
56
- payload?: Envelope;
57
- } | ({
58
- kind: "timer";
59
- name: string;
60
- } & TimerAt) | {
61
- kind: "switch";
62
- subject: string;
63
- cases: SwitchCase[];
64
- default?: FlowNode[];
65
- } | {
66
- kind: "branch";
67
- condition: string;
68
- then: FlowNode[];
69
- else?: FlowNode[];
70
- } | {
71
- kind: "loop";
72
- body: FlowNode[];
73
- } | {
74
- kind: "break";
75
- } | {
76
- kind: "continue";
77
- } | {
78
- kind: "parallel";
79
- branches: FlowNode[][];
80
- } | {
81
- kind: "forEach";
82
- /** FEEL expression evaluating to the list to fan out over. */
83
- collection: string;
84
- /** Variable each item is bound to in the child's scope (`inputElement`). */
85
- itemVar: string;
86
- body: FlowNode[];
87
- /** Run children one at a time (sequential MI) instead of all at once. */
88
- sequential?: boolean;
89
- /** List variable that each child's `outputElement` is collected into. */
90
- outputCollection?: string;
91
- /** FEEL expression producing each child's contribution to `outputCollection`. */
92
- outputElement?: string;
93
- /** FEEL boolean that, once true, completes the body early. */
94
- completionCondition?: string;
95
- };
40
+ /** The OPEN registry of flow-node kinds — the single extension seam for the
41
+ * `FlowNode` union (epic #314, S0/#315). Each kind module under `src/nodes/`
42
+ * declaration-merges ONE property into this interface (`kindName: NodeShape`),
43
+ * so `FlowNode` grows without editing a central union here. The built-in kinds
44
+ * register `run`/`task`/`signal`/`timer`/`switch`/`branch`/`loop`/`break`/
45
+ * `continue`/`parallel`/`forEach` from their own modules through this same
46
+ * mechanism — see `src/nodes/README.md`.
47
+ *
48
+ * Invariant: every value type MUST be an object with a `kind` discriminant
49
+ * equal to its key, so `FlowNode` stays a discriminated union.
50
+ *
51
+ * This interface is intentionally empty here: it is the augmentation target the
52
+ * per-kind modules declaration-merge their variant into. */
53
+ export interface FlowNodeRegistry {
54
+ }
55
+ /** A node in a declarative flow tree, derived from the {@link FlowNodeRegistry}.
56
+ * Leaf activities carry optional data envelopes (lifted to `nano:shape` +
57
+ * `dataEnvelope` in the model); structural combinators carry nested
58
+ * `FlowNode[]` bodies. */
59
+ export type FlowNode = FlowNodeRegistry[keyof FlowNodeRegistry];
96
60
  /** A timer intermediate-catch definition: exactly one of `after` (an ISO-8601
97
61
  * delay or FEEL expression) or `at` (an absolute instant or FEEL expression).
98
62
  * Encoded as an XOR so downstream consumers can rely on exactly-one. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanobpm/workflow",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "Code-first durable orchestration for nanobpmn (ADR 0044/0045). Author a durable workflow as a declarative flow — `defineFlow(id, w => { w.run(...); w.signal(...); w.task(...) })` — and the SDK derives the executable BPMN model, the job types, and the message/correlation wiring, then hosts a generic worker. `w.run` is a local step, `w.signal` is a durable human-in-the-loop wait, `w.task` is a step serviced by an external worker. `defineFlow` is the single code-first surface; the imperative `defineWorkflow` replay function is experimental/internal, not a recommended API. No diagram, no task-type wiring, no correlation plumbing authored by hand — and you can optionally auto-generate a diagram (DI) for the derived model with `declarativeToLayoutedBpmn` via the optional `bpmn-auto-layout` peer dependency. Talks to a running nanobpmn gateway through @nanobpm/nano-sdk (the shared engine-transport spine — ADR 0055), which negotiates the Falcon protocol on a Nano server and REST elsewhere.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -17,6 +17,10 @@
17
17
  "types": "./dist/index.d.ts",
18
18
  "default": "./dist/index.js"
19
19
  },
20
+ "./test-support": {
21
+ "types": "./dist/test-support/index.d.ts",
22
+ "default": "./dist/test-support/index.js"
23
+ },
20
24
  "./source": "./src/index.ts"
21
25
  },
22
26
  "files": [
@@ -26,13 +30,17 @@
26
30
  "engines": {
27
31
  "node": ">=22.6"
28
32
  },
29
- "sideEffects": false,
33
+ "sideEffects": [
34
+ "./dist/nodes/*.js",
35
+ "./src/nodes/*.ts"
36
+ ],
30
37
  "dependencies": {
31
38
  "@nanobpm/nano-sdk": "^1.2.5"
32
39
  },
33
40
  "scripts": {
34
- "build": "tsc -p tsconfig.json",
35
- "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.types.json",
41
+ "gen:nodes": "node scripts/gen-node-registry.mjs",
42
+ "build": "npm run gen:nodes && tsc -p tsconfig.json",
43
+ "typecheck": "npm run gen:nodes && tsc -p tsconfig.json --noEmit && tsc -p tsconfig.types.json",
36
44
  "test": "npm run build && npm run test:ci",
37
45
  "test:ci": "node --experimental-strip-types --test test/*.test.ts",
38
46
  "test:integration": "node --experimental-strip-types --test test/integration.test.ts",