@djvlc/runtime-core 1.0.3 → 1.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.
package/dist/index.d.cts CHANGED
@@ -1,7 +1,83 @@
1
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';
2
+ import { PageResolveResponse, ActionResult, ExpressionContext, Expression, ExpressionValidationResult, ActionRef, EventHandler, HostApi, NavigateOptions, TrackEvent, DialogOptions, DialogResult, ToastOptions, ClipboardApi, StorageApi, ShareOptions, ShareResult, ConfirmOptions, ActionSheetOptions, ActionSheetResult, PreviewImageOptions, ScanCodeResult, ComponentContext, ErrorCode, ManifestItem, PageManifest, CapabilityName, PageSchema, PageLifecycle } from '@djvlc/contracts-types';
3
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';
4
4
 
5
+ /**
6
+ * User API 适配器接口
7
+ * 统一 Runtime 与 User API 的交互:页面解析、动作执行、数据查询、埋点
8
+ * 认证、请求头、重试等逻辑由实现类统一处理,便于测试与切换实现(如 openapi-user-client)
9
+ */
10
+
11
+ /** 页面解析入参 */
12
+ interface ResolvePageParams {
13
+ pageId: string;
14
+ uid?: string;
15
+ deviceId?: string;
16
+ env?: 'preview' | 'staging' | 'prod';
17
+ channel?: string;
18
+ previewToken?: string;
19
+ }
20
+ /** 动作执行入参 */
21
+ interface ExecuteActionParams {
22
+ actionType: string;
23
+ params: Record<string, unknown>;
24
+ context: {
25
+ pageVersionId: string;
26
+ uid?: string;
27
+ deviceId?: string;
28
+ channel?: string;
29
+ appId: string;
30
+ };
31
+ idempotencyKey: string;
32
+ }
33
+ /** 数据查询入参 */
34
+ interface ExecuteQueryParams {
35
+ queryVersionId: string;
36
+ params?: Record<string, unknown>;
37
+ context: {
38
+ pageVersionId: string;
39
+ uid?: string;
40
+ deviceId?: string;
41
+ };
42
+ }
43
+ /** 数据查询出参(与 User API /data/query 一致) */
44
+ interface ExecuteQueryResult {
45
+ success: boolean;
46
+ data?: unknown;
47
+ message?: string;
48
+ errorMessage?: string;
49
+ }
50
+ /** 埋点 payload(与 User API /track 一致) */
51
+ interface TrackPayload {
52
+ eventName: string;
53
+ params?: Record<string, unknown>;
54
+ type?: string;
55
+ timestamp?: number;
56
+ context: {
57
+ pageVersionId: string;
58
+ runtimeVersion: string;
59
+ userId?: string;
60
+ deviceId?: string;
61
+ channel?: string;
62
+ appId: string;
63
+ env: string;
64
+ };
65
+ }
66
+ /**
67
+ * User API 适配器
68
+ * 只约定入参/出参,不绑定具体 HTTP 库;由 Runtime 注入实现,单测可注入 Mock
69
+ */
70
+ interface UserApiAdapter {
71
+ /** 页面解析 GET /page/resolve */
72
+ resolvePage(params: ResolvePageParams): Promise<PageResolveResponse>;
73
+ /** 动作执行 POST /actions/execute */
74
+ executeAction<T = unknown>(params: ExecuteActionParams): Promise<ActionResult<T>>;
75
+ /** 数据查询 POST /data/query */
76
+ executeQuery(params: ExecuteQueryParams): Promise<ExecuteQueryResult>;
77
+ /** 埋点 POST /track(可异步、不阻塞) */
78
+ track(payload: TrackPayload): void;
79
+ }
80
+
5
81
  /**
6
82
  * 运行时初始化选项
7
83
  */
@@ -42,6 +118,8 @@ interface RuntimeOptions {
42
118
  onMetric?: (metric: PerformanceMetric) => void;
43
119
  /** 加载完成回调 */
44
120
  onLoad?: (data: PageResolveResult) => void;
121
+ /** User API 适配器(必填,由宿主注入,core 不绑定具体 HTTP/鉴权实现) */
122
+ userApiAdapter: UserApiAdapter;
45
123
  }
46
124
  /**
47
125
  * 运行时阶段
@@ -841,6 +919,44 @@ declare class ActionBridge {
841
919
  private log;
842
920
  }
843
921
 
922
+ /**
923
+ * 测试用 User API 适配器 Mock
924
+ * 单测中注入,不再 mock global.fetch
925
+ */
926
+
927
+ interface MockUserApiAdapterOptions {
928
+ /** resolvePage 返回值(可覆盖) */
929
+ resolvePageResponse?: PageResolveResponse;
930
+ /** executeAction 返回值(可覆盖) */
931
+ executeActionResponse?: ActionResult<unknown>;
932
+ /** executeQuery 返回值(可覆盖) */
933
+ executeQueryResponse?: ExecuteQueryResult;
934
+ /** 是否在 resolve 时抛错 */
935
+ resolvePageThrow?: Error;
936
+ /** 是否在 executeAction 时抛错 */
937
+ executeActionThrow?: Error;
938
+ /** 是否在 executeQuery 时抛错 */
939
+ executeQueryThrow?: Error;
940
+ }
941
+ /**
942
+ * 测试用 Mock 实现
943
+ * 记录调用次数与参数,返回可配置的默认值或自定义值
944
+ */
945
+ declare class MockUserApiAdapter implements UserApiAdapter {
946
+ private readonly options;
947
+ readonly calls: {
948
+ resolvePage: ResolvePageParams[];
949
+ executeAction: ExecuteActionParams[];
950
+ executeQuery: ExecuteQueryParams[];
951
+ track: TrackPayload[];
952
+ };
953
+ constructor(options?: MockUserApiAdapterOptions);
954
+ resolvePage(params: ResolvePageParams): Promise<PageResolveResponse>;
955
+ executeAction<T = unknown>(params: ExecuteActionParams): Promise<ActionResult<T>>;
956
+ executeQuery(params: ExecuteQueryParams): Promise<ExecuteQueryResult>;
957
+ track(payload: TrackPayload): void;
958
+ }
959
+
844
960
  /**
845
961
  * Host API 实现
846
962
  * 提供给组件的安全 API,完全对齐 @djvlc/contracts-types 中的 HostApi 定义
@@ -871,12 +987,8 @@ interface RuntimeContext {
871
987
  * Host API 配置
872
988
  */
873
989
  interface HostAPIOptions {
874
- /** API 基础 URL */
875
- apiBaseUrl: string;
876
- /** 认证 Token */
877
- authToken?: string;
878
- /** 请求头 */
879
- headers?: Record<string, string>;
990
+ /** User API 适配器(必填,仅通过适配器调用 User API) */
991
+ userApiAdapter: UserApiAdapter;
880
992
  /** 状态管理器 */
881
993
  stateManager: StateManager;
882
994
  /** 事件总线 */
@@ -919,7 +1031,6 @@ declare class HostAPIImpl implements HostApi {
919
1031
  getState<T = unknown>(path: string): T | undefined;
920
1032
  setState(path: string, value: unknown): void;
921
1033
  getContext(): ComponentContext;
922
- private buildHeaders;
923
1034
  private generateIdempotencyKey;
924
1035
  private simpleHash;
925
1036
  private fallbackCopy;
@@ -965,6 +1076,8 @@ declare class DjvlcRuntime {
965
1076
  private renderer;
966
1077
  private hostApi;
967
1078
  private lifecycleManager;
1079
+ /** User API 适配器,统一认证/请求头,供 PageLoader 与 HostAPIImpl 使用 */
1080
+ private userApiAdapter;
968
1081
  private logger;
969
1082
  constructor(options: RuntimeOptions);
970
1083
  /**
@@ -1141,17 +1254,17 @@ declare class RenderError extends DjvlcRuntimeError {
1141
1254
  * 页面加载器配置
1142
1255
  */
1143
1256
  interface PageLoaderOptions {
1144
- /** API 基础 URL */
1257
+ /** API 基础 URL(用于 preconnect、CDN 请求头) */
1145
1258
  apiBaseUrl: string;
1259
+ /** User API 适配器(必填,仅通过适配器调用 User API) */
1260
+ userApiAdapter: UserApiAdapter;
1146
1261
  /** 环境 */
1147
1262
  env?: 'preview' | 'staging' | 'prod';
1148
1263
  /** 渠道 */
1149
1264
  channel?: string;
1150
- /** 认证 Token */
1151
- authToken?: string;
1152
1265
  /** 预览 Token */
1153
1266
  previewToken?: string;
1154
- /** 请求头 */
1267
+ /** 请求头(CDN 请求用) */
1155
1268
  headers?: Record<string, string>;
1156
1269
  /** 缓存配置 */
1157
1270
  cache?: {
@@ -1180,10 +1293,9 @@ declare class PageLoader {
1180
1293
  deviceId?: string;
1181
1294
  }): Promise<PageResolveResult>;
1182
1295
  /**
1183
- * 调用 Resolve API
1184
- * 严格按照 contracts 中的 PageResolveRequest 和 PageResolveResponse
1296
+ * 通过 User API 适配器调用 resolve
1185
1297
  */
1186
- private callResolveApi;
1298
+ private callResolveViaAdapter;
1187
1299
  /**
1188
1300
  * 从 CDN 加载 snapshot 和 manifest
1189
1301
  */
@@ -1222,29 +1334,34 @@ declare class PageLoader {
1222
1334
 
1223
1335
  /**
1224
1336
  * 组件加载器
1225
- * 负责从 CDN 加载组件资源
1337
+ * 负责从 CDN 动态加载 Web Components,支持 SRI 完整性校验、组件阻断、并行加载与降级处理。
1338
+ * @see SKILL: runtime-component-loading
1226
1339
  */
1227
1340
 
1228
1341
  /**
1229
1342
  * 组件加载器配置
1230
1343
  */
1231
1344
  interface ComponentLoaderOptions {
1232
- /** CDN 基础 URL */
1345
+ /** CDN 基础 URL,用于解析相对 entry 路径 */
1233
1346
  cdnBaseUrl: string;
1234
- /** 是否启用 SRI 校验 */
1347
+ /** 是否启用 SRI 完整性校验,默认 true */
1235
1348
  enableSRI?: boolean;
1236
- /** 被阻断的组件列表 */
1349
+ /** 被阻断的组件列表(如 `['comp@1.0.0']` 或仅组件名 `['comp']`),用于紧急止血 */
1237
1350
  blockedComponents?: string[];
1238
- /** 并行加载数量 */
1351
+ /** 并行加载数量,默认 4 */
1239
1352
  concurrency?: number;
1240
- /** 超时时间(毫秒) */
1353
+ /** 单次请求超时(毫秒),默认 30000 */
1241
1354
  timeout?: number;
1355
+ /** 请求 CDN 时附加的请求头(如认证等) */
1356
+ headers?: Record<string, string>;
1357
+ /** 自定义拉取脚本的实现,不传则使用全局 fetch;测试或自定义 CDN 客户端时注入 */
1358
+ fetchComponentScript?: (url: string, init?: RequestInit) => Promise<Response>;
1242
1359
  /** 日志器 */
1243
1360
  logger?: Logger;
1244
1361
  }
1245
1362
  /**
1246
1363
  * 组件加载器
1247
- * 负责加载 Web Components
1364
+ * 负责从 CDN 加载 Web Components,支持缓存、阻断、SRI 校验与并行加载。
1248
1365
  */
1249
1366
  declare class ComponentLoader {
1250
1367
  private options;
@@ -1253,37 +1370,47 @@ declare class ComponentLoader {
1253
1370
  private blockedSet;
1254
1371
  constructor(options: ComponentLoaderOptions);
1255
1372
  /**
1256
- * 加载单个组件
1373
+ * 加载单个组件(按 name@version 去重,同一组件并发请求复用同一 Promise)
1374
+ * @param item - 清单项(name、version、entry、integrity 等)
1375
+ * @returns 已加载的组件信息(含 Component 构造函数)
1376
+ * @throws ComponentBlockedError 当组件在阻断列表中
1377
+ * @throws ComponentLoadError 当拉取或执行失败
1378
+ * @throws IntegrityError 当 SRI 校验失败
1257
1379
  */
1258
1380
  load(item: ManifestItem): Promise<LoadedComponent>;
1259
1381
  /**
1260
- * 加载 Manifest 中的所有组件
1382
+ * manifest 批量加载组件,按 concurrency 分批并行;关键组件失败会抛出,非关键组件记录失败。
1383
+ * @param manifest - 页面清单(含 components 数组)
1384
+ * @returns 以 name@version 为 key 的加载结果 Map(status: loaded | failed | blocked)
1261
1385
  */
1262
1386
  loadAll(manifest: PageManifest): Promise<Map<string, ComponentLoadResult>>;
1263
1387
  /**
1264
- * 预加载组件
1388
+ * 预加载组件:向 document.head 插入 &lt;link rel="preload" as="script"&gt;,仅作提示,不阻塞 load。
1389
+ * @param items - 需要预加载的清单项
1265
1390
  */
1266
1391
  preload(items: ManifestItem[]): void;
1267
1392
  /**
1268
- * 检查组件是否已加载
1393
+ * 检查指定 name@version 的组件是否已加载并缓存
1269
1394
  */
1270
1395
  isLoaded(name: string, version: string): boolean;
1271
1396
  /**
1272
- * 获取已加载的组件
1397
+ * 获取已加载的组件信息,未加载则返回 undefined
1273
1398
  */
1274
1399
  get(name: string, version: string): LoadedComponent | undefined;
1275
1400
  /**
1276
- * 检查组件是否被阻断
1401
+ * 检查组件是否在阻断列表中(支持 `name@version` 或仅 `name`)
1277
1402
  */
1278
1403
  isBlocked(name: string, version: string): boolean;
1279
1404
  /**
1280
- * 更新阻断列表
1405
+ * 更新阻断列表(会完全替换当前列表)
1281
1406
  */
1282
1407
  updateBlockedList(blocked: string[]): void;
1283
1408
  private loadComponent;
1284
1409
  private fetchWithTimeout;
1285
1410
  private validateIntegrity;
1286
1411
  private executeScript;
1412
+ /** kebab-case -> PascalCase,用于匹配组件命名导出 */
1413
+ private kebabToPascal;
1287
1414
  private resolveUrl;
1288
1415
  private getComponentKey;
1289
1416
  private log;
@@ -2040,4 +2167,4 @@ declare class LifecycleManager {
2040
2167
  */
2041
2168
  declare const RUNTIME_VERSION = "1.0.0";
2042
2169
 
2043
- 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 };
2170
+ 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, type ExecuteActionParams, type ExecuteQueryParams, type ExecuteQueryResult, ExpressionEngine, type ExpressionEngineOptions, ExpressionError, HostAPIImpl, type HostAPIOptions, IntegrityError, Lexer, type LifecycleEventType, LifecycleManager, type LifecycleManagerOptions, type LoadedComponent, type LogLevel, type Logger, type MetricType, MockUserApiAdapter, type MockUserApiAdapterOptions, PageLoadError, PageLoader, type PageLoaderOptions, type PageResolveResult, Parser, type PerformanceMetric, QueryError, RUNTIME_VERSION, RenderError, type Renderer, type ResolvePageParams, type RuntimeContext, 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 TrackPayload, type Unsubscribe, type UserApiAdapter, builtinFunctions, createFallbackElement, createRuntime, registerFallbackComponents };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,83 @@
1
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';
2
+ import { PageResolveResponse, ActionResult, ExpressionContext, Expression, ExpressionValidationResult, ActionRef, EventHandler, HostApi, NavigateOptions, TrackEvent, DialogOptions, DialogResult, ToastOptions, ClipboardApi, StorageApi, ShareOptions, ShareResult, ConfirmOptions, ActionSheetOptions, ActionSheetResult, PreviewImageOptions, ScanCodeResult, ComponentContext, ErrorCode, ManifestItem, PageManifest, CapabilityName, PageSchema, PageLifecycle } from '@djvlc/contracts-types';
3
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';
4
4
 
5
+ /**
6
+ * User API 适配器接口
7
+ * 统一 Runtime 与 User API 的交互:页面解析、动作执行、数据查询、埋点
8
+ * 认证、请求头、重试等逻辑由实现类统一处理,便于测试与切换实现(如 openapi-user-client)
9
+ */
10
+
11
+ /** 页面解析入参 */
12
+ interface ResolvePageParams {
13
+ pageId: string;
14
+ uid?: string;
15
+ deviceId?: string;
16
+ env?: 'preview' | 'staging' | 'prod';
17
+ channel?: string;
18
+ previewToken?: string;
19
+ }
20
+ /** 动作执行入参 */
21
+ interface ExecuteActionParams {
22
+ actionType: string;
23
+ params: Record<string, unknown>;
24
+ context: {
25
+ pageVersionId: string;
26
+ uid?: string;
27
+ deviceId?: string;
28
+ channel?: string;
29
+ appId: string;
30
+ };
31
+ idempotencyKey: string;
32
+ }
33
+ /** 数据查询入参 */
34
+ interface ExecuteQueryParams {
35
+ queryVersionId: string;
36
+ params?: Record<string, unknown>;
37
+ context: {
38
+ pageVersionId: string;
39
+ uid?: string;
40
+ deviceId?: string;
41
+ };
42
+ }
43
+ /** 数据查询出参(与 User API /data/query 一致) */
44
+ interface ExecuteQueryResult {
45
+ success: boolean;
46
+ data?: unknown;
47
+ message?: string;
48
+ errorMessage?: string;
49
+ }
50
+ /** 埋点 payload(与 User API /track 一致) */
51
+ interface TrackPayload {
52
+ eventName: string;
53
+ params?: Record<string, unknown>;
54
+ type?: string;
55
+ timestamp?: number;
56
+ context: {
57
+ pageVersionId: string;
58
+ runtimeVersion: string;
59
+ userId?: string;
60
+ deviceId?: string;
61
+ channel?: string;
62
+ appId: string;
63
+ env: string;
64
+ };
65
+ }
66
+ /**
67
+ * User API 适配器
68
+ * 只约定入参/出参,不绑定具体 HTTP 库;由 Runtime 注入实现,单测可注入 Mock
69
+ */
70
+ interface UserApiAdapter {
71
+ /** 页面解析 GET /page/resolve */
72
+ resolvePage(params: ResolvePageParams): Promise<PageResolveResponse>;
73
+ /** 动作执行 POST /actions/execute */
74
+ executeAction<T = unknown>(params: ExecuteActionParams): Promise<ActionResult<T>>;
75
+ /** 数据查询 POST /data/query */
76
+ executeQuery(params: ExecuteQueryParams): Promise<ExecuteQueryResult>;
77
+ /** 埋点 POST /track(可异步、不阻塞) */
78
+ track(payload: TrackPayload): void;
79
+ }
80
+
5
81
  /**
6
82
  * 运行时初始化选项
7
83
  */
@@ -42,6 +118,8 @@ interface RuntimeOptions {
42
118
  onMetric?: (metric: PerformanceMetric) => void;
43
119
  /** 加载完成回调 */
44
120
  onLoad?: (data: PageResolveResult) => void;
121
+ /** User API 适配器(必填,由宿主注入,core 不绑定具体 HTTP/鉴权实现) */
122
+ userApiAdapter: UserApiAdapter;
45
123
  }
46
124
  /**
47
125
  * 运行时阶段
@@ -841,6 +919,44 @@ declare class ActionBridge {
841
919
  private log;
842
920
  }
843
921
 
922
+ /**
923
+ * 测试用 User API 适配器 Mock
924
+ * 单测中注入,不再 mock global.fetch
925
+ */
926
+
927
+ interface MockUserApiAdapterOptions {
928
+ /** resolvePage 返回值(可覆盖) */
929
+ resolvePageResponse?: PageResolveResponse;
930
+ /** executeAction 返回值(可覆盖) */
931
+ executeActionResponse?: ActionResult<unknown>;
932
+ /** executeQuery 返回值(可覆盖) */
933
+ executeQueryResponse?: ExecuteQueryResult;
934
+ /** 是否在 resolve 时抛错 */
935
+ resolvePageThrow?: Error;
936
+ /** 是否在 executeAction 时抛错 */
937
+ executeActionThrow?: Error;
938
+ /** 是否在 executeQuery 时抛错 */
939
+ executeQueryThrow?: Error;
940
+ }
941
+ /**
942
+ * 测试用 Mock 实现
943
+ * 记录调用次数与参数,返回可配置的默认值或自定义值
944
+ */
945
+ declare class MockUserApiAdapter implements UserApiAdapter {
946
+ private readonly options;
947
+ readonly calls: {
948
+ resolvePage: ResolvePageParams[];
949
+ executeAction: ExecuteActionParams[];
950
+ executeQuery: ExecuteQueryParams[];
951
+ track: TrackPayload[];
952
+ };
953
+ constructor(options?: MockUserApiAdapterOptions);
954
+ resolvePage(params: ResolvePageParams): Promise<PageResolveResponse>;
955
+ executeAction<T = unknown>(params: ExecuteActionParams): Promise<ActionResult<T>>;
956
+ executeQuery(params: ExecuteQueryParams): Promise<ExecuteQueryResult>;
957
+ track(payload: TrackPayload): void;
958
+ }
959
+
844
960
  /**
845
961
  * Host API 实现
846
962
  * 提供给组件的安全 API,完全对齐 @djvlc/contracts-types 中的 HostApi 定义
@@ -871,12 +987,8 @@ interface RuntimeContext {
871
987
  * Host API 配置
872
988
  */
873
989
  interface HostAPIOptions {
874
- /** API 基础 URL */
875
- apiBaseUrl: string;
876
- /** 认证 Token */
877
- authToken?: string;
878
- /** 请求头 */
879
- headers?: Record<string, string>;
990
+ /** User API 适配器(必填,仅通过适配器调用 User API) */
991
+ userApiAdapter: UserApiAdapter;
880
992
  /** 状态管理器 */
881
993
  stateManager: StateManager;
882
994
  /** 事件总线 */
@@ -919,7 +1031,6 @@ declare class HostAPIImpl implements HostApi {
919
1031
  getState<T = unknown>(path: string): T | undefined;
920
1032
  setState(path: string, value: unknown): void;
921
1033
  getContext(): ComponentContext;
922
- private buildHeaders;
923
1034
  private generateIdempotencyKey;
924
1035
  private simpleHash;
925
1036
  private fallbackCopy;
@@ -965,6 +1076,8 @@ declare class DjvlcRuntime {
965
1076
  private renderer;
966
1077
  private hostApi;
967
1078
  private lifecycleManager;
1079
+ /** User API 适配器,统一认证/请求头,供 PageLoader 与 HostAPIImpl 使用 */
1080
+ private userApiAdapter;
968
1081
  private logger;
969
1082
  constructor(options: RuntimeOptions);
970
1083
  /**
@@ -1141,17 +1254,17 @@ declare class RenderError extends DjvlcRuntimeError {
1141
1254
  * 页面加载器配置
1142
1255
  */
1143
1256
  interface PageLoaderOptions {
1144
- /** API 基础 URL */
1257
+ /** API 基础 URL(用于 preconnect、CDN 请求头) */
1145
1258
  apiBaseUrl: string;
1259
+ /** User API 适配器(必填,仅通过适配器调用 User API) */
1260
+ userApiAdapter: UserApiAdapter;
1146
1261
  /** 环境 */
1147
1262
  env?: 'preview' | 'staging' | 'prod';
1148
1263
  /** 渠道 */
1149
1264
  channel?: string;
1150
- /** 认证 Token */
1151
- authToken?: string;
1152
1265
  /** 预览 Token */
1153
1266
  previewToken?: string;
1154
- /** 请求头 */
1267
+ /** 请求头(CDN 请求用) */
1155
1268
  headers?: Record<string, string>;
1156
1269
  /** 缓存配置 */
1157
1270
  cache?: {
@@ -1180,10 +1293,9 @@ declare class PageLoader {
1180
1293
  deviceId?: string;
1181
1294
  }): Promise<PageResolveResult>;
1182
1295
  /**
1183
- * 调用 Resolve API
1184
- * 严格按照 contracts 中的 PageResolveRequest 和 PageResolveResponse
1296
+ * 通过 User API 适配器调用 resolve
1185
1297
  */
1186
- private callResolveApi;
1298
+ private callResolveViaAdapter;
1187
1299
  /**
1188
1300
  * 从 CDN 加载 snapshot 和 manifest
1189
1301
  */
@@ -1222,29 +1334,34 @@ declare class PageLoader {
1222
1334
 
1223
1335
  /**
1224
1336
  * 组件加载器
1225
- * 负责从 CDN 加载组件资源
1337
+ * 负责从 CDN 动态加载 Web Components,支持 SRI 完整性校验、组件阻断、并行加载与降级处理。
1338
+ * @see SKILL: runtime-component-loading
1226
1339
  */
1227
1340
 
1228
1341
  /**
1229
1342
  * 组件加载器配置
1230
1343
  */
1231
1344
  interface ComponentLoaderOptions {
1232
- /** CDN 基础 URL */
1345
+ /** CDN 基础 URL,用于解析相对 entry 路径 */
1233
1346
  cdnBaseUrl: string;
1234
- /** 是否启用 SRI 校验 */
1347
+ /** 是否启用 SRI 完整性校验,默认 true */
1235
1348
  enableSRI?: boolean;
1236
- /** 被阻断的组件列表 */
1349
+ /** 被阻断的组件列表(如 `['comp@1.0.0']` 或仅组件名 `['comp']`),用于紧急止血 */
1237
1350
  blockedComponents?: string[];
1238
- /** 并行加载数量 */
1351
+ /** 并行加载数量,默认 4 */
1239
1352
  concurrency?: number;
1240
- /** 超时时间(毫秒) */
1353
+ /** 单次请求超时(毫秒),默认 30000 */
1241
1354
  timeout?: number;
1355
+ /** 请求 CDN 时附加的请求头(如认证等) */
1356
+ headers?: Record<string, string>;
1357
+ /** 自定义拉取脚本的实现,不传则使用全局 fetch;测试或自定义 CDN 客户端时注入 */
1358
+ fetchComponentScript?: (url: string, init?: RequestInit) => Promise<Response>;
1242
1359
  /** 日志器 */
1243
1360
  logger?: Logger;
1244
1361
  }
1245
1362
  /**
1246
1363
  * 组件加载器
1247
- * 负责加载 Web Components
1364
+ * 负责从 CDN 加载 Web Components,支持缓存、阻断、SRI 校验与并行加载。
1248
1365
  */
1249
1366
  declare class ComponentLoader {
1250
1367
  private options;
@@ -1253,37 +1370,47 @@ declare class ComponentLoader {
1253
1370
  private blockedSet;
1254
1371
  constructor(options: ComponentLoaderOptions);
1255
1372
  /**
1256
- * 加载单个组件
1373
+ * 加载单个组件(按 name@version 去重,同一组件并发请求复用同一 Promise)
1374
+ * @param item - 清单项(name、version、entry、integrity 等)
1375
+ * @returns 已加载的组件信息(含 Component 构造函数)
1376
+ * @throws ComponentBlockedError 当组件在阻断列表中
1377
+ * @throws ComponentLoadError 当拉取或执行失败
1378
+ * @throws IntegrityError 当 SRI 校验失败
1257
1379
  */
1258
1380
  load(item: ManifestItem): Promise<LoadedComponent>;
1259
1381
  /**
1260
- * 加载 Manifest 中的所有组件
1382
+ * manifest 批量加载组件,按 concurrency 分批并行;关键组件失败会抛出,非关键组件记录失败。
1383
+ * @param manifest - 页面清单(含 components 数组)
1384
+ * @returns 以 name@version 为 key 的加载结果 Map(status: loaded | failed | blocked)
1261
1385
  */
1262
1386
  loadAll(manifest: PageManifest): Promise<Map<string, ComponentLoadResult>>;
1263
1387
  /**
1264
- * 预加载组件
1388
+ * 预加载组件:向 document.head 插入 &lt;link rel="preload" as="script"&gt;,仅作提示,不阻塞 load。
1389
+ * @param items - 需要预加载的清单项
1265
1390
  */
1266
1391
  preload(items: ManifestItem[]): void;
1267
1392
  /**
1268
- * 检查组件是否已加载
1393
+ * 检查指定 name@version 的组件是否已加载并缓存
1269
1394
  */
1270
1395
  isLoaded(name: string, version: string): boolean;
1271
1396
  /**
1272
- * 获取已加载的组件
1397
+ * 获取已加载的组件信息,未加载则返回 undefined
1273
1398
  */
1274
1399
  get(name: string, version: string): LoadedComponent | undefined;
1275
1400
  /**
1276
- * 检查组件是否被阻断
1401
+ * 检查组件是否在阻断列表中(支持 `name@version` 或仅 `name`)
1277
1402
  */
1278
1403
  isBlocked(name: string, version: string): boolean;
1279
1404
  /**
1280
- * 更新阻断列表
1405
+ * 更新阻断列表(会完全替换当前列表)
1281
1406
  */
1282
1407
  updateBlockedList(blocked: string[]): void;
1283
1408
  private loadComponent;
1284
1409
  private fetchWithTimeout;
1285
1410
  private validateIntegrity;
1286
1411
  private executeScript;
1412
+ /** kebab-case -> PascalCase,用于匹配组件命名导出 */
1413
+ private kebabToPascal;
1287
1414
  private resolveUrl;
1288
1415
  private getComponentKey;
1289
1416
  private log;
@@ -2040,4 +2167,4 @@ declare class LifecycleManager {
2040
2167
  */
2041
2168
  declare const RUNTIME_VERSION = "1.0.0";
2042
2169
 
2043
- 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 };
2170
+ 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, type ExecuteActionParams, type ExecuteQueryParams, type ExecuteQueryResult, ExpressionEngine, type ExpressionEngineOptions, ExpressionError, HostAPIImpl, type HostAPIOptions, IntegrityError, Lexer, type LifecycleEventType, LifecycleManager, type LifecycleManagerOptions, type LoadedComponent, type LogLevel, type Logger, type MetricType, MockUserApiAdapter, type MockUserApiAdapterOptions, PageLoadError, PageLoader, type PageLoaderOptions, type PageResolveResult, Parser, type PerformanceMetric, QueryError, RUNTIME_VERSION, RenderError, type Renderer, type ResolvePageParams, type RuntimeContext, 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 TrackPayload, type Unsubscribe, type UserApiAdapter, builtinFunctions, createFallbackElement, createRuntime, registerFallbackComponents };