soma-core 2.2.5 → 2.2.7
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 +1972 -3312
- 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/IContainerAdapter.ts +4 -0
- package/core/client/container/SomaContainerAdapter.ts +52 -3
- package/core/client/data/data-object-resolver.ts +523 -184
- 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 +29 -108
- 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 +387 -99
- 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/infra/notify/DefaultNotifyService.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 +100 -7
- package/core/client/parser/data-config-parser.ts +28 -211
- package/core/client/parser/pipeline-parser.ts +16 -60
- 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/render-ports/hide-transition.ts +9 -0
- package/core/client/render-ports/index.ts +4 -0
- package/core/client/render-ports/notify-renderer.ts +2 -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 +17 -39
- 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 +8 -4
- package/core/server/api/paged-list.ts +259 -0
- package/core/server/compile-port.ts +1 -1
- package/core/server/host/app-config-composer.ts +2 -1
- package/core/server/host/empty-host-bootstrap.mjs +3 -0
- package/core/server/host/host-assembly-entry.ts +1 -0
- package/core/server/host/host-assembly.ts +50 -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/source-aliases.cjs +345 -0
- package/core/server/host/source-aliases.ts +32 -0
- package/core/server/host/source-packages.cjs +1 -0
- package/core/server/host/virtual-pages.ts +3 -2
- package/core/server/infra/cache/ServerFileCache.ts +83 -10
- package/core/server/node.ts +10 -0
- 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 +78 -16
- 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 +14 -0
- package/core/shared/data-declaration-checks.ts +29 -3
- 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 +21 -19
- package/core/shared/registry-generated/diagnostic-registry.ts +170 -177
- package/core/shared/registry-generated/limits.ts +65 -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 +43 -21
- 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 +20 -18
- package/core/shared/types/engine.types.ts +0 -3
- package/core/shared/types/event.types.ts +10 -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 +2733 -2712
- package/dist/core/index.cjs +5665 -5534
- package/dist/host-native/aot-builder.cjs +330 -1
- package/dist/host-native/native-server.cjs +5434 -4726
- package/dist/lsp/component-tree.cjs +5868 -7040
- 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/container/IContainerAdapter.d.ts +3 -0
- package/dist/types/client/container/SomaContainerAdapter.d.ts +5 -0
- package/dist/types/client/data/data-object-resolver.d.ts +93 -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/confirm-spec.d.ts +27 -3
- 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/render-ports/hide-transition.d.ts +9 -0
- package/dist/types/client/render-ports/index.d.ts +4 -0
- package/dist/types/client/render-ports/notify-renderer.d.ts +2 -1
- 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 +6 -11
- 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/api/paged-list.d.ts +101 -0
- 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/host/source-aliases.d.cts +53 -0
- package/dist/types/server/host/source-aliases.d.ts +30 -0
- package/dist/types/server/host/source-packages.d.cts +2 -0
- package/dist/types/server/infra/cache/ServerFileCache.d.ts +8 -0
- package/dist/types/server/node.d.ts +1 -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 +149 -158
- package/dist/types/shared/registry-generated/limits.d.ts +54 -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 +110 -242
- package/dist/types/shared/soma-error.d.ts +15 -0
- package/dist/types/shared/types/data-config.types.d.ts +17 -17
- package/dist/types/shared/types/engine.types.d.ts +0 -3
- package/dist/types/shared/types/event.types.d.ts +9 -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 +21 -172
- package/host-native/src/aot-builder.ts +75 -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/host-native/styles/globals.css +30 -28
- package/package.json +14 -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
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// 服务端分页整形助手(协议中性、纯函数、无 node 依赖;服务端薄壳 / 手写 service 用,不进浏览器包)。
|
|
2
|
+
//
|
|
3
|
+
// 页面的列表数据读取约定:`GET ?pageNo=&pageSize=&<筛选字段>=…`(键名可用 pageKey / sizeKey 改,筛选字段名即字段名,空值不带),
|
|
4
|
+
// 期望响应 `{ rows, total }`。旧后台常见三种不一样,这里只做「响应形状翻译」,不碰业务、不发请求(请求由调用方的 load 函数发):
|
|
5
|
+
// 1 后端页码键叫 page / pageNum,页长叫 size / limit,筛选参数名与字段名不同 -> createPagedListAdapter 的 upstream* 与 filterMap;
|
|
6
|
+
// 2 响应是业务信封(失败判定 / 数据位置由调用方声明:failWhen / messagePath / dataPath),列表键与总数键不统一
|
|
7
|
+
// -> unwrapResponseEnvelope + 适配器的 rowsPath / totalPaths;
|
|
8
|
+
// 3 游标分页(cursor / limit / nextCursor),没有页码 -> cursorPageWindow(沿游标走到第 N 页)。
|
|
9
|
+
//
|
|
10
|
+
// 信封失败判定复用 `envelopeFailureOf`(与 `data:` 的 failWhen 同一份实现),不另写第二套谓词求值。
|
|
11
|
+
|
|
12
|
+
import { envelopeFailureOf, type SomaEnvelopeFailure } from '../../client/data/envelope-failure';
|
|
13
|
+
import { SOMA_PIPELINE_LIMITS } from '../../shared/registry-generated/limits';
|
|
14
|
+
|
|
15
|
+
type Rec = Record<string, unknown>;
|
|
16
|
+
|
|
17
|
+
const isRec = (v: unknown): v is Rec => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
18
|
+
|
|
19
|
+
/** 缺省页长 / 页长上限(调用方可用 defaultPageSize / maxPageSize 覆盖);数字只在 design 真源 pipeline/limits.json 的 pagedList,经生成表读 */
|
|
20
|
+
export const SOMA_PAGED_LIST_LIMITS = {
|
|
21
|
+
pageSizeDefault: SOMA_PIPELINE_LIMITS.pagedList.pageSizeDefault,
|
|
22
|
+
pageSizeMax: SOMA_PIPELINE_LIMITS.pagedList.pageSizeMax,
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
const DEFAULT_PAGE_KEY = 'pageNo';
|
|
26
|
+
const DEFAULT_SIZE_KEY = 'pageSize';
|
|
27
|
+
const RESERVED_EXTRA: readonly string[] = ['window', 'sort', 'order'];
|
|
28
|
+
const DEFAULT_ROW_KEYS: readonly string[] = ['list', 'records', 'rows', 'items', 'data'];
|
|
29
|
+
const DEFAULT_TOTAL_PATHS: readonly string[] = ['totalCount', 'total', 'count'];
|
|
30
|
+
|
|
31
|
+
function positiveInt(raw: unknown): number | null {
|
|
32
|
+
const n = typeof raw === 'number' ? raw : typeof raw === 'string' && raw.trim() !== '' ? Number(raw) : Number.NaN;
|
|
33
|
+
return Number.isInteger(n) && n > 0 ? n : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function pathGet(root: unknown, path: string): unknown {
|
|
37
|
+
let cur = root;
|
|
38
|
+
for (const key of path.split('.')) {
|
|
39
|
+
if (!isRec(cur)) return undefined;
|
|
40
|
+
cur = cur[key];
|
|
41
|
+
}
|
|
42
|
+
return cur;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ---------------------------------------------------------------- 业务信封
|
|
46
|
+
|
|
47
|
+
/** 响应信封声明:failWhen 为闭集谓词(命中 = 失败)、messagePath 为失败文案点路径、dataPath 为成功数据点路径(缺省不拆) */
|
|
48
|
+
export interface SomaResponseEnvelope {
|
|
49
|
+
failWhen?: Rec;
|
|
50
|
+
messagePath?: string;
|
|
51
|
+
dataPath?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type SomaUnwrappedEnvelope = { ok: true; data: unknown } | { ok: false; error: Rec };
|
|
55
|
+
|
|
56
|
+
/** 响应命中信封失败判定(没有声明 failWhen 一律不判失败) */
|
|
57
|
+
export function isResponseEnvelopeFailure(raw: unknown, env?: SomaResponseEnvelope): raw is Rec {
|
|
58
|
+
if (env?.failWhen === undefined) return false;
|
|
59
|
+
return envelopeFailureOf(raw, { failWhen: env.failWhen as unknown as SomaEnvelopeFailure['failWhen'] }, '') !== null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 拆信封:失败信封原样放进 `error`;成功按 dataPath 取数据(没声明不拆) */
|
|
63
|
+
export function unwrapResponseEnvelope(raw: unknown, env?: SomaResponseEnvelope): SomaUnwrappedEnvelope {
|
|
64
|
+
if (isResponseEnvelopeFailure(raw, env)) return { ok: false, error: raw };
|
|
65
|
+
if (env?.dataPath === undefined) return { ok: true, data: raw };
|
|
66
|
+
return { ok: true, data: pathGet(raw, env.dataPath) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ---------------------------------------------------------------- 页码协议适配器
|
|
70
|
+
|
|
71
|
+
export interface PagedListAdapterOptions {
|
|
72
|
+
/** 请求里的页码键 / 页长键(缺省 pageNo / pageSize;其余键都当筛选) */
|
|
73
|
+
pageKey?: string;
|
|
74
|
+
sizeKey?: string;
|
|
75
|
+
/** 上游页码参数名(缺省 page) */
|
|
76
|
+
upstreamPage?: string;
|
|
77
|
+
/** 上游页长参数名(缺省 pageSize) */
|
|
78
|
+
upstreamSize?: string;
|
|
79
|
+
/** 响应里行数组的位置:点路径(相对拆信封后的 data),或候选键列表(取第一个数组);缺省 list / records / rows / items / data */
|
|
80
|
+
rowsPath?: string | readonly string[];
|
|
81
|
+
/** 总数的候选位置(相对拆信封后的 data);缺省 totalCount / total / count */
|
|
82
|
+
totalPaths?: readonly string[];
|
|
83
|
+
/** 筛选参数:字段名 -> 上游参数名;数组 = 同名透传的白名单;缺省收全部非保留参数同名透传 */
|
|
84
|
+
filterMap?: Readonly<Record<string, string>> | readonly string[];
|
|
85
|
+
/** 给行补主键 id:依次取这些键里第一个非空值(已有非空 id 不动) */
|
|
86
|
+
rowId?: string | readonly string[];
|
|
87
|
+
/** 缺省页长 / 页长上限(缺省 SOMA_PAGED_LIST_LIMITS) */
|
|
88
|
+
defaultPageSize?: number;
|
|
89
|
+
maxPageSize?: number;
|
|
90
|
+
/** 业务信封声明(由调用方显式传入;缺省按无信封处理,不读任何全局) */
|
|
91
|
+
envelope?: SomaResponseEnvelope;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface PagedQuery {
|
|
95
|
+
/** 从 1 起 */
|
|
96
|
+
page: number;
|
|
97
|
+
pageSize: number;
|
|
98
|
+
/** 字段名 -> 值(已去空白,空值不进) */
|
|
99
|
+
filters: Record<string, string>;
|
|
100
|
+
/** 可直接拼到上游请求的参数:页码、页长、已改名的筛选 */
|
|
101
|
+
upstream: Record<string, string | number>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface PagedList {
|
|
105
|
+
rows: Rec[];
|
|
106
|
+
total: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface PagedListAdapter {
|
|
110
|
+
/** 请求里没有页码键 = 旧调用方(调用方可据此原样转交旧服务) */
|
|
111
|
+
hasPage(input: unknown): boolean;
|
|
112
|
+
readQuery(input: unknown): PagedQuery;
|
|
113
|
+
/** 上游响应 -> 列表;失败信封返回 { error } */
|
|
114
|
+
toList(raw: unknown, mapRow?: (row: Rec, index: number) => Rec): PagedList | { error: Rec };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function entriesOf(input: unknown): Array<[string, unknown]> {
|
|
118
|
+
if (input instanceof URLSearchParams) return [...input.entries()];
|
|
119
|
+
if (typeof input === 'string') return [...new URLSearchParams(input.includes('?') ? input.slice(input.indexOf('?') + 1) : input).entries()];
|
|
120
|
+
if (isRec(input)) return Object.entries(input);
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function rowsOf(data: unknown, rowsPath: string | readonly string[] | undefined): Rec[] {
|
|
125
|
+
let list: unknown;
|
|
126
|
+
if (Array.isArray(data)) list = data;
|
|
127
|
+
else if (typeof rowsPath === 'string') list = pathGet(data, rowsPath);
|
|
128
|
+
else list = (rowsPath ?? DEFAULT_ROW_KEYS).map((key) => (isRec(data) ? data[key] : undefined)).find(Array.isArray);
|
|
129
|
+
return Array.isArray(list) ? list.filter(isRec) : [];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function totalOf(data: unknown, paths: readonly string[], fallback: number): number {
|
|
133
|
+
for (const path of paths) {
|
|
134
|
+
const v = pathGet(data, path);
|
|
135
|
+
if (typeof v === 'number' && Number.isFinite(v)) return v;
|
|
136
|
+
if (typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v))) return Number(v);
|
|
137
|
+
}
|
|
138
|
+
return fallback;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 给行补主键:id 缺省时依次取 keys 里第一个非空值(字符串化),已有非空 id 不动 */
|
|
142
|
+
export function withRowId(row: Rec, keys: string | readonly string[]): Rec {
|
|
143
|
+
if (typeof row.id === 'string' && row.id !== '') return row;
|
|
144
|
+
if (typeof row.id === 'number') return { ...row, id: String(row.id) };
|
|
145
|
+
for (const key of typeof keys === 'string' ? [keys] : keys) {
|
|
146
|
+
const v = row[key];
|
|
147
|
+
if (typeof v === 'string' && v.trim() !== '') return { ...row, id: v.trim() };
|
|
148
|
+
if (typeof v === 'number') return { ...row, id: String(v) };
|
|
149
|
+
}
|
|
150
|
+
return { ...row, id: '' };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function createPagedListAdapter(options: PagedListAdapterOptions = {}): PagedListAdapter {
|
|
154
|
+
const pageKey = options.pageKey ?? DEFAULT_PAGE_KEY;
|
|
155
|
+
const sizeKey = options.sizeKey ?? DEFAULT_SIZE_KEY;
|
|
156
|
+
const reserved = new Set<string>([pageKey, sizeKey, ...RESERVED_EXTRA]);
|
|
157
|
+
const upstreamPage = options.upstreamPage ?? 'page';
|
|
158
|
+
const upstreamSize = options.upstreamSize ?? 'pageSize';
|
|
159
|
+
const totalPaths = options.totalPaths ?? DEFAULT_TOTAL_PATHS;
|
|
160
|
+
const defaultSize = options.defaultPageSize ?? SOMA_PAGED_LIST_LIMITS.pageSizeDefault;
|
|
161
|
+
const maxSize = options.maxPageSize ?? SOMA_PAGED_LIST_LIMITS.pageSizeMax;
|
|
162
|
+
const filterMap = options.filterMap;
|
|
163
|
+
const filterName = (key: string): string | null => {
|
|
164
|
+
if (filterMap === undefined) return key;
|
|
165
|
+
if (Array.isArray(filterMap)) return filterMap.includes(key) ? key : null;
|
|
166
|
+
const mapped = (filterMap as Readonly<Record<string, string>>)[key];
|
|
167
|
+
return mapped === undefined ? null : mapped;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
hasPage(input) {
|
|
172
|
+
return entriesOf(input).some(([k, v]) => k === pageKey && positiveInt(v) !== null);
|
|
173
|
+
},
|
|
174
|
+
readQuery(input) {
|
|
175
|
+
const entries = entriesOf(input);
|
|
176
|
+
const pick = (key: string): number | null => {
|
|
177
|
+
const hit = entries.find(([k]) => k === key);
|
|
178
|
+
return hit === undefined ? null : positiveInt(hit[1]);
|
|
179
|
+
};
|
|
180
|
+
const page = pick(pageKey) ?? 1;
|
|
181
|
+
const pageSize = Math.min(pick(sizeKey) ?? defaultSize, maxSize);
|
|
182
|
+
const filters: Record<string, string> = {};
|
|
183
|
+
const upstream: Record<string, string | number> = { [upstreamPage]: page, [upstreamSize]: pageSize };
|
|
184
|
+
for (const [key, value] of entries) {
|
|
185
|
+
if (reserved.has(key) || value === undefined || value === null) continue;
|
|
186
|
+
const text = String(value).trim();
|
|
187
|
+
const target = filterName(key);
|
|
188
|
+
if (text === '' || target === null) continue;
|
|
189
|
+
filters[key] = text;
|
|
190
|
+
upstream[target] = text;
|
|
191
|
+
}
|
|
192
|
+
return { page, pageSize, filters, upstream };
|
|
193
|
+
},
|
|
194
|
+
toList(raw, mapRow) {
|
|
195
|
+
const unwrapped = unwrapResponseEnvelope(raw, options.envelope);
|
|
196
|
+
if (!unwrapped.ok) return { error: unwrapped.error };
|
|
197
|
+
let rows = rowsOf(unwrapped.data, options.rowsPath);
|
|
198
|
+
if (options.rowId !== undefined) rows = rows.map((r) => withRowId(r, options.rowId!));
|
|
199
|
+
if (mapRow !== undefined) rows = rows.map(mapRow);
|
|
200
|
+
return { rows, total: totalOf(unwrapped.data, totalPaths, rows.length) };
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ---------------------------------------------------------------- 游标分页
|
|
206
|
+
|
|
207
|
+
export type CursorLoader = (query: { cursor?: string; limit: number }) => Promise<unknown>;
|
|
208
|
+
|
|
209
|
+
export interface CursorPageOptions<T> {
|
|
210
|
+
load: CursorLoader;
|
|
211
|
+
/** 响应里行数组的键(缺省 rows) */
|
|
212
|
+
rowsKey?: string;
|
|
213
|
+
/** 响应里总数的键(缺省 total) */
|
|
214
|
+
totalKey?: string;
|
|
215
|
+
/** 响应里下一页游标的键(缺省 nextCursor) */
|
|
216
|
+
nextKey?: string;
|
|
217
|
+
parse?: (raw: unknown) => T;
|
|
218
|
+
/** 目标页(从 1 起)与页长 */
|
|
219
|
+
page: number;
|
|
220
|
+
pageSize: number;
|
|
221
|
+
/** 出错信息前缀(如「接口目录」) */
|
|
222
|
+
label: string;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface CursorPageResult<T> {
|
|
226
|
+
rows: T[];
|
|
227
|
+
total: number;
|
|
228
|
+
/** 最后一次取到的原始响应(调用方要读其它字段,如 snapshotVersion);页号超过末页时是末页那次的响应 */
|
|
229
|
+
raw: Rec | null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 沿游标走到第 page 页,返回该页行与总数;页号超过末页返回空行(total 仍真实)。
|
|
234
|
+
* 响应结构非法(行不是数组、总数不是非负整数、行数超过页长)抛错,不静默截断;游标重复也抛错(防死循环)。
|
|
235
|
+
*/
|
|
236
|
+
export async function cursorPageWindow<T = unknown>(options: CursorPageOptions<T>): Promise<CursorPageResult<T>> {
|
|
237
|
+
const { load, page, pageSize, label } = options;
|
|
238
|
+
const rowsKey = options.rowsKey ?? 'rows';
|
|
239
|
+
const totalKey = options.totalKey ?? 'total';
|
|
240
|
+
const nextKey = options.nextKey ?? 'nextCursor';
|
|
241
|
+
const parse = options.parse ?? ((raw: unknown) => raw as T);
|
|
242
|
+
let cursor: string | undefined;
|
|
243
|
+
const seen = new Set<string>();
|
|
244
|
+
let total = 0;
|
|
245
|
+
for (let current = 1; current <= page; current += 1) {
|
|
246
|
+
const response = await load({ ...(cursor !== undefined ? { cursor } : {}), limit: pageSize });
|
|
247
|
+
if (!isRec(response)) throw new Error(`${label}分页响应结构非法`);
|
|
248
|
+
const result: Rec = response;
|
|
249
|
+
const raw = result[rowsKey];
|
|
250
|
+
total = Number(result[totalKey]);
|
|
251
|
+
if (!Array.isArray(raw) || !Number.isSafeInteger(total) || total < 0 || raw.length > pageSize) throw new Error(`${label}分页响应结构非法`);
|
|
252
|
+
if (current === page) return { rows: raw.map(parse), total, raw: result };
|
|
253
|
+
const next = typeof result[nextKey] === 'string' ? (result[nextKey] as string).trim() : '';
|
|
254
|
+
if (next === '' || next === cursor || seen.has(next)) return { rows: [], total, raw: result };
|
|
255
|
+
seen.add(next);
|
|
256
|
+
cursor = next;
|
|
257
|
+
}
|
|
258
|
+
return { rows: [], total, raw: null };
|
|
259
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Server-side compile-port entry (subpath `soma-core/server/compile-port`, 06e §2.10).
|
|
3
3
|
*
|
|
4
|
-
* core 不持有编译器,也不回调方言编译器:方言(soma-ui / soma-
|
|
4
|
+
* core 不持有编译器,也不回调方言编译器:方言(soma-ui / soma-kit)在运行期把自己的页面编译成标准格式后按 URL 注入 core。
|
|
5
5
|
* 这里只剩编译用途闭集与页面生成资产来源的类型。
|
|
6
6
|
*
|
|
7
7
|
* 刻意不 `import 'server-only'`(与 `server/page-declaration.ts` 同一理由):宿主的 `soma.app.ts`
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import yaml from 'js-yaml';
|
|
3
3
|
import { resolveHostAppConfigPath } from './host-config-paths';
|
|
4
|
+
import { currentHostDir } from './host-dir';
|
|
4
5
|
|
|
5
6
|
type AppConfigRecord = Record<string, unknown>;
|
|
6
7
|
|
|
@@ -70,7 +71,7 @@ export function composeAppConfigYaml(
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
/** Browser metadata uses the Host App's displayName, never a source-package brand. */
|
|
73
|
-
export function readHostAppDisplayName(hostDir =
|
|
74
|
+
export function readHostAppDisplayName(hostDir = currentHostDir()): string {
|
|
74
75
|
if (!hostDir) {
|
|
75
76
|
return '';
|
|
76
77
|
}
|
|
@@ -53,6 +53,7 @@ export function getJitiFactory(anchorDir?: string): JitiFactory {
|
|
|
53
53
|
}
|
|
54
54
|
import type { SomaHostAssembly } from '../../shared/types/host-assembly.types';
|
|
55
55
|
import { getSomaHostSourceRoot } from './source-packages';
|
|
56
|
+
import { getSomaSourceRootAliases } from './source-aliases';
|
|
56
57
|
|
|
57
58
|
/** Host 装配文件相对应用根的位置 */
|
|
58
59
|
export const SOMA_HOST_ASSEMBLY_FILE = 'soma.app.ts';
|
|
@@ -70,16 +71,22 @@ const HOLDER_KEY = Symbol.for('soma.core.hostAssemblyHolder');
|
|
|
70
71
|
interface Holder {
|
|
71
72
|
readonly cache: Map<string, LoadedApp | null>;
|
|
72
73
|
readonly ready: Set<string>;
|
|
74
|
+
/** 当前代可共享的 server() Promise */
|
|
73
75
|
readonly pendingServer: Map<string, Promise<void>>;
|
|
76
|
+
/** 同一应用根的跨代 server() 顺序屏障,防止旧代晚完成覆盖新代副作用 */
|
|
77
|
+
readonly serverBarrier: Map<string, Promise<void>>;
|
|
78
|
+
readonly generations: Map<string, number>;
|
|
74
79
|
readonly startups: Map<string, Promise<void>>;
|
|
75
80
|
}
|
|
76
81
|
const holder: Holder = ((globalThis as Record<symbol, unknown>)[HOLDER_KEY] ??= {
|
|
77
82
|
cache: new Map<string, LoadedApp | null>(),
|
|
78
83
|
ready: new Set<string>(),
|
|
79
84
|
pendingServer: new Map<string, Promise<void>>(),
|
|
85
|
+
serverBarrier: new Map<string, Promise<void>>(),
|
|
86
|
+
generations: new Map<string, number>(),
|
|
80
87
|
startups: new Map<string, Promise<void>>(),
|
|
81
88
|
}) as Holder;
|
|
82
|
-
const { cache, ready, pendingServer, startups } = holder;
|
|
89
|
+
const { cache, ready, pendingServer, serverBarrier, generations, startups } = holder;
|
|
83
90
|
|
|
84
91
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
85
92
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
@@ -281,8 +288,11 @@ function loadAppSync(appRoot: string): LoadedApp | null {
|
|
|
281
288
|
const coreDir = resolveSelfCoreDir();
|
|
282
289
|
const loader = createJiti(path.resolve(key, 'package.json'), {
|
|
283
290
|
interopDefault: false,
|
|
291
|
+
moduleCache: false,
|
|
284
292
|
requireCache: false,
|
|
285
293
|
alias: {
|
|
294
|
+
// 源码根别名(soma.sourceRoots):与服务加载器同一实现;固定别名在后,不可被覆盖
|
|
295
|
+
...getSomaSourceRootAliases(key),
|
|
286
296
|
'@soma-host': hostRoot,
|
|
287
297
|
'@core': coreDir,
|
|
288
298
|
...(serverOnlyEmpty !== undefined ? { 'server-only': serverOnlyEmpty } : {}),
|
|
@@ -304,13 +314,38 @@ export async function loadHostAssembly(appRoot: string): Promise<SomaHostAssembl
|
|
|
304
314
|
const key = path.resolve(appRoot);
|
|
305
315
|
const app = loadAppSync(key);
|
|
306
316
|
if (app === null) return null;
|
|
317
|
+
const generation = generations.get(key) ?? 0;
|
|
307
318
|
if (app.server !== undefined && !ready.has(key)) {
|
|
308
319
|
let pending = pendingServer.get(key);
|
|
309
320
|
if (pending === undefined) {
|
|
310
|
-
|
|
321
|
+
const previous = serverBarrier.get(key);
|
|
322
|
+
pending = (async () => {
|
|
323
|
+
if (previous !== undefined) await previous.catch(() => {});
|
|
324
|
+
// 等旧代的服务端副作用结算期间又发生了失效:不启动已经过期的装配。
|
|
325
|
+
if ((generations.get(key) ?? 0) !== generation) return;
|
|
326
|
+
await app.server!();
|
|
327
|
+
if ((generations.get(key) ?? 0) === generation) ready.add(key);
|
|
328
|
+
})();
|
|
311
329
|
pendingServer.set(key, pending);
|
|
330
|
+
serverBarrier.set(key, pending);
|
|
331
|
+
const clearBarrier = () => {
|
|
332
|
+
if (serverBarrier.get(key) === pending) serverBarrier.delete(key);
|
|
333
|
+
};
|
|
334
|
+
void pending.then(clearBarrier, () => {
|
|
335
|
+
if ((generations.get(key) ?? 0) === generation && pendingServer.get(key) === pending) {
|
|
336
|
+
pendingServer.delete(key);
|
|
337
|
+
}
|
|
338
|
+
clearBarrier();
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
await pending;
|
|
343
|
+
} catch (error) {
|
|
344
|
+
if ((generations.get(key) ?? 0) !== generation) return loadHostAssembly(key);
|
|
345
|
+
throw error;
|
|
312
346
|
}
|
|
313
|
-
|
|
347
|
+
// 同步 load() 已读出的装配若在等待期间被失效,旧调用方也只能收到当前代结果。
|
|
348
|
+
if ((generations.get(key) ?? 0) !== generation) return loadHostAssembly(key);
|
|
314
349
|
}
|
|
315
350
|
return app.host;
|
|
316
351
|
}
|
|
@@ -345,8 +380,20 @@ export function isHostAssemblyReady(appRoot: string): boolean {
|
|
|
345
380
|
|
|
346
381
|
/** 只给测试用:清空装配缓存 */
|
|
347
382
|
export function resetHostAssemblyCache(): void {
|
|
383
|
+
const keys = new Set([...cache.keys(), ...pendingServer.keys(), ...serverBarrier.keys()]);
|
|
384
|
+
for (const key of keys) generations.set(key, (generations.get(key) ?? 0) + 1);
|
|
348
385
|
cache.clear();
|
|
349
386
|
ready.clear();
|
|
350
387
|
pendingServer.clear();
|
|
388
|
+
// 保留未结算的 serverBarrier:后续装配必须等旧代副作用结束。
|
|
351
389
|
startups.clear();
|
|
352
390
|
}
|
|
391
|
+
|
|
392
|
+
/** 清除单个应用的装配缓存(热更新重装配用);启动钩子记忆仍按进程保留一次 */
|
|
393
|
+
export function invalidateHostAssembly(appRoot: string): void {
|
|
394
|
+
const key = path.resolve(appRoot);
|
|
395
|
+
generations.set(key, (generations.get(key) ?? 0) + 1);
|
|
396
|
+
cache.delete(key);
|
|
397
|
+
ready.delete(key);
|
|
398
|
+
pendingServer.delete(key);
|
|
399
|
+
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// 装配的服务端面装配失败属于「拒绝启动」,仍原样上抛。
|
|
7
7
|
import { loadHostAssembly } from './host-assembly';
|
|
8
8
|
import { readAccessIdentity } from '../runtime-space/access-gate';
|
|
9
|
+
import { currentHostDir } from './host-dir';
|
|
9
10
|
|
|
10
11
|
export interface SomaHostCaller {
|
|
11
12
|
readonly signedIn: boolean;
|
|
@@ -39,7 +40,7 @@ export function readHostCaller(raw: unknown): SomaHostCaller {
|
|
|
39
40
|
/** 解析这次请求的调用方;装配的 `identity` 抛错按匿名处理 */
|
|
40
41
|
export async function resolveHostCaller(
|
|
41
42
|
request: Request | undefined,
|
|
42
|
-
hostDir: string | undefined =
|
|
43
|
+
hostDir: string | undefined = currentHostDir(),
|
|
43
44
|
): Promise<SomaHostCaller> {
|
|
44
45
|
if (request === undefined || hostDir === undefined || hostDir.trim() === '') return ANONYMOUS;
|
|
45
46
|
const assembly = await loadHostAssembly(hostDir);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// 宿主根的唯一读取点:`SOMA_HOST_DIR`(缺省不设)。
|
|
2
|
+
// 无依赖的小模块,供底层模块直接引用而不引入循环;`runtime-space/bindings.ts` 从这里再导出。
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
/** 宿主根(SOMA_HOST_DIR,未设或空白 → undefined;否则 path.resolve 后的绝对路径) */
|
|
6
|
+
export function currentHostDir(): string | undefined {
|
|
7
|
+
const configured = process.env.SOMA_HOST_DIR;
|
|
8
|
+
return configured !== undefined && configured.trim() !== '' ? path.resolve(configured) : undefined;
|
|
9
|
+
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// 同名头以装配返回值为准。core 不认识具体头,也不替业务挑选类型(图片除外之类的取舍在装配的回调里写)。
|
|
6
6
|
import { loadHostAssembly } from './host-assembly';
|
|
7
7
|
import type { SomaHostResponseHeadersContext } from '../../shared/types/host-assembly.types';
|
|
8
|
+
import { currentHostDir } from './host-dir';
|
|
8
9
|
|
|
9
10
|
/** 按路径分类:`/api/*` → api;`/_next/*`、`/soma-runtime/*`、`/favicon.ico`、带文件扩展名的路径 → asset;其余 → page */
|
|
10
11
|
export function classifyHostResponseKind(pathname: string): SomaHostResponseHeadersContext['kind'] {
|
|
@@ -21,7 +22,7 @@ export function classifyHostResponseKind(pathname: string): SomaHostResponseHead
|
|
|
21
22
|
*/
|
|
22
23
|
export async function resolveHostResponseHeaders(
|
|
23
24
|
request: { readonly method: string; readonly pathname: string },
|
|
24
|
-
hostDir: string | undefined =
|
|
25
|
+
hostDir: string | undefined = currentHostDir(),
|
|
25
26
|
): Promise<Readonly<Record<string, string>> | undefined> {
|
|
26
27
|
if (hostDir === undefined || hostDir.trim() === '') return undefined;
|
|
27
28
|
if (typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge') return undefined;
|
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { loadHostAssembly } from './host-assembly';
|
|
2
2
|
import {
|
|
3
3
|
resolveHostRequestHook,
|
|
4
|
+
type ResolveHostRequestHookOptions,
|
|
4
5
|
type SomaHostRequestContext,
|
|
5
6
|
type SomaRequestHook,
|
|
6
7
|
} from './resolve-host-request-hook';
|
|
8
|
+
import { currentHostDir } from './host-dir';
|
|
7
9
|
|
|
8
|
-
export type SomaRequestHookResolver = (
|
|
10
|
+
export type SomaRequestHookResolver = (
|
|
11
|
+
options?: ResolveHostRequestHookOptions,
|
|
12
|
+
) => Promise<SomaRequestHook | null>;
|
|
13
|
+
|
|
14
|
+
export interface InvokeHostRequestHookOptions {
|
|
15
|
+
/**
|
|
16
|
+
* 显式传入的主机根目录。
|
|
17
|
+
* 仅当调用方未传时才回退环境变量 process.env.SOMA_HOST_DIR(兼容旧对外导出调用方)。
|
|
18
|
+
*/
|
|
19
|
+
readonly hostDir?: string;
|
|
20
|
+
}
|
|
9
21
|
|
|
10
22
|
function requestPathname(path: string): string {
|
|
11
23
|
try {
|
|
@@ -27,16 +39,28 @@ export function isSomaInternalRequestPath(path: string): boolean {
|
|
|
27
39
|
export async function invokeHostRequestHook(
|
|
28
40
|
request: Request,
|
|
29
41
|
context: SomaHostRequestContext,
|
|
30
|
-
|
|
42
|
+
resolveHookOrOptions?: SomaRequestHookResolver | InvokeHostRequestHookOptions,
|
|
43
|
+
maybeOptions?: InvokeHostRequestHookOptions,
|
|
31
44
|
): Promise<Response | undefined> {
|
|
45
|
+
const resolveHook: SomaRequestHookResolver =
|
|
46
|
+
typeof resolveHookOrOptions === 'function'
|
|
47
|
+
? resolveHookOrOptions
|
|
48
|
+
: resolveHostRequestHook;
|
|
49
|
+
|
|
50
|
+
const options: InvokeHostRequestHookOptions | undefined =
|
|
51
|
+
typeof resolveHookOrOptions === 'object' && resolveHookOrOptions !== null
|
|
52
|
+
? resolveHookOrOptions
|
|
53
|
+
: maybeOptions;
|
|
54
|
+
|
|
32
55
|
// 有 soma.app.ts 时先等 Host 装配的服务端面完成(装配 S1–S8;失败即拒绝启动,原样上抛):
|
|
33
56
|
// /soma 内部路径虽不进 Host 钩子,其后的取页 / 编译点同样依赖装配已就绪,所以这一步在分流之前。
|
|
34
|
-
|
|
57
|
+
// 显式 hostDir 优先;仅当调用方未传时才回退环境变量 process.env.SOMA_HOST_DIR(兼容对外导出)
|
|
58
|
+
const hostDir = options?.hostDir ?? currentHostDir();
|
|
35
59
|
if (hostDir !== undefined && hostDir.trim() !== '') await loadHostAssembly(hostDir);
|
|
36
60
|
if (isSomaInternalRequestPath(context.path)) {
|
|
37
61
|
return undefined;
|
|
38
62
|
}
|
|
39
63
|
|
|
40
|
-
const hook = await resolveHook();
|
|
64
|
+
const hook = await resolveHook({ hostDir });
|
|
41
65
|
return hook?.(request, context);
|
|
42
66
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// (见 published-page-artifact-cache.ts)。
|
|
7
7
|
// 2. 产物随编译器漂移。存档的编译身份与当前不等是**第一级**:只记提示(一次),不重编——何时用新编译器重编,由维护者显式重发布决定(D63)。
|
|
8
8
|
|
|
9
|
+
import { SomaError } from '../../shared/soma-error';
|
|
9
10
|
import { publishedPageSlugFromPath } from '../../shared/published-page-route';
|
|
10
11
|
import { getPageRegistryRepository } from '../registry/page-registry-repository';
|
|
11
12
|
import { currentComponentSupply } from '../registry/component-supply';
|
|
@@ -21,7 +22,7 @@ export const registryPageNameFromPath = publishedPageSlugFromPath;
|
|
|
21
22
|
* 整页错误(页面主题整体解析失败等,端口给的整页失败):不存档、不缓存,Host 路由据此回受控文案 + 诊断码,
|
|
22
23
|
* 而不是静默替换成平台默认主题或把整页混成 404。`message` 是对访客的受控文案(不含内部细节),细节在 `diagnostics`。
|
|
23
24
|
*/
|
|
24
|
-
export class SomaPublishedPageError extends
|
|
25
|
+
export class SomaPublishedPageError extends SomaError {
|
|
25
26
|
readonly code: string;
|
|
26
27
|
readonly diagnostics: readonly SomaGenerationErrorShape[];
|
|
27
28
|
|
|
@@ -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
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
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
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
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 (
|
|
67
|
-
watchedRoots.add(root);
|
|
67
|
+
if (cache.isWatching(root)) return;
|
|
68
68
|
cache.watchAndInvalidate(root);
|
|
69
69
|
}
|
|
70
70
|
|