soma-core 2.2.4 → 2.2.6
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 +5 -6
- package/bin/soma.cjs +2 -2
- package/core/build/compiler/flat-page.ts +16 -0
- package/core/build/compiler/full-manifest-builder.ts +0 -3
- package/core/build/compiler/page-assets.ts +0 -1
- package/core/build/compiler/page-closure.ts +245 -0
- package/core/build/compiler/page-declaration-compiler.ts +2 -5
- package/core/build/compiler/soma-precompile.js +1848 -3244
- package/core/build/compiler/soma-precompile.ts +4 -7
- package/core/build/component-tree/artifact-codec.ts +4 -8
- package/core/build/lsp/soma-declaration.cjs +1 -1
- package/core/build/lsp/std-completion.generated.json +3 -3
- package/core/client/a11y/a11y-config.ts +2 -1
- package/core/client/a11y/dom-id.ts +1 -1
- package/core/client/base/SomaHttpClient.ts +11 -3
- package/core/client/base/SomaPageRootBase.ts +6 -0
- package/core/client/base/defineSomaView.ts +178 -0
- package/core/client/base/index.ts +13 -0
- package/core/client/base/view-context.ts +328 -0
- package/core/client/behavior/adapter-collection.ts +2 -2
- package/core/client/compiler/manifest-compiler.ts +5 -36
- package/core/client/component/component-definition.types.ts +2 -1
- package/core/client/component/component-manager.ts +110 -20
- package/core/client/component/structure-adapter.ts +3 -7
- package/core/client/container/SomaContainerAdapter.ts +9 -1
- package/core/client/data/data-object-resolver.ts +516 -181
- package/core/client/data/data-write-actions.ts +79 -15
- package/core/client/data/format-config.ts +3 -1
- package/core/client/data/region-scope-port.ts +11 -0
- package/core/client/engine/asset-resolver.ts +2 -5
- package/core/client/engine/dom-renderer.ts +7 -40
- package/core/client/engine/event-engine.ts +1 -105
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +78 -17
- package/core/client/engine/runtime-builder.ts +0 -14
- package/core/client/engine/scope-resolver.ts +18 -0
- package/core/client/engine/soma-bootstrap.ts +385 -98
- package/core/client/engine/style-binder.ts +49 -106
- package/core/client/engine/timer-engine.ts +11 -1
- package/core/client/host-bridge/load-host-domain-class.ts +2 -1
- package/core/client/infra/http/SomaHttpService.ts +15 -4
- package/core/client/infra/i18n/pack-locales.ts +2 -2
- package/core/client/mount/domain-rule.ts +7 -3
- package/core/client/mount/mount-page.ts +38 -5
- package/core/client/mount/types.ts +2 -0
- package/core/client/parser/confirm-spec.ts +1 -1
- package/core/client/parser/data-config-parser.ts +27 -210
- package/core/client/parser/pipeline-parser.ts +14 -59
- package/core/client/parser/style-config-parser.ts +101 -95
- package/core/client/parser/timer-parser.ts +21 -0
- package/core/client/parser/ui-config-parser.ts +64 -12
- package/core/client/registry/declaration-renderer.ts +1 -1
- package/core/client/runtime/SomaRuntimeServices.ts +2 -1
- package/core/client/runtime/SomaSnapshotStore.ts +5 -0
- package/core/client/security/url-guard.ts +71 -11
- package/core/client/validation/async-check-registry.ts +2 -1
- package/core/client/validation/contract-validator.ts +2 -6
- package/core/client/validation/field-rules.ts +3 -13
- package/core/client/validation/object-rules.ts +2 -10
- package/core/index.ts +1 -35
- package/core/inspect/check-product.ts +5 -5
- package/core/inspect/runtime.ts +2 -1
- package/core/server/api/dispatch-service-api.ts +35 -36
- package/core/server/api/load-host-service.ts +5 -4
- package/core/server/compile-port.ts +1 -1
- package/core/server/host/app-config-composer.ts +2 -1
- package/core/server/host/host-assembly-entry.ts +1 -0
- package/core/server/host/host-assembly.ts +47 -3
- package/core/server/host/host-caller.ts +2 -1
- package/core/server/host/host-dir.ts +9 -0
- package/core/server/host/host-response-headers.ts +2 -1
- package/core/server/host/invoke-host-request-hook.ts +28 -4
- package/core/server/host/published-page-assets.ts +2 -1
- package/core/server/host/resolve-host-request-hook.ts +25 -7
- package/core/server/host/server-page-assets.ts +4 -4
- package/core/server/host/virtual-pages.ts +3 -2
- package/core/server/infra/cache/ServerFileCache.ts +79 -9
- package/core/server/registry/api-registry-store.ts +3 -1
- package/core/server/registry/declaration-precompiler.ts +0 -2
- package/core/server/registry/page-registry-repository.ts +2 -1
- package/core/server/registry/page-registry-store.ts +2 -1
- package/core/server/registry/registry-error.ts +6 -1
- package/core/server/registry/registry-file-lock.ts +296 -23
- package/core/server/registry/request-body-limit.ts +3 -2
- package/core/server/registry/validation/declaration-safety-scan.ts +151 -27
- package/core/server/runtime-space/access-gate.ts +21 -7
- package/core/server/runtime-space/bindings.ts +3 -6
- package/core/server/runtime-space/dynamic-pages.ts +181 -49
- package/core/server/runtime-space/errors.ts +3 -0
- package/core/server/runtime-space/inject.ts +74 -15
- package/core/server/runtime-space/page-grants.ts +7 -1
- package/core/server/runtime-space/resolve.ts +66 -19
- package/core/server/runtime-space/store.ts +32 -3
- package/core/server/runtime-space/submit-steps.ts +2 -1
- package/core/server/runtime-space/types.ts +3 -1
- package/core/shared/component-tree-errors.ts +2 -1
- package/core/shared/contract-a11y.ts +1 -1
- package/core/shared/contract-sections.ts +2 -2
- package/core/shared/core-diagnostic-codes.ts +8 -0
- package/core/shared/declaration-keys.ts +1 -1
- package/core/shared/engines/engine-error.ts +2 -1
- package/core/shared/generation-error.ts +2 -1
- package/core/shared/page-envelope.ts +1 -1
- package/core/shared/pure-binding.ts +88 -0
- package/core/shared/registry-generated/diagnostic-registry-index.ts +16 -19
- package/core/shared/registry-generated/diagnostic-registry.ts +135 -177
- package/core/shared/registry-generated/limits.ts +49 -13
- package/core/shared/registry-generated/std-grammar-completion.ts +0 -4
- package/core/shared/registry-generated/std-grammar-views.ts +40 -10
- package/core/shared/registry-generated/std-grammar.ts +42 -20
- package/core/shared/soma-error.ts +36 -0
- package/core/shared/types/compile-port.types.ts +1 -1
- package/core/shared/types/data-config.types.ts +17 -17
- package/core/shared/types/engine.types.ts +0 -3
- package/core/shared/types/event.types.ts +1 -11
- package/core/shared/types/generation-error.types.ts +5 -1
- package/core/shared/types/host-assembly.types.ts +7 -1
- package/core/shared/types/manifest.types.ts +0 -3
- package/core/shared/types/region-scope.types.ts +5 -1
- package/core/shared/types/scope-manifest.types.ts +0 -2
- package/core/shared/types/style-config.types.ts +21 -21
- package/core/shared/types/ui-config.types.ts +4 -5
- package/core/shared/types/watcher.types.ts +1 -1
- package/dist/browser/runtime-bundle.manifest.json +11 -13
- package/dist/browser/soma-runtime.js +2567 -2696
- package/dist/core/index.cjs +5446 -5486
- package/dist/host-native/aot-builder.cjs +46 -1
- package/dist/host-native/native-server.cjs +4879 -4505
- package/dist/lsp/component-tree.cjs +5814 -7042
- package/dist/types/build/compiler/full-manifest-builder.d.ts +0 -2
- package/dist/types/build/compiler/page-closure.d.ts +21 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -4
- package/dist/types/build/compiler/soma-precompile.d.ts +1 -3
- package/dist/types/build/component-tree/artifact-codec.d.ts +0 -2
- package/dist/types/client/a11y/a11y-config.d.ts +2 -1
- package/dist/types/client/base/SomaHttpClient.d.ts +1 -1
- package/dist/types/client/base/SomaPageRootBase.d.ts +5 -0
- package/dist/types/client/base/defineSomaView.d.ts +46 -0
- package/dist/types/client/base/index.d.ts +2 -0
- package/dist/types/client/base/view-context.d.ts +136 -0
- package/dist/types/client/behavior/adapter-collection.d.ts +1 -1
- package/dist/types/client/compiler/manifest-compiler.d.ts +0 -3
- package/dist/types/client/component/component-definition.types.d.ts +2 -1
- package/dist/types/client/component/component-manager.d.ts +29 -0
- package/dist/types/client/data/data-object-resolver.d.ts +91 -31
- package/dist/types/client/data/format-config.d.ts +2 -1
- package/dist/types/client/engine/dom-renderer.d.ts +1 -2
- package/dist/types/client/engine/event-engine.d.ts +0 -7
- package/dist/types/client/engine/reactive-proxy.d.ts +11 -4
- package/dist/types/client/engine/runtime-builder.d.ts +0 -4
- package/dist/types/client/engine/soma-bootstrap.d.ts +44 -6
- package/dist/types/client/engine/style-binder.d.ts +8 -4
- package/dist/types/client/host-bridge/load-host-domain-class.d.ts +2 -1
- package/dist/types/client/mount/domain-rule.d.ts +2 -0
- package/dist/types/client/mount/mount-page.d.ts +2 -1
- package/dist/types/client/mount/types.d.ts +2 -0
- package/dist/types/client/parser/data-config-parser.d.ts +6 -5
- package/dist/types/client/parser/style-config-parser.d.ts +7 -3
- package/dist/types/client/parser/timer-parser.d.ts +5 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +6 -2
- package/dist/types/client/runtime/SomaSnapshotStore.d.ts +2 -0
- package/dist/types/client/security/url-guard.d.ts +14 -0
- package/dist/types/client/validation/async-check-registry.d.ts +2 -1
- package/dist/types/client/validation/object-rules.d.ts +0 -5
- package/dist/types/index.d.ts +2 -7
- package/dist/types/inspect/runtime.d.ts +2 -1
- package/dist/types/server/api/dispatch-service-api.d.ts +5 -0
- package/dist/types/server/api/load-host-service.d.ts +1 -1
- package/dist/types/server/compile-port.d.ts +1 -1
- package/dist/types/server/host/host-assembly-entry.d.ts +1 -1
- package/dist/types/server/host/host-assembly.d.ts +2 -0
- package/dist/types/server/host/host-dir.d.ts +2 -0
- package/dist/types/server/host/invoke-host-request-hook.d.ts +10 -3
- package/dist/types/server/host/published-page-assets.d.ts +2 -1
- package/dist/types/server/host/resolve-host-request-hook.d.ts +18 -5
- package/dist/types/server/host/server-page-assets.d.ts +2 -1
- package/dist/types/server/infra/cache/ServerFileCache.d.ts +8 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +0 -1
- package/dist/types/server/registry/registry-error.d.ts +5 -1
- package/dist/types/server/registry/registry-file-lock.d.ts +30 -0
- package/dist/types/server/registry/request-body-limit.d.ts +3 -2
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +3 -0
- package/dist/types/server/runtime-space/access-gate.d.ts +7 -0
- package/dist/types/server/runtime-space/bindings.d.ts +3 -2
- package/dist/types/server/runtime-space/dynamic-pages.d.ts +6 -1
- package/dist/types/server/runtime-space/errors.d.ts +1 -1
- package/dist/types/server/runtime-space/resolve.d.ts +2 -0
- package/dist/types/server/runtime-space/store.d.ts +8 -1
- package/dist/types/server/runtime-space/submit-steps.d.ts +2 -1
- package/dist/types/server/runtime-space/types.d.ts +3 -1
- package/dist/types/shared/component-tree-errors.d.ts +2 -1
- package/dist/types/shared/core-diagnostic-codes.d.ts +1 -1
- package/dist/types/shared/declaration-keys.d.ts +1 -1
- package/dist/types/shared/engines/engine-error.d.ts +2 -1
- package/dist/types/shared/generation-error.d.ts +2 -1
- package/dist/types/shared/pure-binding.d.ts +48 -0
- package/dist/types/shared/registry-generated/diagnostic-registry.d.ts +119 -158
- package/dist/types/shared/registry-generated/limits.d.ts +40 -10
- package/dist/types/shared/registry-generated/std-grammar-completion.d.ts +0 -30
- package/dist/types/shared/registry-generated/std-grammar-views.d.ts +97 -23
- package/dist/types/shared/registry-generated/std-grammar.d.ts +109 -241
- package/dist/types/shared/soma-error.d.ts +15 -0
- package/dist/types/shared/types/data-config.types.d.ts +14 -16
- package/dist/types/shared/types/engine.types.d.ts +0 -3
- package/dist/types/shared/types/event.types.d.ts +1 -7
- package/dist/types/shared/types/generation-error.types.d.ts +1 -1
- package/dist/types/shared/types/host-assembly.types.d.ts +2 -0
- package/dist/types/shared/types/manifest.types.d.ts +0 -3
- package/dist/types/shared/types/region-scope.types.d.ts +4 -1
- package/dist/types/shared/types/scope-manifest.types.d.ts +0 -2
- package/dist/types/shared/types/style-config.types.d.ts +17 -19
- package/dist/types/shared/types/ui-config.types.d.ts +4 -5
- package/host-native/bin/host-native.cjs +22 -1
- package/host-native/client/browser-entry.ts +44 -9
- package/host-native/src/aot-builder.ts +52 -0
- package/host-native/src/host-prepare.ts +39 -5
- package/host-native/src/manifest-http.ts +2 -3
- package/host-native/src/native-server.ts +812 -174
- package/host-native/src/page-http.ts +3 -3
- package/host-native/src/public-packages.cjs +0 -1
- package/host-native/src/registry-handler.ts +12 -5
- package/host-native/src/static-file.ts +203 -53
- package/package.json +3 -2
- package/core/client/engine/step-runner.ts +0 -212
- package/core/client/engine/trigger-engine.ts +0 -55
- package/core/client/format/zoned.ts +0 -10
- package/core/client/parser/step-parser.ts +0 -359
- package/core/client/parser/trigger-parser.ts +0 -90
- package/core/client/parser/value-lang.ts +0 -340
- package/core/client/time/instant.ts +0 -2
- package/core/shared/calc/coerce.ts +0 -2
- package/core/shared/calc/errors.ts +0 -40
- package/core/shared/calc/evaluator.ts +0 -113
- package/core/shared/calc/index.ts +0 -11
- package/core/shared/calc/lexer.ts +0 -189
- package/core/shared/calc/parser.ts +0 -193
- package/core/shared/calc/typer.ts +0 -171
- package/core/shared/date-parts.ts +0 -2
- package/core/shared/show-when.ts +0 -77
- package/core/shared/types/step.types.ts +0 -91
- package/core/shared/types/trigger.types.ts +0 -14
- package/dist/types/client/engine/step-runner.d.ts +0 -42
- package/dist/types/client/engine/trigger-engine.d.ts +0 -16
- package/dist/types/client/format/zoned.d.ts +0 -1
- package/dist/types/client/parser/step-parser.d.ts +0 -23
- package/dist/types/client/parser/trigger-parser.d.ts +0 -13
- package/dist/types/client/parser/value-lang.d.ts +0 -63
- package/dist/types/client/time/instant.d.ts +0 -1
- package/dist/types/shared/calc/coerce.d.ts +0 -1
- package/dist/types/shared/calc/errors.d.ts +0 -14
- package/dist/types/shared/calc/evaluator.d.ts +0 -2
- package/dist/types/shared/calc/index.d.ts +0 -6
- package/dist/types/shared/calc/lexer.d.ts +0 -19
- package/dist/types/shared/calc/parser.d.ts +0 -43
- package/dist/types/shared/calc/typer.d.ts +0 -11
- package/dist/types/shared/date-parts.d.ts +0 -1
- package/dist/types/shared/show-when.d.ts +0 -21
- package/dist/types/shared/types/step.types.d.ts +0 -76
- package/dist/types/shared/types/trigger.types.d.ts +0 -12
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// 身份取自 Host 装配的 `identity(request)`(服务端权威判定)。core 不规定身份对象的形状(它是 Host 的),只按下面的约定读:
|
|
8
8
|
// `null / undefined` 或 `signedIn === false` 视为未登录;角色取 `roles`(字符串数组)或 `role`(字符串)。
|
|
9
9
|
// 身份解析失败一律按未登录处理(失败即关闭),不放行也不冒泡。
|
|
10
|
+
// 策略读取失败(装配损坏,或 `defaultAccess` / `pageGrants` 抛错)同样失败即关闭:抛受控 500,不降级为开放。
|
|
10
11
|
//
|
|
11
12
|
// 纪律:错误文案与观测里没有角色值(既不写页面要求的角色,也不写调用者的角色);缓存键与身份无关(KI-23)。
|
|
12
13
|
// 无 DOM。判定函数是纯函数,可单测;`gateAccess` 才碰 Host 装配。
|
|
@@ -124,24 +125,37 @@ export interface SomaAccessPolicy {
|
|
|
124
125
|
|
|
125
126
|
export const SOMA_OPEN_ACCESS_POLICY: SomaAccessPolicy = Object.freeze({ defaultAccess: undefined, grants: [] });
|
|
126
127
|
|
|
127
|
-
|
|
128
|
+
/** 上一次已打印的策略读取失败消息(连续同一条只打一次;内容变化后再打) */
|
|
129
|
+
let lastPolicyFailure: string | null = null;
|
|
128
130
|
|
|
131
|
+
/** 只给测试用 */
|
|
132
|
+
export function resetAccessPolicyReport(): void {
|
|
133
|
+
lastPolicyFailure = null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 读访问策略。失败即关闭:装配加载抛错、或宿主的 `defaultAccess()` / `pageGrants()` 抛错时,
|
|
138
|
+
* 不降级为开放,抛受控内部错误(`CORE-RUNTIME-GRANT-INVALID`,500,不带原始异常文本);原始消息只进服务端日志。
|
|
139
|
+
* 没有 hostDir、无 soma.app.ts、未配 `defaultAccess` 仍是开放策略(正常分支)。
|
|
140
|
+
*/
|
|
129
141
|
export function readAccessPolicy(hostDir: string | undefined): SomaAccessPolicy {
|
|
130
142
|
if (hostDir === undefined) return SOMA_OPEN_ACCESS_POLICY;
|
|
131
143
|
try {
|
|
132
144
|
const assembly = loadHostAssemblySync(hostDir);
|
|
133
145
|
if (assembly === null) return SOMA_OPEN_ACCESS_POLICY;
|
|
134
|
-
|
|
146
|
+
const policy: SomaAccessPolicy = {
|
|
135
147
|
defaultAccess: assembly.defaultAccess?.(),
|
|
136
148
|
grants: assembly.pageGrants?.() ?? [],
|
|
137
149
|
};
|
|
150
|
+
lastPolicyFailure = null; // 成功读取后复位:下次失败(即使文本相同)重新打印
|
|
151
|
+
return policy;
|
|
138
152
|
} catch (error) {
|
|
139
|
-
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
console.error(`[soma-core][引擎]
|
|
153
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
154
|
+
if (message !== lastPolicyFailure) {
|
|
155
|
+
lastPolicyFailure = message;
|
|
156
|
+
console.error(`[soma-core][引擎] 访问策略读取失败,动态页一律拒绝(500):${message}`);
|
|
143
157
|
}
|
|
144
|
-
|
|
158
|
+
throw runtimeError('CORE-RUNTIME-GRANT-INVALID', '读取访问策略', '宿主的访问策略(defaultAccess / pageGrants / 装配)读取失败', '修好 soma.app.ts 后重试;详情见服务端日志');
|
|
145
159
|
}
|
|
146
160
|
}
|
|
147
161
|
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// 本文件管:表与代次、持久绑定的装入与多进程 mtime 对账、到期清扫、写入期冲突判定。
|
|
8
8
|
// 不管:载荷校验与摘要(inject.ts)、取页与编译(resolve.ts)。
|
|
9
9
|
|
|
10
|
-
import
|
|
10
|
+
import { currentHostDir } from '../host/host-dir';
|
|
11
11
|
import {
|
|
12
12
|
addressKindOf,
|
|
13
13
|
addressShapeKey,
|
|
@@ -132,11 +132,8 @@ export function runtimeStore(layout: SomaRuntimeLayout = runtimeLayout()): SomaR
|
|
|
132
132
|
return store;
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
/**
|
|
136
|
-
export
|
|
137
|
-
const configured = process.env.SOMA_HOST_DIR;
|
|
138
|
-
return configured !== undefined && configured.trim() !== '' ? path.resolve(configured) : undefined;
|
|
139
|
-
}
|
|
135
|
+
/** 宿主根单点实现在 host/host-dir(无依赖,避免底层模块反向引入本文件成环) */
|
|
136
|
+
export { currentHostDir };
|
|
140
137
|
|
|
141
138
|
/** 外壳路由 = 宿主壳经 `registerShellRoutes` 登记的 ∪ 宿主装配声明的 `reservedRoutes`(后者只读已装配完的,不触发加载) */
|
|
142
139
|
export function currentReservedRoutes(): readonly string[] {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// · HTML 实例树(串或节点树);core DSL 文档(文本或已解析对象);locale;
|
|
6
6
|
// · 不认任何方言键:未知键、方言段一律 `CORE-REG-INPUT-INVALID` / `CORE-INJECT-NONSTANDARD-KEY` 拒收。
|
|
7
7
|
//
|
|
8
|
-
// 一个 URL 只挂一个来源类型,注册必须带来源类型 `owner`(`native` / `soma-ui` / `soma-
|
|
8
|
+
// 一个 URL 只挂一个来源类型,注册必须带来源类型 `owner`(`native` / `soma-ui` / `soma-kit`)。注册表只判断不同类型之间的冲突:
|
|
9
9
|
// · 同类型再注册 = 页面变了,直接覆盖,不报错,旧页面的编译缓存随之作废(方言「要不要重新注册」就是同类型再注册一次);
|
|
10
10
|
// · 不同类型 = 两个不同的东西抢同一个 URL:不覆盖,保留先挂的,注册日志记一条 error,不抛;
|
|
11
11
|
// · 撤下只能同类型;不同类型撤下被忽略并记日志;撤下后 URL 空出,其他类型才能挂上来。
|
|
@@ -17,8 +17,12 @@
|
|
|
17
17
|
import yaml from 'js-yaml';
|
|
18
18
|
import { looksLikeBlockDeclaration } from '../../shared/declaration-keys';
|
|
19
19
|
import { parseAddress } from '../../shared/runtime-address';
|
|
20
|
+
import { SOMA_PIPELINE_LIMITS } from '../../shared/registry-generated/limits';
|
|
21
|
+
import { FORBIDDEN_TAG_SET, scanDeclarationSafety } from '../registry/validation/declaration-safety-scan';
|
|
22
|
+
import { normalizeDeclarationAssets } from '../registry/standard-product';
|
|
23
|
+
import { hasBlockedScheme } from '../../client/security/url-guard';
|
|
20
24
|
import { allEntries, currentHostDir, findEntry } from './bindings';
|
|
21
|
-
import { runtimeError } from './errors';
|
|
25
|
+
import { runtimeError, type SomaRuntimeBatchFailure } from './errors';
|
|
22
26
|
import { inject, injectMany, unbind, validateAddress, type SomaInjectManyOutcome } from './inject';
|
|
23
27
|
import { resolveSomaPageSource } from '../host/server-page-assets';
|
|
24
28
|
import { getSomaAssetSources } from '../host/source-packages';
|
|
@@ -71,7 +75,7 @@ export interface SomaPageInput {
|
|
|
71
75
|
export type SomaCompiledPage = SomaPageInput;
|
|
72
76
|
|
|
73
77
|
export type SomaPageRegisterOptions = Omit<SomaInjectOptions, 'expectGeneration' | 'fixed' | 'version' | 'owner'> & {
|
|
74
|
-
/** 来源类型(必填):`soma-ui` / `soma-
|
|
78
|
+
/** 来源类型(必填):`soma-ui` / `soma-kit`(原生页由 core 初始化扫描登记,来源类型 `native`) */
|
|
75
79
|
readonly owner: string;
|
|
76
80
|
/** 覆盖产物自带的 version */
|
|
77
81
|
readonly version?: string;
|
|
@@ -100,22 +104,42 @@ function escapeAttr(text: string): string {
|
|
|
100
104
|
return escapeText(text).replace(/"/g, '"');
|
|
101
105
|
}
|
|
102
106
|
|
|
107
|
+
/** 是不是调用栈溢出(V8:`Maximum call stack size exceeded`);其他 RangeError(如字符串长度超限)不算 */
|
|
108
|
+
function isStackOverflow(error: RangeError): boolean {
|
|
109
|
+
return /Maximum call stack/i.test(error.message);
|
|
110
|
+
}
|
|
111
|
+
|
|
103
112
|
/** 实例树 → HTML 串。标签 / 属性名按文法校验,拒收 script 标签与 on* 事件属性(事件写在 core DSL 里) */
|
|
104
113
|
export function renderHtmlTree(node: SomaHtmlChild, url = '<tree>'): string {
|
|
114
|
+
try {
|
|
115
|
+
return renderHtmlNode(node, url);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
// 深嵌套 htmlTree 会撑爆调用栈:转成受控的输入非法错误(不靠数字限值)
|
|
118
|
+
if (error instanceof RangeError) {
|
|
119
|
+
return rejectInput(url, isStackOverflow(error) ? 'htmlTree 嵌套过深(超出调用栈)' : `htmlTree 输入非法:${error.message}`);
|
|
120
|
+
}
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function renderHtmlNode(node: SomaHtmlChild, url: string): string {
|
|
105
126
|
if (typeof node === 'string') return escapeText(node);
|
|
106
127
|
if (node === null || typeof node !== 'object' || typeof node.tag !== 'string' || !TAG.test(node.tag)) {
|
|
107
128
|
return rejectInput(url, `htmlTree 节点必须是 { tag, attrs?, children? }(tag 文法 ${String(TAG)})`);
|
|
108
129
|
}
|
|
109
130
|
const tag = node.tag.toLowerCase();
|
|
110
|
-
if (tag
|
|
131
|
+
if (FORBIDDEN_TAG_SET.has(tag)) return rejectInput(url, `htmlTree 不允许 ${tag} 标签`);
|
|
111
132
|
let attrs = '';
|
|
112
133
|
for (const [name, value] of Object.entries(node.attrs ?? {})) {
|
|
113
134
|
if (value === undefined || value === null || value === false) continue;
|
|
114
135
|
if (!ATTR.test(name) || /^on/i.test(name)) return rejectInput(url, `htmlTree 属性名 ${JSON.stringify(name)} 不合法(事件写在 core DSL 里,不写 on* 属性)`);
|
|
115
|
-
|
|
136
|
+
if (/^srcdoc$/i.test(name)) return rejectInput(url, 'htmlTree 属性名 "srcdoc" 不合法(不允许内联完整文档)');
|
|
137
|
+
const strVal = String(value);
|
|
138
|
+
if (hasBlockedScheme(name, strVal)) return rejectInput(url, `htmlTree 属性 ${name} 包含危险伪协议`);
|
|
139
|
+
attrs += value === true ? ` ${name}` : ` ${name}="${escapeAttr(strVal)}"`;
|
|
116
140
|
}
|
|
117
141
|
if (VOID_TAGS.has(tag)) return `<${tag}${attrs}>`;
|
|
118
|
-
const inner = (node.children ?? []).map((child) =>
|
|
142
|
+
const inner = (node.children ?? []).map((child) => renderHtmlNode(child, url)).join('');
|
|
119
143
|
return `<${tag}${attrs}>${inner}</${tag}>`;
|
|
120
144
|
}
|
|
121
145
|
|
|
@@ -125,7 +149,13 @@ export type SomaDslKind = 'ui' | 'event' | 'style' | 'locale';
|
|
|
125
149
|
|
|
126
150
|
export type SomaDslParseResult =
|
|
127
151
|
| { readonly ok: true; readonly value: unknown }
|
|
128
|
-
| {
|
|
152
|
+
| {
|
|
153
|
+
readonly ok: false;
|
|
154
|
+
readonly diagnostics: readonly { readonly message: string; readonly line?: number; readonly column?: number }[];
|
|
155
|
+
/** YAML 语法错误的位置(1 起始);仅底层异常带 mark 时才有 */
|
|
156
|
+
readonly line?: number;
|
|
157
|
+
readonly column?: number;
|
|
158
|
+
};
|
|
129
159
|
|
|
130
160
|
/**
|
|
131
161
|
* 运行期把 core DSL 文本解析成对象(无 fs、无端口、无副作用):四件套都是平铺 YAML,按 YAML 读。
|
|
@@ -141,7 +171,15 @@ export function parseSomaDsl(text: string, kind: SomaDslKind = 'ui'): SomaDslPar
|
|
|
141
171
|
const value = yaml.load(text);
|
|
142
172
|
return { ok: true, value };
|
|
143
173
|
} catch (error) {
|
|
144
|
-
|
|
174
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
175
|
+
const mark = (error as { name?: unknown; mark?: { line?: unknown; column?: unknown } } | null)?.mark;
|
|
176
|
+
if (error instanceof Error && error.name === 'YAMLException' && mark && typeof mark.line === 'number' && typeof mark.column === 'number') {
|
|
177
|
+
// js-yaml 的 mark 是 0 起始,对外统一 1 起始
|
|
178
|
+
const line = mark.line + 1;
|
|
179
|
+
const column = mark.column + 1;
|
|
180
|
+
return { ok: false, diagnostics: [{ message, line, column }], line, column };
|
|
181
|
+
}
|
|
182
|
+
return { ok: false, diagnostics: [{ message }] };
|
|
145
183
|
}
|
|
146
184
|
}
|
|
147
185
|
|
|
@@ -161,32 +199,110 @@ function dslText(url: string, doc: SomaDslDoc, kind: SomaDslKind): string {
|
|
|
161
199
|
|
|
162
200
|
/** 标准格式输入 → core 内部的 `SomaStandardPage`(校验闭集键、二选一、文档形状) */
|
|
163
201
|
export function normalizeSomaPageInput(url: string, input: SomaPageInput): SomaStandardPage {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
202
|
+
try {
|
|
203
|
+
if (input === null || typeof input !== 'object' || Array.isArray(input)) return rejectInput(url, '载荷不是对象');
|
|
204
|
+
for (const key of Object.keys(input)) {
|
|
205
|
+
if (!INPUT_KEYS.has(key)) return rejectInput(url, `不支持键 ${JSON.stringify(key)}(标准格式只有 ${[...INPUT_KEYS].join(' / ')})`);
|
|
206
|
+
}
|
|
207
|
+
if (input.format !== undefined && input.format !== 1) return rejectInput(url, `format 只能是 1,收到 ${JSON.stringify(input.format)}`);
|
|
208
|
+
if ((input.html === undefined) === (input.htmlTree === undefined)) return rejectInput(url, 'html 与 htmlTree 必须二选一');
|
|
209
|
+
if (input.html !== undefined && typeof input.html !== 'string') return rejectInput(url, 'html 必须是字符串');
|
|
210
|
+
const { uiYaml: ui, eventYaml: event, styleYaml: style, locale } = input;
|
|
211
|
+
if (ui === undefined) return rejectInput(url, '缺 uiYaml 文档');
|
|
212
|
+
if (input.version !== undefined && (typeof input.version !== 'string' || input.version === '' || input.version.length > 128)) {
|
|
213
|
+
return rejectInput(url, 'version 必须是 1..128 位字符串');
|
|
214
|
+
}
|
|
215
|
+
const html = input.html ?? renderHtmlTree(input.htmlTree as SomaHtmlNode, url);
|
|
216
|
+
const uiYamlText = dslText(url, ui, 'ui');
|
|
217
|
+
const eventYamlText = event !== undefined ? dslText(url, event, 'event') : undefined;
|
|
218
|
+
const styleYamlText = style !== undefined ? dslText(url, style, 'style') : undefined;
|
|
219
|
+
const localeText = locale !== undefined ? dslText(url, locale, 'locale') : undefined;
|
|
220
|
+
// includes:有值(非 undefined / null)必须是普通对象且每个值是字符串,先于计量与扫描
|
|
221
|
+
if (input.includes !== undefined && input.includes !== null) {
|
|
222
|
+
const includes: unknown = input.includes;
|
|
223
|
+
const proto = typeof includes === 'object' ? Object.getPrototypeOf(includes) : undefined;
|
|
224
|
+
if (typeof includes !== 'object' || Array.isArray(includes) || (proto !== Object.prototype && proto !== null)) {
|
|
225
|
+
return rejectInput(url, 'includes 必须是 { 名称: HTML 片段字符串 } 的普通对象');
|
|
226
|
+
}
|
|
227
|
+
for (const [name, fragment] of Object.entries(includes as Record<string, unknown>)) {
|
|
228
|
+
if (typeof fragment !== 'string') return rejectInput(url, `includes[${JSON.stringify(name)}] 必须是字符串`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// 单页注入载荷总字节上限(真源 limits.json#inject.pageMaxBytes,暂定值):先量后扫,超限不进安全扫描
|
|
232
|
+
const maxBytes = SOMA_PIPELINE_LIMITS.inject.pageMaxBytes;
|
|
233
|
+
let payloadBytes = 0;
|
|
234
|
+
for (const text of [html, uiYamlText, eventYamlText, styleYamlText, localeText]) {
|
|
235
|
+
if (text !== undefined) payloadBytes += Buffer.byteLength(text, 'utf8');
|
|
236
|
+
}
|
|
237
|
+
if (input.includes !== undefined && input.includes !== null && typeof input.includes === 'object') {
|
|
238
|
+
for (const fragment of Object.values(input.includes)) {
|
|
239
|
+
if (typeof fragment === 'string') payloadBytes += Buffer.byteLength(fragment, 'utf8');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (Array.isArray(input.styles)) {
|
|
243
|
+
for (const item of input.styles as readonly unknown[]) {
|
|
244
|
+
const css = (item as { css?: unknown } | null)?.css;
|
|
245
|
+
if (typeof css === 'string') payloadBytes += Buffer.byteLength(css, 'utf8');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (payloadBytes > maxBytes) return rejectInput(url, `注入载荷 ${payloadBytes} 字节超过单页上限 ${maxBytes} 字节`);
|
|
249
|
+
const safetyIssues = scanDeclarationSafety({
|
|
250
|
+
html,
|
|
251
|
+
uiYaml: uiYamlText,
|
|
252
|
+
eventYaml: eventYamlText,
|
|
253
|
+
styleYaml: styleYamlText,
|
|
254
|
+
});
|
|
255
|
+
// includes 的每个值会被客户端 innerHTML:与 html 同一把尺子
|
|
256
|
+
if (input.includes !== undefined && input.includes !== null && typeof input.includes === 'object') {
|
|
257
|
+
for (const [name, fragment] of Object.entries(input.includes)) {
|
|
258
|
+
if (typeof fragment !== 'string') return rejectInput(url, `includes[${JSON.stringify(name)}] 必须是字符串`);
|
|
259
|
+
for (const issue of scanDeclarationSafety({ html: fragment })) safetyIssues.push(`includes[${JSON.stringify(name)}] ${issue}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// styles[].css 与 Registry 路径同一资产安全判据(normalizeDeclarationAssets 内的 ASSET_UNSAFE)
|
|
263
|
+
if (input.styles !== undefined) {
|
|
264
|
+
try {
|
|
265
|
+
normalizeDeclarationAssets({ styles: input.styles }, maxBytes);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
safetyIssues.push(error instanceof Error ? error.message : String(error));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (safetyIssues.length > 0) {
|
|
271
|
+
return rejectInput(url, `声明安全扫描未通过:${safetyIssues.join(';')}`);
|
|
272
|
+
}
|
|
273
|
+
// 入表前深拷贝:含函数 / symbol 的对象会让 structuredClone 抛 DataCloneError,转成点名字段的输入非法
|
|
274
|
+
const cloneField = <T>(field: string, value: T): T => {
|
|
275
|
+
try {
|
|
276
|
+
return structuredClone(value);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (error instanceof RangeError && isStackOverflow(error)) {
|
|
279
|
+
return rejectInput(url, `${field} 嵌套过深(超出调用栈)`);
|
|
280
|
+
}
|
|
281
|
+
return rejectInput(url, `${field} 含不可序列化的值(函数 / symbol 等):${error instanceof Error ? error.message : String(error)}`);
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
const page: SomaStandardPage = {
|
|
285
|
+
format: 1,
|
|
286
|
+
html,
|
|
287
|
+
uiYaml: uiYamlText,
|
|
288
|
+
...(eventYamlText !== undefined ? { eventYaml: eventYamlText } : {}),
|
|
289
|
+
...(styleYamlText !== undefined ? { styleYaml: styleYamlText } : {}),
|
|
290
|
+
...(localeText !== undefined ? { locale: localeText } : {}),
|
|
291
|
+
// 入表前深拷贝:不与调用方对象共享引用(只拷贝,不冻结)
|
|
292
|
+
...(input.styles !== undefined ? { styles: cloneField('styles', input.styles) } : {}),
|
|
293
|
+
...(input.includes !== undefined ? { includes: cloneField('includes', input.includes) } : {}),
|
|
294
|
+
...(input.colorScheme !== undefined ? { colorScheme: input.colorScheme } : {}),
|
|
295
|
+
...(input.titleKey !== undefined ? { titleKey: input.titleKey } : {}),
|
|
296
|
+
...(input.envelope !== undefined ? { envelope: cloneField('envelope', input.envelope) } : {}),
|
|
297
|
+
};
|
|
298
|
+
return page;
|
|
299
|
+
} catch (error) {
|
|
300
|
+
// 深嵌套对象(envelope / uiYaml 对象 / styles 等)同样会让递归栈溢出:转成受控错误
|
|
301
|
+
if (error instanceof RangeError) {
|
|
302
|
+
return rejectInput(url, isStackOverflow(error) ? '载荷嵌套过深(超出调用栈)' : `载荷输入非法:${error.message}`);
|
|
303
|
+
}
|
|
304
|
+
throw error;
|
|
175
305
|
}
|
|
176
|
-
const html = input.html ?? renderHtmlTree(input.htmlTree as SomaHtmlNode, url);
|
|
177
|
-
return {
|
|
178
|
-
format: 1,
|
|
179
|
-
html,
|
|
180
|
-
uiYaml: dslText(url, ui, 'ui'),
|
|
181
|
-
...(event !== undefined ? { eventYaml: dslText(url, event, 'event') } : {}),
|
|
182
|
-
...(style !== undefined ? { styleYaml: dslText(url, style, 'style') } : {}),
|
|
183
|
-
...(locale !== undefined ? { locale: dslText(url, locale, 'locale') } : {}),
|
|
184
|
-
...(input.styles !== undefined ? { styles: input.styles } : {}),
|
|
185
|
-
...(input.includes !== undefined ? { includes: input.includes } : {}),
|
|
186
|
-
...(input.colorScheme !== undefined ? { colorScheme: input.colorScheme } : {}),
|
|
187
|
-
...(input.titleKey !== undefined ? { titleKey: input.titleKey } : {}),
|
|
188
|
-
...(input.envelope !== undefined ? { envelope: input.envelope } : {}),
|
|
189
|
-
};
|
|
190
306
|
}
|
|
191
307
|
|
|
192
308
|
/** 原生页的来源类型 */
|
|
@@ -226,7 +342,7 @@ function currentOwner(url: string): { readonly owner: string; readonly binding:
|
|
|
226
342
|
function ownerOf(url: string, options: SomaPageRegisterOptions | undefined): string {
|
|
227
343
|
const owner = options?.owner;
|
|
228
344
|
if (typeof owner !== 'string' || owner === '') {
|
|
229
|
-
throw runtimeError('CORE-REG-INPUT-INVALID', `注册 ${url}`, '缺来源标识 owner', '注册必须带 owner(来源类型):soma-ui / soma-
|
|
345
|
+
throw runtimeError('CORE-REG-INPUT-INVALID', `注册 ${url}`, '缺来源标识 owner', '注册必须带 owner(来源类型):soma-ui / soma-kit');
|
|
230
346
|
}
|
|
231
347
|
return owner;
|
|
232
348
|
}
|
|
@@ -279,20 +395,36 @@ export function registerSomaPages(entries: readonly SomaPageBatchEntry[], option
|
|
|
279
395
|
const outcomes: SomaPageBatchOutcome[] = [];
|
|
280
396
|
const todo: { readonly url: string; readonly entry: SomaPageBatchEntry; readonly page: SomaStandardPage }[] = [];
|
|
281
397
|
const batchOwner = new Map<string, string>();
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
398
|
+
const atomic = options.atomic !== false;
|
|
399
|
+
const failures: SomaRuntimeBatchFailure[] = [];
|
|
400
|
+
entries.forEach((entry, index) => {
|
|
401
|
+
const rawUrl = typeof entry?.url === 'string' ? entry.url : String(entry?.url);
|
|
402
|
+
try {
|
|
403
|
+
const url = normalizedUrl(entry.url);
|
|
404
|
+
const owner = ownerOf(url, entry.options);
|
|
405
|
+
const page = normalizeSomaPageInput(url, entry.page);
|
|
406
|
+
const current = currentOwner(url);
|
|
407
|
+
const earlier = batchOwner.get(url);
|
|
408
|
+
const taken = earlier !== undefined ? earlier : current?.owner;
|
|
409
|
+
if (taken !== undefined && taken !== owner) {
|
|
410
|
+
console.error(`[soma-core][注册中心] URL ${url} 已由来源 ${taken === '' ? '(未标注)' : taken} 挂上,来源 ${owner} 的注册不生效(异源不覆盖)`);
|
|
411
|
+
outcomes.push({ url, registered: false, ...(current?.binding != null ? { binding: current.binding } : {}) });
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
batchOwner.set(url, owner);
|
|
415
|
+
todo.push({ url, entry, page });
|
|
416
|
+
} catch (error) {
|
|
417
|
+
// 逐条规范化的异常收进该条结果;原子批整体以受控的批失败抛出(不是无 code 的裸异常)
|
|
418
|
+
const code = typeof (error as { code?: unknown })?.code === 'string' ? (error as { code: string }).code : 'CORE-REG-INPUT-INVALID';
|
|
419
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
420
|
+
failures.push({ index, address: rawUrl, code, message });
|
|
421
|
+
outcomes.push({ url: rawUrl, registered: false, code, message });
|
|
293
422
|
}
|
|
294
|
-
|
|
295
|
-
|
|
423
|
+
});
|
|
424
|
+
if (failures.length > 0 && atomic) {
|
|
425
|
+
throw runtimeError('CORE-RUNTIME-BATCH', `批量注册 ${entries.length} 页`,
|
|
426
|
+
`${failures.length} 条失败,整批未生效:${failures.map((item) => `#${item.index} ${item.address} ${item.code}`).join(';')}`,
|
|
427
|
+
'逐条修复后整批重试', { failures });
|
|
296
428
|
}
|
|
297
429
|
const results: readonly SomaInjectManyOutcome[] = injectMany(
|
|
298
430
|
todo.map(({ url, entry, page }) => ({ address: url, page, options: optionsOf(entry.page, entry.options) })),
|
|
@@ -315,8 +447,8 @@ export function registerSomaPages(entries: readonly SomaPageBatchEntry[], option
|
|
|
315
447
|
* 异源撤下被忽略并记日志,返回 false;URL 没有表内登记返回 false,不抛。
|
|
316
448
|
*/
|
|
317
449
|
export function withdrawPage(url: string, owner: string): boolean {
|
|
318
|
-
const check =
|
|
319
|
-
if (!check.ok
|
|
450
|
+
const check = parseAddress(url);
|
|
451
|
+
if (!check.ok) return false;
|
|
320
452
|
const entry = findEntry(check.normalized);
|
|
321
453
|
if (entry === undefined || entry.binding.address !== check.normalized) return false;
|
|
322
454
|
const held = entry.binding.owner ?? '';
|
|
@@ -13,6 +13,7 @@ export type SomaRuntimeErrorCode = Extract<
|
|
|
13
13
|
| 'CORE-RUNTIME-ADDRESS-MISSING' | 'CORE-RUNTIME-ADDRESS-EXPIRED' | 'CORE-RUNTIME-GENERATION'
|
|
14
14
|
| 'CORE-RUNTIME-BATCH' | 'CORE-RUNTIME-GRANT' | 'CORE-RUNTIME-GRANT-INVALID' | 'CORE-RUNTIME-PARAM-INVALID' | 'CORE-RUNTIME-ACCESS-DENIED' | 'CORE-NAV-OVERFLOW'
|
|
15
15
|
| 'CORE-FORMAT-VERSION-UNSUPPORTED' | 'CORE-FORMAT-VERSION-MISMATCH' | 'CORE-INJECT-NONSTANDARD-KEY'
|
|
16
|
+
| 'CORE-INJECT-PAGE-LOGIC' | 'CORE-INJECT-PAGE-CLOSURE'
|
|
16
17
|
| 'CORE-REG-INPUT-INVALID' | 'CORE-REG-NO-COMPILED-ARTIFACT'
|
|
17
18
|
>;
|
|
18
19
|
|
|
@@ -37,6 +38,8 @@ const STATUS: Readonly<Record<SomaRuntimeErrorCode, number>> = {
|
|
|
37
38
|
'CORE-FORMAT-VERSION-UNSUPPORTED': 400,
|
|
38
39
|
'CORE-FORMAT-VERSION-MISMATCH': 400,
|
|
39
40
|
'CORE-INJECT-NONSTANDARD-KEY': 400,
|
|
41
|
+
'CORE-INJECT-PAGE-LOGIC': 400,
|
|
42
|
+
'CORE-INJECT-PAGE-CLOSURE': 400,
|
|
40
43
|
'CORE-REG-INPUT-INVALID': 400,
|
|
41
44
|
'CORE-REG-NO-COMPILED-ARTIFACT': 503,
|
|
42
45
|
};
|
|
@@ -9,12 +9,13 @@
|
|
|
9
9
|
// 注入时不编译:校验 → 摘要 → 冲突判定 → 入表(L1 常驻;`persist` 时再写 L2)。编译在取页时做(resolve.ts)。
|
|
10
10
|
|
|
11
11
|
import { createHash } from 'node:crypto';
|
|
12
|
-
import { flatUiNonStandardKeys, readFlatEnvelope, readFlatProbe } from '../../build/compiler/flat-page';
|
|
12
|
+
import { flatUiNonStandardKeys, readFlatDoc, readFlatEnvelope, readFlatProbe } from '../../build/compiler/flat-page';
|
|
13
|
+
import { formatPageClosureIssues, inspectPageClosure, primaryPageClosureCode } from '../../build/compiler/page-closure';
|
|
13
14
|
import { looksLikeBlockDeclaration } from '../../shared/declaration-keys';
|
|
14
15
|
import { domainStaticOnlyHint } from '../../client/mount/domain-rule';
|
|
15
16
|
import { parseAddress, validateAddressText } from '../../shared/runtime-address';
|
|
16
17
|
import { SOMA_PAGE_FORMAT_VERSION } from '../../shared/page-envelope';
|
|
17
|
-
import { computeStandardPageDigest } from './digest';
|
|
18
|
+
import { bindingStorageKey, computeStandardPageDigest } from './digest';
|
|
18
19
|
import { evictArtifactsOfUrl } from './artifact-evict';
|
|
19
20
|
import { assertNavCapacity } from './nav-catalog';
|
|
20
21
|
import { runtimeError, SomaRuntimeError, type SomaRuntimeBatchFailure } from './errors';
|
|
@@ -51,7 +52,7 @@ import type {
|
|
|
51
52
|
} from './types';
|
|
52
53
|
|
|
53
54
|
const LABEL = /^[a-z][a-z0-9-]{0,31}$/;
|
|
54
|
-
/** 来源类型:小写标签(native / soma-ui / soma-
|
|
55
|
+
/** 来源类型:小写标签(native / soma-ui / soma-kit …),不带子标识——同类型内部的事归来源自己管 */
|
|
55
56
|
const OWNER = LABEL;
|
|
56
57
|
|
|
57
58
|
// ── 地址 ─────────────────────────────────────────────────────────────────────
|
|
@@ -125,7 +126,7 @@ function checkOptions(address: string, options: SomaInjectOptions, now: number):
|
|
|
125
126
|
throw invalid(`注入 ${address}`, 'version 必须是 1..128 位字符串', 'version 是方言随产物给出的不透明版本号');
|
|
126
127
|
}
|
|
127
128
|
if (options.owner !== undefined && (typeof options.owner !== 'string' || !OWNER.test(options.owner))) {
|
|
128
|
-
throw invalid(`注入 ${address}`, `owner ${JSON.stringify(options.owner)} 不合(来源类型,形如 soma-ui / soma-
|
|
129
|
+
throw invalid(`注入 ${address}`, `owner ${JSON.stringify(options.owner)} 不合(来源类型,形如 soma-ui / soma-kit / native)`, 'owner 是来源类型:原生页 native,方言写方言名');
|
|
129
130
|
}
|
|
130
131
|
const source = options.source;
|
|
131
132
|
if (source !== undefined && (!isRecord(source) || typeof source.format !== 'string' || !LABEL.test(source.format)
|
|
@@ -182,6 +183,25 @@ interface Prepared {
|
|
|
182
183
|
readonly options: SomaInjectOptions;
|
|
183
184
|
}
|
|
184
185
|
|
|
186
|
+
/** 注入口闭集守门:四件套里出现逻辑写法(CORE-INJECT-PAGE-LOGIC)或越出闭集(CORE-INJECT-PAGE-CLOSURE)即拒收 */
|
|
187
|
+
function assertPageClosure(address: string, page: SomaStandardPage): void {
|
|
188
|
+
let ui: Record<string, unknown>;
|
|
189
|
+
let event: Record<string, unknown> | undefined;
|
|
190
|
+
let style: Record<string, unknown> | undefined;
|
|
191
|
+
try {
|
|
192
|
+
ui = readFlatDoc(page.uiYaml, 'ui');
|
|
193
|
+
event = page.eventYaml === undefined ? undefined : readFlatDoc(page.eventYaml, 'event');
|
|
194
|
+
style = page.styleYaml === undefined ? undefined : readFlatDoc(page.styleYaml, 'style');
|
|
195
|
+
} catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const issues = inspectPageClosure({ ui, ...(event !== undefined ? { event } : {}), ...(style !== undefined ? { style } : {}) });
|
|
199
|
+
if (issues.length === 0) return;
|
|
200
|
+
throw runtimeError(primaryPageClosureCode(issues), `注入 ${address}`,
|
|
201
|
+
formatPageClosureIssues(issues),
|
|
202
|
+
'页面只保留视觉与纯绑定;逻辑归展示对象(Domain 页面类的属性 / 方法)与业务对象(实体 / 接口),页面只绑定它们给出的值');
|
|
203
|
+
}
|
|
204
|
+
|
|
185
205
|
function prepare(address: string, page: SomaStandardPage, options: SomaInjectOptions): Prepared {
|
|
186
206
|
ensureLoaded();
|
|
187
207
|
checkOptions(address, options, Date.now());
|
|
@@ -207,8 +227,11 @@ function prepare(address: string, page: SomaStandardPage, options: SomaInjectOpt
|
|
|
207
227
|
`${key} 是块声明容器({{ }}),不是平铺 YAML`, '侧车必须是平铺 YAML 文本或已解析对象');
|
|
208
228
|
}
|
|
209
229
|
}
|
|
230
|
+
// 逻辑写法与闭集外写法一律拒收(所有来源同一道:native / soma-ui / soma-kit / ai 都走本函数);
|
|
231
|
+
// 页面里不留逻辑,不降级放行。文档读不开(坏 YAML)交取页时的编译去报。
|
|
232
|
+
assertPageClosure(normalized, page);
|
|
210
233
|
// 动态页写了非缺省 domain:提示一次(判据与挂载期同一份 domainStaticOnlyHint),页面照常注入,运行期退成缺省根
|
|
211
|
-
const hint = domainStaticOnlyHint({ origin: 'inject', domain: probe.domain });
|
|
234
|
+
const hint = domainStaticOnlyHint({ origin: 'inject', domain: probe.domain, provided: options.domainProvided === true });
|
|
212
235
|
if (hint !== null) announceOnce(`domain:${normalized}:${digest}`, `${hint.code} ${normalized}:${hint.message}`);
|
|
213
236
|
const envelope = page.envelope ?? probe.envelope;
|
|
214
237
|
const declared = [page.envelope?.url, probe.envelope?.url].filter((item): item is string => typeof item === 'string');
|
|
@@ -242,8 +265,8 @@ function persistedMtime(address: string): number | null {
|
|
|
242
265
|
}
|
|
243
266
|
|
|
244
267
|
interface ApplyContext {
|
|
245
|
-
/**
|
|
246
|
-
readonly deferred: (
|
|
268
|
+
/** 批次内:写盘动作先攒着,整批通过再执行(带地址,写盘失败回滚时据此还原 / 清理文件) */
|
|
269
|
+
readonly deferred: { readonly address: string; readonly run: () => void }[] | null;
|
|
247
270
|
readonly batch: ReadonlySet<string>;
|
|
248
271
|
}
|
|
249
272
|
|
|
@@ -279,6 +302,9 @@ function applyInject(prepared: Prepared, context: ApplyContext): SomaRuntimeBind
|
|
|
279
302
|
if (existing !== undefined && sameBinding(existing.binding, draft)) {
|
|
280
303
|
return existing.binding;
|
|
281
304
|
}
|
|
305
|
+
// 单条注入且要写盘:先留内存快照,写盘失败时整体回滚(批内由 injectMany 持有整批快照;非持久路径不取快照,零额外开销)
|
|
306
|
+
const needsWrite = persist || existing?.binding.persisted === true;
|
|
307
|
+
const rollback = needsWrite && context.deferred === null ? snapshotSpace() : null;
|
|
282
308
|
const binding: SomaRuntimeBinding = { ...draft, generation: nextGeneration(address) };
|
|
283
309
|
const entry: SpaceEntry = {
|
|
284
310
|
binding, page, envelope, prefix: null, registryName: null, mtimeMs: null,
|
|
@@ -299,12 +325,20 @@ function applyInject(prepared: Prepared, context: ApplyContext): SomaRuntimeBind
|
|
|
299
325
|
store.writeBinding(binding);
|
|
300
326
|
entry.mtimeMs = persistedMtime(address);
|
|
301
327
|
} else if (existing?.binding.persisted === true) {
|
|
302
|
-
store.
|
|
328
|
+
store.removeBindingStrict(address); // 删不掉要报错回滚,不能留着文件让重启后页面复活
|
|
303
329
|
}
|
|
304
330
|
};
|
|
305
|
-
if (
|
|
306
|
-
if (context.deferred !== null) context.deferred.push(write);
|
|
307
|
-
else
|
|
331
|
+
if (needsWrite) {
|
|
332
|
+
if (context.deferred !== null) context.deferred.push({ address, run: write });
|
|
333
|
+
else {
|
|
334
|
+
try {
|
|
335
|
+
write();
|
|
336
|
+
} catch (error) {
|
|
337
|
+
// 磁盘满 / 目录不可写:内存退回注入前(表项、代次、导航代次),错误沿原路径抛出
|
|
338
|
+
if (rollback !== null) restoreSpace(rollback);
|
|
339
|
+
throw error;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
308
342
|
}
|
|
309
343
|
return binding;
|
|
310
344
|
}
|
|
@@ -333,7 +367,7 @@ export interface SomaInjectManyOutcome {
|
|
|
333
367
|
export function injectMany(entries: readonly SomaInjectManyEntry[], options: SomaInjectManyOptions = {}): readonly SomaInjectManyOutcome[] {
|
|
334
368
|
const atomic = options.atomic !== false;
|
|
335
369
|
const snapshot = snapshotSpace();
|
|
336
|
-
const deferred: (
|
|
370
|
+
const deferred: { readonly address: string; readonly run: () => void }[] = [];
|
|
337
371
|
const batch = new Set<string>(entries.flatMap((entry) => (typeof entry.address === 'string' ? [entry.address] : [])));
|
|
338
372
|
const outcomes: SomaInjectManyOutcome[] = [];
|
|
339
373
|
const failures: SomaRuntimeBatchFailure[] = [];
|
|
@@ -343,7 +377,11 @@ export function injectMany(entries: readonly SomaInjectManyEntry[], options: Som
|
|
|
343
377
|
const binding = applyInject(prepare(entry.address, entry.page, entry.options ?? {}), { deferred, batch });
|
|
344
378
|
outcomes.push({ address: binding.address, ok: true, binding });
|
|
345
379
|
} catch (error) {
|
|
346
|
-
if (!(error instanceof SomaRuntimeError) && (error as { code?: unknown })?.code === undefined)
|
|
380
|
+
if (!(error instanceof SomaRuntimeError) && (error as { code?: unknown })?.code === undefined) {
|
|
381
|
+
// 意外异常:与批失败同样先回滚(deferred 写盘尚未执行,直接丢弃),再原样抛出
|
|
382
|
+
restoreSpace(snapshot);
|
|
383
|
+
throw error;
|
|
384
|
+
}
|
|
347
385
|
const code = (error as { code: string }).code;
|
|
348
386
|
const message = (error as Error).message;
|
|
349
387
|
outcomes.push({ address: entry.address, ok: false, code, message });
|
|
@@ -356,7 +394,27 @@ export function injectMany(entries: readonly SomaInjectManyEntry[], options: Som
|
|
|
356
394
|
`${failures.length} 条失败,整批未生效:${failures.map((item) => `#${item.index} ${item.address} ${item.code}`).join(';')}`,
|
|
357
395
|
'逐条修复后整批重试', { failures });
|
|
358
396
|
}
|
|
359
|
-
|
|
397
|
+
const attempted: string[] = [];
|
|
398
|
+
try {
|
|
399
|
+
for (const write of deferred) {
|
|
400
|
+
attempted.push(write.address);
|
|
401
|
+
write.run();
|
|
402
|
+
}
|
|
403
|
+
} catch (error) {
|
|
404
|
+
// 某条写盘失败:整批内存回滚,并尽力把本批动过的绑定文件还原(原本持久的写回旧绑定,原本没有的删掉);清理失败只记日志
|
|
405
|
+
restoreSpace(snapshot);
|
|
406
|
+
const store = runtimeStore();
|
|
407
|
+
for (const address of attempted) {
|
|
408
|
+
try {
|
|
409
|
+
const old = snapshot.entries.get(bindingStorageKey(address))?.binding;
|
|
410
|
+
if (old !== undefined && old.persisted) store.writeBinding(old);
|
|
411
|
+
else store.removeBindingStrict(address);
|
|
412
|
+
} catch (cleanup) {
|
|
413
|
+
console.error(`[soma-core][引擎] 批量写盘回滚时清理 ${address} 的绑定文件失败:${cleanup instanceof Error ? cleanup.message : String(cleanup)}`);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
throw error;
|
|
417
|
+
}
|
|
360
418
|
// 批内每条都加过一次导航代次:整批只算一次
|
|
361
419
|
setNavGeneration(outcomes.some((item) => item.ok) ? navBefore + 1 : navBefore);
|
|
362
420
|
return outcomes;
|
|
@@ -429,8 +487,9 @@ export function unbind(address: string, options: { readonly expectGeneration?: n
|
|
|
429
487
|
throw runtimeError('CORE-RUNTIME-GENERATION', `解绑 ${binding.address}`,
|
|
430
488
|
`期望代次 ${options.expectGeneration},当前代次 ${binding.generation}`, '重新读取绑定后再解绑');
|
|
431
489
|
}
|
|
490
|
+
// 先删文件再动内存:删文件失败(非「本来就不在」)则内存原样保留并抛出,不会出现重启后页面复活
|
|
491
|
+
if (binding.persisted) runtimeStore().removeBindingStrict(binding.address);
|
|
432
492
|
deleteEntry(binding.address);
|
|
433
|
-
if (binding.persisted) runtimeStore().removeBinding(binding.address);
|
|
434
493
|
if (binding.digest !== null) evictCompiled(binding.digest);
|
|
435
494
|
evictArtifactsOfUrl(binding.address);
|
|
436
495
|
bumpNavGeneration();
|
|
@@ -297,7 +297,13 @@ function fixedAddressesNow(): readonly string[] {
|
|
|
297
297
|
const validatedHosts = new Set<string>();
|
|
298
298
|
export function assertPageGrantsValid(hostDir: string | undefined): void {
|
|
299
299
|
if (hostDir === undefined || validatedHosts.has(hostDir)) return;
|
|
300
|
-
|
|
300
|
+
let policy: ReturnType<typeof readAccessPolicy>;
|
|
301
|
+
try {
|
|
302
|
+
policy = readAccessPolicy(hostDir);
|
|
303
|
+
} catch {
|
|
304
|
+
// 策略读不出来:不在此处拒绝(静态页要保持可达),也不记为已校验;动态页由访问门、写入由 hostGrants() 因同一错误拒绝
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
301
307
|
if (policy.grants.length === 0) return;
|
|
302
308
|
validatePageGrants(policy.grants, { staticAddresses: listStaticAddresses(hostDir), fixedAddresses: fixedAddressesNow() });
|
|
303
309
|
validatedHosts.add(hostDir);
|