soma-kit 0.1.10 → 0.1.12
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 +103 -110
- package/bin/soma-kit.mjs +1 -1
- package/bin/soma.mjs +22 -0
- package/package.json +13 -7
- package/packs/finance/reconcile-board.ts +5 -2
- package/packs/finance/reconcile-desk/compile.ts +3 -2
- package/spec/generated/kit-app.schema.json +776 -38
- package/spec/generated/kit-page.schema.json +1613 -224
- package/spec/kit-grammar.json +770 -529
- package/spec/kit-grammar.schema.json +1962 -392
- package/src/app/app-config.ts +371 -347
- package/src/app/component-gate.ts +12 -15
- package/src/app/compose-apps.ts +79 -8
- package/src/app/define-kit-actions.ts +245 -41
- package/src/app/define-kit-app.ts +174 -31
- package/src/app/define-kit-view.ts +100 -0
- package/src/app/envelope.ts +1 -161
- package/src/app/index.ts +13 -2
- package/src/app/kit-view-context.ts +290 -0
- package/src/app/notify-renderer.ts +1 -115
- package/src/app/paged-list-adapter.ts +1 -240
- package/src/app/server-pages.ts +8 -2
- package/src/app/server.ts +491 -101
- package/src/app/submit.types.ts +1 -1
- package/src/app/theme-endpoint.ts +63 -0
- package/src/app/theme-runtime-shared.ts +1 -0
- package/src/app/theme-runtime.ts +663 -0
- package/src/base/action-busy.ts +2 -2
- package/src/base/action-gate.ts +4 -255
- package/src/base/action-slots.ts +47 -0
- package/src/base/alert.ts +5 -3
- package/src/base/avatar.ts +2 -2
- package/src/base/button.ts +46 -29
- package/src/base/cascader.ts +11 -12
- package/src/base/check-core.ts +1 -1
- package/src/base/choice.ts +28 -26
- package/src/base/class-candidates.ts +17 -0
- package/src/base/color-picker.ts +6 -6
- package/src/base/combobox.ts +5 -3
- package/src/base/command-palette.ts +9 -5
- package/src/base/countdown.ts +1 -1
- package/src/base/date-range.ts +25 -11
- package/src/base/date.ts +85 -17
- package/src/base/divider.ts +2 -2
- package/src/base/fab.ts +11 -4
- package/src/base/image.ts +1 -1
- package/src/base/index.ts +9 -3
- package/src/base/input.ts +42 -13
- package/src/base/kbd.ts +3 -1
- package/src/base/link.ts +4 -4
- package/src/base/listbox-core.ts +50 -30
- package/src/base/loading-gate.ts +133 -0
- package/src/base/mentions.ts +5 -5
- package/src/base/menu-button.ts +6 -1
- package/src/base/menu-core.ts +4 -0
- package/src/base/motion/aura.ts +2 -2
- package/src/base/motion/hover-3d.ts +2 -2
- package/src/base/motion/hover-gallery.ts +2 -2
- package/src/base/motion/marquee.ts +2 -2
- package/src/base/motion/text-rotate.ts +2 -2
- package/src/base/number.ts +93 -80
- package/src/base/otp.ts +4 -1
- package/src/base/permission-gate.ts +6 -0
- package/src/base/popover.ts +40 -8
- package/src/base/popup.ts +79 -14
- package/src/base/price-text.ts +2 -2
- package/src/base/progress.ts +2 -2
- package/src/base/qr-code.ts +4 -4
- package/src/base/qty-stepper.ts +9 -3
- package/src/base/range.ts +2 -2
- package/src/base/rating.ts +1 -1
- package/src/base/segmented.ts +4 -0
- package/src/base/select.ts +4 -3
- package/src/base/shared.ts +221 -29
- package/src/base/swap.ts +4 -1
- package/src/base/switch.ts +1 -1
- package/src/base/tag-input.ts +14 -10
- package/src/base/tag.ts +5 -1
- package/src/base/text.ts +39 -6
- package/src/base/textarea.ts +37 -11
- package/src/base/time-picker.ts +12 -9
- package/src/base/tree-select.ts +15 -8
- package/src/base/upload.ts +34 -5
- package/src/base/virtual-list.ts +107 -1
- package/src/biz/approval-desk/compile.ts +23 -41
- package/src/biz/auth-panel/compile.ts +37 -52
- package/src/biz/auth-panel/index.ts +1 -1
- package/src/biz/category-crud/compile.ts +22 -36
- package/src/biz/checkout-page/compile.ts +21 -5
- package/src/biz/compose.ts +13 -27
- package/src/biz/define.ts +5 -4
- package/src/biz/import-wizard/compile.ts +22 -26
- package/src/biz/import-wizard/hub.ts +126 -61
- package/src/biz/import-wizard/shell.ts +3 -3
- package/src/biz/index.ts +3 -3
- package/src/biz/landing-page/compile.ts +19 -3
- package/src/biz/link/hub.ts +49 -20
- package/src/biz/link/shared.ts +115 -118
- package/src/biz/master-sub/compile.ts +31 -44
- package/src/biz/multi-board/compile.ts +34 -49
- package/src/biz/multi-board/index.ts +18 -1
- package/src/biz/multi-board/shell.ts +24 -15
- package/src/biz/notification-center/compile.ts +21 -33
- package/src/biz/page-templates/domain-templates.ts +80 -13
- package/src/biz/page-templates/result-page/compile.ts +11 -2
- package/src/biz/page-templates/shared.ts +34 -36
- package/src/biz/record-detail/compile.ts +47 -42
- package/src/biz/record-detail/index.ts +11 -1
- package/src/biz/settings-center/compile.ts +6 -5
- package/src/biz/shared/action-host.ts +91 -211
- package/src/biz/shared/compile-helpers.ts +80 -0
- package/src/biz/shared/coordinator.ts +486 -83
- package/src/biz/shared/index.ts +8 -2
- package/src/biz/shared/overlay-shell.ts +80 -0
- package/src/biz/shared/pipes.ts +43 -30
- package/src/biz/shared/remove-key.ts +6 -0
- package/src/biz/shared/remove-wire.ts +29 -0
- package/src/biz/shared/runtime-hub.ts +216 -24
- package/src/biz/single-form/compile.ts +15 -37
- package/src/biz/standard-crud/compile.ts +62 -107
- package/src/biz/standard-crud/shell.ts +16 -66
- package/src/biz/step-form/compile.ts +26 -97
- package/src/biz/step-form/hub.ts +34 -41
- package/src/biz/types.ts +4 -3
- package/src/cli/core-imports.ts +54 -0
- package/src/cli/kit-cli.ts +130 -26
- package/src/cli/kit-init.ts +222 -0
- package/src/cli/service-endpoint-check.ts +425 -0
- package/src/cli/source-path-check.d.mts +17 -0
- package/src/cli/source-path-check.mjs +334 -0
- package/src/cli/view-writes.ts +752 -0
- package/src/compile/action-host-attach.ts +21 -29
- package/src/compile/closed-set.ts +509 -0
- package/src/compile/compile-page.ts +219 -72
- package/src/compile/component-candidates.ts +1 -1
- package/src/compile/hints.ts +30 -1
- package/src/compile/index.ts +1 -0
- package/src/compile/kit-error.ts +1 -0
- package/src/compile/locate.ts +21 -1
- package/src/compile/parse.ts +370 -41
- package/src/compile/redirect.ts +6 -2
- package/src/compile/styles.ts +35 -2
- package/src/compile/types.ts +18 -2
- package/src/compile/view-check.ts +151 -0
- package/src/data/amount-breakdown.ts +2 -2
- package/src/data/anchor-nav.ts +2 -2
- package/src/data/approval-flow.ts +2 -2
- package/src/data/assignment-picker.ts +40 -29
- package/src/data/async-result-panel.ts +62 -6
- package/src/data/breadcrumbs.ts +2 -2
- package/src/data/calendar.ts +22 -25
- package/src/data/carousel.ts +5 -2
- package/src/data/chart.ts +42 -34
- package/src/data/checklist.ts +2 -2
- package/src/data/code-view.ts +4 -2
- package/src/data/comment-thread.ts +5 -2
- package/src/data/confirm-dialog.ts +10 -0
- package/src/data/context-menu.ts +29 -37
- package/src/data/d9-util.ts +2 -18
- package/src/data/diff.ts +4 -2
- package/src/data/dock.ts +2 -2
- package/src/data/embed.ts +2 -1
- package/src/data/field-mapper.ts +31 -26
- package/src/data/flow-editor-contract.ts +1 -1
- package/src/data/flow-editor.ts +89 -100
- package/src/data/flow-node-options.ts +90 -0
- package/src/data/flow-remote-options.ts +0 -103
- package/src/data/footer.ts +2 -2
- package/src/data/form-draft-registry.ts +2 -4
- package/src/data/gantt.ts +14 -17
- package/src/data/hero.ts +2 -2
- package/src/data/image-viewer.ts +20 -26
- package/src/data/index.ts +0 -2
- package/src/data/item-list.ts +31 -22
- package/src/data/kanban.ts +43 -42
- package/src/data/media-grid.ts +14 -19
- package/src/data/menu.ts +2 -2
- package/src/data/mockup.ts +2 -2
- package/src/data/modal-dialog.ts +22 -18
- package/src/data/navbar.ts +7 -2
- package/src/data/notify-renderer.ts +235 -0
- package/src/data/object-form.ts +1036 -648
- package/src/data/pager.ts +65 -29
- package/src/data/permission-matrix.ts +20 -21
- package/src/data/permission-tree.ts +28 -26
- package/src/data/pricing-table.ts +9 -3
- package/src/data/reaction-bar.ts +77 -88
- package/src/data/read-rollback.ts +73 -0
- package/src/data/record-card.ts +3 -3
- package/src/data/record-picker.ts +93 -50
- package/src/data/redirect.ts +32 -32
- package/src/data/remote-options.ts +13 -6
- package/src/data/request-console.ts +5 -3
- package/src/data/resident.ts +2 -1
- package/src/data/result-panel.ts +4 -2
- package/src/data/rich-text.ts +45 -55
- package/src/data/row-editor.ts +285 -52
- package/src/data/rule-builder.ts +18 -12
- package/src/data/shape-util.ts +4 -0
- package/src/data/stat-group.ts +243 -101
- package/src/data/static-registry.ts +22 -11
- package/src/data/steps.ts +2 -2
- package/src/data/survey.ts +24 -29
- package/src/data/system-switcher.ts +2 -2
- package/src/data/timeline.ts +2 -2
- package/src/data/tour-guide.ts +22 -29
- package/src/data/transfer.ts +27 -23
- package/src/data/tree-table.ts +96 -48
- package/src/data/tree.ts +27 -34
- package/src/data/trend-line.ts +6 -24
- package/src/data/virtual-rows.ts +118 -76
- package/src/data/watermark.ts +2 -2
- package/src/data/wrap.ts +63 -38
- package/src/frames/blank-frame.ts +1 -0
- package/src/frames/index.ts +2 -2
- package/src/frames/page-bar.ts +12 -11
- package/src/frames/page-header.ts +14 -5
- package/src/frames/page-status.ts +2 -2
- package/src/frames/shared.ts +23 -17
- package/src/frames/single-frame.ts +1 -0
- package/src/frames/split-frame/shell.ts +5 -5
- package/src/frames/split-frame/wiring.ts +2 -2
- package/src/frames/tabs-frame/shell.ts +2 -13
- package/src/frames/tabs-frame/wiring.ts +4 -8
- package/src/frames/top-main-frame.ts +1 -0
- package/src/frames/types.ts +13 -2
- package/src/frames/workspace-frame/extras.ts +20 -4
- package/src/frames/workspace-frame/index.ts +1 -1
- package/src/frames/workspace-frame/shell.ts +1 -0
- package/src/frames/workspace-frame/state.ts +13 -6
- package/src/frames/workspace-frame/view.ts +119 -25
- package/src/generated/kit-grammar-runtime.ts +430 -366
- package/src/generated/page-origin.ts +0 -11
- package/src/i18n/kit-text.ts +23 -27
- package/src/index.ts +2 -1
- package/src/object-renderer/action-gate.ts +11 -147
- package/src/object-renderer/arrange.ts +71 -77
- package/src/object-renderer/collapse-wiring.ts +2 -2
- package/src/object-renderer/collection-expand.ts +2 -2
- package/src/object-renderer/collection-parts.ts +216 -100
- package/src/object-renderer/collection-table.ts +180 -42
- package/src/object-renderer/collection.ts +95 -79
- package/src/object-renderer/compile-data.ts +85 -100
- package/src/object-renderer/crud-bridge.ts +32 -0
- package/src/object-renderer/defaults.ts +8 -14
- package/src/object-renderer/display-checks.ts +3 -24
- package/src/object-renderer/emit-events.ts +186 -172
- package/src/object-renderer/emit-object.ts +15 -2
- package/src/object-renderer/emit-static.ts +174 -132
- package/src/object-renderer/field-value.ts +593 -396
- package/src/object-renderer/filter-reload.ts +70 -0
- package/src/object-renderer/form-detail.ts +112 -2
- package/src/object-renderer/index.ts +26 -0
- package/src/object-renderer/key-field.ts +12 -0
- package/src/object-renderer/linkage.ts +7 -270
- package/src/object-renderer/merge-class.ts +18 -1
- package/src/object-renderer/path-pick.ts +27 -0
- package/src/object-renderer/render-object.ts +405 -86
- package/src/object-renderer/request-actions.ts +230 -249
- package/src/object-renderer/table-actions.ts +195 -95
- package/src/object-renderer/table-summary.ts +2 -2
- package/src/object-renderer/table-tools.ts +418 -84
- package/src/object-renderer/types.ts +128 -122
- package/src/object-renderer/view-binding.ts +196 -0
- package/src/page-target/ai-surface.ts +37 -0
- package/src/page-target/compile.ts +48 -8
- package/src/page-target/index.ts +8 -1
- package/src/page-target/intent-schema.ts +154 -28
- package/src/page-target/target.ts +257 -50
- package/src/page-target/writer-policy.ts +14 -3
- package/src/recipes/container.ts +165 -0
- package/src/recipes/control.ts +219 -0
- package/src/recipes/index.ts +263 -0
- package/src/recipes/overlay.ts +182 -0
- package/src/recipes/state.ts +70 -0
- package/src/recipes/table-row.ts +51 -0
- package/src/recipes/typography.ts +87 -0
- package/src/shared/action-field-guard.ts +41 -0
- package/src/shared/envelope.ts +207 -0
- package/src/shared/escape.ts +57 -0
- package/src/shared/kit-error.ts +21 -0
- package/src/shared/paged-list-adapter.ts +240 -0
- package/src/shared/path-guard.ts +127 -0
- package/src/shared/theme-runtime-shared.ts +175 -0
- package/src/shared/view-info.ts +10 -0
- package/src/theme/index.ts +12 -0
- package/src/theme/kit-theme-text.ts +1 -1
- package/src/theme/kit-theme.css +38 -1
- package/src/theme/kit-theme.tpl +38 -1
- package/src/theme/nine-dim-structure.ts +77 -0
- package/src/theme/surface-layers.ts +97 -0
- package/src/theme/theme-tokens.ts +85 -79
- package/src/theme/theme-types.ts +71 -0
- package/src/theme/typography.ts +17 -17
- package/src/biz/shared/step-values.ts +0 -25
- package/src/biz/step-form/steps-ref.ts +0 -112
- package/src/data/options-bus.ts +0 -24
- package/src/data/prompt-dialog.ts +0 -235
- package/src/object-renderer/adopt-gate.ts +0 -333
- package/src/object-renderer/field-policy.ts +0 -89
- package/src/object-renderer/outcome-draft.ts +0 -61
- package/src/theme/nine-dim-palettes.ts +0 -1278
package/src/app/server.ts
CHANGED
|
@@ -1,30 +1,40 @@
|
|
|
1
|
+
import { escapeHtml } from '../shared/escape';
|
|
1
2
|
// 服务端面:装配 + 扫描 + 编译 + 按 URL 注册进 core(owner 'soma-kit')。
|
|
2
3
|
// 只在服务端 / build 面可达;node 内置模块与 core 服务端子路径全部动态 import,并带 webpackIgnore,
|
|
3
4
|
// 即便 defineKitApp 的惰性 import 被打进浏览器包图,也不会在解析期报错。
|
|
4
5
|
|
|
5
6
|
import { createHash } from 'node:crypto';
|
|
7
|
+
import { canonicalJson } from 'soma-foundation/fingerprint';
|
|
6
8
|
import { KIT_BASE_COMPONENTS } from '../base/index';
|
|
7
|
-
import { KIT_STATIC_BUILTIN_SPECS } from '../data/static-registry';
|
|
8
|
-
import { compileKitPage } from '../compile/compile-page';
|
|
9
|
+
import { KIT_STATIC_BUILTIN_SPECS, staticPropTypesOf } from '../data/static-registry';
|
|
10
|
+
import { compileKitPage, KIT_ROOT_HOST } from '../compile/compile-page';
|
|
11
|
+
import grammarJson from '../../spec/kit-grammar.json';
|
|
12
|
+
import { inferPageUrlFromPath } from '../compile/parse';
|
|
9
13
|
import { createYamlLocator } from '../compile/locate';
|
|
10
14
|
import { formatKitDiagnostic } from '../compile/hints';
|
|
11
|
-
import type { KitAppContext, KitPageArtifact } from '../compile/types';
|
|
15
|
+
import type { KitAppContext, KitCompileResult, KitPageArtifact } from '../compile/types';
|
|
12
16
|
import { KIT_FORM_FIELD_DATA_COMPONENTS } from '../object-renderer/defaults';
|
|
13
17
|
import type { KitDiagnostic } from '../object-renderer/types';
|
|
14
18
|
import { KIT_DEFAULT_THEME_CSS } from '../theme/kit-theme-text';
|
|
15
|
-
import { resolveKitThemeTokens, type KitThemeConfig } from '../theme/theme-tokens';
|
|
16
|
-
import { parseAppConfig, type KitAppTheme } from './app-config';
|
|
19
|
+
import { kitThemeConfigOf, resolveKitThemeTokens, type KitThemeConfig, type NineDimDegradation } from '../theme/theme-tokens';
|
|
20
|
+
import { parseAppConfig, checkRemovedThemeKeys, type KitAppTheme } from './app-config';
|
|
17
21
|
import { scanAppComponentSources } from './component-gate';
|
|
18
|
-
import {
|
|
22
|
+
import { handleKitThemeRequest } from './theme-endpoint';
|
|
19
23
|
import type { DefineKitAppOptions, KitAppRegistry } from './define-kit-app';
|
|
20
24
|
import { KIT_PAGE_OWNER, createKitServerPages } from './server-pages';
|
|
21
25
|
import type { KitServerPages } from './submit.types';
|
|
22
26
|
|
|
23
27
|
export const KIT_OWNER = KIT_PAGE_OWNER;
|
|
24
|
-
export const
|
|
28
|
+
export const KIT_GRAMMAR_DIGEST = createHash('sha256')
|
|
29
|
+
.update(JSON.stringify(grammarJson))
|
|
30
|
+
.digest('hex')
|
|
31
|
+
.slice(0, 16);
|
|
32
|
+
export const KIT_GRAMMAR_VERSION = KIT_GRAMMAR_DIGEST;
|
|
25
33
|
export const MAX_KIT_PAGE_CACHE = 256;
|
|
26
34
|
|
|
27
35
|
const kitPageCompileCache = new Map<string, KitPageArtifact>();
|
|
36
|
+
const kitRegistryCacheScopeIds = new WeakMap<KitAppRegistry, number>();
|
|
37
|
+
let nextKitRegistryCacheScopeId = 1;
|
|
28
38
|
|
|
29
39
|
/** 清空页面编译产物缓存(供测试或热重载) */
|
|
30
40
|
export function clearKitPageCompileCache(): void {
|
|
@@ -36,33 +46,228 @@ export function getKitPageCompileCacheSize(): number {
|
|
|
36
46
|
return kitPageCompileCache.size;
|
|
37
47
|
}
|
|
38
48
|
|
|
39
|
-
|
|
49
|
+
function scopedKitPageCacheKey(registry: KitAppRegistry, fingerprint: string): string {
|
|
50
|
+
let scopeId = kitRegistryCacheScopeIds.get(registry);
|
|
51
|
+
if (scopeId === undefined) {
|
|
52
|
+
scopeId = nextKitRegistryCacheScopeId++;
|
|
53
|
+
kitRegistryCacheScopeIds.set(registry, scopeId);
|
|
54
|
+
}
|
|
55
|
+
return `${scopeId}\0${fingerprint}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function cachedKitPageArtifact(key: string): KitPageArtifact | undefined {
|
|
59
|
+
const cached = kitPageCompileCache.get(key);
|
|
60
|
+
if (cached === undefined) return undefined;
|
|
61
|
+
kitPageCompileCache.delete(key);
|
|
62
|
+
kitPageCompileCache.set(key, cached);
|
|
63
|
+
return cached;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function storeKitPageArtifact(key: string, artifact: KitPageArtifact): void {
|
|
67
|
+
while (kitPageCompileCache.size >= MAX_KIT_PAGE_CACHE) {
|
|
68
|
+
const oldest = kitPageCompileCache.keys().next().value;
|
|
69
|
+
if (oldest === undefined) break;
|
|
70
|
+
kitPageCompileCache.delete(oldest);
|
|
71
|
+
}
|
|
72
|
+
kitPageCompileCache.set(key, cloneKitPageArtifact(artifact));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 计算确定性输入指纹;上下文不能安全归一时返回 undefined 并跳过缓存 */
|
|
76
|
+
const cacheFunctionIds = new WeakMap<Function, number>();
|
|
77
|
+
const cacheObjectIds = new WeakMap<object, number>();
|
|
78
|
+
let nextCacheFunctionId = 1;
|
|
79
|
+
let nextCacheObjectId = 1;
|
|
80
|
+
|
|
81
|
+
class UnsafeKitCacheInput extends Error {}
|
|
82
|
+
|
|
83
|
+
function cacheFunctionId(value: Function): number {
|
|
84
|
+
let id = cacheFunctionIds.get(value);
|
|
85
|
+
if (id === undefined) {
|
|
86
|
+
id = nextCacheFunctionId++;
|
|
87
|
+
cacheFunctionIds.set(value, id);
|
|
88
|
+
}
|
|
89
|
+
return id;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function cacheObjectId(value: object): number {
|
|
93
|
+
let id = cacheObjectIds.get(value);
|
|
94
|
+
if (id === undefined) {
|
|
95
|
+
id = nextCacheObjectId++;
|
|
96
|
+
cacheObjectIds.set(value, id);
|
|
97
|
+
}
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 读取纯数据属性;accessor 与无法探测的对象形态让调用方跳过缓存,不触发 getter。 */
|
|
102
|
+
function dataDescriptors(value: object): { names: string[]; descriptors: PropertyDescriptorMap; nullPrototype: boolean } {
|
|
103
|
+
let prototype: object | null;
|
|
104
|
+
let descriptors: PropertyDescriptorMap;
|
|
105
|
+
let names: string[];
|
|
106
|
+
try {
|
|
107
|
+
prototype = Object.getPrototypeOf(value);
|
|
108
|
+
descriptors = Object.getOwnPropertyDescriptors(value);
|
|
109
|
+
names = Object.getOwnPropertyNames(value);
|
|
110
|
+
if (Object.getOwnPropertySymbols(value).length > 0) throw new UnsafeKitCacheInput();
|
|
111
|
+
} catch {
|
|
112
|
+
throw new UnsafeKitCacheInput();
|
|
113
|
+
}
|
|
114
|
+
if (prototype !== Object.prototype && prototype !== null) throw new UnsafeKitCacheInput();
|
|
115
|
+
for (const name of names) {
|
|
116
|
+
const descriptor = descriptors[name];
|
|
117
|
+
if (descriptor === undefined || !Object.hasOwn(descriptor, 'value')) throw new UnsafeKitCacheInput();
|
|
118
|
+
}
|
|
119
|
+
return { names, descriptors, nullPrototype: prototype === null };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 登记定义按对象身份与全部数据成员归一,防止同一 compile 函数被不同 this 对象替换后误命中。 */
|
|
123
|
+
function identityTable(value: unknown): unknown {
|
|
124
|
+
if (value === undefined || value === null) return value;
|
|
125
|
+
if (typeof value !== 'object' || Array.isArray(value)) throw new UnsafeKitCacheInput();
|
|
126
|
+
const { names, descriptors, nullPrototype } = dataDescriptors(value);
|
|
127
|
+
return [
|
|
128
|
+
'definition-table', nullPrototype,
|
|
129
|
+
names.map((name) => {
|
|
130
|
+
const descriptor = descriptors[name]!;
|
|
131
|
+
const definition = descriptor.value;
|
|
132
|
+
const identity = (typeof definition === 'object' && definition !== null) || typeof definition === 'function'
|
|
133
|
+
? cacheObjectId(definition as object)
|
|
134
|
+
: null;
|
|
135
|
+
return [name, descriptor.enumerable === true, descriptor.configurable === true, descriptor.writable === true,
|
|
136
|
+
identity, definition];
|
|
137
|
+
}),
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function normalizeKitCacheValue(value: unknown, active = new Set<object>()): unknown {
|
|
142
|
+
if (value === undefined) return ['undefined'];
|
|
143
|
+
if (value === null) return ['null'];
|
|
144
|
+
if (typeof value === 'string') return ['string', value];
|
|
145
|
+
if (typeof value === 'boolean') return ['boolean', value];
|
|
146
|
+
if (typeof value === 'number') {
|
|
147
|
+
if (!Number.isFinite(value)) throw new UnsafeKitCacheInput();
|
|
148
|
+
return ['number', value];
|
|
149
|
+
}
|
|
150
|
+
if (typeof value === 'function') {
|
|
151
|
+
const fn = value as Function;
|
|
152
|
+
if (active.has(fn)) throw new UnsafeKitCacheInput();
|
|
153
|
+
active.add(fn);
|
|
154
|
+
try {
|
|
155
|
+
let descriptors: PropertyDescriptorMap;
|
|
156
|
+
try {
|
|
157
|
+
descriptors = Object.getOwnPropertyDescriptors(fn);
|
|
158
|
+
if (Object.getOwnPropertySymbols(fn).length > 0) throw new UnsafeKitCacheInput();
|
|
159
|
+
} catch {
|
|
160
|
+
throw new UnsafeKitCacheInput();
|
|
161
|
+
}
|
|
162
|
+
const props: unknown[] = [];
|
|
163
|
+
for (const name of Object.keys(descriptors).sort()) {
|
|
164
|
+
const descriptor = descriptors[name]!;
|
|
165
|
+
if (!descriptor.enumerable) continue;
|
|
166
|
+
if (!Object.hasOwn(descriptor, 'value')) throw new UnsafeKitCacheInput();
|
|
167
|
+
props.push([name, normalizeKitCacheValue(descriptor.value, active)]);
|
|
168
|
+
}
|
|
169
|
+
return ['function', cacheFunctionId(fn), props];
|
|
170
|
+
} finally {
|
|
171
|
+
active.delete(fn);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (typeof value !== 'object') throw new UnsafeKitCacheInput();
|
|
175
|
+
const object = value as object;
|
|
176
|
+
if (active.has(object)) throw new UnsafeKitCacheInput();
|
|
177
|
+
active.add(object);
|
|
178
|
+
try {
|
|
179
|
+
if (Array.isArray(object)) {
|
|
180
|
+
if (Object.getPrototypeOf(object) !== Array.prototype || Object.getOwnPropertySymbols(object).length > 0) throw new UnsafeKitCacheInput();
|
|
181
|
+
const descriptors = Object.getOwnPropertyDescriptors(object);
|
|
182
|
+
const length = object.length;
|
|
183
|
+
const values: unknown[] = [];
|
|
184
|
+
for (let i = 0; i < length; i += 1) {
|
|
185
|
+
const descriptor = descriptors[String(i)];
|
|
186
|
+
if (descriptor === undefined) values.push(['hole']);
|
|
187
|
+
else {
|
|
188
|
+
if (!Object.hasOwn(descriptor, 'value')) throw new UnsafeKitCacheInput();
|
|
189
|
+
values.push(normalizeKitCacheValue(descriptor.value, active));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const extras: unknown[] = [];
|
|
193
|
+
for (const name of Object.keys(descriptors).filter((key) => key !== 'length' && !/^(0|[1-9]\d*)$/.test(key)).sort()) {
|
|
194
|
+
const descriptor = descriptors[name]!;
|
|
195
|
+
if (!Object.hasOwn(descriptor, 'value')) throw new UnsafeKitCacheInput();
|
|
196
|
+
extras.push([name, normalizeKitCacheValue(descriptor.value, active)]);
|
|
197
|
+
}
|
|
198
|
+
return ['array', length, values, extras];
|
|
199
|
+
}
|
|
200
|
+
if (object instanceof Set) {
|
|
201
|
+
if (Object.getPrototypeOf(object) !== Set.prototype || Object.getOwnPropertyNames(object).length > 0 || Object.getOwnPropertySymbols(object).length > 0) throw new UnsafeKitCacheInput();
|
|
202
|
+
const values = [...object].map((item) => normalizeKitCacheValue(item, active));
|
|
203
|
+
values.sort((a, b) => canonicalJson(a).localeCompare(canonicalJson(b)));
|
|
204
|
+
return ['set', values];
|
|
205
|
+
}
|
|
206
|
+
if (object instanceof Map) {
|
|
207
|
+
if (Object.getPrototypeOf(object) !== Map.prototype || Object.getOwnPropertyNames(object).length > 0 || Object.getOwnPropertySymbols(object).length > 0) throw new UnsafeKitCacheInput();
|
|
208
|
+
const entries = [...object].map(([key, item]) => [normalizeKitCacheValue(key, active), normalizeKitCacheValue(item, active)] as const);
|
|
209
|
+
entries.sort((a, b) => canonicalJson(a[0]).localeCompare(canonicalJson(b[0])) || canonicalJson(a[1]).localeCompare(canonicalJson(b[1])));
|
|
210
|
+
return ['map', entries];
|
|
211
|
+
}
|
|
212
|
+
const { names, descriptors, nullPrototype } = dataDescriptors(object);
|
|
213
|
+
return ['record', nullPrototype, names.map((name) => {
|
|
214
|
+
const descriptor = descriptors[name]!;
|
|
215
|
+
return [name, descriptor.enumerable === true, descriptor.configurable === true, descriptor.writable === true,
|
|
216
|
+
normalizeKitCacheValue(descriptor.value, active)];
|
|
217
|
+
})];
|
|
218
|
+
} finally {
|
|
219
|
+
active.delete(object);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function cacheInputOf(appContext: KitAppContext, ignoreDefaultStyleEngine = false): unknown {
|
|
224
|
+
const { names, descriptors, nullPrototype } = dataDescriptors(appContext);
|
|
225
|
+
const entries: unknown[] = [];
|
|
226
|
+
for (const name of names) {
|
|
227
|
+
const descriptor = descriptors[name]!;
|
|
228
|
+
let value = descriptor.value;
|
|
229
|
+
if (name === 'bizComponents' || name === 'frames' || name === 'componentDefinitions') value = identityTable(value);
|
|
230
|
+
if (name === 'styleEngine' && ignoreDefaultStyleEngine) {
|
|
231
|
+
// assembleKitServer 每次新建同一份框架默认引擎;它是确定实现,不以新实例身份污染真实缓存命中。
|
|
232
|
+
value = ['soma-kit-default-style-engine'];
|
|
233
|
+
} else if (name === 'styleEngine' && value !== undefined && value !== null) {
|
|
234
|
+
if (typeof value !== 'object' && typeof value !== 'function') throw new UnsafeKitCacheInput();
|
|
235
|
+
value = ['style-engine', cacheObjectId(value as object), value];
|
|
236
|
+
}
|
|
237
|
+
entries.push([name, descriptor.enumerable === true, descriptor.configurable === true, descriptor.writable === true, value]);
|
|
238
|
+
}
|
|
239
|
+
return ['app-context', nullPrototype, entries];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function computeKitPageCacheKeyInternal(
|
|
243
|
+
yamlText: string,
|
|
244
|
+
declaredUrl: string,
|
|
245
|
+
appContext: KitAppContext,
|
|
246
|
+
ignoreDefaultStyleEngine: boolean,
|
|
247
|
+
): string | undefined {
|
|
248
|
+
try {
|
|
249
|
+
const normalized = normalizeKitCacheValue({
|
|
250
|
+
context: cacheInputOf(appContext, ignoreDefaultStyleEngine),
|
|
251
|
+
url: declaredUrl,
|
|
252
|
+
yaml: yamlText,
|
|
253
|
+
});
|
|
254
|
+
return createHash('sha256')
|
|
255
|
+
.update(KIT_GRAMMAR_DIGEST)
|
|
256
|
+
.update('\0')
|
|
257
|
+
.update(canonicalJson(normalized))
|
|
258
|
+
.digest('hex');
|
|
259
|
+
} catch {
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** 计算确定性输入指纹;函数用引用身份而非 toString,无法安全归一时返回 undefined。 */
|
|
40
265
|
export function computeKitPageCacheKey(
|
|
41
266
|
yamlText: string,
|
|
42
267
|
declaredUrl: string,
|
|
43
268
|
appContext: KitAppContext,
|
|
44
|
-
): string {
|
|
45
|
-
return
|
|
46
|
-
.update(KIT_GRAMMAR_VERSION)
|
|
47
|
-
.update('\0')
|
|
48
|
-
.update(appContext.themeTokensCss ?? '')
|
|
49
|
-
.update('\0')
|
|
50
|
-
.update(appContext.themeCss)
|
|
51
|
-
.update('\0')
|
|
52
|
-
.update(JSON.stringify(appContext.envelope ?? null))
|
|
53
|
-
.update('\0')
|
|
54
|
-
.update(JSON.stringify(appContext.layout ?? null))
|
|
55
|
-
.update('\0')
|
|
56
|
-
.update(JSON.stringify(appContext.theme ?? null))
|
|
57
|
-
.update('\0')
|
|
58
|
-
.update(JSON.stringify(appContext.locales ?? null))
|
|
59
|
-
.update('\0')
|
|
60
|
-
.update((appContext.appCandidates ?? []).join('\0'))
|
|
61
|
-
.update('\0')
|
|
62
|
-
.update(declaredUrl)
|
|
63
|
-
.update('\0')
|
|
64
|
-
.update(yamlText)
|
|
65
|
-
.digest('hex');
|
|
269
|
+
): string | undefined {
|
|
270
|
+
return computeKitPageCacheKeyInternal(yamlText, declaredUrl, appContext, false);
|
|
66
271
|
}
|
|
67
272
|
|
|
68
273
|
function cloneKitPageArtifact(artifact: KitPageArtifact): KitPageArtifact {
|
|
@@ -101,6 +306,12 @@ export interface KitServerFace {
|
|
|
101
306
|
prewarm(): Promise<void>;
|
|
102
307
|
/** 按 URL 提交 / 撤下 / 列出动态页面(owner `soma-kit`;授权取 `defineKitApp({ pageGrants })`) */
|
|
103
308
|
readonly pages: KitServerPages;
|
|
309
|
+
/** 运行期主题端点:按九维声明求解令牌 CSS(浏览器切换主题用;见 theme-endpoint.ts) */
|
|
310
|
+
themeResponse(request: Request): Response;
|
|
311
|
+
/** dev 降级页面的编译诊断(生产模式与 check 模式为空) */
|
|
312
|
+
readonly diagnostics?: readonly KitDiagnostic[];
|
|
313
|
+
/** 九维主题求解产生的降级提示清单 */
|
|
314
|
+
readonly themeDegradations?: readonly NineDimDegradation[];
|
|
104
315
|
}
|
|
105
316
|
|
|
106
317
|
const nodeFs = (): Promise<typeof import('node:fs/promises')> => import(/* webpackIgnore: true */ 'node:fs/promises');
|
|
@@ -116,13 +327,119 @@ async function readIfExists(file: string): Promise<string | undefined> {
|
|
|
116
327
|
}
|
|
117
328
|
}
|
|
118
329
|
|
|
119
|
-
async function parseYaml(text: string, label: string): Promise<unknown> {
|
|
330
|
+
async function parseYaml(text: string, label: string, diagnostics: KitDiagnostic[]): Promise<unknown> {
|
|
120
331
|
const { parseSomaDsl } = await import('soma-core/server/runtime');
|
|
121
332
|
const r = parseSomaDsl(text, 'ui');
|
|
122
|
-
if (!r.ok)
|
|
333
|
+
if (!r.ok) {
|
|
334
|
+
diagnostics.push({ code: 'KIT-APP-001', path: label, message: r.diagnostics.map((d) => d.message).join(';') });
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
123
337
|
return r.value;
|
|
124
338
|
}
|
|
125
339
|
|
|
340
|
+
async function pageUrlFromSource(text: string): Promise<string | undefined> {
|
|
341
|
+
try {
|
|
342
|
+
const { parseSomaDsl } = await import('soma-core/server/runtime');
|
|
343
|
+
const parsed = parseSomaDsl(text, 'ui');
|
|
344
|
+
if (!parsed.ok || parsed.value === null || typeof parsed.value !== 'object' || Array.isArray(parsed.value)) return undefined;
|
|
345
|
+
const page = (parsed.value as Record<string, unknown>).page;
|
|
346
|
+
if (page === null || typeof page !== 'object' || Array.isArray(page)) return undefined;
|
|
347
|
+
const url = (page as Record<string, unknown>).url;
|
|
348
|
+
return typeof url === 'string' ? url : undefined;
|
|
349
|
+
} catch {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** dev 模式下单页编译失败时生成可读的诊断降级页产物,不阻断其它正常页面访问 */
|
|
355
|
+
function buildDevDiagnosticFallbackArtifact(
|
|
356
|
+
url: string,
|
|
357
|
+
file: string,
|
|
358
|
+
diags: readonly KitDiagnostic[],
|
|
359
|
+
appContext: KitAppContext,
|
|
360
|
+
): KitPageArtifact {
|
|
361
|
+
const itemsHtml = diags
|
|
362
|
+
.map((d) => {
|
|
363
|
+
const loc = d.line !== undefined ? `${d.file ?? file}:${d.line}${d.column !== undefined ? `:${d.column}` : ''}` : (d.path ?? file);
|
|
364
|
+
const hintHtml = d.hint ? `<div class="mt-1 text-xs text-fg-muted">建议:${escapeHtml(d.hint)}</div>` : '';
|
|
365
|
+
return `<li class="rounded border border-subtle bg-surface p-3"><div class="font-mono text-xs font-semibold text-danger-on-soft">[${escapeHtml(d.code)}] ${escapeHtml(loc)}</div><div class="mt-1 text-sm text-fg">${escapeHtml(d.message)}</div>${hintHtml}</li>`;
|
|
366
|
+
})
|
|
367
|
+
.join('\n');
|
|
368
|
+
const html = `<div data-soma-host="${KIT_ROOT_HOST}" data-kit-dev-diagnostic="true" class="p-6 space-y-4"><div class="rounded border border-subtle bg-surface-sunken p-4"><h1 class="text-base font-semibold text-danger-on-soft">页面编译失败(开发态诊断降级页):${escapeHtml(file)}</h1><p class="mt-1 text-xs text-fg-muted">路由:${escapeHtml(url)} — 修复以下 ${diags.length} 条诊断后保存即可恢复正常渲染</p><ul class="mt-3 space-y-2">\n${itemsHtml}\n</ul></div></div>`;
|
|
369
|
+
const css = [appContext.themeTokensCss ?? '', appContext.themeCss].filter((s) => s !== '').join('\n');
|
|
370
|
+
const version = createHash('sha256')
|
|
371
|
+
.update('dev-fallback:')
|
|
372
|
+
.update(url)
|
|
373
|
+
.update('\0')
|
|
374
|
+
.update(JSON.stringify(diags))
|
|
375
|
+
.digest('hex')
|
|
376
|
+
.slice(0, 16);
|
|
377
|
+
return {
|
|
378
|
+
url,
|
|
379
|
+
html,
|
|
380
|
+
uiYaml: JSON.stringify({ soma: 1, bind: { obj: KIT_ROOT_HOST, var: 'page' }, show: {}, envelope: { url, title: `编译诊断: ${file}` } }),
|
|
381
|
+
eventYaml: JSON.stringify({}),
|
|
382
|
+
styles: [{ key: 'kit-page', css }],
|
|
383
|
+
version,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
interface ScannedPageFile {
|
|
388
|
+
dir: string;
|
|
389
|
+
/** 相对于所在页面目录(如 orders/detail.kit.yaml) */
|
|
390
|
+
relPath: string;
|
|
391
|
+
/** 诊断展示路径 */
|
|
392
|
+
file: string;
|
|
393
|
+
/** 绝对路径 */
|
|
394
|
+
fullPath: string;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** 递归扫描页面目录:收集 .kit.yaml 页面,并对 .kit.yml 或非 .kit.yaml 的 .yaml/.yml 文件报 KIT-PAGE-001 诊断 */
|
|
398
|
+
async function scanPageDirectory(
|
|
399
|
+
fs: typeof import('node:fs/promises'),
|
|
400
|
+
path: typeof import('node:path'),
|
|
401
|
+
root: string,
|
|
402
|
+
baseDir: string,
|
|
403
|
+
isPrimaryDir: boolean,
|
|
404
|
+
pageFiles: ScannedPageFile[],
|
|
405
|
+
failures: KitDiagnostic[],
|
|
406
|
+
): Promise<void> {
|
|
407
|
+
const walk = async (currentDir: string): Promise<void> => {
|
|
408
|
+
let entries: import('node:fs').Dirent[];
|
|
409
|
+
try {
|
|
410
|
+
entries = await fs.readdir(currentDir, { withFileTypes: true });
|
|
411
|
+
} catch (error) {
|
|
412
|
+
if ((error as { code?: string }).code === 'ENOENT') return;
|
|
413
|
+
throw error;
|
|
414
|
+
}
|
|
415
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
416
|
+
for (const entry of entries) {
|
|
417
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules') continue;
|
|
418
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
419
|
+
if (entry.isDirectory()) {
|
|
420
|
+
await walk(fullPath);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (!entry.isFile()) continue;
|
|
424
|
+
const relPath = path.relative(baseDir, fullPath).split(path.sep).join('/');
|
|
425
|
+
const displayFile = isPrimaryDir ? relPath : (path.relative(root, fullPath).split(path.sep).join('/') || relPath);
|
|
426
|
+
if (entry.name.endsWith('.kit.yaml')) {
|
|
427
|
+
pageFiles.push({ dir: baseDir, relPath, file: displayFile, fullPath });
|
|
428
|
+
} else if (entry.name.endsWith('.kit.yml') || entry.name.endsWith('.yaml') || entry.name.endsWith('.yml')) {
|
|
429
|
+
const suggested = displayFile.replace(/\.(?:kit\.yml|ya?ml)$/i, '.kit.yaml');
|
|
430
|
+
failures.push({
|
|
431
|
+
code: 'KIT-PAGE-001',
|
|
432
|
+
path: displayFile,
|
|
433
|
+
file: displayFile,
|
|
434
|
+
message: `页面文件 ${displayFile} 后缀不受支持(soma-kit 页面文件必须以 .kit.yaml 结尾)`,
|
|
435
|
+
hint: `将 ${displayFile} 重命名为 ${suggested}`,
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
await walk(baseDir);
|
|
441
|
+
}
|
|
442
|
+
|
|
126
443
|
/** 装配 server 面:读配置 → 对账登记 → 建上下文 → 编译全部页面 → 注册。失败抛 KitAppAssemblyError */
|
|
127
444
|
export async function assembleKitServer(options: DefineKitAppOptions, registry: KitAppRegistry): Promise<KitServerFace> {
|
|
128
445
|
const path = await nodePath();
|
|
@@ -133,19 +450,18 @@ export async function assembleKitServer(options: DefineKitAppOptions, registry:
|
|
|
133
450
|
const appFile = path.join(root, options.app ?? 'kit.app.yaml');
|
|
134
451
|
const appText = await readIfExists(appFile);
|
|
135
452
|
const diagnostics: KitDiagnostic[] = [];
|
|
136
|
-
const
|
|
453
|
+
const parsedAppYaml = appText === undefined ? undefined : await parseYaml(appText, 'kit.app.yaml', diagnostics);
|
|
454
|
+
const config = parseAppConfig(parsedAppYaml, diagnostics);
|
|
137
455
|
|
|
138
456
|
// 2 与代码登记对账(两边都要有)
|
|
139
457
|
for (const name of Object.keys(config.baseComponents)) {
|
|
140
458
|
if (!(name in registry.baseComponents)) diagnostics.push({ code: 'KIT-APP-001', path: `baseComponents.${name}`, message: `kit.app.yaml 登记了基础组件 ${name},soma.app.ts 没有提供实现` });
|
|
141
459
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
const staticContracts: Record<string, { accept: 'single' | 'list'; emits: readonly string[]; props: readonly string[] }> = {};
|
|
460
|
+
// 只在代码里登记(kit.app.yaml#baseComponents 没有条目):不报错,按代码登记即可(不往配置对象里写东西)
|
|
461
|
+
const staticContracts: Record<string, { accept: 'single' | 'list'; emits: readonly string[]; props: readonly string[]; propTypes: Readonly<Record<string, string>> }> = {};
|
|
146
462
|
for (const spec of KIT_STATIC_BUILTIN_SPECS) {
|
|
147
463
|
const def = spec.define();
|
|
148
|
-
staticContracts[spec.name] = { accept: spec.accept, emits: Object.keys(def.contract.emits ?? {}), props: Object.keys(def.contract.props ?? {}) };
|
|
464
|
+
staticContracts[spec.name] = { accept: spec.accept, emits: Object.keys(def.contract.emits ?? {}), props: Object.keys(def.contract.props ?? {}), propTypes: staticPropTypesOf(def) };
|
|
149
465
|
}
|
|
150
466
|
for (const name of Object.keys(config.staticObjectComponents)) {
|
|
151
467
|
if (!(name in registry.staticComponents)) diagnostics.push({ code: 'KIT-APP-001', path: `staticObjectComponents.${name}`, message: `kit.app.yaml 登记了数据组件 ${name},soma.app.ts 没有提供实现` });
|
|
@@ -159,40 +475,38 @@ export async function assembleKitServer(options: DefineKitAppOptions, registry:
|
|
|
159
475
|
if (accept === undefined) {
|
|
160
476
|
diagnostics.push({ code: 'KIT-APP-001', path: `staticObjectComponents.${name}`, message: `数据组件 ${name} 没有 accept:请在 kit.app.yaml 的 staticObjectComponents 登记` });
|
|
161
477
|
} else {
|
|
162
|
-
staticContracts[name] = { accept, emits: Object.keys(entry.definition.contract.emits ?? {}), props: Object.keys(entry.definition.contract.props ?? {}) };
|
|
478
|
+
staticContracts[name] = { accept, emits: Object.keys(entry.definition.contract.emits ?? {}), props: Object.keys(entry.definition.contract.props ?? {}), propTypes: staticPropTypesOf(entry.definition) };
|
|
163
479
|
}
|
|
164
480
|
}
|
|
165
481
|
const sourceGateDiagnostics = await scanAppComponentSources(root);
|
|
166
482
|
diagnostics.push(...sourceGateDiagnostics);
|
|
167
|
-
if (diagnostics.length > 0) throw new KitAppAssemblyError('应用配置或扩展组件未通过唯一接入路径编译检查,拒绝启动', diagnostics);
|
|
168
483
|
|
|
169
|
-
// 3
|
|
170
|
-
let themeCss
|
|
484
|
+
// 3 主题头(不在此处提前抛错,确保后面页面的编译诊断也能一次收集报全)
|
|
485
|
+
let themeCss = KIT_DEFAULT_THEME_CSS;
|
|
171
486
|
if (config.style !== undefined) {
|
|
172
487
|
const text = await readIfExists(path.resolve(root, config.style));
|
|
173
|
-
if (text === undefined)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
488
|
+
if (text === undefined) {
|
|
489
|
+
diagnostics.push({ code: 'KIT-APP-001', path: 'style', message: `找不到 ${config.style}` });
|
|
490
|
+
} else {
|
|
491
|
+
themeCss = text;
|
|
492
|
+
}
|
|
177
493
|
}
|
|
178
494
|
|
|
179
495
|
// 令牌 CSS:theme 声明交 soma-foundation 求解(亮 / 暗两套),拼在主题头前面;没写 theme 用 foundation 默认方案
|
|
180
496
|
// kit.app.yaml 写了 theme 则以 yaml 为准;没写才用 defineKitApp 的 defaultTheme(外壳继承用)
|
|
497
|
+
if (options.defaultTheme !== undefined) {
|
|
498
|
+
checkRemovedThemeKeys(options.defaultTheme, 'defaultTheme', diagnostics);
|
|
499
|
+
}
|
|
181
500
|
const sourceTheme = (config.theme ?? options.defaultTheme) as KitAppTheme | KitThemeConfig | undefined;
|
|
182
|
-
const themeInput: KitThemeConfig | undefined = sourceTheme === undefined ? undefined :
|
|
183
|
-
...(sourceTheme.paletteScheme !== undefined ? { paletteScheme: sourceTheme.paletteScheme } : {}),
|
|
184
|
-
...(sourceTheme.fontScheme !== undefined ? { fontScheme: sourceTheme.fontScheme } : {}),
|
|
185
|
-
...(sourceTheme.color !== undefined ? { color: sourceTheme.color } : {}),
|
|
186
|
-
...(sourceTheme.mode !== undefined ? { mode: sourceTheme.mode as KitThemeConfig['mode'] } : {}),
|
|
187
|
-
...(sourceTheme.density !== undefined ? { density: sourceTheme.density as KitThemeConfig['density'] } : {}),
|
|
188
|
-
};
|
|
501
|
+
const themeInput: KitThemeConfig | undefined = sourceTheme === undefined ? undefined : kitThemeConfigOf(sourceTheme);
|
|
189
502
|
const tokens = resolveKitThemeTokens(themeInput, options.defaultLocale);
|
|
190
|
-
if (tokens.diagnostics.length > 0)
|
|
503
|
+
if (tokens.diagnostics.length > 0) {
|
|
504
|
+
diagnostics.push(...tokens.diagnostics);
|
|
505
|
+
}
|
|
191
506
|
|
|
192
507
|
// 应用词条表:`locales/<语言>.yaml`(平铺 `键: 文案`)读成 { 语言: { 键: 文案 } };没有该目录就没有应用词条表
|
|
193
508
|
const localeTables: Record<string, Record<string, string>> = {};
|
|
194
509
|
{
|
|
195
|
-
const fs = await nodeFs();
|
|
196
510
|
let names: string[] = [];
|
|
197
511
|
try {
|
|
198
512
|
names = (await fs.readdir(path.join(root, 'locales'))).filter((n) => /\.ya?ml$/.test(n)).sort();
|
|
@@ -200,9 +514,12 @@ export async function assembleKitServer(options: DefineKitAppOptions, registry:
|
|
|
200
514
|
if ((error as { code?: string }).code !== 'ENOENT') throw error;
|
|
201
515
|
}
|
|
202
516
|
for (const n of names) {
|
|
203
|
-
const
|
|
517
|
+
const before = diagnostics.length;
|
|
518
|
+
const doc = await parseYaml((await readIfExists(path.join(root, 'locales', n))) ?? '', `locales/${n}`, diagnostics);
|
|
519
|
+
if (diagnostics.length > before) continue;
|
|
204
520
|
if (doc === null || typeof doc !== 'object' || Array.isArray(doc)) {
|
|
205
|
-
|
|
521
|
+
diagnostics.push({ code: 'KIT-APP-001', path: `locales/${n}`, message: '词条表必须是平铺的「键: 文案」映射' });
|
|
522
|
+
continue;
|
|
206
523
|
}
|
|
207
524
|
const table: Record<string, string> = {};
|
|
208
525
|
for (const [k, v] of Object.entries(doc as Record<string, unknown>)) if (typeof v === 'string') table[k] = v;
|
|
@@ -218,17 +535,25 @@ export async function assembleKitServer(options: DefineKitAppOptions, registry:
|
|
|
218
535
|
sharedStyleEngine = createStyleEnginePort();
|
|
219
536
|
}
|
|
220
537
|
|
|
221
|
-
// 业务信封约定:编译期编进 core
|
|
222
|
-
|
|
538
|
+
// 业务信封约定:编译期编进 core 数据声明;交给本应用实例(registry)持有,同时随 appContext.envelope 沿装配对象传递,
|
|
539
|
+
// 服务端薄壳(分页适配等)从装配对象取来显式传入——不写任何进程全局,同进程多应用互不覆盖(重装配同一应用用新值覆盖本应用自己的持有者)
|
|
540
|
+
registry.appEnvelope.current = config.envelope;
|
|
541
|
+
const mergedDicts = config.dicts ?? options.dicts;
|
|
542
|
+
const mergedEntities = config.entities ?? options.entities;
|
|
223
543
|
const appContext: KitAppContext = {
|
|
544
|
+
...(mergedDicts !== undefined ? { dicts: mergedDicts } : {}),
|
|
545
|
+
...(mergedEntities !== undefined ? { entities: mergedEntities } : {}),
|
|
224
546
|
...(config.envelope !== undefined ? { envelope: config.envelope } : {}),
|
|
225
547
|
...(config.layout !== undefined ? { layout: config.layout } : {}),
|
|
548
|
+
...((options.appName ?? options.title) !== undefined ? { appName: options.appName ?? options.title } : {}),
|
|
226
549
|
...(options.defaultLocale !== undefined ? { defaultLocale: options.defaultLocale } : {}),
|
|
227
550
|
...(Object.keys(localeTables).length > 0 ? { locales: localeTables } : {}),
|
|
228
551
|
themeTokensCss: tokens.css,
|
|
229
552
|
...(themeInput !== undefined ? { themeConfig: themeInput } : {}),
|
|
230
553
|
...((config.theme ?? (options.defaultTheme as KitAppTheme | undefined)) !== undefined ? { theme: config.theme ?? (options.defaultTheme as KitAppTheme | undefined) } : {}),
|
|
554
|
+
...(tokens.degradations !== undefined && tokens.degradations.length > 0 ? { themeDegradations: tokens.degradations } : {}),
|
|
231
555
|
registeredActions: new Set(Object.keys(registry.actions)),
|
|
556
|
+
...(Object.keys(registry.viewInfos).length > 0 ? { views: registry.viewInfos } : {}),
|
|
232
557
|
registeredComponents: new Set([...Object.keys(KIT_BASE_COMPONENTS), ...KIT_FORM_FIELD_DATA_COMPONENTS, ...Object.keys(registry.baseComponents)]),
|
|
233
558
|
staticComponents: staticContracts,
|
|
234
559
|
themeCss,
|
|
@@ -243,79 +568,127 @@ export async function assembleKitServer(options: DefineKitAppOptions, registry:
|
|
|
243
568
|
},
|
|
244
569
|
};
|
|
245
570
|
|
|
246
|
-
// 4
|
|
571
|
+
// 4 递归扫描并编译页面
|
|
247
572
|
// 主页面目录 + extraPagesDirs(绝对路径或相对应用根);同一目录只扫一次
|
|
248
573
|
const pageDirs = [...new Set([path.join(root, options.pagesDir ?? 'pages'), ...(options.extraPagesDirs ?? []).map((d) => path.resolve(root, d))])];
|
|
249
|
-
const pageFiles:
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if ((error as { code?: string }).code !== 'ENOENT') throw error;
|
|
255
|
-
}
|
|
574
|
+
const pageFiles: ScannedPageFile[] = [];
|
|
575
|
+
const failures: KitDiagnostic[] = [];
|
|
576
|
+
for (let i = 0; i < pageDirs.length; i++) {
|
|
577
|
+
const dir = pageDirs[i]!;
|
|
578
|
+
await scanPageDirectory(fs, path, root, dir, i === 0, pageFiles, failures);
|
|
256
579
|
}
|
|
580
|
+
|
|
257
581
|
const artifacts = new Map<string, KitPageArtifact>();
|
|
258
|
-
const
|
|
582
|
+
const viewUsers = new Map<string, string>();
|
|
583
|
+
const degradedDiagnostics: KitDiagnostic[] = [];
|
|
259
584
|
const isCustomStyleEngine = customStyleEngine !== undefined;
|
|
585
|
+
const devFallback = options.devFallback === true && process.env.NODE_ENV !== 'production';
|
|
586
|
+
const allowDevFallback =
|
|
587
|
+
devFallback ||
|
|
588
|
+
(process.env.SOMA_DEV_SERVER === '1' && process.env.SOMA_KIT_CHECK !== '1' && process.env.NODE_ENV !== 'production');
|
|
260
589
|
|
|
261
|
-
for (const {
|
|
262
|
-
const
|
|
263
|
-
const
|
|
590
|
+
for (const { relPath, file, fullPath } of pageFiles) {
|
|
591
|
+
const text = await fs.readFile(fullPath, 'utf8');
|
|
592
|
+
const inferredUrl = inferPageUrlFromPath(relPath);
|
|
593
|
+
const hasSubdir = relPath.includes('/');
|
|
594
|
+
const defaultUrl = hasSubdir ? inferredUrl : undefined;
|
|
264
595
|
|
|
265
596
|
let artifact: KitPageArtifact | undefined;
|
|
266
|
-
const
|
|
267
|
-
const
|
|
268
|
-
const
|
|
269
|
-
|
|
597
|
+
const declaredUrl = (await pageUrlFromSource(text)) ?? (defaultUrl ?? '');
|
|
598
|
+
const viewMatch = /^\s*view:\s*['"]?([A-Za-z0-9_]+)['"]?/m.exec(text);
|
|
599
|
+
const rawViewName = viewMatch ? viewMatch[1] : undefined;
|
|
600
|
+
|
|
601
|
+
const fingerprint = !isCustomStyleEngine
|
|
602
|
+
? computeKitPageCacheKeyInternal(text, declaredUrl, appContext, true)
|
|
270
603
|
: undefined;
|
|
604
|
+
const cacheKey = fingerprint === undefined ? undefined : scopedKitPageCacheKey(registry, fingerprint);
|
|
271
605
|
|
|
272
606
|
if (cacheKey !== undefined) {
|
|
273
|
-
const cached =
|
|
274
|
-
if (cached !== undefined)
|
|
275
|
-
kitPageCompileCache.delete(cacheKey);
|
|
276
|
-
kitPageCompileCache.set(cacheKey, cached);
|
|
277
|
-
artifact = deepFreezeKitPageArtifact(cloneKitPageArtifact(cached));
|
|
278
|
-
}
|
|
607
|
+
const cached = cachedKitPageArtifact(cacheKey);
|
|
608
|
+
if (cached !== undefined) artifact = deepFreezeKitPageArtifact(cloneKitPageArtifact(cached));
|
|
279
609
|
}
|
|
280
610
|
|
|
281
611
|
if (artifact === undefined) {
|
|
282
|
-
|
|
612
|
+
let res: KitCompileResult;
|
|
613
|
+
try {
|
|
614
|
+
res = await compileKitPage(text, appContext, defaultUrl !== undefined ? { defaultUrl } : undefined);
|
|
615
|
+
} catch (err) {
|
|
616
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
617
|
+
res = {
|
|
618
|
+
diagnostics: [
|
|
619
|
+
{
|
|
620
|
+
code: 'KIT-PAGE-001',
|
|
621
|
+
path: 'page',
|
|
622
|
+
file,
|
|
623
|
+
message: `页面编译异常: ${msg}`,
|
|
624
|
+
},
|
|
625
|
+
],
|
|
626
|
+
};
|
|
627
|
+
}
|
|
283
628
|
if (res.artifact === undefined || res.diagnostics.length > 0) {
|
|
284
629
|
const locate = createYamlLocator(text);
|
|
285
|
-
|
|
286
|
-
|
|
630
|
+
const pageDiags: KitDiagnostic[] = res.diagnostics.map((d: KitDiagnostic) => {
|
|
631
|
+
// 诊断自带行列(如 YAML 语法错误的真实位置)时不再用路径定位覆盖
|
|
632
|
+
const pos = d.line !== undefined && d.column !== undefined ? undefined : locate(d.path);
|
|
287
633
|
return { ...d, path: `${file}:${d.path}`, file, ...(pos ? { line: pos.line, column: pos.column } : {}) };
|
|
288
|
-
})
|
|
634
|
+
});
|
|
289
635
|
// 没产物又没诊断也是失败:不许静默吞掉一页
|
|
290
|
-
if (
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
636
|
+
if (pageDiags.length === 0) {
|
|
637
|
+
pageDiags.push({ code: 'KIT-PAGE-004', path: file, file, message: '页面没有产出产物,也没有给出诊断' });
|
|
638
|
+
}
|
|
639
|
+
if (rawViewName !== undefined && !viewUsers.has(rawViewName)) {
|
|
640
|
+
viewUsers.set(rawViewName, file);
|
|
641
|
+
}
|
|
642
|
+
if (allowDevFallback && diagnostics.length === 0) {
|
|
643
|
+
const fallbackUrl = declaredUrl.startsWith('/') ? declaredUrl : inferredUrl;
|
|
644
|
+
degradedDiagnostics.push(...pageDiags);
|
|
645
|
+
if (!artifacts.has(fallbackUrl)) {
|
|
646
|
+
artifacts.set(fallbackUrl, buildDevDiagnosticFallbackArtifact(fallbackUrl, file, pageDiags, appContext));
|
|
647
|
+
}
|
|
648
|
+
continue;
|
|
299
649
|
}
|
|
300
|
-
|
|
650
|
+
failures.push(...pageDiags);
|
|
651
|
+
continue;
|
|
301
652
|
}
|
|
653
|
+
artifact = res.artifact!;
|
|
654
|
+
if (cacheKey !== undefined) storeKitPageArtifact(cacheKey, artifact);
|
|
302
655
|
}
|
|
303
656
|
|
|
657
|
+
if (artifact === undefined) continue;
|
|
304
658
|
if (artifacts.has(artifact.url)) {
|
|
305
|
-
failures.push({ code: 'KIT-PAGE-004', path: file, message: `页面地址 ${artifact.url} 重复` });
|
|
659
|
+
failures.push({ code: 'KIT-PAGE-004', path: file, file, message: `页面地址 ${artifact.url} 重复` });
|
|
306
660
|
continue;
|
|
307
661
|
}
|
|
308
662
|
artifacts.set(artifact.url, artifact);
|
|
663
|
+
// 展示对象与页面 1:1:编译产物的 domain 就是页面引用的展示对象,一个展示对象至多被一页引用
|
|
664
|
+
const viewName = (JSON.parse(artifact.uiYaml) as { domain?: unknown }).domain;
|
|
665
|
+
if (typeof viewName === 'string') {
|
|
666
|
+
const owner = viewUsers.get(viewName);
|
|
667
|
+
if (owner !== undefined) failures.push({ code: 'KIT-VIEW-002', path: `${file}:page.view`, file, message: `展示对象 ${viewName} 已被页面 ${owner} 引用,一个展示对象只能对应一个页面(展示对象与页面 1:1)` });
|
|
668
|
+
else viewUsers.set(viewName, file);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
for (const name of Object.keys(registry.views)) {
|
|
672
|
+
if (!viewUsers.has(name)) failures.push({ code: 'KIT-VIEW-005', path: `views.${name}`, message: `展示对象 ${name} 已登记,但没有页面用 page.view: ${name} 引用它(展示对象与页面 1:1)` });
|
|
673
|
+
}
|
|
674
|
+
const allFailures = [...diagnostics, ...failures];
|
|
675
|
+
if (allFailures.length > 0) {
|
|
676
|
+
throw new KitAppAssemblyError('应用配置或页面编译未通过检查,拒绝启动', allFailures);
|
|
309
677
|
}
|
|
310
|
-
if (failures.length > 0) throw new KitAppAssemblyError('页面编译失败,拒绝启动', failures);
|
|
311
678
|
|
|
312
679
|
// 5 注册(若同一 URL 已由 soma-kit 预热落盘,再次装配时保持 persist: true,避免抹除持久绑定文件)
|
|
313
680
|
const register = async (persist: boolean): Promise<void> => {
|
|
314
|
-
const { listBindings, registerSomaPages } = await import('soma-core/server/runtime');
|
|
681
|
+
const { listBindings, registerSomaPages, withdrawPage } = await import('soma-core/server/runtime');
|
|
682
|
+
const existing = listBindings();
|
|
683
|
+
for (const b of existing) {
|
|
684
|
+
if (b.owner === KIT_OWNER && b.caller === null && !artifacts.has(b.address)) {
|
|
685
|
+
withdrawPage(b.address, KIT_OWNER);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
315
688
|
const persistedUrls = persist
|
|
316
689
|
? null
|
|
317
690
|
: new Set(
|
|
318
|
-
|
|
691
|
+
existing
|
|
319
692
|
.filter((b) => b.owner === KIT_OWNER && b.persisted)
|
|
320
693
|
.map((b) => b.address),
|
|
321
694
|
);
|
|
@@ -324,7 +697,13 @@ export async function assembleKitServer(options: DefineKitAppOptions, registry:
|
|
|
324
697
|
return {
|
|
325
698
|
url: a.url,
|
|
326
699
|
page: { html: a.html, uiYaml: a.uiYaml, eventYaml: a.eventYaml, styles: a.styles, version: a.version },
|
|
327
|
-
options: {
|
|
700
|
+
options: {
|
|
701
|
+
owner: KIT_OWNER,
|
|
702
|
+
version: a.version,
|
|
703
|
+
...(keepPersist ? { persist: true } : {}),
|
|
704
|
+
// 页面引用了展示对象(domain:):类由浏览器侧 browserMount().pageRoots 提供,注入期不提示 CORE-RUNTIME-DOMAIN-STATIC-ONLY
|
|
705
|
+
...(typeof (JSON.parse(a.uiYaml) as { domain?: unknown }).domain === 'string' ? { domainProvided: true } : {}),
|
|
706
|
+
},
|
|
328
707
|
};
|
|
329
708
|
});
|
|
330
709
|
const outcomes = registerSomaPages(entries);
|
|
@@ -348,7 +727,18 @@ export async function assembleKitServer(options: DefineKitAppOptions, registry:
|
|
|
348
727
|
tableAddresses: () => [...artifacts.keys()],
|
|
349
728
|
});
|
|
350
729
|
|
|
351
|
-
|
|
730
|
+
// 运行期主题端点:与编译期同一条求解链(见 theme-endpoint.ts);defineKitApp 的 requestHook 把 KIT_THEME_ENDPOINT 的请求交到这里
|
|
731
|
+
const themeResponse = (request: Request): Response => handleKitThemeRequest(request, options.defaultLocale, themeInput);
|
|
732
|
+
|
|
733
|
+
return {
|
|
734
|
+
appContext,
|
|
735
|
+
artifacts,
|
|
736
|
+
prewarm: () => register(true),
|
|
737
|
+
pages,
|
|
738
|
+
themeResponse,
|
|
739
|
+
...(degradedDiagnostics.length > 0 ? { diagnostics: degradedDiagnostics } : {}),
|
|
740
|
+
...(tokens.degradations !== undefined && tokens.degradations.length > 0 ? { themeDegradations: tokens.degradations } : {}),
|
|
741
|
+
};
|
|
352
742
|
}
|
|
353
743
|
|
|
354
744
|
(globalThis as Record<symbol, unknown>)[Symbol.for('soma-kit.serverModule')] = { assembleKitServer };
|