@mldong/jeeflow 1.8.44 → 1.8.45

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/dist/engine.d.ts CHANGED
@@ -44,6 +44,9 @@ export interface Engine {
44
44
  executeAndJumpToEnd(taskId: string, operator: string, args?: Record<string, any>): Promise<ProcessInstance>;
45
45
  executeAndJumpTask(taskId: string, operator: string, args: Record<string, any>, targetTaskName?: string): Promise<ProcessInstance>;
46
46
  executeAndJumpToFirstTaskNode(taskId: string, operator: string, args?: Record<string, any>): Promise<ProcessInstance>;
47
+ /** 事务包裹(audit8 145-2 / spec/12 §12.2):仓储有事务载体则并入(嵌套经 withTx 的 join 支),
48
+ * 无(内存仓)则裸跑=显式降级(§12.1 原则 4)。门面动作级包裹也走这一枚。 */
49
+ inTx<T>(fn: () => Promise<T>): Promise<T>;
47
50
  }
48
51
  /**
49
52
  * 委托代理自动生效的注入面(issues/116)——只需要 getSurrogate 一侧能力,
@@ -143,11 +146,19 @@ export declare class EngineImpl implements Engine {
143
146
  * 接线是为将来终止腿落地时不再漏发,不是给现有路径凭空多发事件。
144
147
  */
145
148
  notifyInstanceEnd(inst: ProcessInstance, operator: string, reason?: string): Promise<void>;
149
+ /** 事务包裹:有事务载体(JDBC `withTx`)则整段并入外层/自开一段;无(内存仓)裸跑。
150
+ * 引擎入口包裹与门面动作级包裹都走这一枚 —— 嵌套调用由 withTx 的 join 支并入最外层。 */
151
+ inTx<T>(fn: () => Promise<T>): Promise<T>;
146
152
  startProcessInstanceById(defineId: string, operator: string, args?: Record<string, any>): Promise<ProcessInstance>;
153
+ private startProcessInstanceByIdInner;
147
154
  executeProcessTask(taskId: string, operator: string, args?: Record<string, any>): Promise<ProcessInstance>;
155
+ private executeProcessTaskInner;
148
156
  executeAndJumpToEnd(taskId: string, operator: string, args?: Record<string, any>): Promise<ProcessInstance>;
157
+ private executeAndJumpToEndInner;
149
158
  executeAndJumpTask(taskId: string, operator: string, args?: Record<string, any>, targetTaskName?: string): Promise<ProcessInstance>;
159
+ private executeAndJumpTaskInner;
150
160
  executeAndJumpToFirstTaskNode(taskId: string, operator: string, args?: Record<string, any>): Promise<ProcessInstance>;
161
+ private executeAndJumpToFirstTaskNodeInner;
151
162
  private prepareExecuteTask;
152
163
  private isFirstTaskNode;
153
164
  /**
package/dist/engine.js CHANGED
@@ -328,8 +328,19 @@ export class EngineImpl {
328
328
  await this.fireEvent({ type: EventType.InstanceTerminated, instanceId: inst.id, operator, reason });
329
329
  }
330
330
  }
331
+ // ─── 事务(audit8 145-2 / spec/12 §12.2)───────────────────────────────────
332
+ /** 事务包裹:有事务载体(JDBC `withTx`)则整段并入外层/自开一段;无(内存仓)裸跑。
333
+ * 引擎入口包裹与门面动作级包裹都走这一枚 —— 嵌套调用由 withTx 的 join 支并入最外层。 */
334
+ async inTx(fn) {
335
+ return this.repo.withTx ? this.repo.withTx(fn) : fn();
336
+ }
331
337
  // ─── Start ─────────────────────────────────────────────────────────────────
332
338
  async startProcessInstanceById(defineId, operator, args = {}) {
339
+ // audit8 145-2①:入口级包裹——直连引擎的调用方也拿到"一次动作一笔原子"(spec/12 §12.2)。
340
+ // 门面动作级包裹在流内再调本入口时,经 withTx join 支并入同一段事务(不双开)。
341
+ return this.inTx(() => this.startProcessInstanceByIdInner(defineId, operator, args));
342
+ }
343
+ async startProcessInstanceByIdInner(defineId, operator, args = {}) {
333
344
  const def = await this.repo.findDefineById(defineId);
334
345
  if (!def)
335
346
  throw new Error(`define not found: ${defineId}`);
@@ -366,6 +377,10 @@ export class EngineImpl {
366
377
  }
367
378
  // ─── Execute ───────────────────────────────────────────────────────────────
368
379
  async executeProcessTask(taskId, operator, args = {}) {
380
+ // audit8 145-2①:入口级包裹(同 startProcessInstanceById 注释)
381
+ return this.inTx(() => this.executeProcessTaskInner(taskId, operator, args));
382
+ }
383
+ async executeProcessTaskInner(taskId, operator, args = {}) {
369
384
  const { task, inst, flow, vars } = await this.prepareExecuteTask(taskId, operator, args);
370
385
  // issues/127 / 规范 11 §11.7「办理时抄送」腿的统一出口:任务更新已在 prepareExecuteTask
371
386
  // 落库,此处建 cc 行并逐人 fire CC_CREATE(4),再回读实例。
@@ -488,6 +503,10 @@ export class EngineImpl {
488
503
  }
489
504
  // ─── Reject ────────────────────────────────────────────────────────────────
490
505
  async executeAndJumpToEnd(taskId, operator, args = {}) {
506
+ // audit8 145-2①:入口级包裹(同 startProcessInstanceById 注释)
507
+ return this.inTx(() => this.executeAndJumpToEndInner(taskId, operator, args));
508
+ }
509
+ async executeAndJumpToEndInner(taskId, operator, args = {}) {
491
510
  const { inst } = await this.prepareExecuteTask(taskId, operator, args);
492
511
  // 门面 submitType=2 REJECT 唯一入口(对齐 Java executeAndJumpToEnd 语义)
493
512
  inst.reject(new Date());
@@ -499,6 +518,10 @@ export class EngineImpl {
499
518
  }
500
519
  // ─── Jump(ROLLBACK 空 target / JUMP 命名 target,boot2 executeAndJumpTask)────
501
520
  async executeAndJumpTask(taskId, operator, args = {}, targetTaskName) {
521
+ // audit8 145-2①:入口级包裹(同 startProcessInstanceById 注释)
522
+ return this.inTx(() => this.executeAndJumpTaskInner(taskId, operator, args, targetTaskName));
523
+ }
524
+ async executeAndJumpTaskInner(taskId, operator, args = {}, targetTaskName) {
502
525
  const { task, inst, flow, vars } = await this.prepareExecuteTask(taskId, operator, args);
503
526
  if (!targetTaskName) {
504
527
  // issues/121 P2:ROLLBACK 走血缘版——复活 task.parentTaskId 指的那条历史行,
@@ -522,6 +545,10 @@ export class EngineImpl {
522
545
  }
523
546
  // ─── Jump To First Task(退回发起人,boot2 ROLLBACK_TO_OPERATOR=6)────────────
524
547
  async executeAndJumpToFirstTaskNode(taskId, operator, args = {}) {
548
+ // audit8 145-2①:入口级包裹(同 startProcessInstanceById 注释)
549
+ return this.inTx(() => this.executeAndJumpToFirstTaskNodeInner(taskId, operator, args));
550
+ }
551
+ async executeAndJumpToFirstTaskNodeInner(taskId, operator, args = {}) {
525
552
  const { inst, flow, vars } = await this.prepareExecuteTask(taskId, operator, args);
526
553
  // 找到第一个任务节点,强制参与者为发起人,重新执行
527
554
  const startNode = findNodeByType(flow, TypeStart);
package/dist/facade.d.ts CHANGED
@@ -47,6 +47,8 @@ export declare class JeeflowFacade {
47
47
  setUserSearch(fn: UserSearch): this;
48
48
  setOrgProvider(orgProv: OrgUserProvider): this;
49
49
  flow(action: string, args?: Record<string, any>): Promise<Record<string, any>>;
50
+ /** 事务包裹(audit8 145-2):engine 缺省(纯统计门面用法)时裸跑=显式降级。 */
51
+ private inTx;
50
52
  private dispatch;
51
53
  private startAndExecute;
52
54
  private deploy;
package/dist/facade.js CHANGED
@@ -228,7 +228,11 @@ export class JeeflowFacade {
228
228
  }
229
229
  async flow(action, args = {}) {
230
230
  try {
231
- const data = await this.dispatch(action, args);
231
+ // audit8 145-2(spec/12 §12.2):事务边界=一次动作——门面自组复合写(startAndExecute 的
232
+ // start+参与者写+execute、deploy 的 define+design 两拍等)与引擎入口写同处一事务;
233
+ // 入口内包裹嵌套并入(withTx join 支)。信封在事务之外生成:中途异常先整体回滚,
234
+ // 再由下面的 catch 组装失败信封(宿门外包硬义务形状)。
235
+ const data = await this.inTx(() => this.dispatch(action, args));
232
236
  return { code: 0, msg: '成功', data: data ?? null };
233
237
  }
234
238
  catch (e) {
@@ -245,6 +249,10 @@ export class JeeflowFacade {
245
249
  return { code: 99999999, msg: e?.message ?? String(e) };
246
250
  }
247
251
  }
252
+ /** 事务包裹(audit8 145-2):engine 缺省(纯统计门面用法)时裸跑=显式降级。 */
253
+ async inTx(fn) {
254
+ return this.engine ? this.engine.inTx(fn) : fn();
255
+ }
248
256
  async dispatch(action, args) {
249
257
  switch (action) {
250
258
  case 'processDefine/page':
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export type { ProcessRepository, ProcessExtRepository, UserProvider, OrgUserProv
8
8
  export { SqliteDynamicTableWriter, PersistPostInterceptor, registerPersistMeta, type DynamicTableWriter, type DefineLoader, } from './persist.js';
9
9
  export { registerBuiltinAssignments, OperatorAssignmentHandler, FormFieldAssigneeHandler, DeptLeaderAssignmentHandler, DeptMainLeaderAssignmentHandler, ApplicantDeptLeaderAssignmentHandler, ApplicantDeptMainLeaderAssignmentHandler, TaskRoleAssigneeHandler, HANDLER_OPERATOR_ASSIGNMENT, HANDLER_FORM_FIELD_ASSIGNEE, HANDLER_DEPT_LEADER, HANDLER_DEPT_MAIN_LEADER, HANDLER_APPLICANT_DEPT_LEADER, HANDLER_APPLICANT_DEPT_MAIN_LEADER, HANDLER_TASK_ROLE_ASSIGNEE, } from './builtin.js';
10
10
  export { JeeflowFacade } from './facade.js';
11
+ export { currentTxConnection, transactionAwarePool } from './jdbc/shared.js';
11
12
  export { EventType, ProcessStart, ProcessFinish, ProcessReject, TaskCreate, TaskComplete, CcCreate, } from './extensions.js';
12
13
  export type { EngineExtensions, FlowInterceptor, ProcessEventListener, ProcessEvent, AssignmentHandler, DecisionHandler } from './extensions.js';
13
14
  export { JdbcProcessExtRepository } from './jdbc/ext.js';
package/dist/index.js CHANGED
@@ -8,6 +8,9 @@ export { SqliteDynamicTableWriter, PersistPostInterceptor, registerPersistMeta,
8
8
  export { registerBuiltinAssignments, OperatorAssignmentHandler, FormFieldAssigneeHandler, DeptLeaderAssignmentHandler, DeptMainLeaderAssignmentHandler, ApplicantDeptLeaderAssignmentHandler, ApplicantDeptMainLeaderAssignmentHandler, TaskRoleAssigneeHandler, HANDLER_OPERATOR_ASSIGNMENT, HANDLER_FORM_FIELD_ASSIGNEE, HANDLER_DEPT_LEADER, HANDLER_DEPT_MAIN_LEADER, HANDLER_APPLICANT_DEPT_LEADER, HANDLER_APPLICANT_DEPT_MAIN_LEADER, HANDLER_TASK_ROLE_ASSIGNEE, } from './builtin.js';
9
9
  // issues/35:包导出面补齐——门面/扩展类型/扩展仓储(集成方组装完整引擎链)
10
10
  export { JeeflowFacade } from './facade.js';
11
+ // audit8 145-2:集成侧写腿并入引擎事务的两个公开入口——集成方自研 DynamicTableWriter
12
+ // (如 nestjs 壳的 MysqlDynamicTableWriter)用 transactionAwarePool 包池即可并入同一条连接。
13
+ export { currentTxConnection, transactionAwarePool } from './jdbc/shared.js';
11
14
  // issues/132 §11.3:码表重排后 EventType 必须能从包门面 value-import——
12
15
  // `./extensions` 子路径不在 exports map 里(集成层此前只能在自己仓镜像一份数字码,
13
16
  // nestjs 壳 wf-message.listener.spec.ts 就是这么写的),拿规范名分派的契约要求
package/dist/jdbc/ext.js CHANGED
@@ -1,11 +1,11 @@
1
1
  // 扩展仓储 JDBC 参考实现(v1.1.0)——流程设计 / 设计历史 / 委托代理
2
2
  //
3
3
  // 与 shared.ts 同一套 SqlAdapter / 占位符约定;分页为单表简单过滤(filters 字段名 EQ)。
4
- import { AsyncLocalStorage } from 'node:async_hooks';
5
4
  import { isBlankOwnership, isBlankValue } from '../spi.js';
6
- import { TsIDGenerator, rowId } from './shared.js';
5
+ import { TsIDGenerator, rowId, txStore } from './shared.js';
7
6
  import { surrogateIsEffective, surrogateTimeMs, surrogateHydrateEnabled } from '../surrogate-rule.js';
8
- const txStore = new AsyncLocalStorage();
7
+ // audit8 145-2②:与主仓共用同一枚 AsyncLocalStorage(此前这里自带第二枚且从不 run ⇒
8
+ // 扩展写腿即使外面开着事务也永远逃逸)。共一枚后 `c()` 在事务内取到引擎事务连接。
9
9
  export class JdbcProcessExtRepository {
10
10
  adapter;
11
11
  idGen;
@@ -1,3 +1,3 @@
1
- export { JdbcRepository, TsIDGenerator, convertPlaceholder, repeatPh, type SqlAdapter, type SqlConnection, } from './shared.js';
1
+ export { JdbcRepository, TsIDGenerator, convertPlaceholder, repeatPh, currentTxConnection, transactionAwarePool, type SqlAdapter, type SqlConnection, } from './shared.js';
2
2
  export { MysqlAdapter, MysqlConnection } from './mysql.js';
3
3
  export { PostgresAdapter, PostgresConnection } from './postgres.js';
@@ -2,6 +2,8 @@
2
2
  //
3
3
  // - JdbcRepository / TsIDGenerator / SqlAdapter(共享核心,见 shared.ts)
4
4
  // - MysqlAdapter(mysql2,`?` 原生)/ PostgresAdapter(pg,`$n`)
5
- export { JdbcRepository, TsIDGenerator, convertPlaceholder, repeatPh, } from './shared.js';
5
+ export { JdbcRepository, TsIDGenerator, convertPlaceholder, repeatPh,
6
+ // audit8 145-2:集成侧写腿并入引擎事务的两个公开入口(C99 ⑥ node 腿)
7
+ currentTxConnection, transactionAwarePool, } from './shared.js';
6
8
  export { MysqlAdapter, MysqlConnection } from './mysql.js';
7
9
  export { PostgresAdapter, PostgresConnection } from './postgres.js';
@@ -1,5 +1,20 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
1
2
  import { ProcessInstance, ProcessTask, type ProcessDefine, type CcInstanceRow, type DefineRow, type InstanceRow, type TaskRow } from '../model.js';
2
3
  import type { IDGenerator, ProcessRepository, QueryCondition, InstanceStatsRow, TaskStatsRow } from '../spi.js';
4
+ export declare const txStore: AsyncLocalStorage<SqlConnection>;
5
+ /** 当前事务连接(无事务 ⇒ null)。集成方自研写腿(如 DynamicTableWriter 的 MySQL 实现)
6
+ * 用它并入引擎事务——spec/12 §12.2 覆盖面:一次动作内所有写腿取同一条连接。 */
7
+ export declare function currentTxConnection(): SqlConnection | null;
8
+ /**
9
+ * 事务感知池包装(145-2 / C99 ⑥ 集成侧写腿的并入通道):
10
+ * 有活动事务时把 execute/query 路由到**事务连接**(适配器 raw 驱动的同名方法,返回形状不变);
11
+ * 无事务时原样回落被包的池。与 java `TransactionAwareDataSources.transactionAware` 同一动机,
12
+ * 包在装配点、调用方零改动。约定 SqlConnection 实现的 `raw` 暴露底层驱动对象(两适配器均满足)。
13
+ */
14
+ export declare function transactionAwarePool<P extends {
15
+ execute: (...a: any[]) => any;
16
+ query: (...a: any[]) => any;
17
+ }>(pool: P): P;
3
18
  /** 默认 ID 生成器:时间戳毫秒 + 同毫秒递增序号(对齐 Java nextId 默认实现)——
4
19
  * issue 38 E9:返回 string(数字值在 2^53 内精确,转字符串统一承载) */
5
20
  export declare class TsIDGenerator implements IDGenerator {
@@ -36,8 +36,37 @@ const DEFINE_WHITELIST = new Set([
36
36
  't.id', 't.name', 't.display_name', 't.type', 't.state', 't.version',
37
37
  't.create_time', 't.update_time',
38
38
  ]);
39
- // 当前异步上下文绑定的事务连接
40
- const txStore = new AsyncLocalStorage();
39
+ // 当前异步上下文绑定的事务连接。
40
+ // audit8 145-2:**导出**——ext.ts 必须共用这一枚(此前它自带第二枚且从不 run,扩展写腿
41
+ // 永远逃逸出引擎事务)。集成方写腿也可经 currentTxConnection/transactionAwarePool 并入。
42
+ export const txStore = new AsyncLocalStorage();
43
+ /** 当前事务连接(无事务 ⇒ null)。集成方自研写腿(如 DynamicTableWriter 的 MySQL 实现)
44
+ * 用它并入引擎事务——spec/12 §12.2 覆盖面:一次动作内所有写腿取同一条连接。 */
45
+ export function currentTxConnection() {
46
+ return txStore.getStore() ?? null;
47
+ }
48
+ /**
49
+ * 事务感知池包装(145-2 / C99 ⑥ 集成侧写腿的并入通道):
50
+ * 有活动事务时把 execute/query 路由到**事务连接**(适配器 raw 驱动的同名方法,返回形状不变);
51
+ * 无事务时原样回落被包的池。与 java `TransactionAwareDataSources.transactionAware` 同一动机,
52
+ * 包在装配点、调用方零改动。约定 SqlConnection 实现的 `raw` 暴露底层驱动对象(两适配器均满足)。
53
+ */
54
+ export function transactionAwarePool(pool) {
55
+ return new Proxy(pool, {
56
+ get(target, prop, receiver) {
57
+ if (prop === 'execute' || prop === 'query') {
58
+ return (...a) => {
59
+ const tx = txStore.getStore();
60
+ const raw = tx?.raw;
61
+ if (raw && typeof raw[prop] === 'function')
62
+ return raw[prop](...a);
63
+ return target[prop](...a);
64
+ };
65
+ }
66
+ return Reflect.get(target, prop, receiver);
67
+ },
68
+ });
69
+ }
41
70
  /** 默认 ID 生成器:时间戳毫秒 + 同毫秒递增序号(对齐 Java nextId 默认实现)——
42
71
  * issue 38 E9:返回 string(数字值在 2^53 内精确,转字符串统一承载) */
43
72
  export class TsIDGenerator {
@@ -84,6 +113,10 @@ export class JdbcRepository {
84
113
  }
85
114
  // ── 事务(spec §7.4:AsyncLocalStorage 绑定连接)─────────────────────────
86
115
  async withTx(fn) {
116
+ // audit8 145-2③(spec/12 §12.2 归属面):已有活动事务 ⇒ **并入**(不再开第二段事务、
117
+ // 不提前提交;提交/回滚归最外层)——引擎入口包裹与门面动作级包裹嵌套时落这一支。
118
+ if (txStore.getStore())
119
+ return fn();
87
120
  const conn = await this.adapter.acquire();
88
121
  try {
89
122
  await conn.begin();
package/dist/spi.d.ts CHANGED
@@ -145,6 +145,9 @@ export interface TaskStatsRow {
145
145
  expireTime: Date | string | null;
146
146
  }
147
147
  export interface ProcessRepository {
148
+ /** 事务载体(audit8 145-2,spec/12 §12.2)——JDBC 仓储天然实现(AsyncLocalStorage);
149
+ * 内存仓没有此方法 ⇒ 引擎 inTx 显式降级裸跑(§12.1 原则 4)。 */
150
+ withTx?<T>(fn: () => Promise<T>): Promise<T>;
148
151
  findDefineById(id: string): Promise<ProcessDefine | null>;
149
152
  findDefineByName(name: string): Promise<ProcessDefine | null>;
150
153
  saveDefine(define: ProcessDefine): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mldong/jeeflow",
3
- "version": "1.8.44",
3
+ "version": "1.8.45",
4
4
  "description": "jeeflow workflow engine — Node.js / TypeScript implementation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -20,7 +20,7 @@
20
20
  "build": "tsc",
21
21
  "typecheck": "tsc -p tsconfig.check.json",
22
22
  "test": "node --import tsx __tests__/spec.test.ts",
23
- "test-mysql": "node --import tsx --test __tests__/jdbc.test.ts",
23
+ "test-mysql": "node --import tsx --test __tests__/jdbc.test.ts __tests__/tx-tier-145.test.ts",
24
24
  "demo": "node --import tsx demo/main.ts",
25
25
  "prepublishOnly": "npm run build"
26
26
  },