soma-core 2.2.5 → 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.
Files changed (259) hide show
  1. package/README.md +5 -6
  2. package/bin/soma.cjs +2 -2
  3. package/core/build/compiler/flat-page.ts +16 -0
  4. package/core/build/compiler/full-manifest-builder.ts +0 -3
  5. package/core/build/compiler/page-assets.ts +0 -1
  6. package/core/build/compiler/page-closure.ts +245 -0
  7. package/core/build/compiler/page-declaration-compiler.ts +2 -5
  8. package/core/build/compiler/soma-precompile.js +1848 -3244
  9. package/core/build/compiler/soma-precompile.ts +4 -7
  10. package/core/build/component-tree/artifact-codec.ts +4 -8
  11. package/core/build/lsp/soma-declaration.cjs +1 -1
  12. package/core/build/lsp/std-completion.generated.json +3 -3
  13. package/core/client/a11y/a11y-config.ts +2 -1
  14. package/core/client/a11y/dom-id.ts +1 -1
  15. package/core/client/base/SomaHttpClient.ts +11 -3
  16. package/core/client/base/SomaPageRootBase.ts +6 -0
  17. package/core/client/base/defineSomaView.ts +178 -0
  18. package/core/client/base/index.ts +13 -0
  19. package/core/client/base/view-context.ts +328 -0
  20. package/core/client/behavior/adapter-collection.ts +2 -2
  21. package/core/client/compiler/manifest-compiler.ts +5 -36
  22. package/core/client/component/component-definition.types.ts +2 -1
  23. package/core/client/component/component-manager.ts +110 -20
  24. package/core/client/component/structure-adapter.ts +3 -7
  25. package/core/client/container/SomaContainerAdapter.ts +9 -1
  26. package/core/client/data/data-object-resolver.ts +516 -181
  27. package/core/client/data/data-write-actions.ts +79 -15
  28. package/core/client/data/format-config.ts +3 -1
  29. package/core/client/data/region-scope-port.ts +11 -0
  30. package/core/client/engine/asset-resolver.ts +2 -5
  31. package/core/client/engine/dom-renderer.ts +7 -40
  32. package/core/client/engine/event-engine.ts +1 -105
  33. package/core/client/engine/property-watcher.ts +1 -1
  34. package/core/client/engine/reactive-proxy.ts +78 -17
  35. package/core/client/engine/runtime-builder.ts +0 -14
  36. package/core/client/engine/scope-resolver.ts +18 -0
  37. package/core/client/engine/soma-bootstrap.ts +385 -98
  38. package/core/client/engine/style-binder.ts +49 -106
  39. package/core/client/engine/timer-engine.ts +11 -1
  40. package/core/client/host-bridge/load-host-domain-class.ts +2 -1
  41. package/core/client/infra/http/SomaHttpService.ts +15 -4
  42. package/core/client/infra/i18n/pack-locales.ts +2 -2
  43. package/core/client/mount/domain-rule.ts +7 -3
  44. package/core/client/mount/mount-page.ts +38 -5
  45. package/core/client/mount/types.ts +2 -0
  46. package/core/client/parser/confirm-spec.ts +1 -1
  47. package/core/client/parser/data-config-parser.ts +27 -210
  48. package/core/client/parser/pipeline-parser.ts +14 -59
  49. package/core/client/parser/style-config-parser.ts +101 -95
  50. package/core/client/parser/timer-parser.ts +21 -0
  51. package/core/client/parser/ui-config-parser.ts +64 -12
  52. package/core/client/registry/declaration-renderer.ts +1 -1
  53. package/core/client/runtime/SomaRuntimeServices.ts +2 -1
  54. package/core/client/runtime/SomaSnapshotStore.ts +5 -0
  55. package/core/client/security/url-guard.ts +71 -11
  56. package/core/client/validation/async-check-registry.ts +2 -1
  57. package/core/client/validation/contract-validator.ts +2 -6
  58. package/core/client/validation/field-rules.ts +3 -13
  59. package/core/client/validation/object-rules.ts +2 -10
  60. package/core/index.ts +1 -35
  61. package/core/inspect/check-product.ts +5 -5
  62. package/core/inspect/runtime.ts +2 -1
  63. package/core/server/api/dispatch-service-api.ts +35 -36
  64. package/core/server/api/load-host-service.ts +5 -4
  65. package/core/server/compile-port.ts +1 -1
  66. package/core/server/host/app-config-composer.ts +2 -1
  67. package/core/server/host/host-assembly-entry.ts +1 -0
  68. package/core/server/host/host-assembly.ts +47 -3
  69. package/core/server/host/host-caller.ts +2 -1
  70. package/core/server/host/host-dir.ts +9 -0
  71. package/core/server/host/host-response-headers.ts +2 -1
  72. package/core/server/host/invoke-host-request-hook.ts +28 -4
  73. package/core/server/host/published-page-assets.ts +2 -1
  74. package/core/server/host/resolve-host-request-hook.ts +25 -7
  75. package/core/server/host/server-page-assets.ts +4 -4
  76. package/core/server/host/virtual-pages.ts +3 -2
  77. package/core/server/infra/cache/ServerFileCache.ts +79 -9
  78. package/core/server/registry/api-registry-store.ts +3 -1
  79. package/core/server/registry/declaration-precompiler.ts +0 -2
  80. package/core/server/registry/page-registry-repository.ts +2 -1
  81. package/core/server/registry/page-registry-store.ts +2 -1
  82. package/core/server/registry/registry-error.ts +6 -1
  83. package/core/server/registry/registry-file-lock.ts +296 -23
  84. package/core/server/registry/request-body-limit.ts +3 -2
  85. package/core/server/registry/validation/declaration-safety-scan.ts +151 -27
  86. package/core/server/runtime-space/access-gate.ts +21 -7
  87. package/core/server/runtime-space/bindings.ts +3 -6
  88. package/core/server/runtime-space/dynamic-pages.ts +181 -49
  89. package/core/server/runtime-space/errors.ts +3 -0
  90. package/core/server/runtime-space/inject.ts +74 -15
  91. package/core/server/runtime-space/page-grants.ts +7 -1
  92. package/core/server/runtime-space/resolve.ts +66 -19
  93. package/core/server/runtime-space/store.ts +32 -3
  94. package/core/server/runtime-space/submit-steps.ts +2 -1
  95. package/core/server/runtime-space/types.ts +3 -1
  96. package/core/shared/component-tree-errors.ts +2 -1
  97. package/core/shared/contract-a11y.ts +1 -1
  98. package/core/shared/contract-sections.ts +2 -2
  99. package/core/shared/core-diagnostic-codes.ts +8 -0
  100. package/core/shared/declaration-keys.ts +1 -1
  101. package/core/shared/engines/engine-error.ts +2 -1
  102. package/core/shared/generation-error.ts +2 -1
  103. package/core/shared/page-envelope.ts +1 -1
  104. package/core/shared/pure-binding.ts +88 -0
  105. package/core/shared/registry-generated/diagnostic-registry-index.ts +16 -19
  106. package/core/shared/registry-generated/diagnostic-registry.ts +135 -177
  107. package/core/shared/registry-generated/limits.ts +49 -13
  108. package/core/shared/registry-generated/std-grammar-completion.ts +0 -4
  109. package/core/shared/registry-generated/std-grammar-views.ts +40 -10
  110. package/core/shared/registry-generated/std-grammar.ts +42 -20
  111. package/core/shared/soma-error.ts +36 -0
  112. package/core/shared/types/compile-port.types.ts +1 -1
  113. package/core/shared/types/data-config.types.ts +17 -17
  114. package/core/shared/types/engine.types.ts +0 -3
  115. package/core/shared/types/event.types.ts +1 -11
  116. package/core/shared/types/generation-error.types.ts +5 -1
  117. package/core/shared/types/host-assembly.types.ts +7 -1
  118. package/core/shared/types/manifest.types.ts +0 -3
  119. package/core/shared/types/region-scope.types.ts +5 -1
  120. package/core/shared/types/scope-manifest.types.ts +0 -2
  121. package/core/shared/types/style-config.types.ts +21 -21
  122. package/core/shared/types/ui-config.types.ts +4 -5
  123. package/core/shared/types/watcher.types.ts +1 -1
  124. package/dist/browser/runtime-bundle.manifest.json +11 -13
  125. package/dist/browser/soma-runtime.js +2567 -2696
  126. package/dist/core/index.cjs +5446 -5486
  127. package/dist/host-native/aot-builder.cjs +46 -1
  128. package/dist/host-native/native-server.cjs +4879 -4505
  129. package/dist/lsp/component-tree.cjs +5814 -7042
  130. package/dist/types/build/compiler/full-manifest-builder.d.ts +0 -2
  131. package/dist/types/build/compiler/page-closure.d.ts +21 -0
  132. package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -4
  133. package/dist/types/build/compiler/soma-precompile.d.ts +1 -3
  134. package/dist/types/build/component-tree/artifact-codec.d.ts +0 -2
  135. package/dist/types/client/a11y/a11y-config.d.ts +2 -1
  136. package/dist/types/client/base/SomaHttpClient.d.ts +1 -1
  137. package/dist/types/client/base/SomaPageRootBase.d.ts +5 -0
  138. package/dist/types/client/base/defineSomaView.d.ts +46 -0
  139. package/dist/types/client/base/index.d.ts +2 -0
  140. package/dist/types/client/base/view-context.d.ts +136 -0
  141. package/dist/types/client/behavior/adapter-collection.d.ts +1 -1
  142. package/dist/types/client/compiler/manifest-compiler.d.ts +0 -3
  143. package/dist/types/client/component/component-definition.types.d.ts +2 -1
  144. package/dist/types/client/component/component-manager.d.ts +29 -0
  145. package/dist/types/client/data/data-object-resolver.d.ts +91 -31
  146. package/dist/types/client/data/format-config.d.ts +2 -1
  147. package/dist/types/client/engine/dom-renderer.d.ts +1 -2
  148. package/dist/types/client/engine/event-engine.d.ts +0 -7
  149. package/dist/types/client/engine/reactive-proxy.d.ts +11 -4
  150. package/dist/types/client/engine/runtime-builder.d.ts +0 -4
  151. package/dist/types/client/engine/soma-bootstrap.d.ts +44 -6
  152. package/dist/types/client/engine/style-binder.d.ts +8 -4
  153. package/dist/types/client/host-bridge/load-host-domain-class.d.ts +2 -1
  154. package/dist/types/client/mount/domain-rule.d.ts +2 -0
  155. package/dist/types/client/mount/mount-page.d.ts +2 -1
  156. package/dist/types/client/mount/types.d.ts +2 -0
  157. package/dist/types/client/parser/data-config-parser.d.ts +6 -5
  158. package/dist/types/client/parser/style-config-parser.d.ts +7 -3
  159. package/dist/types/client/parser/timer-parser.d.ts +5 -0
  160. package/dist/types/client/parser/ui-config-parser.d.ts +6 -2
  161. package/dist/types/client/runtime/SomaSnapshotStore.d.ts +2 -0
  162. package/dist/types/client/security/url-guard.d.ts +14 -0
  163. package/dist/types/client/validation/async-check-registry.d.ts +2 -1
  164. package/dist/types/client/validation/object-rules.d.ts +0 -5
  165. package/dist/types/index.d.ts +2 -7
  166. package/dist/types/inspect/runtime.d.ts +2 -1
  167. package/dist/types/server/api/dispatch-service-api.d.ts +5 -0
  168. package/dist/types/server/api/load-host-service.d.ts +1 -1
  169. package/dist/types/server/compile-port.d.ts +1 -1
  170. package/dist/types/server/host/host-assembly-entry.d.ts +1 -1
  171. package/dist/types/server/host/host-assembly.d.ts +2 -0
  172. package/dist/types/server/host/host-dir.d.ts +2 -0
  173. package/dist/types/server/host/invoke-host-request-hook.d.ts +10 -3
  174. package/dist/types/server/host/published-page-assets.d.ts +2 -1
  175. package/dist/types/server/host/resolve-host-request-hook.d.ts +18 -5
  176. package/dist/types/server/host/server-page-assets.d.ts +2 -1
  177. package/dist/types/server/infra/cache/ServerFileCache.d.ts +8 -0
  178. package/dist/types/server/registry/declaration-precompiler.d.ts +0 -1
  179. package/dist/types/server/registry/registry-error.d.ts +5 -1
  180. package/dist/types/server/registry/registry-file-lock.d.ts +30 -0
  181. package/dist/types/server/registry/request-body-limit.d.ts +3 -2
  182. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +3 -0
  183. package/dist/types/server/runtime-space/access-gate.d.ts +7 -0
  184. package/dist/types/server/runtime-space/bindings.d.ts +3 -2
  185. package/dist/types/server/runtime-space/dynamic-pages.d.ts +6 -1
  186. package/dist/types/server/runtime-space/errors.d.ts +1 -1
  187. package/dist/types/server/runtime-space/resolve.d.ts +2 -0
  188. package/dist/types/server/runtime-space/store.d.ts +8 -1
  189. package/dist/types/server/runtime-space/submit-steps.d.ts +2 -1
  190. package/dist/types/server/runtime-space/types.d.ts +3 -1
  191. package/dist/types/shared/component-tree-errors.d.ts +2 -1
  192. package/dist/types/shared/core-diagnostic-codes.d.ts +1 -1
  193. package/dist/types/shared/declaration-keys.d.ts +1 -1
  194. package/dist/types/shared/engines/engine-error.d.ts +2 -1
  195. package/dist/types/shared/generation-error.d.ts +2 -1
  196. package/dist/types/shared/pure-binding.d.ts +48 -0
  197. package/dist/types/shared/registry-generated/diagnostic-registry.d.ts +119 -158
  198. package/dist/types/shared/registry-generated/limits.d.ts +40 -10
  199. package/dist/types/shared/registry-generated/std-grammar-completion.d.ts +0 -30
  200. package/dist/types/shared/registry-generated/std-grammar-views.d.ts +97 -23
  201. package/dist/types/shared/registry-generated/std-grammar.d.ts +109 -241
  202. package/dist/types/shared/soma-error.d.ts +15 -0
  203. package/dist/types/shared/types/data-config.types.d.ts +14 -16
  204. package/dist/types/shared/types/engine.types.d.ts +0 -3
  205. package/dist/types/shared/types/event.types.d.ts +1 -7
  206. package/dist/types/shared/types/generation-error.types.d.ts +1 -1
  207. package/dist/types/shared/types/host-assembly.types.d.ts +2 -0
  208. package/dist/types/shared/types/manifest.types.d.ts +0 -3
  209. package/dist/types/shared/types/region-scope.types.d.ts +4 -1
  210. package/dist/types/shared/types/scope-manifest.types.d.ts +0 -2
  211. package/dist/types/shared/types/style-config.types.d.ts +17 -19
  212. package/dist/types/shared/types/ui-config.types.d.ts +4 -5
  213. package/host-native/bin/host-native.cjs +22 -1
  214. package/host-native/client/browser-entry.ts +21 -172
  215. package/host-native/src/aot-builder.ts +52 -0
  216. package/host-native/src/host-prepare.ts +39 -5
  217. package/host-native/src/manifest-http.ts +2 -3
  218. package/host-native/src/native-server.ts +812 -174
  219. package/host-native/src/page-http.ts +3 -3
  220. package/host-native/src/public-packages.cjs +0 -1
  221. package/host-native/src/registry-handler.ts +12 -5
  222. package/host-native/src/static-file.ts +203 -53
  223. package/package.json +3 -2
  224. package/core/client/engine/step-runner.ts +0 -212
  225. package/core/client/engine/trigger-engine.ts +0 -55
  226. package/core/client/format/zoned.ts +0 -10
  227. package/core/client/parser/step-parser.ts +0 -359
  228. package/core/client/parser/trigger-parser.ts +0 -90
  229. package/core/client/parser/value-lang.ts +0 -340
  230. package/core/client/time/instant.ts +0 -2
  231. package/core/shared/calc/coerce.ts +0 -2
  232. package/core/shared/calc/errors.ts +0 -40
  233. package/core/shared/calc/evaluator.ts +0 -113
  234. package/core/shared/calc/index.ts +0 -11
  235. package/core/shared/calc/lexer.ts +0 -189
  236. package/core/shared/calc/parser.ts +0 -193
  237. package/core/shared/calc/typer.ts +0 -171
  238. package/core/shared/date-parts.ts +0 -2
  239. package/core/shared/show-when.ts +0 -77
  240. package/core/shared/types/step.types.ts +0 -91
  241. package/core/shared/types/trigger.types.ts +0 -14
  242. package/dist/types/client/engine/step-runner.d.ts +0 -42
  243. package/dist/types/client/engine/trigger-engine.d.ts +0 -16
  244. package/dist/types/client/format/zoned.d.ts +0 -1
  245. package/dist/types/client/parser/step-parser.d.ts +0 -23
  246. package/dist/types/client/parser/trigger-parser.d.ts +0 -13
  247. package/dist/types/client/parser/value-lang.d.ts +0 -63
  248. package/dist/types/client/time/instant.d.ts +0 -1
  249. package/dist/types/shared/calc/coerce.d.ts +0 -1
  250. package/dist/types/shared/calc/errors.d.ts +0 -14
  251. package/dist/types/shared/calc/evaluator.d.ts +0 -2
  252. package/dist/types/shared/calc/index.d.ts +0 -6
  253. package/dist/types/shared/calc/lexer.d.ts +0 -19
  254. package/dist/types/shared/calc/parser.d.ts +0 -43
  255. package/dist/types/shared/calc/typer.d.ts +0 -11
  256. package/dist/types/shared/date-parts.d.ts +0 -1
  257. package/dist/types/shared/show-when.d.ts +0 -21
  258. package/dist/types/shared/types/step.types.d.ts +0 -76
  259. package/dist/types/shared/types/trigger.types.d.ts +0 -12
@@ -1,4 +1,5 @@
1
1
  import { loadHostAssembly } from './host-assembly';
2
+ import { currentHostDir } from './host-dir';
2
3
 
3
4
  export type SomaHostRequestKind = 'page' | 'api';
4
5
 
@@ -6,25 +7,42 @@ export interface SomaHostRequestContext {
6
7
  kind: SomaHostRequestKind;
7
8
  path: string;
8
9
  pageHref?: string;
10
+ /** 原生宿主可选的惰性 body 入口;一次读取、限流、断连取消,返回独立字节副本。 */
11
+ readBody?: () => Promise<Uint8Array>;
9
12
  }
10
13
 
11
14
  /**
12
15
  * Generic server ingress extension. soma-core does not know authentication,
13
16
  * authorization, Admin, or any business application.
14
17
  *
15
- * The hook receives the same Request object later dispatched to the Host
16
- * service. A Host may therefore normalize or add server-only request headers
17
- * before returning undefined. Returning a Response still short-circuits the
18
- * request. Secrets must never be copied into a response or client config.
18
+ * A Host may normalize or add server-only request headers before returning
19
+ * undefined. The native adapter constructs a separate service Request with the
20
+ * same post-hook headers and the same original body bytes; Fetch bodies are
21
+ * one-shot, so the hook Request object itself is not reused for dispatch. On
22
+ * native API requests, use optional context.readBody() to lazily read a copied
23
+ * byte array without changing request.body; other adapters may omit it.
24
+ * Returning a Response still short-circuits the request. Client cancellation
25
+ * stops transport work only; it does not roll back server-side writes.
19
26
  */
20
27
  export type SomaRequestHook = (
21
28
  request: Request,
22
29
  context: SomaHostRequestContext,
23
30
  ) => Response | undefined | Promise<Response | undefined>;
24
31
 
25
- export async function resolveHostRequestHook(): Promise<SomaRequestHook | null> {
26
- // 先等服务端面装配完(失败即拒绝启动,原样上抛),请求钩子只取 soma.app.ts 装配的
27
- const hostDir = process.env.SOMA_HOST_DIR;
32
+ export interface ResolveHostRequestHookOptions {
33
+ /**
34
+ * 显式传入的主机根目录。
35
+ * 仅当调用方未传时才回退环境变量 process.env.SOMA_HOST_DIR(兼容旧对外导出调用方)。
36
+ */
37
+ readonly hostDir?: string;
38
+ }
39
+
40
+ export async function resolveHostRequestHook(
41
+ options?: ResolveHostRequestHookOptions | string,
42
+ ): Promise<SomaRequestHook | null> {
43
+ // 先等服务端面装配完(失败即拒绝启动,原样上抛),请求钩子只取 soma.app.ts 装配的。
44
+ // 显式 hostDir 优先;仅当调用方未传时才回退环境变量 process.env.SOMA_HOST_DIR(兼容对外导出)
45
+ const hostDir = (typeof options === 'string' ? options : options?.hostDir) ?? currentHostDir();
28
46
  if (hostDir !== undefined && hostDir.trim() !== '') {
29
47
  const assembly = await loadHostAssembly(hostDir);
30
48
  if (assembly !== null) return assembly.requestHook?.() ?? null;
@@ -1,3 +1,4 @@
1
+ import { SomaError } from '../../shared/soma-error';
1
2
  import { existsSync, readFileSync } from 'node:fs';
2
3
  import { isAbsolute, join, relative, resolve } from 'node:path';
3
4
  import { isSafePageBase, urlPathToCandidates } from '../../build/compiler/page-base-resolver';
@@ -47,7 +48,7 @@ export interface SomaServerPageAssets {
47
48
  titleKey?: string;
48
49
  }
49
50
 
50
- export class SomaPageAssetError extends Error {
51
+ export class SomaPageAssetError extends SomaError {
51
52
  readonly code: string;
52
53
  readonly status: number;
53
54
 
@@ -60,11 +61,10 @@ export class SomaPageAssetError extends Error {
60
61
  }
61
62
 
62
63
  const cache = ServerFileCache.getInstance();
63
- const watchedRoots = new Set<string>();
64
64
 
65
+ /** 以缓存自己的监听登记为准:监听出错被移除后,下次读取会重新建立(失败次数上限在缓存内) */
65
66
  function ensureWatch(root: string): void {
66
- if (watchedRoots.has(root)) return;
67
- watchedRoots.add(root);
67
+ if (cache.isWatching(root)) return;
68
68
  cache.watchAndInvalidate(root);
69
69
  }
70
70
 
@@ -29,6 +29,7 @@ import { runtimeAssetsOfArtifact } from './published-page-assets';
29
29
  import { resolveHostTreeCompileContext } from './resolve-host-tree-context';
30
30
  import { bindPrefixHandler, validateAddress } from '../runtime-space/inject';
31
31
  import { isHostAssemblyReady, loadHostAssemblySync } from './host-assembly';
32
+ import { currentHostDir } from './host-dir';
32
33
 
33
34
  /**
34
35
  * 解析器交回的虚拟页:声明(与 Registry 声明同一个闭集)+ 可选标题。
@@ -99,7 +100,7 @@ const loadedHosts = new Set<string>();
99
100
  * 按 `SOMA_HOST_DIR` 读 Host 装配的 `virtualPages()` 并登记一次。没有目录 / 没有 `soma.app.ts` → 什么也不做;
100
101
  * 装配形状不对在装配读取时已当场抛。
101
102
  */
102
- export function ensureHostVirtualPages(hostDir: string | undefined = process.env.SOMA_HOST_DIR): void {
103
+ export function ensureHostVirtualPages(hostDir: string | undefined = currentHostDir()): void {
103
104
  if (hostDir === undefined || hostDir.trim() === '') return;
104
105
  const key = path.resolve(hostDir);
105
106
  if (loadedHosts.has(key)) return;
@@ -154,7 +155,7 @@ function virtualPageAssets(page: SomaVirtualPage, pagePath: string): Record<stri
154
155
  const { declaration } = page;
155
156
  let treeContext;
156
157
  if (declaration.markup === 'component-tree') {
157
- treeContext = resolveHostTreeCompileContext(process.env.SOMA_HOST_DIR, { pageBase: pagePath });
158
+ treeContext = resolveHostTreeCompileContext(currentHostDir(), { pageBase: pagePath });
158
159
  if (!treeContext) {
159
160
  throw new SomaComponentTreeError({
160
161
  code: 'CT-REGISTRY-CONTEXT', phase: 'artifact', sourceId: '<virtual>', nodePath: 'treeContext',
@@ -1,5 +1,6 @@
1
1
  import { readFileSync, readdirSync, watch, type FSWatcher } from 'fs';
2
2
  import { resolve, extname, normalize, sep } from 'path';
3
+ import { SOMA_PIPELINE_LIMITS } from '../../../shared/registry-generated/limits';
3
4
 
4
5
  export interface ServerFileCacheOptions {
5
6
  maxFiles?: number;
@@ -12,6 +13,12 @@ interface FileEntry {
12
13
  bytes: number;
13
14
  }
14
15
 
16
+ /**
17
+ * 每个根「连续失败后至多再重试一次」:结构性上限(连续两次失败即放弃;监听真正投递过变更回调后计数清零),取设计真源 limits.json#cache.watchFailLimit。
18
+ * 缓存类里没有现成的时间间隔字段,故不用退避,改为放弃后绕过缓存。
19
+ */
20
+ const WATCH_FAIL_LIMIT: number = SOMA_PIPELINE_LIMITS.cache.watchFailLimit;
21
+
15
22
  /**
16
23
  * 服务端文件缓存单例(有界 LRU + 监听去重)
17
24
  *
@@ -29,12 +36,18 @@ export class ServerFileCache {
29
36
  private dirCache = new Map<string, string[]>();
30
37
  private watchers: FSWatcher[] = [];
31
38
  private watchedRoots = new Set<string>();
39
+ /** 各根连续的监听失败次数(建立失败或运行中 error);建立成功不清零(递归监听的错误是建立之后才异步到达的),只有该根的监听真正投递过一次变更回调才清零;达到 `WATCH_FAIL_LIMIT` 即放弃该根 */
40
+ private watchFailures = new Map<string, number>();
41
+ /** 放弃监听的根:其下的读取绕过缓存直读磁盘(无人失效时,正确性优先于命中率) */
42
+ private unwatchableRoots = new Set<string>();
32
43
  private currentFileBytes = 0;
33
44
 
34
45
  constructor(options: ServerFileCacheOptions = {}) {
35
- this.maxFiles = options.maxFiles ?? 512;
36
- this.maxFileBytes = options.maxFileBytes ?? 33_554_432;
37
- this.maxDirs = options.maxDirs ?? 256;
46
+ // 缺省值取设计真源 limits.json#cache.serverFile* / serverDir*;构造选项优先
47
+ const { serverFileMaxEntries, serverFileMaxBytes, serverDirMaxEntries } = SOMA_PIPELINE_LIMITS.cache;
48
+ this.maxFiles = options.maxFiles ?? serverFileMaxEntries;
49
+ this.maxFileBytes = options.maxFileBytes ?? serverFileMaxBytes;
50
+ this.maxDirs = options.maxDirs ?? serverDirMaxEntries;
38
51
  }
39
52
 
40
53
  static getInstance(options?: ServerFileCacheOptions): ServerFileCache {
@@ -65,6 +78,7 @@ export class ServerFileCache {
65
78
  */
66
79
  readFile(filePath: string): string {
67
80
  const key = normalize(filePath);
81
+ if (this.isUnwatchable(key)) return readFileSync(filePath, 'utf-8');
68
82
  const cached = this.fileCache.get(key);
69
83
  if (cached !== undefined) {
70
84
  this.fileCache.delete(key);
@@ -120,6 +134,9 @@ export class ServerFileCache {
120
134
  */
121
135
  readDirFiles(dirPath: string, ext: string): string[] {
122
136
  const dirKey = `${normalize(dirPath)}:${ext}`;
137
+ if (this.isUnwatchable(normalize(dirPath))) {
138
+ return readdirSync(dirPath).filter((f) => extname(f) === ext).map((f) => this.readFile(resolve(dirPath, f)));
139
+ }
123
140
  const cached = this.dirCache.get(dirKey);
124
141
  if (cached !== undefined) {
125
142
  this.dirCache.delete(dirKey);
@@ -137,6 +154,7 @@ export class ServerFileCache {
137
154
 
138
155
  readDirNames(dirPath: string, ext: string): string[] {
139
156
  const dirKey = `${normalize(dirPath)}:names:${ext}`;
157
+ if (this.isUnwatchable(normalize(dirPath))) return readdirSync(dirPath).filter((f) => extname(f) === ext);
140
158
  const cached = this.dirCache.get(dirKey);
141
159
  if (cached !== undefined) {
142
160
  this.dirCache.delete(dirKey);
@@ -161,23 +179,53 @@ export class ServerFileCache {
161
179
  this.dirCache.set(key, list);
162
180
  }
163
181
 
164
- /**
165
- * 监听目录变化,自动失效缓存。
166
- * 使用 fs.watch 递归监听;已监听同一目录或其祖先目录时自动去重。
167
- */
168
- watchAndInvalidate(rootDir: string): void {
182
+ private isUnwatchable(p: string): boolean {
183
+ const abs = resolve(p);
184
+ for (const root of this.unwatchableRoots) {
185
+ if (abs === root || abs.startsWith(root.endsWith(sep) ? root : `${root}${sep}`)) return true;
186
+ }
187
+ return false;
188
+ }
189
+
190
+ private recordWatchFailure(root: string): void {
191
+ const count = (this.watchFailures.get(root) ?? 0) + 1;
192
+ this.watchFailures.set(root, count);
193
+ if (count >= WATCH_FAIL_LIMIT) {
194
+ if (!this.unwatchableRoots.has(root)) {
195
+ // 放弃监听必须留痕(不受环境影响),否则文件改动不再失效缓存的原因无从查起
196
+ console.warn(`[soma-core][缓存] 对 ${root} 的监听连续失败 ${count} 次,已放弃监听:该目录下的文件改为每次直读、不进缓存。`);
197
+ }
198
+ this.unwatchableRoots.add(root);
199
+ this.invalidate();
200
+ }
201
+ }
202
+
203
+ /** 该根(或其祖先)当前是否有存活的监听;监听出错被移除后为 false,调用方据此重试 `watchAndInvalidate` */
204
+ isWatching(rootDir: string): boolean {
169
205
  const resolvedRoot = resolve(rootDir);
170
206
  for (const watched of this.watchedRoots) {
171
207
  if (
172
208
  resolvedRoot === watched ||
173
209
  resolvedRoot.startsWith(watched.endsWith(sep) ? watched : `${watched}${sep}`)
174
210
  ) {
175
- return;
211
+ return true;
176
212
  }
177
213
  }
214
+ return false;
215
+ }
216
+
217
+ /**
218
+ * 监听目录变化,自动失效缓存。
219
+ * 使用 fs.watch 递归监听;已监听同一目录或其祖先目录时自动去重。
220
+ */
221
+ watchAndInvalidate(rootDir: string): void {
222
+ const resolvedRoot = resolve(rootDir);
223
+ if (this.isWatching(resolvedRoot) || this.unwatchableRoots.has(resolvedRoot)) return;
178
224
 
179
225
  try {
180
226
  const watcher = watch(resolvedRoot, { recursive: true }, (_event, filename) => {
227
+ // 监听投递过变更回调,证明它在工作:此前的失败计数作废
228
+ this.watchFailures.delete(resolvedRoot);
181
229
  if (!filename) return;
182
230
 
183
231
  const fullPath = normalize(resolve(resolvedRoot, filename));
@@ -189,9 +237,29 @@ export class ServerFileCache {
189
237
  this.dirCache.clear();
190
238
  });
191
239
 
240
+ // 目录被删或 inotify 耗尽时 FSWatcher 会发 error;无监听器则成为未捕获异常崩进程。
241
+ watcher.on('error', (err: unknown) => {
242
+ console.warn(
243
+ `[soma-core][缓存] fs.watch 出错(${(err as NodeJS.ErrnoException)?.code ?? (err as Error)?.message}),`
244
+ + `已关闭对 ${resolvedRoot} 的监听并清空缓存。`,
245
+ );
246
+ try {
247
+ watcher.close();
248
+ } catch {
249
+ // ignore
250
+ }
251
+ this.watchers = this.watchers.filter((w) => w !== watcher);
252
+ this.watchedRoots.delete(resolvedRoot);
253
+ this.invalidate();
254
+ this.recordWatchFailure(resolvedRoot);
255
+ });
256
+ // 监听器只负责失效缓存,进程是否存活由 HTTP 服务等决定:unref 让它不阻止进程退出(期望行为)
257
+ (watcher as { unref?: () => unknown }).unref?.();
258
+
192
259
  this.watchers.push(watcher);
193
260
  this.watchedRoots.add(resolvedRoot);
194
261
  } catch (e: any) {
262
+ this.recordWatchFailure(resolvedRoot);
195
263
  if (process.env.NODE_ENV !== 'production') {
196
264
  console.warn(
197
265
  `[soma-core][缓存] fs.watch recursive failed (${e.code ?? e.message}). ` +
@@ -231,6 +299,8 @@ export class ServerFileCache {
231
299
  }
232
300
  this.watchers = [];
233
301
  this.watchedRoots.clear();
302
+ this.watchFailures.clear();
303
+ this.unwatchableRoots.clear();
234
304
  ServerFileCache.instance = null;
235
305
  }
236
306
  }
@@ -20,7 +20,7 @@ import fs from 'node:fs';
20
20
  import path from 'node:path';
21
21
  import { currentRuntimeLayout } from '../runtime-space/root';
22
22
  import type { SomaDataFieldConfig } from '../../shared/types/data-config.types';
23
- import { withRegistryLock, writeFileAtomic } from './registry-file-lock';
23
+ import { assertRegistryLockOwned, withRegistryLock, writeFileAtomic } from './registry-file-lock';
24
24
  import { RegistryError } from './registry-error';
25
25
  import { scanCredentialLeak } from './validation/credential-ban';
26
26
  import { SOMA_PIPELINE_LIMITS } from '../../shared/registry-generated/limits';
@@ -178,6 +178,8 @@ export class ApiRegistryStore {
178
178
  if (!record) {
179
179
  throw new ApiRegistryError(ApiRegistryErrorCodes.API_NOT_FOUND, `接口未注册:${safeName}`);
180
180
  }
181
+ // 删除同样要先确认锁仍归自己:锁被接管后恢复的旧持有者不能删掉新持有者刚写入的记录
182
+ assertRegistryLockOwned(this.fileOf(safeName));
181
183
  fs.rmSync(this.fileOf(safeName), { force: true });
182
184
  this.cache.delete(safeName);
183
185
  return cloneApiRecord(record);
@@ -56,7 +56,6 @@ export interface CompiledDeclaration extends ComponentTreeRuntimeEnvelope {
56
56
  assetSupplyKey?: string;
57
57
  uiConfig?: unknown;
58
58
  eventConfig?: unknown;
59
- triggerConfig?: unknown;
60
59
  timerConfig?: unknown;
61
60
  styleConfig?: unknown;
62
61
  dataConfig?: unknown;
@@ -123,7 +122,6 @@ export function precompileDeclaration(
123
122
  } : supplyKey ? { assetSupplyKey: supplyKey } : {}),
124
123
  uiConfig: page.uiConfig,
125
124
  eventConfig: page.eventConfig,
126
- triggerConfig: page.triggers,
127
125
  timerConfig: page.timers,
128
126
  ...(page.styleConfig ? { styleConfig: page.styleConfig } : {}),
129
127
  ...(page.dataConfig ? { dataConfig: page.dataConfig } : {}),
@@ -46,6 +46,7 @@ import type { SomaPageGenerationAssets } from '../../shared/types/compile-port.t
46
46
  import type { TreeCompileContext } from '../../shared/types/component-tree.types';
47
47
  import { resolveHostTreeCompileContext } from '../host/resolve-host-tree-context';
48
48
  import { SOMA_PIPELINE_LIMITS } from '../../shared/registry-generated/limits';
49
+ import { currentHostDir } from '../host/host-dir';
49
50
 
50
51
  const MAX_CAS_ATTEMPTS = SOMA_PIPELINE_LIMITS.retry.casAttempts;
51
52
  const AGGREGATE_SCHEMA_VERSION = 1;
@@ -326,7 +327,7 @@ export class ConditionalPageRegistryRepository implements PageRegistryRepository
326
327
  getTreeCompileContext(name: string, app: string): TreeCompileContext | undefined {
327
328
  return this.options.treeContextResolver
328
329
  ? this.options.treeContextResolver(name, app)
329
- : resolveHostTreeCompileContext(process.env.SOMA_HOST_DIR, { pageBase: name, app });
330
+ : resolveHostTreeCompileContext(currentHostDir(), { pageBase: name, app });
330
331
  }
331
332
 
332
333
  async publish(
@@ -57,6 +57,7 @@ import { scanRegionScopes } from './validation/region-scopes';
57
57
  import { SOMA_PIPELINE_LIMITS } from '../../shared/registry-generated/limits';
58
58
  import { validateAddressText } from '../../shared/runtime-address';
59
59
  import { SOMA_PIPELINE_ERROR_CODES } from '../../shared/registry-generated/error-codes';
60
+ import { currentHostDir } from '../host/host-dir';
60
61
 
61
62
  /** 页面名约束:小写字母 / 数字 / 连字符,1~64 位(可直接用作文件名与 URL 片段) */
62
63
  export const PAGE_NAME_PATTERN = /^[a-z0-9-]{1,64}$/;
@@ -408,7 +409,7 @@ export class PageRegistryStore {
408
409
  this.baseDir = path.resolve(options.baseDir ?? currentRuntimeLayout().dirs.pages);
409
410
  this.now = options.now ?? Date.now;
410
411
  this.treeContextResolver = options.treeContextResolver ?? ((name, app) => (
411
- resolveHostTreeCompileContext(process.env.SOMA_HOST_DIR, { pageBase: name, app })
412
+ resolveHostTreeCompileContext(currentHostDir(), { pageBase: name, app })
412
413
  ));
413
414
  }
414
415
 
@@ -1,3 +1,5 @@
1
+ import { SomaError } from '../../shared/soma-error';
2
+
1
3
  /**
2
4
  * 注册中心的错误基类。
3
5
  *
@@ -5,8 +7,11 @@
5
7
  * 响应映射器。给两者一个共同基类,映射器就只需要认识**一种**错误——
6
8
  * 否则每加一张表都要去改一次 `pageRegistryErrorResponse`,漏改的表现是
7
9
  * 业务错误原样抛穿成 500(实测:API 表刚接上时 404 变成了未处理异常)。
10
+ *
11
+ * 同时继承 core 统一错误基类 `SomaError`:按 `instanceof SomaError` 统一处理 / 上报的代码能认出它。
12
+ * 数字码 `code`(45xx)原样保留、仍按数字比较;基类的 `code` 类型已放宽为 `string | number`,这里收窄为 `number`。
8
13
  */
9
- export class RegistryError extends Error {
14
+ export class RegistryError extends SomaError {
10
15
  readonly code: number;
11
16
 
12
17
  constructor(code: number, message: string) {