@bpmnkit/core 1.0.0 → 1.2.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 +30 -3
- package/dist/bpmn/agentic.js +3 -1
- package/dist/bpmn/auto-layout.js +184 -15
- package/dist/bpmn/bpmn-builder.js +69 -7
- package/dist/bpmn/bpmn-model.d.ts +78 -0
- package/dist/bpmn/bpmn-parser.js +195 -37
- package/dist/bpmn/bpmn-serializer.js +121 -72
- package/dist/bpmn/bpmnlint.d.ts +153 -0
- package/dist/bpmn/bpmnlint.js +364 -0
- package/dist/bpmn/camunda-compat-data.d.ts +165 -0
- package/dist/bpmn/camunda-compat-data.js +423 -0
- package/dist/bpmn/camunda-compat-feel.d.ts +68 -0
- package/dist/bpmn/camunda-compat-feel.js +338 -0
- package/dist/bpmn/camunda-compat.d.ts +120 -0
- package/dist/bpmn/camunda-compat.js +1677 -0
- package/dist/bpmn/camunda7/juel.d.ts +41 -0
- package/dist/bpmn/camunda7/juel.js +333 -0
- package/dist/bpmn/camunda7/migrate.d.ts +72 -0
- package/dist/bpmn/camunda7/migrate.js +1152 -0
- package/dist/bpmn/compact.d.ts +4 -0
- package/dist/bpmn/compact.js +80 -0
- package/dist/bpmn/full-operations.js +5 -2
- package/dist/bpmn/label-style.d.ts +51 -0
- package/dist/bpmn/label-style.js +116 -0
- package/dist/bpmn/lint.d.ts +23 -3
- package/dist/bpmn/lint.js +47 -10
- package/dist/bpmn/optimize/bpmnlint-rules.d.ts +31 -0
- package/dist/bpmn/optimize/bpmnlint-rules.js +916 -0
- package/dist/bpmn/optimize/deploy.d.ts +8 -2
- package/dist/bpmn/optimize/deploy.js +16 -3
- package/dist/bpmn/optimize/index.js +11 -2
- package/dist/bpmn/optimize/patterns.js +3 -0
- package/dist/bpmn/optimize/types.d.ts +14 -0
- package/dist/bpmn/optimize/variable-flow.js +4 -1
- package/dist/bpmn/process-delta.d.ts +88 -0
- package/dist/bpmn/process-delta.js +236 -0
- package/dist/bpmn/process-text-writer.d.ts +53 -0
- package/dist/bpmn/process-text-writer.js +193 -0
- package/dist/bpmn/process-text.d.ts +175 -0
- package/dist/bpmn/process-text.js +1255 -0
- package/dist/bpmn/process-types.d.ts +112 -0
- package/dist/bpmn/process-types.js +490 -0
- package/dist/bpmn/svg.js +51 -22
- package/dist/doc/docx.d.ts +20 -0
- package/dist/doc/docx.js +253 -0
- package/dist/doc/html.d.ts +21 -0
- package/dist/doc/html.js +303 -0
- package/dist/doc/index.d.ts +7 -0
- package/dist/doc/index.js +5 -0
- package/dist/doc/markdown.d.ts +11 -0
- package/dist/doc/markdown.js +160 -0
- package/dist/doc/model.d.ts +129 -0
- package/dist/doc/model.js +617 -0
- package/dist/doc/zip.d.ts +5 -0
- package/dist/doc/zip.js +75 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +11 -0
- package/dist/layout/annotations.d.ts +4 -2
- package/dist/layout/annotations.js +31 -2
- package/dist/layout/collaboration/ordering.d.ts +8 -0
- package/dist/layout/collaboration/ordering.js +57 -18
- package/dist/layout/orthogonal.d.ts +32 -0
- package/dist/layout/orthogonal.js +367 -0
- package/dist/layout/repair.d.ts +31 -0
- package/dist/layout/repair.js +220 -0
- package/dist/layout/semantic/route.js +45 -3
- package/dist/node/bpmnlint.d.ts +109 -0
- package/dist/node/bpmnlint.js +198 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.js +1 -0
- package/dist/xml/xml-patch.d.ts +21 -0
- package/dist/xml/xml-patch.js +27 -1
- package/package.json +2 -2
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed code generation from BPMN — the contract between a process model and
|
|
3
|
+
* the job workers, message publishers and error throwers written against it.
|
|
4
|
+
*
|
|
5
|
+
* {@link extractProcessContract} reads what a set of definitions promises
|
|
6
|
+
* (process ids, job types with their variables and headers, message / signal /
|
|
7
|
+
* error / escalation names) and {@link generateProcessTypes} renders it as
|
|
8
|
+
* TypeScript source.
|
|
9
|
+
*
|
|
10
|
+
* The types are necessarily approximate — FEEL cannot be fully typed — so
|
|
11
|
+
* every value is `unknown`, but the keys are exact. The rules:
|
|
12
|
+
*
|
|
13
|
+
* - **Jobs** are every element carrying `zeebe:taskDefinition` with a static
|
|
14
|
+
* type: service, send, script and business rule tasks implemented by a job
|
|
15
|
+
* worker, message throw events, and ad-hoc sub-processes (AI Agent job
|
|
16
|
+
* workers). User tasks are left out: Camunda user tasks have no job, and
|
|
17
|
+
* job-based user tasks all share the engine's own type. A type written as a
|
|
18
|
+
* FEEL expression (`=…`) is dynamic and is skipped.
|
|
19
|
+
* - **Variables** (what the worker receives): the targets of the element's
|
|
20
|
+
* `zeebe:input` mappings, all required. With no input mappings, the
|
|
21
|
+
* variables the variable-flow analysis finds in scope on the element's
|
|
22
|
+
* incoming flows (other elements' input targets are local to them and left
|
|
23
|
+
* out), all optional.
|
|
24
|
+
* - **Output** (what the worker should complete with): the root variables the
|
|
25
|
+
* element's `zeebe:output` sources read, minus its own input targets —
|
|
26
|
+
* required when the source is a plain reference (`=total`), optional
|
|
27
|
+
* otherwise. With no output mappings, the variables read downstream that
|
|
28
|
+
* nothing in the process sets, all optional.
|
|
29
|
+
* - **Headers**: `zeebe:taskHeaders` with their literal values; a key missing
|
|
30
|
+
* from some of the elements sharing the job type is optional.
|
|
31
|
+
* - **Errors** a job can throw: the codes caught by error boundary events on the
|
|
32
|
+
* element or its enclosing sub-processes, and by error event sub-processes in
|
|
33
|
+
* those scopes. A catch-all error event widens this to `string`; no catch at
|
|
34
|
+
* all gives `never` (an uncaught error raises an incident).
|
|
35
|
+
* - Elements sharing a job type are merged: a key is required only when every
|
|
36
|
+
* element requires it.
|
|
37
|
+
* - Only executable processes (`isExecutable="true"`) are read.
|
|
38
|
+
*/
|
|
39
|
+
import type { BpmnDefinitions, BpmnFlowElement } from "./bpmn-model.js";
|
|
40
|
+
/** A variable key in a job contract. */
|
|
41
|
+
export interface ContractVariable {
|
|
42
|
+
name: string;
|
|
43
|
+
/** False when the model does not guarantee the key is present. */
|
|
44
|
+
required: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** A task header key and the literal values it takes across elements. */
|
|
47
|
+
export interface ContractHeader {
|
|
48
|
+
key: string;
|
|
49
|
+
values: string[];
|
|
50
|
+
required: boolean;
|
|
51
|
+
}
|
|
52
|
+
/** Where a job type, message, signal, error or escalation is used. */
|
|
53
|
+
export interface ContractElementRef {
|
|
54
|
+
processId: string;
|
|
55
|
+
elementId: string;
|
|
56
|
+
elementType: BpmnFlowElement["type"];
|
|
57
|
+
}
|
|
58
|
+
/** Everything a worker for one job type can rely on. */
|
|
59
|
+
export interface JobContract {
|
|
60
|
+
type: string;
|
|
61
|
+
elements: ContractElementRef[];
|
|
62
|
+
variables: ContractVariable[];
|
|
63
|
+
output: ContractVariable[];
|
|
64
|
+
headers: ContractHeader[];
|
|
65
|
+
/** Error codes a worker may throw and have caught; `"any"` when a catch-all exists. */
|
|
66
|
+
errors: string[] | "any";
|
|
67
|
+
}
|
|
68
|
+
/** A message name and the correlation keys its catch events subscribe with. */
|
|
69
|
+
export interface MessageContract {
|
|
70
|
+
name: string;
|
|
71
|
+
correlationKeys: string[];
|
|
72
|
+
elements: ContractElementRef[];
|
|
73
|
+
}
|
|
74
|
+
/** A signal name, error code or escalation code. */
|
|
75
|
+
export interface CodeContract {
|
|
76
|
+
code: string;
|
|
77
|
+
elements: ContractElementRef[];
|
|
78
|
+
}
|
|
79
|
+
/** The contract a set of BPMN definitions promises its workers and clients. */
|
|
80
|
+
export interface ProcessContract {
|
|
81
|
+
processIds: string[];
|
|
82
|
+
jobs: JobContract[];
|
|
83
|
+
messages: MessageContract[];
|
|
84
|
+
signals: CodeContract[];
|
|
85
|
+
errors: CodeContract[];
|
|
86
|
+
escalations: CodeContract[];
|
|
87
|
+
/** Job types written as FEEL expressions — resolved at runtime, so not typed. */
|
|
88
|
+
dynamicJobTypes: ContractElementRef[];
|
|
89
|
+
}
|
|
90
|
+
/** Options for {@link generateProcessTypes}. */
|
|
91
|
+
export interface ProcessTypesOptions {
|
|
92
|
+
/** First line of the header comment. Default: "generated by casen gen types — do not edit". */
|
|
93
|
+
banner?: string;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Read the worker-facing contract out of one or more BPMN definitions. Pure and
|
|
97
|
+
* deterministic: every list is sorted.
|
|
98
|
+
*/
|
|
99
|
+
export declare function extractProcessContract(definitions: BpmnDefinitions | readonly BpmnDefinitions[]): ProcessContract;
|
|
100
|
+
/**
|
|
101
|
+
* Render the {@link ProcessContract} of one or more BPMN definitions as
|
|
102
|
+
* TypeScript source. The output is deterministic (everything sorted) and
|
|
103
|
+
* starts with a "do not edit" header.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* import { Bpmn, generateProcessTypes } from "@bpmnkit/core"
|
|
108
|
+
* const source = generateProcessTypes(Bpmn.parse(xml))
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function generateProcessTypes(definitions: BpmnDefinitions | readonly BpmnDefinitions[], options?: ProcessTypesOptions): string;
|
|
112
|
+
//# sourceMappingURL=process-types.d.ts.map
|
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import { buildFlowIndex, reachableFrom, readZeebeIoMapping, readZeebeTaskHeaders, readZeebeTaskType, } from "./optimize/utils.js";
|
|
2
|
+
import { analyzeVariableFlow, extractFeelIdentifiers } from "./optimize/variable-flow.js";
|
|
3
|
+
function isContainer(el) {
|
|
4
|
+
return (el.type === "subProcess" ||
|
|
5
|
+
el.type === "adHocSubProcess" ||
|
|
6
|
+
el.type === "eventSubProcess" ||
|
|
7
|
+
el.type === "transaction");
|
|
8
|
+
}
|
|
9
|
+
function eventDefinitions(el) {
|
|
10
|
+
return "eventDefinitions" in el ? el.eventDefinitions : [];
|
|
11
|
+
}
|
|
12
|
+
const isDynamic = (value) => value.trim().startsWith("=");
|
|
13
|
+
function addRef(map, key, ref) {
|
|
14
|
+
const list = map.get(key);
|
|
15
|
+
if (list)
|
|
16
|
+
list.push(ref);
|
|
17
|
+
else
|
|
18
|
+
map.set(key, [ref]);
|
|
19
|
+
}
|
|
20
|
+
const byText = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
21
|
+
const byRef = (a, b) => byText(a.processId, b.processId) || byText(a.elementId, b.elementId);
|
|
22
|
+
function uniqueRefs(refs) {
|
|
23
|
+
const seen = new Set();
|
|
24
|
+
const out = [];
|
|
25
|
+
for (const ref of [...refs].sort(byRef)) {
|
|
26
|
+
const key = `${ref.processId}\u0000${ref.elementId}`;
|
|
27
|
+
if (seen.has(key))
|
|
28
|
+
continue;
|
|
29
|
+
seen.add(key);
|
|
30
|
+
out.push(ref);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/** Merge per-element key maps: a key is required only when every element requires it. */
|
|
35
|
+
function mergeKeys(maps) {
|
|
36
|
+
const names = new Set();
|
|
37
|
+
for (const map of maps)
|
|
38
|
+
for (const name of map.keys())
|
|
39
|
+
names.add(name);
|
|
40
|
+
return [...names].sort(byText).map((name) => ({
|
|
41
|
+
name,
|
|
42
|
+
required: maps.every((map) => map.get(name) === true),
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
function readSubscriptionKey(ext) {
|
|
46
|
+
const sub = ext.find((e) => e.name === "zeebe:subscription");
|
|
47
|
+
const key = sub?.attributes.correlationKey?.trim();
|
|
48
|
+
return key === undefined || key === "" ? undefined : key;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Read the worker-facing contract out of one or more BPMN definitions. Pure and
|
|
52
|
+
* deterministic: every list is sorted.
|
|
53
|
+
*/
|
|
54
|
+
export function extractProcessContract(definitions) {
|
|
55
|
+
const all = Array.isArray(definitions)
|
|
56
|
+
? definitions
|
|
57
|
+
: [definitions];
|
|
58
|
+
const processIds = new Set();
|
|
59
|
+
const jobs = new Map();
|
|
60
|
+
const dynamicJobTypes = [];
|
|
61
|
+
const messageRefs = new Map();
|
|
62
|
+
const messageKeys = new Map();
|
|
63
|
+
const signals = new Map();
|
|
64
|
+
const errors = new Map();
|
|
65
|
+
const escalations = new Map();
|
|
66
|
+
for (const defs of all) {
|
|
67
|
+
const errorById = new Map(defs.errors.map((e) => [e.id, e]));
|
|
68
|
+
const escalationById = new Map(defs.escalations.map((e) => [e.id, e]));
|
|
69
|
+
const messageById = new Map(defs.messages.map((m) => [m.id, m]));
|
|
70
|
+
const signalById = new Map(defs.signals.map((s) => [s.id, s]));
|
|
71
|
+
for (const process of defs.processes) {
|
|
72
|
+
if (process.isExecutable !== true)
|
|
73
|
+
continue;
|
|
74
|
+
processIds.add(process.id);
|
|
75
|
+
const flow = variableFlowIndex(process);
|
|
76
|
+
const topIndex = buildFlowIndex(process);
|
|
77
|
+
/** Error codes caught at a scope (boundary on `el`, or error event sub-processes inside `container`). */
|
|
78
|
+
const caughtIn = (scopeElement, container) => {
|
|
79
|
+
const codes = [];
|
|
80
|
+
let any = false;
|
|
81
|
+
const collect = (defsList) => {
|
|
82
|
+
for (const def of defsList) {
|
|
83
|
+
if (def.type !== "error")
|
|
84
|
+
continue;
|
|
85
|
+
const code = def.errorRef === undefined ? undefined : errorById.get(def.errorRef)?.errorCode;
|
|
86
|
+
if (code === undefined || code.trim() === "")
|
|
87
|
+
any = true;
|
|
88
|
+
else
|
|
89
|
+
codes.push(code);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
if (scopeElement !== undefined) {
|
|
93
|
+
for (const el of container.flowElements) {
|
|
94
|
+
if (el.type === "boundaryEvent" && el.attachedToRef === scopeElement.id) {
|
|
95
|
+
collect(el.eventDefinitions);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { codes, any };
|
|
100
|
+
};
|
|
101
|
+
const eventSubProcessCatches = (container) => {
|
|
102
|
+
const codes = [];
|
|
103
|
+
let any = false;
|
|
104
|
+
for (const el of container.flowElements) {
|
|
105
|
+
const isEventSub = el.type === "eventSubProcess" || (el.type === "subProcess" && el.triggeredByEvent);
|
|
106
|
+
if (!isEventSub || !isContainer(el))
|
|
107
|
+
continue;
|
|
108
|
+
for (const start of el.flowElements) {
|
|
109
|
+
if (start.type !== "startEvent")
|
|
110
|
+
continue;
|
|
111
|
+
for (const def of start.eventDefinitions) {
|
|
112
|
+
if (def.type !== "error")
|
|
113
|
+
continue;
|
|
114
|
+
const code = def.errorRef === undefined ? undefined : errorById.get(def.errorRef)?.errorCode;
|
|
115
|
+
if (code === undefined || code.trim() === "")
|
|
116
|
+
any = true;
|
|
117
|
+
else
|
|
118
|
+
codes.push(code);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { codes, any };
|
|
123
|
+
};
|
|
124
|
+
const visit = (container, ancestors) => {
|
|
125
|
+
for (const el of container.flowElements) {
|
|
126
|
+
const ref = {
|
|
127
|
+
processId: process.id,
|
|
128
|
+
elementId: el.id,
|
|
129
|
+
elementType: el.type,
|
|
130
|
+
};
|
|
131
|
+
collectEvents(el, ref);
|
|
132
|
+
const jobType = el.type === "userTask" ? null : readZeebeTaskType(el.extensionElements);
|
|
133
|
+
if (jobType !== null && jobType.trim() !== "") {
|
|
134
|
+
if (isDynamic(jobType))
|
|
135
|
+
dynamicJobTypes.push(ref);
|
|
136
|
+
else
|
|
137
|
+
addJob(jobType, el, ref, { ancestors, container });
|
|
138
|
+
}
|
|
139
|
+
if (isContainer(el))
|
|
140
|
+
visit(el, [el, ...ancestors]);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const collectEvents = (el, ref) => {
|
|
144
|
+
const messageRefIds = [];
|
|
145
|
+
if ((el.type === "sendTask" || el.type === "receiveTask") && el.messageRef) {
|
|
146
|
+
messageRefIds.push(el.messageRef);
|
|
147
|
+
}
|
|
148
|
+
for (const def of eventDefinitions(el)) {
|
|
149
|
+
if (def.type === "message" && def.messageRef)
|
|
150
|
+
messageRefIds.push(def.messageRef);
|
|
151
|
+
if (def.type === "signal" && def.signalRef) {
|
|
152
|
+
const name = signalById.get(def.signalRef)?.name;
|
|
153
|
+
if (name && !isDynamic(name))
|
|
154
|
+
addRef(signals, name, ref);
|
|
155
|
+
}
|
|
156
|
+
if (def.type === "error" && def.errorRef) {
|
|
157
|
+
const code = errorById.get(def.errorRef)?.errorCode;
|
|
158
|
+
if (code && !isDynamic(code))
|
|
159
|
+
addRef(errors, code, ref);
|
|
160
|
+
}
|
|
161
|
+
if (def.type === "escalation" && def.escalationRef) {
|
|
162
|
+
const code = escalationById.get(def.escalationRef)?.escalationCode;
|
|
163
|
+
if (code && !isDynamic(code))
|
|
164
|
+
addRef(escalations, code, ref);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const id of messageRefIds) {
|
|
168
|
+
const message = messageById.get(id);
|
|
169
|
+
const name = message?.name;
|
|
170
|
+
if (!name || isDynamic(name))
|
|
171
|
+
continue;
|
|
172
|
+
addRef(messageRefs, name, ref);
|
|
173
|
+
const key = readSubscriptionKey(el.extensionElements) ??
|
|
174
|
+
readSubscriptionKey(message.extensionElements ?? []);
|
|
175
|
+
const keys = messageKeys.get(name) ?? new Set();
|
|
176
|
+
if (key !== undefined)
|
|
177
|
+
keys.add(key);
|
|
178
|
+
messageKeys.set(name, keys);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const addJob = (jobType, el, ref, scope) => {
|
|
182
|
+
const acc = jobs.get(jobType) ?? {
|
|
183
|
+
elements: [],
|
|
184
|
+
variables: [],
|
|
185
|
+
output: [],
|
|
186
|
+
headers: [],
|
|
187
|
+
errors: new Set(),
|
|
188
|
+
anyError: false,
|
|
189
|
+
};
|
|
190
|
+
jobs.set(jobType, acc);
|
|
191
|
+
acc.elements.push(ref);
|
|
192
|
+
const io = readZeebeIoMapping(el.extensionElements);
|
|
193
|
+
const inputTargets = (io?.inputs ?? [])
|
|
194
|
+
.map((input) => input.target.trim())
|
|
195
|
+
.filter((target) => target !== "");
|
|
196
|
+
const variables = new Map();
|
|
197
|
+
if (inputTargets.length > 0) {
|
|
198
|
+
for (const target of inputTargets)
|
|
199
|
+
variables.set(target, true);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
for (const name of flow.scopeBefore(el, scope))
|
|
203
|
+
variables.set(name, false);
|
|
204
|
+
}
|
|
205
|
+
acc.variables.push(variables);
|
|
206
|
+
const output = new Map();
|
|
207
|
+
const outputs = (io?.outputs ?? []).filter((o) => o.target.trim() !== "");
|
|
208
|
+
if (outputs.length > 0) {
|
|
209
|
+
const locals = new Set(inputTargets);
|
|
210
|
+
for (const mapping of outputs) {
|
|
211
|
+
const plain = /^=?\s*[A-Za-z_$][\w$]*(\s*\.\s*[A-Za-z_$][\w$]*)*\s*$/.test(mapping.source);
|
|
212
|
+
for (const name of extractFeelIdentifiers(mapping.source)) {
|
|
213
|
+
if (locals.has(name))
|
|
214
|
+
continue;
|
|
215
|
+
output.set(name, (output.get(name) ?? false) || plain);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else if (scope.ancestors.length === 0) {
|
|
220
|
+
const downstream = reachableFrom(topIndex.bySource.get(el.id)?.map((f) => f.targetRef) ?? [], topIndex.bySource);
|
|
221
|
+
for (const id of [...downstream].sort(byText)) {
|
|
222
|
+
if (id === el.id)
|
|
223
|
+
continue;
|
|
224
|
+
for (const name of flow.consumedBy.get(id) ?? []) {
|
|
225
|
+
if (!flow.produced.has(name))
|
|
226
|
+
output.set(name, false);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
acc.output.push(output);
|
|
231
|
+
const headers = new Map();
|
|
232
|
+
for (const header of readZeebeTaskHeaders(el.extensionElements)?.headers ?? []) {
|
|
233
|
+
if (header.key !== "")
|
|
234
|
+
headers.set(header.key, header.value);
|
|
235
|
+
}
|
|
236
|
+
acc.headers.push(headers);
|
|
237
|
+
const scopes = [
|
|
238
|
+
{ element: el, container: scope.container },
|
|
239
|
+
];
|
|
240
|
+
let containerOf = process;
|
|
241
|
+
for (let i = scope.ancestors.length - 1; i >= 0; i--) {
|
|
242
|
+
const ancestor = scope.ancestors[i];
|
|
243
|
+
scopes.push({ element: ancestor, container: containerOf });
|
|
244
|
+
if (isContainer(ancestor))
|
|
245
|
+
containerOf = ancestor;
|
|
246
|
+
}
|
|
247
|
+
const catches = [
|
|
248
|
+
...scopes.map((s) => caughtIn(s.element, s.container)),
|
|
249
|
+
eventSubProcessCatches(process),
|
|
250
|
+
...scope.ancestors.filter(isContainer).map(eventSubProcessCatches),
|
|
251
|
+
];
|
|
252
|
+
for (const c of catches) {
|
|
253
|
+
for (const code of c.codes)
|
|
254
|
+
acc.errors.add(code);
|
|
255
|
+
if (c.any)
|
|
256
|
+
acc.anyError = true;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
visit(process, []);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const jobList = [...jobs.entries()]
|
|
263
|
+
.sort(([a], [b]) => byText(a, b))
|
|
264
|
+
.map(([type, acc]) => {
|
|
265
|
+
const headerKeys = new Set();
|
|
266
|
+
for (const map of acc.headers)
|
|
267
|
+
for (const key of map.keys())
|
|
268
|
+
headerKeys.add(key);
|
|
269
|
+
return {
|
|
270
|
+
type,
|
|
271
|
+
elements: uniqueRefs(acc.elements),
|
|
272
|
+
variables: mergeKeys(acc.variables),
|
|
273
|
+
output: mergeKeys(acc.output),
|
|
274
|
+
headers: [...headerKeys].sort(byText).map((key) => {
|
|
275
|
+
const values = new Set();
|
|
276
|
+
for (const map of acc.headers) {
|
|
277
|
+
const value = map.get(key);
|
|
278
|
+
if (value !== undefined)
|
|
279
|
+
values.add(value);
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
key,
|
|
283
|
+
values: [...values].sort(byText),
|
|
284
|
+
required: acc.headers.every((map) => map.has(key)),
|
|
285
|
+
};
|
|
286
|
+
}),
|
|
287
|
+
errors: acc.anyError ? "any" : [...acc.errors].sort(byText),
|
|
288
|
+
};
|
|
289
|
+
});
|
|
290
|
+
const codes = (map) => [...map.entries()]
|
|
291
|
+
.sort(([a], [b]) => byText(a, b))
|
|
292
|
+
.map(([code, refs]) => ({ code, elements: uniqueRefs(refs) }));
|
|
293
|
+
return {
|
|
294
|
+
processIds: [...processIds].sort(byText),
|
|
295
|
+
jobs: jobList,
|
|
296
|
+
messages: [...messageRefs.entries()]
|
|
297
|
+
.sort(([a], [b]) => byText(a, b))
|
|
298
|
+
.map(([name, refs]) => ({
|
|
299
|
+
name,
|
|
300
|
+
correlationKeys: [...(messageKeys.get(name) ?? [])].sort(byText),
|
|
301
|
+
elements: uniqueRefs(refs),
|
|
302
|
+
})),
|
|
303
|
+
signals: codes(signals),
|
|
304
|
+
errors: codes(errors),
|
|
305
|
+
escalations: codes(escalations),
|
|
306
|
+
dynamicJobTypes: uniqueRefs(dynamicJobTypes),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/** What the variable-flow analysis knows about one process, indexed for lookup. */
|
|
310
|
+
function variableFlowIndex(process) {
|
|
311
|
+
// The analysis counts `zeebe:input` targets as produced, but they are local to
|
|
312
|
+
// their element: nothing downstream sees one unless something else also sets it.
|
|
313
|
+
const inputTargets = new Set();
|
|
314
|
+
const setGlobally = new Set();
|
|
315
|
+
for (const el of process.flowElements) {
|
|
316
|
+
const io = readZeebeIoMapping(el.extensionElements);
|
|
317
|
+
for (const input of io?.inputs ?? [])
|
|
318
|
+
inputTargets.add(input.target.trim());
|
|
319
|
+
for (const output of io?.outputs ?? [])
|
|
320
|
+
setGlobally.add(output.target.trim());
|
|
321
|
+
for (const ext of el.extensionElements) {
|
|
322
|
+
const result = ext.attributes.resultVariable;
|
|
323
|
+
if (ext.name === "zeebe:calledDecision" && result)
|
|
324
|
+
setGlobally.add(result);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const visible = (name) => !inputTargets.has(name) || setGlobally.has(name);
|
|
328
|
+
const edgeScope = new Map();
|
|
329
|
+
const consumedBy = new Map();
|
|
330
|
+
const produced = new Set();
|
|
331
|
+
for (const f of analyzeVariableFlow(process)) {
|
|
332
|
+
if (f.id.startsWith("data-flow/edge-scope:")) {
|
|
333
|
+
const flowId = f.elementIds[0];
|
|
334
|
+
if (flowId !== undefined)
|
|
335
|
+
edgeScope.set(flowId, (f.produces ?? []).filter(visible));
|
|
336
|
+
}
|
|
337
|
+
else if (f.id.startsWith("data-flow/role:")) {
|
|
338
|
+
const elementId = f.elementIds[0];
|
|
339
|
+
if (elementId !== undefined)
|
|
340
|
+
consumedBy.set(elementId, f.consumes ?? []);
|
|
341
|
+
for (const name of f.produces ?? [])
|
|
342
|
+
if (visible(name))
|
|
343
|
+
produced.add(name);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
consumedBy,
|
|
348
|
+
produced,
|
|
349
|
+
/**
|
|
350
|
+
* Variables in scope on the element's incoming flows — or, for an element
|
|
351
|
+
* with none known, on the incoming flows of its nearest enclosing scope.
|
|
352
|
+
*/
|
|
353
|
+
scopeBefore(el, scope) {
|
|
354
|
+
for (const candidate of [el, ...scope.ancestors]) {
|
|
355
|
+
const names = new Set();
|
|
356
|
+
let known = false;
|
|
357
|
+
for (const flowId of candidate.incoming) {
|
|
358
|
+
const vars = edgeScope.get(flowId);
|
|
359
|
+
if (vars === undefined)
|
|
360
|
+
continue;
|
|
361
|
+
known = true;
|
|
362
|
+
for (const name of vars)
|
|
363
|
+
names.add(name);
|
|
364
|
+
}
|
|
365
|
+
if (known)
|
|
366
|
+
return [...names];
|
|
367
|
+
}
|
|
368
|
+
return [];
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
// ---------------------------------------------------------------------------
|
|
373
|
+
// Rendering
|
|
374
|
+
// ---------------------------------------------------------------------------
|
|
375
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
376
|
+
const propertyKey = (name) => (IDENTIFIER.test(name) ? name : JSON.stringify(name));
|
|
377
|
+
const literal = (value) => JSON.stringify(value);
|
|
378
|
+
const comment = (text) => text.replace(/\*\//g, "*\\/").replace(/[\r\n]+/g, " ");
|
|
379
|
+
const union = (values, empty = "never") => values.length === 0 ? empty : values.map(literal).join(" | ");
|
|
380
|
+
function pascalCase(value) {
|
|
381
|
+
const words = value.split(/[^A-Za-z0-9]+/).filter((w) => w !== "");
|
|
382
|
+
const joined = words.map((w) => w[0].toUpperCase() + w.slice(1)).join("");
|
|
383
|
+
if (joined === "")
|
|
384
|
+
return "Job";
|
|
385
|
+
return /^[0-9]/.test(joined) ? `Job${joined}` : joined;
|
|
386
|
+
}
|
|
387
|
+
/** Assign every job type a distinct TypeScript name stem, in sorted job type order. */
|
|
388
|
+
function jobTypeNames(types) {
|
|
389
|
+
const taken = new Set();
|
|
390
|
+
const out = new Map();
|
|
391
|
+
for (const type of types) {
|
|
392
|
+
const base = pascalCase(type);
|
|
393
|
+
let name = base;
|
|
394
|
+
for (let n = 2; taken.has(name); n++)
|
|
395
|
+
name = `${base}${n}`;
|
|
396
|
+
taken.add(name);
|
|
397
|
+
out.set(type, name);
|
|
398
|
+
}
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
function refList(refs) {
|
|
402
|
+
return refs.map((r) => `${r.processId}#${r.elementId}`).join(", ");
|
|
403
|
+
}
|
|
404
|
+
function renderKeys(name, keys, doc) {
|
|
405
|
+
const lines = [`/** ${comment(doc)} */`, `export interface ${name} {`];
|
|
406
|
+
for (const key of keys) {
|
|
407
|
+
lines.push(`\t${propertyKey(key.name)}${key.required ? "" : "?"}: unknown`);
|
|
408
|
+
}
|
|
409
|
+
lines.push("}");
|
|
410
|
+
return lines;
|
|
411
|
+
}
|
|
412
|
+
function renderConstUnion(constName, typeName, values) {
|
|
413
|
+
return [
|
|
414
|
+
`export const ${constName} = [${values.map(literal).join(", ")}] as const`,
|
|
415
|
+
`export type ${typeName} = (typeof ${constName})[number]`,
|
|
416
|
+
];
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Render the {@link ProcessContract} of one or more BPMN definitions as
|
|
420
|
+
* TypeScript source. The output is deterministic (everything sorted) and
|
|
421
|
+
* starts with a "do not edit" header.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* import { Bpmn, generateProcessTypes } from "@bpmnkit/core"
|
|
426
|
+
* const source = generateProcessTypes(Bpmn.parse(xml))
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
export function generateProcessTypes(definitions, options = {}) {
|
|
430
|
+
const contract = extractProcessContract(definitions);
|
|
431
|
+
const banner = options.banner ?? "generated by casen gen types — do not edit";
|
|
432
|
+
const names = jobTypeNames(contract.jobs.map((j) => j.type));
|
|
433
|
+
const out = [
|
|
434
|
+
`// ${comment(banner)}`,
|
|
435
|
+
"//",
|
|
436
|
+
"// Keys are exact; values are `unknown` because FEEL cannot be typed statically.",
|
|
437
|
+
"// Optional keys are ones the model does not guarantee. See the typed workers guide:",
|
|
438
|
+
"// https://bpmnkit.com/docs/guides/typed-workers",
|
|
439
|
+
];
|
|
440
|
+
if (contract.dynamicJobTypes.length > 0) {
|
|
441
|
+
out.push(`// Skipped dynamic job types (FEEL expressions): ${comment(refList(contract.dynamicJobTypes))}`);
|
|
442
|
+
}
|
|
443
|
+
out.push("");
|
|
444
|
+
out.push(...renderConstUnion("processIds", "ProcessId", contract.processIds), "");
|
|
445
|
+
out.push(...renderConstUnion("jobTypes", "JobType", contract.jobs.map((j) => j.type)), "");
|
|
446
|
+
for (const job of contract.jobs) {
|
|
447
|
+
const stem = names.get(job.type);
|
|
448
|
+
const where = `Job type ${literal(job.type)} — ${refList(job.elements)}`;
|
|
449
|
+
out.push(...renderKeys(`${stem}Variables`, job.variables, `Variables of ${where}`), "");
|
|
450
|
+
out.push(...renderKeys(`${stem}Output`, job.output, `Output of ${where}`), "");
|
|
451
|
+
out.push(`/** Task headers of ${comment(where)} */`, `export interface ${stem}Headers {`);
|
|
452
|
+
for (const header of job.headers) {
|
|
453
|
+
out.push(`\treadonly ${propertyKey(header.key)}${header.required ? "" : "?"}: ${union(header.values)}`);
|
|
454
|
+
}
|
|
455
|
+
out.push("}", "");
|
|
456
|
+
}
|
|
457
|
+
out.push("/** Job type → the contract a worker for it can rely on. */", "export type JobTypes = {");
|
|
458
|
+
for (const job of contract.jobs) {
|
|
459
|
+
const stem = names.get(job.type);
|
|
460
|
+
const processes = [...new Set(job.elements.map((e) => e.processId))];
|
|
461
|
+
const errorsType = job.errors === "any" ? "string" : union(job.errors);
|
|
462
|
+
out.push(`\t${literal(job.type)}: {`, `\t\tvariables: ${stem}Variables`, `\t\toutput: ${stem}Output`, `\t\theaders: ${stem}Headers`, `\t\terrors: ${errorsType}`, `\t\tprocessIds: ${union(processes)}`, `\t\telementIds: ${union(job.elements.map((e) => e.elementId))}`, "\t}");
|
|
463
|
+
}
|
|
464
|
+
out.push("}", "");
|
|
465
|
+
out.push("/** Message name → the correlation key expressions its subscriptions use. */");
|
|
466
|
+
out.push("export interface Messages {");
|
|
467
|
+
for (const message of contract.messages) {
|
|
468
|
+
const keys = message.correlationKeys;
|
|
469
|
+
out.push(`\t/** ${comment(keys.length > 0
|
|
470
|
+
? `Correlation key: ${keys.join(" | ")} — ${refList(message.elements)}`
|
|
471
|
+
: `No correlation key — ${refList(message.elements)}`)} */`, `\t${propertyKey(message.name)}: { correlationKey: ${union(keys, "undefined")} }`);
|
|
472
|
+
}
|
|
473
|
+
out.push("}");
|
|
474
|
+
out.push(...renderConstUnion("messageNames", "MessageName", contract.messages.map((m) => m.name)), "");
|
|
475
|
+
const codeSection = (constName, typeName, doc, list) => {
|
|
476
|
+
out.push("/**", ` * ${doc}`);
|
|
477
|
+
for (const item of list)
|
|
478
|
+
out.push(` * - ${comment(`${item.code} — ${refList(item.elements)}`)}`);
|
|
479
|
+
out.push(" */", ...renderConstUnion(constName, typeName, list.map((c) => c.code)), "");
|
|
480
|
+
};
|
|
481
|
+
codeSection("signalNames", "SignalName", "Signal names thrown or caught.", contract.signals);
|
|
482
|
+
codeSection("errorCodes", "ErrorCode", "Error codes thrown or caught.", contract.errors);
|
|
483
|
+
codeSection("escalationCodes", "EscalationCode", "Escalation codes thrown or caught.", contract.escalations);
|
|
484
|
+
// An empty body renders as `{}` rather than a brace pair split over two lines.
|
|
485
|
+
return `${out
|
|
486
|
+
.join("\n")
|
|
487
|
+
.replace(/\{\n\}/g, "{}")
|
|
488
|
+
.trimEnd()}\n`;
|
|
489
|
+
}
|
|
490
|
+
//# sourceMappingURL=process-types.js.map
|