@djvlc/runtime-core 1.0.0 → 1.0.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.
package/dist/index.d.cts CHANGED
@@ -1,10 +1,6 @@
1
- import { RuntimeError, RuntimeEvent, PerformanceMetric, PageResolveResult, ComponentLoadResult, PageSchema, HostAPI, ErrorCode, ManifestItem, PageManifest, EventBus as EventBus$1, RuntimeEventType, ComponentEvent, ExpressionNode, ExpressionContext, ExpressionValidationResult, RuntimeContext, DataQueryResponse, ActionExecuteResponse, NavigateOptions, DialogOptions, ToastOptions, TrackEvent, ClipboardResult, StorageOptions, BlockedComponent } from '@djvlc/contracts-types';
2
- export { ActionExecuteResponse, ComponentInstance, ComponentLoadResult, ComponentLoadStatus, DataQueryResponse, ExpressionContext, HostAPI, ManifestItem, PageManifest, PageResolveResult, PageSchema, PerformanceMetric, RuntimeConfig, RuntimeError, RuntimeEvent, RuntimeEventType } from '@djvlc/contracts-types';
3
-
4
- /**
5
- * Runtime 类型定义
6
- * 基于 @djvlc/contracts-types 扩展的运行时特有类型
7
- */
1
+ import * as _djvlc_contracts_types from '@djvlc/contracts-types';
2
+ import { ExpressionContext, Expression, ExpressionValidationResult, ActionRef, EventHandler, HostApi, NavigateOptions, TrackEvent, ActionResult, DialogOptions, DialogResult, ToastOptions, ClipboardApi, StorageApi, ShareOptions, ShareResult, ConfirmOptions, ActionSheetOptions, ActionSheetResult, PreviewImageOptions, ScanCodeResult, ComponentContext, ErrorCode, ManifestItem, PageManifest, CapabilityName, PageSchema, PageLifecycle } from '@djvlc/contracts-types';
3
+ export { ActionExecuteRequest, ActionExecuteResponse, ActionPolicy, ActionRef, ActionResult, AnyValueOrExpression, BlockedComponentInfo, BuiltinActionType, CapabilityDeclaration, CompatInfo, ComponentContext, ComponentMeta, ComponentNode, DataBinding, DataLoadStrategy, DataQueryRequest, DataQueryResponse, DialogOptions, DialogResult, ErrorCode, ErrorMessages, EventDeclaration, EventHandler, Expression, ExpressionContext, ExpressionLocal, ExpressionMeta, ExpressionType, ExpressionValidationResult, HostApi as HostAPI, HostApi, JsonValue, LayoutConfig, LayoutValues, LoopConfig, ManifestItem, NavigateOptions, NodeStyleConfig, PageConfig, PageLifecycle, PageManifest, PageMeta, PageSEO, PageSchema, PageState, PropDefinition, PropsSchema, RuntimeSpec, SchemaVersion, SemVer, StateFieldDefinition, ToastOptions, TrackEvent, UniqueId } from '@djvlc/contracts-types';
8
4
 
9
5
  /**
10
6
  * 运行时初始化选项
@@ -12,7 +8,7 @@ export { ActionExecuteResponse, ComponentInstance, ComponentLoadResult, Componen
12
8
  interface RuntimeOptions {
13
9
  /** 容器元素或选择器 */
14
10
  container: string | HTMLElement;
15
- /** 页面 UID */
11
+ /** 页面 UID(pageId) */
16
12
  pageUid: string;
17
13
  /** API 基础 URL */
18
14
  apiBaseUrl: string;
@@ -34,28 +30,34 @@ interface RuntimeOptions {
34
30
  enableSRI?: boolean;
35
31
  /** 自定义请求头 */
36
32
  headers?: Record<string, string>;
33
+ /** 应用 ID */
34
+ appId?: string;
35
+ /** 环境 */
36
+ env?: 'development' | 'staging' | 'production';
37
37
  /** 错误回调 */
38
38
  onError?: (error: RuntimeError) => void;
39
39
  /** 事件回调 */
40
40
  onEvent?: (event: RuntimeEvent) => void;
41
41
  /** 性能指标回调 */
42
42
  onMetric?: (metric: PerformanceMetric) => void;
43
+ /** 加载完成回调 */
44
+ onLoad?: (data: PageResolveResult) => void;
43
45
  }
44
46
  /**
45
47
  * 运行时阶段
46
48
  */
47
- type RuntimePhase = 'idle' | 'resolving' | 'loading' | 'rendering' | 'ready' | 'error';
49
+ type RuntimePhase = 'idle' | 'resolving' | 'loading' | 'rendering' | 'ready' | 'error' | 'destroyed';
48
50
  /**
49
51
  * 运行时状态
50
52
  */
51
53
  interface RuntimeState {
52
54
  /** 当前阶段 */
53
55
  phase: RuntimePhase;
54
- /** 页面数据 */
56
+ /** 页面解析结果 */
55
57
  page: PageResolveResult | null;
56
- /** 页面状态(可变) */
58
+ /** 页面状态变量(可变) */
57
59
  variables: Record<string, unknown>;
58
- /** 查询结果缓存 */
60
+ /** 数据查询结果缓存 */
59
61
  queries: Record<string, unknown>;
60
62
  /** 组件加载状态 */
61
63
  components: Map<string, ComponentLoadResult>;
@@ -64,6 +66,60 @@ interface RuntimeState {
64
66
  /** 是否已销毁 */
65
67
  destroyed: boolean;
66
68
  }
69
+ /**
70
+ * 页面解析结果(Runtime 使用)
71
+ */
72
+ interface PageResolveResult {
73
+ /** 页面 UID */
74
+ pageUid: string;
75
+ /** 页面版本 ID */
76
+ pageVersionId: string;
77
+ /** 页面 JSON(PageSchema) */
78
+ pageJson: _djvlc_contracts_types.PageSchema;
79
+ /** 组件清单 */
80
+ manifest: _djvlc_contracts_types.PageManifest;
81
+ /** 运行时配置 */
82
+ runtimeConfig?: {
83
+ /** 被阻断的组件列表 */
84
+ blockedComponents?: Array<{
85
+ name: string;
86
+ version: string;
87
+ reason: string;
88
+ }>;
89
+ /** Kill Switch 状态 */
90
+ killSwitches?: Array<{
91
+ type: string;
92
+ enabled: boolean;
93
+ }>;
94
+ /** 功能开关 */
95
+ features?: Record<string, boolean>;
96
+ };
97
+ /** 是否为预览模式 */
98
+ isPreview?: boolean;
99
+ /** 预加载的数据 */
100
+ preloadedData?: Record<string, unknown>;
101
+ }
102
+ /**
103
+ * 组件加载状态
104
+ */
105
+ type ComponentLoadStatus = 'loading' | 'loaded' | 'failed' | 'blocked';
106
+ /**
107
+ * 组件加载结果
108
+ */
109
+ interface ComponentLoadResult {
110
+ /** 组件名称 */
111
+ name: string;
112
+ /** 组件版本 */
113
+ version: string;
114
+ /** 加载状态 */
115
+ status: ComponentLoadStatus;
116
+ /** 组件构造函数(加载成功时) */
117
+ component?: CustomElementConstructor | unknown;
118
+ /** 加载耗时(毫秒) */
119
+ loadTime?: number;
120
+ /** 错误信息(加载失败时) */
121
+ error?: string;
122
+ }
67
123
  /**
68
124
  * 已加载的组件模块
69
125
  */
@@ -77,7 +133,72 @@ interface LoadedComponent {
77
133
  /** 加载耗时 */
78
134
  loadTime: number;
79
135
  /** 元数据 */
80
- meta?: Record<string, unknown>;
136
+ meta?: _djvlc_contracts_types.ComponentMeta;
137
+ }
138
+ /**
139
+ * 运行时错误类型
140
+ */
141
+ type RuntimeErrorType = 'LOAD_ERROR' | 'RENDER_ERROR' | 'ACTION_ERROR' | 'QUERY_ERROR' | 'EXPRESSION_ERROR' | 'COMPONENT_ERROR' | 'SECURITY_ERROR' | 'NETWORK_ERROR' | 'UNKNOWN_ERROR';
142
+ /**
143
+ * 运行时错误
144
+ */
145
+ interface RuntimeError {
146
+ /** 错误类型 */
147
+ type: RuntimeErrorType;
148
+ /** 错误消息 */
149
+ message: string;
150
+ /** 错误码 */
151
+ code?: string;
152
+ /** 详细信息 */
153
+ details?: Record<string, unknown>;
154
+ /** 原始错误 */
155
+ cause?: Error;
156
+ /** 追踪 ID */
157
+ traceId?: string;
158
+ /** 时间戳 */
159
+ timestamp: number;
160
+ }
161
+ /**
162
+ * 运行时事件类型
163
+ */
164
+ type RuntimeEventType = 'page:resolving' | 'page:resolved' | 'page:loading' | 'page:loaded' | 'page:rendered' | 'page:error' | 'page:destroyed' | 'component:loading' | 'component:loaded' | 'component:error' | 'action:executing' | 'action:executed' | 'action:error' | 'query:fetching' | 'query:fetched' | 'query:error' | 'state:changed';
165
+ /**
166
+ * 运行时事件
167
+ */
168
+ interface RuntimeEvent<T = unknown> {
169
+ /** 事件类型 */
170
+ type: RuntimeEventType;
171
+ /** 事件数据 */
172
+ data: T;
173
+ /** 追踪 ID */
174
+ traceId?: string;
175
+ /** 时间戳 */
176
+ timestamp: number;
177
+ }
178
+ /**
179
+ * 性能指标类型
180
+ */
181
+ type MetricType = 'pageResolveTime' | 'pageLoadTime' | 'componentLoadTime' | 'firstRenderTime' | 'actionExecuteTime' | 'queryFetchTime' | 'expressionEvalTime';
182
+ /**
183
+ * 性能指标
184
+ */
185
+ interface PerformanceMetric {
186
+ /** 指标类型 */
187
+ type: MetricType;
188
+ /** 指标值(毫秒) */
189
+ value: number;
190
+ /** 页面版本 ID */
191
+ pageVersionId?: string;
192
+ /** 组件名称 */
193
+ componentName?: string;
194
+ /** 组件版本 */
195
+ componentVersion?: string;
196
+ /** 动作类型 */
197
+ actionType?: string;
198
+ /** 查询 ID */
199
+ queryId?: string;
200
+ /** 时间戳 */
201
+ timestamp: number;
81
202
  }
82
203
  /**
83
204
  * 渲染器接口(框架无关)
@@ -86,16 +207,18 @@ interface Renderer {
86
207
  /** 初始化 */
87
208
  init(): void;
88
209
  /** 渲染页面 */
89
- render(schema: PageSchema, container: HTMLElement): void;
210
+ render(schema: _djvlc_contracts_types.PageSchema, container: HTMLElement): void;
90
211
  /** 更新组件 */
91
212
  updateComponent(componentId: string, props: Record<string, unknown>): void;
213
+ /** 更新表达式上下文 */
214
+ updateContext(context: Partial<_djvlc_contracts_types.ExpressionContext>): void;
92
215
  /** 销毁 */
93
216
  destroy(): void;
94
217
  }
95
218
  /**
96
- * 事件处理器
219
+ * 事件处理器函数
97
220
  */
98
- type EventHandler<T = unknown> = (event: RuntimeEvent<T>) => void;
221
+ type EventHandlerFn<T = unknown> = (event: RuntimeEvent<T>) => void;
99
222
  /**
100
223
  * 事件订阅取消函数
101
224
  */
@@ -115,841 +238,1112 @@ interface Logger {
115
238
  }
116
239
 
117
240
  /**
118
- * 主运行时类
119
- * 统一管理整个运行时生命周期
241
+ * 状态管理器
242
+ * 管理页面运行时状态,兼容 PageSchema V2
120
243
  */
121
244
 
122
245
  /**
123
- * 创建运行时实例
246
+ * 状态变更监听器
124
247
  */
125
- declare function createRuntime(options: RuntimeOptions): DjvlcRuntime;
248
+ type StateListener = (state: RuntimeState) => void;
126
249
  /**
127
- * DJVLC 运行时
250
+ * 状态变更详情
128
251
  */
129
- declare class DjvlcRuntime {
130
- private options;
131
- private container;
132
- private pageLoader;
133
- private componentLoader;
134
- private assetLoader;
135
- private stateManager;
136
- private eventBus;
137
- private expressionEngine;
138
- private securityManager;
139
- private telemetryManager;
140
- private renderer;
141
- private hostApi;
142
- private logger;
143
- constructor(options: RuntimeOptions);
252
+ interface StateChangeDetail {
253
+ /** 变更的键 */
254
+ key: string;
255
+ /** 旧值 */
256
+ oldValue: unknown;
257
+ /** 新值 */
258
+ newValue: unknown;
259
+ }
260
+ /**
261
+ * 状态变更回调
262
+ */
263
+ type StateChangeCallback = (detail: StateChangeDetail) => void;
264
+ /**
265
+ * 状态管理器
266
+ */
267
+ declare class StateManager {
268
+ private state;
269
+ private listeners;
270
+ private changeCallbacks;
271
+ constructor();
144
272
  /**
145
- * 初始化运行时
273
+ * 获取当前状态
146
274
  */
147
- init(): Promise<void>;
275
+ getState(): RuntimeState;
148
276
  /**
149
- * 加载页面
277
+ * 获取当前阶段
150
278
  */
151
- load(): Promise<PageResolveResult>;
279
+ getPhase(): RuntimePhase;
152
280
  /**
153
- * 渲染页面
281
+ * 设置阶段
154
282
  */
155
- render(): Promise<void>;
283
+ setPhase(phase: RuntimePhase): void;
156
284
  /**
157
- * 获取 Host API
285
+ * 设置页面数据(兼容 V2 PageSchema)
158
286
  */
159
- getHostApi(): HostAPI;
287
+ setPage(page: PageResolveResult): void;
160
288
  /**
161
- * 获取当前状态
289
+ * 初始化页面状态(从 PageSchema V2 的 state 定义)
162
290
  */
163
- getState(): RuntimeState;
291
+ private initializePageState;
164
292
  /**
165
- * 订阅状态变更
293
+ * 设置错误
166
294
  */
167
- onStateChange(listener: (state: RuntimeState) => void): () => void;
295
+ setError(error: RuntimeError): void;
168
296
  /**
169
- * 订阅事件
297
+ * 清除错误
170
298
  */
171
- on<T>(type: RuntimeEvent<T>['type'], handler: (event: RuntimeEvent<T>) => void): () => void;
299
+ clearError(): void;
172
300
  /**
173
- * 更新组件
301
+ * 获取变量值
174
302
  */
175
- updateComponent(componentId: string, props: Record<string, unknown>): void;
303
+ getVariable<T = unknown>(key: string): T | undefined;
176
304
  /**
177
- * 设置变量
305
+ * 设置变量值
178
306
  */
179
307
  setVariable(key: string, value: unknown): void;
180
308
  /**
181
- * 刷新数据
309
+ * 批量设置变量
182
310
  */
183
- refreshData(queryId: string): Promise<void>;
311
+ setVariables(variables: Record<string, unknown>): void;
184
312
  /**
185
- * 销毁运行时
313
+ * 获取查询结果
186
314
  */
187
- destroy(): void;
188
- private resolveContainer;
189
- private initHostApi;
190
- private initRenderer;
191
- private handleError;
192
- private emitEvent;
193
- private createLogger;
194
- private log;
315
+ getQuery<T = unknown>(queryId: string): T | undefined;
316
+ /**
317
+ * 设置查询结果
318
+ */
319
+ setQuery(queryId: string, data: unknown): void;
320
+ /**
321
+ * 清除查询结果
322
+ */
323
+ clearQuery(queryId: string): void;
324
+ /**
325
+ * 更新组件加载状态
326
+ */
327
+ setComponentStatus(key: string, result: ComponentLoadResult): void;
328
+ /**
329
+ * 获取组件加载状态
330
+ */
331
+ getComponentStatus(key: string): ComponentLoadResult | undefined;
332
+ /**
333
+ * 标记为已销毁
334
+ */
335
+ setDestroyed(): void;
336
+ /**
337
+ * 检查是否已销毁
338
+ */
339
+ isDestroyed(): boolean;
340
+ /**
341
+ * 订阅状态变更
342
+ */
343
+ subscribe(listener: StateListener): () => void;
344
+ /**
345
+ * 订阅特定状态键的变更
346
+ */
347
+ onStateChange(callback: StateChangeCallback): () => void;
348
+ /**
349
+ * 重置状态
350
+ */
351
+ reset(): void;
352
+ /**
353
+ * 获取表达式上下文
354
+ * 对齐 contracts 中的 ExpressionContext 定义:
355
+ * - state: 页面状态变量
356
+ * - binding: 数据绑定结果(即查询结果)
357
+ * - local: 局部变量(循环变量、事件参数等,由渲染器管理)
358
+ * - context: 运行时上下文(额外信息)
359
+ */
360
+ getExpressionContext(): {
361
+ state: Record<string, unknown>;
362
+ binding: Record<string, unknown>;
363
+ local: Record<string, unknown>;
364
+ context: Record<string, unknown>;
365
+ };
366
+ private setState;
367
+ private notifyListeners;
368
+ private notifyChange;
369
+ private getNestedValue;
370
+ private setNestedValue;
371
+ private createInitialState;
195
372
  }
196
373
 
197
374
  /**
198
- * 运行时错误类型定义
375
+ * 表达式词法分析器
376
+ * 将表达式字符串分解为 Token
199
377
  */
200
-
201
378
  /**
202
- * 运行时错误基类
379
+ * Token 类型
203
380
  */
204
- declare class DjvlcRuntimeError extends Error {
205
- readonly code: ErrorCode;
206
- readonly details?: Record<string, unknown>;
207
- readonly traceId?: string;
208
- readonly timestamp: number;
209
- constructor(code: ErrorCode, message?: string, details?: Record<string, unknown>, traceId?: string);
210
- toJSON(): {
211
- name: string;
212
- code: ErrorCode;
213
- message: string;
214
- details: Record<string, unknown> | undefined;
215
- traceId: string | undefined;
216
- timestamp: number;
217
- };
218
- }
381
+ type TokenType = 'NUMBER' | 'STRING' | 'BOOLEAN' | 'NULL' | 'IDENTIFIER' | 'OPERATOR' | 'DOT' | 'LBRACKET' | 'RBRACKET' | 'LPAREN' | 'RPAREN' | 'COMMA' | 'QUESTION' | 'COLON' | 'EOF';
219
382
  /**
220
- * 页面加载错误
383
+ * Token
221
384
  */
222
- declare class PageLoadError extends DjvlcRuntimeError {
223
- constructor(message: string, details?: Record<string, unknown>, traceId?: string);
385
+ interface Token {
386
+ type: TokenType;
387
+ value: string | number | boolean | null;
388
+ start: number;
389
+ end: number;
224
390
  }
225
391
  /**
226
- * 组件加载错误
392
+ * 词法分析器
227
393
  */
228
- declare class ComponentLoadError extends DjvlcRuntimeError {
229
- readonly componentName: string;
230
- readonly componentVersion: string;
231
- constructor(componentName: string, componentVersion: string, message: string, code?: ErrorCode, details?: Record<string, unknown>);
394
+ declare class Lexer {
395
+ private input;
396
+ private pos;
397
+ private tokens;
398
+ constructor(input: string);
399
+ /**
400
+ * 分析表达式,返回 Token 列表
401
+ */
402
+ tokenize(): Token[];
403
+ private readToken;
404
+ private readNumber;
405
+ private readString;
406
+ private readIdentifier;
407
+ private readOperator;
408
+ private skipWhitespace;
409
+ private isDigit;
410
+ private isIdentifierStart;
411
+ private isIdentifierChar;
412
+ private peek;
232
413
  }
414
+
233
415
  /**
234
- * 组件完整性校验错误
416
+ * 表达式解析器
417
+ * 将 Token 列表解析为 AST
235
418
  */
236
- declare class IntegrityError extends DjvlcRuntimeError {
237
- readonly componentName: string;
238
- readonly componentVersion: string;
239
- readonly expectedHash: string;
240
- readonly actualHash: string;
241
- constructor(componentName: string, componentVersion: string, expectedHash: string, actualHash: string);
242
- }
419
+
243
420
  /**
244
- * 组件阻断错误
421
+ * AST 基础节点接口
245
422
  */
246
- declare class ComponentBlockedError extends DjvlcRuntimeError {
247
- readonly componentName: string;
248
- readonly componentVersion: string;
249
- readonly reason: string;
250
- constructor(componentName: string, componentVersion: string, reason: string);
423
+ interface BaseASTNode {
424
+ type: string;
425
+ /** 起始位置(可选,用于错误定位) */
426
+ start?: number;
427
+ /** 结束位置(可选,用于错误定位) */
428
+ end?: number;
429
+ /** 原始字符串(可选,用于调试) */
430
+ raw?: string;
251
431
  }
252
432
  /**
253
- * 表达式求值错误
433
+ * AST 节点类型
254
434
  */
255
- declare class ExpressionError extends DjvlcRuntimeError {
256
- readonly expression: string;
257
- readonly position?: {
258
- start: number;
259
- end: number;
260
- };
261
- constructor(expression: string, message: string, position?: {
262
- start: number;
263
- end: number;
264
- }, details?: Record<string, unknown>);
435
+ interface LiteralASTNode extends BaseASTNode {
436
+ type: 'literal';
437
+ value: string | number | boolean | null;
265
438
  }
266
- /**
267
- * 动作执行错误
268
- */
269
- declare class ActionError extends DjvlcRuntimeError {
270
- readonly actionType: string;
271
- readonly actionId?: string;
272
- constructor(actionType: string, message: string, code?: ErrorCode, actionId?: string, details?: Record<string, unknown>);
439
+ interface IdentifierASTNode extends BaseASTNode {
440
+ type: 'identifier';
441
+ name: string;
273
442
  }
274
- /**
275
- * 数据查询错误
276
- */
277
- declare class QueryError extends DjvlcRuntimeError {
278
- readonly queryId: string;
279
- constructor(queryId: string, message: string, code?: ErrorCode, details?: Record<string, unknown>);
443
+ interface MemberASTNode extends BaseASTNode {
444
+ type: 'member';
445
+ object: ASTNode;
446
+ property: string | ASTNode;
447
+ computed: boolean;
280
448
  }
281
- /**
282
- * 渲染错误
449
+ interface CallASTNode extends BaseASTNode {
450
+ type: 'call';
451
+ callee: string;
452
+ arguments: ASTNode[];
453
+ }
454
+ interface BinaryASTNode extends BaseASTNode {
455
+ type: 'binary';
456
+ operator: string;
457
+ left: ASTNode;
458
+ right: ASTNode;
459
+ }
460
+ interface UnaryASTNode extends BaseASTNode {
461
+ type: 'unary';
462
+ operator: string;
463
+ argument: ASTNode;
464
+ }
465
+ interface ConditionalASTNode extends BaseASTNode {
466
+ type: 'conditional';
467
+ test: ASTNode;
468
+ consequent: ASTNode;
469
+ alternate: ASTNode;
470
+ }
471
+ interface ArrayASTNode extends BaseASTNode {
472
+ type: 'array';
473
+ elements: ASTNode[];
474
+ }
475
+ type ASTNode = LiteralASTNode | IdentifierASTNode | MemberASTNode | CallASTNode | BinaryASTNode | UnaryASTNode | ConditionalASTNode | ArrayASTNode;
476
+ /**
477
+ * 表达式解析器
283
478
  */
284
- declare class RenderError extends DjvlcRuntimeError {
285
- readonly componentId: string;
286
- readonly componentType: string;
287
- constructor(componentId: string, componentType: string, message: string, details?: Record<string, unknown>);
479
+ declare class Parser {
480
+ private tokens;
481
+ private pos;
482
+ constructor(tokens: Token[]);
483
+ /**
484
+ * 解析表达式
485
+ */
486
+ parse(): ASTNode;
487
+ private parseExpression;
488
+ private parseTernary;
489
+ private parseBinary;
490
+ private parseUnary;
491
+ private parsePostfix;
492
+ private parsePrimary;
493
+ private parseArray;
494
+ private parseArguments;
495
+ private current;
496
+ private advance;
497
+ private expect;
288
498
  }
289
499
 
290
500
  /**
291
- * 页面加载器
292
- * 负责从 API 解析页面配置
501
+ * 表达式求值器
502
+ * 安全地执行 AST,无 eval
293
503
  */
294
504
 
295
505
  /**
296
- * 页面加载器配置
506
+ * 求值结果
297
507
  */
298
- interface PageLoaderOptions {
299
- /** API 基础 URL */
300
- apiBaseUrl: string;
301
- /** 渠道 */
302
- channel?: 'preview' | 'prod' | 'gray';
303
- /** 认证 Token */
304
- authToken?: string;
305
- /** 预览 Token */
306
- previewToken?: string;
307
- /** 请求头 */
308
- headers?: Record<string, string>;
309
- /** 缓存配置 */
310
- cache?: {
311
- enabled: boolean;
312
- maxAge?: number;
313
- };
314
- /** 日志器 */
315
- logger?: Logger;
508
+ interface EvaluationResult {
509
+ value: unknown;
510
+ error?: Error;
316
511
  }
317
512
  /**
318
- * 页面加载器
319
- * 负责调用 /page/resolve 接口获取页面数据
513
+ * 求值器配置
320
514
  */
321
- declare class PageLoader {
515
+ interface EvaluatorOptions {
516
+ /** 最大递归深度 */
517
+ maxDepth?: number;
518
+ /** 最大执行时间(毫秒) */
519
+ timeout?: number;
520
+ /** 调试模式 */
521
+ debug?: boolean;
522
+ }
523
+ /**
524
+ * 表达式求值器
525
+ */
526
+ declare class Evaluator {
322
527
  private options;
323
- private cache;
324
- constructor(options: PageLoaderOptions);
325
- /**
326
- * 解析页面
327
- * @param pageUid 页面 UID
328
- * @param params 额外参数
329
- */
330
- resolve(pageUid: string, params?: {
331
- uid?: string;
332
- deviceId?: string;
333
- }): Promise<PageResolveResult>;
334
- /**
335
- * 预连接 API 服务器
336
- */
337
- preconnect(): void;
528
+ private depth;
529
+ private startTime;
530
+ constructor(options?: EvaluatorOptions);
338
531
  /**
339
- * 清除缓存
532
+ * 求值表达式
340
533
  */
341
- clearCache(pageUid?: string): void;
342
- private buildResolveUrl;
343
- private buildHeaders;
344
- private getCacheKey;
345
- private isCacheValid;
346
- private isValidPageResolveResult;
347
- private log;
534
+ evaluate(ast: ASTNode, context: ExpressionContext): EvaluationResult;
535
+ private evaluateNode;
536
+ private evaluateLiteral;
537
+ private evaluateIdentifier;
538
+ private evaluateMember;
539
+ private evaluateCall;
540
+ private evaluateBinary;
541
+ private evaluateUnary;
542
+ private evaluateConditional;
543
+ private evaluateArray;
544
+ private checkLimits;
348
545
  }
349
546
 
350
547
  /**
351
- * 组件加载器
352
- * 负责从 CDN 加载组件资源
548
+ * 表达式引擎
549
+ * 统一的表达式解析和求值入口
353
550
  */
354
551
 
355
552
  /**
356
- * 组件加载器配置
553
+ * 表达式引擎配置
357
554
  */
358
- interface ComponentLoaderOptions {
359
- /** CDN 基础 URL */
360
- cdnBaseUrl: string;
361
- /** 是否启用 SRI 校验 */
362
- enableSRI?: boolean;
363
- /** 被阻断的组件列表 */
364
- blockedComponents?: string[];
365
- /** 并行加载数量 */
366
- concurrency?: number;
367
- /** 超时时间(毫秒) */
368
- timeout?: number;
555
+ interface ExpressionEngineOptions extends EvaluatorOptions {
556
+ /** 是否缓存 AST */
557
+ cacheAST?: boolean;
558
+ /** 最大缓存大小 */
559
+ maxCacheSize?: number;
369
560
  /** 日志器 */
370
561
  logger?: Logger;
371
562
  }
372
563
  /**
373
- * 组件加载器
374
- * 负责加载 Web Components
564
+ * 表达式引擎
565
+ * 安全的表达式解析和求值,无 eval/new Function
375
566
  */
376
- declare class ComponentLoader {
567
+ declare class ExpressionEngine {
377
568
  private options;
378
- private loadedComponents;
379
- private loadingPromises;
380
- private blockedSet;
381
- constructor(options: ComponentLoaderOptions);
382
- /**
383
- * 加载单个组件
384
- */
385
- load(item: ManifestItem): Promise<LoadedComponent>;
569
+ private evaluator;
570
+ private astCache;
571
+ constructor(options?: ExpressionEngineOptions);
386
572
  /**
387
- * 加载 Manifest 中的所有组件
573
+ * 求值表达式
388
574
  */
389
- loadAll(manifest: PageManifest): Promise<Map<string, ComponentLoadResult>>;
575
+ evaluate(expression: string, context: ExpressionContext): EvaluationResult;
390
576
  /**
391
- * 预加载组件
577
+ * 求值表达式并返回值
578
+ * 出错时返回 fallback 值
579
+ *
580
+ * @param expression - 表达式字符串或 Expression 对象
581
+ * @param context - 表达式上下文
582
+ * @param fallback - 求值失败时的默认值
392
583
  */
393
- preload(items: ManifestItem[]): void;
584
+ evaluateWithFallback<T>(expression: string | Expression, context: ExpressionContext, fallback?: T): T;
394
585
  /**
395
- * 检查组件是否已加载
586
+ * 求值模板字符串
587
+ * 支持 ${expression} 语法
396
588
  */
397
- isLoaded(name: string, version: string): boolean;
589
+ evaluateTemplate(template: string, context: ExpressionContext): string;
398
590
  /**
399
- * 获取已加载的组件
591
+ * 解析表达式为 AST
400
592
  */
401
- get(name: string, version: string): LoadedComponent | undefined;
593
+ parse(expression: string): ASTNode;
402
594
  /**
403
- * 检查组件是否被阻断
595
+ * 校验表达式
404
596
  */
405
- isBlocked(name: string, version: string): boolean;
597
+ validate(expression: string): ExpressionValidationResult;
406
598
  /**
407
- * 更新阻断列表
599
+ * 清除 AST 缓存
408
600
  */
409
- updateBlockedList(blocked: string[]): void;
410
- private loadComponent;
411
- private fetchWithTimeout;
412
- private validateIntegrity;
413
- private executeScript;
414
- private resolveUrl;
415
- private getComponentKey;
601
+ clearCache(): void;
602
+ private collectReferences;
603
+ private buildMemberPath;
416
604
  private log;
417
605
  }
418
606
 
419
607
  /**
420
- * 资源加载器
421
- * 负责预连接、预加载等资源优化
608
+ * 内置函数实现
609
+ * 表达式中可调用的白名单函数
422
610
  */
611
+ type BuiltinFn = (...args: unknown[]) => unknown;
423
612
  /**
424
- * 资源加载器配置
613
+ * 内置函数映射
425
614
  */
426
- interface AssetLoaderOptions {
427
- /** CDN 域名列表 */
428
- cdnHosts: string[];
429
- /** API 域名列表 */
430
- apiHosts: string[];
615
+ declare const builtinFunctions: Record<string, BuiltinFn>;
616
+
617
+ /**
618
+ * 事件总线
619
+ * 运行时事件系统
620
+ */
621
+
622
+ /**
623
+ * 事件总线配置
624
+ */
625
+ interface EventBusOptions {
626
+ /** 是否开启调试 */
627
+ debug?: boolean;
628
+ /** 日志器 */
629
+ logger?: Logger;
630
+ /** 最大监听器数量 */
631
+ maxListeners?: number;
431
632
  }
432
633
  /**
433
- * 资源加载器
434
- * 负责资源预连接、预加载等优化
634
+ * 事件总线实现
435
635
  */
436
- declare class AssetLoader {
636
+ declare class EventBus {
637
+ private handlers;
437
638
  private options;
438
- private preconnectedHosts;
439
- private preloadedAssets;
440
- constructor(options: AssetLoaderOptions);
639
+ constructor(options?: EventBusOptions);
441
640
  /**
442
- * 预连接所有域名
641
+ * 发送事件
443
642
  */
444
- preconnectAll(): void;
643
+ emit<T>(event: RuntimeEvent<T>): void;
445
644
  /**
446
- * 预连接单个域名
645
+ * 订阅事件
447
646
  */
448
- preconnect(host: string): void;
647
+ on<T>(type: RuntimeEventType, handler: EventHandlerFn<T>): Unsubscribe;
449
648
  /**
450
- * DNS 预解析
649
+ * 取消订阅
451
650
  */
452
- dnsPrefetch(host: string): void;
651
+ off<T>(type: RuntimeEventType, handler: EventHandlerFn<T>): void;
453
652
  /**
454
- * 预加载脚本
653
+ * 一次性订阅
455
654
  */
456
- preloadScript(url: string, integrity?: string): void;
655
+ once<T>(type: RuntimeEventType, handler: EventHandlerFn<T>): Unsubscribe;
457
656
  /**
458
- * 预加载样式
657
+ * 清除所有监听器
459
658
  */
460
- preloadStyle(url: string, integrity?: string): void;
659
+ clear(type?: RuntimeEventType): void;
461
660
  /**
462
- * 预加载图片
463
- */
464
- preloadImage(url: string): void;
465
- /**
466
- * 预获取资源(低优先级)
467
- */
468
- prefetch(url: string, as?: 'script' | 'style' | 'image' | 'document'): void;
469
- /**
470
- * 加载样式表
661
+ * 获取监听器数量
471
662
  */
472
- loadStylesheet(url: string, integrity?: string): Promise<void>;
663
+ listenerCount(type: RuntimeEventType): number;
473
664
  /**
474
- * 加载脚本
665
+ * 创建事件
475
666
  */
476
- loadScript(url: string, integrity?: string): Promise<void>;
667
+ static createEvent<T>(type: RuntimeEventType, data: T, traceId?: string): RuntimeEvent<T>;
668
+ private log;
477
669
  }
478
670
 
479
671
  /**
480
- * 状态管理器
481
- * 管理页面运行时状态
672
+ * 动作执行器接口
482
673
  */
483
-
674
+ interface ActionExecutor {
675
+ /** 执行自定义动作(通过 Action Gateway) */
676
+ executeAction<T = unknown>(actionType: string, params: Record<string, unknown>): Promise<{
677
+ success: boolean;
678
+ data?: T;
679
+ errorCode?: string;
680
+ errorMessage?: string;
681
+ }>;
682
+ /** 执行数据查询(通过 Data Proxy) */
683
+ requestData<T = unknown>(queryId: string, params?: Record<string, unknown>): Promise<{
684
+ success: boolean;
685
+ data?: T;
686
+ message?: string;
687
+ }>;
688
+ /** 导航 */
689
+ navigate(options: _djvlc_contracts_types.NavigateOptions): void;
690
+ /** 打开对话框 */
691
+ openDialog(options: {
692
+ type: string;
693
+ content: string;
694
+ title?: string;
695
+ }): Promise<unknown>;
696
+ /** 关闭对话框 */
697
+ closeDialog(dialogId?: string): void;
698
+ /** 显示 Toast */
699
+ showToast(options: {
700
+ message: string;
701
+ type?: string;
702
+ duration?: number;
703
+ }): void;
704
+ /** 显示加载 */
705
+ showLoading(message?: string): void;
706
+ /** 隐藏加载 */
707
+ hideLoading(): void;
708
+ /** 埋点上报 */
709
+ track(event: {
710
+ eventName: string;
711
+ params?: Record<string, unknown>;
712
+ }): void;
713
+ /** 设置状态 */
714
+ setState(key: string, value: unknown): void;
715
+ /** 获取状态 */
716
+ getState<T = unknown>(key: string): T | undefined;
717
+ /** 刷新数据 */
718
+ refreshData(queryId: string): Promise<void>;
719
+ }
484
720
  /**
485
- * 状态变更监听器
721
+ * 动作桥接器配置
486
722
  */
487
- type StateListener = (state: RuntimeState) => void;
723
+ interface ActionBridgeOptions {
724
+ /** 动作执行器 */
725
+ executor: ActionExecutor;
726
+ /** 表达式引擎 */
727
+ expressionEngine: ExpressionEngine;
728
+ /** 调试模式 */
729
+ debug?: boolean;
730
+ /** 日志器 */
731
+ logger?: Logger;
732
+ /** 动作执行前回调 */
733
+ onActionStart?: (actionRef: ActionRef, params: Record<string, unknown>) => void;
734
+ /** 动作执行后回调 */
735
+ onActionComplete?: (actionRef: ActionRef, result: {
736
+ success: boolean;
737
+ data?: unknown;
738
+ error?: Error;
739
+ }) => void;
740
+ }
488
741
  /**
489
- * 状态管理器
742
+ * 动作执行上下文
490
743
  */
491
- declare class StateManager {
492
- private state;
493
- private listeners;
494
- constructor();
744
+ interface ActionExecutionContext {
745
+ /** 事件数据 */
746
+ $event: unknown;
747
+ /** 上一个动作的结果 */
748
+ $prevResult: unknown;
749
+ /** 所有动作的结果 */
750
+ $results: Record<string, unknown>;
751
+ }
752
+ /**
753
+ * 动作桥接器
754
+ * 将组件事件转换为动作执行,支持动作链、条件执行、重试等
755
+ */
756
+ declare class ActionBridge {
757
+ private options;
758
+ private debounceTimers;
759
+ private throttleTimers;
760
+ constructor(options: ActionBridgeOptions);
495
761
  /**
496
- * 获取当前状态
762
+ * 处理事件处理器
497
763
  */
498
- getState(): RuntimeState;
764
+ handleEvent(handler: EventHandler, eventData: unknown, baseContext: ExpressionContext): Promise<void>;
499
765
  /**
500
- * 获取当前阶段
766
+ * 执行动作链
501
767
  */
502
- getPhase(): RuntimePhase;
768
+ executeActionChain(actions: ActionRef[], baseContext: ExpressionContext, executionContext: ActionExecutionContext): Promise<void>;
503
769
  /**
504
- * 设置阶段
770
+ * 执行单个动作
505
771
  */
506
- setPhase(phase: RuntimePhase): void;
772
+ private executeSingleAction;
507
773
  /**
508
- * 设置页面数据
774
+ * 执行动作(带重试)
509
775
  */
510
- setPage(page: PageResolveResult): void;
776
+ private executeWithRetry;
511
777
  /**
512
- * 设置错误
778
+ * 执行内置动作
513
779
  */
514
- setError(error: RuntimeError): void;
780
+ private executeBuiltinAction;
515
781
  /**
516
- * 获取变量值
782
+ * 执行自定义动作
517
783
  */
518
- getVariable<T = unknown>(key: string): T | undefined;
784
+ private executeCustomAction;
519
785
  /**
520
- * 设置变量值
786
+ * Expression 类型列表(用于判断是否为 Expression 对象)
521
787
  */
522
- setVariable(key: string, value: unknown): void;
788
+ private static readonly EXPRESSION_TYPES;
523
789
  /**
524
- * 批量设置变量
790
+ * 解析参数中的表达式
791
+ *
792
+ * @param params - ActionRef.params,类型为 Record<string, AnyValueOrExpression>
525
793
  */
526
- setVariables(variables: Record<string, unknown>): void;
794
+ private resolveParams;
527
795
  /**
528
- * 获取查询结果
796
+ * 解析单个值(支持 AnyValueOrExpression)
797
+ *
798
+ * AnyValueOrExpression 可以是:
799
+ * - Expression 对象:{ type: 'state' | 'binding' | 'local' | 'template' | 'computed', value: string, fallback?: any }
800
+ * - 原始值:string | number | boolean | null
801
+ * - 嵌套数组:AnyValueOrExpression[]
802
+ * - 嵌套对象:{ [key: string]: AnyValueOrExpression }
529
803
  */
530
- getQuery<T = unknown>(queryId: string): T | undefined;
804
+ private resolveValue;
531
805
  /**
532
- * 设置查询结果
806
+ * 求值条件表达式
807
+ * 将事件数据注入到 local 上下文中(对齐 contracts 的 ExpressionContext)
808
+ *
809
+ * @param condition - Expression 对象,包含 type、value、fallback
533
810
  */
534
- setQuery(queryId: string, data: unknown): void;
811
+ private evaluateCondition;
535
812
  /**
536
- * 更新组件加载状态
813
+ * 构建动作执行上下文
814
+ * 将事件数据和动作结果注入到 local 上下文中
537
815
  */
538
- setComponentStatus(key: string, result: ComponentLoadResult): void;
816
+ private buildActionContext;
539
817
  /**
540
- * 标记为已销毁
818
+ * 带超时的 Promise
541
819
  */
542
- setDestroyed(): void;
820
+ private withTimeout;
543
821
  /**
544
- * 订阅状态变更
822
+ * 防抖
545
823
  */
546
- subscribe(listener: StateListener): () => void;
824
+ private debounce;
547
825
  /**
548
- * 重置状态
826
+ * 节流
549
827
  */
550
- reset(): void;
551
- private setState;
552
- private notifyListeners;
553
- private createInitialState;
828
+ private throttle;
829
+ /**
830
+ * 延迟
831
+ */
832
+ private delay;
833
+ /**
834
+ * 销毁
835
+ */
836
+ destroy(): void;
837
+ private log;
554
838
  }
555
839
 
556
840
  /**
557
- * 事件总线
558
- * 运行时事件系统
841
+ * Host API 实现
842
+ * 提供给组件的安全 API,完全对齐 @djvlc/contracts-types 中的 HostApi 定义
843
+ *
844
+ * 组件只能通过 HostAPI 与外界交互,禁止:
845
+ * - 直接调用 window.fetch(绕过 Data Proxy)
846
+ * - 直接操作 location.href(绕过权限/埋点)
847
+ * - 随意访问 localStorage(乱写)
848
+ * - 随意上报(污染数据)
559
849
  */
560
850
 
561
851
  /**
562
- * 事件总线配置
852
+ * 运行时上下文
563
853
  */
564
- interface EventBusOptions {
565
- /** 是否开启调试 */
854
+ interface RuntimeContext {
855
+ pageUid: string;
856
+ pageVersionId: string;
857
+ runtimeVersion: string;
858
+ userId?: string;
859
+ deviceId?: string;
860
+ channel?: string;
861
+ appId: string;
862
+ env: 'development' | 'staging' | 'production';
863
+ isEditMode: boolean;
864
+ isPreviewMode: boolean;
865
+ }
866
+ /**
867
+ * Host API 配置
868
+ */
869
+ interface HostAPIOptions {
870
+ /** API 基础 URL */
871
+ apiBaseUrl: string;
872
+ /** 认证 Token */
873
+ authToken?: string;
874
+ /** 请求头 */
875
+ headers?: Record<string, string>;
876
+ /** 状态管理器 */
877
+ stateManager: StateManager;
878
+ /** 事件总线 */
879
+ eventBus: EventBus;
880
+ /** 表达式引擎 */
881
+ expressionEngine: ExpressionEngine;
882
+ /** 运行时上下文 */
883
+ context: RuntimeContext;
884
+ /** 调试模式 */
566
885
  debug?: boolean;
567
886
  /** 日志器 */
568
887
  logger?: Logger;
569
- /** 最大监听器数量 */
570
- maxListeners?: number;
571
888
  }
572
889
  /**
573
- * 事件总线实现
890
+ * Host API 实现
891
+ * 完全实现 @djvlc/contracts-types 中定义的 HostApi 接口
574
892
  */
575
- declare class EventBus implements EventBus$1 {
576
- private handlers;
893
+ declare class HostAPIImpl implements HostApi {
577
894
  private options;
578
- constructor(options?: EventBusOptions);
895
+ private storageNamespace;
896
+ private loadingCount;
897
+ private loadingElement;
898
+ constructor(options: HostAPIOptions);
899
+ navigate(options: NavigateOptions): Promise<void>;
900
+ track(event: TrackEvent): void;
901
+ requestData<T = unknown>(queryId: string, params?: Record<string, unknown>): Promise<T>;
902
+ executeAction<T = unknown>(actionType: string, params?: Record<string, unknown>): Promise<ActionResult<T>>;
903
+ openDialog(options: DialogOptions): Promise<DialogResult>;
904
+ closeDialog(dialogId?: string): void;
905
+ showToast(options: ToastOptions): void;
906
+ showLoading(message?: string): void;
907
+ hideLoading(): void;
908
+ clipboard: ClipboardApi;
909
+ storage: StorageApi;
910
+ share(options: ShareOptions): Promise<ShareResult>;
911
+ confirm(options: ConfirmOptions): Promise<boolean>;
912
+ showActionSheet(options: ActionSheetOptions): Promise<ActionSheetResult>;
913
+ previewImage(options: PreviewImageOptions): void;
914
+ scanCode(): Promise<ScanCodeResult>;
915
+ getState<T = unknown>(path: string): T | undefined;
916
+ setState(path: string, value: unknown): void;
917
+ getContext(): ComponentContext;
918
+ private buildHeaders;
919
+ private generateIdempotencyKey;
920
+ private simpleHash;
921
+ private fallbackCopy;
922
+ private log;
923
+ }
924
+
925
+ /**
926
+ * 主运行时类
927
+ * 统一管理整个运行时生命周期
928
+ *
929
+ * 仅支持 PageSchema V1.0.0(与 contracts-types 保持一致)
930
+ */
931
+
932
+ /**
933
+ * 支持的 Schema 版本(与 contracts-types 保持一致)
934
+ */
935
+ declare const SUPPORTED_SCHEMA_VERSION = "1.0.0";
936
+ /**
937
+ * 创建运行时实例
938
+ */
939
+ declare function createRuntime(options: RuntimeOptions): DjvlcRuntime;
940
+ /**
941
+ * DJVLC 运行时
942
+ *
943
+ * 核心职责:
944
+ * 1. resolve & load:拿到 pageVersion 与 manifest,按 componentVersion 拉资源
945
+ * 2. render:把 schema 变成 UI(基于 Web Components)
946
+ * 3. boundary:组件只能通过 hostApi 触发 data/action/navigate/track
947
+ */
948
+ declare class DjvlcRuntime {
949
+ private options;
950
+ private container;
951
+ private pageLoader;
952
+ private componentLoader;
953
+ private assetLoader;
954
+ private stateManager;
955
+ private dataBindingManager;
956
+ private eventBus;
957
+ private actionBridge;
958
+ private expressionEngine;
959
+ private securityManager;
960
+ private telemetryManager;
961
+ private renderer;
962
+ private hostApi;
963
+ private lifecycleManager;
964
+ private logger;
965
+ constructor(options: RuntimeOptions);
579
966
  /**
580
- * 发送事件
967
+ * 初始化运行时
581
968
  */
582
- emit<T>(event: RuntimeEvent<T>): void;
969
+ init(): Promise<void>;
970
+ /**
971
+ * 加载页面
972
+ */
973
+ load(): Promise<PageResolveResult>;
974
+ /**
975
+ * 渲染页面
976
+ */
977
+ render(): Promise<void>;
978
+ /**
979
+ * 获取 Host API
980
+ */
981
+ getHostApi(): HostAPIImpl;
982
+ /**
983
+ * 获取当前状态
984
+ */
985
+ getState(): RuntimeState;
986
+ /**
987
+ * 订阅状态变更
988
+ */
989
+ onStateChange(listener: (state: RuntimeState) => void): () => void;
583
990
  /**
584
991
  * 订阅事件
585
992
  */
586
- on<T>(type: RuntimeEventType, handler: EventHandler<T>): Unsubscribe;
993
+ on<T>(type: RuntimeEvent<T>['type'], handler: (event: RuntimeEvent<T>) => void): () => void;
994
+ /**
995
+ * 更新组件
996
+ */
997
+ updateComponent(componentId: string, props: Record<string, unknown>): void;
587
998
  /**
588
- * 取消订阅
999
+ * 设置变量
589
1000
  */
590
- off<T>(type: RuntimeEventType, handler: EventHandler<T>): void;
1001
+ setVariable(key: string, value: unknown): void;
591
1002
  /**
592
- * 一次性订阅
1003
+ * 获取变量
593
1004
  */
594
- once<T>(type: RuntimeEventType, handler: EventHandler<T>): Unsubscribe;
1005
+ getVariable<T = unknown>(key: string): T | undefined;
595
1006
  /**
596
- * 清除所有监听器
1007
+ * 刷新数据
597
1008
  */
598
- clear(type?: RuntimeEventType): void;
1009
+ refreshData(queryId: string): Promise<void>;
599
1010
  /**
600
- * 获取监听器数量
1011
+ * 执行动作
601
1012
  */
602
- listenerCount(type: RuntimeEventType): number;
1013
+ executeAction(actionType: string, params?: Record<string, unknown>): Promise<unknown>;
603
1014
  /**
604
- * 创建事件
1015
+ * 销毁运行时
605
1016
  */
606
- static createEvent<T>(type: RuntimeEventType, data: T, traceId?: string): RuntimeEvent<T>;
1017
+ destroy(): Promise<void>;
1018
+ /**
1019
+ * 验证 Schema 版本
1020
+ */
1021
+ private validateSchemaVersion;
1022
+ private resolveContainer;
1023
+ private initHostApi;
1024
+ private initActionBridge;
1025
+ private initDataBindingManager;
1026
+ private initLifecycleManager;
1027
+ private initRenderer;
1028
+ /**
1029
+ * 处理组件事件
1030
+ */
1031
+ private handleComponentEvent;
1032
+ private handleError;
1033
+ private emitEvent;
1034
+ private createLogger;
607
1035
  private log;
608
1036
  }
609
1037
 
610
1038
  /**
611
- * 动作桥接器
612
- * 处理组件事件到动作的映射
1039
+ * 运行时错误类型定义
613
1040
  */
614
1041
 
615
1042
  /**
616
- * 动作执行器
1043
+ * 运行时错误基类
617
1044
  */
618
- interface ActionExecutor {
619
- execute(actionType: string, params: Record<string, unknown>): Promise<unknown>;
1045
+ declare class DjvlcRuntimeError extends Error {
1046
+ readonly code: ErrorCode;
1047
+ readonly details?: Record<string, unknown>;
1048
+ readonly traceId?: string;
1049
+ readonly timestamp: number;
1050
+ constructor(code: ErrorCode, message?: string, details?: Record<string, unknown>, traceId?: string);
1051
+ toJSON(): {
1052
+ name: string;
1053
+ code: ErrorCode;
1054
+ message: string;
1055
+ details: Record<string, unknown> | undefined;
1056
+ traceId: string | undefined;
1057
+ timestamp: number;
1058
+ };
620
1059
  }
621
1060
  /**
622
- * 动作桥接器配置
1061
+ * 页面加载错误
623
1062
  */
624
- interface ActionBridgeOptions {
625
- /** 动作执行器 */
626
- executor: ActionExecutor;
627
- /** 调试模式 */
628
- debug?: boolean;
629
- /** 日志器 */
630
- logger?: Logger;
1063
+ declare class PageLoadError extends DjvlcRuntimeError {
1064
+ constructor(message: string, details?: Record<string, unknown>, traceId?: string);
631
1065
  }
632
1066
  /**
633
- * 动作桥接器
634
- * 将组件事件转换为动作执行
1067
+ * 组件加载错误
635
1068
  */
636
- declare class ActionBridge {
637
- private options;
638
- private debounceTimers;
639
- private throttleTimers;
640
- constructor(options: ActionBridgeOptions);
641
- /**
642
- * 处理组件事件
643
- */
644
- handleEvent(event: ComponentEvent, eventData: Record<string, unknown>, context: Record<string, unknown>): Promise<void>;
645
- /**
646
- * 销毁
647
- */
648
- destroy(): void;
649
- private executeAction;
650
- private resolveParams;
651
- private debounce;
652
- private throttle;
653
- private delay;
654
- private log;
1069
+ declare class ComponentLoadError extends DjvlcRuntimeError {
1070
+ readonly componentName: string;
1071
+ readonly componentVersion: string;
1072
+ constructor(componentName: string, componentVersion: string, message: string, code?: ErrorCode, details?: Record<string, unknown>);
655
1073
  }
656
-
657
1074
  /**
658
- * 表达式词法分析器
659
- * 将表达式字符串分解为 Token
1075
+ * 组件完整性校验错误
660
1076
  */
1077
+ declare class IntegrityError extends DjvlcRuntimeError {
1078
+ readonly componentName: string;
1079
+ readonly componentVersion: string;
1080
+ readonly expectedHash: string;
1081
+ readonly actualHash: string;
1082
+ constructor(componentName: string, componentVersion: string, expectedHash: string, actualHash: string);
1083
+ }
661
1084
  /**
662
- * Token 类型
1085
+ * 组件阻断错误
663
1086
  */
664
- type TokenType = 'NUMBER' | 'STRING' | 'BOOLEAN' | 'NULL' | 'IDENTIFIER' | 'OPERATOR' | 'DOT' | 'LBRACKET' | 'RBRACKET' | 'LPAREN' | 'RPAREN' | 'COMMA' | 'QUESTION' | 'COLON' | 'EOF';
1087
+ declare class ComponentBlockedError extends DjvlcRuntimeError {
1088
+ readonly componentName: string;
1089
+ readonly componentVersion: string;
1090
+ readonly reason: string;
1091
+ constructor(componentName: string, componentVersion: string, reason: string);
1092
+ }
665
1093
  /**
666
- * Token
1094
+ * 表达式求值错误
667
1095
  */
668
- interface Token {
669
- type: TokenType;
670
- value: string | number | boolean | null;
671
- start: number;
672
- end: number;
1096
+ declare class ExpressionError extends DjvlcRuntimeError {
1097
+ readonly expression: string;
1098
+ readonly position?: {
1099
+ start: number;
1100
+ end: number;
1101
+ };
1102
+ constructor(expression: string, message: string, position?: {
1103
+ start: number;
1104
+ end: number;
1105
+ }, details?: Record<string, unknown>);
673
1106
  }
674
1107
  /**
675
- * 词法分析器
1108
+ * 动作执行错误
676
1109
  */
677
- declare class Lexer {
678
- private input;
679
- private pos;
680
- private tokens;
681
- constructor(input: string);
682
- /**
683
- * 分析表达式,返回 Token 列表
684
- */
685
- tokenize(): Token[];
686
- private readToken;
687
- private readNumber;
688
- private readString;
689
- private readIdentifier;
690
- private readOperator;
691
- private skipWhitespace;
692
- private isDigit;
693
- private isIdentifierStart;
694
- private isIdentifierChar;
695
- private peek;
1110
+ declare class ActionError extends DjvlcRuntimeError {
1111
+ readonly actionType: string;
1112
+ readonly actionId?: string;
1113
+ constructor(actionType: string, message: string, code?: ErrorCode, actionId?: string, details?: Record<string, unknown>);
1114
+ }
1115
+ /**
1116
+ * 数据查询错误
1117
+ */
1118
+ declare class QueryError extends DjvlcRuntimeError {
1119
+ readonly queryId: string;
1120
+ constructor(queryId: string, message: string, code?: ErrorCode, details?: Record<string, unknown>);
1121
+ }
1122
+ /**
1123
+ * 渲染错误
1124
+ */
1125
+ declare class RenderError extends DjvlcRuntimeError {
1126
+ readonly componentId: string;
1127
+ readonly componentType: string;
1128
+ constructor(componentId: string, componentType: string, message: string, details?: Record<string, unknown>);
696
1129
  }
697
1130
 
698
1131
  /**
699
- * 表达式解析器
700
- * Token 列表解析为 AST
1132
+ * 页面加载器
1133
+ * 负责从 API 解析页面配置
701
1134
  */
702
1135
 
703
1136
  /**
704
- * AST 节点类型
1137
+ * 页面加载器配置
705
1138
  */
706
- interface LiteralASTNode extends ExpressionNode {
707
- type: 'literal';
708
- value: string | number | boolean | null;
709
- }
710
- interface IdentifierASTNode extends ExpressionNode {
711
- type: 'identifier';
712
- name: string;
713
- }
714
- interface MemberASTNode extends ExpressionNode {
715
- type: 'member';
716
- object: ASTNode;
717
- property: string | ASTNode;
718
- computed: boolean;
719
- }
720
- interface CallASTNode extends ExpressionNode {
721
- type: 'call';
722
- callee: string;
723
- arguments: ASTNode[];
724
- }
725
- interface BinaryASTNode extends ExpressionNode {
726
- type: 'binary';
727
- operator: string;
728
- left: ASTNode;
729
- right: ASTNode;
730
- }
731
- interface UnaryASTNode extends ExpressionNode {
732
- type: 'unary';
733
- operator: string;
734
- argument: ASTNode;
735
- }
736
- interface ConditionalASTNode extends ExpressionNode {
737
- type: 'conditional';
738
- test: ASTNode;
739
- consequent: ASTNode;
740
- alternate: ASTNode;
741
- }
742
- interface ArrayASTNode extends ExpressionNode {
743
- type: 'array';
744
- elements: ASTNode[];
1139
+ interface PageLoaderOptions {
1140
+ /** API 基础 URL */
1141
+ apiBaseUrl: string;
1142
+ /** 渠道 */
1143
+ channel?: 'preview' | 'prod' | 'gray';
1144
+ /** 认证 Token */
1145
+ authToken?: string;
1146
+ /** 预览 Token */
1147
+ previewToken?: string;
1148
+ /** 请求头 */
1149
+ headers?: Record<string, string>;
1150
+ /** 缓存配置 */
1151
+ cache?: {
1152
+ enabled: boolean;
1153
+ maxAge?: number;
1154
+ };
1155
+ /** 日志器 */
1156
+ logger?: Logger;
745
1157
  }
746
- type ASTNode = LiteralASTNode | IdentifierASTNode | MemberASTNode | CallASTNode | BinaryASTNode | UnaryASTNode | ConditionalASTNode | ArrayASTNode;
747
1158
  /**
748
- * 表达式解析器
1159
+ * 页面加载器
1160
+ * 负责调用 /page/resolve 接口获取页面数据
749
1161
  */
750
- declare class Parser {
751
- private tokens;
752
- private pos;
753
- constructor(tokens: Token[]);
1162
+ declare class PageLoader {
1163
+ private options;
1164
+ private cache;
1165
+ constructor(options: PageLoaderOptions);
754
1166
  /**
755
- * 解析表达式
1167
+ * 解析页面
1168
+ * @param pageUid 页面 UID
1169
+ * @param params 额外参数
756
1170
  */
757
- parse(): ASTNode;
758
- private parseExpression;
759
- private parseTernary;
760
- private parseBinary;
761
- private parseUnary;
762
- private parsePostfix;
763
- private parsePrimary;
764
- private parseArray;
765
- private parseArguments;
766
- private current;
767
- private advance;
768
- private expect;
1171
+ resolve(pageUid: string, params?: {
1172
+ uid?: string;
1173
+ deviceId?: string;
1174
+ }): Promise<PageResolveResult>;
1175
+ /**
1176
+ * 预连接 API 服务器
1177
+ */
1178
+ preconnect(): void;
1179
+ /**
1180
+ * 清除缓存
1181
+ */
1182
+ clearCache(pageUid?: string): void;
1183
+ private buildResolveUrl;
1184
+ private buildHeaders;
1185
+ private getCacheKey;
1186
+ private isCacheValid;
1187
+ private isValidPageResolveResult;
1188
+ private log;
769
1189
  }
770
1190
 
771
1191
  /**
772
- * 表达式求值器
773
- * 安全地执行 AST,无 eval
1192
+ * 组件加载器
1193
+ * 负责从 CDN 加载组件资源
774
1194
  */
775
1195
 
776
1196
  /**
777
- * 求值结果
778
- */
779
- interface EvaluationResult {
780
- value: unknown;
781
- error?: Error;
782
- }
783
- /**
784
- * 求值器配置
1197
+ * 组件加载器配置
785
1198
  */
786
- interface EvaluatorOptions {
787
- /** 最大递归深度 */
788
- maxDepth?: number;
789
- /** 最大执行时间(毫秒) */
1199
+ interface ComponentLoaderOptions {
1200
+ /** CDN 基础 URL */
1201
+ cdnBaseUrl: string;
1202
+ /** 是否启用 SRI 校验 */
1203
+ enableSRI?: boolean;
1204
+ /** 被阻断的组件列表 */
1205
+ blockedComponents?: string[];
1206
+ /** 并行加载数量 */
1207
+ concurrency?: number;
1208
+ /** 超时时间(毫秒) */
790
1209
  timeout?: number;
791
- /** 调试模式 */
792
- debug?: boolean;
1210
+ /** 日志器 */
1211
+ logger?: Logger;
793
1212
  }
794
1213
  /**
795
- * 表达式求值器
1214
+ * 组件加载器
1215
+ * 负责加载 Web Components
796
1216
  */
797
- declare class Evaluator {
1217
+ declare class ComponentLoader {
798
1218
  private options;
799
- private depth;
800
- private startTime;
801
- constructor(options?: EvaluatorOptions);
1219
+ private loadedComponents;
1220
+ private loadingPromises;
1221
+ private blockedSet;
1222
+ constructor(options: ComponentLoaderOptions);
802
1223
  /**
803
- * 求值表达式
1224
+ * 加载单个组件
804
1225
  */
805
- evaluate(ast: ASTNode, context: ExpressionContext): EvaluationResult;
806
- private evaluateNode;
807
- private evaluateLiteral;
808
- private evaluateIdentifier;
809
- private evaluateMember;
810
- private evaluateCall;
811
- private evaluateBinary;
812
- private evaluateUnary;
813
- private evaluateConditional;
814
- private evaluateArray;
815
- private checkLimits;
1226
+ load(item: ManifestItem): Promise<LoadedComponent>;
1227
+ /**
1228
+ * 加载 Manifest 中的所有组件
1229
+ */
1230
+ loadAll(manifest: PageManifest): Promise<Map<string, ComponentLoadResult>>;
1231
+ /**
1232
+ * 预加载组件
1233
+ */
1234
+ preload(items: ManifestItem[]): void;
1235
+ /**
1236
+ * 检查组件是否已加载
1237
+ */
1238
+ isLoaded(name: string, version: string): boolean;
1239
+ /**
1240
+ * 获取已加载的组件
1241
+ */
1242
+ get(name: string, version: string): LoadedComponent | undefined;
1243
+ /**
1244
+ * 检查组件是否被阻断
1245
+ */
1246
+ isBlocked(name: string, version: string): boolean;
1247
+ /**
1248
+ * 更新阻断列表
1249
+ */
1250
+ updateBlockedList(blocked: string[]): void;
1251
+ private loadComponent;
1252
+ private fetchWithTimeout;
1253
+ private validateIntegrity;
1254
+ private executeScript;
1255
+ private resolveUrl;
1256
+ private getComponentKey;
1257
+ private log;
816
1258
  }
817
1259
 
818
1260
  /**
819
- * 表达式引擎
820
- * 统一的表达式解析和求值入口
1261
+ * 资源加载器
1262
+ * 负责预连接、预加载等资源优化
821
1263
  */
822
-
823
1264
  /**
824
- * 表达式引擎配置
1265
+ * 资源加载器配置
825
1266
  */
826
- interface ExpressionEngineOptions extends EvaluatorOptions {
827
- /** 是否缓存 AST */
828
- cacheAST?: boolean;
829
- /** 最大缓存大小 */
830
- maxCacheSize?: number;
831
- /** 日志器 */
832
- logger?: Logger;
1267
+ interface AssetLoaderOptions {
1268
+ /** CDN 域名列表 */
1269
+ cdnHosts: string[];
1270
+ /** API 域名列表 */
1271
+ apiHosts: string[];
833
1272
  }
834
1273
  /**
835
- * 表达式引擎
836
- * 安全的表达式解析和求值,无 eval/new Function
1274
+ * 资源加载器
1275
+ * 负责资源预连接、预加载等优化
837
1276
  */
838
- declare class ExpressionEngine {
1277
+ declare class AssetLoader {
839
1278
  private options;
840
- private evaluator;
841
- private astCache;
842
- constructor(options?: ExpressionEngineOptions);
1279
+ private preconnectedHosts;
1280
+ private preloadedAssets;
1281
+ constructor(options: AssetLoaderOptions);
843
1282
  /**
844
- * 求值表达式
1283
+ * 预连接所有域名
845
1284
  */
846
- evaluate(expression: string, context: ExpressionContext): EvaluationResult;
1285
+ preconnectAll(): void;
847
1286
  /**
848
- * 求值表达式并返回值
849
- * 出错时返回 fallback 值
1287
+ * 预连接单个域名
850
1288
  */
851
- evaluateWithFallback<T>(expression: string, context: ExpressionContext, fallback: T): T;
1289
+ preconnect(host: string): void;
852
1290
  /**
853
- * 求值模板字符串
854
- * 支持 ${expression} 语法
1291
+ * DNS 预解析
855
1292
  */
856
- evaluateTemplate(template: string, context: ExpressionContext): string;
1293
+ dnsPrefetch(host: string): void;
857
1294
  /**
858
- * 解析表达式为 AST
1295
+ * 预加载脚本
859
1296
  */
860
- parse(expression: string): ASTNode;
1297
+ preloadScript(url: string, integrity?: string): void;
861
1298
  /**
862
- * 校验表达式
1299
+ * 预加载样式
863
1300
  */
864
- validate(expression: string): ExpressionValidationResult;
1301
+ preloadStyle(url: string, integrity?: string): void;
865
1302
  /**
866
- * 清除 AST 缓存
1303
+ * 预加载图片
867
1304
  */
868
- clearCache(): void;
869
- private collectReferences;
870
- private buildMemberPath;
871
- private log;
1305
+ preloadImage(url: string): void;
1306
+ /**
1307
+ * 预获取资源(低优先级)
1308
+ */
1309
+ prefetch(url: string, as?: 'script' | 'style' | 'image' | 'document'): void;
1310
+ /**
1311
+ * 加载样式表
1312
+ */
1313
+ loadStylesheet(url: string, integrity?: string): Promise<void>;
1314
+ /**
1315
+ * 加载脚本
1316
+ */
1317
+ loadScript(url: string, integrity?: string): Promise<void>;
872
1318
  }
873
1319
 
874
1320
  /**
875
- * 内置函数实现
876
- * 表达式中可调用的白名单函数
877
- */
878
- type BuiltinFn = (...args: unknown[]) => unknown;
879
- /**
880
- * 内置函数映射
881
- */
882
- declare const builtinFunctions: Record<string, BuiltinFn>;
883
-
884
- /**
885
- * Host API 实现
886
- * 提供给组件的安全 API
1321
+ * 安全管理器
1322
+ * SRI 校验、域名白名单、组件阻止、能力校验等
1323
+ *
1324
+ * 核心安全保证:
1325
+ * - 组件加载必须校验 SRI (integrity.json)
1326
+ * - 支持 registry blocklist(紧急止血)
1327
+ * - CSP 推荐配置(脚本域名收敛)
1328
+ * - 表达式引擎无 eval,函数白名单
887
1329
  */
888
1330
 
889
1331
  /**
890
- * Host API 配置
1332
+ * 组件能力类型(与 contracts CapabilityName 对齐)
891
1333
  */
892
- interface HostAPIOptions {
893
- /** API 基础 URL */
894
- apiBaseUrl: string;
895
- /** 认证 Token */
896
- authToken?: string;
897
- /** 请求头 */
898
- headers?: Record<string, string>;
899
- /** 状态管理器 */
900
- stateManager: StateManager;
901
- /** 事件总线 */
902
- eventBus: EventBus;
903
- /** 表达式引擎 */
904
- expressionEngine: ExpressionEngine;
905
- /** 运行时上下文 */
906
- context: RuntimeContext;
907
- /** 调试模式 */
908
- debug?: boolean;
909
- /** 日志器 */
910
- logger?: Logger;
911
- }
1334
+ type CapabilityType = CapabilityName;
912
1335
  /**
913
- * Host API 实现
1336
+ * 被阻断的组件信息
914
1337
  */
915
- declare class HostAPIImpl implements HostAPI {
916
- private options;
917
- private storageNamespace;
918
- constructor(options: HostAPIOptions);
919
- requestData<T = unknown>(queryId: string, params?: Record<string, unknown>): Promise<DataQueryResponse<T>>;
920
- executeAction<T = unknown>(actionType: string, params?: Record<string, unknown>): Promise<ActionExecuteResponse<T>>;
921
- navigate(options: NavigateOptions): void;
922
- goBack(): void;
923
- refresh(): void;
924
- openDialog(options: DialogOptions): Promise<boolean | string>;
925
- closeDialog(): void;
926
- showToast(options: ToastOptions): void;
927
- track(event: TrackEvent): void;
928
- copyToClipboard(text: string): Promise<ClipboardResult>;
929
- readFromClipboard(): Promise<ClipboardResult>;
930
- getState<T = unknown>(key: string): T | undefined;
931
- setState(key: string, value: unknown): void;
932
- getVariable<T = unknown>(name: string): T | undefined;
933
- postMessage(componentId: string, message: unknown): void;
934
- broadcast(channel: string, message: unknown): void;
935
- storage: {
936
- get: <T = unknown>(key: string, options?: StorageOptions) => T | undefined;
937
- set: (key: string, value: unknown, options?: StorageOptions) => void;
938
- remove: (key: string, options?: StorageOptions) => void;
939
- };
940
- getContext(): RuntimeContext;
941
- private buildHeaders;
942
- private getStorageKey;
943
- private generateIdempotencyKey;
944
- private simpleHash;
945
- private log;
1338
+ interface BlockedComponent {
1339
+ name: string;
1340
+ version: string;
1341
+ reason: string;
1342
+ blockedAt?: string;
1343
+ blockedBy?: string;
1344
+ urgent?: boolean;
1345
+ fallbackVersion?: string;
946
1346
  }
947
-
948
- /**
949
- * 安全管理器
950
- * SRI 校验、域名白名单、组件阻止等
951
- */
952
-
953
1347
  /**
954
1348
  * 安全管理器配置
955
1349
  */
@@ -964,6 +1358,10 @@ interface SecurityManagerOptions {
964
1358
  blockedComponents?: BlockedComponent[];
965
1359
  /** 被阻断的动作类型 */
966
1360
  blockedActions?: string[];
1361
+ /** 允许的能力列表(白名单模式) */
1362
+ allowedCapabilities?: CapabilityType[];
1363
+ /** 是否应用 CSP */
1364
+ applyCSPOnInit?: boolean;
967
1365
  /** 日志器 */
968
1366
  logger?: Logger;
969
1367
  }
@@ -974,11 +1372,20 @@ declare class SecurityManager {
974
1372
  private options;
975
1373
  private blockedComponentsMap;
976
1374
  private blockedActionsSet;
1375
+ private allowedCapabilitiesSet;
977
1376
  constructor(options?: SecurityManagerOptions);
978
1377
  /**
979
1378
  * 更新阻断列表
980
1379
  */
981
- updateBlockedList(components: BlockedComponent[], actions: string[]): void;
1380
+ updateBlockedList(components: string[], actions: string[]): void;
1381
+ /**
1382
+ * 添加被阻断的组件
1383
+ */
1384
+ addBlockedComponent(component: BlockedComponent): void;
1385
+ /**
1386
+ * 移除组件阻断
1387
+ */
1388
+ removeBlockedComponent(name: string, version: string): void;
982
1389
  /**
983
1390
  * 检查组件是否被阻断
984
1391
  */
@@ -987,18 +1394,44 @@ declare class SecurityManager {
987
1394
  * 获取组件阻断信息
988
1395
  */
989
1396
  getBlockedInfo(name: string, version: string): BlockedComponent | undefined;
1397
+ /**
1398
+ * 获取所有被阻断的组件
1399
+ */
1400
+ getAllBlockedComponents(): BlockedComponent[];
990
1401
  /**
991
1402
  * 检查动作是否被阻断
992
1403
  */
993
1404
  isActionBlocked(actionType: string): boolean;
994
1405
  /**
995
- * 验证组件完整性
1406
+ * 检查能力是否被允许
1407
+ */
1408
+ isCapabilityAllowed(capability: CapabilityType): boolean;
1409
+ /**
1410
+ * 校验组件能力声明
1411
+ */
1412
+ validateCapabilities(componentName: string, componentVersion: string, capabilities: CapabilityType[]): {
1413
+ valid: boolean;
1414
+ disallowed: CapabilityType[];
1415
+ };
1416
+ /**
1417
+ * 验证组件完整性 (SRI)
1418
+ */
1419
+ validateIntegrity(name: string, version: string, content: string, expectedIntegrity: string): Promise<void>;
1420
+ /**
1421
+ * 生成 SRI 哈希
996
1422
  */
997
- validateComponent(name: string, version: string, content: string, expectedIntegrity: string): Promise<void>;
1423
+ generateIntegrity(content: string, algorithm?: 'sha256' | 'sha384' | 'sha512'): Promise<string>;
998
1424
  /**
999
1425
  * 验证 URL 是否在白名单内
1000
1426
  */
1001
1427
  isAllowedUrl(url: string, type: 'cdn' | 'api'): boolean;
1428
+ /**
1429
+ * 验证外部 URL 安全性
1430
+ */
1431
+ validateExternalUrl(url: string): {
1432
+ safe: boolean;
1433
+ reason?: string;
1434
+ };
1002
1435
  /**
1003
1436
  * 生成 CSP 策略
1004
1437
  */
@@ -1011,17 +1444,21 @@ declare class SecurityManager {
1011
1444
  * 确保组件未被阻断
1012
1445
  */
1013
1446
  assertNotBlocked(name: string, version: string): void;
1447
+ /**
1448
+ * 创建安全的 eval 替代方案(用于动态代码执行)
1449
+ * 注意:应尽量避免使用,优先使用表达式引擎
1450
+ */
1451
+ createSafeEvaluator(): (code: string, context: Record<string, unknown>) => unknown;
1014
1452
  /**
1015
1453
  * 计算哈希值
1016
1454
  */
1017
1455
  private computeHash;
1018
- private _log;
1019
- protected get log(): (level: "debug" | "info" | "warn" | "error", message: string) => void;
1456
+ private log;
1020
1457
  }
1021
1458
 
1022
1459
  /**
1023
1460
  * 基础渲染器
1024
- * 框架无关的渲染核心
1461
+ * 框架无关的渲染核心,支持 PageSchema V1.0.0
1025
1462
  */
1026
1463
 
1027
1464
  /**
@@ -1034,6 +1471,8 @@ interface BaseRendererOptions {
1034
1471
  components: Map<string, LoadedComponent>;
1035
1472
  /** Host API 注入函数 */
1036
1473
  injectHostApi: (element: HTMLElement, componentId: string) => void;
1474
+ /** 事件处理器 */
1475
+ onComponentEvent?: (componentId: string, eventName: string, handlers: EventHandler[], eventData: unknown) => void;
1037
1476
  /** 调试模式 */
1038
1477
  debug?: boolean;
1039
1478
  /** 日志器 */
@@ -1043,13 +1482,25 @@ interface BaseRendererOptions {
1043
1482
  }
1044
1483
  /**
1045
1484
  * 基础渲染器
1046
- * 将 PageSchema 渲染为 DOM
1485
+ * 将 PageSchema V1.0.0 渲染为 DOM
1047
1486
  */
1048
1487
  declare class BaseRenderer implements Renderer {
1049
1488
  private options;
1050
1489
  private container;
1051
1490
  private renderedElements;
1491
+ private componentEventListeners;
1492
+ /**
1493
+ * 表达式上下文
1494
+ * 使用 contracts 中的 ExpressionContext 定义:
1495
+ * - state: 页面状态
1496
+ * - binding: 数据绑定结果
1497
+ * - local: 局部变量(循环变量、事件参数等)
1498
+ * - props?: 组件属性
1499
+ * - event?: 事件上下文
1500
+ */
1052
1501
  private expressionContext;
1502
+ private loopContextStack;
1503
+ private styleElement;
1053
1504
  constructor(options: BaseRendererOptions);
1054
1505
  /**
1055
1506
  * 初始化渲染器
@@ -1067,18 +1518,85 @@ declare class BaseRenderer implements Renderer {
1067
1518
  * 更新表达式上下文
1068
1519
  */
1069
1520
  updateContext(context: Partial<ExpressionContext>): void;
1521
+ /**
1522
+ * 获取当前表达式上下文
1523
+ */
1524
+ getContext(): ExpressionContext;
1070
1525
  /**
1071
1526
  * 销毁渲染器
1072
1527
  */
1073
1528
  destroy(): void;
1074
1529
  /**
1075
- * 渲染单个组件
1530
+ * 清理资源
1531
+ */
1532
+ private cleanup;
1533
+ /**
1534
+ * 渲染组件节点
1535
+ */
1536
+ private renderNode;
1537
+ /**
1538
+ * 创建组件元素
1539
+ */
1540
+ private createElement;
1541
+ /**
1542
+ * 渲染循环
1543
+ */
1544
+ private renderLoop;
1545
+ /**
1546
+ * 渲染子组件
1547
+ */
1548
+ private renderChildren;
1549
+ /**
1550
+ * 创建插槽容器
1076
1551
  */
1077
- private renderComponent;
1552
+ private createSlotContainer;
1553
+ /**
1554
+ * 绑定事件处理器
1555
+ */
1556
+ private bindEventHandlers;
1557
+ /**
1558
+ * 创建节流处理器
1559
+ */
1560
+ private createThrottledHandler;
1561
+ /**
1562
+ * 创建防抖处理器
1563
+ */
1564
+ private createDebouncedHandler;
1565
+ /**
1566
+ * 求值条件表达式
1567
+ */
1568
+ private evaluateCondition;
1569
+ /**
1570
+ * 求值表达式
1571
+ */
1572
+ private evaluateExpression;
1573
+ /**
1574
+ * 构建表达式上下文(包含循环变量)
1575
+ */
1576
+ private buildExpressionContext;
1577
+ /**
1578
+ * 解析 props 中的表达式
1579
+ */
1580
+ /**
1581
+ * Expression 类型列表(用于判断是否为 Expression 对象)
1582
+ */
1583
+ private static readonly EXPRESSION_TYPES;
1078
1584
  /**
1079
1585
  * 解析 props 中的表达式
1586
+ *
1587
+ * @param props - ComponentNode.props,类型为 Record<string, AnyValueOrExpression>
1080
1588
  */
1081
1589
  private resolveProps;
1590
+ /**
1591
+ * 解析单个值(支持 AnyValueOrExpression)
1592
+ *
1593
+ * AnyValueOrExpression 可以是:
1594
+ * - Expression 对象:{ type: 'state' | 'binding' | 'local' | 'template' | 'computed', value: string, fallback?: any }
1595
+ * - 原始值:string | number | boolean | null
1596
+ * - 嵌套数组:AnyValueOrExpression[]
1597
+ * - 嵌套对象:{ [key: string]: AnyValueOrExpression }
1598
+ */
1599
+ private resolveValue;
1082
1600
  /**
1083
1601
  * 应用 props 到元素
1084
1602
  */
@@ -1088,9 +1606,101 @@ declare class BaseRenderer implements Renderer {
1088
1606
  */
1089
1607
  private applyStyles;
1090
1608
  /**
1091
- * 应用页面样式
1609
+ * 应用布局到元素
1610
+ */
1611
+ private applyLayout;
1612
+ /**
1613
+ * 应用响应式布局
1614
+ */
1615
+ private applyResponsiveLayout;
1616
+ /**
1617
+ * 将布局值转换为 CSS
1618
+ */
1619
+ private layoutToCSS;
1620
+ /**
1621
+ * 应用页面配置(PageConfig 结构)
1622
+ *
1623
+ * PageConfig 三层分离结构:
1624
+ * - layout: PageLayoutConfig - 布局决策(关键路径,同步解析)
1625
+ * - styles?: PageStylesConfig - 样式注入(可延迟应用)
1626
+ * - behavior?: PageBehaviorConfig - 行为控制(独立处理)
1627
+ */
1628
+ private applyPageConfig;
1629
+ /**
1630
+ * 应用行为配置
1631
+ */
1632
+ private applyBehaviorConfig;
1633
+ /**
1634
+ * 应用国际化配置
1635
+ *
1636
+ * @description
1637
+ * 根据 PageI18nConfig 设置页面语言环境:
1638
+ * - 检测当前语言(根据 detection 策略)
1639
+ * - 设置 HTML lang 属性
1640
+ * - 初始化翻译上下文
1641
+ */
1642
+ private applyI18nConfig;
1643
+ /**
1644
+ * 检测当前语言
1645
+ *
1646
+ * @description
1647
+ * 根据检测策略确定当前语言:
1648
+ * - browser: 使用浏览器语言
1649
+ * - url: URL 参数(如 ?lang=zh)
1650
+ * - path: URL 路径(如 /zh/page)
1651
+ * - cookie: Cookie 中的语言设置
1652
+ * - header: Accept-Language 头(服务端使用)
1653
+ * - manual: 手动设置,使用默认语言
1092
1654
  */
1093
- private applyPageStyles;
1655
+ private detectLocale;
1656
+ /**
1657
+ * 从浏览器语言设置检测
1658
+ */
1659
+ private detectBrowserLocale;
1660
+ /**
1661
+ * 从 URL 参数检测(如 ?lang=zh)
1662
+ */
1663
+ private detectUrlLocale;
1664
+ /**
1665
+ * 从 URL 路径检测(如 /zh/page)
1666
+ */
1667
+ private detectPathLocale;
1668
+ /**
1669
+ * 从 Cookie 检测
1670
+ */
1671
+ private detectCookieLocale;
1672
+ /**
1673
+ * 获取文本方向(RTL 语言支持)
1674
+ */
1675
+ private getTextDirection;
1676
+ /**
1677
+ * 应用布局配置
1678
+ */
1679
+ private applyLayoutConfig;
1680
+ /**
1681
+ * 应用样式配置
1682
+ */
1683
+ private applyStylesConfig;
1684
+ /**
1685
+ * 应用主题配置
1686
+ */
1687
+ private applyThemeConfig;
1688
+ /**
1689
+ * 应用背景配置
1690
+ */
1691
+ private applyBackgroundConfig;
1692
+ /**
1693
+ * 注入自定义 CSS
1694
+ */
1695
+ private injectCustomCSS;
1696
+ /**
1697
+ * 初始化页面状态(从 PageState.fields)
1698
+ */
1699
+ private initializePageState;
1700
+ /**
1701
+ * 创建错误 fallback 元素
1702
+ */
1703
+ private createErrorFallback;
1094
1704
  private log;
1095
1705
  }
1096
1706
 
@@ -1110,19 +1720,33 @@ declare function createFallbackElement(type: 'fallback' | 'blocked' | 'error', m
1110
1720
  /**
1111
1721
  * 遥测管理器
1112
1722
  * 链路追踪、性能监控、错误上报
1723
+ *
1724
+ * 可观测字段贯穿:pageVersionId + componentVersion + actionId + traceId
1725
+ *
1726
+ * 集成方式:
1727
+ * - OTel trace context 透传模块
1728
+ * - Sentry 上报适配(release 对齐 runtimeVersion)
1113
1729
  */
1114
1730
 
1115
1731
  /**
1116
- * Span 信息
1732
+ * Span 信息(OpenTelemetry 兼容)
1117
1733
  */
1118
1734
  interface Span {
1735
+ /** Span ID */
1119
1736
  spanId: string;
1737
+ /** Trace ID */
1120
1738
  traceId: string;
1739
+ /** 父 Span ID */
1121
1740
  parentSpanId?: string;
1741
+ /** Span 名称 */
1122
1742
  name: string;
1743
+ /** 开始时间 (ms) */
1123
1744
  startTime: number;
1745
+ /** 结束时间 (ms) */
1124
1746
  endTime?: number;
1747
+ /** 状态 */
1125
1748
  status?: 'ok' | 'error';
1749
+ /** 属性 */
1126
1750
  attributes?: Record<string, unknown>;
1127
1751
  }
1128
1752
  /**
@@ -1135,14 +1759,47 @@ interface TelemetryManagerOptions {
1135
1759
  pageVersionId: string;
1136
1760
  /** 应用 ID */
1137
1761
  appId?: string;
1762
+ /** 运行时版本(用于 Sentry release) */
1763
+ runtimeVersion?: string;
1138
1764
  /** 采样率 (0-1) */
1139
1765
  sampleRate?: number;
1140
1766
  /** 上报 URL */
1141
1767
  endpoint?: string;
1768
+ /** 批量上报大小 */
1769
+ batchSize?: number;
1770
+ /** 批量上报间隔 (ms) */
1771
+ flushInterval?: number;
1142
1772
  /** 日志器 */
1143
1773
  logger?: Logger;
1144
1774
  /** 调试模式 */
1145
1775
  debug?: boolean;
1776
+ /** 指标回调 */
1777
+ onMetric?: (metric: PerformanceMetric) => void;
1778
+ /** 错误回调 */
1779
+ onError?: (error: TelemetryError) => void;
1780
+ }
1781
+ /**
1782
+ * 遥测错误
1783
+ */
1784
+ interface TelemetryError {
1785
+ /** 错误类型 */
1786
+ type: string;
1787
+ /** 错误消息 */
1788
+ message: string;
1789
+ /** 堆栈 */
1790
+ stack?: string;
1791
+ /** 追踪 ID */
1792
+ traceId: string;
1793
+ /** 页面版本 ID */
1794
+ pageVersionId: string;
1795
+ /** 组件版本 */
1796
+ componentVersion?: string;
1797
+ /** 动作 ID */
1798
+ actionId?: string;
1799
+ /** 额外上下文 */
1800
+ context?: Record<string, unknown>;
1801
+ /** 时间戳 */
1802
+ timestamp: number;
1146
1803
  }
1147
1804
  /**
1148
1805
  * 遥测管理器
@@ -1154,6 +1811,7 @@ declare class TelemetryManager {
1154
1811
  private metrics;
1155
1812
  private errors;
1156
1813
  private shouldSample;
1814
+ private flushTimer?;
1157
1815
  constructor(options: TelemetryManagerOptions);
1158
1816
  /**
1159
1817
  * 获取 Trace ID
@@ -1161,8 +1819,17 @@ declare class TelemetryManager {
1161
1819
  getTraceId(): string;
1162
1820
  /**
1163
1821
  * 获取 W3C Trace Context 格式的 traceparent
1822
+ * 格式: version-traceId-parentId-flags
1823
+ */
1824
+ getTraceparent(parentSpanId?: string): string;
1825
+ /**
1826
+ * 从 traceparent 解析 trace context
1164
1827
  */
1165
- getTraceparent(): string;
1828
+ parseTraceparent(traceparent: string): {
1829
+ traceId: string;
1830
+ parentSpanId: string;
1831
+ sampled: boolean;
1832
+ } | null;
1166
1833
  /**
1167
1834
  * 开始一个 Span
1168
1835
  */
@@ -1170,23 +1837,15 @@ declare class TelemetryManager {
1170
1837
  /**
1171
1838
  * 结束一个 Span
1172
1839
  */
1173
- endSpan(spanId: string, status?: 'ok' | 'error'): void;
1174
- /**
1175
- * 记录性能指标
1176
- */
1177
- recordMetric(type: PerformanceMetric['type'], name: string, duration: number, extra?: Record<string, unknown>): void;
1178
- /**
1179
- * 记录错误
1180
- */
1181
- recordError(error: Error | RuntimeError, context?: Record<string, unknown>): void;
1840
+ endSpan(spanId: string, status?: 'ok' | 'error', attributes?: Record<string, unknown>): void;
1182
1841
  /**
1183
1842
  * 记录页面加载时间
1184
1843
  */
1185
- recordPageLoad(duration: number, extra?: Record<string, unknown>): void;
1844
+ recordPageLoad(duration: number): void;
1186
1845
  /**
1187
1846
  * 记录组件加载时间
1188
1847
  */
1189
- recordComponentLoad(name: string, version: string, duration: number, success: boolean): void;
1848
+ recordComponentLoad(componentName: string, componentVersion: string, duration: number, success: boolean): void;
1190
1849
  /**
1191
1850
  * 记录首次渲染时间
1192
1851
  */
@@ -1194,11 +1853,19 @@ declare class TelemetryManager {
1194
1853
  /**
1195
1854
  * 记录动作执行时间
1196
1855
  */
1197
- recordActionExecute(actionType: string, duration: number, success: boolean): void;
1856
+ recordActionExecute(actionType: string, actionId: string, duration: number, success: boolean): void;
1198
1857
  /**
1199
1858
  * 记录查询执行时间
1200
1859
  */
1201
- recordQueryExecute(queryId: string, duration: number, success: boolean, fromCache: boolean): void;
1860
+ recordQueryExecute(queryId: string, duration: number, success: boolean, fromCache?: boolean): void;
1861
+ /**
1862
+ * 记录表达式求值时间
1863
+ */
1864
+ recordExpressionEval(_expression: string, duration: number, _success: boolean): void;
1865
+ /**
1866
+ * 记录错误
1867
+ */
1868
+ recordError(error: Error, context?: Record<string, unknown>): void;
1202
1869
  /**
1203
1870
  * 获取所有指标
1204
1871
  */
@@ -1207,6 +1874,20 @@ declare class TelemetryManager {
1207
1874
  * 获取所有 Span
1208
1875
  */
1209
1876
  getSpans(): Span[];
1877
+ /**
1878
+ * 获取所有错误
1879
+ */
1880
+ getErrors(): TelemetryError[];
1881
+ /**
1882
+ * 获取性能摘要
1883
+ */
1884
+ getPerformanceSummary(): {
1885
+ pageLoadTime?: number;
1886
+ firstRenderTime?: number;
1887
+ componentCount: number;
1888
+ avgComponentLoadTime: number;
1889
+ errorCount: number;
1890
+ };
1210
1891
  /**
1211
1892
  * 清理数据
1212
1893
  */
@@ -1215,14 +1896,116 @@ declare class TelemetryManager {
1215
1896
  * 刷新上报
1216
1897
  */
1217
1898
  flush(): Promise<void>;
1218
- private reportError;
1219
- private generateId;
1899
+ /**
1900
+ * 销毁管理器
1901
+ */
1902
+ destroy(): void;
1903
+ private recordMetricInternal;
1904
+ private startAutoFlush;
1905
+ private generateTraceId;
1906
+ private generateSpanId;
1220
1907
  private log;
1221
1908
  }
1222
- declare module './telemetry-manager' {
1223
- interface TelemetryManagerOptions {
1224
- onMetric?: (metric: PerformanceMetric) => void;
1225
- }
1909
+
1910
+ /**
1911
+ * 生命周期管理器
1912
+ * 处理 PageSchema 中的 PageLifecycle 配置
1913
+ *
1914
+ * 支持的生命周期钩子:
1915
+ * - onMounted: 页面挂载完成时触发
1916
+ * - onBeforeUnmount: 页面卸载前触发
1917
+ * - onVisibilityChange: 页面可见性变化时触发
1918
+ */
1919
+
1920
+ /**
1921
+ * 生命周期管理器配置
1922
+ */
1923
+ interface LifecycleManagerOptions {
1924
+ /** 动作桥接器 */
1925
+ actionBridge: ActionBridge;
1926
+ /** 获取表达式上下文 */
1927
+ getContext: () => ExpressionContext;
1928
+ /** 调试模式 */
1929
+ debug?: boolean;
1930
+ /** 日志器 */
1931
+ logger?: Logger;
1932
+ /** 生命周期事件回调 */
1933
+ onLifecycleEvent?: (event: LifecycleEventType, data?: unknown) => void;
1934
+ }
1935
+ /**
1936
+ * 生命周期事件类型
1937
+ */
1938
+ type LifecycleEventType = 'mounted' | 'beforeUnmount' | 'visibilityChange';
1939
+ /**
1940
+ * 生命周期管理器
1941
+ * 管理页面生命周期事件的注册和触发
1942
+ */
1943
+ declare class LifecycleManager {
1944
+ private options;
1945
+ private lifecycle;
1946
+ private mounted;
1947
+ private destroyed;
1948
+ private cleanupFns;
1949
+ constructor(options: LifecycleManagerOptions);
1950
+ /**
1951
+ * 注册生命周期钩子
1952
+ */
1953
+ register(lifecycle: PageLifecycle | undefined): void;
1954
+ /**
1955
+ * 触发 onMounted 生命周期
1956
+ */
1957
+ triggerMounted(): Promise<void>;
1958
+ /**
1959
+ * 触发 onBeforeUnmount 生命周期
1960
+ */
1961
+ triggerBeforeUnmount(): Promise<void>;
1962
+ /**
1963
+ * 触发 onVisibilityChange 生命周期
1964
+ */
1965
+ triggerVisibilityChange(isVisible: boolean): Promise<void>;
1966
+ /**
1967
+ * 销毁生命周期管理器
1968
+ */
1969
+ destroy(): Promise<void>;
1970
+ /**
1971
+ * 检查是否已挂载
1972
+ */
1973
+ isMounted(): boolean;
1974
+ /**
1975
+ * 检查是否已销毁
1976
+ */
1977
+ isDestroyed(): boolean;
1978
+ /**
1979
+ * 设置可见性监听器
1980
+ */
1981
+ private setupVisibilityListener;
1982
+ /**
1983
+ * 执行生命周期动作
1984
+ */
1985
+ private executeLifecycleActions;
1986
+ /**
1987
+ * 使用指定上下文执行生命周期动作
1988
+ */
1989
+ private executeLifecycleActionsWithContext;
1990
+ private log;
1226
1991
  }
1227
1992
 
1228
- export { type ASTNode, ActionBridge, type ActionBridgeOptions, ActionError, type ActionExecutor, AssetLoader, type AssetLoaderOptions, BaseRenderer, type BaseRendererOptions, ComponentBlockedError, ComponentLoadError, ComponentLoader, type ComponentLoaderOptions, DjvlcRuntime, DjvlcRuntimeError, type EvaluationResult, Evaluator, type EvaluatorOptions, EventBus, type EventBusOptions, type EventHandler, ExpressionEngine, type ExpressionEngineOptions, ExpressionError, HostAPIImpl, type HostAPIOptions, IntegrityError, Lexer, type LoadedComponent, type LogLevel, type Logger, PageLoadError, PageLoader, type PageLoaderOptions, Parser, QueryError, RenderError, type Renderer, type RuntimeOptions, type RuntimePhase, type RuntimeState, SecurityManager, type SecurityManagerOptions, type Span, type StateListener, StateManager, TelemetryManager, type TelemetryManagerOptions, type Token, type TokenType, type Unsubscribe, builtinFunctions, createFallbackElement, createRuntime, registerFallbackComponents };
1993
+ /**
1994
+ * @djvlc/runtime-core
1995
+ * DJV 低代码平台运行时核心库
1996
+ *
1997
+ * 核心原则:
1998
+ * - 不可变版本:pageVersion/componentVersion 发布后永不修改
1999
+ * - contracts 是宪法仓:只读依赖 @djvlc/contracts-* 系列包
2000
+ * - 可观测字段贯穿:pageVersionId + componentVersion + actionId + traceId
2001
+ * - 危险能力收口:业务动作只走 Action Gateway,数据访问只走 Data Proxy
2002
+ *
2003
+ * @packageDocumentation
2004
+ */
2005
+
2006
+ /**
2007
+ * Runtime 版本号
2008
+ */
2009
+ declare const RUNTIME_VERSION = "1.0.0";
2010
+
2011
+ export { type ASTNode, ActionBridge, type ActionBridgeOptions, ActionError, type ActionExecutor, AssetLoader, type AssetLoaderOptions, BaseRenderer, type BaseRendererOptions, ComponentBlockedError, ComponentLoadError, type ComponentLoadResult, type ComponentLoadStatus, ComponentLoader, type ComponentLoaderOptions, DjvlcRuntime, DjvlcRuntimeError, type EvaluationResult, Evaluator, type EvaluatorOptions, EventBus, type EventBusOptions, type EventHandlerFn, ExpressionEngine, type ExpressionEngineOptions, ExpressionError, HostAPIImpl, type HostAPIOptions, IntegrityError, Lexer, type LifecycleEventType, LifecycleManager, type LifecycleManagerOptions, type LoadedComponent, type LogLevel, type Logger, type MetricType, PageLoadError, PageLoader, type PageLoaderOptions, type PageResolveResult, Parser, type PerformanceMetric, QueryError, RUNTIME_VERSION, RenderError, type Renderer, type RuntimeError, type RuntimeErrorType, type RuntimeEvent, type RuntimeEventType, type RuntimeOptions, type RuntimePhase, type RuntimeState, SUPPORTED_SCHEMA_VERSION as SCHEMA_VERSION, SUPPORTED_SCHEMA_VERSION, SecurityManager, type SecurityManagerOptions, type Span, type StateListener, StateManager, TelemetryManager, type TelemetryManagerOptions, type Token, type TokenType, type Unsubscribe, builtinFunctions, createFallbackElement, createRuntime, registerFallbackComponents };