@variojs/core 0.0.2 → 0.1.1

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 (149) hide show
  1. package/dist/index.d.ts +830 -17
  2. package/dist/index.js +9 -1
  3. package/dist/index.js.map +1 -1
  4. package/package.json +6 -5
  5. package/dist/errors.d.ts +0 -100
  6. package/dist/errors.d.ts.map +0 -1
  7. package/dist/errors.js +0 -4
  8. package/dist/errors.js.map +0 -1
  9. package/dist/expression/cache.d.ts +0 -53
  10. package/dist/expression/cache.d.ts.map +0 -1
  11. package/dist/expression/cache.js +0 -1
  12. package/dist/expression/cache.js.map +0 -1
  13. package/dist/expression/compiler.d.ts +0 -34
  14. package/dist/expression/compiler.d.ts.map +0 -1
  15. package/dist/expression/compiler.js +0 -1
  16. package/dist/expression/compiler.js.map +0 -1
  17. package/dist/expression/dependencies.d.ts +0 -17
  18. package/dist/expression/dependencies.d.ts.map +0 -1
  19. package/dist/expression/dependencies.js +0 -1
  20. package/dist/expression/dependencies.js.map +0 -1
  21. package/dist/expression/evaluate.d.ts +0 -22
  22. package/dist/expression/evaluate.d.ts.map +0 -1
  23. package/dist/expression/evaluate.js +0 -2
  24. package/dist/expression/evaluate.js.map +0 -1
  25. package/dist/expression/evaluator.d.ts +0 -22
  26. package/dist/expression/evaluator.d.ts.map +0 -1
  27. package/dist/expression/evaluator.js +0 -1
  28. package/dist/expression/evaluator.js.map +0 -1
  29. package/dist/expression/index.d.ts +0 -12
  30. package/dist/expression/index.d.ts.map +0 -1
  31. package/dist/expression/index.js +0 -1
  32. package/dist/expression/index.js.map +0 -1
  33. package/dist/expression/parser.d.ts +0 -15
  34. package/dist/expression/parser.d.ts.map +0 -1
  35. package/dist/expression/parser.js +0 -1
  36. package/dist/expression/parser.js.map +0 -1
  37. package/dist/expression/utils.d.ts +0 -46
  38. package/dist/expression/utils.d.ts.map +0 -1
  39. package/dist/expression/utils.js +0 -1
  40. package/dist/expression/utils.js.map +0 -1
  41. package/dist/expression/whitelist.d.ts +0 -24
  42. package/dist/expression/whitelist.d.ts.map +0 -1
  43. package/dist/expression/whitelist.js +0 -3
  44. package/dist/expression/whitelist.js.map +0 -1
  45. package/dist/index.d.ts.map +0 -1
  46. package/dist/runtime/context.d.ts +0 -8
  47. package/dist/runtime/context.d.ts.map +0 -1
  48. package/dist/runtime/context.js +0 -1
  49. package/dist/runtime/context.js.map +0 -1
  50. package/dist/runtime/create-context.d.ts +0 -20
  51. package/dist/runtime/create-context.d.ts.map +0 -1
  52. package/dist/runtime/create-context.js +0 -1
  53. package/dist/runtime/create-context.js.map +0 -1
  54. package/dist/runtime/index.d.ts +0 -10
  55. package/dist/runtime/index.d.ts.map +0 -1
  56. package/dist/runtime/index.js +0 -1
  57. package/dist/runtime/index.js.map +0 -1
  58. package/dist/runtime/loop-context-pool.d.ts +0 -58
  59. package/dist/runtime/loop-context-pool.d.ts.map +0 -1
  60. package/dist/runtime/loop-context-pool.js +0 -1
  61. package/dist/runtime/loop-context-pool.js.map +0 -1
  62. package/dist/runtime/path.d.ts +0 -114
  63. package/dist/runtime/path.d.ts.map +0 -1
  64. package/dist/runtime/path.js +0 -1
  65. package/dist/runtime/path.js.map +0 -1
  66. package/dist/runtime/proxy.d.ts +0 -18
  67. package/dist/runtime/proxy.d.ts.map +0 -1
  68. package/dist/runtime/proxy.js +0 -1
  69. package/dist/runtime/proxy.js.map +0 -1
  70. package/dist/runtime/sandbox.d.ts +0 -20
  71. package/dist/runtime/sandbox.d.ts.map +0 -1
  72. package/dist/runtime/sandbox.js +0 -1
  73. package/dist/runtime/sandbox.js.map +0 -1
  74. package/dist/types.d.ts +0 -191
  75. package/dist/types.d.ts.map +0 -1
  76. package/dist/types.js +0 -1
  77. package/dist/types.js.map +0 -1
  78. package/dist/vm/errors.d.ts +0 -5
  79. package/dist/vm/errors.d.ts.map +0 -1
  80. package/dist/vm/errors.js +0 -1
  81. package/dist/vm/errors.js.map +0 -1
  82. package/dist/vm/executor.d.ts +0 -35
  83. package/dist/vm/executor.d.ts.map +0 -1
  84. package/dist/vm/executor.js +0 -2
  85. package/dist/vm/executor.js.map +0 -1
  86. package/dist/vm/handlers/array/pop.d.ts +0 -12
  87. package/dist/vm/handlers/array/pop.d.ts.map +0 -1
  88. package/dist/vm/handlers/array/pop.js +0 -1
  89. package/dist/vm/handlers/array/pop.js.map +0 -1
  90. package/dist/vm/handlers/array/push.d.ts +0 -13
  91. package/dist/vm/handlers/array/push.d.ts.map +0 -1
  92. package/dist/vm/handlers/array/push.js +0 -1
  93. package/dist/vm/handlers/array/push.js.map +0 -1
  94. package/dist/vm/handlers/array/shift.d.ts +0 -12
  95. package/dist/vm/handlers/array/shift.d.ts.map +0 -1
  96. package/dist/vm/handlers/array/shift.js +0 -1
  97. package/dist/vm/handlers/array/shift.js.map +0 -1
  98. package/dist/vm/handlers/array/splice.d.ts +0 -12
  99. package/dist/vm/handlers/array/splice.d.ts.map +0 -1
  100. package/dist/vm/handlers/array/splice.js +0 -1
  101. package/dist/vm/handlers/array/splice.js.map +0 -1
  102. package/dist/vm/handlers/array/unshift.d.ts +0 -13
  103. package/dist/vm/handlers/array/unshift.d.ts.map +0 -1
  104. package/dist/vm/handlers/array/unshift.js +0 -1
  105. package/dist/vm/handlers/array/unshift.js.map +0 -1
  106. package/dist/vm/handlers/array/utils.d.ts +0 -10
  107. package/dist/vm/handlers/array/utils.d.ts.map +0 -1
  108. package/dist/vm/handlers/array/utils.js +0 -1
  109. package/dist/vm/handlers/array/utils.js.map +0 -1
  110. package/dist/vm/handlers/batch.d.ts +0 -12
  111. package/dist/vm/handlers/batch.d.ts.map +0 -1
  112. package/dist/vm/handlers/batch.js +0 -1
  113. package/dist/vm/handlers/batch.js.map +0 -1
  114. package/dist/vm/handlers/call.d.ts +0 -14
  115. package/dist/vm/handlers/call.d.ts.map +0 -1
  116. package/dist/vm/handlers/call.js +0 -1
  117. package/dist/vm/handlers/call.js.map +0 -1
  118. package/dist/vm/handlers/emit.d.ts +0 -12
  119. package/dist/vm/handlers/emit.d.ts.map +0 -1
  120. package/dist/vm/handlers/emit.js +0 -1
  121. package/dist/vm/handlers/emit.js.map +0 -1
  122. package/dist/vm/handlers/if.d.ts +0 -13
  123. package/dist/vm/handlers/if.d.ts.map +0 -1
  124. package/dist/vm/handlers/if.js +0 -1
  125. package/dist/vm/handlers/if.js.map +0 -1
  126. package/dist/vm/handlers/index.d.ts +0 -14
  127. package/dist/vm/handlers/index.d.ts.map +0 -1
  128. package/dist/vm/handlers/index.js +0 -1
  129. package/dist/vm/handlers/index.js.map +0 -1
  130. package/dist/vm/handlers/log.d.ts +0 -12
  131. package/dist/vm/handlers/log.d.ts.map +0 -1
  132. package/dist/vm/handlers/log.js +0 -1
  133. package/dist/vm/handlers/log.js.map +0 -1
  134. package/dist/vm/handlers/loop.d.ts +0 -12
  135. package/dist/vm/handlers/loop.d.ts.map +0 -1
  136. package/dist/vm/handlers/loop.js +0 -1
  137. package/dist/vm/handlers/loop.js.map +0 -1
  138. package/dist/vm/handlers/navigate.d.ts +0 -12
  139. package/dist/vm/handlers/navigate.d.ts.map +0 -1
  140. package/dist/vm/handlers/navigate.js +0 -1
  141. package/dist/vm/handlers/navigate.js.map +0 -1
  142. package/dist/vm/handlers/set.d.ts +0 -15
  143. package/dist/vm/handlers/set.d.ts.map +0 -1
  144. package/dist/vm/handlers/set.js +0 -1
  145. package/dist/vm/handlers/set.js.map +0 -1
  146. package/dist/vm/index.d.ts +0 -8
  147. package/dist/vm/index.d.ts.map +0 -1
  148. package/dist/vm/index.js +0 -1
  149. package/dist/vm/index.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,18 +1,831 @@
1
+ import { SchemaNode } from '@variojs/schema';
2
+ import * as ESTree from '@babel/types';
3
+
1
4
  /**
2
- * @variojs/core - Vario Core Runtime
3
- *
4
- * 核心运行时模块,包含:
5
- * - RuntimeContext: 扁平化状态 + $ 前缀系统 API
6
- * - Expression System: 安全表达式求值
7
- * - Action VM: 动作虚拟机
8
- * - Path Utilities: 统一的路径解析工具
9
- */
10
- export { createRuntimeContext, createProxy, createExpressionSandbox, isSafePropertyAccess, createLoopContext, releaseLoopContext, parsePath, parsePathCached, clearPathCache, stringifyPath, getPathValue, setPathValue, matchPath, getParentPath, getLastSegment } from './runtime/index.js';
11
- export type { RuntimeContext, CreateContextOptions, OnStateChangeCallback } from './types.js';
12
- export type { PathSegment } from './runtime/path.js';
13
- export { parseExpression, validateAST, evaluateExpression, evaluate, extractExpression, extractDependencies, getCachedExpression, setCachedExpression, invalidateCache, clearCache, getCacheStats } from './expression/index.js';
14
- export { execute, registerBuiltinMethods } from './vm/index.js';
15
- export type { ExecuteOptions } from './vm/index.js';
16
- export { ActionError, ExpressionError, ServiceError, BatchError, VarioError, ErrorCodes, type ErrorContext, type ErrorCode } from './errors.js';
17
- export type { Action, ActionMap, ActionHandler, ExpressionCache, MethodsRegistry, ExpressionOptions } from './types.js';
18
- //# sourceMappingURL=index.d.ts.map
5
+ * Vario 错误处理体系
6
+ *
7
+ * 统一的错误基类和错误码系统
8
+ */
9
+
10
+ /**
11
+ * 错误上下文信息
12
+ */
13
+ interface ErrorContext {
14
+ /** Schema 路径(如 "events.click[0]") */
15
+ schemaPath?: string;
16
+ /** 表达式字符串 */
17
+ expression?: string;
18
+ /** 动作对象 */
19
+ action?: Action;
20
+ /** 调用栈(简化版) */
21
+ stack?: string[];
22
+ /** 额外上下文信息 */
23
+ metadata?: Record<string, unknown>;
24
+ }
25
+ /**
26
+ * Vario 错误基类
27
+ *
28
+ * 所有 Vario 相关错误都应继承此类
29
+ */
30
+ declare class VarioError extends Error {
31
+ /** 错误码 */
32
+ readonly code: string;
33
+ /** 错误上下文 */
34
+ readonly context: ErrorContext;
35
+ constructor(message: string, code: string, context?: ErrorContext);
36
+ /**
37
+ * 获取友好的错误消息
38
+ */
39
+ getFriendlyMessage(): string;
40
+ /**
41
+ * 转换为 JSON(用于序列化)
42
+ */
43
+ toJSON(): Record<string, unknown>;
44
+ }
45
+ /**
46
+ * 动作执行错误
47
+ */
48
+ declare class ActionError extends VarioError {
49
+ constructor(action: Action, message: string, code?: string, context?: Omit<ErrorContext, 'action'>);
50
+ }
51
+ /**
52
+ * 表达式求值错误
53
+ */
54
+ declare class ExpressionError extends VarioError {
55
+ constructor(expression: string, message: string, code?: string, context?: Omit<ErrorContext, 'expression'>);
56
+ }
57
+ /**
58
+ * 服务调用错误
59
+ */
60
+ declare class ServiceError extends VarioError {
61
+ readonly service: string;
62
+ readonly originalError?: Error;
63
+ constructor(service: string, message: string, originalError?: Error, context?: ErrorContext);
64
+ }
65
+ /**
66
+ * 批量执行错误
67
+ */
68
+ declare class BatchError extends VarioError {
69
+ readonly failedActions: Array<{
70
+ action: Action;
71
+ error: Error;
72
+ }>;
73
+ constructor(failedActions: Array<{
74
+ action: Action;
75
+ error: Error;
76
+ }>, message: string, context?: ErrorContext);
77
+ }
78
+ /**
79
+ * 错误码定义
80
+ */
81
+ declare const ErrorCodes: {
82
+ readonly ACTION_UNKNOWN_TYPE: "ACTION_UNKNOWN_TYPE";
83
+ readonly ACTION_EXECUTION_ERROR: "ACTION_EXECUTION_ERROR";
84
+ readonly ACTION_ABORTED: "ACTION_ABORTED";
85
+ readonly ACTION_TIMEOUT: "ACTION_TIMEOUT";
86
+ readonly ACTION_MAX_STEPS_EXCEEDED: "ACTION_MAX_STEPS_EXCEEDED";
87
+ readonly ACTION_MISSING_PARAM: "ACTION_MISSING_PARAM";
88
+ readonly ACTION_INVALID_PARAM: "ACTION_INVALID_PARAM";
89
+ readonly EXPRESSION_PARSE_ERROR: "EXPRESSION_PARSE_ERROR";
90
+ readonly EXPRESSION_VALIDATION_ERROR: "EXPRESSION_VALIDATION_ERROR";
91
+ readonly EXPRESSION_EVALUATION_ERROR: "EXPRESSION_EVALUATION_ERROR";
92
+ readonly EXPRESSION_TIMEOUT: "EXPRESSION_TIMEOUT";
93
+ readonly EXPRESSION_MAX_STEPS_EXCEEDED: "EXPRESSION_MAX_STEPS_EXCEEDED";
94
+ readonly EXPRESSION_UNSAFE_ACCESS: "EXPRESSION_UNSAFE_ACCESS";
95
+ readonly EXPRESSION_FUNCTION_NOT_WHITELISTED: "EXPRESSION_FUNCTION_NOT_WHITELISTED";
96
+ readonly SERVICE_NOT_FOUND: "SERVICE_NOT_FOUND";
97
+ readonly SERVICE_CALL_ERROR: "SERVICE_CALL_ERROR";
98
+ readonly BATCH_ERROR: "BATCH_ERROR";
99
+ readonly SCHEMA_VALIDATION_ERROR: "SCHEMA_VALIDATION_ERROR";
100
+ readonly SCHEMA_INVALID_ACTION: "SCHEMA_INVALID_ACTION";
101
+ };
102
+ type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
103
+
104
+ /**
105
+ * Vario Core Types
106
+ *
107
+ * 复杂类型集中在此定义,便于推演类型关系、保持业务/运行时逻辑代码简洁。
108
+ * 核心:RuntimeContext、路径值推导(GetPathValue/SetPathValue)、上下文创建选项(CreateContextOptions)等。
109
+ */
110
+ /**
111
+ * 方法处理器类型
112
+ * 所有注册到 $methods 的方法必须符合此签名
113
+ *
114
+ * 注意:动作处理器也通过 $methods 注册,但它们的参数是 Action
115
+ */
116
+ type MethodHandler<TParams = unknown, TResult = unknown> = (ctx: RuntimeContext, params: TParams) => Promise<TResult> | TResult;
117
+ /**
118
+ * 动作处理器类型(特殊的方法处理器)
119
+ */
120
+ type ActionHandler = MethodHandler<Action, void>;
121
+ /**
122
+ * 方法注册表类型
123
+ * 支持普通方法和指令处理器
124
+ */
125
+ type MethodsRegistry = Record<string, MethodHandler>;
126
+ /**
127
+ * 表达式求值选项
128
+ */
129
+ interface ExpressionOptions {
130
+ /**
131
+ * 是否允许访问全局对象(默认 false)
132
+ */
133
+ allowGlobals?: boolean;
134
+ /**
135
+ * 最大求值步数
136
+ */
137
+ maxSteps?: number;
138
+ /**
139
+ * 求值超时(毫秒)
140
+ */
141
+ timeout?: number;
142
+ /**
143
+ * 最大嵌套深度(防止 DoS 攻击,默认 50)
144
+ */
145
+ maxNestingDepth?: number;
146
+ }
147
+ /**
148
+ * 运行时上下文接口
149
+ * 扁平化状态设计:直接属性访问,无 models. 前缀
150
+ *
151
+ * @template TState 状态类型,用于类型推导和约束
152
+ *
153
+ * 设计说明:
154
+ * - 使用接口 + 索引签名,既支持类型推导又支持动态属性
155
+ * - 状态属性通过 TState 泛型约束
156
+ * - 系统 API 使用具体类型定义,确保类型安全
157
+ */
158
+ /**
159
+ * 运行时上下文类型
160
+ * 使用交叉类型结合接口,既支持状态类型推导又支持动态属性
161
+ */
162
+ type RuntimeContext<TState extends Record<string, unknown> = Record<string, unknown>> = TState & {
163
+ $emit: (event: string, data?: unknown) => void;
164
+ $methods: MethodsRegistry;
165
+ $exprOptions?: ExpressionOptions;
166
+ /**
167
+ * 事件值(在事件处理中可用)
168
+ * - 对于 DOM 原生事件:Event 对象
169
+ * - 对于 Vue 组件 emit:emit 的参数值(如 string[]、number 等)
170
+ */
171
+ $event?: unknown;
172
+ $item?: TState[keyof TState];
173
+ $index?: number;
174
+ _get: <TPath extends string>(path: TPath) => GetPathValue<TState, TPath>;
175
+ _set: <TPath extends string>(path: TPath, value: SetPathValue<TState, TPath>, options?: {
176
+ skipCallback?: boolean;
177
+ }) => void;
178
+ } & Record<string, unknown>;
179
+ /**
180
+ * 路径值类型推导工具
181
+ * 根据路径字符串推导对应的值类型,支持对象嵌套与数组索引
182
+ *
183
+ * @example
184
+ * GetPathValue<{ user: { name: string } }, 'user.name'> // string
185
+ * GetPathValue<{ items: number[] }, 'items.0'> // number
186
+ * GetPathValue<{ list: { id: number }[] }, 'list.0.id'> // number
187
+ */
188
+ type GetPathValue<T, TPath extends string> = TPath extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? T[Key] extends Record<string, unknown> ? GetPathValue<T[Key], Rest> : T[Key] extends readonly (infer E)[] ? Rest extends `${number}` ? E : Rest extends `${number}.${infer R}` ? GetPathValue<E, R> : unknown : unknown : unknown : TPath extends keyof T ? T[TPath] : unknown;
189
+ /**
190
+ * 路径设置值类型推导工具
191
+ * 根据路径字符串推导可以设置的值类型
192
+ */
193
+ type SetPathValue<T, TPath extends string> = GetPathValue<T, TPath>;
194
+ /**
195
+ * 状态变更回调:path 与 value 类型联动,TPath 由 path 推导,value 为 GetPathValue<TState, TPath>
196
+ */
197
+ type OnStateChangeCallback<TState extends Record<string, unknown>> = <TPath extends string>(path: TPath, value: GetPathValue<TState, TPath>, ctx: RuntimeContext<TState>) => void;
198
+ /**
199
+ * RuntimeContext 创建选项
200
+ * @template TState 与 context 一致的状态类型,供 onStateChange 等回调获得完整类型推导
201
+ */
202
+ interface CreateContextOptions<TState extends Record<string, unknown> = Record<string, unknown>> {
203
+ onEmit?: (event: string, data?: unknown) => void;
204
+ methods?: MethodsRegistry;
205
+ /** 在 _set 调用后触发,value 类型随 path 推导 */
206
+ onStateChange?: OnStateChangeCallback<TState>;
207
+ createObject?: () => Record<string, unknown>;
208
+ createArray?: () => unknown[];
209
+ exprOptions?: ExpressionOptions;
210
+ }
211
+ /**
212
+ * 动作接口
213
+ * 使用泛型约束,根据 type 类型推导参数结构
214
+ */
215
+ interface Action {
216
+ type: string;
217
+ [key: string]: unknown;
218
+ }
219
+ /**
220
+ * 动作类型映射
221
+ * 根据 type 值推导对应的动作参数类型
222
+ */
223
+ type ActionMap = {
224
+ set: {
225
+ path: string;
226
+ value: string | unknown;
227
+ };
228
+ emit: {
229
+ event: string;
230
+ data?: string | unknown;
231
+ };
232
+ navigate: {
233
+ to: string;
234
+ };
235
+ log: {
236
+ level?: 'info' | 'warn' | 'error';
237
+ message: string;
238
+ };
239
+ if: {
240
+ cond: string;
241
+ then?: Action[];
242
+ else?: Action[];
243
+ };
244
+ loop: {
245
+ var: string;
246
+ in: string;
247
+ body: Action[];
248
+ };
249
+ call: {
250
+ method: string;
251
+ params?: Record<string, unknown>;
252
+ args?: unknown[];
253
+ resultTo?: string;
254
+ modifiers?: Record<string, boolean>;
255
+ };
256
+ batch: {
257
+ actions: Action[];
258
+ };
259
+ push: {
260
+ path: string;
261
+ value: string | unknown;
262
+ };
263
+ pop: {
264
+ path: string;
265
+ };
266
+ shift: {
267
+ path: string;
268
+ };
269
+ unshift: {
270
+ path: string;
271
+ value: string | unknown;
272
+ };
273
+ splice: {
274
+ path: string;
275
+ start: number | string;
276
+ deleteCount?: number | string;
277
+ items?: string | unknown[];
278
+ };
279
+ };
280
+ /**
281
+ * 表达式缓存接口
282
+ * 结果类型使用 unknown,因为表达式求值结果类型无法静态推导
283
+ */
284
+ interface ExpressionCache {
285
+ expr: string;
286
+ result: unknown;
287
+ dependencies: string[];
288
+ timestamp: number;
289
+ }
290
+
291
+ /**
292
+ * RuntimeContext 创建工厂
293
+ *
294
+ * 实现要点:扁平化状态存储、系统 API 保护、命名冲突检测、Proxy 保护。
295
+ * 复杂类型见 ../types.ts(CreateContextOptions、OnStateChangeCallback 等)。
296
+ */
297
+
298
+ /**
299
+ * 创建运行时上下文(传入具体 initialState 时,TState 从实参自动推导)
300
+ */
301
+ declare function createRuntimeContext<TState extends Record<string, unknown>>(initialState: TState, options?: CreateContextOptions<TState>): RuntimeContext<TState>;
302
+ /**
303
+ * 创建运行时上下文(显式指定 TState 时可传空或部分初始状态,如 createRuntimeContext<MyState>({}, options))
304
+ */
305
+ declare function createRuntimeContext<TState extends Record<string, unknown>>(initialState: Partial<TState> & Record<string, unknown>, options?: CreateContextOptions<TState>): RuntimeContext<TState>;
306
+ /**
307
+ * 创建运行时上下文(不传或传空且未指定 TState 时,退回 Record<string, unknown>)
308
+ */
309
+ declare function createRuntimeContext(initialState?: Record<string, unknown>, options?: CreateContextOptions<Record<string, unknown>>): RuntimeContext<Record<string, unknown>>;
310
+
311
+ /**
312
+ * Proxy 沙箱实现
313
+ *
314
+ * 功能:
315
+ * - 防止覆盖系统 API($ 和 _ 前缀保护)
316
+ * - 拦截属性设置操作
317
+ */
318
+
319
+ /**
320
+ * 创建受保护的 Proxy 上下文
321
+ * 禁止设置以 $ 或 _ 开头的属性
322
+ *
323
+ * @template T 上下文类型,必须是 RuntimeContext 的子类型
324
+ * @param ctx 运行时上下文对象
325
+ * @returns 受保护的 Proxy 包装的上下文
326
+ */
327
+ declare function createProxy<T extends RuntimeContext>(ctx: T): T;
328
+
329
+ /**
330
+ * 沙箱边界控制
331
+ *
332
+ * 功能:
333
+ * - 表达式层:严格沙箱,无法访问全局对象
334
+ * - 方法层:白名单控制,可访问全局对象
335
+ */
336
+
337
+ /**
338
+ * 创建表达式沙箱上下文
339
+ * 移除全局对象访问能力
340
+ */
341
+ declare function createExpressionSandbox(ctx: RuntimeContext): RuntimeContext;
342
+ /**
343
+ * 检查属性访问是否安全(用于表达式求值)
344
+ */
345
+ declare function isSafePropertyAccess(prop: string, ctx: RuntimeContext, options?: {
346
+ allowGlobals?: boolean;
347
+ }): boolean;
348
+
349
+ /**
350
+ * 循环上下文对象池
351
+ *
352
+ * 功能:
353
+ * - 复用循环上下文对象,减少内存分配
354
+ * - 提升循环性能
355
+ * - 自动清理和重置
356
+ */
357
+
358
+ /**
359
+ * 创建循环上下文(使用对象池)
360
+ *
361
+ * @param parentCtx 父上下文
362
+ * @param item 循环项
363
+ * @param index 循环索引
364
+ * @returns 循环上下文
365
+ */
366
+ declare function createLoopContext(parentCtx: RuntimeContext, item: unknown, index: number): RuntimeContext;
367
+ /**
368
+ * 释放循环上下文(回收到对象池)
369
+ *
370
+ * @param loopCtx 循环上下文
371
+ */
372
+ declare function releaseLoopContext(loopCtx: Partial<RuntimeContext>): void;
373
+
374
+ /**
375
+ * 路径解析工具模块
376
+ *
377
+ * 统一的路径解析逻辑,供 vario-core 和框架集成层使用
378
+ *
379
+ * 设计原则:
380
+ * - 单一职责:只处理路径解析,不涉及响应式
381
+ * - 可组合:提供原子操作,框架集成层可自由组合
382
+ * - 类型安全:尽可能提供类型推导
383
+ */
384
+ /**
385
+ * 路径段类型
386
+ */
387
+ type PathSegment = string | number;
388
+ /**
389
+ * 解析路径字符串为段数组
390
+ *
391
+ * 支持两种语法:
392
+ * - 点语法:`user.name` → ['user', 'name']
393
+ * - 括号语法:`users[0].name` → ['users', 0, 'name']
394
+ * - 混合语法:`data.users[0].profile.tags[1]` → ['data', 'users', 0, 'profile', 'tags', 1]
395
+ * - 空括号:`users[].name` → ['users', -1, 'name'](-1 表示动态索引,由循环上下文填充)
396
+ *
397
+ * @param path 路径字符串
398
+ * @returns 路径段数组
399
+ *
400
+ * @example
401
+ * parsePath('user.name') // ['user', 'name']
402
+ * parsePath('items.0.text') // ['items', 0, 'text']
403
+ * parsePath('users[0].name') // ['users', 0, 'name']
404
+ * parsePath('users[].name') // ['users', -1, 'name']
405
+ */
406
+ declare function parsePath(path: string): PathSegment[];
407
+ /**
408
+ * 解析路径(带缓存)
409
+ *
410
+ * @param path 点分隔的路径字符串
411
+ * @returns 路径段数组
412
+ */
413
+ declare function parsePathCached(path: string): PathSegment[];
414
+ /**
415
+ * 清理路径缓存
416
+ */
417
+ declare function clearPathCache(): void;
418
+ /**
419
+ * 将路径段数组转换为路径字符串
420
+ *
421
+ * @param segments 路径段数组
422
+ * @returns 点分隔的路径字符串
423
+ */
424
+ declare function stringifyPath(segments: PathSegment[]): string;
425
+ /**
426
+ * 获取嵌套路径的值
427
+ *
428
+ * @param obj 目标对象
429
+ * @param path 路径字符串或路径段数组
430
+ * @returns 路径对应的值,不存在返回 undefined
431
+ */
432
+ declare function getPathValue(obj: Record<string, unknown>, path: string | PathSegment[]): unknown;
433
+ /**
434
+ * 设置嵌套路径的值
435
+ *
436
+ * @param obj 目标对象
437
+ * @param path 路径字符串或路径段数组
438
+ * @param value 要设置的值
439
+ * @param options 配置选项
440
+ * @returns 是否设置成功
441
+ */
442
+ declare function setPathValue(obj: Record<string, unknown>, path: string | PathSegment[], value: unknown, options?: {
443
+ /**
444
+ * 自动创建中间路径
445
+ * @default true
446
+ */
447
+ createIntermediate?: boolean;
448
+ /**
449
+ * 创建对象的工厂函数
450
+ * 用于框架集成层创建响应式对象
451
+ */
452
+ createObject?: () => Record<string, unknown>;
453
+ /**
454
+ * 创建数组的工厂函数
455
+ * 用于框架集成层创建响应式数组
456
+ */
457
+ createArray?: () => unknown[];
458
+ }): boolean;
459
+ /**
460
+ * 检查路径是否匹配(支持通配符)
461
+ *
462
+ * @param pattern 模式路径(支持 * 通配符)
463
+ * @param path 目标路径
464
+ * @returns 是否匹配
465
+ *
466
+ * @example
467
+ * matchPath('items.*', 'items.0') // true
468
+ * matchPath('items.*.name', 'items.0.name') // true
469
+ * matchPath('user.name', 'user.name') // true
470
+ * matchPath('user.name', 'user.age') // false
471
+ */
472
+ declare function matchPath(pattern: string, path: string): boolean;
473
+ /**
474
+ * 提取路径的父路径
475
+ *
476
+ * @param path 路径字符串
477
+ * @returns 父路径,顶层路径返回空字符串
478
+ */
479
+ declare function getParentPath(path: string): string;
480
+ /**
481
+ * 获取路径的最后一段
482
+ *
483
+ * @param path 路径字符串
484
+ * @returns 最后一段
485
+ */
486
+ declare function getLastSegment(path: string): PathSegment | undefined;
487
+
488
+ type TraversalCallback = (node: SchemaNode, path: string, depth: number, parent: SchemaNode | null) => boolean | void;
489
+ /**
490
+ * 通用 Schema 遍历工具 (UI Component Tree 风格)
491
+ * 采用深度优先遍历 (DFS)
492
+ *
493
+ * @param root 根节点
494
+ * @param callback 回调函数
495
+ * @param separator 路径分隔符,默认为 '.'
496
+ */
497
+ declare function traverseSchema(root: SchemaNode, callback: TraversalCallback, separator?: string): void;
498
+
499
+ /**
500
+ * 表达式解析器
501
+ *
502
+ * 使用 @babel/parser 解析 JavaScript 表达式为 AST
503
+ * 移除 models. 前缀支持
504
+ */
505
+
506
+ /**
507
+ * 解析表达式为 AST
508
+ *
509
+ * @param expr 表达式字符串(如 "user.name + 1")
510
+ * @returns ESTree.Node AST 节点
511
+ */
512
+ declare function parseExpression(expr: string): ESTree.Node;
513
+
514
+ /**
515
+ * AST 白名单验证器
516
+ *
517
+ * 功能:
518
+ * - 深度遍历 AST,检查每个节点
519
+ * - 只允许安全的语法节点
520
+ * - 禁止危险的语法(赋值、函数、this 等)
521
+ * - 检查函数调用中的函数名是否在白名单中
522
+ */
523
+
524
+ /**
525
+ * 验证 AST 是否通过白名单检查
526
+ *
527
+ * @param ast AST 节点
528
+ * @param options 验证选项
529
+ * @param options.allowGlobals 是否允许全局函数调用(跳过白名单检查)
530
+ * @param options.maxNestingDepth 最大嵌套深度(默认 50)
531
+ * @throws ExpressionError 如果发现禁止的节点
532
+ */
533
+ declare function validateAST(ast: ESTree.Node, options?: {
534
+ allowGlobals?: boolean;
535
+ maxNestingDepth?: number;
536
+ }): void;
537
+
538
+ /**
539
+ * 表达式求值器
540
+ *
541
+ * 功能:
542
+ * - 安全求值 AST
543
+ * - 支持白名单函数调用
544
+ * - 执行步数/时间限制
545
+ */
546
+
547
+ /**
548
+ * 安全求值 AST
549
+ *
550
+ * @param ast AST 节点
551
+ * @param ctx 运行时上下文
552
+ * @param options 求值选项
553
+ * @returns 求值结果(类型无法静态推导)
554
+ *
555
+ * 注意:表达式求值结果类型无法在编译时确定,返回 unknown
556
+ */
557
+ declare function evaluateExpression(ast: ESTree.Node, ctx: RuntimeContext, options?: ExpressionOptions): unknown;
558
+
559
+ /**
560
+ * 表达式求值入口函数
561
+ *
562
+ * 整合解析、验证、缓存、求值流程
563
+ */
564
+
565
+ /**
566
+ * 求值表达式(完整流程)
567
+ *
568
+ * @param expr 表达式字符串
569
+ * @param ctx 运行时上下文
570
+ * @returns 求值结果(类型无法静态推导,返回 unknown)
571
+ *
572
+ * 注意:表达式求值结果类型无法在编译时确定,因为:
573
+ * 1. 表达式是运行时字符串
574
+ * 2. 状态类型是动态的
575
+ * 3. 表达式可能返回任意类型
576
+ *
577
+ * 如果需要类型安全,应在使用结果时进行类型守卫或类型断言
578
+ */
579
+ declare function evaluate(expr: string, ctx: RuntimeContext, options?: ExpressionOptions): unknown;
580
+
581
+ /**
582
+ * 依赖提取算法
583
+ *
584
+ * 功能:
585
+ * - 从 AST 中提取状态依赖
586
+ * - 支持通配符依赖(items.*)
587
+ * - 保守策略:标记整个对象
588
+ */
589
+
590
+ /**
591
+ * 从 AST 中提取依赖的状态路径
592
+ *
593
+ * @param ast AST 节点
594
+ * @returns 依赖路径数组(支持通配符)
595
+ */
596
+ declare function extractDependencies(ast: ESTree.Node): string[];
597
+
598
+ /**
599
+ * 表达式缓存系统
600
+ *
601
+ * 功能:
602
+ * - 每个 RuntimeContext 独立缓存(WeakMap 关联)
603
+ * - LRU 淘汰策略
604
+ * - 通配符依赖匹配
605
+ * - 缓存失效机制
606
+ *
607
+ * 设计原则:
608
+ * - 惰性求值:只在需要时计算
609
+ * - 依赖追踪:精确的缓存失效
610
+ * - 内存友好:WeakMap + LRU 淘汰
611
+ */
612
+
613
+ /**
614
+ * 获取缓存的表达式结果
615
+ *
616
+ * @param expr 表达式字符串
617
+ * @param ctx 运行时上下文
618
+ * @returns 缓存的结果,无缓存或已失效返回 null
619
+ */
620
+ declare function getCachedExpression(expr: string, ctx: RuntimeContext): unknown | null;
621
+ /**
622
+ * 设置表达式缓存
623
+ *
624
+ * @param expr 表达式字符串
625
+ * @param result 求值结果
626
+ * @param dependencies 依赖的状态路径
627
+ * @param ctx 运行时上下文
628
+ */
629
+ declare function setCachedExpression(expr: string, result: unknown, dependencies: string[], ctx: RuntimeContext): void;
630
+ /**
631
+ * 使缓存失效
632
+ *
633
+ * 当状态变化时调用,精确删除依赖该状态的缓存
634
+ *
635
+ * @param changedPath 变化的状态路径
636
+ * @param ctx 运行时上下文
637
+ */
638
+ declare function invalidateCache(changedPath: string, ctx: RuntimeContext): void;
639
+ /**
640
+ * 清除指定上下文的所有缓存
641
+ */
642
+ declare function clearCache(ctx: RuntimeContext): void;
643
+ /**
644
+ * 获取缓存统计信息(调试用)
645
+ */
646
+ declare function getCacheStats(ctx: RuntimeContext): {
647
+ size: number;
648
+ expressions: string[];
649
+ };
650
+
651
+ /**
652
+ * 表达式工具函数
653
+ *
654
+ * 提供表达式解析、格式化的通用工具
655
+ * 支持 {{ }} 格式的表达式提取和规范化
656
+ */
657
+ /**
658
+ * 提取表达式字符串
659
+ * 支持 {{ expression }} 格式,自动去掉包装
660
+ *
661
+ * @param expr 表达式字符串,可能是 "{{ showContent }}" 或 "showContent"
662
+ * @returns 去掉 {{ }} 包装后的表达式字符串
663
+ *
664
+ * @example
665
+ * ```typescript
666
+ * extractExpression("{{ showContent }}") // => "showContent"
667
+ * extractExpression("showContent") // => "showContent"
668
+ * extractExpression("{{ userRole === 'admin' }}") // => "userRole === 'admin'"
669
+ * ```
670
+ */
671
+ declare function extractExpression(expr: string): string;
672
+
673
+ /**
674
+ * Action VM 执行器
675
+ *
676
+ * 功能:
677
+ * - 执行动作序列
678
+ * - 通过 $methods 查找动作处理器
679
+ * - 错误处理和堆栈跟踪
680
+ * - 超时保护(防止无限循环)
681
+ *
682
+ * 参考架构图:vario-core/vm - Action VM
683
+ */
684
+
685
+ /**
686
+ * 执行动作序列的选项
687
+ */
688
+ interface ExecuteOptions {
689
+ /**
690
+ * 超时时间(毫秒),默认 5000ms
691
+ */
692
+ timeout?: number;
693
+ /**
694
+ * 最大执行步数,默认 10000
695
+ * 每执行一个动作计为一步
696
+ */
697
+ maxSteps?: number;
698
+ }
699
+ /**
700
+ * 执行动作序列
701
+ *
702
+ * @param actions 动作数组
703
+ * @param ctx 运行时上下文
704
+ * @param options 执行选项
705
+ */
706
+ declare function execute(actions: Action[], ctx: RuntimeContext, options?: ExecuteOptions): Promise<void>;
707
+
708
+ /**
709
+ * 内置动作处理器
710
+ *
711
+ * 所有内置动作统一注册到 ctx.$methods
712
+ */
713
+
714
+ /**
715
+ * 注册所有内置动作到 $methods
716
+ *
717
+ * 注意:此函数通过向现有 $methods 对象添加属性来注册方法,
718
+ * 而不是整体覆盖 $methods,以确保在 Proxy 保护下也能正常工作。
719
+ */
720
+ declare function registerBuiltinMethods(ctx: RuntimeContext): void;
721
+
722
+ /**
723
+ * Schema 分析器 - 框架无关的纯函数实现
724
+ *
725
+ * 提供 Schema 统计和索引构建能力,不依赖任何前端框架
726
+ */
727
+
728
+ /**
729
+ * Schema 统计信息
730
+ */
731
+ interface SchemaStats {
732
+ /** 节点总数 */
733
+ nodeCount: number;
734
+ /** 最大深度 */
735
+ maxDepth: number;
736
+ }
737
+ /**
738
+ * Schema 索引映射
739
+ */
740
+ interface SchemaIndex {
741
+ /** ID -> 路径的映射 */
742
+ idMap: Map<string, string>;
743
+ /** 路径 -> 节点的映射(可选,用于快速访问) */
744
+ pathMap?: Map<string, SchemaNode>;
745
+ }
746
+ /**
747
+ * 分析结果
748
+ */
749
+ interface AnalysisResult {
750
+ stats: SchemaStats;
751
+ index: SchemaIndex;
752
+ }
753
+ /**
754
+ * 分析 Schema 结构
755
+ *
756
+ * 遍历整个 Schema 树,收集统计信息和构建索引
757
+ *
758
+ * @param schema 要分析的 Schema 根节点
759
+ * @param options 分析选项
760
+ * @returns 分析结果
761
+ */
762
+ declare function analyzeSchema(schema: SchemaNode, options?: {
763
+ /** 是否构建路径映射(会增加内存开销) */
764
+ buildPathMap?: boolean;
765
+ /** 自定义回调,在遍历每个节点时调用 */
766
+ onNode?: (node: SchemaNode, path: string, depth: number) => void;
767
+ }): AnalysisResult;
768
+ /**
769
+ * 在 Schema 中查找节点
770
+ *
771
+ * @param schema Schema 根节点
772
+ * @param predicate 判断条件
773
+ * @returns 匹配的节点路径数组
774
+ */
775
+ declare function findNodes(schema: SchemaNode, predicate: (node: SchemaNode) => boolean): Array<{
776
+ node: SchemaNode;
777
+ path: string;
778
+ }>;
779
+ /**
780
+ * 在 Schema 中查找第一个匹配的节点
781
+ *
782
+ * @param schema Schema 根节点
783
+ * @param predicate 判断条件
784
+ * @returns 匹配的节点和路径,如果未找到返回 null
785
+ */
786
+ declare function findNode(schema: SchemaNode, predicate: (node: SchemaNode) => boolean): {
787
+ node: SchemaNode;
788
+ path: string;
789
+ } | null;
790
+ /**
791
+ * 通过 ID 查找节点路径
792
+ *
793
+ * @param schema Schema 根节点
794
+ * @param id 节点 ID
795
+ * @returns 节点路径,如果未找到返回 null
796
+ */
797
+ declare function findPathById(schema: SchemaNode, id: string): string | null;
798
+
799
+ /**
800
+ * Schema 查询引擎 - 框架无关的查询 API
801
+ */
802
+
803
+ /**
804
+ * 查询引擎配置
805
+ */
806
+ interface QueryEngineOptions {
807
+ /** Schema 根节点 */
808
+ schema: SchemaNode;
809
+ /** 索引(可选,用于加速 ID 查询) */
810
+ index?: SchemaIndex;
811
+ }
812
+ /**
813
+ * 节点查询结果
814
+ */
815
+ interface NodeResult {
816
+ /** 节点对象 */
817
+ node: SchemaNode;
818
+ /** 节点路径 */
819
+ path: string;
820
+ }
821
+ /**
822
+ * 创建查询引擎
823
+ *
824
+ * 提供高性能的 Schema 查询能力
825
+ */
826
+ declare function createQueryEngine(options: QueryEngineOptions): {
827
+ findById: (id: string) => NodeResult | null;
828
+ getParent: (path: string) => NodeResult | null;
829
+ };
830
+
831
+ export { type Action, ActionError, type ActionHandler, type ActionMap, type AnalysisResult, BatchError, type CreateContextOptions, type ErrorCode, ErrorCodes, type ErrorContext, type ExecuteOptions, type ExpressionCache, ExpressionError, type ExpressionOptions, type MethodsRegistry, type NodeResult, type OnStateChangeCallback, type PathSegment, type QueryEngineOptions, type RuntimeContext, type SchemaIndex, type SchemaStats, ServiceError, type TraversalCallback, VarioError, analyzeSchema, clearCache, clearPathCache, createExpressionSandbox, createLoopContext, createProxy, createQueryEngine, createRuntimeContext, evaluate, evaluateExpression, execute, extractDependencies, extractExpression, findNode, findNodes, findPathById, getCacheStats, getCachedExpression, getLastSegment, getParentPath, getPathValue, invalidateCache, isSafePropertyAccess, matchPath, parseExpression, parsePath, parsePathCached, registerBuiltinMethods, releaseLoopContext, setCachedExpression, setPathValue, stringifyPath, traverseSchema, validateAST };