soma-core 1.3.2 → 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 +26 -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 +39 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +359 -4
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/host-bridge/empty-host/data/README.md +17 -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/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 +5341 -894
- 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 +25 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +116 -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/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 +4 -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,417 @@
|
|
|
1
|
+
// core/client/data/data-object-resolver.ts — 数据对象解析器(数据对象层运行时核心)
|
|
2
|
+
// 职责:按 *_data.yaml 声明取数——解析 `$` 引用构建对象依赖 DAG → 拓扑序取数
|
|
3
|
+
// (无依赖来源并发 Promise.all)→ into 聚合/根浅合并 → transform 走管道注册表补派生字段。
|
|
4
|
+
// 纯客户端实现,http 服务依赖注入;产物 { objects, dicts } 由 bootstrap 挂到 page.data。
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
defaultPipeRegistry,
|
|
8
|
+
hasPipes,
|
|
9
|
+
parsePipedValue,
|
|
10
|
+
SomaPipeError,
|
|
11
|
+
type SomaPipeContext,
|
|
12
|
+
type SomaPipeRegistry,
|
|
13
|
+
} from '../pipes/pipe-registry';
|
|
14
|
+
import { readQueryParam } from '../infra/router/router-path';
|
|
15
|
+
import {
|
|
16
|
+
normalizeDataPipeValue,
|
|
17
|
+
parseDataRef,
|
|
18
|
+
SomaDataConfigError,
|
|
19
|
+
} from '../parser/data-config-parser';
|
|
20
|
+
import type {
|
|
21
|
+
SomaDataConfig,
|
|
22
|
+
SomaDataDicts,
|
|
23
|
+
SomaDataObjectConfig,
|
|
24
|
+
SomaDataSourceConfig,
|
|
25
|
+
} from '../../shared/types/data-config.types';
|
|
26
|
+
|
|
27
|
+
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
/** 数据对象依赖成环(`$<对象>.<字段>` 引用构成环路,无法拓扑排序) */
|
|
30
|
+
export class SomaDataCycleError extends Error {
|
|
31
|
+
/** 环上的对象名列表 */
|
|
32
|
+
readonly cycle: string[];
|
|
33
|
+
|
|
34
|
+
constructor(cycle: string[]) {
|
|
35
|
+
super(`[soma-core][数据] 数据对象依赖成环: ${cycle.join(' → ')}`);
|
|
36
|
+
this.name = 'SomaDataCycleError';
|
|
37
|
+
this.cycle = cycle;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ─── 环境开关 ────────────────────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* mock 通道开关:`SOMA_MOCK_ENABLED` 环境变量(Node/构建注入)
|
|
45
|
+
* 或全局标记 `globalThis.SOMA_MOCK_ENABLED`(浏览器控制台/宿主注入)。
|
|
46
|
+
*/
|
|
47
|
+
export function isSomaMockEnabled(): boolean {
|
|
48
|
+
const globalFlag = (globalThis as Record<string, unknown>).SOMA_MOCK_ENABLED;
|
|
49
|
+
if (globalFlag !== undefined) {
|
|
50
|
+
return globalFlag === true || globalFlag === '1' || globalFlag === 'true';
|
|
51
|
+
}
|
|
52
|
+
const envFlag = typeof process !== 'undefined' ? process.env?.SOMA_MOCK_ENABLED : undefined;
|
|
53
|
+
return envFlag === '1' || envFlag === 'true';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ─── 类型定义 ────────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
/** 解析器需要的最小 http 能力(结构兼容 IHttpClient / SomaHttpService) */
|
|
59
|
+
export interface SomaDataHttpLike {
|
|
60
|
+
get<T = unknown>(url: string, config?: { params?: Record<string, string> }): Promise<T>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface SomaDataResolverOptions {
|
|
64
|
+
/** 依赖注入的 http 服务(后端接口是唯一数据边界) */
|
|
65
|
+
http: SomaDataHttpLike;
|
|
66
|
+
/**
|
|
67
|
+
* $query 读取器:注入的 query 表或取值函数;
|
|
68
|
+
* 缺省回退 location.search(复用 router-path 的读取逻辑)。
|
|
69
|
+
*/
|
|
70
|
+
query?: Record<string, string> | ((key: string) => string);
|
|
71
|
+
/** $page 读取器:Domain 实例(或返回实例的函数,取最新代理引用) */
|
|
72
|
+
page?: object | (() => object | null | undefined);
|
|
73
|
+
/** 格式化管道注册表(缺省为内置管道集;Host 自定义管道经 bootstrap 传入) */
|
|
74
|
+
pipes?: SomaPipeRegistry;
|
|
75
|
+
/** locale 表提供者(money 管道货币符号等本地化取值) */
|
|
76
|
+
locale?: () => Record<string, unknown> | undefined;
|
|
77
|
+
/** mock 通道开关(缺省读 SOMA_MOCK_ENABLED 环境) */
|
|
78
|
+
mockEnabled?: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 解析产物:数据对象 + 字段字典 */
|
|
82
|
+
export interface SomaDataResolveResult {
|
|
83
|
+
objects: Record<string, unknown>;
|
|
84
|
+
dicts: SomaDataDicts;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ─── 工具函数 ────────────────────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
90
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 按点分路径段读取(任一环节为空即返回 undefined) */
|
|
94
|
+
function readPath(root: unknown, segments: string[]): unknown {
|
|
95
|
+
let current: unknown = root;
|
|
96
|
+
for (const segment of segments) {
|
|
97
|
+
if (current === null || current === undefined) return undefined;
|
|
98
|
+
current = (current as Record<string, unknown>)[segment];
|
|
99
|
+
}
|
|
100
|
+
return current;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** mock 样本深拷贝(样本是纯 YAML 数据,JSON 拷贝安全) */
|
|
104
|
+
function cloneSample(sample: unknown): unknown {
|
|
105
|
+
if (sample === null || typeof sample !== 'object') return sample;
|
|
106
|
+
return JSON.parse(JSON.stringify(sample));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** params 值是否引用了指定对象(决定来源在对象内是否延后串行执行) */
|
|
110
|
+
function referencesObject(value: unknown, objectName: string): boolean {
|
|
111
|
+
if (typeof value === 'string') {
|
|
112
|
+
const ref = value.startsWith('$') ? parseDataRef(value) : null;
|
|
113
|
+
return ref?.source === 'object' && ref.object === objectName;
|
|
114
|
+
}
|
|
115
|
+
if (Array.isArray(value)) {
|
|
116
|
+
return value.some((item) => referencesObject(item, objectName));
|
|
117
|
+
}
|
|
118
|
+
if (isPlainObject(value)) {
|
|
119
|
+
return Object.values(value).some((item) => referencesObject(item, objectName));
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ─── 解析器 ──────────────────────────────────────────────────────────────────
|
|
125
|
+
|
|
126
|
+
export class SomaDataObjectResolver {
|
|
127
|
+
private readonly config: SomaDataConfig;
|
|
128
|
+
private readonly http: SomaDataHttpLike;
|
|
129
|
+
private readonly queryOption?: Record<string, string> | ((key: string) => string);
|
|
130
|
+
private readonly pageOption?: object | (() => object | null | undefined);
|
|
131
|
+
private readonly pipes: SomaPipeRegistry;
|
|
132
|
+
private readonly localeProvider?: () => Record<string, unknown> | undefined;
|
|
133
|
+
private readonly mockEnabled: boolean;
|
|
134
|
+
|
|
135
|
+
/** 拓扑执行过程中已解析的对象(供 `$<对象>.<字段>` 引用读取) */
|
|
136
|
+
private objects: Record<string, unknown> = {};
|
|
137
|
+
|
|
138
|
+
constructor(config: SomaDataConfig, options: SomaDataResolverOptions) {
|
|
139
|
+
this.config = config;
|
|
140
|
+
this.http = options.http;
|
|
141
|
+
this.queryOption = options.query;
|
|
142
|
+
this.pageOption = options.page;
|
|
143
|
+
this.pipes = options.pipes ?? defaultPipeRegistry;
|
|
144
|
+
this.localeProvider = options.locale;
|
|
145
|
+
this.mockEnabled = options.mockEnabled ?? isSomaMockEnabled();
|
|
146
|
+
|
|
147
|
+
// 声明期白名单校验:transform 引用了未注册的管道名 → 声明错误(fail fast,不吞)
|
|
148
|
+
for (const decl of Object.values(config.objects)) {
|
|
149
|
+
for (const [key, rawValue] of Object.entries(decl.transform ?? {})) {
|
|
150
|
+
const normalized = normalizeDataPipeValue(rawValue);
|
|
151
|
+
if (!hasPipes(normalized)) continue;
|
|
152
|
+
try {
|
|
153
|
+
const parsed = parsePipedValue(normalized, () => false);
|
|
154
|
+
this.pipes.assertKnown(parsed.pipes, `data.${decl.name}.transform.${key}`);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (error instanceof SomaPipeError) {
|
|
157
|
+
throw new SomaDataConfigError(error.message, `data.${decl.name}.transform.${key}`);
|
|
158
|
+
}
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 执行整份声明的取数编排。
|
|
167
|
+
* 对象按依赖 DAG 拓扑分层:同层对象(互不依赖)并发解析;成环抛 SomaDataCycleError。
|
|
168
|
+
*/
|
|
169
|
+
async resolve(): Promise<SomaDataResolveResult> {
|
|
170
|
+
this.objects = {};
|
|
171
|
+
const layers = this.buildTopologicalLayers();
|
|
172
|
+
|
|
173
|
+
for (const layer of layers) {
|
|
174
|
+
await Promise.all(
|
|
175
|
+
layer.map(async (name) => {
|
|
176
|
+
this.objects[name] = await this.resolveObject(this.config.objects[name]);
|
|
177
|
+
}),
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const dicts: SomaDataDicts = {};
|
|
182
|
+
for (const decl of Object.values(this.config.objects)) {
|
|
183
|
+
if (decl.fields) {
|
|
184
|
+
dicts[decl.name] = decl.fields;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return { objects: this.objects, dicts };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ─── DAG 拓扑 ──────────────────────────────────────────────────────────────
|
|
192
|
+
|
|
193
|
+
/** Kahn 分层拓扑排序:返回逐层对象名(每层内部无依赖,可并发);残留节点即环 */
|
|
194
|
+
private buildTopologicalLayers(): string[][] {
|
|
195
|
+
const names = Object.keys(this.config.objects);
|
|
196
|
+
const indegree = new Map<string, number>();
|
|
197
|
+
const dependents = new Map<string, string[]>();
|
|
198
|
+
|
|
199
|
+
for (const name of names) {
|
|
200
|
+
indegree.set(name, 0);
|
|
201
|
+
dependents.set(name, []);
|
|
202
|
+
}
|
|
203
|
+
for (const name of names) {
|
|
204
|
+
for (const dep of this.config.objects[name].deps) {
|
|
205
|
+
if (!indegree.has(dep)) continue; // 解析器已保证依赖存在,此处防御
|
|
206
|
+
indegree.set(name, (indegree.get(name) ?? 0) + 1);
|
|
207
|
+
dependents.get(dep)!.push(name);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const layers: string[][] = [];
|
|
212
|
+
let current = names.filter((name) => indegree.get(name) === 0);
|
|
213
|
+
let resolved = 0;
|
|
214
|
+
|
|
215
|
+
while (current.length > 0) {
|
|
216
|
+
layers.push(current);
|
|
217
|
+
resolved += current.length;
|
|
218
|
+
const next: string[] = [];
|
|
219
|
+
for (const name of current) {
|
|
220
|
+
for (const dependent of dependents.get(name)!) {
|
|
221
|
+
const remaining = indegree.get(dependent)! - 1;
|
|
222
|
+
indegree.set(dependent, remaining);
|
|
223
|
+
if (remaining === 0) {
|
|
224
|
+
next.push(dependent);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
current = next;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (resolved < names.length) {
|
|
232
|
+
const cyclic = names.filter((name) => (indegree.get(name) ?? 0) > 0);
|
|
233
|
+
throw new SomaDataCycleError(cyclic);
|
|
234
|
+
}
|
|
235
|
+
return layers;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ─── 单对象解析 ────────────────────────────────────────────────────────────
|
|
239
|
+
|
|
240
|
+
private async resolveObject(decl: SomaDataObjectConfig): Promise<unknown> {
|
|
241
|
+
// mock 开关全局打开且声明了样本:直接用样本(透传 transform 派生)
|
|
242
|
+
if (this.mockEnabled && decl.mock !== undefined) {
|
|
243
|
+
console.warn(`[soma-core][数据] 对象 "${decl.name}" 使用 mock 样本(SOMA_MOCK_ENABLED)`);
|
|
244
|
+
return this.applyTransform(cloneSample(decl.mock), decl);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// 无接口预览对象(仅 mock):样本即数据
|
|
248
|
+
if (decl.from.length === 0) {
|
|
249
|
+
return this.applyTransform(cloneSample(decl.mock) ?? {}, decl);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// 来源分波:不自引用的来源无内部依赖 → 首波并发 Promise.all;
|
|
253
|
+
// 引用本对象($<自身>.<字段>,如 $order.id)的来源需读取先行来源的合并结果 → 次波按声明序串行。
|
|
254
|
+
const concurrent: SomaDataSourceConfig[] = [];
|
|
255
|
+
const deferred: SomaDataSourceConfig[] = [];
|
|
256
|
+
for (const source of decl.from) {
|
|
257
|
+
(referencesObject(source.params, decl.name) ? deferred : concurrent).push(source);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
let value: unknown = {};
|
|
261
|
+
let failed: unknown = null;
|
|
262
|
+
|
|
263
|
+
const settled = await Promise.all(
|
|
264
|
+
concurrent.map(async (source) => {
|
|
265
|
+
try {
|
|
266
|
+
return { source, data: await this.fetchSource(source, decl.name, value) };
|
|
267
|
+
} catch (error) {
|
|
268
|
+
return { source, error };
|
|
269
|
+
}
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
for (const outcome of settled) {
|
|
273
|
+
if ('error' in outcome && outcome.error !== undefined) {
|
|
274
|
+
failed = { api: outcome.source.api, error: outcome.error };
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
value = this.mergeSource(value, outcome.source, (outcome as { data: unknown }).data);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (!failed) {
|
|
281
|
+
for (const source of deferred) {
|
|
282
|
+
try {
|
|
283
|
+
const data = await this.fetchSource(source, decl.name, value);
|
|
284
|
+
value = this.mergeSource(value, source, data);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
failed = { api: source.api, error };
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (failed) {
|
|
293
|
+
const { api, error } = failed as { api: string; error: unknown };
|
|
294
|
+
// 接口失败且声明了 mock:整对象回退样本并警示(不吞、不阻塞其余对象)
|
|
295
|
+
if (decl.mock !== undefined) {
|
|
296
|
+
console.warn(`[soma-core][数据] 对象 "${decl.name}" 来源 "${api}" 取数失败,回退 mock 样本:`, error);
|
|
297
|
+
return this.applyTransform(cloneSample(decl.mock), decl);
|
|
298
|
+
}
|
|
299
|
+
console.error(`[soma-core][数据] 对象 "${decl.name}" 来源 "${api}" 取数失败(保留已取得的部分数据):`, error);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return this.applyTransform(value, decl);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private async fetchSource(
|
|
306
|
+
source: SomaDataSourceConfig,
|
|
307
|
+
selfName: string,
|
|
308
|
+
selfValue: unknown,
|
|
309
|
+
): Promise<unknown> {
|
|
310
|
+
const params = this.resolveParams(source.params, selfName, selfValue);
|
|
311
|
+
return this.http.get(source.api, params ? { params } : undefined);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** 聚合来源结果:into 写入子键;缺省浅合并进根(非对象结果整体替换) */
|
|
315
|
+
private mergeSource(current: unknown, source: SomaDataSourceConfig, data: unknown): unknown {
|
|
316
|
+
if (source.into) {
|
|
317
|
+
const target = isPlainObject(current) ? current : {};
|
|
318
|
+
target[source.into] = data;
|
|
319
|
+
return target;
|
|
320
|
+
}
|
|
321
|
+
if (isPlainObject(data)) {
|
|
322
|
+
if (isPlainObject(current)) {
|
|
323
|
+
Object.assign(current, data);
|
|
324
|
+
return current;
|
|
325
|
+
}
|
|
326
|
+
return { ...data };
|
|
327
|
+
}
|
|
328
|
+
// 非对象结果(数组/标量)且未声明 into:对象值整体即该结果(如列表型数据对象)
|
|
329
|
+
return data;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// ─── `$` 引用求值 ──────────────────────────────────────────────────────────
|
|
333
|
+
|
|
334
|
+
/** 解析 params:`$` 引用替换为实际值 → 展平为 http query 参数表(空值参数省略) */
|
|
335
|
+
private resolveParams(
|
|
336
|
+
params: Record<string, unknown> | undefined,
|
|
337
|
+
selfName: string,
|
|
338
|
+
selfValue: unknown,
|
|
339
|
+
): Record<string, string> | undefined {
|
|
340
|
+
if (!params) return undefined;
|
|
341
|
+
const resolved: Record<string, string> = {};
|
|
342
|
+
for (const [key, raw] of Object.entries(params)) {
|
|
343
|
+
const value = this.resolveParamValue(raw, selfName, selfValue);
|
|
344
|
+
if (value === undefined || value === null) continue;
|
|
345
|
+
resolved[key] = typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
346
|
+
}
|
|
347
|
+
return Object.keys(resolved).length > 0 ? resolved : undefined;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private resolveParamValue(raw: unknown, selfName: string, selfValue: unknown): unknown {
|
|
351
|
+
if (typeof raw === 'string' && raw.startsWith('$')) {
|
|
352
|
+
const ref = parseDataRef(raw);
|
|
353
|
+
if (!ref) return raw; // 解析器已校验;防御性透传
|
|
354
|
+
if (ref.source === 'query') {
|
|
355
|
+
return this.readQuery(ref.path.join('.'));
|
|
356
|
+
}
|
|
357
|
+
if (ref.source === 'page') {
|
|
358
|
+
return readPath(this.readPage(), ref.path);
|
|
359
|
+
}
|
|
360
|
+
const target = ref.object === selfName ? selfValue : this.objects[ref.object!];
|
|
361
|
+
return readPath(target, ref.path);
|
|
362
|
+
}
|
|
363
|
+
if (Array.isArray(raw)) {
|
|
364
|
+
return raw.map((item) => this.resolveParamValue(item, selfName, selfValue));
|
|
365
|
+
}
|
|
366
|
+
if (isPlainObject(raw)) {
|
|
367
|
+
const result: Record<string, unknown> = {};
|
|
368
|
+
for (const [key, item] of Object.entries(raw)) {
|
|
369
|
+
result[key] = this.resolveParamValue(item, selfName, selfValue);
|
|
370
|
+
}
|
|
371
|
+
return result;
|
|
372
|
+
}
|
|
373
|
+
return raw;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private readQuery(key: string): string {
|
|
377
|
+
if (typeof this.queryOption === 'function') {
|
|
378
|
+
return this.queryOption(key);
|
|
379
|
+
}
|
|
380
|
+
if (this.queryOption) {
|
|
381
|
+
return this.queryOption[key] ?? '';
|
|
382
|
+
}
|
|
383
|
+
return readQueryParam(key);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private readPage(): object | null | undefined {
|
|
387
|
+
return typeof this.pageOption === 'function' ? this.pageOption() : this.pageOption;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// ─── transform 派生字段 ────────────────────────────────────────────────────
|
|
391
|
+
|
|
392
|
+
/** 按 "字段路径 | 管道链" 补派生字段(复用 pipe-registry 链式执行) */
|
|
393
|
+
private applyTransform(value: unknown, decl: SomaDataObjectConfig): unknown {
|
|
394
|
+
if (!decl.transform) return value;
|
|
395
|
+
if (!isPlainObject(value)) {
|
|
396
|
+
console.warn(`[soma-core][数据] 对象 "${decl.name}" 不是对象结构,transform 派生字段被跳过`);
|
|
397
|
+
return value;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const ctx: SomaPipeContext = {
|
|
401
|
+
read: (path) => readPath(value, path.split('.')),
|
|
402
|
+
locale: () => this.localeProvider?.(),
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
for (const [key, rawValue] of Object.entries(decl.transform)) {
|
|
406
|
+
const normalized = normalizeDataPipeValue(rawValue);
|
|
407
|
+
if (!hasPipes(normalized)) {
|
|
408
|
+
value[key] = readPath(value, normalized.split('.'));
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
const parsed = parsePipedValue(normalized, () => false);
|
|
412
|
+
const base = readPath(value, parsed.basePath.split('.'));
|
|
413
|
+
value[key] = this.pipes.apply(base, parsed.pipes, ctx);
|
|
414
|
+
}
|
|
415
|
+
return value;
|
|
416
|
+
}
|
|
417
|
+
}
|