soma-core 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/README.md +42 -0
  2. package/app/SomaPageClient.tsx +39 -2
  3. package/app/[...path]/page.tsx +7 -6
  4. package/app/api/[...path]/route.ts +3 -3
  5. package/app/api/soma-registry/[...path]/route.ts +67 -0
  6. package/app/components/SomaHostPage.tsx +3 -8
  7. package/app/components/SomaPageClientBoundary.tsx +13 -0
  8. package/app/layout.tsx +6 -1
  9. package/app/page.tsx +3 -3
  10. package/app/soma/manifest/route.ts +1 -0
  11. package/app/soma/page-object/route.ts +1 -1
  12. package/core/build/compiler/full-manifest-builder.ts +6 -1
  13. package/core/build/compiler/rollup-plugin-soma.ts +6 -0
  14. package/core/build/compiler/soma-manifest-webpack-plugin.js +4 -0
  15. package/core/build/compiler/soma-precompile.js +3507 -2387
  16. package/core/build/compiler/soma-precompile.ts +31 -2
  17. package/core/build/compiler/soma-rspack-yaml-loader.js +3 -0
  18. package/core/build/compiler/soma-webpack-loader.js +7 -1
  19. package/core/build/compiler/vite-plugin-soma.ts +11 -1
  20. package/core/build/component/package-loader.ts +135 -0
  21. package/core/build/eject/apply-eject.ts +133 -0
  22. package/core/build/eject/plan-eject.ts +139 -0
  23. package/core/client/adaptive/flush-scheduler.ts +96 -0
  24. package/core/client/adaptive/hysteresis.ts +56 -0
  25. package/core/client/adaptive/index.ts +23 -0
  26. package/core/client/adaptive/observer-pool.ts +90 -0
  27. package/core/client/base/SomaPageRootBase.ts +50 -0
  28. package/core/client/compiler/adaptive-compile.ts +136 -0
  29. package/core/client/compiler/manifest-compiler.ts +155 -27
  30. package/core/client/component/ai-catalog.ts +262 -0
  31. package/core/client/component/component-definition.types.ts +65 -0
  32. package/core/client/component/component-expander.ts +119 -0
  33. package/core/client/component/component-manager.ts +933 -0
  34. package/core/client/component/component-pack.ts +271 -0
  35. package/core/client/component/component-registry.ts +71 -0
  36. package/core/client/component/pack-styles.ts +94 -0
  37. package/core/client/component/scoped-adapter.ts +85 -0
  38. package/core/client/component/verify-pack.ts +247 -0
  39. package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
  40. package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
  41. package/core/client/container/SomaContainerAdapter.ts +186 -180
  42. package/core/client/data/data-object-resolver.ts +417 -0
  43. package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
  44. package/core/client/diagnostics/boot-timings.ts +152 -0
  45. package/core/client/engine/asset-resolver.ts +12 -2
  46. package/core/client/engine/dom-renderer.ts +235 -16
  47. package/core/client/engine/emit-event.ts +21 -0
  48. package/core/client/engine/event-engine.ts +34 -4
  49. package/core/client/engine/on-change-engine.ts +1 -1
  50. package/core/client/engine/property-watcher.ts +1 -1
  51. package/core/client/engine/reactive-proxy.ts +3 -3
  52. package/core/client/engine/render-engine.ts +43 -4
  53. package/core/client/engine/runtime-builder.ts +23 -0
  54. package/core/client/engine/soma-bootstrap.ts +389 -5
  55. package/core/client/engine/style-binder.ts +47 -1
  56. package/core/client/host-bridge/empty-host/data/README.md +17 -0
  57. package/core/client/infra/http/SomaHttpService.ts +5 -0
  58. package/core/client/observability/error-reporter.ts +173 -0
  59. package/core/client/parser/adaptive-parser.ts +264 -0
  60. package/core/client/parser/data-config-parser.ts +396 -0
  61. package/core/client/parser/pipeline-parser.ts +15 -3
  62. package/core/client/parser/style-config-parser.ts +65 -2
  63. package/core/client/parser/ui-config-parser.ts +52 -3
  64. package/core/client/pipes/pipe-registry.ts +249 -0
  65. package/core/client/quality/SomaQualityRegistry.ts +3 -0
  66. package/core/client/registry/declaration-renderer.ts +159 -0
  67. package/core/client/registry/index.ts +28 -0
  68. package/core/client/registry/page-lifecycle.ts +171 -0
  69. package/core/client/registry/remote-page.ts +139 -0
  70. package/core/client/runtime/SomaRuntimeServices.ts +24 -0
  71. package/core/client/scheduler/SomaScheduler.ts +29 -1
  72. package/core/client/security/attribute-presence.ts +70 -0
  73. package/core/client/security/html-sanitizer.ts +113 -0
  74. package/core/client/security/url-guard.ts +0 -0
  75. package/core/client/theme/theme-engine.ts +936 -0
  76. package/core/client/typography/typography-base.ts +34 -0
  77. package/core/client/validation/contract-validator.ts +48 -5
  78. package/core/index.ts +190 -2
  79. package/core/server/api/IService.ts +18 -6
  80. package/core/server/api/load-host-service.ts +3 -3
  81. package/core/server/host/content-security-policy.ts +190 -0
  82. package/core/server/host/csp-nonce-metadata.ts +75 -0
  83. package/core/server/host/resolve-host-request-hook.ts +5 -0
  84. package/core/server/host/server-page-assets.ts +10 -0
  85. package/core/server/index.ts +30 -1
  86. package/core/server/infra/cache/ServerFileCache.ts +1 -1
  87. package/core/server/mock/node/cli.mjs +2 -2
  88. package/core/server/registry/api-registry-store.ts +518 -0
  89. package/core/server/registry/conditional-object-store.ts +29 -0
  90. package/core/server/registry/declaration-precompiler.ts +129 -0
  91. package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
  92. package/core/server/registry/index.ts +142 -0
  93. package/core/server/registry/page-registry-repository.ts +373 -0
  94. package/core/server/registry/page-registry-store.ts +903 -0
  95. package/core/server/registry/registry-error.ts +17 -0
  96. package/core/server/registry/registry-file-lock.ts +173 -0
  97. package/core/server/registry/registry-response.ts +71 -0
  98. package/core/server/registry/registry-subjects.ts +194 -0
  99. package/core/server/registry/registry-write-pipeline.ts +240 -0
  100. package/core/server/registry/request-body-limit.ts +99 -0
  101. package/core/server/registry/services/apis/Get.ts +25 -0
  102. package/core/server/registry/services/apis/List.ts +41 -0
  103. package/core/server/registry/services/apis/Register.ts +39 -0
  104. package/core/server/registry/services/apis/Unregister.ts +24 -0
  105. package/core/server/registry/services/index.ts +40 -0
  106. package/core/server/registry/services/pages/Get.ts +86 -0
  107. package/core/server/registry/services/pages/List.ts +53 -0
  108. package/core/server/registry/services/pages/Register.ts +49 -0
  109. package/core/server/registry/services/pages/Rollback.ts +36 -0
  110. package/core/server/registry/services/pages/Unregister.ts +23 -0
  111. package/core/server/registry/services/service-utils.ts +21 -0
  112. package/core/server/registry/validation/compile-gate.ts +71 -0
  113. package/core/server/registry/validation/component-existence.ts +55 -0
  114. package/core/server/registry/validation/credential-ban.ts +86 -0
  115. package/core/server/registry/validation/cross-check.ts +77 -0
  116. package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
  117. package/core/shared/types/adaptive.types.ts +109 -0
  118. package/core/shared/types/component-contract.types.ts +110 -0
  119. package/core/shared/types/data-config.types.ts +105 -0
  120. package/core/shared/types/domain.types.ts +6 -0
  121. package/core/shared/types/engine.types.ts +41 -35
  122. package/core/shared/types/manifest.types.ts +55 -32
  123. package/core/shared/types/scope-manifest.types.ts +96 -0
  124. package/core/shared/types/style-config.types.ts +9 -0
  125. package/core/shared/types/ui-config.types.ts +118 -98
  126. package/dist/cli/soma.js +20 -5
  127. package/dist/core/index.cjs +5388 -888
  128. package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -0
  129. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +1 -0
  130. package/dist/types/build/compiler/soma-precompile.d.ts +9 -0
  131. package/dist/types/build/compiler/vite-plugin-soma.d.ts +3 -0
  132. package/dist/types/build/component/package-loader.d.ts +9 -0
  133. package/dist/types/build/eject/apply-eject.d.ts +22 -0
  134. package/dist/types/build/eject/plan-eject.d.ts +32 -0
  135. package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
  136. package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
  137. package/dist/types/client/adaptive/index.d.ts +5 -0
  138. package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
  139. package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
  140. package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
  141. package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
  142. package/dist/types/client/component/ai-catalog.d.ts +73 -0
  143. package/dist/types/client/component/component-definition.types.d.ts +54 -0
  144. package/dist/types/client/component/component-expander.d.ts +25 -0
  145. package/dist/types/client/component/component-manager.d.ts +160 -0
  146. package/dist/types/client/component/component-pack.d.ts +90 -0
  147. package/dist/types/client/component/component-registry.d.ts +12 -0
  148. package/dist/types/client/component/pack-styles.d.ts +22 -0
  149. package/dist/types/client/component/scoped-adapter.d.ts +18 -0
  150. package/dist/types/client/component/verify-pack.d.ts +14 -0
  151. package/dist/types/client/data/data-object-resolver.d.ts +71 -0
  152. package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
  153. package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
  154. package/dist/types/client/engine/asset-resolver.d.ts +7 -1
  155. package/dist/types/client/engine/dom-renderer.d.ts +49 -1
  156. package/dist/types/client/engine/emit-event.d.ts +6 -0
  157. package/dist/types/client/engine/event-engine.d.ts +6 -0
  158. package/dist/types/client/engine/render-engine.d.ts +26 -1
  159. package/dist/types/client/engine/runtime-builder.d.ts +7 -0
  160. package/dist/types/client/engine/soma-bootstrap.d.ts +122 -0
  161. package/dist/types/client/infra/http/SomaHttpService.d.ts +2 -0
  162. package/dist/types/client/observability/error-reporter.d.ts +51 -0
  163. package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
  164. package/dist/types/client/parser/data-config-parser.d.ts +34 -0
  165. package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
  166. package/dist/types/client/parser/style-config-parser.d.ts +5 -3
  167. package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
  168. package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
  169. package/dist/types/client/registry/declaration-renderer.d.ts +51 -0
  170. package/dist/types/client/registry/index.d.ts +10 -0
  171. package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
  172. package/dist/types/client/registry/remote-page.d.ts +52 -0
  173. package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
  174. package/dist/types/client/scheduler/SomaScheduler.d.ts +8 -0
  175. package/dist/types/client/security/attribute-presence.d.ts +7 -0
  176. package/dist/types/client/security/html-sanitizer.d.ts +37 -0
  177. package/dist/types/client/security/url-guard.d.ts +11 -0
  178. package/dist/types/client/theme/theme-engine.d.ts +49 -0
  179. package/dist/types/client/typography/typography-base.d.ts +15 -0
  180. package/dist/types/client/validation/contract-validator.d.ts +2 -0
  181. package/dist/types/index.d.ts +36 -2
  182. package/dist/types/server/api/IService.d.ts +18 -6
  183. package/dist/types/server/host/content-security-policy.d.ts +53 -0
  184. package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
  185. package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
  186. package/dist/types/server/host/server-page-assets.d.ts +5 -0
  187. package/dist/types/server/index.d.ts +4 -1
  188. package/dist/types/server/registry/api-registry-store.d.ts +141 -0
  189. package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
  190. package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
  191. package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
  192. package/dist/types/server/registry/index.d.ts +29 -0
  193. package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
  194. package/dist/types/server/registry/page-registry-store.d.ts +259 -0
  195. package/dist/types/server/registry/registry-error.d.ts +12 -0
  196. package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
  197. package/dist/types/server/registry/registry-response.d.ts +18 -0
  198. package/dist/types/server/registry/registry-subjects.d.ts +59 -0
  199. package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
  200. package/dist/types/server/registry/request-body-limit.d.ts +31 -0
  201. package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
  202. package/dist/types/server/registry/services/apis/List.d.ts +10 -0
  203. package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
  204. package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
  205. package/dist/types/server/registry/services/index.d.ts +3 -0
  206. package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
  207. package/dist/types/server/registry/services/pages/List.d.ts +13 -0
  208. package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
  209. package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
  210. package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
  211. package/dist/types/server/registry/services/service-utils.d.ts +10 -0
  212. package/dist/types/server/registry/validation/compile-gate.d.ts +10 -0
  213. package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
  214. package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
  215. package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
  216. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
  217. package/dist/types/shared/types/adaptive.types.d.ts +88 -0
  218. package/dist/types/shared/types/component-contract.types.d.ts +68 -0
  219. package/dist/types/shared/types/data-config.types.d.ts +81 -0
  220. package/dist/types/shared/types/domain.types.d.ts +6 -0
  221. package/dist/types/shared/types/engine.types.d.ts +6 -0
  222. package/dist/types/shared/types/manifest.types.d.ts +23 -0
  223. package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
  224. package/dist/types/shared/types/style-config.types.d.ts +8 -0
  225. package/dist/types/shared/types/ui-config.types.d.ts +20 -0
  226. package/global.d.ts +12 -0
  227. package/next.config.js +76 -1
  228. package/package.json +32 -11
  229. package/proxy.ts +62 -0
  230. package/core/client/engine/theme-variables.ts +0 -37
  231. package/dist/tsconfig.types.tsbuildinfo +0 -1
  232. package/dist/types/client/engine/theme-variables.d.ts +0 -3
  233. package/middleware.ts +0 -33
@@ -0,0 +1,139 @@
1
+ // 注册页面的运行时渲染:按名从注册中心拉取页面声明,复用声明渲染通道挂载。
2
+ //
3
+ // 「声明即数据」的消费端:
4
+ // - 声明存服务端(/api/soma-registry/pages/*,带版本可回滚),注册即生效;
5
+ // - 拉取成功 → 走 createGeneratedPagePreview 通道渲染(空 Domain + initProps);
6
+ // - 拉取失败(404 / 网络异常 / 响应异常)抛出带上下文的明确错误,由调用方兜底。
7
+ //
8
+ // 注意:本文件是浏览器入口,不得引入 Node 内建或服务端模块,
9
+ // 因此声明记录的类型在此独立声明(与 PageRegistryStore 的 PageVersionRecord 同构)。
10
+
11
+ import type { SomaComponentDefinition } from '../component/component-definition.types';
12
+ import type { SomaAdaptiveVocabulary } from '../../shared/types/adaptive.types';
13
+ import {
14
+ createGeneratedPagePreview,
15
+ type GeneratedPageDeclarations,
16
+ type PrecompiledDeclarationAssets,
17
+ type PreviewObservabilityOptions,
18
+ } from './declaration-renderer';
19
+
20
+ /** 服务端返回的版本记录(与注册中心 PageVersionRecord 同构) */
21
+ export interface PublishedPageRecord {
22
+ name: string;
23
+ version: number;
24
+ declaration: GeneratedPageDeclarations & { notes?: string };
25
+ status: 'published' | 'draft';
26
+ createdAt: number;
27
+ /**
28
+ * 注册期预编译产物(P-a)。服务端已做过版本闸:发下来就是能用的,
29
+ * 没发下来就说明版本对不上或压根没编译过——客户端照常运行时解析。
30
+ */
31
+ compiled?: PrecompiledDeclarationAssets;
32
+ }
33
+
34
+ export interface RenderPublishedPageOptions {
35
+ /** 指定版本号;缺省拉最新 published 版本 */
36
+ version?: number;
37
+ /** 可注入 fetch(测试 / 自定义传输),默认 globalThis.fetch */
38
+ http?: typeof globalThis.fetch;
39
+ /** 拉取端点,默认 /api/soma-registry/pages/get */
40
+ endpoint?: string;
41
+ /**
42
+ * 非敏感请求头(例如 Host 的页面来源标识)。用于让服务端能力网关识别调用页面;
43
+ * 禁止放 Authorization、签名密钥或任何长期凭据。
44
+ */
45
+ requestHeaders?: Readonly<Record<string, string>>;
46
+ /**
47
+ * 错误采集(默认关闭;给了就开启,`observability: {}` 即为一行开启)。
48
+ * 页面身份由本函数按拉取结果自动盖戳 `{ name, version, source: 'registry' }`——
49
+ * 声明可以随时注册新版本,只有这里知道渲染的究竟是哪一版。
50
+ */
51
+ observability?: PreviewObservabilityOptions;
52
+ /** 页面声明含 adaptive.ladder 时由 Host 注入的组件词汇。 */
53
+ adaptiveVocabulary?: SomaAdaptiveVocabulary;
54
+ }
55
+
56
+ export interface PublishedPageHandle {
57
+ /** 实际渲染的页面名 */
58
+ name: string;
59
+ /** 实际渲染的版本号(缺省拉取时即最新 published 版本) */
60
+ version: number;
61
+ /** 卸载页面并清空挂载点 */
62
+ destroy(): Promise<void>;
63
+ }
64
+
65
+ /**
66
+ * 拉取注册页面声明并渲染到 mountId 对应的挂载点。
67
+ * 任何拉取 / 解析失败都会抛出带页面名上下文的 Error(调用方 catch 后兜底展示)。
68
+ */
69
+ export async function renderPublishedPage(
70
+ mountId: string,
71
+ name: string,
72
+ components: Record<string, SomaComponentDefinition>,
73
+ options: RenderPublishedPageOptions = {},
74
+ ): Promise<PublishedPageHandle> {
75
+ const fetcher = options.http ?? globalThis.fetch;
76
+ if (typeof fetcher !== 'function') {
77
+ throw new Error('[soma-core][注册页] 当前环境没有可用的 fetch 实现');
78
+ }
79
+
80
+ const query = new URLSearchParams({ name });
81
+ if (options.version !== undefined) {
82
+ query.set('version', String(options.version));
83
+ }
84
+ const url = `${options.endpoint ?? '/api/soma-registry/pages/get'}?${query.toString()}`;
85
+
86
+ let response: Response;
87
+ try {
88
+ response = await fetcher(url, {
89
+ credentials: 'include',
90
+ headers: { Accept: 'application/json', ...options.requestHeaders },
91
+ });
92
+ } catch (error) {
93
+ throw new Error(`[soma-core][注册页] 拉取页面声明失败:${name}(网络异常)`, {
94
+ cause: error,
95
+ });
96
+ }
97
+
98
+ const body = (await response.json().catch(() => null)) as {
99
+ success?: unknown;
100
+ message?: unknown;
101
+ data?: unknown;
102
+ } | null;
103
+ if (!response.ok || !body || body.success !== true || !isPublishedPageRecord(body.data)) {
104
+ const reason = body && typeof body.message === 'string' && body.message
105
+ ? body.message
106
+ : `HTTP ${response.status}`;
107
+ throw new Error(`[soma-core][注册页] 页面声明不可用:${name}(${reason})`);
108
+ }
109
+
110
+ const record = body.data;
111
+ const preview = createGeneratedPagePreview(mountId, components, {
112
+ observability: options.observability,
113
+ adaptiveVocabulary: options.adaptiveVocabulary,
114
+ });
115
+ // 版本号只有拉取回来才知道,页面身份必须在这一刻盖戳,否则错误再也归不了因
116
+ await preview.render(
117
+ record.declaration,
118
+ { name: record.name, version: record.version, source: 'registry' },
119
+ record.compiled,
120
+ );
121
+ return {
122
+ name: record.name,
123
+ version: record.version,
124
+ destroy: () => preview.destroy(),
125
+ };
126
+ }
127
+
128
+ function isPublishedPageRecord(value: unknown): value is PublishedPageRecord {
129
+ if (!value || typeof value !== 'object') {
130
+ return false;
131
+ }
132
+ const record = value as Partial<PublishedPageRecord>;
133
+ return typeof record.name === 'string'
134
+ && typeof record.version === 'number'
135
+ && !!record.declaration
136
+ && typeof record.declaration === 'object'
137
+ && typeof record.declaration.html === 'string'
138
+ && typeof record.declaration.uiYaml === 'string';
139
+ }
@@ -32,6 +32,28 @@ export interface SomaRuntimeServices {
32
32
  permission: SomaPermission;
33
33
  i18n: DefaultI18n;
34
34
  realtime: IRealtimeService;
35
+ /**
36
+ * 界面中途提交:把此刻改过的页面状态立刻上屏,**不结束**当前事件事务。
37
+ *
38
+ * 存在的理由:一次事件 = 一次原子提交是刻意的设计(避免中间态闪烁、出错整体回滚),
39
+ * 代价是**长任务里的中途进展播不出去**——实测 AI 生成 P50 约 40 秒,期间 Domain
40
+ * 改多少次状态,界面一次都不动,连"生成中"都看不到。
41
+ *
42
+ * 它是**自愿**的:不调用,行为与过去逐字节一致;调用了,就等于声明
43
+ * "已上屏的这部分不再享有失败回滚"。这个取舍只能由业务自己做。
44
+ * 引擎未接线时(纯 Node 单测、无渲染宿主)为空操作,调用方不必判空。
45
+ */
46
+ ui: SomaUiService;
47
+ }
48
+
49
+ /** 界面中途提交服务(见 SomaRuntimeServices.ui) */
50
+ export interface SomaUiService {
51
+ publish(): Promise<void>;
52
+ }
53
+
54
+ /** 缺省实现:引擎未接线时的空操作(纯 Node 单测、无渲染宿主) */
55
+ function createNoopUiService(): SomaUiService {
56
+ return { publish: async () => undefined };
35
57
  }
36
58
 
37
59
  export function createDefaultRuntimeServices(options?: {
@@ -100,5 +122,7 @@ export function createDefaultRuntimeServices(options?: {
100
122
  wsBaseUrl: config.get<string>('realtime.wsBaseUrl'),
101
123
  tokenProvider: () => auth.getToken(),
102
124
  }),
125
+ // 渲染引擎就绪后由 bootstrap 换成真实实现;在那之前(以及无渲染宿主时)是空操作
126
+ ui: createNoopUiService(),
103
127
  };
104
128
  }
@@ -25,6 +25,7 @@ export class SomaScheduler {
25
25
  private diagnostics: SomaDiagnostics | null = null;
26
26
  private flushQueued = false;
27
27
  private flushing = false;
28
+ private settledWaiters: Array<() => void> = [];
28
29
 
29
30
  constructor(
30
31
  options: SomaSchedulerOptions & {
@@ -114,12 +115,39 @@ export class SomaScheduler {
114
115
  this.flushing = false;
115
116
  if (this.autoFlush && this.hasPendingTasks()) {
116
117
  this.queueFlush();
118
+ } else {
119
+ this.resolveSettledWaiters();
117
120
  }
118
121
  }
119
122
  }
120
123
 
124
+ /** Number of tasks waiting to be executed (excluding the currently running task). */
125
+ pendingCount(): number {
126
+ return PRIORITY_ORDER.reduce(
127
+ (count, priority) => count + (this.queues.get(priority)?.length ?? 0),
128
+ 0,
129
+ );
130
+ }
131
+
132
+ /** True only when no task is queued or currently being flushed. */
133
+ isSettled(): boolean {
134
+ return !this.flushing && !this.flushQueued && this.pendingCount() === 0;
135
+ }
136
+
137
+ /** Wait until the scheduler has finished its current batch. */
138
+ whenSettled(): Promise<void> {
139
+ if (this.isSettled()) return Promise.resolve();
140
+ return new Promise((resolve) => this.settledWaiters.push(resolve));
141
+ }
142
+
121
143
  private hasPendingTasks(): boolean {
122
- return PRIORITY_ORDER.some((priority) => (this.queues.get(priority)?.length ?? 0) > 0);
144
+ return this.pendingCount() > 0;
145
+ }
146
+
147
+ private resolveSettledWaiters(): void {
148
+ if (!this.isSettled()) return;
149
+ const waiters = this.settledWaiters.splice(0);
150
+ for (const resolve of waiters) resolve();
123
151
  }
124
152
 
125
153
  private queueFlush(): void {
@@ -0,0 +1,70 @@
1
+ // 属性「存在性」语义(B4)。
2
+ //
3
+ // 问题:声明绑定是静态的——`'node@aria-expanded': 'row.ariaExpanded'` 这一行写下去,
4
+ // 每一行数据都会写出这个属性。但有些属性的正确用法是「要么带正确值,要么根本不出现」:
5
+ // - ARIA 状态属性:树的叶子节点不该有 aria-expanded(有值就等于宣称"我能展开"),
6
+ // 空串 aria-expanded="" 是无效值,不同读屏器解读不一致;
7
+ // - HTML 布尔属性:readonly="" 等同 readonly=true,想表达"不只读"必须让属性消失。
8
+ //
9
+ // 规则:上述两类属性拿到空串时删除属性,而不是写入空串。
10
+ // 反过来,`alt=""`(装饰图)、`value=""`、`placeholder=""` 是有意义的空值,一律照写。
11
+ // 于是控制器里 `ariaExpanded: isLeaf ? '' : String(isOpen)` 就能表达"这行不要这个属性"。
12
+
13
+ /** HTML 布尔属性:属性一旦存在即为真,值无关紧要 */
14
+ const BOOLEAN_ATTRS = new Set([
15
+ 'disabled',
16
+ 'readonly',
17
+ 'required',
18
+ 'checked',
19
+ 'selected',
20
+ 'multiple',
21
+ 'hidden',
22
+ 'open',
23
+ 'autofocus',
24
+ 'novalidate',
25
+ 'inert',
26
+ ]);
27
+
28
+ /**
29
+ * ARIA 里"缺省即无此状态"的属性。
30
+ * 只收状态/关系类;aria-label 这类"内容型"不在此列(空 label 直接不写更合适,
31
+ * 但那属于内容缺失,交由 a11y 闸门管,不在写入层默默吞掉)。
32
+ */
33
+ const ABSENT_WHEN_EMPTY_ARIA = new Set([
34
+ 'aria-expanded',
35
+ 'aria-selected',
36
+ 'aria-checked',
37
+ 'aria-pressed',
38
+ 'aria-current',
39
+ 'aria-disabled',
40
+ 'aria-hidden',
41
+ 'aria-level',
42
+ 'aria-setsize',
43
+ 'aria-posinset',
44
+ 'aria-controls',
45
+ 'aria-describedby',
46
+ 'aria-labelledby',
47
+ 'aria-owns',
48
+ 'aria-activedescendant',
49
+ // 2026-08-17(soma-ui B10-2 DataTable 受控排序)补:aria-sort 的合法值只有
50
+ // ascending/descending/none/other,空串是无效值;且"这一列根本不可排序"与
51
+ // "这一列可排序但当前没排"是两回事——前者必须没有这个属性,后者才写 none。
52
+ 'aria-sort',
53
+ // 同理:aria-invalid 空串在部分读屏器上会被当作 true("存在即为真"的老实现),
54
+ // 表单控件在非错误态下写空串等于凭空宣告"这个字段填错了"。
55
+ 'aria-invalid',
56
+ ]);
57
+
58
+ /** 空串是否意味着「删除该属性」 */
59
+ export function isAbsentWhenEmpty(attrName: string): boolean {
60
+ const name = attrName.toLowerCase();
61
+ return BOOLEAN_ATTRS.has(name) || ABSENT_WHEN_EMPTY_ARIA.has(name);
62
+ }
63
+
64
+ /**
65
+ * 属性写入前的存在性判定。
66
+ * 返回 true = 应删除属性(而非写入空串)。
67
+ */
68
+ export function shouldRemoveAttribute(attrName: string, value: string): boolean {
69
+ return value === '' && isAbsentWhenEmpty(attrName);
70
+ }
@@ -0,0 +1,113 @@
1
+ // `@html` 绑定的净化收口(B6 安全域)。
2
+ //
3
+ // 为什么要有这一层:`somaId@html` 是引擎里唯一一条「把字符串当 DOM 解释」的通道,
4
+ // 而 innerHTML 插入的 `<script>` 虽然不执行,`<img onerror=…>` 却会立刻执行——
5
+ // 也就是说这条通道一旦喂进不可信内容(接口富文本、AI 产物、用户输入),
6
+ // DSL「没有脚本能力」的守线就被从数据侧绕过了。
7
+ //
8
+ // 早期实现把探测与降级直接写死在 Web 容器适配器里:全局有 DOMPurify 就 sanitize,
9
+ // 没有就整串转义。行为是安全的,但有三个工程问题:
10
+ // 1. 宿主想接自研/服务端同款 sanitizer 时,只能去污染 globalThis;
11
+ // 2. 三个容器适配器(Web / 小程序 / RN)各写各的降级,语义无处对账;
12
+ // 3. sanitize 配置散落——富文本页面各自调参,等于没有统一策略。
13
+ // 故抽出本模块:**一个可注入的 sanitizer 抽象 + 一份统一 profile + 最安全的默认降级**。
14
+ //
15
+ // 优先级(高 → 低),故意让"显式注入"压过"全局探测",避免第三方脚本挂个假 DOMPurify
16
+ // 就能把净化策略换掉:
17
+ // 1. 宿主经 setSomaHtmlSanitizer() 显式注入的 sanitizer;
18
+ // 2. globalThis.DOMPurify(宿主打包 dompurify 后的常规姿势),按 SOMA_PURIFY_PROFILE 调用;
19
+ // 3. 都没有 → 整串转义成文本(fail-safe:宁可显示成源码,也不产生可执行 DOM)。
20
+
21
+ /** 宿主可注入的净化器最小面:一个纯函数级的 sanitize */
22
+ export interface SomaHtmlSanitizer {
23
+ sanitize(html: string): string;
24
+ }
25
+
26
+ /** globalThis.DOMPurify 的最小结构(第二个入参为配置对象) */
27
+ interface GlobalDomPurifyLike {
28
+ sanitize(html: string, config?: Record<string, unknown>): string;
29
+ }
30
+
31
+ /**
32
+ * 统一 sanitize 策略:全站一份,不许各页面自配。
33
+ *
34
+ * 取舍说明:
35
+ * - `script` / `style` 一律剔除——声明态页面的样式走 `_style.yaml` 与主题令牌,
36
+ * 富文本里出现 `<style>` 只可能是越权改版面(模板纯度闸门在手写侧也是这条规矩);
37
+ * - `style` 属性剔除:内联样式既是 CSP `style-src` 的敌人,也是颜色纪律的漏口;
38
+ * - 表单/框架类标签剔除:富文本没有合法理由携带 `<form>` / `<iframe>` / `<base>`;
39
+ * - `ALLOW_DATA_ATTR: false`:data-soma-* 是引擎的锚点命名空间,不能被内容伪造。
40
+ * DOMPurify 默认已剔除 on* 事件属性与 `javascript:` URL,此处不重复声明,只做加严。
41
+ */
42
+ export const SOMA_PURIFY_PROFILE: Record<string, unknown> = {
43
+ USE_PROFILES: { html: true },
44
+ FORBID_TAGS: ['script', 'style', 'iframe', 'object', 'embed', 'form', 'base', 'link', 'meta'],
45
+ FORBID_ATTR: ['style', 'srcdoc', 'formaction', 'ping'],
46
+ ALLOW_DATA_ATTR: false,
47
+ };
48
+
49
+ /** HTML 文本转义表(降级路径不依赖 document,小程序 / RN 容器同样可用) */
50
+ const ESCAPE_MAP: Record<string, string> = {
51
+ '&': '&amp;',
52
+ '<': '&lt;',
53
+ '>': '&gt;',
54
+ '"': '&quot;',
55
+ "'": '&#39;',
56
+ };
57
+
58
+ let injected: SomaHtmlSanitizer | null = null;
59
+ let degradeWarned = false;
60
+
61
+ /**
62
+ * 宿主注入净化器(在 bootstrap 里调用一次)。
63
+ * 传 null 复位为「未注入」,便于测试与运行期关停。
64
+ */
65
+ export function setSomaHtmlSanitizer(sanitizer: SomaHtmlSanitizer | null): void {
66
+ injected = sanitizer;
67
+ degradeWarned = false;
68
+ }
69
+
70
+ /** 当前生效的净化器;返回 null 表示将走转义降级 */
71
+ export function resolveSomaHtmlSanitizer(): SomaHtmlSanitizer | null {
72
+ if (injected) {
73
+ return injected;
74
+ }
75
+ const purify = (globalThis as { DOMPurify?: GlobalDomPurifyLike }).DOMPurify;
76
+ if (purify && typeof purify.sanitize === 'function') {
77
+ // 包一层而不是直接返回:统一 profile 必须由我们施加,不能指望调用方记得传
78
+ return { sanitize: (html: string) => purify.sanitize(html, SOMA_PURIFY_PROFILE) };
79
+ }
80
+ return null;
81
+ }
82
+
83
+ /** 纯字符串转义,不经 DOM——降级路径与非 Web 容器共用 */
84
+ export function escapeSomaHtml(value: string): string {
85
+ return value.replace(/[&<>"']/g, (char) => ESCAPE_MAP[char]);
86
+ }
87
+
88
+ /** 净化结果:`sanitized=false` 表示走了转义降级(内容以纯文本呈现) */
89
+ export interface SomaHtmlSanitizeResult {
90
+ html: string;
91
+ sanitized: boolean;
92
+ }
93
+
94
+ /**
95
+ * `@html` 落地前的统一净化。
96
+ *
97
+ * 降级时给一次 console.warn(只警告一次,避免长列表刷屏):
98
+ * 静默降级会让富文本页面「显示成源码」而没人知道为什么,这是实测最难排查的一类困惑。
99
+ */
100
+ export function sanitizeSomaHtml(value: string): SomaHtmlSanitizeResult {
101
+ const sanitizer = resolveSomaHtmlSanitizer();
102
+ if (sanitizer) {
103
+ return { html: sanitizer.sanitize(value), sanitized: true };
104
+ }
105
+ if (!degradeWarned) {
106
+ degradeWarned = true;
107
+ console.warn(
108
+ '[SomaSecurity] @html 未接净化器,已按纯文本转义降级——'
109
+ + '需要富文本请在宿主 bootstrap 调用 setSomaHtmlSanitizer(DOMPurify) 或挂 globalThis.DOMPurify',
110
+ );
111
+ }
112
+ return { html: escapeSomaHtml(value), sanitized: false };
113
+ }