oak-domain 6.1.0 → 6.1.2

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 (161) hide show
  1. package/es/base-app-domain/ActionDefDict.d.ts +1 -0
  2. package/es/base-app-domain/ActionDefDict.js +3 -1
  3. package/es/base-app-domain/EntityDict.d.ts +86 -1
  4. package/es/base-app-domain/Log/Storage.js +4 -0
  5. package/es/base-app-domain/Log/_baseSchema.d.ts +5 -0
  6. package/es/base-app-domain/Storage.js +3 -1
  7. package/es/base-app-domain/StyleDict.js +3 -1
  8. package/es/base-app-domain/Version/Action.d.ts +6 -0
  9. package/es/base-app-domain/Version/Action.js +2 -0
  10. package/es/base-app-domain/Version/Schema.d.ts +19 -0
  11. package/es/base-app-domain/Version/Schema.js +1 -0
  12. package/es/base-app-domain/Version/Storage.d.ts +3 -0
  13. package/es/base-app-domain/Version/Storage.js +54 -0
  14. package/es/base-app-domain/Version/Style.d.ts +3 -0
  15. package/es/base-app-domain/Version/Style.js +6 -0
  16. package/es/base-app-domain/Version/_baseSchema.d.ts +57 -0
  17. package/es/base-app-domain/Version/_baseSchema.js +1 -0
  18. package/es/compiler/dependencyBuilder.d.ts +1 -0
  19. package/es/compiler/dependencyBuilder.js +173 -55
  20. package/es/compiler/routerBuilder.d.ts +4 -1
  21. package/es/compiler/routerBuilder.js +22 -19
  22. package/es/compiler/schemaBuilder.d.ts +5 -1
  23. package/es/compiler/schemaBuilder.js +10 -0
  24. package/es/context/BackendRuntimeContext.d.ts +9 -1
  25. package/es/context/BackendRuntimeContext.js +13 -1
  26. package/es/entities/Log.d.ts +2 -0
  27. package/es/entities/Log.js +2 -0
  28. package/es/entities/Version.d.ts +14 -0
  29. package/es/entities/Version.js +42 -0
  30. package/es/store/AsyncRowStore.d.ts +4 -1
  31. package/es/store/AsyncRowStore.js +7 -0
  32. package/es/store/CascadeStore.js +22 -0
  33. package/es/store/RelationAuth/authorizationFilter.d.ts +11 -0
  34. package/es/store/RelationAuth/authorizationFilter.js +104 -0
  35. package/es/store/RelationAuth/continuation.d.ts +9 -0
  36. package/es/store/RelationAuth/continuation.js +33 -0
  37. package/es/store/RelationAuth/index.d.ts +141 -0
  38. package/es/store/RelationAuth/index.js +1440 -0
  39. package/es/store/RelationAuth/model.d.ts +38 -0
  40. package/es/store/RelationAuth/model.js +24 -0
  41. package/es/store/RelationAuth/userLookup.d.ts +21 -0
  42. package/es/store/RelationAuth/userLookup.js +102 -0
  43. package/es/store/SyncRowStore.d.ts +2 -1
  44. package/es/store/SyncRowStore.js +3 -0
  45. package/es/store/filter.fix.d.ts +128 -0
  46. package/es/store/filter.fix.js +2862 -0
  47. package/es/store/filter.js +14 -9
  48. package/es/store/triggers.js +239 -44
  49. package/es/types/Context.d.ts +2 -1
  50. package/es/types/Demand.d.ts +11 -1
  51. package/es/types/Environment.d.ts +13 -2
  52. package/es/types/Exception.d.ts +3 -3
  53. package/es/types/Exception.js +25 -22
  54. package/es/types/Expression.d.ts +4 -1
  55. package/es/types/Expression.js +1 -1
  56. package/es/types/Storage.d.ts +15 -2
  57. package/es/utils/SimpleConnector.js +5 -2
  58. package/es/utils/decimal.js +2 -1
  59. package/es/utils/fetch/index.ali.d.ts +3 -0
  60. package/es/utils/fetch/index.ali.js +31 -0
  61. package/es/utils/fetch/index.d.ts +3 -0
  62. package/es/utils/fetch/index.js +5 -0
  63. package/es/utils/fetch/index.mp.d.ts +5 -3
  64. package/es/utils/fetch/index.mp.js +5 -280
  65. package/es/utils/fetch/index.wechat.d.ts +5 -0
  66. package/es/utils/fetch/index.wechat.js +174 -0
  67. package/es/utils/fetch/mini-program.shared.d.ts +37 -0
  68. package/es/utils/fetch/mini-program.shared.js +85 -0
  69. package/es/utils/geo.d.ts +1 -1
  70. package/es/utils/geo.js +1 -1
  71. package/es/utils/random/random.ali.d.ts +6 -0
  72. package/es/utils/random/random.ali.js +21 -0
  73. package/es/utils/random/random.mp.d.ts +5 -1
  74. package/es/utils/random/random.mp.js +11 -5
  75. package/es/utils/random/random.native.d.ts +1 -1
  76. package/es/utils/random/random.native.js +7 -4
  77. package/es/utils/random/random.wechat.d.ts +1 -0
  78. package/es/utils/random/random.wechat.js +10 -0
  79. package/lib/base-app-domain/ActionDefDict.d.ts +1 -0
  80. package/lib/base-app-domain/ActionDefDict.js +3 -1
  81. package/lib/base-app-domain/EntityDict.d.ts +86 -1
  82. package/lib/base-app-domain/Log/Storage.js +4 -0
  83. package/lib/base-app-domain/Log/_baseSchema.d.ts +5 -0
  84. package/lib/base-app-domain/Storage.js +3 -1
  85. package/lib/base-app-domain/StyleDict.js +3 -1
  86. package/lib/base-app-domain/Version/Action.d.ts +6 -0
  87. package/lib/base-app-domain/Version/Action.js +5 -0
  88. package/lib/base-app-domain/Version/Schema.d.ts +19 -0
  89. package/lib/base-app-domain/Version/Schema.js +4 -0
  90. package/lib/base-app-domain/Version/Storage.d.ts +3 -0
  91. package/lib/base-app-domain/Version/Storage.js +57 -0
  92. package/lib/base-app-domain/Version/Style.d.ts +3 -0
  93. package/lib/base-app-domain/Version/Style.js +9 -0
  94. package/lib/base-app-domain/Version/_baseSchema.d.ts +57 -0
  95. package/lib/base-app-domain/Version/_baseSchema.js +2 -0
  96. package/lib/compiler/dependencyBuilder.d.ts +1 -0
  97. package/lib/compiler/dependencyBuilder.js +173 -54
  98. package/lib/compiler/routerBuilder.d.ts +4 -1
  99. package/lib/compiler/routerBuilder.js +22 -19
  100. package/lib/compiler/schemaBuilder.d.ts +5 -1
  101. package/lib/compiler/schemaBuilder.js +10 -0
  102. package/lib/context/BackendRuntimeContext.d.ts +9 -1
  103. package/lib/context/BackendRuntimeContext.js +13 -1
  104. package/lib/entities/Log.d.ts +2 -0
  105. package/lib/entities/Log.js +2 -0
  106. package/lib/entities/Version.d.ts +14 -0
  107. package/lib/entities/Version.js +43 -0
  108. package/lib/store/AsyncRowStore.d.ts +4 -1
  109. package/lib/store/AsyncRowStore.js +7 -0
  110. package/lib/store/CascadeStore.js +22 -0
  111. package/lib/store/RelationAuth/authorizationFilter.d.ts +11 -0
  112. package/lib/store/RelationAuth/authorizationFilter.js +110 -0
  113. package/lib/store/RelationAuth/continuation.d.ts +9 -0
  114. package/lib/store/RelationAuth/continuation.js +37 -0
  115. package/lib/store/RelationAuth/index.d.ts +141 -0
  116. package/lib/store/RelationAuth/index.js +1445 -0
  117. package/lib/store/RelationAuth/model.d.ts +38 -0
  118. package/lib/store/RelationAuth/model.js +27 -0
  119. package/lib/store/RelationAuth/userLookup.d.ts +21 -0
  120. package/lib/store/RelationAuth/userLookup.js +105 -0
  121. package/lib/store/SyncRowStore.d.ts +2 -1
  122. package/lib/store/SyncRowStore.js +3 -0
  123. package/lib/store/filter.fix.d.ts +128 -0
  124. package/lib/store/filter.fix.js +2876 -0
  125. package/lib/store/filter.js +14 -9
  126. package/lib/store/triggers.js +239 -44
  127. package/lib/types/Context.d.ts +2 -1
  128. package/lib/types/Demand.d.ts +11 -1
  129. package/lib/types/Environment.d.ts +13 -2
  130. package/lib/types/Exception.d.ts +3 -3
  131. package/lib/types/Exception.js +25 -22
  132. package/lib/types/Expression.d.ts +4 -1
  133. package/lib/types/Expression.js +1 -1
  134. package/lib/types/Storage.d.ts +15 -2
  135. package/lib/utils/SimpleConnector.js +5 -2
  136. package/lib/utils/decimal.js +2 -1
  137. package/lib/utils/fetch/index.ali.d.ts +3 -0
  138. package/lib/utils/fetch/index.ali.js +36 -0
  139. package/lib/utils/fetch/index.d.ts +3 -0
  140. package/lib/utils/fetch/index.js +7 -0
  141. package/lib/utils/fetch/index.mp.d.ts +5 -3
  142. package/lib/utils/fetch/index.mp.js +5 -282
  143. package/lib/utils/fetch/index.wechat.d.ts +5 -0
  144. package/lib/utils/fetch/index.wechat.js +180 -0
  145. package/lib/utils/fetch/mini-program.shared.d.ts +37 -0
  146. package/lib/utils/fetch/mini-program.shared.js +96 -0
  147. package/lib/utils/geo.d.ts +1 -1
  148. package/lib/utils/geo.js +1 -1
  149. package/lib/utils/random/random.ali.d.ts +6 -0
  150. package/lib/utils/random/random.ali.js +24 -0
  151. package/lib/utils/random/random.mp.d.ts +5 -1
  152. package/lib/utils/random/random.mp.js +11 -5
  153. package/lib/utils/random/random.native.d.ts +1 -1
  154. package/lib/utils/random/random.native.js +7 -5
  155. package/lib/utils/random/random.wechat.d.ts +1 -0
  156. package/lib/utils/random/random.wechat.js +13 -0
  157. package/package.json +8 -5
  158. package/es/store/RelationAuth.d.ts +0 -115
  159. package/es/store/RelationAuth.js +0 -1340
  160. package/lib/store/RelationAuth.d.ts +0 -115
  161. package/lib/store/RelationAuth.js +0 -1345
@@ -6,6 +6,7 @@ export const entityDesc = {
6
6
  attr: {
7
7
  entity: '关联对象',
8
8
  entityId: '关联对象ID',
9
+ version: '所属版本',
9
10
  iState: '状态'
10
11
  },
11
12
  action: {
@@ -24,6 +25,7 @@ export const entityDesc = {
24
25
  attr: {
25
26
  entity: 'Entity',
26
27
  entityId: 'Entity ID',
28
+ version: 'Version',
27
29
  iState: 'Status',
28
30
  },
29
31
  action: {
@@ -0,0 +1,14 @@
1
+ import { String } from "../types/DataType";
2
+ import { EntityShape } from "../types/Entity";
3
+ import { Schema as User } from "./User";
4
+ import { Schema as Log } from "./Log";
5
+ export interface Schema extends EntityShape {
6
+ entity: String<32>;
7
+ entityId: String<64>;
8
+ name: String<128>;
9
+ operator?: User;
10
+ log?: Log;
11
+ iState: 'normal' | 'rollbacked';
12
+ }
13
+ export type IAction = 'undo' | 'redo';
14
+ export type Action = IAction;
@@ -0,0 +1,42 @@
1
+ ;
2
+ const entityDesc = {
3
+ locales: {
4
+ zh_CN: {
5
+ name: '版本',
6
+ attr: {
7
+ entity: '关联对象',
8
+ entityId: '关联对象ID',
9
+ name: '名称',
10
+ operator: '操作者',
11
+ log: '日志',
12
+ iState: '状态',
13
+ },
14
+ action: { undo: '回滚', redo: '重做' },
15
+ v: { iState: { normal: '正常的', rollbacked: '已回滚' } },
16
+ },
17
+ en_US: {
18
+ name: 'Version',
19
+ attr: {
20
+ entity: 'Entity',
21
+ entityId: 'Entity ID',
22
+ name: 'Name',
23
+ operator: 'Operator',
24
+ log: 'Log',
25
+ iState: 'Status',
26
+ },
27
+ action: { undo: 'Undo', redo: 'Redo' },
28
+ v: { iState: { normal: 'Normal', rollbacked: 'Rolled back' } },
29
+ },
30
+ },
31
+ indexes: [
32
+ {
33
+ name: 'index_log_unique',
34
+ attributes: [{ name: 'log' }],
35
+ config: { unique: true },
36
+ },
37
+ ],
38
+ style: {
39
+ icon: { undo: '', redo: '' },
40
+ },
41
+ };
42
+ export {};
@@ -9,6 +9,7 @@ export declare abstract class AsyncContext<ED extends EntityDict & BaseEntityDic
9
9
  private uuid?;
10
10
  opRecords: OpRecord<ED>[];
11
11
  private scene?;
12
+ private aspectName?;
12
13
  headers?: IncomingHttpHeaders;
13
14
  clusterInfo?: ClusterInfo;
14
15
  opResult: OperationResult<ED>;
@@ -25,6 +26,8 @@ export declare abstract class AsyncContext<ED extends EntityDict & BaseEntityDic
25
26
  restartToExecute(routine: (context: this) => Promise<any>): Promise<void>;
26
27
  getHeader(key: string): string | string[] | undefined;
27
28
  getScene(): string | undefined;
29
+ getAspectName(): string | undefined;
30
+ setAspectName(aspectName?: string): void;
28
31
  setScene(scene?: string): void;
29
32
  private resetEvents;
30
33
  on(event: 'commit' | 'rollback', callback: (records: OpRecord<ED>[], cxtStr: string) => Promise<void>): void;
@@ -53,7 +56,7 @@ export declare abstract class AsyncContext<ED extends EntityDict & BaseEntityDic
53
56
  setMessage(message: string): void;
54
57
  getMessage(): string | undefined;
55
58
  abstract isRoot(): boolean;
56
- abstract getCurrentUserId<T extends boolean>(allowUnloggedIn?: T): T extends true ? string | undefined : string;
59
+ abstract getCurrentUserId<T extends boolean = false>(allowUnloggedIn?: T): T extends true ? string | undefined : string;
57
60
  abstract setCurrentUserId(userId: string | undefined): void;
58
61
  abstract toString(): Promise<string>;
59
62
  protected abstract getSerializedData(): Promise<object>;
@@ -10,6 +10,7 @@ export class AsyncContext {
10
10
  uuid;
11
11
  opRecords;
12
12
  scene;
13
+ aspectName;
13
14
  headers;
14
15
  clusterInfo;
15
16
  opResult;
@@ -53,6 +54,12 @@ export class AsyncContext {
53
54
  getScene() {
54
55
  return this.scene;
55
56
  }
57
+ getAspectName() {
58
+ return this.aspectName;
59
+ }
60
+ setAspectName(aspectName) {
61
+ this.aspectName = aspectName;
62
+ }
56
63
  setScene(scene) {
57
64
  this.scene = scene;
58
65
  }
@@ -119,6 +119,13 @@ export function polishSelection(schema, entity, selection, context, option) {
119
119
  const checkSortAttr = (e2, sortAttr, projNode) => {
120
120
  const necessaryAttrs = [];
121
121
  for (const attr in sortAttr) {
122
+ if (attr.toLowerCase().startsWith(EXPRESSION_PREFIX)) {
123
+ const expressionAttrs = getAttrRefInExpression(sortAttr[attr]);
124
+ const expressionNodes = Object.keys(expressionAttrs);
125
+ assert(expressionNodes.every((nodeName) => nodeName === '#current'), 'sorter expression only supports attributes on the current node');
126
+ necessaryAttrs.push(...(expressionAttrs['#current'] || []));
127
+ continue;
128
+ }
122
129
  const rel = judgeRelation(schema, e2, attr);
123
130
  if (typeof rel === 'number' && [0, 1].includes(rel)) {
124
131
  necessaryAttrs.push(attr);
@@ -232,6 +239,21 @@ export function polishSelection(schema, entity, selection, context, option) {
232
239
  // 如果对象上有relation关系,在此将本用户相关的relation和actionAuth全部取出
233
240
  // 还要将actionAuth上没有relation关系但destEntity为本对象的行也全部取出,这些是指向userId的可能路径
234
241
  // 放在这里有点怪异,暂先这样
242
+ // 前端回放只凭目标 Version/Log 是否存在来判断“从目标到历史末端”的数据是否已经取全。
243
+ // 因此普通 Aspect 不能把一段不完整的历史关联写入 Cache,否则 replayVersion 会把残缺数据
244
+ // 当成完整历史。这里复用 projection 原有的递归过程逐层检查,不需要再遍历一次整棵 projection。
245
+ // getVersionData 会一次返回完整历史链;dontCollect 查询不会进入前端 Cache,二者均可放行。
246
+ const aspectName = context?.getAspectName();
247
+ if (aspectName && aspectName !== 'getVersionData' && !option?.dontCollect) {
248
+ assert(entity2 !== 'oper' || !Object.prototype.hasOwnProperty.call(projectionNode, 'logId'), 'oper.logId and oper.log can only be selected by getVersionData');
249
+ assert(entity2 !== 'version'
250
+ || (!Object.prototype.hasOwnProperty.call(projectionNode, 'log')
251
+ && !Object.prototype.hasOwnProperty.call(projectionNode, 'log$version')), 'version.log and version.log$version can only be selected by getVersionData');
252
+ assert(entity2 !== 'log'
253
+ || (!Object.prototype.hasOwnProperty.call(projectionNode, 'version')
254
+ && !Object.prototype.hasOwnProperty.call(projectionNode, 'version$log')
255
+ && !Object.prototype.hasOwnProperty.call(projectionNode, 'oper$log')), 'log.version, log.version$log and log.oper$log can only be selected by getVersionData');
256
+ }
235
257
  if (context && !option?.dontCollect) {
236
258
  const userId = context.getCurrentUserId(true);
237
259
  if (userId && !SYSTEM_RESERVE_ENTITIES.includes(entity2)) {
@@ -0,0 +1,11 @@
1
+ import { EntityDict } from '../../base-app-domain';
2
+ import { EntityDict as BaseEntityDict, StorageSchema } from '../../types';
3
+ /** 稳定序列化 filter,用于授权分支去重;循环引用只保留为不透明节点。 */
4
+ export declare const stableFilterKey: (value: unknown, ancestors?: WeakSet<object>) => string;
5
+ /** 检测用户传入或缓存中的授权 filter 是否带循环引用。 */
6
+ export declare const hasCyclicFilter: (value: unknown, ancestors?: WeakSet<object>) => boolean;
7
+ /**
8
+ * 在不改变授权集合的前提下裁剪 OR 分支(§3.3 授权合成只减少查询,不扩大权限)。
9
+ * 对授权过滤器集合 P_i,有 ⋃ P_i = ⋃ {P_i | 不存在 P_i ⊆ P_j 且 i ≠ j}。
10
+ */
11
+ export declare function minimizeAuthorizationUnion<ED extends EntityDict & BaseEntityDict, T extends keyof ED>(entity: T, schema: StorageSchema<ED>, filters: ED[T]['Filter'][]): ED[T]['Filter'][];
@@ -0,0 +1,104 @@
1
+ import { contains } from '../filter.fix';
2
+ /** 稳定序列化 filter,用于授权分支去重;循环引用只保留为不透明节点。 */
3
+ export const stableFilterKey = (value, ancestors = new WeakSet()) => {
4
+ if (value === null) {
5
+ return 'null';
6
+ }
7
+ if (value === undefined) {
8
+ return 'undefined';
9
+ }
10
+ if (typeof value === 'number') {
11
+ if (Number.isNaN(value)) {
12
+ return 'number:NaN';
13
+ }
14
+ if (value === Infinity) {
15
+ return 'number:Infinity';
16
+ }
17
+ if (value === -Infinity) {
18
+ return 'number:-Infinity';
19
+ }
20
+ return `number:${Object.is(value, -0) ? '-0' : String(value)}`;
21
+ }
22
+ if (typeof value === 'string') {
23
+ return `string:${JSON.stringify(value)}`;
24
+ }
25
+ if (typeof value === 'boolean') {
26
+ return `boolean:${value}`;
27
+ }
28
+ if (typeof value === 'bigint') {
29
+ return `bigint:${value.toString()}`;
30
+ }
31
+ if (value instanceof Date) {
32
+ return `Date(${value.getTime()})`;
33
+ }
34
+ if (Array.isArray(value)) {
35
+ if (ancestors.has(value)) {
36
+ console.warn('RelationAuth: cyclic authorization filter; keeping the branch opaque');
37
+ return '[Circular]';
38
+ }
39
+ ancestors.add(value);
40
+ try {
41
+ return `[${value.map((item) => stableFilterKey(item, ancestors)).join(',')}]`;
42
+ }
43
+ finally {
44
+ ancestors.delete(value);
45
+ }
46
+ }
47
+ if (value && typeof value === 'object') {
48
+ if (ancestors.has(value)) {
49
+ console.warn('RelationAuth: cyclic authorization filter; keeping the branch opaque');
50
+ return '{Circular}';
51
+ }
52
+ ancestors.add(value);
53
+ try {
54
+ const record = value;
55
+ return `{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${stableFilterKey(record[key], ancestors)}`).join(',')}}`;
56
+ }
57
+ finally {
58
+ ancestors.delete(value);
59
+ }
60
+ }
61
+ return JSON.stringify(value) ?? String(value);
62
+ };
63
+ /** 检测用户传入或缓存中的授权 filter 是否带循环引用。 */
64
+ export const hasCyclicFilter = (value, ancestors = new WeakSet()) => {
65
+ if (!value || typeof value !== 'object' || value instanceof Date) {
66
+ return false;
67
+ }
68
+ if (ancestors.has(value)) {
69
+ return true;
70
+ }
71
+ ancestors.add(value);
72
+ try {
73
+ if (Array.isArray(value)) {
74
+ return value.some((item) => hasCyclicFilter(item, ancestors));
75
+ }
76
+ return Object.values(value).some((item) => hasCyclicFilter(item, ancestors));
77
+ }
78
+ finally {
79
+ ancestors.delete(value);
80
+ }
81
+ };
82
+ /**
83
+ * 在不改变授权集合的前提下裁剪 OR 分支(§3.3 授权合成只减少查询,不扩大权限)。
84
+ * 对授权过滤器集合 P_i,有 ⋃ P_i = ⋃ {P_i | 不存在 P_i ⊆ P_j 且 i ≠ j}。
85
+ */
86
+ export function minimizeAuthorizationUnion(entity, schema, filters) {
87
+ if (filters.some((filter) => hasCyclicFilter(filter))) {
88
+ console.warn('RelationAuth: cyclic authorization filter; preserving all authorization branches');
89
+ return filters;
90
+ }
91
+ const unique = [...new Map(filters.map((filter) => [stableFilterKey(filter), filter])).values()];
92
+ return unique.filter((candidate, candidateIndex) => unique.every((other, otherIndex) => {
93
+ if (candidateIndex === otherIndex) {
94
+ return true;
95
+ }
96
+ if (contains(entity, schema, candidate, other) !== true) {
97
+ return true;
98
+ }
99
+ if (contains(entity, schema, other, candidate) === true) {
100
+ return otherIndex < candidateIndex;
101
+ }
102
+ return false;
103
+ }));
104
+ }
@@ -0,0 +1,9 @@
1
+ /** IO 叶子的结果形态:同步上下文为 T,异步上下文为 Promise<T>。 */
2
+ export type Tr<T> = T | Promise<T>;
3
+ /**
4
+ * 单源接续子:value 为 Promise 时链接续延,否则同步求值;续延返回 Promise 时结果扁平化。
5
+ * 同步链路全程不产生 Promise。
6
+ */
7
+ export declare function bind<R, S>(value: Tr<R>, next: (v: R) => S | Promise<S>): Tr<S>;
8
+ /** 顺序串联(保序):全部同步 ⇒ 同步结果,不产生 Promise。 */
9
+ export declare function allBind<T, R>(values: Tr<T>[], next: (v: T[]) => R | Promise<R>): Tr<R>;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * 单源接续子:value 为 Promise 时链接续延,否则同步求值;续延返回 Promise 时结果扁平化。
3
+ * 同步链路全程不产生 Promise。
4
+ */
5
+ export function bind(value, next) {
6
+ if (value instanceof Promise) {
7
+ return value.then((v) => {
8
+ const r = next(v);
9
+ return r instanceof Promise ? r : r;
10
+ });
11
+ }
12
+ const r = next(value);
13
+ return r instanceof Promise ? r : r;
14
+ }
15
+ /** 顺序串联(保序):全部同步 ⇒ 同步结果,不产生 Promise。 */
16
+ export function allBind(values, next) {
17
+ const result = [];
18
+ let index = 0;
19
+ const step = () => {
20
+ while (index < values.length) {
21
+ const current = values[index++];
22
+ if (current instanceof Promise) {
23
+ return current.then((v) => {
24
+ result.push(v);
25
+ return step();
26
+ });
27
+ }
28
+ result.push(current);
29
+ }
30
+ return next(result);
31
+ };
32
+ return step();
33
+ }
@@ -0,0 +1,141 @@
1
+ import { EntityDict } from "../../base-app-domain";
2
+ import { StorageSchema } from "../../types";
3
+ import { EntityDict as BaseEntityDict, AuthDeduceRelationMap } from "../../types/Entity";
4
+ import { AsyncContext } from "../AsyncRowStore";
5
+ import { SyncContext } from "../SyncRowStore";
6
+ /**
7
+ * RelationAuth —— 按 docs/permission-model.md(v0.5)重写的权限判定内核。
8
+ *
9
+ * 不变量映射:
10
+ * - I2 逐层必检:读路径以 `ReadTask` 链表达投影 OTM 骨架上的每个节点任务(D1/D2/D6),
11
+ * 根任务无条件执行;MTO 投影不构成任务(D5/I3)。
12
+ * - I3 MTO 零成本:`buildReadTasks` 不沿 MTO 边下降,不为其发起任何查询。
13
+ * - I4 双模式同结果:判定逻辑单源;sync/async 分叉只发生在 IO 叶子,由 `bind`/`allBind`
14
+ * 统一接续(同步输入 ⇒ 同步输出,不产生 Promise)。
15
+ * - I5 deduce 单点:`edgeRuleOf(...) === 'deduce'` 是唯一策略谓词;树形剪枝、投影不展开、
16
+ * 写侧放行、多态折算全部经它分派。
17
+ * - I6 豁免粒度:selectFree/updateFree 只把任务标记为 exempt(豁免节点自身行集要求),
18
+ * 不删除任务、不豁免链上其它节点(D4)。
19
+ * - I7 写语义保持:destructOperation/checkOperationTree 结构与现实现同构,仅把路径
20
+ * 拼接替换为 I8 原语。
21
+ * - I8 边传播单源:`deriveChildPath` 是唯一的授权路径派生原语——OTM 前缀边属性名、
22
+ * MTO 剥首段、deduce 零检查;读写共用。
23
+ * - E1 自顶向下短路:任务链按根→叶顺序求值,首个失败立即返回(异常在 checkActions
24
+ * 唯一汇点抛出,种类与时机读写一致)。
25
+ * - E2 扩展路径快路:父任务经见证 actionAuth 行通过后,其路径沿边派生为子任务候选见证,
26
+ * 仍以 actionAuth 数据存在为生效条件;E3 禁止无数据合成继承。
27
+ * - 保守拒绝:判别器伪造、目标歧义(反查 2 行封顶)、未知属性、$not/$xor 删减、环结构
28
+ * filter 一律不放大授权面。
29
+ */
30
+ export declare class RelationAuth<ED extends EntityDict & BaseEntityDict> {
31
+ private authDeduceRelationMap;
32
+ private schema;
33
+ static SPECIAL_ENTITIES: string[];
34
+ private selectFreeEntities;
35
+ private updateFreeDict;
36
+ constructor(schema: StorageSchema<ED>, authDeduceRelationMap: AuthDeduceRelationMap<ED>, selectFreeEntities?: (keyof ED)[], updateFreeDict?: {
37
+ [A in keyof ED]?: string[];
38
+ });
39
+ checkRelationSync<T extends keyof ED, Cxt extends SyncContext<ED>>(entity: T, operation: Omit<ED[T]['Operation'] | ED[T]['Selection'], 'id'>, context: Cxt): void;
40
+ checkRelationAsync<T extends keyof ED, Cxt extends AsyncContext<ED>>(entity: T, operation: Omit<ED[T]['Operation'] | ED[T]['Selection'], 'id'>, context: Cxt): Promise<void>;
41
+ /** 节点自身是否不参与对象授权判定:selectFree 只豁免自身,特殊系统实体沿用旧行为整节点豁免。 */
42
+ private isSelfExemptEntity;
43
+ /** I5:deduce 策略唯一定义点 */
44
+ private edgeRuleOf;
45
+ /**
46
+ * 检查当前用户有无权限对filter约束的userRelation进行action操作(§6 特殊实体)
47
+ */
48
+ private checkUserRelation;
49
+ private checkOperateSpecialEntities;
50
+ private isKnownEntity;
51
+ private collectStaticDeduceTargets;
52
+ /**
53
+ * deduce 折算(I5 消费方):实体权限可折算为其多态 entity/entityId 所指父对象的权限。
54
+ * 候选集合是并集 U(root) ∪ ⋃U(targetᵢ);任何不能静态确证目标的情形都只保留根候选
55
+ * (宁可保守拒绝,不能任选一行扩大授权)。
56
+ */
57
+ private getDeducedEntityFilters;
58
+ /**
59
+ * 投影侧 filter 精修(§4.1 filter 三语义位置):
60
+ * 只保留能约束授权路径的属性,同时返回投影是否保持了原表达式。
61
+ * 若原查询为 Q=A∧B,删除 B 得到 A,则 Q⊆A,适合作为授权检查的安全上界;
62
+ * 若 Q=A∨B,只要每个分支都向上近似,A∨B⊆A'∨B' 仍成立。
63
+ * 但 NOT 和 XOR 不满足这种单调性:其内部发生删减时丢弃整个 NOT/XOR,
64
+ * 宁可少优化也不能误拒绝或扩大授权。
65
+ */
66
+ private refineFilter;
67
+ /**
68
+ * 读路径任务链(D1/D2/D6,I2 逐层必检):
69
+ * 投影 OTM 骨架上每个节点生成一个独立任务;任务 filter = 精修后的根 filter 沿
70
+ * OTM 链逐层压平(left-join 语义下父链约束只由外键嵌入表达,不含子存在性约束,D6)。
71
+ * MTO 投影不生成任务(D5/I3);deduce 节点不展开(I5),其可见性由锚点节点任务担保。
72
+ * 根任务无条件生成——包括根下挂 OTM(旧"只检最深叶"的托付已废弃,D3)与根 selectFree
73
+ * (selectFree 只豁免自身,链上其它节点照常生成任务;根自身的豁免由 exempt 标记表达)。
74
+ */
75
+ private buildReadTasks;
76
+ /**
77
+ * 单个读任务的包含性判定:filter ⊆ Auth(entity,'select')。
78
+ * 返回 [是否通过, 通过见证(可用于 E2 扩展的 actionAuth 行)]。
79
+ */
80
+ private checkReadTask;
81
+ /**
82
+ * E2:把父任务通过见证沿边派生为子任务的扩展路径(I8 原语),
83
+ * 并在 actionAuth 中以数据见证确认扩展路径的授权行存在;不存在则回落正常判定。
84
+ */
85
+ private probeWitnesses;
86
+ /**
87
+ * 任务的授权并集判定:对每个 deduce 候选(OR 语义,通过一个即可),
88
+ * 以该实体全部可用 actionAuth 的 scope 并集(§3.3)做包含性检查(静态优先,
89
+ * 数据比对兜底,不确定即拒绝)。返回所有使任务通过的见证行(供 E2)。
90
+ */
91
+ private checkTaskAgainstAuths;
92
+ /** scope 合成:destEntity 匹配的授权行(并集成员,§3.3) */
93
+ private collectScopeAuths;
94
+ /** F ⊆ ⋃scope(minimize 后合成,数据比对兜底) */
95
+ private scopeContainsFilter;
96
+ private checkSelection;
97
+ /**
98
+ * 对于operation,解构出一个树形结构,以方便自顶向下的进行访问
99
+ * 但对于deduce的子对象,不必再向底层查看(I5:addChild 内经 edgeRuleOf 剪枝)
100
+ */
101
+ private destructOperation;
102
+ /**
103
+ * 授权 scope filter 展开:把 path(destEntity 走向 user 的属性链)与末端谓词 filter
104
+ * 合成为 destEntity 上的 filter。
105
+ */
106
+ private makePathFilter;
107
+ /**
108
+ * 对所有满足操作要求的actionAuth加以判断,找到可以满足当前用户身份的actionAuth
109
+ */
110
+ private filterActionAuths;
111
+ /**
112
+ * 对于有些特殊的查询(带很多$or的查询,多发生在系统级别),单个actionAuth无法满足,需要共同加以判定
113
+ */
114
+ private checkActionAuthInGroup;
115
+ /**
116
+ * 此函数判定一个结点是否能通过权限检测,同时寻找该结点本身对象上成立的actionAuth,用于本结点子孙结点的快速检测
117
+ * 如果结点因其deduce的对象通过了检测,其被推断对象的actionAuth无法用于更低对象的权限检测
118
+ */
119
+ private findActionAuthsOnNode;
120
+ /**
121
+ * 级联树边解析(I8 前置)。destructOperation 的键形:
122
+ * - OTM 反指:键 = 父数据上的反指属性名 `子实体名$边名`('comment$post'),
123
+ * 前缀传播,边名 = 尾段;
124
+ * - MTO 正向/多态折算:键 = `父实体名$attr`('comment$user'、批式 'post$entity')或
125
+ * 裸 attr(历史手工树),剥首段,边名 = 子指向父的外键基名。
126
+ * 键形约定存在碰撞(如 'post$entity'),以 schema 双向外键探测定方向:
127
+ * 父侧存在名为尾段的 ref→子 外键 ⇒ MTO;否则子侧存在 ⇒ OTM;再否则多态折算 ⇒ MTO
128
+ * 以 'entity' 折算边;都不可解析 ⇒ 保守不传播。
129
+ */
130
+ private resolveTreeEdge;
131
+ private checkOperationTree;
132
+ private checkOperation;
133
+ /**
134
+ * 检查一个operation是否能被通过权限测试。
135
+ * 判定汇点唯一(I4):读→任务链(E1 短路,失败 OakDataInvisible),
136
+ * 写→级联树自顶向下(失败 OakOperationUnpermitted);
137
+ * 异常种类与时机由本函数单点决定,读写内核不再各自抛异常。
138
+ */
139
+ private checkActions;
140
+ }
141
+ export { getUserRelationsByActions } from './userLookup';