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.
- package/README.md +42 -0
- package/app/SomaPageClient.tsx +39 -2
- package/app/[...path]/page.tsx +7 -6
- package/app/api/[...path]/route.ts +3 -3
- package/app/api/soma-registry/[...path]/route.ts +67 -0
- package/app/components/SomaHostPage.tsx +3 -8
- package/app/components/SomaPageClientBoundary.tsx +13 -0
- package/app/layout.tsx +6 -1
- package/app/page.tsx +3 -3
- package/app/soma/manifest/route.ts +1 -0
- package/app/soma/page-object/route.ts +1 -1
- package/core/build/compiler/full-manifest-builder.ts +6 -1
- package/core/build/compiler/rollup-plugin-soma.ts +6 -0
- package/core/build/compiler/soma-manifest-webpack-plugin.js +4 -0
- package/core/build/compiler/soma-precompile.js +3507 -2387
- package/core/build/compiler/soma-precompile.ts +31 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +3 -0
- package/core/build/compiler/soma-webpack-loader.js +7 -1
- package/core/build/compiler/vite-plugin-soma.ts +11 -1
- package/core/build/component/package-loader.ts +135 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +139 -0
- package/core/client/adaptive/flush-scheduler.ts +96 -0
- package/core/client/adaptive/hysteresis.ts +56 -0
- package/core/client/adaptive/index.ts +23 -0
- package/core/client/adaptive/observer-pool.ts +90 -0
- package/core/client/base/SomaPageRootBase.ts +50 -0
- package/core/client/compiler/adaptive-compile.ts +136 -0
- package/core/client/compiler/manifest-compiler.ts +155 -27
- package/core/client/component/ai-catalog.ts +262 -0
- package/core/client/component/component-definition.types.ts +65 -0
- package/core/client/component/component-expander.ts +119 -0
- package/core/client/component/component-manager.ts +933 -0
- package/core/client/component/component-pack.ts +271 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/pack-styles.ts +94 -0
- package/core/client/component/scoped-adapter.ts +85 -0
- package/core/client/component/verify-pack.ts +247 -0
- package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
- package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
- package/core/client/container/SomaContainerAdapter.ts +186 -180
- package/core/client/data/data-object-resolver.ts +417 -0
- package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
- package/core/client/diagnostics/boot-timings.ts +152 -0
- package/core/client/engine/asset-resolver.ts +12 -2
- package/core/client/engine/dom-renderer.ts +235 -16
- package/core/client/engine/emit-event.ts +21 -0
- package/core/client/engine/event-engine.ts +34 -4
- package/core/client/engine/on-change-engine.ts +1 -1
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +3 -3
- package/core/client/engine/render-engine.ts +43 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +389 -5
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/host-bridge/empty-host/data/README.md +17 -0
- package/core/client/infra/http/SomaHttpService.ts +5 -0
- package/core/client/observability/error-reporter.ts +173 -0
- package/core/client/parser/adaptive-parser.ts +264 -0
- package/core/client/parser/data-config-parser.ts +396 -0
- package/core/client/parser/pipeline-parser.ts +15 -3
- package/core/client/parser/style-config-parser.ts +65 -2
- package/core/client/parser/ui-config-parser.ts +52 -3
- package/core/client/pipes/pipe-registry.ts +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +159 -0
- package/core/client/registry/index.ts +28 -0
- package/core/client/registry/page-lifecycle.ts +171 -0
- package/core/client/registry/remote-page.ts +139 -0
- package/core/client/runtime/SomaRuntimeServices.ts +24 -0
- package/core/client/scheduler/SomaScheduler.ts +29 -1
- package/core/client/security/attribute-presence.ts +70 -0
- package/core/client/security/html-sanitizer.ts +113 -0
- package/core/client/security/url-guard.ts +0 -0
- package/core/client/theme/theme-engine.ts +936 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +190 -2
- package/core/server/api/IService.ts +18 -6
- package/core/server/api/load-host-service.ts +3 -3
- package/core/server/host/content-security-policy.ts +190 -0
- package/core/server/host/csp-nonce-metadata.ts +75 -0
- package/core/server/host/resolve-host-request-hook.ts +5 -0
- package/core/server/host/server-page-assets.ts +10 -0
- package/core/server/index.ts +30 -1
- package/core/server/infra/cache/ServerFileCache.ts +1 -1
- package/core/server/mock/node/cli.mjs +2 -2
- package/core/server/registry/api-registry-store.ts +518 -0
- package/core/server/registry/conditional-object-store.ts +29 -0
- package/core/server/registry/declaration-precompiler.ts +129 -0
- package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
- package/core/server/registry/index.ts +142 -0
- package/core/server/registry/page-registry-repository.ts +373 -0
- package/core/server/registry/page-registry-store.ts +903 -0
- package/core/server/registry/registry-error.ts +17 -0
- package/core/server/registry/registry-file-lock.ts +173 -0
- package/core/server/registry/registry-response.ts +71 -0
- package/core/server/registry/registry-subjects.ts +194 -0
- package/core/server/registry/registry-write-pipeline.ts +240 -0
- package/core/server/registry/request-body-limit.ts +99 -0
- package/core/server/registry/services/apis/Get.ts +25 -0
- package/core/server/registry/services/apis/List.ts +41 -0
- package/core/server/registry/services/apis/Register.ts +39 -0
- package/core/server/registry/services/apis/Unregister.ts +24 -0
- package/core/server/registry/services/index.ts +40 -0
- package/core/server/registry/services/pages/Get.ts +86 -0
- package/core/server/registry/services/pages/List.ts +53 -0
- package/core/server/registry/services/pages/Register.ts +49 -0
- package/core/server/registry/services/pages/Rollback.ts +36 -0
- package/core/server/registry/services/pages/Unregister.ts +23 -0
- package/core/server/registry/services/service-utils.ts +21 -0
- package/core/server/registry/validation/compile-gate.ts +71 -0
- package/core/server/registry/validation/component-existence.ts +55 -0
- package/core/server/registry/validation/credential-ban.ts +86 -0
- package/core/server/registry/validation/cross-check.ts +77 -0
- package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
- package/core/shared/types/adaptive.types.ts +109 -0
- package/core/shared/types/component-contract.types.ts +110 -0
- package/core/shared/types/data-config.types.ts +105 -0
- package/core/shared/types/domain.types.ts +6 -0
- package/core/shared/types/engine.types.ts +41 -35
- package/core/shared/types/manifest.types.ts +55 -32
- package/core/shared/types/scope-manifest.types.ts +96 -0
- package/core/shared/types/style-config.types.ts +9 -0
- package/core/shared/types/ui-config.types.ts +118 -98
- package/dist/cli/soma.js +20 -5
- package/dist/core/index.cjs +5388 -888
- package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +1 -0
- package/dist/types/build/compiler/soma-precompile.d.ts +9 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +3 -0
- package/dist/types/build/component/package-loader.d.ts +9 -0
- package/dist/types/build/eject/apply-eject.d.ts +22 -0
- package/dist/types/build/eject/plan-eject.d.ts +32 -0
- package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
- package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
- package/dist/types/client/adaptive/index.d.ts +5 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
- package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
- package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
- package/dist/types/client/component/ai-catalog.d.ts +73 -0
- package/dist/types/client/component/component-definition.types.d.ts +54 -0
- package/dist/types/client/component/component-expander.d.ts +25 -0
- package/dist/types/client/component/component-manager.d.ts +160 -0
- package/dist/types/client/component/component-pack.d.ts +90 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- package/dist/types/client/component/pack-styles.d.ts +22 -0
- package/dist/types/client/component/scoped-adapter.d.ts +18 -0
- package/dist/types/client/component/verify-pack.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +71 -0
- package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
- package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
- package/dist/types/client/engine/asset-resolver.d.ts +7 -1
- package/dist/types/client/engine/dom-renderer.d.ts +49 -1
- package/dist/types/client/engine/emit-event.d.ts +6 -0
- package/dist/types/client/engine/event-engine.d.ts +6 -0
- package/dist/types/client/engine/render-engine.d.ts +26 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +122 -0
- package/dist/types/client/infra/http/SomaHttpService.d.ts +2 -0
- package/dist/types/client/observability/error-reporter.d.ts +51 -0
- package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
- package/dist/types/client/parser/data-config-parser.d.ts +34 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
- package/dist/types/client/parser/style-config-parser.d.ts +5 -3
- package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
- package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +51 -0
- package/dist/types/client/registry/index.d.ts +10 -0
- package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
- package/dist/types/client/registry/remote-page.d.ts +52 -0
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
- package/dist/types/client/scheduler/SomaScheduler.d.ts +8 -0
- package/dist/types/client/security/attribute-presence.d.ts +7 -0
- package/dist/types/client/security/html-sanitizer.d.ts +37 -0
- package/dist/types/client/security/url-guard.d.ts +11 -0
- package/dist/types/client/theme/theme-engine.d.ts +49 -0
- package/dist/types/client/typography/typography-base.d.ts +15 -0
- package/dist/types/client/validation/contract-validator.d.ts +2 -0
- package/dist/types/index.d.ts +36 -2
- package/dist/types/server/api/IService.d.ts +18 -6
- package/dist/types/server/host/content-security-policy.d.ts +53 -0
- package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
- package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
- package/dist/types/server/host/server-page-assets.d.ts +5 -0
- package/dist/types/server/index.d.ts +4 -1
- package/dist/types/server/registry/api-registry-store.d.ts +141 -0
- package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
- package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
- package/dist/types/server/registry/index.d.ts +29 -0
- package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
- package/dist/types/server/registry/page-registry-store.d.ts +259 -0
- package/dist/types/server/registry/registry-error.d.ts +12 -0
- package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
- package/dist/types/server/registry/registry-response.d.ts +18 -0
- package/dist/types/server/registry/registry-subjects.d.ts +59 -0
- package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
- package/dist/types/server/registry/request-body-limit.d.ts +31 -0
- package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
- package/dist/types/server/registry/services/apis/List.d.ts +10 -0
- package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
- package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
- package/dist/types/server/registry/services/index.d.ts +3 -0
- package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
- package/dist/types/server/registry/services/pages/List.d.ts +13 -0
- package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
- package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
- package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
- package/dist/types/server/registry/services/service-utils.d.ts +10 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +10 -0
- package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
- package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
- package/dist/types/shared/types/adaptive.types.d.ts +88 -0
- package/dist/types/shared/types/component-contract.types.d.ts +68 -0
- package/dist/types/shared/types/data-config.types.d.ts +81 -0
- package/dist/types/shared/types/domain.types.d.ts +6 -0
- package/dist/types/shared/types/engine.types.d.ts +6 -0
- package/dist/types/shared/types/manifest.types.d.ts +23 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
- package/dist/types/shared/types/style-config.types.d.ts +8 -0
- package/dist/types/shared/types/ui-config.types.d.ts +20 -0
- package/global.d.ts +12 -0
- package/next.config.js +76 -1
- package/package.json +32 -11
- package/proxy.ts +62 -0
- package/core/client/engine/theme-variables.ts +0 -37
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/client/engine/theme-variables.d.ts +0 -3
- package/middleware.ts +0 -33
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// core/client/typography/typography-base.ts — 中文排版基线(F-05 / 契约 C2)
|
|
2
|
+
//
|
|
3
|
+
// 归属理由(arch/契约与边界.md §1):不用组件库的纯声明页面也应具备中文排版基线,
|
|
4
|
+
// 所以它是地基能力而不是组件资产。soma-ui 基座**引用而不复制**这个常量——
|
|
5
|
+
// 复制即产生第二份事实,G6 同源门判红。
|
|
6
|
+
//
|
|
7
|
+
// 作用域挂 `[data-soma-host]` 而不是 `:root`:core 是被宿主集成的框架,
|
|
8
|
+
// 污染宿主全局是越界;挂宿主根既覆盖全部 soma 渲染内容,又不碰宿主自有区域。
|
|
9
|
+
//
|
|
10
|
+
// 渐进增强,零兜底:四条款全部是"支持则更好",浏览器不支持时渲染即现状。
|
|
11
|
+
// **明确不做特性检测与 JS polyfill**——排版兜底的收益远低于其复杂度与性能代价,
|
|
12
|
+
// 因此本资产的运行时成本恒为零(无 JS、无监听、无测量)。
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 中文排版基线样式。Host 集成时随主题一并注入。
|
|
16
|
+
*
|
|
17
|
+
* 条款与理由:
|
|
18
|
+
* - `text-autospace`:中西文之间的自动空隙(盘古之白)。
|
|
19
|
+
* 由排版引擎产生,手敲空格是内容污染——空格会进搜索索引、进复制粘贴、进后端字段。
|
|
20
|
+
* - `line-break: strict`:行首行尾禁则(避免"。"落行首、"("落行尾)。
|
|
21
|
+
* - `word-break: normal` + `overflow-wrap: break-word`:
|
|
22
|
+
* 中文按正常规则断,长 URL / 哈希这类无空格长串才允许强断,两者缺一都会出问题。
|
|
23
|
+
* - `hanging-punctuation: first last`:标点悬挂,让首尾行的文字边缘对齐。
|
|
24
|
+
*
|
|
25
|
+
* 不承诺竖排(`writing-mode`);繁体 / 日文的禁则差异走 i18n 层 locale 排版参数,
|
|
26
|
+
* 本资产只预留概念位。
|
|
27
|
+
*/
|
|
28
|
+
export const TYPOGRAPHY_BASE_CSS = `[data-soma-host] {
|
|
29
|
+
text-autospace: normal;
|
|
30
|
+
line-break: strict;
|
|
31
|
+
word-break: normal;
|
|
32
|
+
overflow-wrap: break-word;
|
|
33
|
+
hanging-punctuation: first last;
|
|
34
|
+
}`;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SomaScopeResolver } from '../engine/scope-resolver';
|
|
2
|
+
import { hasPipes, parsePipedValue } from '../pipes/pipe-registry';
|
|
2
3
|
import type { ParsedEvent } from '../../shared/types/event.types';
|
|
3
4
|
import type {
|
|
4
5
|
ExtendedShowValue,
|
|
@@ -121,6 +122,29 @@ export class SomaContractValidator {
|
|
|
121
122
|
return { anchors, duplicates };
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
/** 展示值可携带格式化管道:仅校验管道前的基础路径与绑定型实参路径 */
|
|
126
|
+
private validatePipedPath(
|
|
127
|
+
rawValue: string,
|
|
128
|
+
currentPath: string,
|
|
129
|
+
resolver: SomaScopeResolver,
|
|
130
|
+
rootInstance: object,
|
|
131
|
+
report: SomaContractReport,
|
|
132
|
+
): void {
|
|
133
|
+
if (!hasPipes(rawValue)) {
|
|
134
|
+
this.validateReadablePath(rawValue, currentPath, resolver, rootInstance, report);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
const parsed = parsePipedValue(rawValue, (seg) => resolver.hasVar(seg));
|
|
139
|
+
this.validateReadablePath(parsed.basePath, currentPath, resolver, rootInstance, report);
|
|
140
|
+
for (const argPath of parsed.argPaths) {
|
|
141
|
+
this.validateReadablePath(argPath, `${currentPath}(pipe-arg)`, resolver, rootInstance, report);
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
this.addError(report, `${currentPath}: ${(error as Error).message}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
124
148
|
private validateShowMapping(
|
|
125
149
|
show: ShowMapping,
|
|
126
150
|
anchors: Set<string>,
|
|
@@ -132,6 +156,16 @@ export class SomaContractValidator {
|
|
|
132
156
|
for (const [rawKey, value] of Object.entries(show)) {
|
|
133
157
|
report.bindingCount += 1;
|
|
134
158
|
const currentPath = `${basePath}.${rawKey}`;
|
|
159
|
+
|
|
160
|
+
// 异构列表分组键(item:变体):本身不是锚点,仅递归校验子映射
|
|
161
|
+
if (/^item:.+/.test(rawKey)) {
|
|
162
|
+
const sub = (value as ExtendedShowValue | undefined)?.show;
|
|
163
|
+
if (sub) {
|
|
164
|
+
this.validateShowMapping(sub, anchors, resolver, rootInstance, report, currentPath);
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
|
|
135
169
|
const somaId = this.extractSomaId(rawKey);
|
|
136
170
|
|
|
137
171
|
if (!anchors.has(somaId)) {
|
|
@@ -139,7 +173,7 @@ export class SomaContractValidator {
|
|
|
139
173
|
}
|
|
140
174
|
|
|
141
175
|
if (typeof value === 'string') {
|
|
142
|
-
this.
|
|
176
|
+
this.validatePipedPath(value, currentPath, resolver, rootInstance, report);
|
|
143
177
|
continue;
|
|
144
178
|
}
|
|
145
179
|
|
|
@@ -164,7 +198,7 @@ export class SomaContractValidator {
|
|
|
164
198
|
|
|
165
199
|
if (this.isExtendedShowValue(value)) {
|
|
166
200
|
if (value.text) {
|
|
167
|
-
this.
|
|
201
|
+
this.validatePipedPath(
|
|
168
202
|
value.text,
|
|
169
203
|
`${currentPath}.text`,
|
|
170
204
|
resolver,
|
|
@@ -226,10 +260,19 @@ export class SomaContractValidator {
|
|
|
226
260
|
|
|
227
261
|
for (const event of events) {
|
|
228
262
|
const eventKey = `${event.domEvent}:${event.somaId}`;
|
|
229
|
-
|
|
230
|
-
|
|
263
|
+
// 去重要带上按键修饰符:同一个输入框绑 ↑ ↓ ← → Enter Esc 各一条是
|
|
264
|
+
// listbox/tablist/tree 的 ARIA 键盘模式的常规写法,不是重复声明。
|
|
265
|
+
// 只有"同锚点 + 同事件 + 同按键"才是真重复。
|
|
266
|
+
const dedupeKey = `${eventKey}:${event.keyModifier ?? ''}`;
|
|
267
|
+
if (eventKeys.has(dedupeKey)) {
|
|
268
|
+
this.addError(
|
|
269
|
+
report,
|
|
270
|
+
event.keyModifier
|
|
271
|
+
? `event.${eventKey}.${event.keyModifier}: 重复声明的事件绑定`
|
|
272
|
+
: `event.${eventKey}: 重复声明的事件绑定`,
|
|
273
|
+
);
|
|
231
274
|
} else {
|
|
232
|
-
eventKeys.add(
|
|
275
|
+
eventKeys.add(dedupeKey);
|
|
233
276
|
}
|
|
234
277
|
|
|
235
278
|
if (!anchors.has(event.somaId)) {
|
package/core/index.ts
CHANGED
|
@@ -6,14 +6,149 @@ export * from './client/base/index';
|
|
|
6
6
|
export type { SomaAppConfig, SomaPageRoot } from './shared/types/domain.types';
|
|
7
7
|
|
|
8
8
|
// Host API service contract
|
|
9
|
-
export type { IService } from './server/api/IService';
|
|
9
|
+
export type { IService, SomaServiceRequest } from './server/api/IService';
|
|
10
10
|
|
|
11
11
|
// Bootstrap & runtime
|
|
12
12
|
export { SomaBootstrap } from './client/engine/soma-bootstrap';
|
|
13
|
-
export type {
|
|
13
|
+
export type {
|
|
14
|
+
SomaPageAssets,
|
|
15
|
+
SomaBootstrapOptions,
|
|
16
|
+
SomaBootstrapErrorReporting,
|
|
17
|
+
} from './client/engine/soma-bootstrap';
|
|
18
|
+
|
|
19
|
+
// ── 组件体系(模块 7) ──
|
|
20
|
+
export { SomaComponentRegistry } from './client/component/component-registry';
|
|
21
|
+
export { SomaComponentManager } from './client/component/component-manager';
|
|
22
|
+
export { generateAiCatalog, buildCatalogEntry } from './client/component/ai-catalog';
|
|
23
|
+
export type {
|
|
24
|
+
SomaAiCatalog,
|
|
25
|
+
SomaAiCatalogEntry,
|
|
26
|
+
GenerateAiCatalogOptions,
|
|
27
|
+
} from './client/component/ai-catalog';
|
|
28
|
+
export { SomaPipeRegistry, defaultPipeRegistry } from './client/pipes/pipe-registry';
|
|
29
|
+
export type { SomaPipeFn, SomaPipeContext } from './client/pipes/pipe-registry';
|
|
30
|
+
|
|
31
|
+
// ── 数据对象层(*_data.yaml,五件套可选件) ──
|
|
32
|
+
export {
|
|
33
|
+
SomaDataObjectResolver,
|
|
34
|
+
SomaDataCycleError,
|
|
35
|
+
isSomaMockEnabled,
|
|
36
|
+
} from './client/data/data-object-resolver';
|
|
37
|
+
export type {
|
|
38
|
+
SomaDataHttpLike,
|
|
39
|
+
SomaDataResolverOptions,
|
|
40
|
+
SomaDataResolveResult,
|
|
41
|
+
} from './client/data/data-object-resolver';
|
|
42
|
+
export {
|
|
43
|
+
parseDataConfig,
|
|
44
|
+
parseDataYaml,
|
|
45
|
+
parseDataRef,
|
|
46
|
+
normalizeDataPipeValue,
|
|
47
|
+
SomaDataConfigError,
|
|
48
|
+
} from './client/parser/data-config-parser';
|
|
49
|
+
export type { SomaDataRef } from './client/parser/data-config-parser';
|
|
50
|
+
export type {
|
|
51
|
+
SomaDataConfig,
|
|
52
|
+
SomaDataObjectConfig,
|
|
53
|
+
SomaDataSourceConfig,
|
|
54
|
+
SomaDataFieldConfig,
|
|
55
|
+
SomaDataFieldRule,
|
|
56
|
+
SomaDataFieldOption,
|
|
57
|
+
SomaDataFieldType,
|
|
58
|
+
SomaDataFieldPermission,
|
|
59
|
+
SomaDataDicts,
|
|
60
|
+
} from './shared/types/data-config.types';
|
|
61
|
+
export {
|
|
62
|
+
deriveTheme,
|
|
63
|
+
applyTheme,
|
|
64
|
+
contrastRatio,
|
|
65
|
+
THEME_SWITCHING_CLASS,
|
|
66
|
+
} from './client/theme/theme-engine';
|
|
67
|
+
export type { SomaThemeInput, SomaDerivedTheme } from './client/theme/theme-engine';
|
|
68
|
+
|
|
69
|
+
// ── 中文排版基线(F-05 / 契约 C2)──
|
|
70
|
+
// soma-ui 基座引用而不复制:复制即第二份事实。
|
|
71
|
+
export { TYPOGRAPHY_BASE_CSS } from './client/typography/typography-base';
|
|
72
|
+
|
|
73
|
+
// ── 降级阶梯:声明语法 + 词汇注册 + 编译产物(F-07 – F-10 / 契约 C4 C5 C6)──
|
|
74
|
+
export {
|
|
75
|
+
parseAdaptiveConfig,
|
|
76
|
+
createAdaptiveVocabulary,
|
|
77
|
+
SomaAdaptiveConfigError,
|
|
78
|
+
} from './client/parser/adaptive-parser';
|
|
79
|
+
export {
|
|
80
|
+
compileAdaptive,
|
|
81
|
+
applyCompiledAdaptive,
|
|
82
|
+
SOMA_ADAPTIVE_PROFILE_ATTR,
|
|
83
|
+
} from './client/compiler/adaptive-compile';
|
|
84
|
+
export {
|
|
85
|
+
SOMA_ADAPTIVE_PROFILES,
|
|
86
|
+
SOMA_ADAPTIVE_MAX_LADDER,
|
|
87
|
+
} from './shared/types/adaptive.types';
|
|
88
|
+
export type {
|
|
89
|
+
SomaAdaptiveProfile,
|
|
90
|
+
SomaAdaptiveConfig,
|
|
91
|
+
SomaAdaptiveLevel,
|
|
92
|
+
SomaAdaptiveLevelKind,
|
|
93
|
+
SomaAdaptiveLevelDefinition,
|
|
94
|
+
SomaAdaptiveVocabulary,
|
|
95
|
+
SomaAdaptiveScopeClass,
|
|
96
|
+
SomaAdaptiveExecutorConfig,
|
|
97
|
+
SomaCompiledAdaptive,
|
|
98
|
+
} from './shared/types/adaptive.types';
|
|
99
|
+
|
|
100
|
+
// ── 自适应调度原语三件套(F-11 – F-14 / 契约 C7)──
|
|
101
|
+
// 也可经 `soma-core/adaptive` 子路径单独引入:不 attach 即不加载不执行。
|
|
102
|
+
export {
|
|
103
|
+
createAdaptiveObserverPool,
|
|
104
|
+
scheduleAdaptiveFlush,
|
|
105
|
+
resolveTierWithHysteresis,
|
|
106
|
+
SOMA_DEFAULT_HYSTERESIS,
|
|
107
|
+
} from './client/adaptive/index';
|
|
108
|
+
export type {
|
|
109
|
+
SomaAdaptiveObserverPool,
|
|
110
|
+
SomaAdaptiveSize,
|
|
111
|
+
SomaAdaptiveSizeCallback,
|
|
112
|
+
SomaAdaptiveTask,
|
|
113
|
+
} from './client/adaptive/index';
|
|
114
|
+
export type {
|
|
115
|
+
SomaComponentDefinition,
|
|
116
|
+
SomaComponentControllerContext,
|
|
117
|
+
SomaComponentControllerFactory,
|
|
118
|
+
} from './client/component/component-definition.types';
|
|
119
|
+
export type {
|
|
120
|
+
SomaComponentContract,
|
|
121
|
+
SomaComponentLayer,
|
|
122
|
+
SomaGeometryFamily,
|
|
123
|
+
SomaContractProp,
|
|
124
|
+
SomaContractEmit,
|
|
125
|
+
SomaContractSlot,
|
|
126
|
+
} from './shared/types/component-contract.types';
|
|
127
|
+
export { findNestedMutableContractDefault } from './shared/types/component-contract.types';
|
|
14
128
|
export { createDefaultRuntimeServices } from './client/runtime/SomaRuntimeServices';
|
|
15
129
|
export type { SomaRuntimeServices } from './client/runtime/SomaRuntimeServices';
|
|
16
130
|
export { SomaDiagnostics } from './client/diagnostics/SomaDiagnostics';
|
|
131
|
+
|
|
132
|
+
// 可观测性:前端错误采集(错误 → 页面名@版本 归因通道)
|
|
133
|
+
export { installSomaErrorReporter } from './client/observability/error-reporter';
|
|
134
|
+
export type {
|
|
135
|
+
SomaErrorEvent,
|
|
136
|
+
SomaPageIdentity,
|
|
137
|
+
SomaErrorReporterOptions,
|
|
138
|
+
SomaErrorReporterHandle,
|
|
139
|
+
} from './client/observability/error-reporter';
|
|
140
|
+
|
|
141
|
+
// 安全:属性写入闸(伪协议 URL / 事件处理器属性拦截,已内建于容器适配器)
|
|
142
|
+
export { sanitizeAttributeWrite, hasBlockedScheme, isEventHandlerAttribute } from './client/security/url-guard';
|
|
143
|
+
// 安全:@html 净化收口(宿主可注入 sanitizer,缺省转义降级)
|
|
144
|
+
export {
|
|
145
|
+
setSomaHtmlSanitizer,
|
|
146
|
+
resolveSomaHtmlSanitizer,
|
|
147
|
+
sanitizeSomaHtml,
|
|
148
|
+
escapeSomaHtml,
|
|
149
|
+
SOMA_PURIFY_PROFILE,
|
|
150
|
+
} from './client/security/html-sanitizer';
|
|
151
|
+
export type { SomaHtmlSanitizer, SomaHtmlSanitizeResult } from './client/security/html-sanitizer';
|
|
17
152
|
export type { SomaDiagnosticsSnapshot } from './client/diagnostics/SomaDiagnostics';
|
|
18
153
|
export { matchGlobPath } from './client/engine/property-watcher';
|
|
19
154
|
export { extractConfigFromDomainClass } from './client/parser/annotation-extractor';
|
|
@@ -24,6 +159,17 @@ export type { AsyncStateHost } from './client/base/runAsync';
|
|
|
24
159
|
export { SomaQualityRegistry } from './client/quality/SomaQualityRegistry';
|
|
25
160
|
export { SomaTelemetry } from './client/diagnostics/SomaTelemetry';
|
|
26
161
|
export { setActiveTelemetry, recordMetric } from './client/diagnostics/telemetry-context';
|
|
162
|
+
|
|
163
|
+
// 性能:首屏三段打点(资产就绪 → 编译完成 → 首次渲染完成),默认关闭零开销
|
|
164
|
+
export {
|
|
165
|
+
startBootTimings,
|
|
166
|
+
maybeStartBootTimings,
|
|
167
|
+
markBootPhase,
|
|
168
|
+
readBootTimings,
|
|
169
|
+
resetBootTimings,
|
|
170
|
+
isBootTimingsEnabled,
|
|
171
|
+
} from './client/diagnostics/boot-timings';
|
|
172
|
+
export type { SomaBootTimings, SomaBootPhase } from './client/diagnostics/boot-timings';
|
|
27
173
|
export type { IContainerAdapter, SomaAnchorStats } from './client/container/IContainerAdapter';
|
|
28
174
|
export { SomaContainerAdapter } from './client/container/SomaContainerAdapter';
|
|
29
175
|
export { ReactNativeContainerAdapter } from './client/container/ReactNativeContainerAdapter';
|
|
@@ -61,3 +207,45 @@ export {
|
|
|
61
207
|
findSomaDomainLoader,
|
|
62
208
|
loadSomaDomainModuleFromRegistry,
|
|
63
209
|
} from './client/host-bridge/soma-domain-loaders';
|
|
210
|
+
|
|
211
|
+
// 运行时注册中心 · 客户端(REQ-002)。
|
|
212
|
+
// 也有 `soma-core/registry` 子路径入口;宿主 Domain 请用本裸包名——
|
|
213
|
+
// 宿主的 webpack 上下文只解析裸包名与 @soma-host 别名,workspace 包的
|
|
214
|
+
// subpath exports 在那里不可达(与 'soma-core/compiler/precompile' 同款实弹教训)。
|
|
215
|
+
export {
|
|
216
|
+
createGeneratedPagePreview,
|
|
217
|
+
createSomaPageLifecycle,
|
|
218
|
+
renderPublishedPage,
|
|
219
|
+
SomaPageLifecycle,
|
|
220
|
+
type GeneratedPageDeclarations,
|
|
221
|
+
type GeneratedPagePreviewOptions,
|
|
222
|
+
type PrecompiledDeclarationAssets,
|
|
223
|
+
type PreviewHandle,
|
|
224
|
+
type PreviewObservabilityOptions,
|
|
225
|
+
type MountedPageInfo,
|
|
226
|
+
type PublishedPageHandle,
|
|
227
|
+
type PublishedPageRecord,
|
|
228
|
+
type RegisterDeclarationOptions,
|
|
229
|
+
type RenderPublishedPageOptions,
|
|
230
|
+
type SomaPageLifecycleOptions,
|
|
231
|
+
} from './client/registry/index';
|
|
232
|
+
|
|
233
|
+
// 组件包(业务拓展组件库):合成、查重、目录、自检。
|
|
234
|
+
// 接入指南见 soma-core/doc/soma 组件包接入指南.md。
|
|
235
|
+
export {
|
|
236
|
+
composeComponentPacks,
|
|
237
|
+
generatePackCatalog,
|
|
238
|
+
ComponentPackError,
|
|
239
|
+
PACK_NAME_PATTERN,
|
|
240
|
+
type ComposedComponentPacks,
|
|
241
|
+
type ComposePacksOptions,
|
|
242
|
+
type SomaComponentPack,
|
|
243
|
+
type SomaComponentPackStyles,
|
|
244
|
+
} from './client/component/component-pack';
|
|
245
|
+
export { verifyComponentPack, type VerifyPackOptions } from './client/component/verify-pack';
|
|
246
|
+
export {
|
|
247
|
+
attachPackStyles,
|
|
248
|
+
ensurePackBaseStyles,
|
|
249
|
+
resetPackStyleInjection,
|
|
250
|
+
type PackStyleOptions,
|
|
251
|
+
} from './client/component/pack-styles';
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The service boundary deliberately uses the Web Request surface instead of a
|
|
3
|
+
* concrete NextRequest class. Hosts can resolve soma-core and Next.js through
|
|
4
|
+
* different workspace copies (for example a published AI package may use
|
|
5
|
+
* Next 14 while the host runs Next 16); those classes are structurally the
|
|
6
|
+
* same request at this boundary but are nominally incompatible in TypeScript.
|
|
7
|
+
*/
|
|
8
|
+
export interface SomaServiceRequest {
|
|
9
|
+
readonly headers: Headers;
|
|
10
|
+
readonly signal: AbortSignal;
|
|
11
|
+
readonly method: string;
|
|
12
|
+
readonly url: string;
|
|
13
|
+
}
|
|
2
14
|
|
|
3
15
|
/**
|
|
4
16
|
* SOMA 后端服务接口 —— 宿主项目 `api/service/` 下的服务类可实现此接口。
|
|
@@ -21,9 +33,9 @@ import { NextRequest } from 'next/server';
|
|
|
21
33
|
* ```
|
|
22
34
|
*/
|
|
23
35
|
export interface IService {
|
|
24
|
-
get?(req:
|
|
25
|
-
post?(req:
|
|
26
|
-
put?(req:
|
|
27
|
-
patch?(req:
|
|
28
|
-
delete?(req:
|
|
36
|
+
get?(req: SomaServiceRequest): Promise<Response>;
|
|
37
|
+
post?(req: SomaServiceRequest): Promise<Response>;
|
|
38
|
+
put?(req: SomaServiceRequest): Promise<Response>;
|
|
39
|
+
patch?(req: SomaServiceRequest): Promise<Response>;
|
|
40
|
+
delete?(req: SomaServiceRequest): Promise<Response>;
|
|
29
41
|
}
|
|
@@ -55,13 +55,13 @@ export function loadHostService(segments: string[]): IService | null {
|
|
|
55
55
|
const mod = getJiti(hostDir)(filePath) as { default?: new () => IService };
|
|
56
56
|
const ServiceClass = mod?.default;
|
|
57
57
|
if (!ServiceClass || typeof ServiceClass !== 'function') {
|
|
58
|
-
console.warn(`[soma-
|
|
58
|
+
console.warn(`[soma-core][接口] ${filePath} missing default export`);
|
|
59
59
|
return null;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
const instance = new ServiceClass();
|
|
63
63
|
if (!isService(instance)) {
|
|
64
|
-
console.warn(`[soma-
|
|
64
|
+
console.warn(`[soma-core][接口] ${filePath} does not implement IService`);
|
|
65
65
|
return null;
|
|
66
66
|
}
|
|
67
67
|
|
|
@@ -69,7 +69,7 @@ export function loadHostService(segments: string[]): IService | null {
|
|
|
69
69
|
return instance;
|
|
70
70
|
} catch (e: unknown) {
|
|
71
71
|
const message = e instanceof Error ? e.message : String(e);
|
|
72
|
-
console.error(`[soma-
|
|
72
|
+
console.error(`[soma-core][接口] failed to load ${filePath}:`, message);
|
|
73
73
|
return null;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -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,
|