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,129 @@
|
|
|
1
|
+
// 声明态页面的发布期预编译(P-a)。
|
|
2
|
+
//
|
|
3
|
+
// 现状:每次取页,客户端都要把 uiYaml / eventYaml / styleYaml / dataYaml 现场解析一遍。
|
|
4
|
+
// 页面能正常渲染,只是把同一份解析在每个访客的浏览器里重做一次——
|
|
5
|
+
// 而声明一旦发布就是不可变的,这份解析结果完全可以在**落库时算一次**。
|
|
6
|
+
//
|
|
7
|
+
// 三条纪律,第一条最重要:
|
|
8
|
+
// 1. **预编译是缓存,不是真理**。parser 一旦升级,库里躺着的产物就可能与新语义不符。
|
|
9
|
+
// 所以每份产物带 `compilerVersion`,消费端版本对不上就**整份丢弃、回落运行时解析**。
|
|
10
|
+
// 宁可慢一点,也不能拿旧编译器的产物渲染新语义。
|
|
11
|
+
// 2. **编译失败不阻断发布**。声明本身已经过校验闸门;预编译只是加速,
|
|
12
|
+
// 它挂了就当没有这份缓存——绝不能让一个性能优化变成发布的新失败面。
|
|
13
|
+
// 3. **只编译纯数据部分**。html 不编译(它本来就是字符串),
|
|
14
|
+
// locale / appConfig 归宿主而不归声明。
|
|
15
|
+
|
|
16
|
+
import { createRequire } from 'node:module';
|
|
17
|
+
import {
|
|
18
|
+
compileDataConfig,
|
|
19
|
+
compileEventConfig,
|
|
20
|
+
compileStyleConfig,
|
|
21
|
+
compileUiConfig,
|
|
22
|
+
} from '../../build/compiler/soma-precompile';
|
|
23
|
+
import type { PageRegistryDeclaration } from './page-registry-store';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 编译器版本。与 soma-core 的包版本对齐——语义变更必然伴随版本号变化,
|
|
27
|
+
* 拿它当"这份缓存还能不能用"的判据,比自己再造一套指纹可靠。
|
|
28
|
+
*
|
|
29
|
+
* 读不到版本时**故意返回一个永不相等的值**(带进程随机后缀),而不是回落成 '0'。
|
|
30
|
+
* 回落成固定值会让版本闸恒真——库里的旧产物照样被采用,那正是这道闸要防的事。
|
|
31
|
+
* 失败方向必须是"不用缓存",不是"照用"。
|
|
32
|
+
*/
|
|
33
|
+
export const DECLARATION_COMPILER_VERSION = readCoreVersion();
|
|
34
|
+
|
|
35
|
+
function readCoreVersion(): string {
|
|
36
|
+
try {
|
|
37
|
+
const require = createRequire(import.meta.url);
|
|
38
|
+
const version = (require('../../../package.json') as { version?: string }).version;
|
|
39
|
+
if (typeof version === 'string' && version !== '') {
|
|
40
|
+
return version;
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
// 落到下面的不可用标记
|
|
44
|
+
}
|
|
45
|
+
console.warn('[soma-core][注册中心] 读不到 soma-core 包版本,本进程一律不采用预编译产物');
|
|
46
|
+
return `unknown-${Math.random().toString(36).slice(2, 10)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 落库的预编译产物(全部可选:哪一段编译不出来就不带哪一段) */
|
|
50
|
+
export interface CompiledDeclaration {
|
|
51
|
+
compilerVersion: string;
|
|
52
|
+
uiConfig?: unknown;
|
|
53
|
+
eventConfig?: unknown;
|
|
54
|
+
triggerConfig?: unknown;
|
|
55
|
+
timerConfig?: unknown;
|
|
56
|
+
styleConfig?: unknown;
|
|
57
|
+
dataConfig?: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 把声明编译成运行时可直接消费的配置对象。
|
|
62
|
+
* 任何一段失败都只是少一段缓存,不抛。
|
|
63
|
+
*/
|
|
64
|
+
export function precompileDeclaration(
|
|
65
|
+
declaration: PageRegistryDeclaration,
|
|
66
|
+
): CompiledDeclaration | null {
|
|
67
|
+
const compiled: CompiledDeclaration = { compilerVersion: DECLARATION_COMPILER_VERSION };
|
|
68
|
+
let any = false;
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
compiled.uiConfig = compileUiConfig(declaration.uiYaml ?? '');
|
|
72
|
+
any = true;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.warn(`[soma-core][注册中心] uiYaml 预编译失败,回落运行时解析:${message(error)}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const events = compileEventConfig(declaration.eventYaml ?? '');
|
|
79
|
+
compiled.eventConfig = events.events;
|
|
80
|
+
compiled.triggerConfig = events.triggers;
|
|
81
|
+
compiled.timerConfig = events.timers;
|
|
82
|
+
any = true;
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.warn(`[soma-core][注册中心] eventYaml 预编译失败,回落运行时解析:${message(error)}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (declaration.styleYaml?.trim()) {
|
|
88
|
+
try {
|
|
89
|
+
compiled.styleConfig = compileStyleConfig(declaration.styleYaml);
|
|
90
|
+
any = true;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.warn(`[soma-core][注册中心] styleYaml 预编译失败:${message(error)}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (declaration.dataYaml?.trim()) {
|
|
97
|
+
try {
|
|
98
|
+
compiled.dataConfig = compileDataConfig(declaration.dataYaml);
|
|
99
|
+
any = true;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.warn(`[soma-core][注册中心] dataYaml 预编译失败:${message(error)}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// uiConfig 与 eventConfig 缺一,运行时就走不了预编译分支(asset-resolver 要求两者齐备),
|
|
106
|
+
// 这时整份产物没有意义,不如不存——省得给消费端留个半成品去判断。
|
|
107
|
+
if (!any || compiled.uiConfig === undefined || compiled.eventConfig === undefined) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return compiled;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 消费端的版本闸:编译器版本对不上就整份丢弃。
|
|
115
|
+
* 这是"缓存不是真理"那条纪律的执行点。
|
|
116
|
+
*/
|
|
117
|
+
export function isCompiledUsable(
|
|
118
|
+
compiled: CompiledDeclaration | null | undefined,
|
|
119
|
+
runtimeVersion = DECLARATION_COMPILER_VERSION,
|
|
120
|
+
): boolean {
|
|
121
|
+
if (!compiled || typeof compiled !== 'object') return false;
|
|
122
|
+
return compiled.compilerVersion === runtimeVersion
|
|
123
|
+
&& compiled.uiConfig !== undefined
|
|
124
|
+
&& compiled.eventConfig !== undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function message(error: unknown): string {
|
|
128
|
+
return error instanceof Error ? error.message : String(error);
|
|
129
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ConditionalObjectStore,
|
|
3
|
+
ConditionalWriteResult,
|
|
4
|
+
ObjectSnapshot,
|
|
5
|
+
WriteCondition,
|
|
6
|
+
} from './conditional-object-store';
|
|
7
|
+
|
|
8
|
+
interface StoredObject {
|
|
9
|
+
text: string;
|
|
10
|
+
revision: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** 可运行参考与测试后端;单进程内串行比较并交换,语义等同远端原子条件写。 */
|
|
14
|
+
export class InMemoryConditionalObjectStore implements ConditionalObjectStore {
|
|
15
|
+
private readonly objects = new Map<string, StoredObject>();
|
|
16
|
+
|
|
17
|
+
async read(key: string): Promise<ObjectSnapshot | null> {
|
|
18
|
+
const stored = this.objects.get(key);
|
|
19
|
+
return stored
|
|
20
|
+
? { key, text: stored.text, revision: String(stored.revision) }
|
|
21
|
+
: null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async list(prefix: string): Promise<readonly string[]> {
|
|
25
|
+
return [...this.objects.keys()].filter((key) => key.startsWith(prefix)).sort();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async compareAndSet(
|
|
29
|
+
key: string,
|
|
30
|
+
expected: WriteCondition,
|
|
31
|
+
text: string,
|
|
32
|
+
): Promise<ConditionalWriteResult> {
|
|
33
|
+
const current = this.objects.get(key);
|
|
34
|
+
if (expected.kind === 'absent') {
|
|
35
|
+
if (current) return { committed: false };
|
|
36
|
+
} else if (!current || String(current.revision) !== expected.revision) {
|
|
37
|
+
return { committed: false };
|
|
38
|
+
}
|
|
39
|
+
const revision = (current?.revision ?? 0) + 1;
|
|
40
|
+
this.objects.set(key, { text, revision });
|
|
41
|
+
return { committed: true, revision: String(revision) };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 运行时注册中心(服务端桶)。
|
|
3
|
+
*
|
|
4
|
+
* REQ-002:注册/校验/存储/生命周期归 soma-core(能力),谁可注册、注册什么、
|
|
5
|
+
* 挂在哪归业务应用(组织)。soma-ai 只是本模块的一个普通调用方。
|
|
6
|
+
* HTTP 端点(/api/soma-registry/*)由 Host 内置路由直接分发,宿主零接线。
|
|
7
|
+
*
|
|
8
|
+
* 刻意不 import 'server-only':本桶会被宿主服务加载器(jiti)与单测在纯 Node
|
|
9
|
+
* 语境加载,server-only 在那里解析不了(soma-ai HttpGuards 同款实弹教训)。
|
|
10
|
+
* 客户端不引本桶由 eslint 客户端边界规则看守。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
PageRegistryStore,
|
|
15
|
+
PageRegistryError,
|
|
16
|
+
PageRegistryErrorCodes,
|
|
17
|
+
PAGE_NAME_PATTERN,
|
|
18
|
+
PAGE_APP_PATTERN,
|
|
19
|
+
PAGE_DECLARATION_MAX_BYTES,
|
|
20
|
+
declarationByteSize,
|
|
21
|
+
getPageRegistryStore,
|
|
22
|
+
resetPageRegistryStore,
|
|
23
|
+
} from './page-registry-store';
|
|
24
|
+
export type {
|
|
25
|
+
PageRegistryDeclaration,
|
|
26
|
+
PageRegistryErrorCode,
|
|
27
|
+
PageRegistryListItem,
|
|
28
|
+
PageRegistryListOptions,
|
|
29
|
+
PageRegistryListResult,
|
|
30
|
+
PageRegistryMeta,
|
|
31
|
+
PageRegistryPublishOptions,
|
|
32
|
+
PageRegistryStatus,
|
|
33
|
+
PageRegistryStoreOptions,
|
|
34
|
+
PageRegistryWriteOptions,
|
|
35
|
+
PageVersionRecord,
|
|
36
|
+
} from './page-registry-store';
|
|
37
|
+
export {
|
|
38
|
+
ConditionalPageRegistryRepository,
|
|
39
|
+
FilePageRegistryRepository,
|
|
40
|
+
getPageRegistryRepository,
|
|
41
|
+
InMemoryConditionalObjectStore,
|
|
42
|
+
setPageRegistryRepository,
|
|
43
|
+
useConditionalPageRegistryStore,
|
|
44
|
+
type PageRegistryRepository,
|
|
45
|
+
type PublishResult,
|
|
46
|
+
type RollbackResult,
|
|
47
|
+
} from './page-registry-repository';
|
|
48
|
+
export type {
|
|
49
|
+
ConditionalObjectStore,
|
|
50
|
+
ConditionalWriteResult,
|
|
51
|
+
ObjectRevision,
|
|
52
|
+
ObjectSnapshot,
|
|
53
|
+
WriteCondition,
|
|
54
|
+
} from './conditional-object-store';
|
|
55
|
+
export {
|
|
56
|
+
pageRegistryErrorResponse,
|
|
57
|
+
pageRegistrySuccessResponse,
|
|
58
|
+
createRegistryResponseBody,
|
|
59
|
+
PAGE_REGISTRY_SCHEMA_VERSION,
|
|
60
|
+
type RegistryResponseBody,
|
|
61
|
+
} from './registry-response';
|
|
62
|
+
export {
|
|
63
|
+
authorizePageRegistryWrite,
|
|
64
|
+
isRegistryReadPrivileged,
|
|
65
|
+
resolveWriteSubject,
|
|
66
|
+
runPageRegistryWrite,
|
|
67
|
+
resetRegistryWriteRateLimit,
|
|
68
|
+
REGISTRY_OPERATOR_HEADER,
|
|
69
|
+
type PageRegistryWriteContext,
|
|
70
|
+
type RegistryWriteRequestLike,
|
|
71
|
+
} from './registry-write-pipeline';
|
|
72
|
+
export {
|
|
73
|
+
hasConfiguredCredentials,
|
|
74
|
+
isRegistryDemoOpenDoor,
|
|
75
|
+
looksLikeProduction,
|
|
76
|
+
parseRegistryCredentials,
|
|
77
|
+
resetRegistrySubjectCache,
|
|
78
|
+
resolveRegistrySubject,
|
|
79
|
+
subjectHasScope,
|
|
80
|
+
timingSafeStringEqual,
|
|
81
|
+
REGISTRY_OPEN_ENV,
|
|
82
|
+
REGISTRY_TOKEN_ENV,
|
|
83
|
+
REGISTRY_TOKENS_ENV,
|
|
84
|
+
type RegistryScope,
|
|
85
|
+
type RegistrySubject,
|
|
86
|
+
type SubjectRequestLike,
|
|
87
|
+
} from './registry-subjects';
|
|
88
|
+
export {
|
|
89
|
+
setRegistryLockBackend,
|
|
90
|
+
withRegistryLock,
|
|
91
|
+
writeFileAtomic,
|
|
92
|
+
type RegistryLockBackend,
|
|
93
|
+
} from './registry-file-lock';
|
|
94
|
+
export {
|
|
95
|
+
readJsonWithLimit,
|
|
96
|
+
RequestBodyTooLargeError,
|
|
97
|
+
REGISTRY_WRITE_MAX_BYTES,
|
|
98
|
+
type BodyLimitedRequest,
|
|
99
|
+
} from './request-body-limit';
|
|
100
|
+
export {
|
|
101
|
+
precompileDeclaration,
|
|
102
|
+
isCompiledUsable,
|
|
103
|
+
DECLARATION_COMPILER_VERSION,
|
|
104
|
+
type CompiledDeclaration,
|
|
105
|
+
} from './declaration-precompiler';
|
|
106
|
+
export { scanDeclarationSafety, type ScannableDeclaration } from './validation/declaration-safety-scan';
|
|
107
|
+
export { RegistryError } from './registry-error';
|
|
108
|
+
export {
|
|
109
|
+
ApiRegistryStore,
|
|
110
|
+
ApiRegistryError,
|
|
111
|
+
ApiRegistryErrorCodes,
|
|
112
|
+
API_NAME_PATTERN,
|
|
113
|
+
API_APP_PATTERN,
|
|
114
|
+
API_METHODS,
|
|
115
|
+
API_RECORD_MAX_BYTES,
|
|
116
|
+
getApiRegistryStore,
|
|
117
|
+
resetApiRegistryStore,
|
|
118
|
+
type ApiMethod,
|
|
119
|
+
type ApiRecord,
|
|
120
|
+
type ApiRegisterInput,
|
|
121
|
+
type ApiRegistryErrorCode,
|
|
122
|
+
type ApiRegistryListItem,
|
|
123
|
+
type ApiRegistryListOptions,
|
|
124
|
+
type ApiRegistryListResult,
|
|
125
|
+
type ApiRegistryStoreOptions,
|
|
126
|
+
} from './api-registry-store';
|
|
127
|
+
export { scanCredentialLeak, type CredentialScannable } from './validation/credential-ban';
|
|
128
|
+
export { dryRunCompile, type CompilableDeclaration } from './validation/compile-gate';
|
|
129
|
+
export {
|
|
130
|
+
collectReferencedComponents,
|
|
131
|
+
scanUnknownComponents,
|
|
132
|
+
setKnownComponentsResolver,
|
|
133
|
+
type KnownComponentsResolver,
|
|
134
|
+
} from './validation/component-existence';
|
|
135
|
+
export {
|
|
136
|
+
collectDeclaredApiPaths,
|
|
137
|
+
crossCheckDeclaredApis,
|
|
138
|
+
normalizeApiPath,
|
|
139
|
+
setRegisteredPathsResolver,
|
|
140
|
+
type RegisteredPathsResolver,
|
|
141
|
+
} from './validation/cross-check';
|
|
142
|
+
export { resolveRegistryService } from './services/index';
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertPageName,
|
|
3
|
+
assertVersionNumber,
|
|
4
|
+
cloneRecord,
|
|
5
|
+
createPageVersionRecord,
|
|
6
|
+
normalizeDeclaration,
|
|
7
|
+
normalizePublishOptions,
|
|
8
|
+
normalizeStoredRecord,
|
|
9
|
+
PageRegistryError,
|
|
10
|
+
PageRegistryErrorCodes,
|
|
11
|
+
type PageRegistryDeclaration,
|
|
12
|
+
type PageRegistryListOptions,
|
|
13
|
+
type PageRegistryListResult,
|
|
14
|
+
type PageRegistryMeta,
|
|
15
|
+
type PageRegistryPublishOptions,
|
|
16
|
+
type PageRegistryStatus,
|
|
17
|
+
type PageRegistryWriteOptions,
|
|
18
|
+
type PageVersionRecord,
|
|
19
|
+
} from './page-registry-store';
|
|
20
|
+
import {
|
|
21
|
+
getPageRegistryStore,
|
|
22
|
+
type PageRegistryStore,
|
|
23
|
+
} from './page-registry-store';
|
|
24
|
+
import type { ConditionalObjectStore, WriteCondition } from './conditional-object-store';
|
|
25
|
+
import { withRegistryLock } from './registry-file-lock';
|
|
26
|
+
|
|
27
|
+
const MAX_CAS_ATTEMPTS = 5;
|
|
28
|
+
const AGGREGATE_SCHEMA_VERSION = 1;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 单页聚合对象。schemaVersion=1 与迁移前同构:旧数据里可能带 gray 字段
|
|
32
|
+
* (灰度能力已随 REQ-002 删除),读入时原样忽略,不迁移、不报错。
|
|
33
|
+
*/
|
|
34
|
+
interface PageAggregate {
|
|
35
|
+
schemaVersion: 1;
|
|
36
|
+
name: string;
|
|
37
|
+
versions: PageVersionRecord[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface PublishResult {
|
|
41
|
+
record: PageVersionRecord;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface RollbackResult {
|
|
45
|
+
record: PageVersionRecord;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** API 的唯一仓储门面;全部方法异步,后端可为共享文件系统或对象存储 CAS。 */
|
|
49
|
+
export interface PageRegistryRepository {
|
|
50
|
+
publish(
|
|
51
|
+
name: string,
|
|
52
|
+
declaration: unknown,
|
|
53
|
+
options?: PageRegistryStatus | PageRegistryPublishOptions,
|
|
54
|
+
): Promise<PublishResult>;
|
|
55
|
+
get(name: string, version?: number): Promise<PageVersionRecord>;
|
|
56
|
+
list(options?: PageRegistryListOptions): Promise<PageRegistryListResult>;
|
|
57
|
+
rollback(name: string, version: number, options?: PageRegistryWriteOptions): Promise<RollbackResult>;
|
|
58
|
+
archive(name: string, options?: PageRegistryWriteOptions): Promise<PageVersionRecord>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 默认共享 FS 适配器。所有写动作共用一把页面级 hardlink 锁,
|
|
63
|
+
* 跨文件的业务判断与提交不会互相穿透。
|
|
64
|
+
*/
|
|
65
|
+
export class FilePageRegistryRepository implements PageRegistryRepository {
|
|
66
|
+
constructor(private readonly injectedPages?: PageRegistryStore) {}
|
|
67
|
+
|
|
68
|
+
async publish(
|
|
69
|
+
name: string,
|
|
70
|
+
declaration: unknown,
|
|
71
|
+
options: PageRegistryStatus | PageRegistryPublishOptions = {},
|
|
72
|
+
): Promise<PublishResult> {
|
|
73
|
+
const safeName = assertPageName(name);
|
|
74
|
+
return withRegistryLock(this.assertSharedDirectory(), safeName, () => (
|
|
75
|
+
{ record: this.pages().publishWhileLocked(safeName, declaration, options) }
|
|
76
|
+
));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async get(name: string, version?: number): Promise<PageVersionRecord> {
|
|
80
|
+
return this.pages().get(name, version);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async list(options: PageRegistryListOptions = {}): Promise<PageRegistryListResult> {
|
|
84
|
+
return this.pages().list(options);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async rollback(
|
|
88
|
+
name: string,
|
|
89
|
+
version: number,
|
|
90
|
+
options: PageRegistryWriteOptions = {},
|
|
91
|
+
): Promise<RollbackResult> {
|
|
92
|
+
const safeName = assertPageName(name);
|
|
93
|
+
return withRegistryLock(this.assertSharedDirectory(), safeName, () => (
|
|
94
|
+
{ record: this.pages().rollbackWhileLocked(safeName, version, options) }
|
|
95
|
+
));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async archive(name: string, options: PageRegistryWriteOptions = {}): Promise<PageVersionRecord> {
|
|
99
|
+
const safeName = assertPageName(name);
|
|
100
|
+
return withRegistryLock(this.assertSharedDirectory(), safeName, () =>
|
|
101
|
+
this.pages().archiveWhileLocked(safeName, options));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private pages(): PageRegistryStore {
|
|
105
|
+
return this.injectedPages ?? getPageRegistryStore();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private assertSharedDirectory(): string {
|
|
109
|
+
return this.pages().storageDirectory();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 单页聚合 CAS:整页版本数组在同一个对象中提交,消除跨键检查—写入竞态。 */
|
|
114
|
+
export class ConditionalPageRegistryRepository implements PageRegistryRepository {
|
|
115
|
+
constructor(
|
|
116
|
+
private readonly objects: ConditionalObjectStore,
|
|
117
|
+
private readonly options: {
|
|
118
|
+
prefix?: string;
|
|
119
|
+
now?: () => number;
|
|
120
|
+
maxAttempts?: number;
|
|
121
|
+
} = {},
|
|
122
|
+
) {}
|
|
123
|
+
|
|
124
|
+
async publish(
|
|
125
|
+
name: string,
|
|
126
|
+
declaration: unknown,
|
|
127
|
+
options: PageRegistryStatus | PageRegistryPublishOptions = {},
|
|
128
|
+
): Promise<PublishResult> {
|
|
129
|
+
const safeName = assertPageName(name);
|
|
130
|
+
const normalizedOptions = normalizePublishOptions(options);
|
|
131
|
+
const normalized = normalizeDeclaration(declaration);
|
|
132
|
+
return this.mutate(safeName, (aggregate) => {
|
|
133
|
+
const record = createPageVersionRecord(safeName, aggregate.versions, {
|
|
134
|
+
declaration: normalized,
|
|
135
|
+
status: normalizedOptions.status ?? 'published',
|
|
136
|
+
app: normalizedOptions.app ?? 'default',
|
|
137
|
+
tags: normalizedOptions.tags ?? [],
|
|
138
|
+
meta: normalizedOptions.meta,
|
|
139
|
+
operator: normalizedOptions.operator,
|
|
140
|
+
}, this.now());
|
|
141
|
+
aggregate.versions.push(record);
|
|
142
|
+
return { value: { record: cloneRecord(record) }, aggregate };
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async get(name: string, version?: number): Promise<PageVersionRecord> {
|
|
147
|
+
const safeName = assertPageName(name);
|
|
148
|
+
const aggregate = (await this.readAggregate(safeName)).aggregate;
|
|
149
|
+
return selectVersion(safeName, aggregate.versions, version);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async list(options: PageRegistryListOptions = {}): Promise<PageRegistryListResult> {
|
|
153
|
+
const keys = await this.objects.list(this.prefix());
|
|
154
|
+
const items = [];
|
|
155
|
+
const keyword = options.keyword?.trim().toLowerCase() ?? '';
|
|
156
|
+
for (const key of keys) {
|
|
157
|
+
if (!key.endsWith('.page.json')) continue;
|
|
158
|
+
const snapshot = await this.objects.read(key);
|
|
159
|
+
if (!snapshot) continue;
|
|
160
|
+
const aggregate = parseAggregate(snapshot.text, key);
|
|
161
|
+
const latest = aggregate.versions[aggregate.versions.length - 1];
|
|
162
|
+
if (!latest) continue;
|
|
163
|
+
if (options.publishedOnly && latest.status !== 'published') continue;
|
|
164
|
+
if (!options.includeArchived && latest.status === 'archived') continue;
|
|
165
|
+
if (options.app !== undefined && latest.app !== options.app) continue;
|
|
166
|
+
const title = latest.meta?.title;
|
|
167
|
+
if (keyword) {
|
|
168
|
+
const haystack = [aggregate.name, title ?? '', latest.meta?.description ?? ''].join('\n').toLowerCase();
|
|
169
|
+
if (!haystack.includes(keyword)) continue;
|
|
170
|
+
}
|
|
171
|
+
items.push({
|
|
172
|
+
name: aggregate.name,
|
|
173
|
+
latestVersion: latest.version,
|
|
174
|
+
updatedAt: latest.createdAt,
|
|
175
|
+
status: latest.status,
|
|
176
|
+
app: latest.app,
|
|
177
|
+
tags: [...latest.tags],
|
|
178
|
+
...(title ? { title } : {}),
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
items.sort((left, right) => left.name.localeCompare(right.name));
|
|
182
|
+
const total = items.length;
|
|
183
|
+
const offset = validPageParam(options.offset) ?? 0;
|
|
184
|
+
const limit = validPageParam(options.limit);
|
|
185
|
+
return { pages: items.slice(offset, limit === undefined ? undefined : offset + limit), total };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async rollback(
|
|
189
|
+
name: string,
|
|
190
|
+
version: number,
|
|
191
|
+
options: PageRegistryWriteOptions = {},
|
|
192
|
+
): Promise<RollbackResult> {
|
|
193
|
+
const safeName = assertPageName(name);
|
|
194
|
+
assertVersionNumber(version);
|
|
195
|
+
return this.mutate(safeName, (aggregate) => {
|
|
196
|
+
const target = findVersion(safeName, aggregate.versions, version, '无法回滚');
|
|
197
|
+
const record = copyAsPublished(safeName, aggregate.versions, target, options.operator, this.now());
|
|
198
|
+
aggregate.versions.push(record);
|
|
199
|
+
return { value: { record: cloneRecord(record) }, aggregate };
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async archive(name: string, options: PageRegistryWriteOptions = {}): Promise<PageVersionRecord> {
|
|
204
|
+
const safeName = assertPageName(name);
|
|
205
|
+
return this.mutate(safeName, (aggregate) => {
|
|
206
|
+
const latest = aggregate.versions[aggregate.versions.length - 1];
|
|
207
|
+
if (!latest) throw new PageRegistryError(PageRegistryErrorCodes.PAGE_NOT_FOUND, `页面不存在:${safeName}`);
|
|
208
|
+
if (latest.status === 'archived') {
|
|
209
|
+
throw new PageRegistryError(PageRegistryErrorCodes.PAGE_STATUS_INVALID, `页面 ${safeName} 已是归档状态`);
|
|
210
|
+
}
|
|
211
|
+
const record = createPageVersionRecord(safeName, aggregate.versions, {
|
|
212
|
+
declaration: latest.declaration,
|
|
213
|
+
status: 'archived',
|
|
214
|
+
app: latest.app,
|
|
215
|
+
tags: latest.tags,
|
|
216
|
+
meta: latest.meta,
|
|
217
|
+
operator: options.operator,
|
|
218
|
+
}, this.now());
|
|
219
|
+
aggregate.versions.push(record);
|
|
220
|
+
return { value: cloneRecord(record), aggregate };
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private async mutate<T>(
|
|
225
|
+
name: string,
|
|
226
|
+
change: (aggregate: PageAggregate) => { value: T; aggregate: PageAggregate },
|
|
227
|
+
): Promise<T> {
|
|
228
|
+
const attempts = this.options.maxAttempts ?? MAX_CAS_ATTEMPTS;
|
|
229
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
230
|
+
const current = await this.readAggregate(name);
|
|
231
|
+
const working = cloneAggregate(current.aggregate);
|
|
232
|
+
const changed = change(working);
|
|
233
|
+
const expected: WriteCondition = current.revision === null
|
|
234
|
+
? { kind: 'absent' }
|
|
235
|
+
: { kind: 'revision', revision: current.revision };
|
|
236
|
+
const committed = await this.objects.compareAndSet(
|
|
237
|
+
this.keyOf(name),
|
|
238
|
+
expected,
|
|
239
|
+
`${JSON.stringify(changed.aggregate)}\n`,
|
|
240
|
+
);
|
|
241
|
+
if (committed.committed) return changed.value;
|
|
242
|
+
}
|
|
243
|
+
throw new PageRegistryError(
|
|
244
|
+
PageRegistryErrorCodes.PAGE_WRITE_CONFLICT,
|
|
245
|
+
`页面 ${name} 在 ${attempts} 次条件写重试内持续冲突,请重新读取后重试`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private async readAggregate(name: string): Promise<{ aggregate: PageAggregate; revision: string | null }> {
|
|
250
|
+
const snapshot = await this.objects.read(this.keyOf(name));
|
|
251
|
+
if (!snapshot) {
|
|
252
|
+
return {
|
|
253
|
+
aggregate: { schemaVersion: AGGREGATE_SCHEMA_VERSION, name, versions: [] },
|
|
254
|
+
revision: null,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return { aggregate: parseAggregate(snapshot.text, snapshot.key), revision: snapshot.revision };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private keyOf(name: string): string {
|
|
261
|
+
return `${this.prefix()}${name}.page.json`;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private prefix(): string {
|
|
265
|
+
const raw = this.options.prefix?.trim() ?? '';
|
|
266
|
+
return raw === '' || raw.endsWith('/') ? raw : `${raw}/`;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private now(): number {
|
|
270
|
+
return (this.options.now ?? Date.now)();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function parseAggregate(text: string, key: string): PageAggregate {
|
|
275
|
+
let parsed: unknown;
|
|
276
|
+
try {
|
|
277
|
+
parsed = JSON.parse(text);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
throw new Error(`[PageRegistryRepository] 聚合对象损坏(非法 JSON):${key}`, { cause: error });
|
|
280
|
+
}
|
|
281
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
282
|
+
throw new Error(`[PageRegistryRepository] 聚合对象损坏(应为对象):${key}`);
|
|
283
|
+
}
|
|
284
|
+
const raw = parsed as Record<string, unknown>;
|
|
285
|
+
if (raw.schemaVersion !== 1 || typeof raw.name !== 'string' || !Array.isArray(raw.versions)) {
|
|
286
|
+
throw new Error(`[PageRegistryRepository] 聚合对象结构无效:${key}`);
|
|
287
|
+
}
|
|
288
|
+
// 旧数据可能带 gray 字段(灰度已删),原样丢弃即可,版本数组不受影响
|
|
289
|
+
return {
|
|
290
|
+
schemaVersion: 1,
|
|
291
|
+
name: raw.name,
|
|
292
|
+
versions: (raw.versions as Array<Record<string, unknown>>).map(normalizeStoredRecord),
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function cloneAggregate(aggregate: PageAggregate): PageAggregate {
|
|
297
|
+
return JSON.parse(JSON.stringify(aggregate)) as PageAggregate;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function selectVersion(name: string, versions: PageVersionRecord[], version?: number): PageVersionRecord {
|
|
301
|
+
if (versions.length === 0) throw new PageRegistryError(PageRegistryErrorCodes.PAGE_NOT_FOUND, `页面不存在:${name}`);
|
|
302
|
+
if (version !== undefined) return cloneRecord(findVersion(name, versions, version, ''));
|
|
303
|
+
for (let index = versions.length - 1; index >= 0; index -= 1) {
|
|
304
|
+
if (versions[index].status === 'archived') {
|
|
305
|
+
throw new PageRegistryError(PageRegistryErrorCodes.PAGE_NOT_FOUND, `页面 ${name} 已归档(历史版本可按版本号查看,rollback 旧版本可恢复上架)`);
|
|
306
|
+
}
|
|
307
|
+
if (versions[index].status === 'published') return cloneRecord(versions[index]);
|
|
308
|
+
}
|
|
309
|
+
throw new PageRegistryError(PageRegistryErrorCodes.PAGE_NOT_FOUND, `页面 ${name} 没有已发布版本`);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function findVersion(
|
|
313
|
+
name: string,
|
|
314
|
+
versions: PageVersionRecord[],
|
|
315
|
+
version: number,
|
|
316
|
+
action: string,
|
|
317
|
+
): PageVersionRecord {
|
|
318
|
+
assertVersionNumber(version);
|
|
319
|
+
if (versions.length === 0) throw new PageRegistryError(PageRegistryErrorCodes.PAGE_NOT_FOUND, `页面不存在:${name}`);
|
|
320
|
+
const target = versions.find((item) => item.version === version);
|
|
321
|
+
if (!target) {
|
|
322
|
+
throw new PageRegistryError(
|
|
323
|
+
PageRegistryErrorCodes.PAGE_VERSION_NOT_FOUND,
|
|
324
|
+
`页面 ${name} 不存在版本 ${version}${action ? `,${action}` : ''}`,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
return target;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function copyAsPublished(
|
|
331
|
+
name: string,
|
|
332
|
+
versions: PageVersionRecord[],
|
|
333
|
+
target: PageVersionRecord,
|
|
334
|
+
operator: string | undefined,
|
|
335
|
+
now: number,
|
|
336
|
+
): PageVersionRecord {
|
|
337
|
+
return createPageVersionRecord(name, versions, {
|
|
338
|
+
declaration: target.declaration,
|
|
339
|
+
status: 'published',
|
|
340
|
+
app: target.app,
|
|
341
|
+
tags: target.tags,
|
|
342
|
+
meta: target.meta,
|
|
343
|
+
operator,
|
|
344
|
+
}, now);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function validPageParam(value: unknown): number | undefined {
|
|
348
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0 ? value : undefined;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let sharedRepository: PageRegistryRepository | undefined;
|
|
352
|
+
|
|
353
|
+
export function getPageRegistryRepository(): PageRegistryRepository {
|
|
354
|
+
sharedRepository ??= new FilePageRegistryRepository();
|
|
355
|
+
return sharedRepository;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function setPageRegistryRepository(repository: PageRegistryRepository | null): void {
|
|
359
|
+
sharedRepository = repository ?? undefined;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export function useConditionalPageRegistryStore(
|
|
363
|
+
store: ConditionalObjectStore,
|
|
364
|
+
options: ConstructorParameters<typeof ConditionalPageRegistryRepository>[1] = {},
|
|
365
|
+
): ConditionalPageRegistryRepository {
|
|
366
|
+
const repository = new ConditionalPageRegistryRepository(store, options);
|
|
367
|
+
setPageRegistryRepository(repository);
|
|
368
|
+
return repository;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export type { ConditionalObjectStore } from './conditional-object-store';
|
|
372
|
+
export { InMemoryConditionalObjectStore } from './in-memory-conditional-object-store';
|
|
373
|
+
export type { PageRegistryDeclaration, PageRegistryMeta };
|