soma-core 1.3.2 → 1.3.4

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 (281) hide show
  1. package/README.md +43 -0
  2. package/app/SomaPageClient.tsx +22 -68
  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/bin/soma-lsp.cjs +1 -4
  13. package/core/build/compiler/full-manifest-builder.ts +19 -23
  14. package/core/build/compiler/html-include-expander.ts +56 -0
  15. package/core/build/compiler/page-assets.ts +26 -0
  16. package/core/build/compiler/page-declaration-compiler.ts +85 -0
  17. package/core/build/compiler/rollup-plugin-soma.ts +4 -13
  18. package/core/build/compiler/route-builder.ts +0 -2
  19. package/core/build/compiler/rspack-plugin-soma.js +1 -10
  20. package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -9
  21. package/core/build/compiler/soma-precompile.js +3581 -2387
  22. package/core/build/compiler/soma-precompile.ts +44 -2
  23. package/core/build/compiler/soma-rspack-yaml-loader.js +5 -8
  24. package/core/build/compiler/soma-webpack-loader.js +6 -15
  25. package/core/build/compiler/vite-plugin-soma.ts +9 -21
  26. package/core/build/component/pack-from-dir.ts +58 -0
  27. package/core/build/component/package-loader.ts +281 -0
  28. package/core/build/eject/apply-eject.ts +133 -0
  29. package/core/build/eject/plan-eject.ts +133 -0
  30. package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
  31. package/core/build/lsp/extra-completion.cjs +14 -8
  32. package/core/build/lsp/lsp-navigation.cjs +50 -43
  33. package/core/build/lsp/soma-declaration.cjs +85 -0
  34. package/core/build/lsp/ui-yaml-completion.cjs +98 -104
  35. package/core/build/v2/block-compile.ts +200 -0
  36. package/core/build/v2/block-loader.ts +33 -0
  37. package/core/build/v2/html-index.ts +120 -0
  38. package/core/build/v2/migrate-v1.ts +370 -0
  39. package/core/build/v2/soma-file-loader.ts +216 -0
  40. package/core/client/adaptive/flush-scheduler.ts +96 -0
  41. package/core/client/adaptive/hysteresis.ts +56 -0
  42. package/core/client/adaptive/index.ts +23 -0
  43. package/core/client/adaptive/observer-pool.ts +90 -0
  44. package/core/client/base/SomaPageRootBase.ts +50 -0
  45. package/core/client/compiler/adaptive-compile.ts +136 -0
  46. package/core/client/compiler/manifest-compiler.ts +155 -27
  47. package/core/client/component/ai-catalog.ts +277 -0
  48. package/core/client/component/component-definition.types.ts +65 -0
  49. package/core/client/component/component-expander.ts +119 -0
  50. package/core/client/component/component-manager.ts +933 -0
  51. package/core/client/component/component-pack.ts +445 -0
  52. package/core/client/component/component-registry.ts +71 -0
  53. package/core/client/component/contract-compat.ts +132 -0
  54. package/core/client/component/example-v2.ts +132 -0
  55. package/core/client/component/pack-styles.ts +94 -0
  56. package/core/client/component/scoped-adapter.ts +85 -0
  57. package/core/client/component/verify-pack.ts +247 -0
  58. package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
  59. package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
  60. package/core/client/container/SomaContainerAdapter.ts +186 -180
  61. package/core/client/data/data-object-resolver.ts +417 -0
  62. package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
  63. package/core/client/diagnostics/boot-timings.ts +152 -0
  64. package/core/client/engine/asset-resolver.ts +12 -2
  65. package/core/client/engine/dom-renderer.ts +249 -24
  66. package/core/client/engine/emit-event.ts +21 -0
  67. package/core/client/engine/event-engine.ts +34 -4
  68. package/core/client/engine/on-change-engine.ts +1 -1
  69. package/core/client/engine/property-watcher.ts +1 -1
  70. package/core/client/engine/reactive-proxy.ts +11 -10
  71. package/core/client/engine/render-engine.ts +39 -4
  72. package/core/client/engine/runtime-builder.ts +23 -0
  73. package/core/client/engine/soma-bootstrap.ts +380 -7
  74. package/core/client/engine/style-binder.ts +47 -1
  75. package/core/client/observability/error-reporter.ts +173 -0
  76. package/core/client/parser/adaptive-parser.ts +264 -0
  77. package/core/client/parser/data-config-parser.ts +396 -0
  78. package/core/client/parser/pipeline-parser.ts +15 -3
  79. package/core/client/parser/style-config-parser.ts +65 -2
  80. package/core/client/parser/ui-config-parser.ts +52 -3
  81. package/core/client/parser/v2/namespace.ts +182 -0
  82. package/core/client/parser/v2/page-parser.ts +359 -0
  83. package/core/client/parser/v2/use-parser.ts +412 -0
  84. package/core/client/parser/value-lang.ts +340 -0
  85. package/core/client/pipes/pipe-registry.ts +249 -0
  86. package/core/client/quality/SomaQualityRegistry.ts +3 -0
  87. package/core/client/registry/declaration-renderer.ts +161 -0
  88. package/core/client/registry/index.ts +28 -0
  89. package/core/client/registry/page-lifecycle.ts +171 -0
  90. package/core/client/registry/remote-page.ts +139 -0
  91. package/core/client/runtime/SomaRuntimeServices.ts +24 -0
  92. package/core/client/security/attribute-presence.ts +70 -0
  93. package/core/client/security/html-sanitizer.ts +113 -0
  94. package/core/client/security/url-guard.ts +0 -0
  95. package/core/client/theme/harmony.ts +307 -0
  96. package/core/client/theme/scene-tokens.ts +134 -0
  97. package/core/client/theme/theme-axes.ts +228 -0
  98. package/core/client/theme/theme-engine.ts +1196 -0
  99. package/core/client/theme/tone-solver.ts +118 -0
  100. package/core/client/typography/typography-base.ts +34 -0
  101. package/core/client/validation/contract-validator.ts +48 -5
  102. package/core/index.ts +262 -2
  103. package/core/server/api/IService.ts +18 -6
  104. package/core/server/api/load-host-service.ts +3 -3
  105. package/core/server/host/content-security-policy.ts +190 -0
  106. package/core/server/host/csp-nonce-metadata.ts +75 -0
  107. package/core/server/host/resolve-host-request-hook.ts +5 -0
  108. package/core/server/host/server-page-assets.ts +34 -7
  109. package/core/server/host/soma-page-compile.ts +33 -0
  110. package/core/server/index.ts +45 -1
  111. package/core/server/infra/cache/ServerFileCache.ts +1 -1
  112. package/core/server/mock/node/cli.mjs +2 -2
  113. package/core/server/registry/api-registry-store.ts +518 -0
  114. package/core/server/registry/conditional-object-store.ts +29 -0
  115. package/core/server/registry/declaration-precompiler.ts +99 -0
  116. package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
  117. package/core/server/registry/index.ts +142 -0
  118. package/core/server/registry/page-registry-repository.ts +373 -0
  119. package/core/server/registry/page-registry-store.ts +932 -0
  120. package/core/server/registry/registry-error.ts +17 -0
  121. package/core/server/registry/registry-file-lock.ts +173 -0
  122. package/core/server/registry/registry-response.ts +71 -0
  123. package/core/server/registry/registry-subjects.ts +194 -0
  124. package/core/server/registry/registry-write-pipeline.ts +240 -0
  125. package/core/server/registry/request-body-limit.ts +99 -0
  126. package/core/server/registry/services/apis/Get.ts +25 -0
  127. package/core/server/registry/services/apis/List.ts +41 -0
  128. package/core/server/registry/services/apis/Register.ts +39 -0
  129. package/core/server/registry/services/apis/Unregister.ts +24 -0
  130. package/core/server/registry/services/index.ts +40 -0
  131. package/core/server/registry/services/pages/Get.ts +96 -0
  132. package/core/server/registry/services/pages/List.ts +53 -0
  133. package/core/server/registry/services/pages/Register.ts +49 -0
  134. package/core/server/registry/services/pages/Rollback.ts +36 -0
  135. package/core/server/registry/services/pages/Unregister.ts +23 -0
  136. package/core/server/registry/services/service-utils.ts +21 -0
  137. package/core/server/registry/validation/compile-gate.ts +54 -0
  138. package/core/server/registry/validation/component-existence.ts +55 -0
  139. package/core/server/registry/validation/credential-ban.ts +86 -0
  140. package/core/server/registry/validation/cross-check.ts +67 -0
  141. package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
  142. package/core/shared/types/adaptive.types.ts +109 -0
  143. package/core/shared/types/component-contract.types.ts +120 -0
  144. package/core/shared/types/data-config.types.ts +105 -0
  145. package/core/shared/types/domain.types.ts +6 -0
  146. package/core/shared/types/engine.types.ts +41 -35
  147. package/core/shared/types/manifest.types.ts +55 -32
  148. package/core/shared/types/scope-manifest.types.ts +96 -0
  149. package/core/shared/types/style-config.types.ts +9 -0
  150. package/core/shared/types/ui-config.types.ts +118 -98
  151. package/dist/cli/soma.js +20 -5
  152. package/dist/core/index.cjs +7811 -1380
  153. package/dist/types/build/compiler/full-manifest-builder.d.ts +6 -2
  154. package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
  155. package/dist/types/build/compiler/page-assets.d.ts +3 -0
  156. package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
  157. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -2
  158. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  159. package/dist/types/build/compiler/soma-precompile.d.ts +17 -0
  160. package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -7
  161. package/dist/types/build/component/pack-from-dir.d.ts +17 -0
  162. package/dist/types/build/component/package-loader.d.ts +40 -0
  163. package/dist/types/build/eject/apply-eject.d.ts +22 -0
  164. package/dist/types/build/eject/plan-eject.d.ts +32 -0
  165. package/dist/types/build/v2/block-compile.d.ts +28 -0
  166. package/dist/types/build/v2/block-loader.d.ts +9 -0
  167. package/dist/types/build/v2/html-index.d.ts +37 -0
  168. package/dist/types/build/v2/migrate-v1.d.ts +32 -0
  169. package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
  170. package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
  171. package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
  172. package/dist/types/client/adaptive/index.d.ts +5 -0
  173. package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
  174. package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
  175. package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
  176. package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
  177. package/dist/types/client/component/ai-catalog.d.ts +73 -0
  178. package/dist/types/client/component/component-definition.types.d.ts +54 -0
  179. package/dist/types/client/component/component-expander.d.ts +25 -0
  180. package/dist/types/client/component/component-manager.d.ts +160 -0
  181. package/dist/types/client/component/component-pack.d.ts +110 -0
  182. package/dist/types/client/component/component-registry.d.ts +12 -0
  183. package/dist/types/client/component/contract-compat.d.ts +16 -0
  184. package/dist/types/client/component/example-v2.d.ts +9 -0
  185. package/dist/types/client/component/pack-styles.d.ts +22 -0
  186. package/dist/types/client/component/scoped-adapter.d.ts +18 -0
  187. package/dist/types/client/component/verify-pack.d.ts +14 -0
  188. package/dist/types/client/data/data-object-resolver.d.ts +71 -0
  189. package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
  190. package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
  191. package/dist/types/client/engine/asset-resolver.d.ts +7 -1
  192. package/dist/types/client/engine/dom-renderer.d.ts +51 -1
  193. package/dist/types/client/engine/emit-event.d.ts +6 -0
  194. package/dist/types/client/engine/event-engine.d.ts +6 -0
  195. package/dist/types/client/engine/render-engine.d.ts +25 -1
  196. package/dist/types/client/engine/runtime-builder.d.ts +7 -0
  197. package/dist/types/client/engine/soma-bootstrap.d.ts +118 -0
  198. package/dist/types/client/observability/error-reporter.d.ts +51 -0
  199. package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
  200. package/dist/types/client/parser/data-config-parser.d.ts +34 -0
  201. package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
  202. package/dist/types/client/parser/style-config-parser.d.ts +5 -3
  203. package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
  204. package/dist/types/client/parser/v2/namespace.d.ts +52 -0
  205. package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
  206. package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
  207. package/dist/types/client/parser/value-lang.d.ts +63 -0
  208. package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
  209. package/dist/types/client/registry/declaration-renderer.d.ts +50 -0
  210. package/dist/types/client/registry/index.d.ts +10 -0
  211. package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
  212. package/dist/types/client/registry/remote-page.d.ts +52 -0
  213. package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
  214. package/dist/types/client/security/attribute-presence.d.ts +7 -0
  215. package/dist/types/client/security/html-sanitizer.d.ts +37 -0
  216. package/dist/types/client/security/url-guard.d.ts +11 -0
  217. package/dist/types/client/theme/harmony.d.ts +77 -0
  218. package/dist/types/client/theme/scene-tokens.d.ts +25 -0
  219. package/dist/types/client/theme/theme-axes.d.ts +81 -0
  220. package/dist/types/client/theme/theme-engine.d.ts +77 -0
  221. package/dist/types/client/theme/tone-solver.d.ts +47 -0
  222. package/dist/types/client/typography/typography-base.d.ts +15 -0
  223. package/dist/types/client/validation/contract-validator.d.ts +2 -0
  224. package/dist/types/index.d.ts +47 -2
  225. package/dist/types/server/api/IService.d.ts +18 -6
  226. package/dist/types/server/host/content-security-policy.d.ts +53 -0
  227. package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
  228. package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
  229. package/dist/types/server/host/server-page-assets.d.ts +2 -0
  230. package/dist/types/server/host/soma-page-compile.d.ts +7 -0
  231. package/dist/types/server/index.d.ts +6 -1
  232. package/dist/types/server/registry/api-registry-store.d.ts +141 -0
  233. package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
  234. package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
  235. package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
  236. package/dist/types/server/registry/index.d.ts +29 -0
  237. package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
  238. package/dist/types/server/registry/page-registry-store.d.ts +259 -0
  239. package/dist/types/server/registry/registry-error.d.ts +12 -0
  240. package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
  241. package/dist/types/server/registry/registry-response.d.ts +18 -0
  242. package/dist/types/server/registry/registry-subjects.d.ts +59 -0
  243. package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
  244. package/dist/types/server/registry/request-body-limit.d.ts +31 -0
  245. package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
  246. package/dist/types/server/registry/services/apis/List.d.ts +10 -0
  247. package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
  248. package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
  249. package/dist/types/server/registry/services/index.d.ts +3 -0
  250. package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
  251. package/dist/types/server/registry/services/pages/List.d.ts +13 -0
  252. package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
  253. package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
  254. package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
  255. package/dist/types/server/registry/services/service-utils.d.ts +10 -0
  256. package/dist/types/server/registry/validation/compile-gate.d.ts +12 -0
  257. package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
  258. package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
  259. package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
  260. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
  261. package/dist/types/shared/types/adaptive.types.d.ts +88 -0
  262. package/dist/types/shared/types/component-contract.types.d.ts +78 -0
  263. package/dist/types/shared/types/data-config.types.d.ts +81 -0
  264. package/dist/types/shared/types/domain.types.d.ts +6 -0
  265. package/dist/types/shared/types/engine.types.d.ts +6 -0
  266. package/dist/types/shared/types/manifest.types.d.ts +23 -0
  267. package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
  268. package/dist/types/shared/types/style-config.types.d.ts +8 -0
  269. package/dist/types/shared/types/ui-config.types.d.ts +20 -0
  270. package/editor/vscode/package.json +1 -24
  271. package/global.d.ts +5 -11
  272. package/next.config.js +85 -28
  273. package/package.json +33 -11
  274. package/proxy.ts +62 -0
  275. package/tailwind.config.ts +0 -1
  276. package/core/build/lsp/event-yaml-completion.cjs +0 -106
  277. package/core/client/engine/theme-variables.ts +0 -37
  278. package/dist/tsconfig.types.tsbuildinfo +0 -1
  279. package/dist/types/client/engine/theme-variables.d.ts +0 -3
  280. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
  281. package/middleware.ts +0 -33
@@ -0,0 +1,190 @@
1
+ // Host 的内容安全策略(CSP)生成器(B6 安全域)。
2
+ //
3
+ // 为什么 SOMA 敢走严格 CSP,而普通前端框架不敢:
4
+ // 1. **DSL 没有脚本能力**——`_event.yaml` 的值是四动词 Pipeline 字符串,只映射到
5
+ // Domain 的 funcPath,引擎全程不用 `eval` / `new Function`(全仓 grep 客户端可达
6
+ // 代码零命中),所以 `script-src` 不需要 `'unsafe-eval'`;
7
+ // 2. **模板纯度**——html 四件套禁 `<script>` / 内联事件属性(手写侧有闸门,
8
+ // AI 产物侧有声明静态扫描),页面里根本不存在需要放行的内联脚本;
9
+ // 3. **组件库零外链**——soma-ui 48 个组件不引 CDN、不拉远程字体、图标是内联 SVG,
10
+ // 所以 `script-src` / `style-src` / `font-src` 一律 `'self'` 即可;
11
+ // 4. **样式按载体拆开**——组件库聚合注入的 `<style>` 必须带 nonce;
12
+ // 主题令牌、显隐与受控 `@style` 会写元素的 CSSOM,浏览器按 `style-src-attr` 管辖,
13
+ // 因此只对属性样式定向放行 `'unsafe-inline'`,不扩大 `<style>` 标签的执行面。
14
+ //
15
+ // 默认关(`SOMA_CSP` 未设 = off):CSP 一旦收紧就会打断存量宿主的第三方接入,
16
+ // 必须让宿主显式开启并先跑一轮 report-only,故不做「悄悄生效」的默认值。
17
+
18
+ /** 策略档位:off 不发头 / report-only 只上报 / enforce 强制拦截 */
19
+ export type SomaCspMode = 'off' | 'report-only' | 'enforce';
20
+
21
+ /** 开关与调参的环境变量(Host 的 .env 里配) */
22
+ export const SOMA_CSP_MODE_ENV = 'SOMA_CSP';
23
+ export const SOMA_CSP_CONNECT_SRC_ENV = 'SOMA_CSP_CONNECT_SRC';
24
+ export const SOMA_CSP_IMG_SRC_ENV = 'SOMA_CSP_IMG_SRC';
25
+ export const SOMA_CSP_FRAME_SRC_ENV = 'SOMA_CSP_FRAME_SRC';
26
+ export const SOMA_CSP_REPORT_URI_ENV = 'SOMA_CSP_REPORT_URI';
27
+
28
+ /**
29
+ * 中间件把本轮 nonce 放在这个请求头上,页面/组件侧要给动态注入的 `<style>` 打 nonce 时取用。
30
+ * (Next 自身的内联脚本由框架依据**请求上的 CSP 头**自动打 nonce,不需要这个头。)
31
+ */
32
+ export const SOMA_CSP_NONCE_HEADER = 'x-soma-csp-nonce';
33
+
34
+ export interface SomaCspOptions {
35
+ mode: SomaCspMode;
36
+ /** 本轮请求的 nonce;缺省表示不启用 nonce(dev 档用 'unsafe-inline' 顶替) */
37
+ nonce?: string;
38
+ /** dev 档:Next 的 React Refresh 需要 eval 与内联脚本,HMR 需要 ws: */
39
+ dev?: boolean;
40
+ /** 业务追加的 connect-src 源(AI 上游域、埋点域) */
41
+ connectSrc?: string[];
42
+ /** 业务追加的 img-src 源(CDN 图床) */
43
+ imgSrc?: string[];
44
+ /** 允许宿主 iframe 加载的 frame-src 源;缺省为空,策略保持 'none'。 */
45
+ frameSrc?: string[];
46
+ /** 违规上报端点 */
47
+ reportUri?: string;
48
+ }
49
+
50
+ /** 生成一次性 nonce(Web Crypto,Edge 与 Node 18+ 都有,不引 node:crypto 以便跑在 Edge 运行时) */
51
+ export function createSomaCspNonce(): string {
52
+ const bytes = new Uint8Array(16);
53
+ crypto.getRandomValues(bytes);
54
+ let binary = '';
55
+ for (const byte of bytes) {
56
+ binary += String.fromCharCode(byte);
57
+ }
58
+ return btoa(binary);
59
+ }
60
+
61
+ /** 环境变量取值归一化:只认三个档位,别的写法一律当 off(配错不该悄悄半开) */
62
+ export function resolveSomaCspMode(raw: string | undefined): SomaCspMode {
63
+ switch (raw?.trim().toLowerCase()) {
64
+ case 'enforce':
65
+ case 'on':
66
+ case '1':
67
+ return 'enforce';
68
+ case 'report-only':
69
+ case 'report':
70
+ return 'report-only';
71
+ default:
72
+ return 'off';
73
+ }
74
+ }
75
+
76
+ /** 逗号 / 空白分隔的源列表 */
77
+ function parseSourceList(raw: string | undefined): string[] {
78
+ return (raw ?? '')
79
+ .split(/[,\s]+/)
80
+ .map((item) => item.trim())
81
+ .filter(Boolean);
82
+ }
83
+
84
+ /** 从进程环境解析出完整选项(dev 档由 NODE_ENV 判定,不额外开关) */
85
+ export function resolveSomaCspOptionsFromEnv(
86
+ env: Record<string, string | undefined>,
87
+ ): SomaCspOptions {
88
+ const mode = resolveSomaCspMode(env[SOMA_CSP_MODE_ENV]);
89
+ const dev = env.NODE_ENV !== 'production';
90
+ return {
91
+ mode,
92
+ dev,
93
+ // dev 档不发 nonce:Next 开发服务器的内联样式无法逐个打 nonce,发了反而全挂
94
+ nonce: mode === 'off' || dev ? undefined : createSomaCspNonce(),
95
+ connectSrc: parseSourceList(env[SOMA_CSP_CONNECT_SRC_ENV]),
96
+ imgSrc: parseSourceList(env[SOMA_CSP_IMG_SRC_ENV]),
97
+ frameSrc: parseSourceList(env[SOMA_CSP_FRAME_SRC_ENV]),
98
+ reportUri: env[SOMA_CSP_REPORT_URI_ENV]?.trim() || undefined,
99
+ };
100
+ }
101
+
102
+ /** 去重且保序(手写顺序即可读性顺序,不排序) */
103
+ function uniq(values: string[]): string[] {
104
+ return [...new Set(values.filter(Boolean))];
105
+ }
106
+
107
+ /**
108
+ * 指令表(有序):key 为指令名,value 为源列表(空数组表示无值指令,如 upgrade-insecure-requests)。
109
+ * 单独导出便于宿主做差异化覆盖与测试逐条断言。
110
+ */
111
+ export function buildSomaCspDirectives(options: SomaCspOptions): Array<[string, string[]]> {
112
+ const { nonce, dev } = options;
113
+ const nonceSource = nonce ? [`'nonce-${nonce}'`] : [];
114
+
115
+ // dev 档的两处放宽都只为 Next 开发服务器本身:React Refresh 用 eval 编译热更模块,
116
+ // webpack style-loader 用内联 <style> 注样式。生产档一律不带。
117
+ const scriptSrc = dev
118
+ ? ["'self'", "'unsafe-inline'", "'unsafe-eval'"]
119
+ : ["'self'", ...nonceSource];
120
+ const styleSrc = dev
121
+ ? ["'self'", "'unsafe-inline'"]
122
+ : ["'self'", ...nonceSource];
123
+
124
+ const frameSrc = uniq(options.frameSrc ?? []);
125
+ const directives: Array<[string, string[]]> = [
126
+ ['default-src', ["'self'"]],
127
+ ['base-uri', ["'self'"]],
128
+ ['object-src', ["'none'"]],
129
+ ['frame-src', frameSrc.length > 0 ? frameSrc : ["'none'"]],
130
+ ['frame-ancestors', ["'self'"]],
131
+ ['form-action', ["'self'"]],
132
+ ['script-src', scriptSrc],
133
+ ['style-src', styleSrc],
134
+ // SOMA 的 when/UI_INTERACT、主题令牌与受控 @style 都写元素 CSSOM。
135
+ // 单独放行属性样式,保持 <style> 标签仍只能 self 或 nonce。
136
+ ['style-src-attr', ["'unsafe-inline'"]],
137
+ // data: 供内联 SVG/占位图;blob: 供前端生成的导出图;https: 是业务图床的兜底,
138
+ // 有确定图床域时应经 SOMA_CSP_IMG_SRC 收窄并删掉 https:
139
+ ['img-src', uniq(["'self'", 'data:', 'blob:', 'https:', ...(options.imgSrc ?? [])])],
140
+ ['font-src', uniq(["'self'", 'data:'])],
141
+ // 声明拉取、发布、telemetry 全部同源;AI 上游域由宿主显式追加
142
+ ['connect-src', uniq(["'self'", ...(dev ? ['ws:', 'wss:'] : []), ...(options.connectSrc ?? [])])],
143
+ ['media-src', ["'self'"]],
144
+ ['manifest-src', ["'self'"]],
145
+ ['worker-src', uniq(["'self'", 'blob:'])],
146
+ ];
147
+
148
+ if (!dev) {
149
+ directives.push(['upgrade-insecure-requests', []]);
150
+ }
151
+ if (options.reportUri) {
152
+ directives.push(['report-uri', [options.reportUri]]);
153
+ }
154
+ return directives;
155
+ }
156
+
157
+ /** 指令表序列化为响应头值 */
158
+ export function buildSomaCspHeaderValue(options: SomaCspOptions): string {
159
+ return buildSomaCspDirectives(options)
160
+ .map(([name, sources]) => (sources.length > 0 ? `${name} ${sources.join(' ')}` : name))
161
+ .join('; ');
162
+ }
163
+
164
+ /** 档位对应的响应头名 */
165
+ export function somaCspHeaderName(mode: Exclude<SomaCspMode, 'off'>): string {
166
+ return mode === 'report-only'
167
+ ? 'content-security-policy-report-only'
168
+ : 'content-security-policy';
169
+ }
170
+
171
+ export interface SomaCspHeader {
172
+ name: string;
173
+ value: string;
174
+ /** 本轮 nonce(dev 档与 off 档为 undefined) */
175
+ nonce?: string;
176
+ }
177
+
178
+ /**
179
+ * 中间件接线点:返回应写的响应头;off 档返回 null(调用方原样放行,零副作用)。
180
+ */
181
+ export function createSomaCspHeader(options: SomaCspOptions): SomaCspHeader | null {
182
+ if (options.mode === 'off') {
183
+ return null;
184
+ }
185
+ return {
186
+ name: somaCspHeaderName(options.mode),
187
+ value: buildSomaCspHeaderValue(options),
188
+ nonce: options.nonce,
189
+ };
190
+ }
@@ -0,0 +1,75 @@
1
+ // 把本轮请求的 CSP nonce 透出给浏览器侧(细节与生产化强化 §4 作业单 D-5)。
2
+ //
3
+ // 为什么需要这个模块:中间件产出的 nonce 只活在请求头 `x-soma-csp-nonce` 里,那是服务端的东西;
4
+ // 真正要消费它的是浏览器里运行期注入 `<style>` 的组件库样式注入器(soma-ui 的
5
+ // `ensureSomaUiStyles`)。运行期注入意味着服务端渲染时那个 `<style>` 还不存在,没法在 SSR
6
+ // 阶段直接给它打 nonce,所以必须留一个客户端读得到的落点——`<meta name="csp-nonce">`。
7
+ //
8
+ // 为什么单独成模块而不是写死在 app/layout.tsx 里:
9
+ // 1. 官方 Host 的 layout 会 `import './globals.css'`(走 tailwind/postcss),把逻辑关在
10
+ // 里面就没法单测;
11
+ // 2. 自己写 layout 的宿主也需要这段能力,抄一遍就会抄漏「CSP 关时别读请求头」这条。
12
+
13
+ import { resolveSomaCspMode, SOMA_CSP_MODE_ENV, SOMA_CSP_NONCE_HEADER } from './content-security-policy';
14
+
15
+ /** Next `headers()` 的返回形状(只用到 get);单测据此做替身,不必拉起整个框架 */
16
+ export interface SomaCspHeaderBag {
17
+ get(name: string): string | null;
18
+ }
19
+
20
+ /**
21
+ * 读取本轮请求的 CSP nonce;CSP 未开或取不到时返回 null。
22
+ *
23
+ * `readHeaders` 由调用方传入(官方 layout 传 `() => headers()`)。做成参数而不是在这里
24
+ * `import { headers } from 'next/headers'`,是为了让这个模块不绑死 Next:中间件与 CSP 策略
25
+ * 模块都能跑在 Edge 运行时,这里也不该成为唯一的框架耦合点。
26
+ *
27
+ * 为什么先判档位再调 readHeaders:Next 的 `headers()` 一被调用,整棵路由树就掉出静态渲染。
28
+ * CSP 缺省是关的(`SOMA_CSP` 未设 = off),绝大多数宿主永远用不到这个 nonce,
29
+ * 不能让他们为一个不会渲染的 meta 丢掉静态化。
30
+ */
31
+ export function readSomaCspNonce(
32
+ readHeaders: () => SomaCspHeaderBag,
33
+ env: Record<string, string | undefined> = process.env,
34
+ ): string | null {
35
+ if (resolveSomaCspMode(env[SOMA_CSP_MODE_ENV]) === 'off') return null;
36
+ try {
37
+ const nonce = readHeaders().get(SOMA_CSP_NONCE_HEADER)?.trim();
38
+ return nonce ? nonce : null;
39
+ } catch {
40
+ // 没有请求作用域(静态导出、构建期预渲染)时 `headers()` 会抛。
41
+ // 这里安静降级:不发 meta,组件库随之退回「不写 nonce」的缺省行为,页面照常渲染。
42
+ return null;
43
+ }
44
+ }
45
+
46
+ /**
47
+ * 供 Next `generateMetadata()` 展开的片段:CSP 开且拿到 nonce 时产出
48
+ * `{ other: { 'csp-nonce': '<r>' } }`(渲染为 `<meta name="csp-nonce" content="<r>">`),
49
+ * 否则产出空对象(缺省行为一字不变)。
50
+ *
51
+ * 走 Metadata API 的 `other` 而不是在 layout 里手写 `<head><meta/></head>`:
52
+ * Next App Router 要求根布局的头部标签由 Metadata API 产出,手写 `<head>` 会与框架
53
+ * 自己的头部注入打架。
54
+ */
55
+ export function somaCspNonceMetadata(
56
+ readHeaders: () => SomaCspHeaderBag,
57
+ env: Record<string, string | undefined> = process.env,
58
+ ): { other?: Record<string, string> } {
59
+ const nonce = readSomaCspNonce(readHeaders, env);
60
+ return nonce ? { other: { 'csp-nonce': nonce } } : {};
61
+ }
62
+
63
+ /** Next 15+ 的 `headers()` 为异步 API;CSP 关闭时仍不触发请求态读取。 */
64
+ export async function somaCspNonceMetadataAsync(
65
+ readHeaders: () => Promise<SomaCspHeaderBag>,
66
+ env: Record<string, string | undefined> = process.env,
67
+ ): Promise<{ other?: Record<string, string> }> {
68
+ if (resolveSomaCspMode(env[SOMA_CSP_MODE_ENV]) === 'off') return {};
69
+ try {
70
+ const nonce = (await readHeaders()).get(SOMA_CSP_NONCE_HEADER)?.trim();
71
+ return nonce ? { other: { 'csp-nonce': nonce } } : {};
72
+ } catch {
73
+ return {};
74
+ }
75
+ }
@@ -9,6 +9,11 @@ export interface SomaHostRequestContext {
9
9
  /**
10
10
  * Generic server ingress extension. soma-core does not know authentication,
11
11
  * authorization, Admin, or any business application.
12
+ *
13
+ * The hook receives the same Request object later dispatched to the Host
14
+ * service. A Host may therefore normalize or add server-only request headers
15
+ * before returning undefined. Returning a Response still short-circuits the
16
+ * request. Secrets must never be copied into a response or client config.
12
17
  */
13
18
  export type SomaRequestHook = (
14
19
  request: Request,
@@ -11,6 +11,7 @@ import {
11
11
  getSomaAssetSources,
12
12
  type SomaAssetSource,
13
13
  } from './source-packages';
14
+ import { compileSomaPageAssets } from './soma-page-compile';
14
15
 
15
16
  export type SomaPageAssetLoadProfile = 'render' | 'headless';
16
17
 
@@ -22,6 +23,8 @@ export interface SomaServerPageAssets {
22
23
  ui: string;
23
24
  event: string;
24
25
  styleYaml: string;
26
+ /** 构建期从统一声明编译出的运行时数据对象资产。 */
27
+ dataYaml?: string;
25
28
  localeYaml: string;
26
29
  appConfigYaml: string;
27
30
  includes?: Record<string, string>;
@@ -142,7 +145,23 @@ export function loadSomaServerPageAssets(options: {
142
145
  const { source, base } = resolvedPage;
143
146
  const htmlPath = assertContainedHtmlPath(source, base);
144
147
  try {
145
- const ui = cache.tryReadFile(join(source.assetRoot, 'ui', base + '_ui.yaml'));
148
+ const uiDir = join(source.assetRoot, 'ui');
149
+ const declarationPath = join(uiDir, base + '_ui.yaml');
150
+ const forbiddenSidecars = [
151
+ join(source.assetRoot, 'event', base + '_event.yaml'),
152
+ join(uiDir, base + '_style.yaml'),
153
+ join(source.assetRoot, 'data', base + '_data.yaml'),
154
+ join(uiDir, base + '_data.yaml'),
155
+ ].filter(existsSync);
156
+ if (forbiddenSidecars.length > 0) {
157
+ throw new SomaPageAssetError(
158
+ 'SOMA_PAGE_SIDECAR_FORBIDDEN',
159
+ 422,
160
+ '页面侧车已退役;请把事件、样式状态与数据对象并入同一份 SOMA DSL 声明:'
161
+ + forbiddenSidecars.map((file) => relative(source.assetRoot, file)).join(', '),
162
+ );
163
+ }
164
+ const ui = cache.tryReadFile(declarationPath);
146
165
  const localeYaml = cache.tryReadFile(join(hostConfigRoot, 'locale.yaml'));
147
166
  const hostAppConfigPath = resolveHostAppConfigPath(hostDir);
148
167
  const hostAppConfigYaml = hostAppConfigPath
@@ -156,14 +175,21 @@ export function loadSomaServerPageAssets(options: {
156
175
  ? hostAppConfigYaml
157
176
  : composeAppConfigYaml(sourceAppConfigYaml, hostAppConfigYaml);
158
177
 
178
+ const html = cache.readFile(htmlPath);
179
+ const includes = collectIncludes(sources);
180
+
181
+ // 唯一页面声明在服务端编译;浏览器只接收运行时资产,不解析源 YAML。
182
+ const compiled = compileSomaPageAssets(ui, html, uiDir, base, includes);
183
+
159
184
  if (profile === 'headless') {
160
185
  return {
161
186
  base,
162
187
  sourceType: source.type,
163
188
  sourcePackage: source.packageName ?? null,
164
- ui,
189
+ ui: compiled.ui,
165
190
  event: '',
166
191
  styleYaml: '',
192
+ ...(compiled.dataYaml ? { dataYaml: compiled.dataYaml } : {}),
167
193
  localeYaml,
168
194
  appConfigYaml,
169
195
  };
@@ -173,13 +199,14 @@ export function loadSomaServerPageAssets(options: {
173
199
  base,
174
200
  sourceType: source.type,
175
201
  sourcePackage: source.packageName ?? null,
176
- html: cache.readFile(htmlPath),
177
- ui,
178
- event: cache.tryReadFile(join(source.assetRoot, 'event', base + '_event.yaml')),
179
- styleYaml: cache.tryReadFile(join(source.assetRoot, 'ui', base + '_style.yaml')),
202
+ html,
203
+ ui: compiled.ui,
204
+ event: compiled.event,
205
+ styleYaml: '',
206
+ ...(compiled.dataYaml ? { dataYaml: compiled.dataYaml } : {}),
180
207
  localeYaml,
181
208
  appConfigYaml,
182
- includes: collectIncludes(sources),
209
+ includes,
183
210
  };
184
211
  } catch (error) {
185
212
  if (error instanceof SomaPageAssetError) throw error;
@@ -0,0 +1,33 @@
1
+ // SOMA DSL 页面接入运行路径:源声明只在服务端编译,浏览器只消费运行时资产。
2
+
3
+ import { compilePageDeclaration } from '../../build/compiler/page-declaration-compiler';
4
+ import { loadExternalBlocks } from '../../build/v2/block-loader';
5
+
6
+ export interface SomaPageCompileResult {
7
+ ui: string;
8
+ event: string;
9
+ dataYaml?: string;
10
+ }
11
+
12
+ /** 外置块约定 `ui/{base}.blocks/*.yaml`,与页面声明同目录。 */
13
+ export function compileSomaPageAssets(
14
+ uiRaw: string,
15
+ html: string,
16
+ uiDir: string,
17
+ base: string,
18
+ includes?: Readonly<Record<string, string>>,
19
+ ): SomaPageCompileResult {
20
+ const externalBlocks = loadExternalBlocks(uiDir, base);
21
+ const compiled = compilePageDeclaration({
22
+ html,
23
+ uiYaml: uiRaw,
24
+ ...(externalBlocks.size > 0 ? { externalBlocks } : {}),
25
+ ...(includes ? { includes } : {}),
26
+ });
27
+
28
+ return {
29
+ ui: compiled.runtimeAssets.uiYaml,
30
+ event: compiled.runtimeAssets.eventYaml,
31
+ ...(compiled.runtimeAssets.dataYaml ? { dataYaml: compiled.runtimeAssets.dataYaml } : {}),
32
+ };
33
+ }
@@ -15,7 +15,7 @@ export {
15
15
  type SomaServerRequestContextOptions,
16
16
  } from './infra/http/soma-server-request-context';
17
17
 
18
- export type { IService } from './api/IService';
18
+ export type { IService, SomaServiceRequest } from './api/IService';
19
19
  export { loadHostService } from './api/load-host-service';
20
20
  export { resolveServiceFilePath, toPascalCase } from './api/service-path';
21
21
 
@@ -26,6 +26,29 @@ export {
26
26
  type SomaHostRequestKind,
27
27
  type SomaRequestHook,
28
28
  } from './host/resolve-host-request-hook';
29
+ export {
30
+ createSomaCspHeader,
31
+ createSomaCspNonce,
32
+ buildSomaCspDirectives,
33
+ buildSomaCspHeaderValue,
34
+ resolveSomaCspMode,
35
+ resolveSomaCspOptionsFromEnv,
36
+ somaCspHeaderName,
37
+ SOMA_CSP_MODE_ENV,
38
+ SOMA_CSP_CONNECT_SRC_ENV,
39
+ SOMA_CSP_IMG_SRC_ENV,
40
+ SOMA_CSP_FRAME_SRC_ENV,
41
+ SOMA_CSP_REPORT_URI_ENV,
42
+ SOMA_CSP_NONCE_HEADER,
43
+ type SomaCspMode,
44
+ type SomaCspOptions,
45
+ type SomaCspHeader,
46
+ } from './host/content-security-policy';
47
+ export {
48
+ readSomaCspNonce,
49
+ somaCspNonceMetadata,
50
+ type SomaCspHeaderBag,
51
+ } from './host/csp-nonce-metadata';
29
52
  export {
30
53
  loadSomaServerPageAssets,
31
54
  SomaPageAssetError,
@@ -33,6 +56,27 @@ export {
33
56
  type SomaPageAssetLoadProfile,
34
57
  } from './host/server-page-assets';
35
58
 
59
+ export {
60
+ loadComponentPackage,
61
+ loadComponentPackages,
62
+ scanComponentPackages,
63
+ SomaComponentPackageError,
64
+ type LoadPackagesOptions,
65
+ type PackScanWarning,
66
+ } from '../build/component/package-loader';
67
+
68
+ export {
69
+ loadComponentPackFromDir,
70
+ type LoadPackFromDirOptions,
71
+ } from '../build/component/pack-from-dir';
72
+
73
+ // DSL v2 容器(REQ-005):开关探测、HTML 锚点索引、块装载、外置块发现。
74
+ // 用 node:fs 的只有 block-loader,其余纯函数(可在浏览器侧复用)。
75
+ export {
76
+ loadExternalBlocks,
77
+ blocksDirOf,
78
+ } from '../build/v2/block-loader';
79
+
36
80
  export {
37
81
  setSomaDb,
38
82
  getSomaDb,
@@ -101,7 +101,7 @@ export class ServerFileCache {
101
101
  // Changes require a server restart in that environment.
102
102
  if (process.env.NODE_ENV !== 'production') {
103
103
  console.warn(
104
- `[soma-core] fs.watch recursive failed (${e.code ?? e.message}). ` +
104
+ `[soma-core][缓存] fs.watch recursive failed (${e.code ?? e.message}). ` +
105
105
  'File cache will not auto-invalidate — restart the server after changes.',
106
106
  );
107
107
  }
@@ -83,10 +83,10 @@ function sanitizeMockEnvironment(environment, preloadUrl, options = {}) {
83
83
 
84
84
  function assertSupportedNode(version = process.versions.node) {
85
85
  const [major, minor] = String(version).split('.').map(Number);
86
- if (major < 18 || (major === 18 && minor < 18)) {
86
+ if (major < 20 || (major === 20 && minor < 9)) {
87
87
  throw new SomaMockCliError(
88
88
  'NODE_VERSION_UNSUPPORTED',
89
- `Server Mock preloading requires Node >=18.18; current version is ${version}`,
89
+ `Server Mock preloading requires Node >=20.9; current version is ${version}`,
90
90
  );
91
91
  }
92
92
  }