soma-core 2.1.5 → 2.2.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/soma.cjs +1 -1
- package/bin/soma.ts +64 -15
- package/core/build/compiler/soma-precompile.js +11 -1
- package/core/client/base/SomaHttpClient.ts +85 -12
- package/core/client/data/data-object-resolver.ts +12 -1
- package/core/client/engine/event-engine.ts +3 -3
- package/core/client/engine/soma-bootstrap.ts +30 -30
- package/core/client/host-bridge/load-host-domain-class.ts +74 -20
- package/core/client/host-bridge/resolve-host-bootstrap.ts +125 -23
- package/core/client/infra/http/IHttpClient.ts +3 -1
- package/core/client/infra/http/SomaHttpService.ts +15 -6
- package/core/client/parser/data-config-parser.ts +6 -1
- package/core/server/api/dispatch-service-api.ts +212 -0
- package/core/server/api/load-host-service.ts +230 -56
- package/core/server/api/service-path.ts +78 -14
- package/core/server/engines/style/candidate-guard.ts +57 -0
- package/core/server/engines/style/index.ts +1 -1
- package/core/server/engines/style/tailwind-adapter.ts +83 -16
- package/core/server/host/source-packages.cjs +96 -30
- package/core/server/host/source-packages.ts +12 -5
- package/core/server/index.ts +39 -17
- package/core/server/infra/cache/ServerFileCache.ts +138 -25
- package/core/server/node.ts +32 -8
- package/core/shared/registry-generated/error-codes.ts +5 -0
- package/core/shared/registry-generated/limits.ts +5 -0
- package/core/shared/types/data-config.types.ts +2 -0
- package/dist/browser/soma-runtime.js +197 -49
- package/dist/cli/soma.js +68 -14
- package/dist/core/index.cjs +284 -64
- package/dist/host-native/aot-builder.cjs +850 -0
- package/dist/host-native/native-server.cjs +15338 -0
- package/dist/lsp/component-tree.cjs +11 -1
- package/dist/types/client/base/SomaHttpClient.d.ts +1 -8
- package/dist/types/client/data/data-object-resolver.d.ts +1 -0
- package/dist/types/client/engine/event-engine.d.ts +2 -2
- package/dist/types/client/engine/soma-bootstrap.d.ts +3 -2
- package/dist/types/client/host-bridge/load-host-domain-class.d.ts +1 -0
- package/dist/types/client/host-bridge/resolve-host-bootstrap.d.ts +20 -0
- package/dist/types/client/infra/http/IHttpClient.d.ts +2 -0
- package/dist/types/server/api/dispatch-service-api.d.ts +18 -0
- package/dist/types/server/api/load-host-service.d.ts +7 -2
- package/dist/types/server/api/service-path.d.ts +21 -1
- package/dist/types/server/engines/style/candidate-guard.d.ts +10 -0
- package/dist/types/server/engines/style/index.d.ts +1 -1
- package/dist/types/server/engines/style/tailwind-adapter.d.ts +12 -0
- package/dist/types/server/host/source-packages.d.cts +13 -1
- package/dist/types/server/host/source-packages.d.ts +7 -4
- package/dist/types/server/index.d.ts +18 -17
- package/dist/types/server/infra/cache/ServerFileCache.d.ts +28 -5
- package/dist/types/server/node.d.ts +10 -7
- package/dist/types/shared/registry-generated/error-codes.d.ts +4 -0
- package/dist/types/shared/registry-generated/limits.d.ts +4 -0
- package/dist/types/shared/types/data-config.types.d.ts +2 -0
- package/host-native/bin/host-native.cjs +204 -0
- package/host-native/client/browser-entry.ts +499 -0
- package/{host-next/app → host-native/client}/page-access.ts +12 -0
- package/host-native/client/router-bridge.ts +120 -0
- package/host-native/src/aot-builder.ts +486 -0
- package/host-native/src/csp-headers.ts +75 -0
- package/host-native/src/host-prepare.ts +123 -0
- package/{host-next/app/soma/manifest/route.ts → host-native/src/manifest-http.ts} +10 -10
- package/host-native/src/native-server.ts +691 -0
- package/host-native/src/page-http.ts +223 -0
- package/host-native/src/public-packages.cjs +167 -0
- package/{host-next/app/api/soma-registry/[...path]/route.ts → host-native/src/registry-handler.ts} +17 -19
- package/{host-next → host-native}/src/shell-routes.cjs +23 -13
- package/host-native/src/static-file.ts +172 -0
- package/host-native/src/workspace.ts +185 -0
- package/{host-next/app → host-native/styles}/globals.css +2 -2
- package/package.json +13 -32
- package/tsconfig.json +1 -1
- package/host-next/app/SomaPageClient.tsx +0 -359
- package/host-next/app/[...path]/page.tsx +0 -22
- package/host-next/app/api/[...path]/route.ts +0 -98
- package/host-next/app/components/SomaHostPage.tsx +0 -128
- package/host-next/app/components/SomaHydrationShell.tsx +0 -20
- package/host-next/app/components/SomaPageClientBoundary.tsx +0 -13
- package/host-next/app/components/SomaPageForbidden.tsx +0 -19
- package/host-next/app/favicon.ico/route.ts +0 -13
- package/host-next/app/layout.tsx +0 -27
- package/host-next/app/page.tsx +0 -9
- package/host-next/app/router-bridge.ts +0 -42
- package/host-next/app/soma/[...path]/route.ts +0 -9
- package/host-next/app/soma/_shared/host-prepare.ts +0 -33
- package/host-next/app/soma/_shared/page-http.ts +0 -86
- package/host-next/app/soma/page/route.ts +0 -36
- package/host-next/app/soma/route.ts +0 -9
- package/host-next/app/soma-runtime/[...file]/route.ts +0 -21
- package/host-next/app/soma-runtime/_shared/static-file.ts +0 -89
- package/host-next/bin/host-next.cjs +0 -60
- package/host-next/instrumentation.ts +0 -18
- package/host-next/next-env.d.ts +0 -7
- package/host-next/next.config.js +0 -410
- package/host-next/proxy.ts +0 -43
- package/host-next/src/workspace.ts +0 -543
|
@@ -5,33 +5,42 @@
|
|
|
5
5
|
// `scripts/verify-single-impl.mjs` 的 S1-d 对「别处再写一份源包判定」报红。
|
|
6
6
|
//
|
|
7
7
|
// 纪律(AD-34):这里只有 Host 与源包两种来源类别,不出现任何产生方的名字。
|
|
8
|
-
const fs = require(
|
|
9
|
-
const path = require(
|
|
8
|
+
const fs = require("node:fs");
|
|
9
|
+
const path = require("node:path");
|
|
10
10
|
|
|
11
11
|
/** @typedef {Readonly<Record<string, string | undefined>>} SomaEnv */
|
|
12
|
-
const { getSomaHostConfigRoot } = require(
|
|
12
|
+
const { getSomaHostConfigRoot } = require("./host-config-paths.cjs");
|
|
13
13
|
|
|
14
14
|
/** 环境变量:显式声明的源包名(逗号分隔),不论其 package.json 怎么写都当源包 */
|
|
15
|
-
const SOMA_SOURCE_PACKAGES_ENV =
|
|
15
|
+
const SOMA_SOURCE_PACKAGES_ENV = "SOMA_SOURCE_PACKAGES";
|
|
16
|
+
|
|
17
|
+
const MAX_ASSET_SOURCE_CACHE = 64;
|
|
18
|
+
/** @typedef {{ type: "host" | "package"; name: string; assetRoot: string; codeRoot: string; apiRoot: string; configRoot: string; packageName?: string }} SomaAssetSourceItem */
|
|
19
|
+
/** @type {Map<string, SomaAssetSourceItem[]>} */
|
|
20
|
+
const assetSourcesCache = new Map();
|
|
21
|
+
|
|
22
|
+
function clearSomaAssetSourcesCache() {
|
|
23
|
+
assetSourcesCache.clear();
|
|
24
|
+
}
|
|
16
25
|
|
|
17
26
|
function readJson(filePath) {
|
|
18
27
|
try {
|
|
19
28
|
if (!fs.existsSync(filePath)) return null;
|
|
20
|
-
return JSON.parse(fs.readFileSync(filePath,
|
|
29
|
+
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
21
30
|
} catch {
|
|
22
31
|
return null;
|
|
23
32
|
}
|
|
24
33
|
}
|
|
25
34
|
|
|
26
35
|
function packagePathInNodeModules(hostDir, packageName) {
|
|
27
|
-
return path.join(hostDir,
|
|
36
|
+
return path.join(hostDir, "node_modules", ...packageName.split("/"));
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
function resolvePackageRoot(hostDir, packageName, version) {
|
|
31
40
|
const nodeModulesPath = packagePathInNodeModules(hostDir, packageName);
|
|
32
41
|
if (fs.existsSync(nodeModulesPath)) return nodeModulesPath;
|
|
33
|
-
if (typeof version ===
|
|
34
|
-
const fileRoot = path.resolve(hostDir, version.slice(
|
|
42
|
+
if (typeof version === "string" && version.startsWith("file:")) {
|
|
43
|
+
const fileRoot = path.resolve(hostDir, version.slice("file:".length));
|
|
35
44
|
if (fs.existsSync(fileRoot)) return fileRoot;
|
|
36
45
|
}
|
|
37
46
|
return null;
|
|
@@ -39,14 +48,14 @@ function resolvePackageRoot(hostDir, packageName, version) {
|
|
|
39
48
|
|
|
40
49
|
/** @param {SomaEnv} [env] */
|
|
41
50
|
function readExplicitSourcePackages(env = process.env) {
|
|
42
|
-
return String(env[SOMA_SOURCE_PACKAGES_ENV] ??
|
|
43
|
-
.split(
|
|
51
|
+
return String(env?.[SOMA_SOURCE_PACKAGES_ENV] ?? "")
|
|
52
|
+
.split(",")
|
|
44
53
|
.map((item) => item.trim())
|
|
45
54
|
.filter(Boolean);
|
|
46
55
|
}
|
|
47
56
|
|
|
48
57
|
function readHostDependencies(hostDir) {
|
|
49
|
-
const packageJson = readJson(path.join(hostDir,
|
|
58
|
+
const packageJson = readJson(path.join(hostDir, "package.json"));
|
|
50
59
|
return {
|
|
51
60
|
...(packageJson?.dependencies ?? {}),
|
|
52
61
|
...(packageJson?.devDependencies ?? {}),
|
|
@@ -56,7 +65,7 @@ function readHostDependencies(hostDir) {
|
|
|
56
65
|
/** 子路径必须落在 parent 内且存在,否则回落 parent(越界不报错、不跟随) */
|
|
57
66
|
function resolveSafeChildRoot(parent, relativePath) {
|
|
58
67
|
const normalizedParent = path.resolve(parent);
|
|
59
|
-
const trimmed = typeof relativePath ===
|
|
68
|
+
const trimmed = typeof relativePath === "string" ? relativePath.trim() : "";
|
|
60
69
|
if (!trimmed) return normalizedParent;
|
|
61
70
|
const resolved = path.resolve(normalizedParent, trimmed);
|
|
62
71
|
if (resolved === normalizedParent || resolved.startsWith(`${normalizedParent}${path.sep}`)) {
|
|
@@ -65,29 +74,66 @@ function resolveSafeChildRoot(parent, relativePath) {
|
|
|
65
74
|
return normalizedParent;
|
|
66
75
|
}
|
|
67
76
|
|
|
68
|
-
|
|
77
|
+
/** 解析显式外部/同级/子目录路径(仅用于宿主自身 hostDir,第三方包不走此函数) */
|
|
78
|
+
function resolveExplicitHostPath(baseDir, rawPath, options = {}) {
|
|
79
|
+
if (typeof rawPath !== "string") return null;
|
|
80
|
+
const trimmed = rawPath.trim();
|
|
81
|
+
if (!trimmed || trimmed.includes("\0")) return null;
|
|
82
|
+
const resolved = path.resolve(baseDir, trimmed);
|
|
83
|
+
if (resolved === path.parse(resolved).root) return null;
|
|
84
|
+
if (options.requireExists && !fs.existsSync(resolved)) return null;
|
|
85
|
+
return resolved;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function getSomaHostSourceRoot(hostDir, env = process.env) {
|
|
69
89
|
const normalizedHostDir = path.resolve(hostDir);
|
|
70
|
-
|
|
90
|
+
if (env && typeof env.SOMA_WEB_DIR === "string" && env.SOMA_WEB_DIR.trim()) {
|
|
91
|
+
const explicit = resolveExplicitHostPath(normalizedHostDir, env.SOMA_WEB_DIR, { requireExists: false });
|
|
92
|
+
if (explicit) return explicit;
|
|
93
|
+
}
|
|
94
|
+
const packageJson = readJson(path.join(normalizedHostDir, "package.json"));
|
|
95
|
+
if (typeof packageJson?.soma?.webRoot === "string" && packageJson.soma.webRoot.trim()) {
|
|
96
|
+
const explicit = resolveExplicitHostPath(normalizedHostDir, packageJson.soma.webRoot, { requireExists: true });
|
|
97
|
+
if (explicit) return explicit;
|
|
98
|
+
}
|
|
71
99
|
return resolveSafeChildRoot(normalizedHostDir, packageJson?.soma?.hostRoot);
|
|
72
100
|
}
|
|
73
101
|
|
|
102
|
+
function getSomaHostApiRoot(hostDir, env = process.env) {
|
|
103
|
+
const normalizedHostDir = path.resolve(hostDir);
|
|
104
|
+
if (env && typeof env.SOMA_API_DIR === "string" && env.SOMA_API_DIR.trim()) {
|
|
105
|
+
const explicit = resolveExplicitHostPath(normalizedHostDir, env.SOMA_API_DIR, { requireExists: false });
|
|
106
|
+
if (explicit) return explicit;
|
|
107
|
+
}
|
|
108
|
+
const packageJson = readJson(path.join(normalizedHostDir, "package.json"));
|
|
109
|
+
const candidate = typeof packageJson?.soma?.apiRoot === "string" && packageJson.soma.apiRoot.trim()
|
|
110
|
+
? packageJson.soma.apiRoot
|
|
111
|
+
: (typeof packageJson?.soma?.serviceRoot === "string" && packageJson.soma.serviceRoot.trim() ? packageJson.soma.serviceRoot : null);
|
|
112
|
+
if (candidate) {
|
|
113
|
+
const explicit = resolveExplicitHostPath(normalizedHostDir, candidate, { requireExists: true });
|
|
114
|
+
if (explicit) return explicit;
|
|
115
|
+
}
|
|
116
|
+
return getSomaHostSourceRoot(normalizedHostDir, env);
|
|
117
|
+
}
|
|
118
|
+
|
|
74
119
|
/** 源包判定:显式声明(环境变量)或 `package.json` 的 `soma.source === true` */
|
|
75
120
|
function isSomaSourcePackage(packageRoot, explicit) {
|
|
76
121
|
if (explicit) return true;
|
|
77
|
-
const packageJson = readJson(path.join(packageRoot,
|
|
122
|
+
const packageJson = readJson(path.join(packageRoot, "package.json"));
|
|
78
123
|
return packageJson?.soma?.source === true;
|
|
79
124
|
}
|
|
80
125
|
|
|
81
126
|
function toPackageSource(packageName, packageRoot) {
|
|
82
|
-
const packageJson = readJson(path.join(packageRoot,
|
|
127
|
+
const packageJson = readJson(path.join(packageRoot, "package.json"));
|
|
83
128
|
const codeRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.hostRoot);
|
|
84
129
|
const configRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.configRoot);
|
|
85
130
|
return {
|
|
86
|
-
type:
|
|
131
|
+
type: "package",
|
|
87
132
|
name: packageName,
|
|
88
133
|
packageName,
|
|
89
134
|
assetRoot: codeRoot,
|
|
90
135
|
codeRoot,
|
|
136
|
+
apiRoot: codeRoot,
|
|
91
137
|
configRoot,
|
|
92
138
|
};
|
|
93
139
|
}
|
|
@@ -96,17 +142,29 @@ function toPackageSource(packageName, packageRoot) {
|
|
|
96
142
|
/** @param {SomaEnv} [env] */
|
|
97
143
|
function getSomaAssetSources(hostDir, env = process.env) {
|
|
98
144
|
const normalizedHostDir = path.resolve(hostDir);
|
|
99
|
-
|
|
145
|
+
|
|
146
|
+
const pkgStat = fs.statSync(path.join(normalizedHostDir, "package.json"), { throwIfNoEntry: false });
|
|
147
|
+
const pkgStatKey = pkgStat ? `${pkgStat.mtimeMs}:${pkgStat.size}` : "none";
|
|
148
|
+
const cacheKey = `${normalizedHostDir}\0${env?.[SOMA_SOURCE_PACKAGES_ENV] ?? ""}\0${env?.SOMA_WEB_DIR ?? ""}\0${env?.SOMA_API_DIR ?? ""}\0${pkgStatKey}`;
|
|
149
|
+
|
|
150
|
+
const cached = assetSourcesCache.get(cacheKey);
|
|
151
|
+
if (cached) {
|
|
152
|
+
return cached.slice();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const hostSourceRoot = getSomaHostSourceRoot(normalizedHostDir, env);
|
|
156
|
+
const hostApiRoot = getSomaHostApiRoot(normalizedHostDir, env);
|
|
100
157
|
const dependencies = readHostDependencies(normalizedHostDir);
|
|
101
158
|
const explicitPackages = new Set(readExplicitSourcePackages(env));
|
|
102
159
|
const packageNames = new Set([...Object.keys(dependencies), ...explicitPackages]);
|
|
103
160
|
|
|
104
161
|
const sources = [
|
|
105
162
|
{
|
|
106
|
-
type:
|
|
107
|
-
name:
|
|
163
|
+
type: "host",
|
|
164
|
+
name: "host",
|
|
108
165
|
assetRoot: hostSourceRoot,
|
|
109
166
|
codeRoot: hostSourceRoot,
|
|
167
|
+
apiRoot: hostApiRoot,
|
|
110
168
|
configRoot: getSomaHostConfigRoot(normalizedHostDir),
|
|
111
169
|
},
|
|
112
170
|
];
|
|
@@ -116,32 +174,38 @@ function getSomaAssetSources(hostDir, env = process.env) {
|
|
|
116
174
|
if (!packageRoot || !isSomaSourcePackage(packageRoot, explicitPackages.has(packageName))) continue;
|
|
117
175
|
sources.push(toPackageSource(packageName, packageRoot));
|
|
118
176
|
}
|
|
119
|
-
|
|
177
|
+
|
|
178
|
+
if (assetSourcesCache.size >= MAX_ASSET_SOURCE_CACHE) {
|
|
179
|
+
assetSourcesCache.delete(assetSourcesCache.keys().next().value);
|
|
180
|
+
}
|
|
181
|
+
assetSourcesCache.set(cacheKey, sources);
|
|
182
|
+
|
|
183
|
+
return sources.slice();
|
|
120
184
|
}
|
|
121
185
|
|
|
122
186
|
/** 源包名清单(next.config 的 transpilePackages 用) */
|
|
123
187
|
/** @param {SomaEnv} [env] */
|
|
124
188
|
function listSomaSourcePackageNames(hostDir, env = process.env) {
|
|
125
189
|
return getSomaAssetSources(hostDir, env)
|
|
126
|
-
.filter((source) => source.type ===
|
|
190
|
+
.filter((source) => source.type === "package")
|
|
127
191
|
.map((source) => source.packageName);
|
|
128
192
|
}
|
|
129
193
|
|
|
130
194
|
/** 覆盖顺序:源包在前、Host 在后(同名页面与片段由 Host 覆盖) */
|
|
131
195
|
function orderSourcesForOverride(sources) {
|
|
132
196
|
return [
|
|
133
|
-
...sources.filter((source) => source.type ===
|
|
134
|
-
...sources.filter((source) => source.type ===
|
|
197
|
+
...sources.filter((source) => source.type === "package"),
|
|
198
|
+
...sources.filter((source) => source.type === "host"),
|
|
135
199
|
];
|
|
136
200
|
}
|
|
137
201
|
|
|
138
|
-
function collectHtmlFiles(dir, prefix =
|
|
202
|
+
function collectHtmlFiles(dir, prefix = "", acc = []) {
|
|
139
203
|
if (!fs.existsSync(dir)) return acc;
|
|
140
204
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
141
205
|
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
142
206
|
const full = path.join(dir, entry.name);
|
|
143
207
|
if (entry.isDirectory()) collectHtmlFiles(full, rel, acc);
|
|
144
|
-
else if (entry.name.endsWith(
|
|
208
|
+
else if (entry.name.endsWith(".html")) acc.push(rel);
|
|
145
209
|
}
|
|
146
210
|
return acc;
|
|
147
211
|
}
|
|
@@ -150,7 +214,7 @@ function collectHtmlFiles(dir, prefix = '', acc = []) {
|
|
|
150
214
|
function collectPageEntries(sources) {
|
|
151
215
|
const byHtmlFile = new Map();
|
|
152
216
|
for (const source of orderSourcesForOverride(sources)) {
|
|
153
|
-
const htmlRoot = path.join(source.assetRoot,
|
|
217
|
+
const htmlRoot = path.join(source.assetRoot, "html");
|
|
154
218
|
for (const htmlFile of collectHtmlFiles(htmlRoot).sort()) {
|
|
155
219
|
byHtmlFile.set(htmlFile, { source, htmlFile });
|
|
156
220
|
}
|
|
@@ -162,11 +226,11 @@ function collectPageEntries(sources) {
|
|
|
162
226
|
function collectIncludes(sources) {
|
|
163
227
|
const includes = {};
|
|
164
228
|
for (const source of orderSourcesForOverride(sources)) {
|
|
165
|
-
const sharedDir = path.join(source.assetRoot,
|
|
229
|
+
const sharedDir = path.join(source.assetRoot, "html", "shared");
|
|
166
230
|
if (!fs.existsSync(sharedDir)) continue;
|
|
167
231
|
for (const entry of fs.readdirSync(sharedDir, { withFileTypes: true })) {
|
|
168
|
-
if (!entry.isFile() || !entry.name.endsWith(
|
|
169
|
-
includes[`shared/${entry.name}`] = fs.readFileSync(path.join(sharedDir, entry.name),
|
|
232
|
+
if (!entry.isFile() || !entry.name.endsWith(".html")) continue;
|
|
233
|
+
includes[`shared/${entry.name}`] = fs.readFileSync(path.join(sharedDir, entry.name), "utf8");
|
|
170
234
|
}
|
|
171
235
|
}
|
|
172
236
|
return includes;
|
|
@@ -175,6 +239,8 @@ function collectIncludes(sources) {
|
|
|
175
239
|
module.exports = {
|
|
176
240
|
SOMA_SOURCE_PACKAGES_ENV,
|
|
177
241
|
getSomaHostSourceRoot,
|
|
242
|
+
getSomaHostApiRoot,
|
|
243
|
+
clearSomaAssetSourcesCache,
|
|
178
244
|
getSomaAssetSources,
|
|
179
245
|
listSomaSourcePackageNames,
|
|
180
246
|
orderSourcesForOverride,
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import * as impl from
|
|
1
|
+
import * as impl from "./source-packages.cjs";
|
|
2
2
|
|
|
3
3
|
export interface SomaAssetSource {
|
|
4
|
-
type:
|
|
4
|
+
type: "host" | "package";
|
|
5
5
|
name: string;
|
|
6
6
|
assetRoot: string;
|
|
7
7
|
codeRoot: string;
|
|
8
|
+
apiRoot: string;
|
|
8
9
|
configRoot: string;
|
|
9
10
|
packageName?: string;
|
|
10
11
|
}
|
|
@@ -16,11 +17,17 @@ export {
|
|
|
16
17
|
resolveAppConfigPath,
|
|
17
18
|
resolveHostAppConfigPath,
|
|
18
19
|
resolveHostConfigRoot,
|
|
19
|
-
} from
|
|
20
|
-
export type { SomaEnvName } from
|
|
20
|
+
} from "./host-config-paths";
|
|
21
|
+
export type { SomaEnvName } from "./host-config-paths";
|
|
21
22
|
|
|
22
23
|
/** 实现只有 `source-packages.cjs` 一份(RT3 单实现);这里只补类型再导出,不另写第二份。 */
|
|
23
|
-
export const getSomaHostSourceRoot: (hostDir: string) => string =
|
|
24
|
+
export const getSomaHostSourceRoot: (hostDir: string, env?: Readonly<Record<string, string | undefined>>) => string =
|
|
25
|
+
impl.getSomaHostSourceRoot;
|
|
26
|
+
|
|
27
|
+
export const getSomaHostApiRoot: (hostDir: string, env?: Readonly<Record<string, string | undefined>>) => string =
|
|
28
|
+
impl.getSomaHostApiRoot;
|
|
29
|
+
|
|
30
|
+
export const clearSomaAssetSourcesCache: () => void = impl.clearSomaAssetSourcesCache;
|
|
24
31
|
|
|
25
32
|
/** Host 自己 + 全部源包;Host 恒在第一位 */
|
|
26
33
|
export const getSomaAssetSources: (hostDir: string, env?: Readonly<Record<string, string | undefined>>) => SomaAssetSource[] =
|
package/core/server/index.ts
CHANGED
|
@@ -7,25 +7,45 @@
|
|
|
7
7
|
* Public Host imports should keep using `soma-core` / `soma-core/base` for page code.
|
|
8
8
|
* This entry is additive and optional.
|
|
9
9
|
*/
|
|
10
|
-
import
|
|
10
|
+
import "server-only";
|
|
11
11
|
|
|
12
|
-
export { ServerFileCache } from
|
|
12
|
+
export { ServerFileCache } from "./infra/cache/ServerFileCache";
|
|
13
13
|
export {
|
|
14
14
|
withSomaServerRequestContext,
|
|
15
15
|
type SomaServerRequestContextOptions,
|
|
16
|
-
} from
|
|
16
|
+
} from "./infra/http/soma-server-request-context";
|
|
17
17
|
|
|
18
|
-
export type { IService, SomaServiceRequest } from
|
|
19
|
-
export {
|
|
20
|
-
|
|
18
|
+
export type { IService, SomaServiceRequest } from "./api/IService";
|
|
19
|
+
export {
|
|
20
|
+
clearHostServiceCache,
|
|
21
|
+
getAotServiceResolver,
|
|
22
|
+
loadHostService,
|
|
23
|
+
registerAotServiceResolver,
|
|
24
|
+
resolveServiceExport,
|
|
25
|
+
type AotServiceResolver,
|
|
26
|
+
} from "./api/load-host-service";
|
|
27
|
+
export {
|
|
28
|
+
resolveHostServiceCandidate,
|
|
29
|
+
resolveServiceFilePath,
|
|
30
|
+
toPascalCase,
|
|
31
|
+
validateServiceSegments,
|
|
32
|
+
type ResolveHostServiceCandidateOptions,
|
|
33
|
+
} from "./api/service-path";
|
|
34
|
+
export {
|
|
35
|
+
dispatchHostServiceApiRequest,
|
|
36
|
+
polyfillServiceRequest,
|
|
37
|
+
type DispatchHostServiceApiOptions,
|
|
38
|
+
type RequestCookie,
|
|
39
|
+
type RequestCookiesAdapter,
|
|
40
|
+
} from "./api/dispatch-service-api";
|
|
21
41
|
|
|
22
|
-
export { invokeHostRequestHook } from
|
|
42
|
+
export { invokeHostRequestHook } from "./host/invoke-host-request-hook";
|
|
23
43
|
export {
|
|
24
44
|
resolveHostRequestHook,
|
|
25
45
|
type SomaHostRequestContext,
|
|
26
46
|
type SomaHostRequestKind,
|
|
27
47
|
type SomaRequestHook,
|
|
28
|
-
} from
|
|
48
|
+
} from "./host/resolve-host-request-hook";
|
|
29
49
|
export {
|
|
30
50
|
createSomaCspHeader,
|
|
31
51
|
createSomaCspNonce,
|
|
@@ -43,27 +63,29 @@ export {
|
|
|
43
63
|
type SomaCspMode,
|
|
44
64
|
type SomaCspOptions,
|
|
45
65
|
type SomaCspHeader,
|
|
46
|
-
} from
|
|
66
|
+
} from "./host/content-security-policy";
|
|
47
67
|
export {
|
|
48
68
|
readSomaCspNonce,
|
|
49
69
|
somaCspNonceMetadata,
|
|
50
70
|
type SomaCspHeaderBag,
|
|
51
|
-
} from
|
|
52
|
-
export { loadHostAssembly, registerShellRoutes, resolveHostLoginPath, SOMA_HOST_ASSEMBLY_FILE } from
|
|
53
|
-
export type { SomaHostAssembly } from
|
|
54
|
-
export { loadPageMarkupConfig, renderColorSchemeMeta } from
|
|
55
|
-
export type { SomaPageMarkup } from
|
|
71
|
+
} from "./host/csp-nonce-metadata";
|
|
72
|
+
export { loadHostAssembly, registerShellRoutes, resolveHostLoginPath, SOMA_HOST_ASSEMBLY_FILE } from "./host/host-assembly";
|
|
73
|
+
export type { SomaHostAssembly } from "../shared/types/host-assembly.types";
|
|
74
|
+
export { loadPageMarkupConfig, renderColorSchemeMeta } from "./host/page-markup-config";
|
|
75
|
+
export type { SomaPageMarkup } from "../shared/types/component-tree.types";
|
|
56
76
|
export {
|
|
57
77
|
loadSomaServerPageAssets,
|
|
58
78
|
resolveSomaPageSource,
|
|
59
79
|
SomaPageAssetError,
|
|
60
80
|
type SomaServerPageAssets,
|
|
61
|
-
} from
|
|
81
|
+
} from "./host/server-page-assets";
|
|
62
82
|
// 宿主资源定位面(《core 公开面补齐表》§2.2):explain / testkit 静态出口按宿主目录找页面源,Host 路由取虚拟页资产
|
|
63
83
|
export {
|
|
84
|
+
clearSomaAssetSourcesCache,
|
|
64
85
|
getSomaAssetSources,
|
|
86
|
+
getSomaHostApiRoot,
|
|
65
87
|
getSomaHostSourceRoot,
|
|
66
88
|
type SomaAssetSource,
|
|
67
|
-
} from
|
|
89
|
+
} from "./host/source-packages";
|
|
68
90
|
// runtime 空间(RT2 / E2):动态注入与地址绑定、按地址取页。
|
|
69
|
-
export * from
|
|
91
|
+
export * from "./runtime-space";
|
|
@@ -1,29 +1,65 @@
|
|
|
1
1
|
import { readFileSync, readdirSync, watch, type FSWatcher } from 'fs';
|
|
2
|
-
import { resolve, extname, normalize } from 'path';
|
|
3
|
-
|
|
2
|
+
import { resolve, extname, normalize, sep } from 'path';
|
|
3
|
+
|
|
4
|
+
export interface ServerFileCacheOptions {
|
|
5
|
+
maxFiles?: number;
|
|
6
|
+
maxFileBytes?: number;
|
|
7
|
+
maxDirs?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface FileEntry {
|
|
11
|
+
content: string;
|
|
12
|
+
bytes: number;
|
|
13
|
+
}
|
|
4
14
|
|
|
5
15
|
/**
|
|
6
|
-
*
|
|
16
|
+
* 服务端文件缓存单例(有界 LRU + 监听去重)
|
|
7
17
|
*
|
|
8
18
|
* 将磁盘文件内容和目录列表缓存到内存中,避免每次请求都执行 I/O。
|
|
9
|
-
*
|
|
19
|
+
* 支持有界文件数、总字节上限与目录缓存上限;支持 fs.watch 监听去重,自动失效对应缓存条目。
|
|
10
20
|
*/
|
|
11
21
|
export class ServerFileCache {
|
|
12
22
|
private static instance: ServerFileCache | null = null;
|
|
13
23
|
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
readonly maxFiles: number;
|
|
25
|
+
readonly maxFileBytes: number;
|
|
26
|
+
readonly maxDirs: number;
|
|
27
|
+
|
|
28
|
+
private fileCache = new Map<string, FileEntry>();
|
|
29
|
+
private dirCache = new Map<string, string[]>();
|
|
16
30
|
private watchers: FSWatcher[] = [];
|
|
31
|
+
private watchedRoots = new Set<string>();
|
|
32
|
+
private currentFileBytes = 0;
|
|
17
33
|
|
|
18
|
-
|
|
34
|
+
constructor(options: ServerFileCacheOptions = {}) {
|
|
35
|
+
this.maxFiles = options.maxFiles ?? 512;
|
|
36
|
+
this.maxFileBytes = options.maxFileBytes ?? 33_554_432;
|
|
37
|
+
this.maxDirs = options.maxDirs ?? 256;
|
|
38
|
+
}
|
|
19
39
|
|
|
20
|
-
static getInstance(): ServerFileCache {
|
|
40
|
+
static getInstance(options?: ServerFileCacheOptions): ServerFileCache {
|
|
21
41
|
if (!ServerFileCache.instance) {
|
|
22
|
-
ServerFileCache.instance = new ServerFileCache();
|
|
42
|
+
ServerFileCache.instance = new ServerFileCache(options);
|
|
23
43
|
}
|
|
24
44
|
return ServerFileCache.instance;
|
|
25
45
|
}
|
|
26
46
|
|
|
47
|
+
get size(): number {
|
|
48
|
+
return this.fileCache.size;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get fileBytes(): number {
|
|
52
|
+
return this.currentFileBytes;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get dirSize(): number {
|
|
56
|
+
return this.dirCache.size;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get watchersCount(): number {
|
|
60
|
+
return this.watchers.length;
|
|
61
|
+
}
|
|
62
|
+
|
|
27
63
|
/**
|
|
28
64
|
* 读取文件内容,命中缓存直接返回,未命中则读磁盘并缓存。
|
|
29
65
|
*/
|
|
@@ -31,11 +67,39 @@ export class ServerFileCache {
|
|
|
31
67
|
const key = normalize(filePath);
|
|
32
68
|
const cached = this.fileCache.get(key);
|
|
33
69
|
if (cached !== undefined) {
|
|
34
|
-
|
|
70
|
+
this.fileCache.delete(key);
|
|
71
|
+
this.fileCache.set(key, cached);
|
|
72
|
+
return cached.content;
|
|
35
73
|
}
|
|
36
74
|
|
|
37
75
|
const content = readFileSync(filePath, 'utf-8');
|
|
38
|
-
|
|
76
|
+
const bytes = Buffer.byteLength(content, 'utf8');
|
|
77
|
+
|
|
78
|
+
if (bytes > this.maxFileBytes / 4) {
|
|
79
|
+
return content;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (this.fileCache.has(key)) {
|
|
83
|
+
const old = this.fileCache.get(key)!;
|
|
84
|
+
this.currentFileBytes -= old.bytes;
|
|
85
|
+
this.fileCache.delete(key);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
while (
|
|
89
|
+
(this.fileCache.size >= this.maxFiles || this.currentFileBytes + bytes > this.maxFileBytes) &&
|
|
90
|
+
this.fileCache.size > 0
|
|
91
|
+
) {
|
|
92
|
+
const oldestKey = this.fileCache.keys().next().value;
|
|
93
|
+
if (oldestKey === undefined) break;
|
|
94
|
+
const oldestEntry = this.fileCache.get(oldestKey);
|
|
95
|
+
if (oldestEntry) {
|
|
96
|
+
this.currentFileBytes -= oldestEntry.bytes;
|
|
97
|
+
}
|
|
98
|
+
this.fileCache.delete(oldestKey);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.fileCache.set(key, { content, bytes });
|
|
102
|
+
this.currentFileBytes += bytes;
|
|
39
103
|
return content;
|
|
40
104
|
}
|
|
41
105
|
|
|
@@ -58,14 +122,16 @@ export class ServerFileCache {
|
|
|
58
122
|
const dirKey = `${normalize(dirPath)}:${ext}`;
|
|
59
123
|
const cached = this.dirCache.get(dirKey);
|
|
60
124
|
if (cached !== undefined) {
|
|
125
|
+
this.dirCache.delete(dirKey);
|
|
126
|
+
this.dirCache.set(dirKey, cached);
|
|
61
127
|
return cached;
|
|
62
128
|
}
|
|
63
129
|
|
|
64
130
|
const files = readdirSync(dirPath)
|
|
65
|
-
.filter(f => extname(f) === ext)
|
|
66
|
-
.map(f => this.readFile(resolve(dirPath, f)));
|
|
131
|
+
.filter((f) => extname(f) === ext)
|
|
132
|
+
.map((f) => this.readFile(resolve(dirPath, f)));
|
|
67
133
|
|
|
68
|
-
this.
|
|
134
|
+
this.setDirCache(dirKey, files);
|
|
69
135
|
return files;
|
|
70
136
|
}
|
|
71
137
|
|
|
@@ -73,32 +139,59 @@ export class ServerFileCache {
|
|
|
73
139
|
const dirKey = `${normalize(dirPath)}:names:${ext}`;
|
|
74
140
|
const cached = this.dirCache.get(dirKey);
|
|
75
141
|
if (cached !== undefined) {
|
|
142
|
+
this.dirCache.delete(dirKey);
|
|
143
|
+
this.dirCache.set(dirKey, cached);
|
|
76
144
|
return cached;
|
|
77
145
|
}
|
|
78
146
|
|
|
79
|
-
const names = readdirSync(dirPath).filter(f => extname(f) === ext);
|
|
80
|
-
this.
|
|
147
|
+
const names = readdirSync(dirPath).filter((f) => extname(f) === ext);
|
|
148
|
+
this.setDirCache(dirKey, names);
|
|
81
149
|
return names;
|
|
82
150
|
}
|
|
83
151
|
|
|
152
|
+
private setDirCache(key: string, list: string[]): void {
|
|
153
|
+
if (this.dirCache.has(key)) {
|
|
154
|
+
this.dirCache.delete(key);
|
|
155
|
+
}
|
|
156
|
+
while (this.dirCache.size >= this.maxDirs && this.dirCache.size > 0) {
|
|
157
|
+
const oldestKey = this.dirCache.keys().next().value;
|
|
158
|
+
if (oldestKey === undefined) break;
|
|
159
|
+
this.dirCache.delete(oldestKey);
|
|
160
|
+
}
|
|
161
|
+
this.dirCache.set(key, list);
|
|
162
|
+
}
|
|
163
|
+
|
|
84
164
|
/**
|
|
85
165
|
* 监听目录变化,自动失效缓存。
|
|
86
|
-
* 使用 fs.watch
|
|
166
|
+
* 使用 fs.watch 递归监听;已监听同一目录或其祖先目录时自动去重。
|
|
87
167
|
*/
|
|
88
168
|
watchAndInvalidate(rootDir: string): void {
|
|
169
|
+
const resolvedRoot = resolve(rootDir);
|
|
170
|
+
for (const watched of this.watchedRoots) {
|
|
171
|
+
if (
|
|
172
|
+
resolvedRoot === watched ||
|
|
173
|
+
resolvedRoot.startsWith(watched.endsWith(sep) ? watched : `${watched}${sep}`)
|
|
174
|
+
) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
89
179
|
try {
|
|
90
|
-
const watcher = watch(
|
|
180
|
+
const watcher = watch(resolvedRoot, { recursive: true }, (_event, filename) => {
|
|
91
181
|
if (!filename) return;
|
|
92
182
|
|
|
93
|
-
const fullPath = normalize(resolve(
|
|
94
|
-
this.fileCache.
|
|
183
|
+
const fullPath = normalize(resolve(resolvedRoot, filename));
|
|
184
|
+
const entry = this.fileCache.get(fullPath);
|
|
185
|
+
if (entry !== undefined) {
|
|
186
|
+
this.currentFileBytes = Math.max(0, this.currentFileBytes - entry.bytes);
|
|
187
|
+
this.fileCache.delete(fullPath);
|
|
188
|
+
}
|
|
95
189
|
this.dirCache.clear();
|
|
96
190
|
});
|
|
97
191
|
|
|
98
192
|
this.watchers.push(watcher);
|
|
193
|
+
this.watchedRoots.add(resolvedRoot);
|
|
99
194
|
} catch (e: any) {
|
|
100
|
-
// fs.watch recursive is not supported on Linux — cache won't auto-invalidate.
|
|
101
|
-
// Changes require a server restart in that environment.
|
|
102
195
|
if (process.env.NODE_ENV !== 'production') {
|
|
103
196
|
console.warn(
|
|
104
197
|
`[soma-core][缓存] fs.watch recursive failed (${e.code ?? e.message}). ` +
|
|
@@ -109,15 +202,35 @@ export class ServerFileCache {
|
|
|
109
202
|
}
|
|
110
203
|
|
|
111
204
|
/**
|
|
112
|
-
*
|
|
205
|
+
* 清除所有缓存。
|
|
113
206
|
*/
|
|
114
|
-
|
|
207
|
+
clear(): void {
|
|
115
208
|
this.fileCache.clear();
|
|
116
209
|
this.dirCache.clear();
|
|
210
|
+
this.currentFileBytes = 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* 使所有缓存失效。
|
|
215
|
+
*/
|
|
216
|
+
invalidate(): void {
|
|
217
|
+
this.clear();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 清除所有缓存并关闭 watcher。
|
|
222
|
+
*/
|
|
223
|
+
dispose(): void {
|
|
224
|
+
this.clear();
|
|
117
225
|
for (const w of this.watchers) {
|
|
118
|
-
|
|
226
|
+
try {
|
|
227
|
+
w.close();
|
|
228
|
+
} catch {
|
|
229
|
+
// ignore
|
|
230
|
+
}
|
|
119
231
|
}
|
|
120
232
|
this.watchers = [];
|
|
233
|
+
this.watchedRoots.clear();
|
|
121
234
|
ServerFileCache.instance = null;
|
|
122
235
|
}
|
|
123
236
|
}
|