soma-core 2.2.8 → 2.2.10
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/core/build/compiler/full-manifest-builder.ts +1 -1
- package/core/build/compiler/host-config-sync.cjs +1 -1
- package/core/build/compiler/soma-manifest-webpack-plugin.js +2 -6
- package/core/build/compiler/soma-precompile.js +84 -4
- package/core/client/a11y/announcer.ts +2 -16
- package/core/client/base/SomaPageRootBase.ts +2 -2
- package/core/client/base/defineSomaView.ts +11 -2
- package/core/client/base/runtime-services.types.ts +57 -0
- package/core/client/data/role-gate.ts +2 -8
- package/core/client/engine/event-engine.ts +1 -0
- package/core/client/engines/diff/adapter-jsdiff.ts +27 -8
- package/core/client/engines/diff/code.ts +205 -0
- package/core/client/engines/diff/index.ts +107 -6
- package/core/client/engines/diff/items.ts +212 -0
- package/core/client/engines/diff/port.ts +215 -3
- package/core/client/engines/diff/prose.ts +443 -0
- package/core/client/engines/engine-adapters.ts +6 -0
- package/core/client/engines/highlight/adapter-lezer.ts +109 -0
- package/core/client/engines/highlight/index.ts +135 -0
- package/core/client/engines/highlight/port.ts +92 -0
- package/core/client/engines/markdown/adapter-markdown-it.ts +65 -0
- package/core/client/engines/markdown/blocks.ts +335 -0
- package/core/client/engines/markdown/index.ts +51 -0
- package/core/client/engines/markdown/port.ts +94 -0
- package/core/client/engines/markdown/vendor-modules.d.ts +6 -0
- package/core/client/engines/structured/adapter-js-yaml.ts +33 -0
- package/core/client/engines/structured/index.ts +139 -0
- package/core/client/engines/structured/port.ts +59 -0
- package/core/client/infra/http/SomaHttpService.ts +2 -4
- package/core/client/infra/notify/DefaultNotifyService.ts +2 -3
- package/core/client/infra/permission/SomaPermission.ts +1 -1
- package/core/client/render-ports/notify-renderer.ts +2 -46
- package/core/client/runtime/SomaRuntimeServices.ts +16 -45
- package/core/server/host/app-config-composer.ts +6 -52
- package/core/server/host/host-config-paths.cjs +2 -100
- package/core/server/host/precompiled-manifest.ts +69 -0
- package/core/server/host/source-packages.cjs +3 -0
- package/core/server/infra/host-config-paths.cjs +100 -0
- package/core/server/page-assets.ts +2 -70
- package/core/server/runtime-space/bindings.ts +33 -4
- package/core/server/runtime-space/store.ts +16 -1
- package/core/shared/app-config-merge.ts +59 -0
- package/core/shared/registry-generated/limits.ts +84 -4
- package/core/shared/types/announcer.types.ts +18 -0
- package/core/shared/types/notify.types.ts +26 -0
- package/core/shared/types/role-gate.types.ts +11 -0
- package/dist/browser/runtime-bundle.manifest.json +51 -7
- package/dist/browser/soma-runtime.js +120952 -104762
- package/dist/core/index.cjs +113613 -97412
- package/dist/host-native/aot-builder.cjs +582 -371
- package/dist/host-native/native-server.cjs +203 -72
- package/dist/lsp/component-tree.cjs +84 -4
- package/dist/types/build/compiler/full-manifest-builder.d.ts +1 -1
- package/dist/types/client/a11y/announcer.d.ts +2 -14
- package/dist/types/client/base/SomaPageRootBase.d.ts +2 -2
- package/dist/types/client/base/runtime-services.types.d.ts +50 -0
- package/dist/types/client/data/role-gate.d.ts +2 -8
- package/dist/types/client/engines/diff/code.d.ts +27 -0
- package/dist/types/client/engines/diff/index.d.ts +2 -1
- package/dist/types/client/engines/diff/items.d.ts +24 -0
- package/dist/types/client/engines/diff/port.d.ts +201 -2
- package/dist/types/client/engines/diff/prose.d.ts +80 -0
- package/dist/types/client/engines/engine-adapters.d.ts +6 -0
- package/dist/types/client/engines/highlight/adapter-lezer.d.ts +6 -0
- package/dist/types/client/engines/highlight/index.d.ts +15 -0
- package/dist/types/client/engines/highlight/port.d.ts +64 -0
- package/dist/types/client/engines/markdown/adapter-markdown-it.d.ts +6 -0
- package/dist/types/client/engines/markdown/blocks.d.ts +29 -0
- package/dist/types/client/engines/markdown/index.d.ts +9 -0
- package/dist/types/client/engines/markdown/port.d.ts +166 -0
- package/dist/types/client/engines/structured/adapter-js-yaml.d.ts +6 -0
- package/dist/types/client/engines/structured/index.d.ts +29 -0
- package/dist/types/client/engines/structured/port.d.ts +45 -0
- package/dist/types/client/infra/http/SomaHttpService.d.ts +1 -1
- package/dist/types/client/infra/notify/DefaultNotifyService.d.ts +2 -3
- package/dist/types/client/infra/permission/SomaPermission.d.ts +1 -1
- package/dist/types/client/render-ports/notify-renderer.d.ts +1 -56
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +9 -44
- package/dist/types/server/host/app-config-composer.d.ts +2 -3
- package/dist/types/server/host/host-config-paths.d.cts +2 -7
- package/dist/types/server/host/precompiled-manifest.d.ts +5 -0
- package/dist/types/server/host/source-packages.d.cts +5 -0
- package/dist/types/server/infra/host-config-paths.d.cts +7 -0
- package/dist/types/server/page-assets.d.ts +2 -5
- package/dist/types/server/runtime-space/store.d.ts +4 -0
- package/dist/types/shared/app-config-merge.d.ts +3 -0
- package/dist/types/shared/registry-generated/limits.d.ts +82 -4
- package/dist/types/shared/types/announcer.types.d.ts +14 -0
- package/dist/types/shared/types/notify.types.d.ts +36 -0
- package/dist/types/shared/types/role-gate.types.d.ts +8 -0
- package/host-native/src/aot-builder.ts +159 -96
- package/host-native/src/dev-chunk-gc.ts +104 -0
- package/host-native/src/host-prepare.ts +1 -1
- package/host-native/src/manifest-http.ts +1 -1
- package/host-native/src/page-http.ts +2 -67
- package/host-native/src/page-payload-cache.ts +68 -0
- package/host-native/src/public-packages.cjs +147 -50
- package/package.json +16 -2
|
@@ -1,64 +1,18 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import yaml from 'js-yaml';
|
|
3
|
+
import {
|
|
4
|
+
mergeSourcePackageAppConfig,
|
|
5
|
+
parseAppConfigYaml,
|
|
6
|
+
} from '../../shared/app-config-merge';
|
|
3
7
|
import { resolveHostAppConfigPath } from './host-config-paths';
|
|
4
8
|
import { currentHostDir } from './host-dir';
|
|
5
9
|
|
|
6
|
-
|
|
10
|
+
export { mergeSourcePackageAppConfig, parseAppConfigYaml };
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function isRecord(value: unknown): value is AppConfigRecord {
|
|
12
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
11
13
|
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
function cloneValue(value: unknown): unknown {
|
|
15
|
-
if (Array.isArray(value)) {
|
|
16
|
-
return value.map((item) => cloneValue(item));
|
|
17
|
-
}
|
|
18
|
-
if (!isRecord(value)) {
|
|
19
|
-
return value;
|
|
20
|
-
}
|
|
21
|
-
const result: AppConfigRecord = {};
|
|
22
|
-
for (const [key, child] of Object.entries(value)) {
|
|
23
|
-
if (!UNSAFE_KEYS.has(key)) {
|
|
24
|
-
result[key] = cloneValue(child);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return result;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function mergeRecords(defaults: AppConfigRecord, host: AppConfigRecord): AppConfigRecord {
|
|
31
|
-
const result = cloneValue(defaults) as AppConfigRecord;
|
|
32
|
-
for (const [key, hostValue] of Object.entries(host)) {
|
|
33
|
-
if (UNSAFE_KEYS.has(key)) {
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
const defaultValue = result[key];
|
|
37
|
-
result[key] = isRecord(defaultValue) && isRecord(hostValue)
|
|
38
|
-
? mergeRecords(defaultValue, hostValue)
|
|
39
|
-
: cloneValue(hostValue);
|
|
40
|
-
}
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function parseAppConfigYaml(source: string): unknown {
|
|
45
|
-
return source.trim() ? yaml.load(source) ?? {} : {};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Source packages provide defaults; the Host remains the final App-config owner. */
|
|
49
|
-
export function mergeSourcePackageAppConfig(
|
|
50
|
-
sourcePackageConfig: unknown,
|
|
51
|
-
hostConfig: unknown,
|
|
52
|
-
): unknown {
|
|
53
|
-
if (isRecord(sourcePackageConfig) && isRecord(hostConfig)) {
|
|
54
|
-
return mergeRecords(sourcePackageConfig, hostConfig);
|
|
55
|
-
}
|
|
56
|
-
if (hostConfig !== undefined && hostConfig !== null) {
|
|
57
|
-
return cloneValue(hostConfig);
|
|
58
|
-
}
|
|
59
|
-
return cloneValue(sourcePackageConfig ?? {});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
16
|
export function composeAppConfigYaml(
|
|
63
17
|
sourcePackageConfigYaml: string,
|
|
64
18
|
hostConfigYaml: string,
|
|
@@ -1,100 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
const path = require('node:path');
|
|
4
|
-
|
|
5
|
-
function readJson(filePath) {
|
|
6
|
-
try {
|
|
7
|
-
if (!fs.existsSync(filePath)) return null;
|
|
8
|
-
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
9
|
-
} catch {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function resolveSafeChildRoot(parent, relativePath, options = {}) {
|
|
15
|
-
const trimmed = typeof relativePath === 'string' ? relativePath.trim() : '';
|
|
16
|
-
if (!trimmed) return parent;
|
|
17
|
-
|
|
18
|
-
const resolved = path.resolve(parent, trimmed);
|
|
19
|
-
const normalizedParent = path.resolve(parent);
|
|
20
|
-
if (resolved === normalizedParent || resolved.startsWith(`${normalizedParent}${path.sep}`)) {
|
|
21
|
-
if (options.requireExists && !fs.existsSync(resolved)) {
|
|
22
|
-
return normalizedParent;
|
|
23
|
-
}
|
|
24
|
-
return resolved;
|
|
25
|
-
}
|
|
26
|
-
return options.fallback ?? normalizedParent;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function normalizeSomaEnv(raw) {
|
|
30
|
-
const value = String(raw ?? '').trim().toLowerCase();
|
|
31
|
-
if (!value) return 'test';
|
|
32
|
-
if (
|
|
33
|
-
['test', 'testing', 'dev', 'development', 'local', 'qa', 'staging', 'pre', 'prepub', 'uat']
|
|
34
|
-
.includes(value)
|
|
35
|
-
) {
|
|
36
|
-
return 'test';
|
|
37
|
-
}
|
|
38
|
-
return 'prod';
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Priority: SOMA_ENV → APP_ENV → NODE_ENV (framework-neutral only). */
|
|
42
|
-
function readSomaEnv(env = process.env) {
|
|
43
|
-
return normalizeSomaEnv(env.SOMA_ENV ?? env.APP_ENV ?? env.NODE_ENV);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function resolveHostConfigRoot(hostDir) {
|
|
47
|
-
const normalizedHostDir = path.resolve(hostDir);
|
|
48
|
-
const packageJson = readJson(path.join(normalizedHostDir, 'package.json'));
|
|
49
|
-
const configured = packageJson?.soma?.configRoot?.trim();
|
|
50
|
-
if (configured) {
|
|
51
|
-
return resolveSafeChildRoot(normalizedHostDir, configured, {
|
|
52
|
-
fallback: path.join(normalizedHostDir, 'config'),
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const hostSourceRoot = resolveSafeChildRoot(normalizedHostDir, packageJson?.soma?.hostRoot, {
|
|
57
|
-
requireExists: true,
|
|
58
|
-
});
|
|
59
|
-
const candidates = [
|
|
60
|
-
path.join(normalizedHostDir, 'config'),
|
|
61
|
-
path.join(hostSourceRoot, 'config'),
|
|
62
|
-
];
|
|
63
|
-
for (const candidate of candidates) {
|
|
64
|
-
if (fs.existsSync(candidate)) {
|
|
65
|
-
return candidate;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return path.join(hostSourceRoot, 'config');
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function getSomaHostConfigRoot(hostDir) {
|
|
72
|
-
return resolveHostConfigRoot(hostDir);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function resolveAppConfigPath(configRoot, envName) {
|
|
76
|
-
const env = envName ?? readSomaEnv();
|
|
77
|
-
const candidates = [
|
|
78
|
-
path.join(configRoot, `app.config.${env}.yaml`),
|
|
79
|
-
path.join(configRoot, 'app.config.yaml'),
|
|
80
|
-
];
|
|
81
|
-
for (const candidate of candidates) {
|
|
82
|
-
if (fs.existsSync(candidate)) {
|
|
83
|
-
return candidate;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function resolveHostAppConfigPath(hostDir, envName) {
|
|
90
|
-
return resolveAppConfigPath(resolveHostConfigRoot(hostDir), envName);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
module.exports = {
|
|
94
|
-
normalizeSomaEnv,
|
|
95
|
-
readSomaEnv,
|
|
96
|
-
resolveHostConfigRoot,
|
|
97
|
-
getSomaHostConfigRoot,
|
|
98
|
-
resolveAppConfigPath,
|
|
99
|
-
resolveHostAppConfigPath,
|
|
100
|
-
};
|
|
1
|
+
/** Backward-compatible re-export of core/server/infra/host-config-paths.cjs (CORE-L02). */
|
|
2
|
+
module.exports = require('../infra/host-config-paths.cjs');
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// 预编译产物载入(CORE-L04:由 server/page-assets.ts 迁入 server/host 应用层;page-assets.ts 仅作薄契约重导出)。
|
|
2
|
+
import type { SomaPrecompiledManifest, SomaPrecompiledPage } from '../../build/compiler/full-manifest-builder';
|
|
3
|
+
import { readPrewarmCompiled } from '../runtime-space/prewarm';
|
|
4
|
+
import { resolveSomaRuntimeLayout } from '../runtime-space/root';
|
|
5
|
+
import { urlPathToCandidates } from '../../build/compiler/page-base-resolver';
|
|
6
|
+
import { assertRuntimeArtifact } from '../../shared/component-tree-artifact';
|
|
7
|
+
import { verifyTreeRuntimeAssets } from '../../build/component-tree/artifact-codec';
|
|
8
|
+
|
|
9
|
+
function validatePages(raw: SomaPrecompiledManifest): SomaPrecompiledManifest | null {
|
|
10
|
+
if (!raw?.pages || !Array.isArray(raw.pages)) return null;
|
|
11
|
+
for (const page of raw.pages) {
|
|
12
|
+
assertRuntimeArtifact(page);
|
|
13
|
+
if (page.sourceMarkup === 'component-tree' && (!page.uiConfig || !Array.isArray(page.eventConfig))) return null;
|
|
14
|
+
if (page.sourceMarkup === 'component-tree' && !verifyTreeRuntimeAssets(page)) return null;
|
|
15
|
+
}
|
|
16
|
+
return raw;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface PrewarmMeta {
|
|
20
|
+
readonly routes?: SomaPrecompiledManifest['routes'];
|
|
21
|
+
readonly locale?: SomaPrecompiledManifest['locale'];
|
|
22
|
+
readonly appConfig?: unknown;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 新来源:runtime 空间的预热(`<root>/prewarm/`,RT3)。没有预热、预热里没有静态页编译结果、或旁档与索引摘要对不上 → undefined(让位给旧文件);对得上但页面校验不过 → null */
|
|
26
|
+
function loadFromPrewarm(hostDir: string, env: Readonly<Record<string, string | undefined>>): SomaPrecompiledManifest | null | undefined {
|
|
27
|
+
let layout;
|
|
28
|
+
try {
|
|
29
|
+
layout = resolveSomaRuntimeLayout(hostDir, env);
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined; // 根越界由启动期诊断负责;这里不拖垮预编译清单的读取
|
|
32
|
+
}
|
|
33
|
+
const read = readPrewarmCompiled<SomaPrecompiledPage, PrewarmMeta>(layout);
|
|
34
|
+
if (read === null) return undefined;
|
|
35
|
+
try {
|
|
36
|
+
return validatePages({
|
|
37
|
+
routes: read.meta?.routes ?? [],
|
|
38
|
+
locale: read.meta?.locale ?? {},
|
|
39
|
+
appConfig: read.meta?.appConfig ?? null,
|
|
40
|
+
pages: [...read.pages],
|
|
41
|
+
});
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 读构建期预编译清单:只读 runtime 空间的预热;没有预热或页面校验不过都返回 null。 */
|
|
48
|
+
export function loadPrecompiledManifest(
|
|
49
|
+
hostDir: string,
|
|
50
|
+
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
51
|
+
): SomaPrecompiledManifest | null {
|
|
52
|
+
return loadFromPrewarm(hostDir, env) ?? null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Find precompiled page by route (supports /items → items/index short path) */
|
|
56
|
+
export function findPrecompiledPage(
|
|
57
|
+
manifest: SomaPrecompiledManifest,
|
|
58
|
+
urlPath: string,
|
|
59
|
+
): SomaPrecompiledPage | null {
|
|
60
|
+
for (const base of urlPathToCandidates(urlPath)) {
|
|
61
|
+
const route = base === 'index' ? '/' : `/${base}`;
|
|
62
|
+
const page = manifest.pages.find((p) => {
|
|
63
|
+
const pageBase = p.path.replace(/^\//, '').replace(/\.html$/i, '');
|
|
64
|
+
return p.path === route || pageBase === base;
|
|
65
|
+
});
|
|
66
|
+
if (page) return page;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
@@ -240,6 +240,9 @@ function collectIncludes(sources) {
|
|
|
240
240
|
|
|
241
241
|
module.exports = {
|
|
242
242
|
SOMA_SOURCE_PACKAGES_ENV,
|
|
243
|
+
isSomaSourcePackage,
|
|
244
|
+
readExplicitSourcePackages,
|
|
245
|
+
resolvePackageRoot,
|
|
243
246
|
getSomaHostSourceRoot,
|
|
244
247
|
getSomaHostApiRoot,
|
|
245
248
|
resolveSafeChildRoot,
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/** CJS host config path resolution (CORE-L02: sunk to server/infra foundation layer). */
|
|
2
|
+
const fs = require('node:fs');
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
|
|
5
|
+
function readJson(filePath) {
|
|
6
|
+
try {
|
|
7
|
+
if (!fs.existsSync(filePath)) return null;
|
|
8
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function resolveSafeChildRoot(parent, relativePath, options = {}) {
|
|
15
|
+
const trimmed = typeof relativePath === 'string' ? relativePath.trim() : '';
|
|
16
|
+
if (!trimmed) return parent;
|
|
17
|
+
|
|
18
|
+
const resolved = path.resolve(parent, trimmed);
|
|
19
|
+
const normalizedParent = path.resolve(parent);
|
|
20
|
+
if (resolved === normalizedParent || resolved.startsWith(`${normalizedParent}${path.sep}`)) {
|
|
21
|
+
if (options.requireExists && !fs.existsSync(resolved)) {
|
|
22
|
+
return normalizedParent;
|
|
23
|
+
}
|
|
24
|
+
return resolved;
|
|
25
|
+
}
|
|
26
|
+
return options.fallback ?? normalizedParent;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function normalizeSomaEnv(raw) {
|
|
30
|
+
const value = String(raw ?? '').trim().toLowerCase();
|
|
31
|
+
if (!value) return 'test';
|
|
32
|
+
if (
|
|
33
|
+
['test', 'testing', 'dev', 'development', 'local', 'qa', 'staging', 'pre', 'prepub', 'uat']
|
|
34
|
+
.includes(value)
|
|
35
|
+
) {
|
|
36
|
+
return 'test';
|
|
37
|
+
}
|
|
38
|
+
return 'prod';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Priority: SOMA_ENV → APP_ENV → NODE_ENV (framework-neutral only). */
|
|
42
|
+
function readSomaEnv(env = process.env) {
|
|
43
|
+
return normalizeSomaEnv(env.SOMA_ENV ?? env.APP_ENV ?? env.NODE_ENV);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveHostConfigRoot(hostDir) {
|
|
47
|
+
const normalizedHostDir = path.resolve(hostDir);
|
|
48
|
+
const packageJson = readJson(path.join(normalizedHostDir, 'package.json'));
|
|
49
|
+
const configured = packageJson?.soma?.configRoot?.trim();
|
|
50
|
+
if (configured) {
|
|
51
|
+
return resolveSafeChildRoot(normalizedHostDir, configured, {
|
|
52
|
+
fallback: path.join(normalizedHostDir, 'config'),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const hostSourceRoot = resolveSafeChildRoot(normalizedHostDir, packageJson?.soma?.hostRoot, {
|
|
57
|
+
requireExists: true,
|
|
58
|
+
});
|
|
59
|
+
const candidates = [
|
|
60
|
+
path.join(normalizedHostDir, 'config'),
|
|
61
|
+
path.join(hostSourceRoot, 'config'),
|
|
62
|
+
];
|
|
63
|
+
for (const candidate of candidates) {
|
|
64
|
+
if (fs.existsSync(candidate)) {
|
|
65
|
+
return candidate;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return path.join(hostSourceRoot, 'config');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getSomaHostConfigRoot(hostDir) {
|
|
72
|
+
return resolveHostConfigRoot(hostDir);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function resolveAppConfigPath(configRoot, envName) {
|
|
76
|
+
const env = envName ?? readSomaEnv();
|
|
77
|
+
const candidates = [
|
|
78
|
+
path.join(configRoot, `app.config.${env}.yaml`),
|
|
79
|
+
path.join(configRoot, 'app.config.yaml'),
|
|
80
|
+
];
|
|
81
|
+
for (const candidate of candidates) {
|
|
82
|
+
if (fs.existsSync(candidate)) {
|
|
83
|
+
return candidate;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function resolveHostAppConfigPath(hostDir, envName) {
|
|
90
|
+
return resolveAppConfigPath(resolveHostConfigRoot(hostDir), envName);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = {
|
|
94
|
+
normalizeSomaEnv,
|
|
95
|
+
readSomaEnv,
|
|
96
|
+
resolveHostConfigRoot,
|
|
97
|
+
getSomaHostConfigRoot,
|
|
98
|
+
resolveAppConfigPath,
|
|
99
|
+
resolveHostAppConfigPath,
|
|
100
|
+
};
|
|
@@ -3,79 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 刻意不 `import 'server-only'`:soma-ai 的冒烟探测器在经 jiti 加载的宿主服务里
|
|
5
5
|
* 动态 import 本入口,server-only 在纯 Node 语境下会抛错(理由同 `server/page-declaration.ts`)。
|
|
6
|
+
* CORE-L04:本文件为纯薄契约重导出。
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
export { compilePageAssets } from '../build/compiler/page-assets';
|
|
9
10
|
// 入口对拍(D7)取 core 的编译入口:宿主文件页访问期编译、已发布页重编
|
|
10
11
|
export { compileSomaPageAssets } from './host/soma-page-compile';
|
|
11
12
|
export { compilePublishedPageArtifact } from './host/published-page-artifact-cache';
|
|
12
|
-
|
|
13
|
-
// 预编译产物载入依赖服务端runtime空间,归本入口,不由构建层反向依赖server。
|
|
14
|
-
import type { SomaPrecompiledManifest, SomaPrecompiledPage } from '../build/compiler/full-manifest-builder';
|
|
15
|
-
import { readPrewarmCompiled } from './runtime-space/prewarm';
|
|
16
|
-
import { resolveSomaRuntimeLayout } from './runtime-space/root';
|
|
17
|
-
import { urlPathToCandidates } from '../build/compiler/page-base-resolver';
|
|
18
|
-
import { assertRuntimeArtifact } from '../shared/component-tree-artifact';
|
|
19
|
-
import { verifyTreeRuntimeAssets } from '../build/component-tree/artifact-codec';
|
|
20
|
-
|
|
21
|
-
function validatePages(raw: SomaPrecompiledManifest): SomaPrecompiledManifest | null {
|
|
22
|
-
if (!raw?.pages || !Array.isArray(raw.pages)) return null;
|
|
23
|
-
for (const page of raw.pages) {
|
|
24
|
-
assertRuntimeArtifact(page);
|
|
25
|
-
if (page.sourceMarkup === 'component-tree' && (!page.uiConfig || !Array.isArray(page.eventConfig))) return null;
|
|
26
|
-
if (page.sourceMarkup === 'component-tree' && !verifyTreeRuntimeAssets(page)) return null;
|
|
27
|
-
}
|
|
28
|
-
return raw;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface PrewarmMeta {
|
|
32
|
-
readonly routes?: SomaPrecompiledManifest['routes'];
|
|
33
|
-
readonly locale?: SomaPrecompiledManifest['locale'];
|
|
34
|
-
readonly appConfig?: unknown;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** 新来源:runtime 空间的预热(`<root>/prewarm/`,RT3)。没有预热、预热里没有静态页编译结果、或旁档与索引摘要对不上 → undefined(让位给旧文件);对得上但页面校验不过 → null */
|
|
38
|
-
function loadFromPrewarm(hostDir: string, env: Readonly<Record<string, string | undefined>>): SomaPrecompiledManifest | null | undefined {
|
|
39
|
-
let layout;
|
|
40
|
-
try {
|
|
41
|
-
layout = resolveSomaRuntimeLayout(hostDir, env);
|
|
42
|
-
} catch {
|
|
43
|
-
return undefined; // 根越界由启动期诊断负责;这里不拖垮预编译清单的读取
|
|
44
|
-
}
|
|
45
|
-
const read = readPrewarmCompiled<SomaPrecompiledPage, PrewarmMeta>(layout);
|
|
46
|
-
if (read === null) return undefined;
|
|
47
|
-
try {
|
|
48
|
-
return validatePages({
|
|
49
|
-
routes: read.meta?.routes ?? [],
|
|
50
|
-
locale: read.meta?.locale ?? {},
|
|
51
|
-
appConfig: read.meta?.appConfig ?? null,
|
|
52
|
-
pages: [...read.pages],
|
|
53
|
-
});
|
|
54
|
-
} catch {
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** 读构建期预编译清单:只读 runtime 空间的预热;没有预热或页面校验不过都返回 null。 */
|
|
60
|
-
export function loadPrecompiledManifest(
|
|
61
|
-
hostDir: string,
|
|
62
|
-
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
63
|
-
): SomaPrecompiledManifest | null {
|
|
64
|
-
return loadFromPrewarm(hostDir, env) ?? null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Find precompiled page by route (supports /items → items/index short path) */
|
|
68
|
-
export function findPrecompiledPage(
|
|
69
|
-
manifest: SomaPrecompiledManifest,
|
|
70
|
-
urlPath: string,
|
|
71
|
-
): SomaPrecompiledPage | null {
|
|
72
|
-
for (const base of urlPathToCandidates(urlPath)) {
|
|
73
|
-
const route = base === 'index' ? '/' : `/${base}`;
|
|
74
|
-
const page = manifest.pages.find((p) => {
|
|
75
|
-
const pageBase = p.path.replace(/^\//, '').replace(/\.html$/i, '');
|
|
76
|
-
return p.path === route || pageBase === base;
|
|
77
|
-
});
|
|
78
|
-
if (page) return page;
|
|
79
|
-
}
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
13
|
+
export { loadPrecompiledManifest, findPrecompiledPage } from './host/precompiled-manifest';
|
|
@@ -124,7 +124,8 @@ export function resetRuntimeSpace(): void {
|
|
|
124
124
|
export function registerOwnerVersionGuard(owner: string, guard: OwnerVersionGuard): void {
|
|
125
125
|
const state = space();
|
|
126
126
|
state.ownerGuards.set(owner, guard);
|
|
127
|
-
let dropped =
|
|
127
|
+
let dropped = 0;
|
|
128
|
+
const digests = new Set<string>();
|
|
128
129
|
for (const [key, entry] of state.entries) {
|
|
129
130
|
const binding = entry.binding;
|
|
130
131
|
if (binding.owner !== owner || !binding.persisted || binding.origin !== 'inject' || guard(binding.version)) continue;
|
|
@@ -134,9 +135,22 @@ export function registerOwnerVersionGuard(owner: string, guard: OwnerVersionGuar
|
|
|
134
135
|
} catch {
|
|
135
136
|
/* 根不可用:没有落盘可清 */
|
|
136
137
|
}
|
|
137
|
-
|
|
138
|
+
if (binding.digest !== null) digests.add(binding.digest);
|
|
139
|
+
dropped += 1;
|
|
140
|
+
}
|
|
141
|
+
if (dropped > 0) {
|
|
142
|
+
bumpNavGeneration();
|
|
143
|
+
// 升级丢弃旧产物:汇总一行日志 + 清掉不再被引用的产物文件
|
|
144
|
+
const live = new Set([...state.entries.values()].flatMap((e) => (e.binding.digest !== null ? [e.binding.digest] : [])));
|
|
145
|
+
let cleaned = 0;
|
|
146
|
+
try {
|
|
147
|
+
const store = runtimeStore();
|
|
148
|
+
for (const digest of digests) if (!live.has(digest) && store.removeArtifact(digest)) cleaned += 1;
|
|
149
|
+
} catch {
|
|
150
|
+
/* 根不可用:没有落盘可清 */
|
|
151
|
+
}
|
|
152
|
+
console.warn(`[soma-core][引擎] 升级后丢弃旧页面产物 ${dropped} 个(产出方 ${owner} 的版本不符),已清理产物文件 ${cleaned} 个;由产出方重新编译注入。`);
|
|
138
153
|
}
|
|
139
|
-
if (dropped) bumpNavGeneration();
|
|
140
154
|
}
|
|
141
155
|
|
|
142
156
|
// ── 布局与存储 ────────────────────────────────────────────────────────────────
|
|
@@ -203,6 +217,10 @@ export function ensureLoaded(): void {
|
|
|
203
217
|
state.loadedRoots.add(layout.root);
|
|
204
218
|
const store = runtimeStore(layout);
|
|
205
219
|
const now = Date.now();
|
|
220
|
+
// 升级丢弃的旧产物:按原因计数(落盘格式版本 / 标准格式版本 = 格式;产出方版本守卫 = 所有者),其产物文件在装入结束后清掉(没有别的绑定引用时)
|
|
221
|
+
const dropped = { format: 0, owner: 0 };
|
|
222
|
+
const droppedDigests = new Set<string>();
|
|
223
|
+
const legacyBefore = store.legacyDiscarded;
|
|
206
224
|
for (const read of store.listBindings()) {
|
|
207
225
|
const binding = read.binding;
|
|
208
226
|
if (binding.origin !== 'inject' || binding.digest === null || binding.kind === 'prefix') continue;
|
|
@@ -223,13 +241,16 @@ export function ensureLoaded(): void {
|
|
|
223
241
|
// 缺信封的方言旧产物由落盘格式版本与产出方版本守卫拦下)
|
|
224
242
|
if (page.format !== SOMA_PAGE_FORMAT_VERSION) {
|
|
225
243
|
store.removeBinding(binding.address);
|
|
226
|
-
|
|
244
|
+
dropped.format += 1;
|
|
245
|
+
droppedDigests.add(binding.digest);
|
|
227
246
|
continue;
|
|
228
247
|
}
|
|
229
248
|
// 产出方版本指纹闸(见 registerOwnerVersionGuard):已登记守卫且不认这个 version → 丢弃
|
|
230
249
|
const guard = binding.owner === undefined ? undefined : state.ownerGuards.get(binding.owner);
|
|
231
250
|
if (guard !== undefined && !guard(binding.version)) {
|
|
232
251
|
store.removeBinding(binding.address);
|
|
252
|
+
dropped.owner += 1;
|
|
253
|
+
droppedDigests.add(binding.digest);
|
|
233
254
|
continue;
|
|
234
255
|
}
|
|
235
256
|
state.generations.set(key, Math.max(state.generations.get(key) ?? 0, binding.generation));
|
|
@@ -242,6 +263,14 @@ export function ensureLoaded(): void {
|
|
|
242
263
|
mtimeMs: read.mtimeMs,
|
|
243
264
|
});
|
|
244
265
|
}
|
|
266
|
+
const legacy = store.legacyDiscarded - legacyBefore;
|
|
267
|
+
dropped.format += legacy;
|
|
268
|
+
if (dropped.format + dropped.owner > 0) {
|
|
269
|
+
const live = new Set([...state.entries.values()].flatMap((entry) => (entry.binding.digest !== null ? [entry.binding.digest] : [])));
|
|
270
|
+
let cleaned = 0;
|
|
271
|
+
for (const digest of droppedDigests) if (!live.has(digest) && store.removeArtifact(digest)) cleaned += 1;
|
|
272
|
+
console.warn(`[soma-core][引擎] 升级后丢弃旧页面产物 ${dropped.format + dropped.owner} 个(格式版本不符 ${dropped.format},产出方版本不符 ${dropped.owner}),已清理产物文件 ${cleaned} 个;由产出方重新编译注入。`);
|
|
273
|
+
}
|
|
245
274
|
if (process.env.SOMA_RUNTIME_GC !== 'off') {
|
|
246
275
|
try {
|
|
247
276
|
const referenced = [...state.entries.values()].flatMap((entry) => (entry.binding.digest !== null ? [entry.binding.digest] : []));
|
|
@@ -143,6 +143,18 @@ export class SomaRuntimeStore {
|
|
|
143
143
|
return page;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/** 删除产物文件(升级丢弃旧产物后清理用;调用方保证没有别的绑定引用它);返回是否真删了文件 */
|
|
147
|
+
removeArtifact(digest: string): boolean {
|
|
148
|
+
if (!isRuntimeDigest(digest)) return false;
|
|
149
|
+
this.pages.delete(digest);
|
|
150
|
+
try {
|
|
151
|
+
fs.unlinkSync(artifactFilePath(this.dirs.artifacts, digest));
|
|
152
|
+
return true;
|
|
153
|
+
} catch {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
146
158
|
hasArtifact(digest: string): boolean {
|
|
147
159
|
return isRuntimeDigest(digest) && fs.existsSync(artifactFilePath(this.dirs.artifacts, digest));
|
|
148
160
|
}
|
|
@@ -228,6 +240,9 @@ export class SomaRuntimeStore {
|
|
|
228
240
|
}
|
|
229
241
|
}
|
|
230
242
|
|
|
243
|
+
/** 本进程读绑定时因落盘格式版本不符而丢弃的旧绑定个数(启动汇总日志用) */
|
|
244
|
+
legacyDiscarded = 0;
|
|
245
|
+
|
|
231
246
|
/** 严格删除:文件本来就不在返回 false(视为成功);其他失败(权限 / IO)原样抛出,不吞 */
|
|
232
247
|
removeBindingStrict(address: string): boolean {
|
|
233
248
|
try {
|
|
@@ -255,7 +270,7 @@ export class SomaRuntimeStore {
|
|
|
255
270
|
const stat = fs.statSync(file);
|
|
256
271
|
const record = JSON.parse(fs.readFileSync(file, 'utf8')) as SomaPersistedBinding;
|
|
257
272
|
if (record.schemaVersion === SOMA_RUNTIME_STORE_FORMAT && record.binding) out.push({ binding: record.binding, mtimeMs: stat.mtimeMs });
|
|
258
|
-
else fs.unlinkSync(file); // 旧格式绑定:丢弃(产物由 GC 回收),由方言所有者重新编译注入
|
|
273
|
+
else { fs.unlinkSync(file); this.legacyDiscarded += 1; } // 旧格式绑定:丢弃(产物由 GC 回收),由方言所有者重新编译注入
|
|
259
274
|
} catch {
|
|
260
275
|
/* 损坏的绑定文件不拖垮启动 */
|
|
261
276
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// 应用配置 YAML 解析与源包/宿主配置深合并(CORE-L02:纯函数下沉至 shared 层,
|
|
2
|
+
// 供构建期 full-manifest-builder 与服务端 host/app-config-composer 共用)。
|
|
3
|
+
import yaml from 'js-yaml';
|
|
4
|
+
|
|
5
|
+
type AppConfigRecord = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
const UNSAFE_KEYS = new Set(['__proto__', 'prototype', 'constructor']);
|
|
8
|
+
|
|
9
|
+
function isRecord(value: unknown): value is AppConfigRecord {
|
|
10
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function cloneValue(value: unknown): unknown {
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
return value.map((item) => cloneValue(item));
|
|
16
|
+
}
|
|
17
|
+
if (!isRecord(value)) {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
const result: AppConfigRecord = {};
|
|
21
|
+
for (const [key, child] of Object.entries(value)) {
|
|
22
|
+
if (!UNSAFE_KEYS.has(key)) {
|
|
23
|
+
result[key] = cloneValue(child);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function mergeRecords(defaults: AppConfigRecord, host: AppConfigRecord): AppConfigRecord {
|
|
30
|
+
const result = cloneValue(defaults) as AppConfigRecord;
|
|
31
|
+
for (const [key, hostValue] of Object.entries(host)) {
|
|
32
|
+
if (UNSAFE_KEYS.has(key)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const defaultValue = result[key];
|
|
36
|
+
result[key] = isRecord(defaultValue) && isRecord(hostValue)
|
|
37
|
+
? mergeRecords(defaultValue, hostValue)
|
|
38
|
+
: cloneValue(hostValue);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function parseAppConfigYaml(source: string): unknown {
|
|
44
|
+
return source.trim() ? yaml.load(source) ?? {} : {};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Source packages provide defaults; the Host remains the final App-config owner. */
|
|
48
|
+
export function mergeSourcePackageAppConfig(
|
|
49
|
+
sourcePackageConfig: unknown,
|
|
50
|
+
hostConfig: unknown,
|
|
51
|
+
): unknown {
|
|
52
|
+
if (isRecord(sourcePackageConfig) && isRecord(hostConfig)) {
|
|
53
|
+
return mergeRecords(sourcePackageConfig, hostConfig);
|
|
54
|
+
}
|
|
55
|
+
if (hostConfig !== undefined && hostConfig !== null) {
|
|
56
|
+
return cloneValue(hostConfig);
|
|
57
|
+
}
|
|
58
|
+
return cloneValue(sourcePackageConfig ?? {});
|
|
59
|
+
}
|