soma-core 2.0.1 → 2.1.0
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 +2 -2
- package/bin/eject.cjs +5 -5
- package/bin/soma-runtime.cjs +5 -123
- package/bin/soma.cjs +2 -2
- package/bin/soma.ts +8 -20
- package/core/build/compiler/page-declaration-compiler.ts +5 -6
- package/core/build/lsp/soma-declaration.cjs +0 -6
- package/core/client/container/SomaContainerAdapter.ts +1 -1
- package/core/client/data/region-scope-views.ts +1 -1
- package/core/client/engine/event-engine.ts +1 -6
- package/core/client/engine/soma-bootstrap.ts +5 -13
- package/core/client/host-bridge/soma-bootstrap-from-app.ts +2 -2
- package/core/client/infra/i18n/II18n.ts +5 -6
- package/core/client/infra/i18n/locale-catalog.ts +8 -5
- package/core/client/infra/identity/identity-port.ts +1 -1
- package/core/client/keyboard/index.ts +0 -1
- package/core/client/keyboard/wiring.ts +0 -3
- package/core/client/parser/ui-config-parser.ts +6 -9
- package/core/client/registry/declaration-renderer.ts +1 -1
- package/core/client/runtime/SomaRuntimeServices.ts +1 -1
- package/core/inspect/check-runtime.ts +2 -3
- package/core/server/host/content-security-policy.ts +2 -11
- package/core/server/host/host-assembly-entry.ts +10 -2
- package/core/server/host/host-assembly.ts +28 -10
- package/core/server/host/host-caller.ts +52 -0
- package/core/server/host/host-response-headers.ts +49 -0
- package/core/server/host/locale-yaml.ts +7 -5
- package/core/server/host/published-page-assets.ts +1 -1
- package/core/server/host/resolve-host-request-hook.ts +2 -11
- package/core/server/host/resolve-host-tree-context.ts +7 -41
- package/core/server/host/server-page-assets.ts +0 -22
- package/core/server/host/source-packages.cjs +3 -4
- package/core/server/host/virtual-pages.ts +12 -70
- package/core/server/index.ts +1 -3
- package/core/server/node.ts +0 -1
- package/core/server/page-assets.ts +2 -20
- package/core/server/page-declaration.ts +1 -1
- package/core/server/registry/api-registry-store.ts +5 -25
- package/core/server/registry/asset-store.ts +5 -23
- package/core/server/registry/page-registry-repository.ts +9 -13
- package/core/server/registry/page-registry-store.ts +44 -82
- package/core/server/registry/publish-asset-pin.ts +3 -4
- package/core/server/registry/registry-subjects.ts +6 -6
- package/core/server/registry/rollback-plan.ts +5 -8
- package/core/server/registry/source/version-store.ts +8 -30
- package/core/server/registry/standard-product.ts +1 -1
- package/core/server/runtime-space/bindings.ts +1 -4
- package/core/server/runtime-space/gc.ts +2 -3
- package/core/server/runtime-space/inject.ts +4 -5
- package/core/server/runtime-space/resolve.ts +13 -27
- package/core/server/runtime-space/root.cjs +9 -100
- package/core/server/runtime-space/root.ts +3 -23
- package/core/server/runtime-space/types.ts +0 -11
- package/core/shared/artifact-compatibility.ts +6 -16
- package/core/shared/component-tree-artifact.ts +4 -8
- package/core/shared/core-diagnostic-codes.ts +1 -3
- package/core/shared/pipeline-error-codes-view.ts +1 -2
- package/core/shared/published-page-route.ts +1 -6
- package/core/shared/registry-generated/diagnostic-registry-index.ts +0 -2
- package/core/shared/registry-generated/diagnostic-registry.ts +0 -14
- package/core/shared/source-file-suffix.ts +8 -0
- package/core/shared/types/component-tree-artifact.types.ts +2 -15
- package/core/shared/types/host-assembly.types.ts +31 -2
- package/core/shared/types/locale.types.ts +3 -3
- package/core/shared/types/region-scope.types.ts +1 -1
- package/core/shared/types/ui-config.types.ts +2 -7
- package/core/shared/types/write.types.ts +1 -1
- package/dist/browser/soma-runtime.js +20 -36
- package/dist/cli/soma.js +8 -21
- package/dist/core/index.cjs +20 -38
- package/dist/lsp/component-tree.cjs +7 -14
- package/dist/types/client/container/SomaContainerAdapter.d.ts +1 -1
- package/dist/types/client/engine/event-engine.d.ts +0 -2
- package/dist/types/client/engine/soma-bootstrap.d.ts +2 -3
- package/dist/types/client/host-bridge/soma-bootstrap-from-app.d.ts +2 -2
- package/dist/types/client/infra/i18n/II18n.d.ts +5 -6
- package/dist/types/client/infra/i18n/locale-catalog.d.ts +3 -3
- package/dist/types/client/infra/identity/identity-port.d.ts +1 -1
- package/dist/types/client/keyboard/index.d.ts +1 -1
- package/dist/types/client/keyboard/wiring.d.ts +0 -2
- package/dist/types/client/registry/declaration-renderer.d.ts +1 -1
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +1 -1
- package/dist/types/server/host/content-security-policy.d.ts +0 -5
- package/dist/types/server/host/host-assembly-entry.d.ts +5 -2
- package/dist/types/server/host/host-assembly.d.ts +9 -4
- package/dist/types/server/host/host-caller.d.ts +10 -0
- package/dist/types/server/host/host-response-headers.d.ts +16 -0
- package/dist/types/server/host/published-page-assets.d.ts +1 -1
- package/dist/types/server/host/resolve-host-request-hook.d.ts +0 -4
- package/dist/types/server/host/resolve-host-tree-context.d.ts +1 -1
- package/dist/types/server/host/server-page-assets.d.ts +0 -2
- package/dist/types/server/host/virtual-pages.d.ts +4 -14
- package/dist/types/server/index.d.ts +1 -2
- package/dist/types/server/node.d.ts +1 -1
- package/dist/types/server/page-assets.d.ts +1 -4
- package/dist/types/server/page-declaration.d.ts +1 -1
- package/dist/types/server/registry/api-registry-store.d.ts +1 -6
- package/dist/types/server/registry/asset-store.d.ts +2 -11
- package/dist/types/server/registry/page-registry-repository.d.ts +4 -4
- package/dist/types/server/registry/page-registry-store.d.ts +22 -30
- package/dist/types/server/registry/registry-subjects.d.ts +2 -2
- package/dist/types/server/registry/rollback-plan.d.ts +0 -3
- package/dist/types/server/registry/source/version-store.d.ts +1 -9
- package/dist/types/server/registry/standard-product.d.ts +1 -1
- package/dist/types/server/runtime-space/resolve.d.ts +0 -3
- package/dist/types/server/runtime-space/root.d.cts +6 -29
- package/dist/types/server/runtime-space/root.d.ts +2 -7
- package/dist/types/server/runtime-space/types.d.ts +0 -9
- package/dist/types/shared/artifact-compatibility.d.ts +1 -3
- package/dist/types/shared/core-diagnostic-codes.d.ts +1 -1
- package/dist/types/shared/published-page-route.d.ts +1 -3
- package/dist/types/shared/registry-generated/diagnostic-registry.d.ts +0 -12
- package/dist/types/shared/types/component-tree-artifact.types.d.ts +2 -15
- package/dist/types/shared/types/host-assembly.types.d.ts +27 -1
- package/dist/types/shared/types/locale.types.d.ts +1 -1
- package/dist/types/shared/types/region-scope.types.d.ts +1 -1
- package/dist/types/shared/types/ui-config.types.d.ts +2 -7
- package/dist/types/shared/types/write.types.d.ts +1 -1
- package/global.d.ts +0 -9
- package/host-next/app/SomaPageClient.tsx +359 -0
- package/host-next/app/[...path]/page.tsx +22 -0
- package/host-next/app/api/[...path]/route.ts +98 -0
- package/host-next/app/api/soma-registry/[...path]/route.ts +79 -0
- package/host-next/app/components/SomaHostPage.tsx +128 -0
- package/host-next/app/components/SomaHydrationShell.tsx +20 -0
- package/host-next/app/components/SomaPageClientBoundary.tsx +13 -0
- package/host-next/app/components/SomaPageForbidden.tsx +19 -0
- package/host-next/app/favicon.ico/route.ts +13 -0
- package/host-next/app/globals.css +177 -0
- package/host-next/app/layout.tsx +27 -0
- package/host-next/app/page-access.ts +47 -0
- package/host-next/app/page.tsx +9 -0
- package/host-next/app/router-bridge.ts +42 -0
- package/host-next/app/soma/[...path]/route.ts +9 -0
- package/host-next/app/soma/_shared/host-prepare.ts +33 -0
- package/host-next/app/soma/_shared/page-http.ts +86 -0
- package/host-next/app/soma/manifest/route.ts +98 -0
- package/host-next/app/soma/page/route.ts +36 -0
- package/host-next/app/soma/route.ts +9 -0
- package/host-next/app/soma-runtime/[...file]/route.ts +21 -0
- package/host-next/app/soma-runtime/_shared/static-file.ts +89 -0
- package/host-next/bin/host-next.cjs +59 -0
- package/host-next/instrumentation.ts +13 -0
- package/host-next/next-env.d.ts +7 -0
- package/host-next/next.config.js +382 -0
- package/host-next/proxy.ts +43 -0
- package/host-next/src/shell-routes.cjs +51 -0
- package/host-next/src/workspace.ts +543 -0
- package/package.json +29 -4
- package/tsconfig.json +2 -1
- package/core/server/host/page-response-mode.cjs +0 -96
- package/core/server/host/page-response-request.ts +0 -33
- package/core/server/host/request-origin.ts +0 -18
- package/core/server/host/soma-request-hook-host.stub.ts +0 -4
- package/core/server/runtime-space/migrate-sources.ts +0 -124
- package/core/shared/source-compat.ts +0 -9
- package/dist/types/server/host/page-response-request.d.ts +0 -8
- package/dist/types/server/host/request-origin.d.ts +0 -1
- package/dist/types/server/host/soma-request-hook-host.stub.d.ts +0 -2
- package/dist/types/server/runtime-space/migrate-sources.d.ts +0 -19
- /package/dist/types/shared/{source-compat.d.ts → source-file-suffix.d.ts} +0 -0
|
@@ -92,8 +92,7 @@ function subsetOf(expected: unknown, actual: unknown): boolean {
|
|
|
92
92
|
|
|
93
93
|
// ── 隔离环境 ─────────────────────────────────────────────────────────────────
|
|
94
94
|
|
|
95
|
-
const ENV_KEYS = ['SOMA_HOST_DIR', 'SOMA_RUNTIME_DIR', 'SOMA_RUNTIME_GC'
|
|
96
|
-
'SOMA_BIZ_REGISTRY_DIR', 'SOMA_REGISTRY_ASSET_DIR', 'SOMA_API_REGISTRY_DIR'] as const;
|
|
95
|
+
const ENV_KEYS = ['SOMA_HOST_DIR', 'SOMA_RUNTIME_DIR', 'SOMA_RUNTIME_GC'] as const;
|
|
97
96
|
|
|
98
97
|
interface HostSetup {
|
|
99
98
|
readonly identity?: unknown;
|
|
@@ -215,7 +214,7 @@ function buildPage(root: string, spec: unknown, variant?: unknown): SomaStandard
|
|
|
215
214
|
if (typeof source.eventYaml === 'string') mutable.eventYaml = source.eventYaml;
|
|
216
215
|
if (typeof source.styleYaml === 'string') mutable.styleYaml = source.styleYaml;
|
|
217
216
|
if (variant === 'changed-heading' && typeof mutable.locale === 'string') {
|
|
218
|
-
mutable.locale = (mutable.locale as string).replace(/^(heading:\s*)(.*)$/m, '$1$2(变)');
|
|
217
|
+
mutable.locale = (mutable.locale as string).replace(/^(\s*heading:\s*)(.*)$/m, '$1$2(变)');
|
|
219
218
|
}
|
|
220
219
|
return mutable as unknown as SomaStandardPage;
|
|
221
220
|
}
|
|
@@ -10,9 +10,7 @@
|
|
|
10
10
|
// 所以 `script-src` / `style-src` / `font-src` 一律 `'self'` 即可;
|
|
11
11
|
// 4. **样式按载体拆开**——组件库聚合注入的 `<style>` 必须带 nonce;
|
|
12
12
|
// 目标态下主题令牌、组件样式都是编译期烘焙的样式片(`compiledStyles`,经 `<style>` 注入),运行期不再写元素的
|
|
13
|
-
// CSSOM(显隐走 class/属性钩子)
|
|
14
|
-
// 仍写内联 `style=""` 属性的存量 Host 经显式开关 `allowStyleAttrInline` 回退(兼容通道,只对属性样式放行,
|
|
15
|
-
// 不扩大 `<style>` 标签的执行面)。
|
|
13
|
+
// CSSOM(显隐走 class/属性钩子),所以**不放行** `style-src-attr 'unsafe-inline'`。
|
|
16
14
|
//
|
|
17
15
|
// 默认关(`SOMA_CSP` 未设 = off):CSP 一旦收紧就会打断存量宿主的第三方接入,
|
|
18
16
|
// 必须让宿主显式开启并先跑一轮 report-only,故不做「悄悄生效」的默认值。
|
|
@@ -47,11 +45,6 @@ export interface SomaCspOptions {
|
|
|
47
45
|
frameSrc?: string[];
|
|
48
46
|
/** 违规上报端点 */
|
|
49
47
|
reportUri?: string;
|
|
50
|
-
/**
|
|
51
|
-
* 兼容开关(缺省 false):放行 `style-src-attr 'unsafe-inline'`。目标态页面的样式全部编译期烘焙,不需要它;
|
|
52
|
-
* 存量 Host 仍有运行期写 `style` 属性的路径时显式开启,并把它视为待清理的技术债。
|
|
53
|
-
*/
|
|
54
|
-
allowStyleAttrInline?: boolean;
|
|
55
48
|
}
|
|
56
49
|
|
|
57
50
|
/** 生成一次性 nonce(Web Crypto,Edge 与 Node 18+ 都有,不引 node:crypto 以便跑在 Edge 运行时) */
|
|
@@ -138,9 +131,7 @@ export function buildSomaCspDirectives(options: SomaCspOptions): Array<[string,
|
|
|
138
131
|
['form-action', ["'self'"]],
|
|
139
132
|
['script-src', scriptSrc],
|
|
140
133
|
['style-src', styleSrc],
|
|
141
|
-
//
|
|
142
|
-
// 存量 Host 经显式开关 allowStyleAttrInline 回退;放行范围只有属性样式,<style> 标签仍只能 self 或 nonce。
|
|
143
|
-
...(options.allowStyleAttrInline === true ? [['style-src-attr', ["'unsafe-inline'"]] as [string, string[]]] : []),
|
|
134
|
+
// 不放行属性样式(M29):编译期烘焙的样式片走 <style>(self / nonce),运行期不写 style 属性。
|
|
144
135
|
// data: 供内联 SVG/占位图;blob: 供前端生成的导出图;https: 是业务图床的兜底,
|
|
145
136
|
// 有确定图床域时应经 SOMA_CSP_IMG_SRC 收窄并删掉 https:
|
|
146
137
|
['img-src', uniq(["'self'", 'data:', 'blob:', 'https:', ...(options.imgSrc ?? [])])],
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// `soma-core/server/host-assembly`:宿主壳与工具对 Host 装配桥的公开口(R-24)。
|
|
2
2
|
//
|
|
3
|
-
// 宿主壳(
|
|
3
|
+
// 宿主壳(host-next)经这里加载装配、登记自己的路由、解析登录页路径;不再直接改装配对象,也不深导入 core 内部文件。
|
|
4
4
|
export {
|
|
5
5
|
loadHostAssembly,
|
|
6
|
+
runHostStartup,
|
|
6
7
|
isHostAssemblyReady,
|
|
7
8
|
assertReservedRoutesShape,
|
|
8
9
|
registerShellRoutes,
|
|
@@ -13,4 +14,11 @@ export {
|
|
|
13
14
|
SOMA_DEFAULT_LOGIN_PATH,
|
|
14
15
|
SOMA_HOST_ASSEMBLY_FILE,
|
|
15
16
|
} from './host-assembly';
|
|
16
|
-
export
|
|
17
|
+
export { resolveHostCaller, readHostCaller } from './host-caller';
|
|
18
|
+
export type { SomaHostCaller } from './host-caller';
|
|
19
|
+
export {
|
|
20
|
+
withHostResponseHeaders,
|
|
21
|
+
resolveHostResponseHeaders,
|
|
22
|
+
classifyHostResponseKind,
|
|
23
|
+
} from './host-response-headers';
|
|
24
|
+
export type { SomaHostAssembly, SomaHostResponseHeadersContext } from '../../shared/types/host-assembly.types';
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
// 是什么:Host 工程把「引导选项、服务、身份、虚拟页、请求钩子、树上下文、编译器登记」收成一个 `soma.app.ts`
|
|
4
4
|
// 默认导出(soma-ui `defineSomaApp(...).host` 实现之);core 在这里按 `SomaHostAssembly` 形状读取,不 import soma-ui。
|
|
5
5
|
//
|
|
6
|
-
// 为什么用 jiti
|
|
7
|
-
//
|
|
6
|
+
// 为什么用 jiti 加载:Host 源码是 TS,服务端在纯 Node 里按需编译加载,`@soma-host` 别名指向 Host 源码根。
|
|
7
|
+
// 无 `soma.app.ts` 返回 null(没有装配:无身份、无虚拟页、无请求钩子、无树上下文,不再有约定钩子文件的回退)。
|
|
8
8
|
//
|
|
9
9
|
// 不变式:形状不对当场抛(半套装配比没有更难查);同一应用根只加载一次(进程内缓存)。
|
|
10
10
|
|
|
@@ -33,13 +33,15 @@ interface Holder {
|
|
|
33
33
|
readonly cache: Map<string, LoadedApp | null>;
|
|
34
34
|
readonly ready: Set<string>;
|
|
35
35
|
readonly pendingServer: Map<string, Promise<void>>;
|
|
36
|
+
readonly startups: Map<string, Promise<void>>;
|
|
36
37
|
}
|
|
37
38
|
const holder: Holder = ((globalThis as Record<symbol, unknown>)[HOLDER_KEY] ??= {
|
|
38
39
|
cache: new Map<string, LoadedApp | null>(),
|
|
39
40
|
ready: new Set<string>(),
|
|
40
41
|
pendingServer: new Map<string, Promise<void>>(),
|
|
42
|
+
startups: new Map<string, Promise<void>>(),
|
|
41
43
|
}) as Holder;
|
|
42
|
-
const { cache, ready, pendingServer } = holder;
|
|
44
|
+
const { cache, ready, pendingServer, startups } = holder;
|
|
43
45
|
|
|
44
46
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
45
47
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
@@ -62,7 +64,7 @@ function assertHostShape(value: unknown, file: string): SomaHostAssembly {
|
|
|
62
64
|
throw new Error(`${file} 的 Host 装配缺少方法 ${method}()`);
|
|
63
65
|
}
|
|
64
66
|
}
|
|
65
|
-
for (const optional of ['identity', 'requestHook', 'prewarm', 'defaultAccess', 'pageGrants'] as const) {
|
|
67
|
+
for (const optional of ['identity', 'requestHook', 'prewarm', 'startup', 'responseHeaders', 'defaultAccess', 'pageGrants'] as const) {
|
|
66
68
|
if (value[optional] !== undefined && typeof value[optional] !== 'function') {
|
|
67
69
|
throw new Error(`${file} 的 Host 装配的 ${optional} 必须是函数`);
|
|
68
70
|
}
|
|
@@ -146,17 +148,13 @@ export function assertLoginShape(login: unknown, file = SOMA_HOST_ASSEMBLY_FILE)
|
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
/**
|
|
149
|
-
*
|
|
150
|
-
* 环境变量不合形状时忽略(回缺省),装配里不合形状的在装配读取时已抛。
|
|
151
|
+
* 登录页路径。唯一来源:装配 `login.path`,缺省 `/login`。装配里不合形状的在装配读取时已抛。
|
|
151
152
|
*/
|
|
152
153
|
export function resolveHostLoginPath(
|
|
153
154
|
assembly?: { readonly login?: { readonly path?: string } } | null,
|
|
154
|
-
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
155
155
|
): string {
|
|
156
156
|
const fromAssembly = assembly?.login?.path;
|
|
157
157
|
if (isSafeLoginPath(fromAssembly)) return fromAssembly;
|
|
158
|
-
const alias = env.SOMA_LOGIN_PATH?.trim();
|
|
159
|
-
if (alias && isSafeLoginPath(alias)) return alias;
|
|
160
158
|
return SOMA_DEFAULT_LOGIN_PATH;
|
|
161
159
|
}
|
|
162
160
|
|
|
@@ -214,7 +212,7 @@ function loadAppSync(appRoot: string): LoadedApp | null {
|
|
|
214
212
|
}
|
|
215
213
|
|
|
216
214
|
/**
|
|
217
|
-
* 读 `<应用根>/soma.app.ts` 默认导出并等服务端面装配完成(`await app.server()`);无该文件 → null
|
|
215
|
+
* 读 `<应用根>/soma.app.ts` 默认导出并等服务端面装配完成(`await app.server()`);无该文件 → null。
|
|
218
216
|
* 装配失败(SomaAppAssemblyError)原样上抛——拒绝启动,不静默回落。同一应用根只装配一次(并发调用共享同一 Promise)。
|
|
219
217
|
* Host 的同步消费点(编译点、虚拟页、树上下文)要求本函数先完成:入口路由在首次取页 / 取接口前 await 它。
|
|
220
218
|
*/
|
|
@@ -233,6 +231,25 @@ export async function loadHostAssembly(appRoot: string): Promise<SomaHostAssembl
|
|
|
233
231
|
return app.host;
|
|
234
232
|
}
|
|
235
233
|
|
|
234
|
+
/**
|
|
235
|
+
* 服务启动:加载装配(含服务端面)后 await 一次 `startup()`。同一应用根进程内只执行一次(并发共享同一 Promise);
|
|
236
|
+
* 装配失败或 `startup()` 抛错原样上抛并清掉记忆(拒绝启动,调用方让进程报错退出)。无装配或无 `startup` → 只完成装配。
|
|
237
|
+
* 只由宿主壳的启动入口(`instrumentation.ts`)调用;`soma build` 不调用。
|
|
238
|
+
*/
|
|
239
|
+
export async function runHostStartup(appRoot: string): Promise<void> {
|
|
240
|
+
const key = path.resolve(appRoot);
|
|
241
|
+
let pending = startups.get(key);
|
|
242
|
+
if (pending === undefined) {
|
|
243
|
+
pending = (async () => {
|
|
244
|
+
const assembly = await loadHostAssembly(key);
|
|
245
|
+
await assembly?.startup?.();
|
|
246
|
+
})();
|
|
247
|
+
pending.catch(() => { startups.delete(key); });
|
|
248
|
+
startups.set(key, pending);
|
|
249
|
+
}
|
|
250
|
+
await pending;
|
|
251
|
+
}
|
|
252
|
+
|
|
236
253
|
/** 该应用根的服务端面是否已装配完(无 server() 的装配恒为 true;无装配为 false) */
|
|
237
254
|
export function isHostAssemblyReady(appRoot: string): boolean {
|
|
238
255
|
const key = path.resolve(appRoot);
|
|
@@ -246,4 +263,5 @@ export function resetHostAssemblyCache(): void {
|
|
|
246
263
|
cache.clear();
|
|
247
264
|
ready.clear();
|
|
248
265
|
pendingServer.clear();
|
|
266
|
+
startups.clear();
|
|
249
267
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// 请求调用方身份的只读视图:把 Host 装配的 `identity(request)` 读成固定形状,供接口服务与工具按身份决策。
|
|
2
|
+
//
|
|
3
|
+
// 与访问门同一个身份来源、同一套读法(`readAccessIdentity`:`null`/`signedIn === false` 为未登录,角色取 `roles` 或 `role`),
|
|
4
|
+
// 另外读两个可选标识:`id`(字符串)与 `actor`(字符串,审计用)。core 不规定身份对象的其余形状,也不转出其它字段。
|
|
5
|
+
// 失败即关闭:没有 `SOMA_HOST_DIR`、没有装配、没有 `identity`、解析抛错,一律是匿名(不冒泡);
|
|
6
|
+
// 装配的服务端面装配失败属于「拒绝启动」,仍原样上抛。
|
|
7
|
+
import { loadHostAssembly } from './host-assembly';
|
|
8
|
+
import { readAccessIdentity } from '../runtime-space/access-gate';
|
|
9
|
+
|
|
10
|
+
export interface SomaHostCaller {
|
|
11
|
+
readonly signedIn: boolean;
|
|
12
|
+
readonly id?: string;
|
|
13
|
+
readonly roles: readonly string[];
|
|
14
|
+
readonly actor?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const ANONYMOUS: SomaHostCaller = Object.freeze({ signedIn: false, roles: Object.freeze([]) as readonly string[] });
|
|
18
|
+
|
|
19
|
+
function stringField(raw: unknown, key: 'id' | 'actor'): string | undefined {
|
|
20
|
+
if (raw === null || typeof raw !== 'object') return undefined;
|
|
21
|
+
const value = (raw as Record<string, unknown>)[key];
|
|
22
|
+
return typeof value === 'string' && value !== '' ? value : undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 读 Host 的身份对象(纯函数,可单测) */
|
|
26
|
+
export function readHostCaller(raw: unknown): SomaHostCaller {
|
|
27
|
+
const identity = readAccessIdentity(raw);
|
|
28
|
+
if (!identity.signedIn) return ANONYMOUS;
|
|
29
|
+
const id = stringField(raw, 'id');
|
|
30
|
+
const actor = stringField(raw, 'actor');
|
|
31
|
+
return Object.freeze({
|
|
32
|
+
signedIn: true,
|
|
33
|
+
roles: Object.freeze([...identity.roles]) as readonly string[],
|
|
34
|
+
...(id === undefined ? {} : { id }),
|
|
35
|
+
...(actor === undefined ? {} : { actor }),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 解析这次请求的调用方;装配的 `identity` 抛错按匿名处理 */
|
|
40
|
+
export async function resolveHostCaller(
|
|
41
|
+
request: Request | undefined,
|
|
42
|
+
hostDir: string | undefined = process.env.SOMA_HOST_DIR,
|
|
43
|
+
): Promise<SomaHostCaller> {
|
|
44
|
+
if (request === undefined || hostDir === undefined || hostDir.trim() === '') return ANONYMOUS;
|
|
45
|
+
const assembly = await loadHostAssembly(hostDir);
|
|
46
|
+
if (assembly?.identity === undefined) return ANONYMOUS;
|
|
47
|
+
try {
|
|
48
|
+
return readHostCaller(await assembly.identity(request));
|
|
49
|
+
} catch {
|
|
50
|
+
return ANONYMOUS;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Host 响应头出口:把装配的 `responseHeaders(context)` 合并进宿主壳产出的响应。
|
|
2
|
+
//
|
|
3
|
+
// 调用点(覆盖面):`proxy.ts` 对每个请求(页面、`/api/*`、静态资源、`_next/*`)调用;页面数据端点(`/soma/page` 等)与
|
|
4
|
+
// `api/[...path]` 路由再对最终响应调用一次,因为路由自设的 `cache-control` 等头会盖过 proxy 的同名头。
|
|
5
|
+
// 同名头以装配返回值为准。core 不认识具体头,也不替业务挑选类型(图片除外之类的取舍在装配的回调里写)。
|
|
6
|
+
import { loadHostAssembly } from './host-assembly';
|
|
7
|
+
import type { SomaHostResponseHeadersContext } from '../../shared/types/host-assembly.types';
|
|
8
|
+
|
|
9
|
+
/** 按路径分类:`/api/*` → api;`/_next/*`、`/soma-runtime/*`、`/favicon.ico`、带文件扩展名的路径 → asset;其余 → page */
|
|
10
|
+
export function classifyHostResponseKind(pathname: string): SomaHostResponseHeadersContext['kind'] {
|
|
11
|
+
if (pathname === '/api' || pathname.startsWith('/api/')) return 'api';
|
|
12
|
+
if (pathname.startsWith('/_next/') || pathname.startsWith('/soma-runtime/') || pathname === '/favicon.ico') return 'asset';
|
|
13
|
+
const last = pathname.split('/').at(-1) ?? '';
|
|
14
|
+
if (/\.[a-zA-Z0-9]+$/.test(last) && !/\.html?$/i.test(last)) return 'asset';
|
|
15
|
+
return 'page';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 取该请求应加的响应头。无 `SOMA_HOST_DIR`、无装配、装配没写 `responseHeaders` 或返回空 → undefined。
|
|
20
|
+
* 装配尚未就绪时会先等服务端面装配完(失败原样上抛)。
|
|
21
|
+
*/
|
|
22
|
+
export async function resolveHostResponseHeaders(
|
|
23
|
+
request: { readonly method: string; readonly pathname: string },
|
|
24
|
+
hostDir: string | undefined = process.env.SOMA_HOST_DIR,
|
|
25
|
+
): Promise<Readonly<Record<string, string>> | undefined> {
|
|
26
|
+
if (hostDir === undefined || hostDir.trim() === '') return undefined;
|
|
27
|
+
const assembly = await loadHostAssembly(hostDir);
|
|
28
|
+
if (assembly?.responseHeaders === undefined) return undefined;
|
|
29
|
+
const headers = await assembly.responseHeaders({
|
|
30
|
+
pathname: request.pathname,
|
|
31
|
+
method: request.method.toUpperCase(),
|
|
32
|
+
kind: classifyHostResponseKind(request.pathname),
|
|
33
|
+
});
|
|
34
|
+
if (headers === undefined || Object.keys(headers).length === 0) return undefined;
|
|
35
|
+
return headers;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 把装配的响应头合并进响应(同名以装配为准);没有要加的头时原样返回同一个 Response */
|
|
39
|
+
export async function withHostResponseHeaders(
|
|
40
|
+
response: Response,
|
|
41
|
+
request: { readonly method: string; readonly pathname: string },
|
|
42
|
+
hostDir?: string,
|
|
43
|
+
): Promise<Response> {
|
|
44
|
+
const extra = await resolveHostResponseHeaders(request, hostDir);
|
|
45
|
+
if (extra === undefined) return response;
|
|
46
|
+
const merged = new Headers(response.headers);
|
|
47
|
+
for (const [name, value] of Object.entries(extra)) merged.set(name, value);
|
|
48
|
+
return new Response(response.body, { status: response.status, statusText: response.statusText, headers: merged });
|
|
49
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// 多语言词表 → `localeYaml` 透传(KC2 `locale`;06e §2.11 `server-page-assets.ts`)。
|
|
2
2
|
//
|
|
3
3
|
// 是什么:编译端口页面段给出的多语言词表(`SomaLocaleBundle`:`{ format, defaultLocale, <语言>: { <键>: <文案> } }`)要随页面
|
|
4
|
-
// 资产一起交给浏览器;Host 另有自己的 `locale.yaml
|
|
4
|
+
// 资产一起交给浏览器;Host 另有自己的 `locale.yaml`(必须是词表信封或空;扁平单字典抛错)。
|
|
5
5
|
// 这里把两者合成**一份** `localeYaml` 文本:
|
|
6
6
|
// · 产物(编译器)同键词条优先于 Host 文件(Host 文件是兜底与覆盖面之外的补充,00e Q-164 同口径);
|
|
7
|
-
// ·
|
|
7
|
+
// · 缺省语言 = 产物词表的 defaultLocale,其次 Host 词表自己的。
|
|
8
8
|
// 没有产物词表时原样返回 Host 文本(字节不变)。纯函数,无 IO。
|
|
9
9
|
|
|
10
10
|
import yaml from 'js-yaml';
|
|
@@ -27,7 +27,11 @@ function stringEntries(value: unknown): Record<string, string> {
|
|
|
27
27
|
export function mergeLocaleYaml(hostYaml: string, compiled: SomaLocaleBundle | undefined): string {
|
|
28
28
|
if (compiled === undefined) return hostYaml;
|
|
29
29
|
const hostRaw: unknown = hostYaml.trim() === '' ? undefined : yaml.load(hostYaml);
|
|
30
|
-
|
|
30
|
+
const hostEmpty = hostRaw === undefined || hostRaw === null
|
|
31
|
+
|| (typeof hostRaw === 'object' && !Array.isArray(hostRaw) && Object.keys(hostRaw).length === 0);
|
|
32
|
+
if (!hostEmpty && !isLocaleBundle(hostRaw)) {
|
|
33
|
+
throw new Error('[soma-core][词表] Host 的 locale.yaml 必须是 { format: soma.locale-bundle, defaultLocale, <语言>: { 键: 文案 } } 信封;不接受扁平单字典');
|
|
34
|
+
}
|
|
31
35
|
const hostBundle = isLocaleBundle(hostRaw) ? hostRaw : undefined;
|
|
32
36
|
const defaultLocale = defaultLocaleOf(compiled) ?? (hostBundle !== undefined ? defaultLocaleOf(hostBundle) : undefined);
|
|
33
37
|
|
|
@@ -37,8 +41,6 @@ export function mergeLocaleYaml(hostYaml: string, compiled: SomaLocaleBundle | u
|
|
|
37
41
|
};
|
|
38
42
|
if (hostBundle !== undefined) {
|
|
39
43
|
for (const locale of localesOf(hostBundle)) put(locale, stringEntries(dictionaryOf(hostBundle, locale)));
|
|
40
|
-
} else if (defaultLocale !== undefined) {
|
|
41
|
-
put(defaultLocale, stringEntries(hostRaw));
|
|
42
44
|
}
|
|
43
45
|
for (const locale of localesOf(compiled)) put(locale, stringEntries(dictionaryOf(compiled, locale)));
|
|
44
46
|
|
|
@@ -14,7 +14,7 @@ import { mergeLocaleYaml } from './locale-yaml';
|
|
|
14
14
|
import type { SomaCoreDiagnostic } from '../../shared/core-diagnostic-codes';
|
|
15
15
|
import type { SomaGenerationErrorShape } from '../../shared/types/generation-error.types';
|
|
16
16
|
|
|
17
|
-
/** Registry `myworkspace`
|
|
17
|
+
/** Registry `myworkspace` 的地址是 `/myworkspace`。 */
|
|
18
18
|
export const registryPageNameFromPath = publishedPageSlugFromPath;
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -22,21 +22,12 @@ export type SomaRequestHook = (
|
|
|
22
22
|
context: SomaHostRequestContext,
|
|
23
23
|
) => Response | undefined | Promise<Response | undefined>;
|
|
24
24
|
|
|
25
|
-
export interface SomaRequestHookModule {
|
|
26
|
-
somaRequestHook?: SomaRequestHook;
|
|
27
|
-
handleSomaRequest?: SomaRequestHook;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
25
|
export async function resolveHostRequestHook(): Promise<SomaRequestHook | null> {
|
|
31
|
-
//
|
|
26
|
+
// 先等服务端面装配完(失败即拒绝启动,原样上抛),请求钩子只取 soma.app.ts 装配的
|
|
32
27
|
const hostDir = process.env.SOMA_HOST_DIR;
|
|
33
28
|
if (hostDir !== undefined && hostDir.trim() !== '') {
|
|
34
29
|
const assembly = await loadHostAssembly(hostDir);
|
|
35
30
|
if (assembly !== null) return assembly.requestHook?.() ?? null;
|
|
36
31
|
}
|
|
37
|
-
|
|
38
|
-
/* webpackInclude: /\/bootstrap\/soma-request-hook\.ts$/ */
|
|
39
|
-
'@soma-host/bootstrap/soma-request-hook'
|
|
40
|
-
) as SomaRequestHookModule;
|
|
41
|
-
return module.somaRequestHook ?? module.handleSomaRequest ?? null;
|
|
32
|
+
return null;
|
|
42
33
|
}
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
|
-
import { join, resolve } from 'node:path';
|
|
3
|
-
import createJiti from 'jiti';
|
|
4
|
-
import { SomaComponentTreeError } from '../../shared/component-tree-errors';
|
|
5
1
|
import type { TreeCompileContext } from '../../shared/types/component-tree.types';
|
|
6
|
-
import {
|
|
7
|
-
import { isHostAssemblyReady, loadHostAssemblySync, resolveServerOnlyEmpty } from './host-assembly';
|
|
2
|
+
import { isHostAssemblyReady, loadHostAssemblySync } from './host-assembly';
|
|
8
3
|
|
|
9
4
|
export interface SomaTreeContextRequest {
|
|
10
5
|
pageBase: string;
|
|
@@ -12,46 +7,17 @@ export interface SomaTreeContextRequest {
|
|
|
12
7
|
app?: string;
|
|
13
8
|
}
|
|
14
9
|
|
|
15
|
-
/** Synchronous, server-only Host assembly hook. No page/request-provided module paths. */
|
|
10
|
+
/** Synchronous, server-only Host assembly hook. No page/request-provided module paths. 只认 soma.app.ts 装配的 treeContext。 */
|
|
16
11
|
export function resolveHostTreeCompileContext(
|
|
17
12
|
hostDir: string | undefined,
|
|
18
13
|
request: SomaTreeContextRequest,
|
|
19
14
|
): TreeCompileContext | undefined {
|
|
20
15
|
if (!hostDir) return undefined;
|
|
21
|
-
//
|
|
16
|
+
// 装配的 treeContext 是唯一来源(服务端面须已装配完,见 loadHostAssembly)
|
|
22
17
|
const assembly = loadHostAssemblySync(hostDir);
|
|
23
|
-
if (assembly
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
return assembly.treeContext({ pageBase: request.pageBase });
|
|
28
|
-
}
|
|
29
|
-
const hostRoot = getSomaHostSourceRoot(hostDir);
|
|
30
|
-
const file = join(hostRoot, 'bootstrap', 'soma-tree-context.ts');
|
|
31
|
-
if (!existsSync(file)) return undefined;
|
|
32
|
-
try {
|
|
33
|
-
const serverOnlyEmpty = resolveServerOnlyEmpty();
|
|
34
|
-
const loader = createJiti(import.meta.url, {
|
|
35
|
-
interopDefault: false,
|
|
36
|
-
requireCache: false,
|
|
37
|
-
alias: {
|
|
38
|
-
'@soma-host': hostRoot,
|
|
39
|
-
'@core': resolve(process.cwd(), 'core'),
|
|
40
|
-
...(serverOnlyEmpty !== undefined ? { 'server-only': serverOnlyEmpty } : {}),
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
const loaded = loader(file) as {
|
|
44
|
-
createSomaTreeCompileContext?: (input: SomaTreeContextRequest) => TreeCompileContext;
|
|
45
|
-
};
|
|
46
|
-
if (typeof loaded?.createSomaTreeCompileContext !== 'function') throw new Error('missing hook');
|
|
47
|
-
const context = loaded.createSomaTreeCompileContext({ ...request });
|
|
48
|
-
if (!context || typeof context !== 'object'
|
|
49
|
-
|| !context.registrySnapshot || !context.compositionPolicy) throw new Error('invalid context');
|
|
50
|
-
return context;
|
|
51
|
-
} catch {
|
|
52
|
-
throw new SomaComponentTreeError({
|
|
53
|
-
code: 'CT-REGISTRY-CONTEXT', phase: 'validate', sourceId: '<host>', nodePath: 'treeContext',
|
|
54
|
-
fixHint: 'Host 的 bootstrap/soma-tree-context.ts 必须同步返回实际装配的组件快照和受控页面策略。',
|
|
55
|
-
});
|
|
18
|
+
if (assembly === null) return undefined;
|
|
19
|
+
if (!isHostAssemblyReady(hostDir)) {
|
|
20
|
+
throw new Error('Host 装配的服务端面尚未完成:入口须先 await loadHostAssembly(应用根)(或 await assembly.server())再取树上下文。');
|
|
56
21
|
}
|
|
22
|
+
return assembly.treeContext({ pageBase: request.pageBase });
|
|
57
23
|
}
|
|
@@ -24,8 +24,6 @@ import type { SomaPageMarkup, TreeCompileContext } from '../../shared/types/comp
|
|
|
24
24
|
import type { TreeArtifact } from '../../shared/types/component-tree-artifact.types';
|
|
25
25
|
import type { SomaCompiledStyle } from '../../shared/types/ui-config.types';
|
|
26
26
|
|
|
27
|
-
export type SomaPageAssetLoadProfile = 'render' | 'headless';
|
|
28
|
-
|
|
29
27
|
export interface SomaServerPageAssets {
|
|
30
28
|
sourceMarkup: SomaPageMarkup;
|
|
31
29
|
artifact?: TreeArtifact;
|
|
@@ -228,7 +226,6 @@ function assertContainedHtmlPath(source: SomaAssetSource, base: string): string
|
|
|
228
226
|
export function loadSomaServerPageAssets(options: {
|
|
229
227
|
hostDir: string | undefined;
|
|
230
228
|
urlPath: string;
|
|
231
|
-
profile?: SomaPageAssetLoadProfile;
|
|
232
229
|
watch?: boolean;
|
|
233
230
|
/** Trusted server context, never read from the page declaration or request body. */
|
|
234
231
|
treeContext?: TreeCompileContext;
|
|
@@ -244,7 +241,6 @@ export function loadSomaServerPageAssets(options: {
|
|
|
244
241
|
);
|
|
245
242
|
}
|
|
246
243
|
|
|
247
|
-
const profile = options.profile ?? 'render';
|
|
248
244
|
const sources = getSomaAssetSources(hostDir);
|
|
249
245
|
const hostSource = sources.find((source) => source.type === 'host');
|
|
250
246
|
const hostConfigRoot = hostSource?.configRoot ?? hostDir;
|
|
@@ -332,24 +328,6 @@ export function loadSomaServerPageAssets(options: {
|
|
|
332
328
|
...(compiled.titleKey ? { titleKey: compiled.titleKey } : {}),
|
|
333
329
|
};
|
|
334
330
|
|
|
335
|
-
if (profile === 'headless') {
|
|
336
|
-
return {
|
|
337
|
-
sourceMarkup: compiled.sourceMarkup,
|
|
338
|
-
...(compiled.artifact ? { artifact: compiled.artifact } : {}),
|
|
339
|
-
...(compiled.sourceMarkup === 'component-tree' ? { html: compiled.html } : {}),
|
|
340
|
-
base,
|
|
341
|
-
sourceType: source.type,
|
|
342
|
-
sourcePackage: source.packageName ?? null,
|
|
343
|
-
ui: compiled.ui,
|
|
344
|
-
event: compiled.sourceMarkup === 'component-tree' ? compiled.event : '',
|
|
345
|
-
styleYaml: '',
|
|
346
|
-
...(compiled.dataYaml ? { dataYaml: compiled.dataYaml } : {}),
|
|
347
|
-
localeYaml: mergedLocaleYaml,
|
|
348
|
-
appConfigYaml,
|
|
349
|
-
...compiledExtras,
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
|
|
353
331
|
return {
|
|
354
332
|
sourceMarkup: compiled.sourceMarkup,
|
|
355
333
|
...(compiled.artifact ? { artifact: compiled.artifact } : {}),
|
|
@@ -71,23 +71,22 @@ function getSomaHostSourceRoot(hostDir) {
|
|
|
71
71
|
return resolveSafeChildRoot(normalizedHostDir, packageJson?.soma?.hostRoot);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
/**
|
|
74
|
+
/** 源包判定:显式声明(环境变量)或 `package.json` 的 `soma.source === true` */
|
|
75
75
|
function isSomaSourcePackage(packageRoot, explicit) {
|
|
76
76
|
if (explicit) return true;
|
|
77
77
|
const packageJson = readJson(path.join(packageRoot, 'package.json'));
|
|
78
|
-
return packageJson?.soma?.source === true
|
|
78
|
+
return packageJson?.soma?.source === true;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
function toPackageSource(packageName, packageRoot) {
|
|
82
82
|
const packageJson = readJson(path.join(packageRoot, 'package.json'));
|
|
83
83
|
const codeRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.hostRoot);
|
|
84
84
|
const configRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.configRoot);
|
|
85
|
-
const shellRoot = path.join(codeRoot, 'shell');
|
|
86
85
|
return {
|
|
87
86
|
type: 'package',
|
|
88
87
|
name: packageName,
|
|
89
88
|
packageName,
|
|
90
|
-
assetRoot:
|
|
89
|
+
assetRoot: codeRoot,
|
|
91
90
|
codeRoot,
|
|
92
91
|
configRoot,
|
|
93
92
|
};
|
|
@@ -3,21 +3,18 @@
|
|
|
3
3
|
//
|
|
4
4
|
// 用途:发布前预览(生成回合的候选页面)、组件目录里的实时示例——它们都是「现算的声明」,
|
|
5
5
|
// 不是发布物,不该占用 Registry 页面名。soma-core 不知道这些页面是什么,只提供挂载点:
|
|
6
|
-
// · Host 在
|
|
7
|
-
//
|
|
6
|
+
// · Host 在 `soma.server.ts`(经 `soma.app.ts` 的 `server`)声明虚拟页,装配的 `virtualPages()` 返回
|
|
7
|
+
// `[{ prefix: '/preview/', resolve(path, request) }]`(按 `SOMA_HOST_DIR` 加载一次);
|
|
8
8
|
// · 解析器返回 `{ declaration, title? }` 认领该路径,返回 null 表示不认领(路由回到常规 404);
|
|
9
9
|
// · 环境闸(只在非生产开放等)是 Host 的责任:解析器拿到原始 request,自己判,不开放就返回 null。
|
|
10
10
|
//
|
|
11
|
-
// 前缀必须与 Registry 根路径页面互不相撞:Registry 认 `/<slug
|
|
11
|
+
// 前缀必须与 Registry 根路径页面互不相撞:Registry 认 `/<slug>`,
|
|
12
12
|
// 虚拟页前缀因此必须是「至少两段」的形态(`/<段>/`,以 `/` 结尾),且首段不能是保留名;
|
|
13
13
|
// 一个本身就是合法单段 slug 的前缀(`/preview`)会和同名 Registry 页面抢路径,注册时当场拒收。
|
|
14
|
-
// RT2 起虚拟页是 runtime 空间的前缀绑定(`/<段>/` 即
|
|
14
|
+
// RT2 起虚拟页是 runtime 空间的前缀绑定(`/<段>/` 即 `/<段>/*`)的薄包装:登记进同一张绑定表(挂在 globalThis 上,
|
|
15
15
|
// Host 服务模块与 core 路由来自不同模块实例也共享),用途映射为 `purpose: 'access'` + `cache: 'no-store'`;取页统一走 `loadPageByAddress`。
|
|
16
16
|
|
|
17
|
-
import fs from 'node:fs';
|
|
18
17
|
import path from 'node:path';
|
|
19
|
-
import { fileURLToPath } from 'node:url';
|
|
20
|
-
import createJiti from 'jiti';
|
|
21
18
|
import {
|
|
22
19
|
publishedPageSlugFromPath,
|
|
23
20
|
RESERVED_PUBLISHED_PAGE_SLUGS,
|
|
@@ -30,10 +27,8 @@ import { SomaPageAssetError } from './server-page-assets';
|
|
|
30
27
|
import { declarationNonStandardKeys } from '../registry/standard-product';
|
|
31
28
|
import { runtimeAssetsOfArtifact } from './published-page-assets';
|
|
32
29
|
import { resolveHostTreeCompileContext } from './resolve-host-tree-context';
|
|
33
|
-
import { getSomaHostSourceRoot } from './source-packages';
|
|
34
30
|
import { bindPrefixHandler, validateAddress } from '../runtime-space/inject';
|
|
35
|
-
import {
|
|
36
|
-
import { isHostAssemblyReady, loadHostAssemblySync, resolveServerOnlyEmpty } from './host-assembly';
|
|
31
|
+
import { isHostAssemblyReady, loadHostAssemblySync } from './host-assembly';
|
|
37
32
|
|
|
38
33
|
/**
|
|
39
34
|
* 解析器交回的虚拟页:声明(与 Registry 声明同一个闭集)+ 可选标题。
|
|
@@ -59,12 +54,6 @@ export interface SomaVirtualPageRegistration {
|
|
|
59
54
|
readonly resolve: SomaVirtualPageResolver;
|
|
60
55
|
}
|
|
61
56
|
|
|
62
|
-
/** 宿主装配钩子的位置(相对宿主源码根)与导出名 */
|
|
63
|
-
export const SOMA_HOST_VIRTUAL_PAGES_HOOK = {
|
|
64
|
-
file: path.join('bootstrap', 'soma-virtual-pages.ts'),
|
|
65
|
-
exportName: 'createSomaVirtualPages',
|
|
66
|
-
} as const;
|
|
67
|
-
|
|
68
57
|
const PREFIX_PATTERN = /^\/([a-z0-9][a-z0-9-]*)\/$/;
|
|
69
58
|
|
|
70
59
|
/** 前缀判据:`/<段>/`、首段非保留名、认领的路径不会被 Registry 路径解析认走。不合格当场抛。 */
|
|
@@ -90,8 +79,8 @@ export function assertVirtualPagePrefix(prefix: unknown): asserts prefix is stri
|
|
|
90
79
|
}
|
|
91
80
|
|
|
92
81
|
/**
|
|
93
|
-
* 登记一个前缀的解析器(`bindPrefix`
|
|
94
|
-
* 余段为空(`/preview/`
|
|
82
|
+
* 登记一个前缀的解析器(`bindPrefix` 的薄包装)。同前缀重复登记以后者为准(Host 热重载友好)。
|
|
83
|
+
* 余段为空(`/preview/` 本身)不认领。
|
|
95
84
|
*/
|
|
96
85
|
export function registerVirtualPageResolver(prefix: string, resolve: SomaVirtualPageResolver): void {
|
|
97
86
|
assertVirtualPagePrefix(prefix);
|
|
@@ -104,21 +93,17 @@ export function registerVirtualPageResolver(prefix: string, resolve: SomaVirtual
|
|
|
104
93
|
}, { cache: 'no-store', purpose: 'access' });
|
|
105
94
|
}
|
|
106
95
|
|
|
107
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
108
|
-
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
96
|
const loadedHosts = new Set<string>();
|
|
112
97
|
|
|
113
98
|
/**
|
|
114
|
-
* 按 `SOMA_HOST_DIR`
|
|
115
|
-
*
|
|
99
|
+
* 按 `SOMA_HOST_DIR` 读 Host 装配的 `virtualPages()` 并登记一次。没有目录 / 没有 `soma.app.ts` → 什么也不做;
|
|
100
|
+
* 装配形状不对在装配读取时已当场抛。
|
|
116
101
|
*/
|
|
117
102
|
export function ensureHostVirtualPages(hostDir: string | undefined = process.env.SOMA_HOST_DIR): void {
|
|
118
103
|
if (hostDir === undefined || hostDir.trim() === '') return;
|
|
119
104
|
const key = path.resolve(hostDir);
|
|
120
105
|
if (loadedHosts.has(key)) return;
|
|
121
|
-
//
|
|
106
|
+
// 虚拟页只取 soma.app.ts 装配的(服务端面须已装配完)
|
|
122
107
|
const assembly = loadHostAssemblySync(key);
|
|
123
108
|
if (assembly !== null) {
|
|
124
109
|
if (!isHostAssemblyReady(key)) {
|
|
@@ -146,35 +131,7 @@ export function ensureHostVirtualPages(hostDir: string | undefined = process.env
|
|
|
146
131
|
loadedHosts.add(key);
|
|
147
132
|
return;
|
|
148
133
|
}
|
|
149
|
-
|
|
150
|
-
const file = path.join(hostRoot, SOMA_HOST_VIRTUAL_PAGES_HOOK.file);
|
|
151
|
-
if (!fs.existsSync(file)) {
|
|
152
|
-
loadedHosts.add(key);
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
const serverOnlyEmpty = resolveServerOnlyEmpty();
|
|
156
|
-
const loader = createJiti(import.meta.url, {
|
|
157
|
-
interopDefault: false,
|
|
158
|
-
requireCache: false,
|
|
159
|
-
alias: {
|
|
160
|
-
'@soma-host': hostRoot,
|
|
161
|
-
'@core': path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'),
|
|
162
|
-
...(serverOnlyEmpty !== undefined ? { 'server-only': serverOnlyEmpty } : {}),
|
|
163
|
-
},
|
|
164
|
-
});
|
|
165
|
-
const loaded: unknown = loader(file);
|
|
166
|
-
const create = isRecord(loaded) ? loaded[SOMA_HOST_VIRTUAL_PAGES_HOOK.exportName] : undefined;
|
|
167
|
-
if (typeof create !== 'function') {
|
|
168
|
-
throw new Error(`宿主的 ${SOMA_HOST_VIRTUAL_PAGES_HOOK.file} 必须导出 ${SOMA_HOST_VIRTUAL_PAGES_HOOK.exportName}()`);
|
|
169
|
-
}
|
|
170
|
-
const registrations: unknown = create();
|
|
171
|
-
if (!Array.isArray(registrations)) {
|
|
172
|
-
throw new Error(`${SOMA_HOST_VIRTUAL_PAGES_HOOK.exportName}() 必须返回 { prefix, resolve } 数组`);
|
|
173
|
-
}
|
|
174
|
-
for (const entry of registrations) {
|
|
175
|
-
if (!isRecord(entry)) throw new Error(`${SOMA_HOST_VIRTUAL_PAGES_HOOK.exportName}() 的条目必须是 { prefix, resolve }`);
|
|
176
|
-
registerVirtualPageResolver(entry.prefix as string, entry.resolve as SomaVirtualPageResolver);
|
|
177
|
-
}
|
|
134
|
+
// 无 soma.app.ts:没有虚拟页,什么也不登记
|
|
178
135
|
loadedHosts.add(key);
|
|
179
136
|
}
|
|
180
137
|
|
|
@@ -201,7 +158,7 @@ function virtualPageAssets(page: SomaVirtualPage, pagePath: string): Record<stri
|
|
|
201
158
|
if (!treeContext) {
|
|
202
159
|
throw new SomaComponentTreeError({
|
|
203
160
|
code: 'CT-REGISTRY-CONTEXT', phase: 'artifact', sourceId: '<virtual>', nodePath: 'treeContext',
|
|
204
|
-
fixHint: '虚拟页声明为组件树时,Host
|
|
161
|
+
fixHint: '虚拟页声明为组件树时,Host 的 soma.app.ts 装配必须提供 treeContext()。',
|
|
205
162
|
});
|
|
206
163
|
}
|
|
207
164
|
}
|
|
@@ -234,21 +191,6 @@ function virtualPageAssets(page: SomaVirtualPage, pagePath: string): Record<stri
|
|
|
234
191
|
}
|
|
235
192
|
}
|
|
236
193
|
|
|
237
|
-
/**
|
|
238
|
-
* 取虚拟页运行时资产(存量公开函数,兼容保留):现在是绑定表上的前缀绑定查找。
|
|
239
|
-
* 没有前缀认领 / 解析器回 null → null(调用方走常规 404)。声明编不过时抛出(与 Registry 页同口径:调用方吞掉并回 404)。
|
|
240
|
-
*/
|
|
241
|
-
export async function loadVirtualPageAssets(pagePath: string, request: Request) {
|
|
242
|
-
ensureHostVirtualPages();
|
|
243
|
-
for (const hit of matchEntries(pagePath)) {
|
|
244
|
-
const handler = hit.entry.prefix;
|
|
245
|
-
if (handler === null) continue;
|
|
246
|
-
const result = await handler(pagePath, request);
|
|
247
|
-
if (result !== null && result.kind === 'assets') return result.assets as ReturnType<typeof virtualPageAssets>;
|
|
248
|
-
}
|
|
249
|
-
return null;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
194
|
/** 只给测试用:忘掉「已按宿主登记过」的标记(配合 runtime 空间复位) */
|
|
253
195
|
export function resetHostVirtualPages(): void {
|
|
254
196
|
loadedHosts.clear();
|