soma-core 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -0
- package/app/SomaPageClient.tsx +22 -68
- 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/bin/soma-lsp.cjs +1 -4
- package/core/build/compiler/full-manifest-builder.ts +19 -23
- package/core/build/compiler/html-include-expander.ts +56 -0
- package/core/build/compiler/page-assets.ts +26 -0
- package/core/build/compiler/page-declaration-compiler.ts +85 -0
- package/core/build/compiler/rollup-plugin-soma.ts +4 -13
- package/core/build/compiler/route-builder.ts +0 -2
- package/core/build/compiler/rspack-plugin-soma.js +1 -10
- package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -9
- package/core/build/compiler/soma-precompile.js +3581 -2387
- package/core/build/compiler/soma-precompile.ts +44 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +5 -8
- package/core/build/compiler/soma-webpack-loader.js +6 -15
- package/core/build/compiler/vite-plugin-soma.ts +9 -21
- package/core/build/component/pack-from-dir.ts +58 -0
- package/core/build/component/package-loader.ts +281 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +133 -0
- package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
- package/core/build/lsp/extra-completion.cjs +14 -8
- package/core/build/lsp/lsp-navigation.cjs +50 -43
- package/core/build/lsp/soma-declaration.cjs +85 -0
- package/core/build/lsp/ui-yaml-completion.cjs +98 -104
- package/core/build/v2/block-compile.ts +200 -0
- package/core/build/v2/block-loader.ts +33 -0
- package/core/build/v2/html-index.ts +120 -0
- package/core/build/v2/migrate-v1.ts +370 -0
- package/core/build/v2/soma-file-loader.ts +216 -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 +277 -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 +445 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/contract-compat.ts +132 -0
- package/core/client/component/example-v2.ts +132 -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 +249 -24
- 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 +11 -10
- 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 +380 -7
- package/core/client/engine/style-binder.ts +47 -1
- 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/parser/v2/namespace.ts +182 -0
- package/core/client/parser/v2/page-parser.ts +359 -0
- package/core/client/parser/v2/use-parser.ts +412 -0
- package/core/client/parser/value-lang.ts +340 -0
- package/core/client/pipes/pipe-registry.ts +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +161 -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/harmony.ts +307 -0
- package/core/client/theme/scene-tokens.ts +134 -0
- package/core/client/theme/theme-axes.ts +228 -0
- package/core/client/theme/theme-engine.ts +1196 -0
- package/core/client/theme/tone-solver.ts +118 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +262 -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 +34 -7
- package/core/server/host/soma-page-compile.ts +33 -0
- package/core/server/index.ts +45 -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 +99 -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 +932 -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 +96 -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 +54 -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 +67 -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 +120 -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 +7811 -1380
- package/dist/types/build/compiler/full-manifest-builder.d.ts +6 -2
- package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
- package/dist/types/build/compiler/page-assets.d.ts +3 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -2
- package/dist/types/build/compiler/route-builder.d.ts +0 -1
- package/dist/types/build/compiler/soma-precompile.d.ts +17 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -7
- package/dist/types/build/component/pack-from-dir.d.ts +17 -0
- package/dist/types/build/component/package-loader.d.ts +40 -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/build/v2/block-compile.d.ts +28 -0
- package/dist/types/build/v2/block-loader.d.ts +9 -0
- package/dist/types/build/v2/html-index.d.ts +37 -0
- package/dist/types/build/v2/migrate-v1.d.ts +32 -0
- package/dist/types/build/v2/soma-file-loader.d.ts +45 -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 +110 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- package/dist/types/client/component/contract-compat.d.ts +16 -0
- package/dist/types/client/component/example-v2.d.ts +9 -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 +51 -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 +118 -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/parser/v2/namespace.d.ts +52 -0
- package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
- package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
- package/dist/types/client/parser/value-lang.d.ts +63 -0
- package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +50 -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/harmony.d.ts +77 -0
- package/dist/types/client/theme/scene-tokens.d.ts +25 -0
- package/dist/types/client/theme/theme-axes.d.ts +81 -0
- package/dist/types/client/theme/theme-engine.d.ts +77 -0
- package/dist/types/client/theme/tone-solver.d.ts +47 -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 +47 -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 +2 -0
- package/dist/types/server/host/soma-page-compile.d.ts +7 -0
- package/dist/types/server/index.d.ts +6 -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 +12 -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 +78 -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/editor/vscode/package.json +1 -24
- package/global.d.ts +5 -11
- package/next.config.js +85 -28
- package/package.json +33 -11
- package/proxy.ts +62 -0
- package/tailwind.config.ts +0 -1
- package/core/build/lsp/event-yaml-completion.cjs +0 -106
- 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/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
- package/middleware.ts +0 -33
|
@@ -0,0 +1,932 @@
|
|
|
1
|
+
// 页面声明仓库:AI 生成的页面声明存服务端,按名拉取渲染,发布不随前端发版。
|
|
2
|
+
//
|
|
3
|
+
// 存储模型:
|
|
4
|
+
// - 一个页面名对应一个 <name>.json 文件,内容是该页面全部版本的数组(按 version 升序);
|
|
5
|
+
// - 版本号自增整数,只增不减;rollback 不删历史,而是把旧版本复制为新版本发布;
|
|
6
|
+
// - 内存缓存 + 写穿:读优先走缓存,写时同步落盘并刷新缓存;
|
|
7
|
+
// - 时间由构造函数注入 now()(默认 Date.now),测试可替换为确定性时钟。
|
|
8
|
+
//
|
|
9
|
+
// 平台化拓展位(P-A 落地,P-B/P-C 零迁移生长):
|
|
10
|
+
// - 生命周期三态:published / draft / archived(归档 = 追加一条 archived 墓碑记录,
|
|
11
|
+
// 缺省 list 消失、缺省 get 404,但全部历史仍可按版本查;rollback 旧版即恢复上架);
|
|
12
|
+
// - 页面元数据 meta(title/描述/来源需求文本/建议路由)随版本记录存储;
|
|
13
|
+
// - declaration 只接受 HTML + 一份统一 SOMA DSL;事件、状态样式与数据对象均内嵌;
|
|
14
|
+
// - 多应用租户预留:记录带 app(应用命名空间,缺省 'default')与 tags,
|
|
15
|
+
// P-C 多应用来时存量数据零迁移;
|
|
16
|
+
// - list 走分页协议 { pages, total },支持 app / 关键词过滤与 limit/offset;
|
|
17
|
+
// - 审计预留:写操作可带 operator(操作者标识),P-C 审计日志直接取用;
|
|
18
|
+
// - 向后兼容:存量 JSON(无 app/tags/meta 的旧记录)读入时自动归一化补缺省值。
|
|
19
|
+
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
import { precompileDeclaration, type CompiledDeclaration } from './declaration-precompiler';
|
|
22
|
+
import { withRegistryLock, writeFileAtomic } from './registry-file-lock';
|
|
23
|
+
import { RegistryError } from './registry-error';
|
|
24
|
+
import path from 'node:path';
|
|
25
|
+
import { scanDeclarationSafety } from './validation/declaration-safety-scan';
|
|
26
|
+
import { crossCheckDeclaredApis } from './validation/cross-check';
|
|
27
|
+
import { dryRunCompile } from './validation/compile-gate';
|
|
28
|
+
import { scanUnknownComponents } from './validation/component-existence';
|
|
29
|
+
import { compilePageDeclaration } from '../../build/compiler/page-declaration-compiler';
|
|
30
|
+
|
|
31
|
+
/** 页面名约束:小写字母 / 数字 / 连字符,1~64 位(可直接用作文件名与 URL 片段) */
|
|
32
|
+
export const PAGE_NAME_PATTERN = /^[a-z0-9-]{1,64}$/;
|
|
33
|
+
|
|
34
|
+
/** 应用命名空间约束:与页面名同形(略短),缺省 'default' */
|
|
35
|
+
export const PAGE_APP_PATTERN = /^[a-z0-9-]{1,32}$/;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 单版本声明体积上限 = 131,072 字节(128KiB)。
|
|
39
|
+
*
|
|
40
|
+
* 为什么必须有:仓库现在能收任意大的声明,而一份声明是**整份取走、整份运行时编译**的——
|
|
41
|
+
* 取页是一个 GET 拉全文,渲染前要把 html + 四份 YAML 全部解析编译一遍(§6.4.1 段二)。
|
|
42
|
+
* 一份超大声明既撑爆取页响应,又把编译段从毫秒级拖到秒级,且失败方式是"整页变慢"而不是报错。
|
|
43
|
+
* 上限要卡在落库这一刀,因为落库之后每一次取页都要为它付一遍代价。
|
|
44
|
+
*
|
|
45
|
+
* 口径:declaration 里全部字符串字段的 UTF-8 字节和(html + uiYaml + eventYaml +
|
|
46
|
+
* styleYaml + dataYaml + notes)。不是只算四件套,因为它们同在一个 JSON 里被取页整份拉走,
|
|
47
|
+
* notes 塞满一样会撑爆响应。
|
|
48
|
+
*
|
|
49
|
+
* 128KiB 这个数的依据(实测,2026-08-12):
|
|
50
|
+
* - 仓库里**最大的真实声明**是 ai-demo 的 gallery 页(html 13,826 + ui 4,407 + event 1,805)
|
|
51
|
+
* = 20,038 字节。128KiB / 20,038 ≈ 6.5×,即人手写得最复杂的整站页面还能再翻 6.5 倍。
|
|
52
|
+
* - 声明仓库里 119 份 AI 生成版本的实测分布:中位 1,777 / p90 2,305 / 最大 2,995 字节,
|
|
53
|
+
* 离上限有 43 倍余量——正常生成永远撞不到这条线。
|
|
54
|
+
* - 上限必须高于生成链**物理上能产出的最大值**,否则会误杀合法生成:
|
|
55
|
+
* PageGenerator 的 MAX_OUTPUT_TOKENS = 24,576,按 HTML/YAML 约 4 字节/token 折算上限约 98KB,
|
|
56
|
+
* 128KiB 正好压在它之上。
|
|
57
|
+
* 结论:撞到这条线的只可能是异常声明(把数据表内联进 html、把图片 base64 塞进声明),
|
|
58
|
+
* 那正是要拒绝的东西。
|
|
59
|
+
*/
|
|
60
|
+
export const PAGE_DECLARATION_MAX_BYTES = 131_072;
|
|
61
|
+
|
|
62
|
+
/** 页面声明仓库错误码(延续 AiErrorCodes 的 45xx 段,占用 452x) */
|
|
63
|
+
export const PageRegistryErrorCodes = {
|
|
64
|
+
/** 页面名不符合 [a-z0-9-]{1,64} */
|
|
65
|
+
PAGE_NAME_INVALID: 4521,
|
|
66
|
+
/** 声明缺少必填字段或字段类型不对 */
|
|
67
|
+
PAGE_DECLARATION_INVALID: 4522,
|
|
68
|
+
/** 页面不存在(或没有已发布版本 / 已归档) */
|
|
69
|
+
PAGE_NOT_FOUND: 4523,
|
|
70
|
+
/** 指定版本不存在 */
|
|
71
|
+
PAGE_VERSION_NOT_FOUND: 4524,
|
|
72
|
+
/** 写操作鉴权失败(SOMA_REGISTRY_TOKEN 设定时缺 token 或 token 不符) */
|
|
73
|
+
PAGE_WRITE_UNAUTHORIZED: 4525,
|
|
74
|
+
/** 状态机拒绝:archive 目标已是归档等 */
|
|
75
|
+
PAGE_STATUS_INVALID: 4526,
|
|
76
|
+
/** 条件写在限定重试内持续冲突;调用方应重新读取后重试 */
|
|
77
|
+
PAGE_WRITE_CONFLICT: 4527,
|
|
78
|
+
/** 写操作触发按主体限流;稍后重试 */
|
|
79
|
+
PAGE_WRITE_RATE_LIMITED: 4528,
|
|
80
|
+
} as const;
|
|
81
|
+
|
|
82
|
+
export type PageRegistryErrorCode =
|
|
83
|
+
(typeof PageRegistryErrorCodes)[keyof typeof PageRegistryErrorCodes];
|
|
84
|
+
|
|
85
|
+
export class PageRegistryError extends RegistryError {
|
|
86
|
+
declare readonly code: PageRegistryErrorCode;
|
|
87
|
+
|
|
88
|
+
constructor(code: PageRegistryErrorCode, message: string) {
|
|
89
|
+
super(code, message);
|
|
90
|
+
this.name = 'PageRegistryError';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 页面声明本体:与 /api/ai-page/generate 的产物同构(dataYaml 为数据对象绑定拓展位) */
|
|
95
|
+
export interface PageRegistryDeclaration {
|
|
96
|
+
html: string;
|
|
97
|
+
uiYaml: string;
|
|
98
|
+
eventYaml: string;
|
|
99
|
+
styleYaml?: string;
|
|
100
|
+
/** 可选第五件:数据对象声明(*_data.yaml 原文),页面与数据绑定自含于声明 */
|
|
101
|
+
dataYaml?: string;
|
|
102
|
+
notes?: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 页面元数据:管理面展示与路由建议的落点(全部可选,字段级只增不改) */
|
|
106
|
+
export interface PageRegistryMeta {
|
|
107
|
+
/** 页面标题(管理面清单列) */
|
|
108
|
+
title?: string;
|
|
109
|
+
/** 页面描述 / 备注 */
|
|
110
|
+
description?: string;
|
|
111
|
+
/** 来源需求原文(对话生成时用户说的那句话) */
|
|
112
|
+
sourceRequirement?: string;
|
|
113
|
+
/** 建议路由(平台只记录,挂载由业务系统决定) */
|
|
114
|
+
suggestedRoute?: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type PageRegistryStatus = 'published' | 'draft' | 'archived';
|
|
118
|
+
|
|
119
|
+
/** 存储单元:一个页面的一个版本 */
|
|
120
|
+
export interface PageVersionRecord {
|
|
121
|
+
name: string;
|
|
122
|
+
version: number;
|
|
123
|
+
declaration: PageRegistryDeclaration;
|
|
124
|
+
status: PageRegistryStatus;
|
|
125
|
+
createdAt: number;
|
|
126
|
+
/** 应用命名空间(多应用租户拓展位,缺省 'default') */
|
|
127
|
+
app: string;
|
|
128
|
+
/** 标签(目录化 / 过滤拓展位,缺省 []) */
|
|
129
|
+
tags: string[];
|
|
130
|
+
/** 页面元数据(可选) */
|
|
131
|
+
meta?: PageRegistryMeta;
|
|
132
|
+
/** 操作者标识(审计拓展位,可选) */
|
|
133
|
+
operator?: string;
|
|
134
|
+
/**
|
|
135
|
+
* 发布期预编译产物(P-a,可选):把 uiYaml/eventYaml 等的解析结果落库,
|
|
136
|
+
* 取页时客户端直接消费;浏览器不解析源声明。
|
|
137
|
+
* **它是缓存不是真理**——带 compilerVersion,消费端版本对不上就整份丢弃、
|
|
138
|
+
* 由服务端从统一源声明重新编译(见 DeclarationPrecompiler.isCompiledUsable)。
|
|
139
|
+
*/
|
|
140
|
+
compiled?: CompiledDeclaration;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** publish 的可选项(第三参也兼容旧的裸 status 字符串写法) */
|
|
144
|
+
export interface PageRegistryPublishOptions {
|
|
145
|
+
/** 生命周期状态:publish 只接受 published / draft(archived 走 archive()) */
|
|
146
|
+
status?: Extract<PageRegistryStatus, 'published' | 'draft'>;
|
|
147
|
+
meta?: PageRegistryMeta;
|
|
148
|
+
app?: string;
|
|
149
|
+
tags?: string[];
|
|
150
|
+
operator?: string;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 只带操作者标识的写操作可选项(rollback / archive 共用) */
|
|
154
|
+
export interface PageRegistryWriteOptions {
|
|
155
|
+
operator?: string;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** 追加新版本时的规范化字段;文件后端与条件写后端共用同一状态机输入。 */
|
|
159
|
+
export interface PageVersionFields {
|
|
160
|
+
declaration: PageRegistryDeclaration;
|
|
161
|
+
status: PageRegistryStatus;
|
|
162
|
+
app: string;
|
|
163
|
+
tags: string[];
|
|
164
|
+
meta?: PageRegistryMeta;
|
|
165
|
+
operator?: string;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** list() 返回的摘要条目(速记列直接可渲:状态 / 应用 / 标签 / 标题齐备) */
|
|
169
|
+
export interface PageRegistryListItem {
|
|
170
|
+
name: string;
|
|
171
|
+
latestVersion: number;
|
|
172
|
+
updatedAt: number;
|
|
173
|
+
/** 最新版本的状态(archived 即整页已归档) */
|
|
174
|
+
status: PageRegistryStatus;
|
|
175
|
+
app: string;
|
|
176
|
+
tags: string[];
|
|
177
|
+
title?: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** list() 过滤与分页选项(P-C 多应用 / 检索的协议位,现在即生效) */
|
|
181
|
+
export interface PageRegistryListOptions {
|
|
182
|
+
/** 只看某应用命名空间 */
|
|
183
|
+
app?: string;
|
|
184
|
+
/** 关键词:命中页面名 / meta.title / meta.description(不区分大小写) */
|
|
185
|
+
keyword?: string;
|
|
186
|
+
/** 是否包含已归档页面(缺省 false:归档页从清单消失) */
|
|
187
|
+
includeArchived?: boolean;
|
|
188
|
+
/**
|
|
189
|
+
* 只列已发布页面(匿名调用方的缺省视角)。
|
|
190
|
+
*
|
|
191
|
+
* 为什么是"过滤"而不是"拒绝整个端点":清单是产品能力——画廊要靠它列页面,
|
|
192
|
+
* 而浏览器里的画廊拿不到管理凭据。给端点加 401 会让配了 token 的部署里
|
|
193
|
+
* 画廊直接瞎掉;按主体过滤则两边都成立:匿名看到产品,管理者看到全部。
|
|
194
|
+
*/
|
|
195
|
+
publishedOnly?: boolean;
|
|
196
|
+
/** 分页:跳过条数(缺省 0) */
|
|
197
|
+
offset?: number;
|
|
198
|
+
/** 分页:最多返回条数(缺省不限) */
|
|
199
|
+
limit?: number;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** list() 分页协议:pages 为当前页,total 为过滤后的总数 */
|
|
203
|
+
export interface PageRegistryListResult {
|
|
204
|
+
pages: PageRegistryListItem[];
|
|
205
|
+
total: number;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface PageRegistryStoreOptions {
|
|
209
|
+
/** 持久化目录(每页面一个 <name>.json),默认 data/soma-runtime/pages */
|
|
210
|
+
baseDir?: string;
|
|
211
|
+
/** 时钟注入点,默认 Date.now;测试传确定性时钟 */
|
|
212
|
+
now?: () => number;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export class PageRegistryStore {
|
|
216
|
+
private readonly baseDir: string;
|
|
217
|
+
private readonly now: () => number;
|
|
218
|
+
/**
|
|
219
|
+
* 内存缓存:页面名 → { 版本数组, 落盘指纹 }。
|
|
220
|
+
*
|
|
221
|
+
* 指纹(mtimeMs + size)是修"跨实例读旧版"那条缺陷的关键:先前只要缓存命中就直接返回、
|
|
222
|
+
* 从不看文件,别的进程发了新版本,本实例的用户会**永远**停在旧版上。
|
|
223
|
+
* 每次 load 先 stat 一下(一次 syscall,远小于解析整份 JSON),指纹变了就重读。
|
|
224
|
+
*/
|
|
225
|
+
private readonly cache = new Map<string, { versions: PageVersionRecord[]; stamp: string }>();
|
|
226
|
+
|
|
227
|
+
constructor(options: PageRegistryStoreOptions = {}) {
|
|
228
|
+
this.baseDir = path.resolve(options.baseDir ?? path.join('data', 'soma-runtime', 'pages'));
|
|
229
|
+
this.now = options.now ?? Date.now;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 供共享 FS 聚合协调器使用;对象存储模式不依赖本地路径。 */
|
|
233
|
+
storageDirectory(): string {
|
|
234
|
+
return this.baseDir;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* 发布一个新版本(版本号自增),返回新版本记录。
|
|
239
|
+
* 第三参兼容两种写法:裸 status 字符串(旧调用姿势)或选项对象(元数据 / app / tags / operator)。
|
|
240
|
+
*/
|
|
241
|
+
publish(
|
|
242
|
+
name: string,
|
|
243
|
+
declaration: unknown,
|
|
244
|
+
options: PageRegistryStatus | PageRegistryPublishOptions = {},
|
|
245
|
+
): PageVersionRecord {
|
|
246
|
+
const safeName = assertPageName(name);
|
|
247
|
+
return withRegistryLock(this.baseDir, safeName, () =>
|
|
248
|
+
this.publishWhileLocked(safeName, declaration, options));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
publishWhileLocked(
|
|
252
|
+
name: string,
|
|
253
|
+
declaration: unknown,
|
|
254
|
+
options: PageRegistryStatus | PageRegistryPublishOptions = {},
|
|
255
|
+
): PageVersionRecord {
|
|
256
|
+
const safeName = assertPageName(name);
|
|
257
|
+
const normalizedOptions = normalizePublishOptions(options);
|
|
258
|
+
const normalized = normalizeDeclaration(declaration);
|
|
259
|
+
this.cache.delete(safeName);
|
|
260
|
+
return this.appendLocked(safeName, {
|
|
261
|
+
declaration: normalized,
|
|
262
|
+
status: normalizedOptions.status ?? 'published',
|
|
263
|
+
app: normalizedOptions.app ?? 'default',
|
|
264
|
+
tags: normalizedOptions.tags ?? [],
|
|
265
|
+
meta: normalizedOptions.meta,
|
|
266
|
+
operator: normalizedOptions.operator,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 取指定版本;缺省版本号时取最新可用 published 版本。
|
|
272
|
+
* 缺省语义(下架即不可拉):从最新往旧扫,先撞到 archived 墓碑记录 → 页面已归档,404;
|
|
273
|
+
* 跳过 draft;命中 published 即返回。
|
|
274
|
+
*/
|
|
275
|
+
get(name: string, version?: number): PageVersionRecord {
|
|
276
|
+
const safeName = assertPageName(name);
|
|
277
|
+
const versions = this.load(safeName);
|
|
278
|
+
if (versions.length === 0) {
|
|
279
|
+
throw new PageRegistryError(
|
|
280
|
+
PageRegistryErrorCodes.PAGE_NOT_FOUND,
|
|
281
|
+
`页面不存在:${safeName}`,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
if (version !== undefined) {
|
|
285
|
+
assertVersionNumber(version);
|
|
286
|
+
const record = versions.find((item) => item.version === version);
|
|
287
|
+
if (!record) {
|
|
288
|
+
throw new PageRegistryError(
|
|
289
|
+
PageRegistryErrorCodes.PAGE_VERSION_NOT_FOUND,
|
|
290
|
+
`页面 ${safeName} 不存在版本 ${version}`,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
return cloneRecord(record);
|
|
294
|
+
}
|
|
295
|
+
for (let index = versions.length - 1; index >= 0; index -= 1) {
|
|
296
|
+
if (versions[index].status === 'archived') {
|
|
297
|
+
throw new PageRegistryError(
|
|
298
|
+
PageRegistryErrorCodes.PAGE_NOT_FOUND,
|
|
299
|
+
`页面 ${safeName} 已归档(历史版本可按版本号查看,rollback 旧版本可恢复上架)`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
if (versions[index].status === 'published') {
|
|
303
|
+
return cloneRecord(versions[index]);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
throw new PageRegistryError(
|
|
307
|
+
PageRegistryErrorCodes.PAGE_NOT_FOUND,
|
|
308
|
+
`页面 ${safeName} 没有已发布版本`,
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 页面摘要清单(按页面名排序,分页协议 { pages, total })。
|
|
314
|
+
* 缺省排除已归档页面;支持 app / 关键词过滤与 limit/offset 分页。
|
|
315
|
+
*/
|
|
316
|
+
list(options: PageRegistryListOptions = {}): PageRegistryListResult {
|
|
317
|
+
const names = new Set<string>(this.cache.keys());
|
|
318
|
+
if (fs.existsSync(this.baseDir)) {
|
|
319
|
+
for (const file of fs.readdirSync(this.baseDir)) {
|
|
320
|
+
if (file.endsWith('.json')) {
|
|
321
|
+
const name = file.slice(0, -'.json'.length);
|
|
322
|
+
if (PAGE_NAME_PATTERN.test(name)) {
|
|
323
|
+
names.add(name);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const keyword = typeof options.keyword === 'string'
|
|
329
|
+
? options.keyword.trim().toLowerCase()
|
|
330
|
+
: '';
|
|
331
|
+
const items: PageRegistryListItem[] = [];
|
|
332
|
+
for (const name of [...names].sort((left, right) => left.localeCompare(right))) {
|
|
333
|
+
const summary = this.summaryOf(name);
|
|
334
|
+
if (!summary) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const { item, description } = summary;
|
|
338
|
+
if (options.publishedOnly && item.status !== 'published') {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (!options.includeArchived && item.status === 'archived') {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
if (options.app !== undefined && item.app !== options.app) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (keyword && !matchesKeyword(item, description, keyword)) {
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
items.push({ ...item, tags: [...item.tags] });
|
|
351
|
+
}
|
|
352
|
+
const total = items.length;
|
|
353
|
+
const offset = normalizePageParam(options.offset) ?? 0;
|
|
354
|
+
const limit = normalizePageParam(options.limit);
|
|
355
|
+
const pages = items.slice(offset, limit === undefined ? undefined : offset + limit);
|
|
356
|
+
return { pages, total };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** 回滚:把指定旧版本(声明 + 元数据 + app/tags)复制为新的 published 版本 */
|
|
360
|
+
rollback(
|
|
361
|
+
name: string,
|
|
362
|
+
version: number,
|
|
363
|
+
options: PageRegistryWriteOptions = {},
|
|
364
|
+
): PageVersionRecord {
|
|
365
|
+
const safeName = assertPageName(name);
|
|
366
|
+
return withRegistryLock(this.baseDir, safeName, () =>
|
|
367
|
+
this.rollbackWhileLocked(safeName, version, options));
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
rollbackWhileLocked(
|
|
371
|
+
name: string,
|
|
372
|
+
version: number,
|
|
373
|
+
options: PageRegistryWriteOptions = {},
|
|
374
|
+
): PageVersionRecord {
|
|
375
|
+
const safeName = assertPageName(name);
|
|
376
|
+
assertVersionNumber(version);
|
|
377
|
+
this.cache.delete(safeName);
|
|
378
|
+
const target = this.mustFindVersion(safeName, version, '无法回滚');
|
|
379
|
+
return this.appendLocked(safeName, {
|
|
380
|
+
declaration: target.declaration,
|
|
381
|
+
status: 'published',
|
|
382
|
+
app: target.app,
|
|
383
|
+
tags: target.tags,
|
|
384
|
+
meta: target.meta,
|
|
385
|
+
operator: options.operator,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* 归档(下架):追加一条 archived 墓碑记录(复制最新版本的声明与元数据)。
|
|
391
|
+
* 归档后缺省 list 消失、缺省 get 404;全部历史仍可按版本查;rollback 旧版本即恢复。
|
|
392
|
+
*/
|
|
393
|
+
archive(name: string, options: PageRegistryWriteOptions = {}): PageVersionRecord {
|
|
394
|
+
const safeName = assertPageName(name);
|
|
395
|
+
return withRegistryLock(this.baseDir, safeName, () =>
|
|
396
|
+
this.archiveWhileLocked(safeName, options));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
archiveWhileLocked(name: string, options: PageRegistryWriteOptions = {}): PageVersionRecord {
|
|
400
|
+
const safeName = assertPageName(name);
|
|
401
|
+
this.cache.delete(safeName);
|
|
402
|
+
const versions = this.load(safeName);
|
|
403
|
+
const latest = versions[versions.length - 1];
|
|
404
|
+
if (!latest) {
|
|
405
|
+
throw new PageRegistryError(
|
|
406
|
+
PageRegistryErrorCodes.PAGE_NOT_FOUND,
|
|
407
|
+
`页面不存在:${safeName}`,
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
if (latest.status === 'archived') {
|
|
411
|
+
throw new PageRegistryError(
|
|
412
|
+
PageRegistryErrorCodes.PAGE_STATUS_INVALID,
|
|
413
|
+
`页面 ${safeName} 已是归档状态`,
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
return this.appendLocked(safeName, {
|
|
417
|
+
declaration: latest.declaration,
|
|
418
|
+
status: 'archived',
|
|
419
|
+
app: latest.app,
|
|
420
|
+
tags: latest.tags,
|
|
421
|
+
meta: latest.meta,
|
|
422
|
+
operator: options.operator,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** 聚合协调器持有页面锁时使用;避免重复获取同名锁。 */
|
|
427
|
+
appendWhileLocked(name: string, fields: PageVersionFields): PageVersionRecord {
|
|
428
|
+
const safeName = assertPageName(name);
|
|
429
|
+
this.cache.delete(safeName);
|
|
430
|
+
return this.appendLocked(safeName, fields);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** 统一追加入口:全部写操作(publish/rollback/archive)都经此产生新版本 */
|
|
434
|
+
private append(
|
|
435
|
+
name: string,
|
|
436
|
+
fields: PageVersionFields,
|
|
437
|
+
): PageVersionRecord {
|
|
438
|
+
// 拿锁之后**必须重新从盘上读**:先前是"读缓存 → 算 last+1 → 写回",
|
|
439
|
+
// 两个进程各读到 v1、各算出 v2、后写的把先写的整份覆盖——一次发布静默消失。
|
|
440
|
+
// 锁的意义就是"从这一刻起看到的是最新状态",所以先清掉本地缓存再 load。
|
|
441
|
+
return withRegistryLock(this.baseDir, name, () => {
|
|
442
|
+
this.cache.delete(name);
|
|
443
|
+
return this.appendLocked(name, fields);
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** 已持锁的追加实现:调用方必须先经 withRegistryLock */
|
|
448
|
+
private appendLocked(
|
|
449
|
+
name: string,
|
|
450
|
+
fields: PageVersionFields,
|
|
451
|
+
): PageVersionRecord {
|
|
452
|
+
const versions = this.load(name);
|
|
453
|
+
const record = createPageVersionRecord(name, versions, fields, this.now());
|
|
454
|
+
this.save(name, [...versions, record]);
|
|
455
|
+
return cloneRecord(record);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** 找指定版本,缺失时报 PAGE_NOT_FOUND / PAGE_VERSION_NOT_FOUND(带动作语境) */
|
|
459
|
+
private mustFindVersion(name: string, version: number, action: string): PageVersionRecord {
|
|
460
|
+
const versions = this.load(name);
|
|
461
|
+
if (versions.length === 0) {
|
|
462
|
+
throw new PageRegistryError(
|
|
463
|
+
PageRegistryErrorCodes.PAGE_NOT_FOUND,
|
|
464
|
+
`页面不存在:${name}`,
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
const target = versions.find((item) => item.version === version);
|
|
468
|
+
if (!target) {
|
|
469
|
+
throw new PageRegistryError(
|
|
470
|
+
PageRegistryErrorCodes.PAGE_VERSION_NOT_FOUND,
|
|
471
|
+
`页面 ${name} 不存在版本 ${version},${action}`,
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
return target;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** 落盘指纹:mtime + 大小。两者都不变而内容变了的概率可以忽略,且 stat 比读文件便宜一个量级 */
|
|
478
|
+
private stampOf(filePath: string): string | null {
|
|
479
|
+
try {
|
|
480
|
+
const stat = fs.statSync(filePath);
|
|
481
|
+
return `${stat.mtimeMs}:${stat.size}`;
|
|
482
|
+
} catch {
|
|
483
|
+
return null;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** 读一个页面的全部版本:指纹未变才用缓存,否则读盘、归一化(兼容旧文件)并入缓存 */
|
|
488
|
+
private load(name: string): PageVersionRecord[] {
|
|
489
|
+
const filePath = this.fileOf(name);
|
|
490
|
+
const stamp = this.stampOf(filePath);
|
|
491
|
+
if (stamp === null) {
|
|
492
|
+
// 文件不在了:连带清掉缓存,否则删库之后还能读到幽灵数据
|
|
493
|
+
this.cache.delete(name);
|
|
494
|
+
return [];
|
|
495
|
+
}
|
|
496
|
+
const cached = this.cache.get(name);
|
|
497
|
+
if (cached && cached.stamp === stamp) {
|
|
498
|
+
return cached.versions;
|
|
499
|
+
}
|
|
500
|
+
const versions = this.parseVersionsFile(filePath);
|
|
501
|
+
this.cache.set(name, { versions, stamp });
|
|
502
|
+
this.trimCache();
|
|
503
|
+
return versions;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** 读盘 + 解析 + 归一化(兼容旧格式)。不碰任何缓存,由调用方决定放哪 */
|
|
507
|
+
private parseVersionsFile(filePath: string): PageVersionRecord[] {
|
|
508
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
509
|
+
let parsed: unknown;
|
|
510
|
+
try {
|
|
511
|
+
parsed = JSON.parse(raw);
|
|
512
|
+
} catch (error) {
|
|
513
|
+
throw new Error(`[PageRegistryStore] 声明文件损坏(非法 JSON):${filePath}`, {
|
|
514
|
+
cause: error,
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
if (!Array.isArray(parsed)) {
|
|
518
|
+
throw new Error(`[PageRegistryStore] 声明文件损坏(应为版本数组):${filePath}`);
|
|
519
|
+
}
|
|
520
|
+
// 向后兼容:旧记录没有 app/tags/meta,读入时补缺省值(app='default'、tags=[])
|
|
521
|
+
return (parsed as Array<Record<string, unknown>>).map(
|
|
522
|
+
(record) => normalizeStoredRecord(record),
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* 清单摘要缓存:页面名 → { 摘要, 落盘指纹 }。
|
|
528
|
+
*
|
|
529
|
+
* 为什么单独一层:`list()` 要的只是每页最新一版的七个字段(名字/版本号/时间/状态/
|
|
530
|
+
* app/标签/标题),但先前它对每个页面调 `load()`——把**全部版本的完整声明**读进来,
|
|
531
|
+
* 只为取最后一条的几个字段。给版本缓存加 64 页上限之后这件事更糟:超过 64 页的仓库
|
|
532
|
+
* 每次 list 都要重新读盘解析全部文件。
|
|
533
|
+
*
|
|
534
|
+
* 摘要一条约 200 字节,与声明体量无关,所以上限给得很松(4096 页)。
|
|
535
|
+
* 指纹口径与版本缓存一致:文件没动就不再碰盘。
|
|
536
|
+
*/
|
|
537
|
+
private readonly summaries = new Map<string, {
|
|
538
|
+
stamp: string;
|
|
539
|
+
item: PageRegistryListItem;
|
|
540
|
+
description: string;
|
|
541
|
+
}>();
|
|
542
|
+
|
|
543
|
+
/** 取一页的清单摘要;文件不在返回 null。不污染版本缓存(那层是给取页用的) */
|
|
544
|
+
private summaryOf(name: string): { item: PageRegistryListItem; description: string } | null {
|
|
545
|
+
const filePath = this.fileOf(name);
|
|
546
|
+
const stamp = this.stampOf(filePath);
|
|
547
|
+
if (stamp === null) {
|
|
548
|
+
this.summaries.delete(name);
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
const cached = this.summaries.get(name);
|
|
552
|
+
if (cached && cached.stamp === stamp) {
|
|
553
|
+
return cached;
|
|
554
|
+
}
|
|
555
|
+
// 版本缓存里正好有同指纹的最新数据就直接用,省一次读盘
|
|
556
|
+
const hot = this.cache.get(name);
|
|
557
|
+
const versions = hot && hot.stamp === stamp ? hot.versions : this.parseVersionsFile(filePath);
|
|
558
|
+
const latest = versions[versions.length - 1];
|
|
559
|
+
if (!latest) {
|
|
560
|
+
this.summaries.delete(name);
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
const entry = {
|
|
564
|
+
stamp,
|
|
565
|
+
item: {
|
|
566
|
+
name,
|
|
567
|
+
latestVersion: latest.version,
|
|
568
|
+
updatedAt: latest.createdAt,
|
|
569
|
+
status: latest.status,
|
|
570
|
+
app: latest.app,
|
|
571
|
+
tags: [...latest.tags],
|
|
572
|
+
title: latest.meta?.title,
|
|
573
|
+
} satisfies PageRegistryListItem,
|
|
574
|
+
description: latest.meta?.description ?? '',
|
|
575
|
+
};
|
|
576
|
+
this.summaries.set(name, entry);
|
|
577
|
+
while (this.summaries.size > 4096) {
|
|
578
|
+
const oldest = this.summaries.keys().next();
|
|
579
|
+
if (oldest.done) break;
|
|
580
|
+
this.summaries.delete(oldest.value);
|
|
581
|
+
}
|
|
582
|
+
return entry;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* 缓存上限。先前是无上限 Map,`list()` 会把仓库里**每一版的完整声明**都读进来常驻
|
|
587
|
+
* (实测 40 页 162 版 = 313KB,且随仓库线性增长,这台机器只有 2GB)。
|
|
588
|
+
* 用插入序当近似 LRU:Map 的迭代序就是插入序,超限时从头删。
|
|
589
|
+
*/
|
|
590
|
+
private trimCache(): void {
|
|
591
|
+
const MAX_CACHED_PAGES = 64;
|
|
592
|
+
while (this.cache.size > MAX_CACHED_PAGES) {
|
|
593
|
+
const oldest = this.cache.keys().next();
|
|
594
|
+
if (oldest.done) break;
|
|
595
|
+
this.cache.delete(oldest.value);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** 写穿:原子落盘(临时文件 + rename)再刷新缓存;写一半被杀不会留下半个 JSON */
|
|
600
|
+
private save(name: string, versions: PageVersionRecord[]): void {
|
|
601
|
+
fs.mkdirSync(this.baseDir, { recursive: true });
|
|
602
|
+
const filePath = this.fileOf(name);
|
|
603
|
+
writeFileAtomic(filePath, `${JSON.stringify(versions, null, 2)}\n`);
|
|
604
|
+
const stamp = this.stampOf(filePath);
|
|
605
|
+
if (stamp !== null) {
|
|
606
|
+
this.cache.set(name, { versions, stamp });
|
|
607
|
+
this.trimCache();
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
private fileOf(name: string): string {
|
|
612
|
+
return path.join(this.baseDir, `${name}.json`);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export function createPageVersionRecord(
|
|
617
|
+
name: string,
|
|
618
|
+
versions: readonly PageVersionRecord[],
|
|
619
|
+
fields: PageVersionFields,
|
|
620
|
+
createdAt: number,
|
|
621
|
+
): PageVersionRecord {
|
|
622
|
+
const record: PageVersionRecord = {
|
|
623
|
+
name,
|
|
624
|
+
version: (versions[versions.length - 1]?.version ?? 0) + 1,
|
|
625
|
+
declaration: cloneDeclaration(fields.declaration),
|
|
626
|
+
status: fields.status,
|
|
627
|
+
createdAt,
|
|
628
|
+
app: fields.app,
|
|
629
|
+
tags: [...fields.tags],
|
|
630
|
+
...(fields.meta ? { meta: { ...fields.meta } } : {}),
|
|
631
|
+
...(fields.operator ? { operator: fields.operator } : {}),
|
|
632
|
+
};
|
|
633
|
+
const compiled = precompileDeclaration(fields.declaration);
|
|
634
|
+
return compiled ? { ...record, compiled } : record;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function cloneDeclaration(declaration: PageRegistryDeclaration): PageRegistryDeclaration {
|
|
638
|
+
return { ...declaration };
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
export function assertPageName(name: unknown): string {
|
|
642
|
+
if (typeof name !== 'string' || !PAGE_NAME_PATTERN.test(name)) {
|
|
643
|
+
throw new PageRegistryError(
|
|
644
|
+
PageRegistryErrorCodes.PAGE_NAME_INVALID,
|
|
645
|
+
'页面名必须匹配 [a-z0-9-]{1,64}(小写字母 / 数字 / 连字符)',
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
return name;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
export function assertVersionNumber(version: unknown): asserts version is number {
|
|
652
|
+
if (typeof version !== 'number' || !Number.isInteger(version) || version < 1) {
|
|
653
|
+
throw new PageRegistryError(
|
|
654
|
+
PageRegistryErrorCodes.PAGE_VERSION_NOT_FOUND,
|
|
655
|
+
'版本号必须是正整数',
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** 分页参数归一化:非法值(非整数 / 负数)按未提供处理,协议宽进严出 */
|
|
661
|
+
function normalizePageParam(value: unknown): number | undefined {
|
|
662
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value < 0) {
|
|
663
|
+
return undefined;
|
|
664
|
+
}
|
|
665
|
+
return value;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/** 关键词命中:页面名 / meta.title / meta.description(不区分大小写) */
|
|
669
|
+
function matchesKeyword(
|
|
670
|
+
item: PageRegistryListItem,
|
|
671
|
+
description: string,
|
|
672
|
+
keyword: string,
|
|
673
|
+
): boolean {
|
|
674
|
+
const haystack = [item.name, item.title ?? '', description].join('\n').toLowerCase();
|
|
675
|
+
return haystack.includes(keyword);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/** publish 第三参归一化:裸 status 字符串(旧写法)或选项对象,逐字段校验 */
|
|
679
|
+
export function normalizePublishOptions(
|
|
680
|
+
options: PageRegistryStatus | PageRegistryPublishOptions,
|
|
681
|
+
): PageRegistryPublishOptions {
|
|
682
|
+
const source: Record<string, unknown> = typeof options === 'string'
|
|
683
|
+
? { status: options }
|
|
684
|
+
: (options as Record<string, unknown> | null) ?? {};
|
|
685
|
+
if (typeof source !== 'object' || Array.isArray(source)) {
|
|
686
|
+
throw new PageRegistryError(
|
|
687
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
688
|
+
'publish 选项必须是 { status?, meta?, app?, tags?, operator? } 对象',
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
const normalized: PageRegistryPublishOptions = {};
|
|
692
|
+
if (source.status !== undefined) {
|
|
693
|
+
if (source.status !== 'published' && source.status !== 'draft') {
|
|
694
|
+
throw new PageRegistryError(
|
|
695
|
+
PageRegistryErrorCodes.PAGE_STATUS_INVALID,
|
|
696
|
+
`publish 只接受 status published / draft(归档走 archive):${String(source.status)}`,
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
normalized.status = source.status;
|
|
700
|
+
}
|
|
701
|
+
if (source.app !== undefined) {
|
|
702
|
+
if (typeof source.app !== 'string' || !PAGE_APP_PATTERN.test(source.app)) {
|
|
703
|
+
throw new PageRegistryError(
|
|
704
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
705
|
+
'app 必须匹配 [a-z0-9-]{1,32}(应用命名空间)',
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
normalized.app = source.app;
|
|
709
|
+
}
|
|
710
|
+
if (source.tags !== undefined) {
|
|
711
|
+
if (!Array.isArray(source.tags)
|
|
712
|
+
|| source.tags.some((tag) => typeof tag !== 'string' || tag === '')) {
|
|
713
|
+
throw new PageRegistryError(
|
|
714
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
715
|
+
'tags 必须是非空字符串数组',
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
normalized.tags = source.tags as string[];
|
|
719
|
+
}
|
|
720
|
+
if (source.meta !== undefined) {
|
|
721
|
+
normalized.meta = normalizeMeta(source.meta);
|
|
722
|
+
}
|
|
723
|
+
if (source.operator !== undefined) {
|
|
724
|
+
if (typeof source.operator !== 'string') {
|
|
725
|
+
throw new PageRegistryError(
|
|
726
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
727
|
+
'operator 提供时必须是字符串',
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
normalized.operator = source.operator;
|
|
731
|
+
}
|
|
732
|
+
return normalized;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** 元数据归一化:白名单字段、全部可选字符串,未知字段丢弃 */
|
|
736
|
+
function normalizeMeta(meta: unknown): PageRegistryMeta {
|
|
737
|
+
if (!meta || typeof meta !== 'object' || Array.isArray(meta)) {
|
|
738
|
+
throw new PageRegistryError(
|
|
739
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
740
|
+
'meta 必须是 { title?, description?, sourceRequirement?, suggestedRoute? } 对象',
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
const source = meta as Record<string, unknown>;
|
|
744
|
+
const normalized: PageRegistryMeta = {};
|
|
745
|
+
for (const field of ['title', 'description', 'sourceRequirement', 'suggestedRoute'] as const) {
|
|
746
|
+
if (source[field] !== undefined) {
|
|
747
|
+
if (typeof source[field] !== 'string') {
|
|
748
|
+
throw new PageRegistryError(
|
|
749
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
750
|
+
`meta.${field} 提供时必须是字符串`,
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
if (source[field] !== '') {
|
|
754
|
+
normalized[field] = source[field] as string;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return normalized;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/** 声明归一化:HTML + 统一 SOMA DSL 必填;页面侧车字段只允许缺省或空串。 */
|
|
762
|
+
export function normalizeDeclaration(declaration: unknown): PageRegistryDeclaration {
|
|
763
|
+
if (!declaration || typeof declaration !== 'object' || Array.isArray(declaration)) {
|
|
764
|
+
throw new PageRegistryError(
|
|
765
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
766
|
+
'declaration 必须是 { html, uiYaml, eventYaml?, styleYaml?, dataYaml?, notes? } 对象',
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
const source = declaration as Record<string, unknown>;
|
|
770
|
+
for (const field of ['html', 'uiYaml'] as const) {
|
|
771
|
+
if (typeof source[field] !== 'string' || source[field] === '') {
|
|
772
|
+
throw new PageRegistryError(
|
|
773
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
774
|
+
`declaration.${field} 必须是非空字符串`,
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
for (const field of ['eventYaml', 'styleYaml', 'dataYaml', 'notes'] as const) {
|
|
779
|
+
if (source[field] !== undefined && typeof source[field] !== 'string') {
|
|
780
|
+
throw new PageRegistryError(
|
|
781
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
782
|
+
`declaration.${field} 提供时必须是字符串`,
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
const retiredSidecars = (['eventYaml', 'styleYaml', 'dataYaml'] as const)
|
|
787
|
+
.filter((field) => typeof source[field] === 'string' && (source[field] as string).trim() !== '');
|
|
788
|
+
if (retiredSidecars.length > 0) {
|
|
789
|
+
throw new PageRegistryError(
|
|
790
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
791
|
+
`页面侧车字段已退役:${retiredSidecars.join('、')};事件、样式状态与数据对象必须写进 uiYaml 的统一 SOMA DSL 声明`,
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
const normalized: PageRegistryDeclaration = {
|
|
795
|
+
html: source.html as string,
|
|
796
|
+
uiYaml: source.uiYaml as string,
|
|
797
|
+
eventYaml: '',
|
|
798
|
+
notes: (source.notes as string | undefined) || undefined,
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
// 体积闸门(P-2):排在安全扫描之前。两个理由——
|
|
802
|
+
// ① 数字节是 O(n) 的一次遍历,比整套扫描正则便宜,超大声明该在最便宜的一关就被挡回去;
|
|
803
|
+
// ② 安全扫描要在整份文本上跑多条正则,先放行几 MB 的声明进去等于把 CPU 送给攻击面。
|
|
804
|
+
assertDeclarationSize(normalized);
|
|
805
|
+
|
|
806
|
+
// 注入面静态扫描:坏声明不许落库。放在归一化里而不是各写操作里,
|
|
807
|
+
// 是因为这里是 publish / draft 等全部写路径的唯一咽喉——新增写操作自动受保护。
|
|
808
|
+
const unsafe = scanDeclarationSafety(normalized);
|
|
809
|
+
if (unsafe.length > 0) {
|
|
810
|
+
throw new PageRegistryError(
|
|
811
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
812
|
+
`声明未通过安全扫描:${unsafe.join(';')}`,
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// 干跑编译:编不过的声明不许落库。排在安全扫描之后、白名单之前——
|
|
817
|
+
// 扫描是纯正则(最便宜),编译要真跑解析器(贵一些),白名单还要读 API 注册表(要读盘)。
|
|
818
|
+
const uncompilable = dryRunCompile(normalized);
|
|
819
|
+
if (uncompilable.length > 0) {
|
|
820
|
+
throw new PageRegistryError(
|
|
821
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
822
|
+
`声明未通过编译闸门:${uncompilable.join(';')}`,
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// 组件存在性:引用了宿主没注册的组件就拦住(宿主没交清单时本闸门不判定)。
|
|
827
|
+
const unknownComponents = scanUnknownComponents(normalized.html);
|
|
828
|
+
if (unknownComponents.length > 0) {
|
|
829
|
+
throw new PageRegistryError(
|
|
830
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
831
|
+
`声明未通过组件存在性校验:${unknownComponents.join(';')}`,
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// 两表交叉校验:页面绑定的数据接口必须已在 API 注册表登记。
|
|
836
|
+
// 与安全扫描同一个咽喉、同一种失败方式——白名单才有牙齿:
|
|
837
|
+
// 没登记过的接口,AI 生成的页面在物理上就绑不上。
|
|
838
|
+
// 编译闸门已经证明可编译;这里再取同一编译器的 dataConfig 做 API 白名单交叉校验,
|
|
839
|
+
// 不另开 dataYaml 解析入口。
|
|
840
|
+
const compiled = compilePageDeclaration(normalized);
|
|
841
|
+
const unregistered = crossCheckDeclaredApis(compiled.dataConfig);
|
|
842
|
+
if (unregistered.length > 0) {
|
|
843
|
+
throw new PageRegistryError(
|
|
844
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
845
|
+
`声明未通过取数白名单:${unregistered.join(';')}`,
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
return normalized;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** 声明总字节数:UTF-8 计,涵盖 declaration 的全部字符串字段 */
|
|
853
|
+
export function declarationByteSize(declaration: PageRegistryDeclaration): number {
|
|
854
|
+
let bytes = 0;
|
|
855
|
+
for (const field of ['html', 'uiYaml', 'eventYaml', 'styleYaml', 'dataYaml', 'notes'] as const) {
|
|
856
|
+
const value = declaration[field];
|
|
857
|
+
if (typeof value === 'string') bytes += Buffer.byteLength(value, 'utf8');
|
|
858
|
+
}
|
|
859
|
+
return bytes;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* 体积闸门:超限即拒(错误码复用 4522 PAGE_DECLARATION_INVALID——
|
|
864
|
+
* 体积超标就是声明本身不合法的一种,不为它另开码段,调用方的错误分支不用跟着长)。
|
|
865
|
+
* 错误文案必须同时给出当前值与上限值,否则发布者只知道"被拒了"却不知道要瘦身多少。
|
|
866
|
+
*/
|
|
867
|
+
function assertDeclarationSize(declaration: PageRegistryDeclaration): void {
|
|
868
|
+
const bytes = declarationByteSize(declaration);
|
|
869
|
+
if (bytes <= PAGE_DECLARATION_MAX_BYTES) return;
|
|
870
|
+
const kb = (n: number) => `${(n / 1024).toFixed(1)}KB`;
|
|
871
|
+
throw new PageRegistryError(
|
|
872
|
+
PageRegistryErrorCodes.PAGE_DECLARATION_INVALID,
|
|
873
|
+
`声明体积超限:当前 ${bytes} 字节(${kb(bytes)})/ 上限 ${PAGE_DECLARATION_MAX_BYTES} 字节(${kb(PAGE_DECLARATION_MAX_BYTES)})。` +
|
|
874
|
+
`一份声明是整份拉取、整份运行时编译的,超大声明会同时拖垮取页与首屏编译;` +
|
|
875
|
+
`请把内联数据改成接口返回、把长页面拆成多页,不要把明细或 base64 资源塞进声明。`,
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/** 存量 JSON 记录归一化:只补元数据;旧页面语法必须离线迁移,不在读取时自动转换。 */
|
|
880
|
+
export function normalizeStoredRecord(record: Record<string, unknown>): PageVersionRecord {
|
|
881
|
+
const normalized = { ...record } as unknown as PageVersionRecord;
|
|
882
|
+
if (typeof normalized.app !== 'string' || !PAGE_APP_PATTERN.test(normalized.app)) {
|
|
883
|
+
normalized.app = 'default';
|
|
884
|
+
}
|
|
885
|
+
if (!Array.isArray(normalized.tags)) {
|
|
886
|
+
normalized.tags = [];
|
|
887
|
+
} else {
|
|
888
|
+
normalized.tags = normalized.tags.filter((tag): tag is string => typeof tag === 'string');
|
|
889
|
+
}
|
|
890
|
+
assertStoredDeclarationCanonical(normalized.declaration);
|
|
891
|
+
return normalized;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
function assertStoredDeclarationCanonical(declaration: unknown): void {
|
|
895
|
+
if (!declaration || typeof declaration !== 'object' || Array.isArray(declaration)) {
|
|
896
|
+
throw new Error('[PageRegistryStore] 存量页面记录缺少 declaration;请修复或离线迁移该记录');
|
|
897
|
+
}
|
|
898
|
+
const source = declaration as Record<string, unknown>;
|
|
899
|
+
const uiYaml = typeof source.uiYaml === 'string' ? source.uiYaml : '';
|
|
900
|
+
const hasRetiredSidecar = ['eventYaml', 'styleYaml', 'dataYaml']
|
|
901
|
+
.some((field) => typeof source[field] === 'string' && (source[field] as string).trim() !== '');
|
|
902
|
+
if (!/^\s*(?:#[^\n]*\n\s*)*\{\{/u.test(uiYaml)
|
|
903
|
+
|| !/\}\}\s*$/u.test(uiYaml)
|
|
904
|
+
|| hasRetiredSidecar) {
|
|
905
|
+
throw new Error(
|
|
906
|
+
'[PageRegistryStore] 检测到旧格式存量页面声明;请使用离线迁移工具转换为 HTML + 单一 SOMA DSL 后再启动。'
|
|
907
|
+
+ '运行时不会自动转换旧 bind/show 或 event/style/data 侧车。',
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/** 对外返回深拷贝,避免调用方改动污染内存缓存 */
|
|
913
|
+
export function cloneRecord(record: PageVersionRecord): PageVersionRecord {
|
|
914
|
+
return JSON.parse(JSON.stringify(record)) as PageVersionRecord;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
// ---------------------------------------------------------------------------
|
|
918
|
+
// 进程级共享实例:API 服务经此取店,测试可注入自定义实例后再复位。
|
|
919
|
+
// ---------------------------------------------------------------------------
|
|
920
|
+
|
|
921
|
+
let sharedStore: PageRegistryStore | undefined;
|
|
922
|
+
|
|
923
|
+
/** 共享实例:baseDir 可用环境变量 SOMA_PAGE_REGISTRY_DIR 覆盖(默认 data/soma-runtime/pages) */
|
|
924
|
+
export function getPageRegistryStore(): PageRegistryStore {
|
|
925
|
+
sharedStore ??= new PageRegistryStore({ baseDir: process.env.SOMA_PAGE_REGISTRY_DIR });
|
|
926
|
+
return sharedStore;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
/** 替换 / 复位共享实例(测试用;不传参即清空,下次访问按当前环境重建) */
|
|
930
|
+
export function resetPageRegistryStore(store?: PageRegistryStore): void {
|
|
931
|
+
sharedStore = store;
|
|
932
|
+
}
|