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
|
@@ -7,7 +7,7 @@ export interface SomaLocaleBundle {
|
|
|
7
7
|
readonly defaultLocale?: string;
|
|
8
8
|
readonly [locale: string]: SomaLocaleDictionary | string | undefined;
|
|
9
9
|
}
|
|
10
|
-
/** 只按 `format`
|
|
10
|
+
/** 只按 `format` 判别词表信封 */
|
|
11
11
|
export declare function isLocaleBundle(value: unknown): value is SomaLocaleBundle;
|
|
12
12
|
/** 词表里的语言列表(保留键除外;按键序) */
|
|
13
13
|
export declare function localesOf(bundle: SomaLocaleBundle): readonly string[];
|
|
@@ -82,7 +82,7 @@ export interface SomaScopeMirrorDecl {
|
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
84
|
* 显隐门:按切片值的业务真值切换页面元素(`data-soma-id` = `hook`)的 `hidden` 属性。
|
|
85
|
-
* 不写内联样式;元素由编译器同时带 `data-soma-gate
|
|
85
|
+
* 不写内联样式;元素由编译器同时带 `data-soma-gate`,隐藏规则在页面级样式片里。
|
|
86
86
|
*/
|
|
87
87
|
export interface SomaScopeGateDecl {
|
|
88
88
|
/** 被门控元素的页面 `data-soma-id` */
|
|
@@ -122,13 +122,8 @@ export interface SomaFragmentRegion {
|
|
|
122
122
|
* @req F-021 R-022
|
|
123
123
|
*/
|
|
124
124
|
export interface SomaPageFragments {
|
|
125
|
-
/**
|
|
126
|
-
|
|
127
|
-
* 两者都缺席或都不认识时运行时整份忽略并告警,不猜字段。
|
|
128
|
-
*/
|
|
129
|
-
format?: typeof SOMA_FRAGMENTS_FORMAT;
|
|
130
|
-
/** 存量产物格式版本(只作引用;新产出方写 `format`) */
|
|
131
|
-
version?: 1;
|
|
125
|
+
/** 产物格式标识(KC11,06e §3.6);缺席或不认识时整份拒收,不猜字段。 */
|
|
126
|
+
format: typeof SOMA_FRAGMENTS_FORMAT;
|
|
132
127
|
/** 按锚点名升序——同一份声明必须产出同一份字节(§19.3 幂等) */
|
|
133
128
|
regions: SomaFragmentRegion[];
|
|
134
129
|
/**
|
|
@@ -50,7 +50,7 @@ export interface SomaTimerPort {
|
|
|
50
50
|
setTimeout(fn: () => void, ms: number): unknown;
|
|
51
51
|
clearTimeout(handle: unknown): void;
|
|
52
52
|
}
|
|
53
|
-
/** 写入端口的结果元信息(附在 `SomaDataSubmitResult`
|
|
53
|
+
/** 写入端口的结果元信息(附在 `SomaDataSubmitResult` 上,全部可选) */
|
|
54
54
|
export interface SomaWriteOutcomeMeta {
|
|
55
55
|
readonly intent?: SomaSaveIntent;
|
|
56
56
|
readonly completion?: SomaSaveCompletion;
|
package/global.d.ts
CHANGED
|
@@ -19,15 +19,6 @@ declare module '@soma-host/bootstrap/soma-bootstrap' {
|
|
|
19
19
|
| undefined;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
declare module '@soma-host/bootstrap/soma-request-hook' {
|
|
23
|
-
export const somaRequestHook:
|
|
24
|
-
| import('./core/server/host/resolve-host-request-hook').SomaRequestHook
|
|
25
|
-
| undefined;
|
|
26
|
-
export const handleSomaRequest:
|
|
27
|
-
| import('./core/server/host/resolve-host-request-hook').SomaRequestHook
|
|
28
|
-
| undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
22
|
declare module '@soma-host/bootstrap/soma-domain-loaders' {
|
|
32
23
|
export const somaDomainLoaders:
|
|
33
24
|
| import('./core/client/host-bridge/soma-domain-loaders').SomaDomainLoaderMap
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
4
|
+
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
|
5
|
+
import { SomaHydrationShell } from './components/SomaHydrationShell';
|
|
6
|
+
import { SomaPageForbidden } from './components/SomaPageForbidden';
|
|
7
|
+
import { PageAccessError, readPageAccessError, loginRedirectTarget } from './page-access';
|
|
8
|
+
import { createNextNavigate, pendingNavigateInfo, type SomaShellNavigate } from './router-bridge';
|
|
9
|
+
import type {
|
|
10
|
+
ComponentTreeRuntimeEnvelope,
|
|
11
|
+
SomaBootstrap as SomaBootstrapType,
|
|
12
|
+
SomaPageAssets,
|
|
13
|
+
SomaRuntimeServices,
|
|
14
|
+
} from 'soma-core/runtime';
|
|
15
|
+
import { assertRuntimeArtifact, maybeStartBootTimings, readBootTimings } from 'soma-core/runtime';
|
|
16
|
+
import { loadHostDomainClass, resolveHostBootstrapOptions } from 'soma-core/host-bridge';
|
|
17
|
+
|
|
18
|
+
const isDev = process.env.NODE_ENV !== 'production';
|
|
19
|
+
|
|
20
|
+
export default function SomaEntry() {
|
|
21
|
+
const pathname = usePathname();
|
|
22
|
+
const searchParams = useSearchParams();
|
|
23
|
+
const router = useRouter();
|
|
24
|
+
// core 带来的落历史方式:replace → router.replace(不入历史),否则 push
|
|
25
|
+
const navigate = useCallback<SomaShellNavigate>((url, info) => {
|
|
26
|
+
createNextNavigate(router)(url, info);
|
|
27
|
+
}, [router]);
|
|
28
|
+
const routerPath = searchParams.toString()
|
|
29
|
+
? `${pathname}?${searchParams.toString()}`
|
|
30
|
+
: pathname;
|
|
31
|
+
return (
|
|
32
|
+
<SomaPageRenderer
|
|
33
|
+
pagePath={pathname}
|
|
34
|
+
routerPath={routerPath}
|
|
35
|
+
onNavigate={navigate}
|
|
36
|
+
/>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function urlPathToPageBase(urlPath: string): string {
|
|
41
|
+
const clean = urlPath.replace(/^\//, '').replace(/\.html$/i, '');
|
|
42
|
+
return clean || 'index';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type PagePayload = ComponentTreeRuntimeEnvelope & {
|
|
46
|
+
base: string;
|
|
47
|
+
sourcePackage?: string | null;
|
|
48
|
+
html: string;
|
|
49
|
+
ui: string;
|
|
50
|
+
event: string;
|
|
51
|
+
styleYaml?: string;
|
|
52
|
+
dataYaml?: string;
|
|
53
|
+
includes?: Record<string, string>;
|
|
54
|
+
localeYaml: string;
|
|
55
|
+
appConfigYaml: string;
|
|
56
|
+
/** 编译样式片与暗色首帧载体(KC2):服务端页面外壳读 colorScheme,浏览器只装载 compiledStyles */
|
|
57
|
+
compiledStyles?: SomaPageAssets['compiledStyles'];
|
|
58
|
+
colorScheme?: SomaPageAssets['colorScheme'];
|
|
59
|
+
/** 取页响应带来的路由上下文(E3 / E4):每次取页都以最新响应为准,不进资产缓存 */
|
|
60
|
+
params?: SomaPageAssets['pageParams'];
|
|
61
|
+
query?: SomaPageAssets['pageQuery'];
|
|
62
|
+
queryContract?: SomaPageAssets['queryContract'];
|
|
63
|
+
nav?: SomaPageAssets['nav'];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
type PrecompiledAssets = Pick<
|
|
67
|
+
SomaPageAssets,
|
|
68
|
+
'html' | 'sourceMarkup' | 'artifact' | 'uiConfig' | 'eventConfig' | 'triggerConfig' | 'timerConfig' | 'styleConfig' | 'locale' | 'appConfig' | 'dataConfig' | 'compiledStyles'
|
|
69
|
+
>;
|
|
70
|
+
|
|
71
|
+
const pageAssetCache = new Map<string, PagePayload>();
|
|
72
|
+
|
|
73
|
+
async function loadPrecompiledFromManifest(path: string, current: ComponentTreeRuntimeEnvelope): Promise<PrecompiledAssets | null> {
|
|
74
|
+
try {
|
|
75
|
+
const res = await fetch(`/soma/manifest?path=${encodeURIComponent(path)}`);
|
|
76
|
+
if (!res.ok) return null;
|
|
77
|
+
const data: PrecompiledAssets & {
|
|
78
|
+
triggers?: SomaPageAssets['triggerConfig']; timers?: SomaPageAssets['timerConfig'];
|
|
79
|
+
} = await res.json();
|
|
80
|
+
assertRuntimeArtifact(data);
|
|
81
|
+
if ((data.sourceMarkup ?? 'legacy-html') !== (current.sourceMarkup ?? 'legacy-html')) return null;
|
|
82
|
+
if (current.sourceMarkup === 'component-tree'
|
|
83
|
+
&& data.artifact?.artifactDigest !== current.artifact?.artifactDigest) return null;
|
|
84
|
+
if (typeof data.html !== 'string') return null;
|
|
85
|
+
return {
|
|
86
|
+
sourceMarkup: data.sourceMarkup,
|
|
87
|
+
html: data.html,
|
|
88
|
+
artifact: data.artifact,
|
|
89
|
+
uiConfig: data.uiConfig,
|
|
90
|
+
eventConfig: data.eventConfig,
|
|
91
|
+
triggerConfig: data.triggers ?? [],
|
|
92
|
+
timerConfig: data.timers ?? [],
|
|
93
|
+
styleConfig: data.styleConfig,
|
|
94
|
+
locale: data.locale ?? {},
|
|
95
|
+
appConfig: data.appConfig ?? null,
|
|
96
|
+
dataConfig: data.dataConfig ?? undefined,
|
|
97
|
+
...(data.compiledStyles !== undefined ? { compiledStyles: data.compiledStyles } : {}),
|
|
98
|
+
};
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function SomaPageRenderer({
|
|
105
|
+
pagePath,
|
|
106
|
+
routerPath,
|
|
107
|
+
onNavigate,
|
|
108
|
+
}: {
|
|
109
|
+
pagePath: string;
|
|
110
|
+
routerPath: string;
|
|
111
|
+
onNavigate: SomaShellNavigate;
|
|
112
|
+
}) {
|
|
113
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
114
|
+
const bootstrapRef = useRef<SomaBootstrapType | null>(null);
|
|
115
|
+
const routerBridgeRef = useRef<(() => void) | null>(null);
|
|
116
|
+
const [error, setError] = useState<string | null>(null);
|
|
117
|
+
const [mounting, setMounting] = useState(true);
|
|
118
|
+
const [forbidden, setForbidden] = useState<{ textKey: string | undefined } | null>(null);
|
|
119
|
+
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!containerRef.current) return;
|
|
122
|
+
|
|
123
|
+
let cancelled = false;
|
|
124
|
+
|
|
125
|
+
const mountInto = async (
|
|
126
|
+
DomainClass: new () => any,
|
|
127
|
+
SomaBootstrapCtor: new (...args: any[]) => SomaBootstrapType,
|
|
128
|
+
assets: Omit<SomaPageAssets, 'html' | 'includes' | 'DomainClass'>,
|
|
129
|
+
html: string,
|
|
130
|
+
includes?: Record<string, string>,
|
|
131
|
+
) => {
|
|
132
|
+
await bootstrapRef.current?.unmount();
|
|
133
|
+
|
|
134
|
+
const getServices = () =>
|
|
135
|
+
bootstrapRef.current?.getDomainInstance()?._services as SomaRuntimeServices | undefined;
|
|
136
|
+
|
|
137
|
+
// 分片重试退避缺省值取 soma-design 真源(经 foundation 投影),core 里不写数字(层标准 D22)
|
|
138
|
+
const { designChunkLoadBackoffMs } = await import('soma-foundation/layout');
|
|
139
|
+
const hostBootstrapOptions = await resolveHostBootstrapOptions(getServices, { chunkLoadBackoffMs: designChunkLoadBackoffMs() });
|
|
140
|
+
|
|
141
|
+
const bs = new SomaBootstrapCtor(hostBootstrapOptions);
|
|
142
|
+
bootstrapRef.current = bs;
|
|
143
|
+
|
|
144
|
+
await bs.mount(containerRef.current!, {
|
|
145
|
+
html,
|
|
146
|
+
includes,
|
|
147
|
+
DomainClass,
|
|
148
|
+
...assets,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await bs.whenSettled();
|
|
152
|
+
if (cancelled) return;
|
|
153
|
+
if (typeof window !== 'undefined') {
|
|
154
|
+
window.__SOMA_DIAGNOSTICS__ = {
|
|
155
|
+
isSettled: () => bs.isSettled(),
|
|
156
|
+
getDomainValue: (path: string) => bs.getDomainValue(path),
|
|
157
|
+
getRegisteredSomaIds: () => bs.getRegisteredSomaIds(),
|
|
158
|
+
// 只读 CSP nonce 链:响应头 nonce → meta → 运行期注入的样式元素。
|
|
159
|
+
// 浏览器插入后会抹掉 nonce 内容属性,这里读 IDL 属性。
|
|
160
|
+
getCspNonceChain: () => ({
|
|
161
|
+
meta: document.querySelector('meta[name="csp-nonce"]')?.getAttribute('content')?.trim() ?? '',
|
|
162
|
+
styles: Array.from(document.head.querySelectorAll('style'))
|
|
163
|
+
.map((style) => (style as HTMLStyleElement).nonce ?? '')
|
|
164
|
+
.filter(Boolean),
|
|
165
|
+
}),
|
|
166
|
+
};
|
|
167
|
+
window.__SOMA_READY__ = bs.isInitialized();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
routerBridgeRef.current?.();
|
|
171
|
+
const domain = bs.getDomainInstance() as {
|
|
172
|
+
_services?: {
|
|
173
|
+
router: {
|
|
174
|
+
getCurrentPath: () => string;
|
|
175
|
+
getHistory?: () => readonly string[];
|
|
176
|
+
onNavigate: (cb: (p: string, info?: { readonly replace?: boolean }) => void) => () => void;
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
} | null;
|
|
180
|
+
if (domain?._services?.router) {
|
|
181
|
+
const somaRouter = domain._services.router;
|
|
182
|
+
routerBridgeRef.current = somaRouter.onNavigate((nextPath, info) => {
|
|
183
|
+
if (nextPath !== routerPath) {
|
|
184
|
+
onNavigate(nextPath, info);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
// onMount 里触发的 navigate 发生在桥接注册之前,此处补同步到 Next 路由(落历史方式按内存路由轨迹推断)
|
|
188
|
+
const pending = somaRouter.getCurrentPath();
|
|
189
|
+
if (pending !== routerPath) {
|
|
190
|
+
onNavigate(pending, pendingNavigateInfo(somaRouter));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (isDev && typeof window !== 'undefined') {
|
|
195
|
+
window.__SOMA__ = {
|
|
196
|
+
getDiagnosticsSnapshot: () => bs.getDiagnosticsSnapshot(),
|
|
197
|
+
getTelemetrySnapshot: () => bs.getTelemetrySnapshot(),
|
|
198
|
+
// 首屏三段:E2E/手工排查从这里读,不必自己去翻 performance 时间线
|
|
199
|
+
getBootTimings: () => readBootTimings(),
|
|
200
|
+
getDomainInstance: () => bs.getDomainInstance(),
|
|
201
|
+
captureSnapshot: () => bs.captureSnapshot(),
|
|
202
|
+
restoreSnapshot: (id: number) => bs.restoreSnapshot(id),
|
|
203
|
+
setLocale: (locale: string) => bs.setLocale(locale),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const run = async () => {
|
|
209
|
+
try {
|
|
210
|
+
// 首屏三段打点的起点:放在页面资产拉取之前,段一才量得到网络那一截。
|
|
211
|
+
// 未开启(globalThis.__SOMA_BOOT_TIMINGS__ 未置真)时只是一次布尔判断。
|
|
212
|
+
maybeStartBootTimings();
|
|
213
|
+
setError(null);
|
|
214
|
+
setForbidden(null);
|
|
215
|
+
setMounting(true);
|
|
216
|
+
|
|
217
|
+
const { SomaBootstrap } = await import('soma-core/runtime');
|
|
218
|
+
|
|
219
|
+
const res = await fetch(`/soma/page?path=${encodeURIComponent(pagePath)}`);
|
|
220
|
+
if (!res.ok) throw await readPageAccessError(res);
|
|
221
|
+
const data: PagePayload = await res.json();
|
|
222
|
+
assertRuntimeArtifact(data);
|
|
223
|
+
if (cancelled) return;
|
|
224
|
+
|
|
225
|
+
const base = (data.base as string) || urlPathToPageBase(pagePath);
|
|
226
|
+
const sourcePackage = typeof data.sourcePackage === 'string' ? data.sourcePackage : null;
|
|
227
|
+
const DomainClass = await loadHostDomainClass({ base, sourcePackage });
|
|
228
|
+
const routeAssets = {
|
|
229
|
+
routerPath,
|
|
230
|
+
...(data.params !== undefined ? { pageParams: data.params } : {}),
|
|
231
|
+
...(data.query !== undefined ? { pageQuery: data.query } : {}),
|
|
232
|
+
...(data.queryContract !== undefined ? { queryContract: data.queryContract } : {}),
|
|
233
|
+
...(data.nav !== undefined ? { nav: data.nav } : {}),
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
if (!isDev) {
|
|
237
|
+
const precompiled = await loadPrecompiledFromManifest(pagePath, data);
|
|
238
|
+
if (precompiled) {
|
|
239
|
+
await mountInto(
|
|
240
|
+
DomainClass,
|
|
241
|
+
SomaBootstrap,
|
|
242
|
+
{ ...precompiled, ...routeAssets },
|
|
243
|
+
precompiled.html,
|
|
244
|
+
);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const cached = pageAssetCache.get(routerPath);
|
|
249
|
+
if (cached && (cached.sourceMarkup ?? 'legacy-html') === (data.sourceMarkup ?? 'legacy-html')
|
|
250
|
+
&& (data.sourceMarkup !== 'component-tree' || cached.artifact?.artifactDigest === data.artifact?.artifactDigest)) {
|
|
251
|
+
const CachedClass = await loadHostDomainClass({
|
|
252
|
+
base: cached.base,
|
|
253
|
+
sourcePackage: cached.sourcePackage,
|
|
254
|
+
});
|
|
255
|
+
await mountInto(CachedClass, SomaBootstrap, {
|
|
256
|
+
uiYaml: cached.ui,
|
|
257
|
+
eventYaml: cached.event,
|
|
258
|
+
styleYaml: cached.styleYaml,
|
|
259
|
+
dataYaml: cached.dataYaml,
|
|
260
|
+
locale: cached.localeYaml ?? '',
|
|
261
|
+
appConfigYaml: cached.appConfigYaml ?? '',
|
|
262
|
+
sourceMarkup: cached.sourceMarkup,
|
|
263
|
+
artifact: cached.artifact,
|
|
264
|
+
...(cached.compiledStyles !== undefined ? { compiledStyles: cached.compiledStyles } : {}),
|
|
265
|
+
...routeAssets,
|
|
266
|
+
}, cached.html, cached.includes);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const payload: PagePayload = {
|
|
272
|
+
base,
|
|
273
|
+
sourcePackage,
|
|
274
|
+
sourceMarkup: data.sourceMarkup,
|
|
275
|
+
artifact: data.artifact,
|
|
276
|
+
html: data.html,
|
|
277
|
+
ui: data.ui,
|
|
278
|
+
event: data.event,
|
|
279
|
+
styleYaml: data.styleYaml ?? '',
|
|
280
|
+
dataYaml: typeof data.dataYaml === 'string' ? data.dataYaml : undefined,
|
|
281
|
+
includes: data.includes,
|
|
282
|
+
localeYaml: data.localeYaml ?? '',
|
|
283
|
+
appConfigYaml: data.appConfigYaml ?? '',
|
|
284
|
+
compiledStyles: data.compiledStyles,
|
|
285
|
+
colorScheme: data.colorScheme,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
if (!isDev) {
|
|
289
|
+
pageAssetCache.set(routerPath, payload);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
await mountInto(DomainClass, SomaBootstrap, {
|
|
293
|
+
uiYaml: payload.ui,
|
|
294
|
+
eventYaml: payload.event,
|
|
295
|
+
styleYaml: payload.styleYaml,
|
|
296
|
+
dataYaml: payload.dataYaml,
|
|
297
|
+
locale: payload.localeYaml ?? '',
|
|
298
|
+
appConfigYaml: payload.appConfigYaml ?? '',
|
|
299
|
+
sourceMarkup: payload.sourceMarkup,
|
|
300
|
+
artifact: payload.artifact,
|
|
301
|
+
...(payload.compiledStyles !== undefined ? { compiledStyles: payload.compiledStyles } : {}),
|
|
302
|
+
...routeAssets,
|
|
303
|
+
}, payload.html, payload.includes);
|
|
304
|
+
} catch (err: any) {
|
|
305
|
+
if (cancelled) return;
|
|
306
|
+
if (err instanceof PageAccessError && err.status === 401) {
|
|
307
|
+
// 未登录:跳 Host 约定的登录页(响应带路径),带回跳地址;已在登录页上时不再跳,避免循环
|
|
308
|
+
const target = loginRedirectTarget(err.loginPath, pagePath, routerPath);
|
|
309
|
+
if (target !== null) {
|
|
310
|
+
window.location.assign(target);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (err instanceof PageAccessError && err.status === 403) {
|
|
315
|
+
setForbidden({ textKey: err.textKey });
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
setError(err?.message ?? String(err));
|
|
319
|
+
} finally {
|
|
320
|
+
if (!cancelled) setMounting(false);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
void run();
|
|
325
|
+
|
|
326
|
+
return () => {
|
|
327
|
+
cancelled = true;
|
|
328
|
+
routerBridgeRef.current?.();
|
|
329
|
+
routerBridgeRef.current = null;
|
|
330
|
+
if (typeof window !== 'undefined') {
|
|
331
|
+
delete window.__SOMA__;
|
|
332
|
+
delete window.__SOMA_READY__;
|
|
333
|
+
delete window.__SOMA_DIAGNOSTICS__;
|
|
334
|
+
}
|
|
335
|
+
void bootstrapRef.current?.unmount();
|
|
336
|
+
bootstrapRef.current = null;
|
|
337
|
+
};
|
|
338
|
+
}, [pagePath, routerPath, onNavigate]);
|
|
339
|
+
|
|
340
|
+
if (forbidden) {
|
|
341
|
+
return <SomaPageForbidden textKey={forbidden.textKey} />;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (error) {
|
|
345
|
+
return (
|
|
346
|
+
<div className="soma-page-error">
|
|
347
|
+
<p className="soma-page-error__title">Page failed to load</p>
|
|
348
|
+
<p className="soma-page-error__message">{error}</p>
|
|
349
|
+
</div>
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return (
|
|
354
|
+
<>
|
|
355
|
+
{mounting && !error ? <SomaHydrationShell /> : null}
|
|
356
|
+
<div ref={containerRef} className={mounting && !error ? 'soma-hidden' : undefined} />
|
|
357
|
+
</>
|
|
358
|
+
);
|
|
359
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SomaHostPage, type SomaPageSearchParams } from '../components/SomaHostPage';
|
|
2
|
+
import { notFound } from 'next/navigation';
|
|
3
|
+
|
|
4
|
+
export default async function SomaCatchAllPage({
|
|
5
|
+
params,
|
|
6
|
+
searchParams,
|
|
7
|
+
}: {
|
|
8
|
+
params: Promise<{ path: string[] }>;
|
|
9
|
+
searchParams?: Promise<SomaPageSearchParams>;
|
|
10
|
+
}) {
|
|
11
|
+
const resolvedParams = await params;
|
|
12
|
+
const lastSegment = resolvedParams.path.at(-1) ?? '';
|
|
13
|
+
if (/\.[a-zA-Z0-9]+$/.test(lastSegment) && !/\.html$/i.test(lastSegment)) {
|
|
14
|
+
notFound();
|
|
15
|
+
}
|
|
16
|
+
return (
|
|
17
|
+
<SomaHostPage
|
|
18
|
+
pathname={`/${resolvedParams.path.join('/')}`}
|
|
19
|
+
searchParams={await searchParams}
|
|
20
|
+
/>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
import type { IService } from '../../../core/server/api/IService';
|
|
3
|
+
import { loadHostService } from '../../../core/server/api/load-host-service';
|
|
4
|
+
import { invokeHostRequestHook } from '../../../core/server/host/invoke-host-request-hook';
|
|
5
|
+
import { withHostResponseHeaders } from '../../../core/server/host/host-response-headers';
|
|
6
|
+
|
|
7
|
+
// ─── HTTP 方法分发工厂 ─────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
10
|
+
|
|
11
|
+
function createHandler(method: HttpMethod) {
|
|
12
|
+
const methodKey = method.toLowerCase() as keyof IService;
|
|
13
|
+
|
|
14
|
+
async function dispatch(
|
|
15
|
+
req: NextRequest,
|
|
16
|
+
{ params }: { params: Promise<{ path: string[] }> },
|
|
17
|
+
): Promise<Response> {
|
|
18
|
+
const segments = (await params).path || [];
|
|
19
|
+
|
|
20
|
+
const rejected = await invokeHostRequestHook(req, {
|
|
21
|
+
kind: 'api',
|
|
22
|
+
path: req.nextUrl.pathname,
|
|
23
|
+
});
|
|
24
|
+
if (rejected) {
|
|
25
|
+
return rejected;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (segments.length === 0) {
|
|
29
|
+
return Response.json(
|
|
30
|
+
{ error: 'Missing API path. Expected: /api/{module}/{action}' },
|
|
31
|
+
{ status: 400 },
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const service = loadHostService(segments);
|
|
36
|
+
|
|
37
|
+
if (!service) {
|
|
38
|
+
return Response.json(
|
|
39
|
+
{ error: `API not found: /api/${segments.join('/')}` },
|
|
40
|
+
{ status: 404 },
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const handlerFn = service[methodKey];
|
|
45
|
+
if (typeof handlerFn !== 'function') {
|
|
46
|
+
return Response.json(
|
|
47
|
+
{
|
|
48
|
+
error: `Method ${method} not allowed: /api/${segments.join('/')}`,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
status: 405,
|
|
52
|
+
headers: { Allow: getAllowHeader(service) },
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
return await handlerFn.call(service, req);
|
|
59
|
+
} catch (e: unknown) {
|
|
60
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
61
|
+
console.error(`[soma-core][接口] ${method} /api/${segments.join('/')}:`, message);
|
|
62
|
+
return Response.json(
|
|
63
|
+
{ error: message || 'Internal server error' },
|
|
64
|
+
{ status: 500 },
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 最终响应(含请求钩子拒绝、404/405/500)统一合并 Host 装配的响应头出口
|
|
70
|
+
return async function handler(
|
|
71
|
+
req: NextRequest,
|
|
72
|
+
context: { params: Promise<{ path: string[] }> },
|
|
73
|
+
): Promise<Response> {
|
|
74
|
+
return withHostResponseHeaders(await dispatch(req, context), {
|
|
75
|
+
method: req.method,
|
|
76
|
+
pathname: req.nextUrl.pathname,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 生成 Allow 响应头 */
|
|
82
|
+
function getAllowHeader(service: IService): string {
|
|
83
|
+
const methods: string[] = [];
|
|
84
|
+
if (typeof service.get === 'function') methods.push('GET');
|
|
85
|
+
if (typeof service.post === 'function') methods.push('POST');
|
|
86
|
+
if (typeof service.put === 'function') methods.push('PUT');
|
|
87
|
+
if (typeof service.patch === 'function') methods.push('PATCH');
|
|
88
|
+
if (typeof service.delete === 'function') methods.push('DELETE');
|
|
89
|
+
return methods.join(', ');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ─── 导出所有 HTTP 方法处理器 ───────────────────────────────
|
|
93
|
+
|
|
94
|
+
export const GET = createHandler('GET');
|
|
95
|
+
export const POST = createHandler('POST');
|
|
96
|
+
export const PUT = createHandler('PUT');
|
|
97
|
+
export const PATCH = createHandler('PATCH');
|
|
98
|
+
export const DELETE = createHandler('DELETE');
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import type { IService } from '../../../../core/server/api/IService';
|
|
4
|
+
import { resolveRegistryService } from '../../../../core/server/registry/services/index';
|
|
5
|
+
import { invokeHostRequestHook } from '../../../../core/server/host/invoke-host-request-hook';
|
|
6
|
+
import { withHostResponseHeaders } from '../../../../core/server/host/host-response-headers';
|
|
7
|
+
|
|
8
|
+
// 注册中心内置端点(/api/soma-registry/*)。
|
|
9
|
+
// 比通用宿主服务路由(/api/[...path])更具体,Next 自动优先匹配——core 自带,宿主零接线。
|
|
10
|
+
// 鉴权分两层:宿主请求钩子先跑(应用决定谁可管理),端点内的主体鉴权再跑(平台底线)。
|
|
11
|
+
|
|
12
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
13
|
+
|
|
14
|
+
function createHandler(method: HttpMethod) {
|
|
15
|
+
const methodKey = method.toLowerCase() as keyof IService;
|
|
16
|
+
|
|
17
|
+
async function dispatch(
|
|
18
|
+
req: NextRequest,
|
|
19
|
+
{ params }: { params: Promise<{ path: string[] }> },
|
|
20
|
+
): Promise<Response> {
|
|
21
|
+
const segments = (await params).path || [];
|
|
22
|
+
|
|
23
|
+
const rejected = await invokeHostRequestHook(req, {
|
|
24
|
+
kind: 'api',
|
|
25
|
+
path: req.nextUrl.pathname,
|
|
26
|
+
});
|
|
27
|
+
if (rejected) {
|
|
28
|
+
return rejected;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const service = resolveRegistryService(segments);
|
|
32
|
+
if (!service) {
|
|
33
|
+
return Response.json(
|
|
34
|
+
{ error: `API not found: /api/soma-registry/${segments.join('/')}` },
|
|
35
|
+
{ status: 404 },
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const handlerFn = service[methodKey];
|
|
40
|
+
if (typeof handlerFn !== 'function') {
|
|
41
|
+
return Response.json(
|
|
42
|
+
{ error: `Method ${method} not allowed: /api/soma-registry/${segments.join('/')}` },
|
|
43
|
+
{ status: 405 },
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
return await handlerFn.call(service, req);
|
|
49
|
+
} catch (e: unknown) {
|
|
50
|
+
// 未预期异常:细节只进服务端日志,回给调用方的只有事件号(不泄漏路径与部署形态)
|
|
51
|
+
const eventId = randomUUID().slice(0, 8);
|
|
52
|
+
console.error(
|
|
53
|
+
`[soma-core][注册中心] 事件 ${eventId} 未处理异常 ${method} /api/soma-registry/${segments.join('/')}:`,
|
|
54
|
+
e instanceof Error ? (e.stack ?? e.message) : String(e),
|
|
55
|
+
);
|
|
56
|
+
return Response.json(
|
|
57
|
+
{ error: `服务器内部错误(事件 ${eventId},详情见服务端日志)` },
|
|
58
|
+
{ status: 500 },
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 最终响应统一合并 Host 装配的响应头出口
|
|
64
|
+
return async function handler(
|
|
65
|
+
req: NextRequest,
|
|
66
|
+
context: { params: Promise<{ path: string[] }> },
|
|
67
|
+
): Promise<Response> {
|
|
68
|
+
return withHostResponseHeaders(await dispatch(req, context), {
|
|
69
|
+
method: req.method,
|
|
70
|
+
pathname: req.nextUrl.pathname,
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const GET = createHandler('GET');
|
|
76
|
+
export const POST = createHandler('POST');
|
|
77
|
+
export const PUT = createHandler('PUT');
|
|
78
|
+
export const PATCH = createHandler('PATCH');
|
|
79
|
+
export const DELETE = createHandler('DELETE');
|