soma-core 2.2.1 → 2.2.2
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/server/api/service-path.ts +5 -1
- package/dist/host-native/aot-builder.cjs +3 -2
- package/dist/host-native/native-server.cjs +20 -8
- package/host-native/bin/host-native.cjs +0 -0
- package/host-native/client/browser-entry.ts +4 -0
- package/host-native/src/aot-builder.ts +3 -2
- package/host-native/src/csp-headers.ts +9 -3
- package/host-native/src/native-server.ts +17 -5
- package/package.json +1 -1
|
@@ -81,11 +81,15 @@ export function resolveHostServiceCandidate(
|
|
|
81
81
|
const extensions = options?.extensions ?? DEFAULT_SERVICE_EXTENSIONS;
|
|
82
82
|
const { dirSegments, className } = validated;
|
|
83
83
|
|
|
84
|
+
const hasLocalServiceSubdir =
|
|
85
|
+
fs.statSync(join(normalizedApiRoot, "api", "service"), { throwIfNoEntry: false })?.isDirectory() === true ||
|
|
86
|
+
fs.statSync(join(normalizedApiRoot, "service"), { throwIfNoEntry: false })?.isDirectory() === true;
|
|
87
|
+
|
|
84
88
|
const candidateBases = [
|
|
85
89
|
join(normalizedApiRoot, "api", "service"),
|
|
86
90
|
join(normalizedApiRoot, "..", "api", "service"),
|
|
87
91
|
join(normalizedApiRoot, "service"),
|
|
88
|
-
...(options?.allowDirectRoot ? [normalizedApiRoot] : []),
|
|
92
|
+
...(options?.allowDirectRoot && !hasLocalServiceSubdir ? [normalizedApiRoot] : []),
|
|
89
93
|
];
|
|
90
94
|
|
|
91
95
|
for (const base of candidateBases) {
|
|
@@ -631,7 +631,7 @@ async function buildBrowserClient(options) {
|
|
|
631
631
|
minify: !options.dev,
|
|
632
632
|
sourcemap: options.dev ? "inline" : false,
|
|
633
633
|
entryNames: options.dev ? "[name]" : "[name]-[hash]",
|
|
634
|
-
chunkNames:
|
|
634
|
+
chunkNames: "chunks/[name]-[hash]",
|
|
635
635
|
nodePaths: [import_node_path.default.join(webRoot, "node_modules"), import_node_path.default.join(hostDir, "node_modules"), import_node_path.default.join(coreRoot, "node_modules")].filter((d) => import_node_fs.default.existsSync(d)),
|
|
636
636
|
plugins: [plugin],
|
|
637
637
|
define: {
|
|
@@ -811,8 +811,9 @@ function scanServiceFiles(apiRoot, webRoot) {
|
|
|
811
811
|
baseCandidates.push({ base: apiRoot, isDirect: true });
|
|
812
812
|
}
|
|
813
813
|
for (const { base, isDirect } of baseCandidates) {
|
|
814
|
-
if (!import_node_fs.default.existsSync(base)) continue;
|
|
814
|
+
if (!import_node_fs.default.existsSync(base) || !import_node_fs.default.statSync(base).isDirectory()) continue;
|
|
815
815
|
walk(base, base, isDirect);
|
|
816
|
+
break;
|
|
816
817
|
}
|
|
817
818
|
function walk(currentDir, baseDir, isDirect) {
|
|
818
819
|
let entries = [];
|
|
@@ -13743,11 +13743,12 @@ function resolveHostServiceCandidate(apiRoot, segments, options) {
|
|
|
13743
13743
|
const normalizedApiRoot = (0, import_node_path14.resolve)(apiRoot);
|
|
13744
13744
|
const extensions = options?.extensions ?? DEFAULT_SERVICE_EXTENSIONS;
|
|
13745
13745
|
const { dirSegments, className } = validated;
|
|
13746
|
+
const hasLocalServiceSubdir = import_node_fs12.default.statSync((0, import_node_path14.join)(normalizedApiRoot, "api", "service"), { throwIfNoEntry: false })?.isDirectory() === true || import_node_fs12.default.statSync((0, import_node_path14.join)(normalizedApiRoot, "service"), { throwIfNoEntry: false })?.isDirectory() === true;
|
|
13746
13747
|
const candidateBases = [
|
|
13747
13748
|
(0, import_node_path14.join)(normalizedApiRoot, "api", "service"),
|
|
13748
13749
|
(0, import_node_path14.join)(normalizedApiRoot, "..", "api", "service"),
|
|
13749
13750
|
(0, import_node_path14.join)(normalizedApiRoot, "service"),
|
|
13750
|
-
...options?.allowDirectRoot ? [normalizedApiRoot] : []
|
|
13751
|
+
...options?.allowDirectRoot && !hasLocalServiceSubdir ? [normalizedApiRoot] : []
|
|
13751
13752
|
];
|
|
13752
13753
|
for (const base of candidateBases) {
|
|
13753
13754
|
const normalizedAllowedBase = (0, import_node_path14.resolve)(base);
|
|
@@ -14746,9 +14747,14 @@ async function somaCspNonceMetadataAsync(readHeaders, env = process.env) {
|
|
|
14746
14747
|
|
|
14747
14748
|
// host-native/src/csp-headers.ts
|
|
14748
14749
|
async function applyHostAndCspHeaders(response, options, env = process.env) {
|
|
14750
|
+
const mutable = new Response(response.body, {
|
|
14751
|
+
status: response.status,
|
|
14752
|
+
statusText: response.statusText,
|
|
14753
|
+
headers: new Headers(response.headers)
|
|
14754
|
+
});
|
|
14749
14755
|
const csp = createSomaCspHeader(resolveSomaCspOptionsFromEnv(env));
|
|
14750
14756
|
if (csp) {
|
|
14751
|
-
|
|
14757
|
+
mutable.headers.set(csp.name, csp.value);
|
|
14752
14758
|
}
|
|
14753
14759
|
const extra = await resolveHostResponseHeaders({
|
|
14754
14760
|
method: options.method,
|
|
@@ -14756,10 +14762,10 @@ async function applyHostAndCspHeaders(response, options, env = process.env) {
|
|
|
14756
14762
|
});
|
|
14757
14763
|
if (extra !== void 0) {
|
|
14758
14764
|
for (const [name, value2] of Object.entries(extra)) {
|
|
14759
|
-
|
|
14765
|
+
mutable.headers.set(name, value2);
|
|
14760
14766
|
}
|
|
14761
14767
|
}
|
|
14762
|
-
return
|
|
14768
|
+
return mutable;
|
|
14763
14769
|
}
|
|
14764
14770
|
async function readCspNonceMeta(nonce, env = process.env) {
|
|
14765
14771
|
const meta = await somaCspNonceMetadataAsync(async () => ({
|
|
@@ -15035,7 +15041,7 @@ data: ${data || ""}
|
|
|
15035
15041
|
res.end("Not found");
|
|
15036
15042
|
return;
|
|
15037
15043
|
}
|
|
15038
|
-
const isHashed = /[-.][0-9a-f]{8,}\.[a-z0-9]
|
|
15044
|
+
const isHashed = /[-.](?:[0-9a-f]{8,}|[0-9A-Z]{8})\.[a-z0-9]+$/.test(subfile);
|
|
15039
15045
|
const etag = `W/"${(0, import_node_crypto9.createHash)("sha1").update(`${stat.size}:${stat.mtimeMs}`).digest("hex").slice(0, 20)}"`;
|
|
15040
15046
|
const ifNoneMatch = req.headers["if-none-match"];
|
|
15041
15047
|
if (ifNoneMatch && ifNoneMatch.includes(etag)) {
|
|
@@ -15176,9 +15182,15 @@ data: ${data || ""}
|
|
|
15176
15182
|
});
|
|
15177
15183
|
if (rejected) {
|
|
15178
15184
|
if (rejected.status >= 300 && rejected.status < 400 && rejected.headers.get("location")) {
|
|
15179
|
-
|
|
15180
|
-
|
|
15181
|
-
|
|
15185
|
+
const hooked = await applyHostAndCspHeaders(rejected, { method, pathname }, env);
|
|
15186
|
+
await sendWebResponse(hooked, res);
|
|
15187
|
+
return;
|
|
15188
|
+
}
|
|
15189
|
+
const rejectedContentType = (rejected.headers.get("content-type") || "").toLowerCase();
|
|
15190
|
+
const acceptHeader = (typeof req.headers["accept"] === "string" ? req.headers["accept"] : "").toLowerCase();
|
|
15191
|
+
if (dev && rejectedContentType.includes("application/json") && (rejected.ok || !acceptHeader.includes("text/html"))) {
|
|
15192
|
+
const hooked = await applyHostAndCspHeaders(rejected, { method, pathname }, env);
|
|
15193
|
+
await sendWebResponse(hooked, res);
|
|
15182
15194
|
return;
|
|
15183
15195
|
}
|
|
15184
15196
|
if ((rejected.headers.get("content-type") || "").toLowerCase().includes("text/html")) {
|
|
File without changes
|
|
@@ -257,6 +257,10 @@ class NativePageClient {
|
|
|
257
257
|
this.showHydration();
|
|
258
258
|
|
|
259
259
|
const res = await fetch(`/soma/page?path=${encodeURIComponent(pagePath)}`);
|
|
260
|
+
if (res.redirected && res.url) {
|
|
261
|
+
window.location.assign(res.url);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
260
264
|
if (!res.ok) throw await readPageAccessError(res);
|
|
261
265
|
const data: PagePayload = await res.json();
|
|
262
266
|
assertRuntimeArtifact(data);
|
|
@@ -230,7 +230,7 @@ export async function buildBrowserClient(
|
|
|
230
230
|
minify: !options.dev,
|
|
231
231
|
sourcemap: options.dev ? 'inline' : false,
|
|
232
232
|
entryNames: options.dev ? '[name]' : '[name]-[hash]',
|
|
233
|
-
chunkNames:
|
|
233
|
+
chunkNames: 'chunks/[name]-[hash]',
|
|
234
234
|
nodePaths: [path.join(webRoot, 'node_modules'), path.join(hostDir, 'node_modules'), path.join(coreRoot, 'node_modules')].filter((d) => fs.existsSync(d)),
|
|
235
235
|
plugins: [plugin],
|
|
236
236
|
define: {
|
|
@@ -442,8 +442,9 @@ function scanServiceFiles(apiRoot: string, webRoot: string): ScannedServiceItem[
|
|
|
442
442
|
}
|
|
443
443
|
|
|
444
444
|
for (const { base, isDirect } of baseCandidates) {
|
|
445
|
-
if (!fs.existsSync(base)) continue;
|
|
445
|
+
if (!fs.existsSync(base) || !fs.statSync(base).isDirectory()) continue;
|
|
446
446
|
walk(base, base, isDirect);
|
|
447
|
+
break;
|
|
447
448
|
}
|
|
448
449
|
|
|
449
450
|
function walk(currentDir: string, baseDir: string, isDirect: boolean) {
|
|
@@ -16,9 +16,15 @@ export async function applyHostAndCspHeaders(
|
|
|
16
16
|
options: { readonly method: string; readonly pathname: string },
|
|
17
17
|
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
18
18
|
): Promise<Response> {
|
|
19
|
+
const mutable = new Response(response.body, {
|
|
20
|
+
status: response.status,
|
|
21
|
+
statusText: response.statusText,
|
|
22
|
+
headers: new Headers(response.headers),
|
|
23
|
+
});
|
|
24
|
+
|
|
19
25
|
const csp = createSomaCspHeader(resolveSomaCspOptionsFromEnv(env));
|
|
20
26
|
if (csp) {
|
|
21
|
-
|
|
27
|
+
mutable.headers.set(csp.name, csp.value);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
const extra = await resolveHostResponseHeaders({
|
|
@@ -27,11 +33,11 @@ export async function applyHostAndCspHeaders(
|
|
|
27
33
|
});
|
|
28
34
|
if (extra !== undefined) {
|
|
29
35
|
for (const [name, value] of Object.entries(extra)) {
|
|
30
|
-
|
|
36
|
+
mutable.headers.set(name, value);
|
|
31
37
|
}
|
|
32
38
|
}
|
|
33
39
|
|
|
34
|
-
return
|
|
40
|
+
return mutable;
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
/**
|
|
@@ -345,7 +345,7 @@ export async function createNativeServer(
|
|
|
345
345
|
return;
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
-
const isHashed = /[-.][0-9a-f]{8,}\.[a-z0-9]
|
|
348
|
+
const isHashed = /[-.](?:[0-9a-f]{8,}|[0-9A-Z]{8})\.[a-z0-9]+$/.test(subfile);
|
|
349
349
|
const etag = `W/"${createHash('sha1').update(`${stat.size}:${stat.mtimeMs}`).digest('hex').slice(0, 20)}"`;
|
|
350
350
|
const ifNoneMatch = req.headers['if-none-match'];
|
|
351
351
|
|
|
@@ -519,11 +519,23 @@ export async function createNativeServer(
|
|
|
519
519
|
});
|
|
520
520
|
|
|
521
521
|
if (rejected) {
|
|
522
|
-
//
|
|
522
|
+
// 重定向(经可写克隆合并宿主头与 CSP,并完整保留 Set-Cookie 等头)
|
|
523
523
|
if (rejected.status >= 300 && rejected.status < 400 && rejected.headers.get('location')) {
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
524
|
+
const hooked = await applyHostAndCspHeaders(rejected, { method, pathname }, env);
|
|
525
|
+
await sendWebResponse(hooked, res);
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// 开发态逻辑测试:仅在 dev 模式下对 JSON 探针响应(2xx 或非浏览器 HTML 导航请求)原样透传;生产态严禁开启
|
|
530
|
+
const rejectedContentType = (rejected.headers.get('content-type') || '').toLowerCase();
|
|
531
|
+
const acceptHeader = (typeof req.headers['accept'] === 'string' ? req.headers['accept'] : '').toLowerCase();
|
|
532
|
+
if (
|
|
533
|
+
dev &&
|
|
534
|
+
rejectedContentType.includes('application/json') &&
|
|
535
|
+
(rejected.ok || !acceptHeader.includes('text/html'))
|
|
536
|
+
) {
|
|
537
|
+
const hooked = await applyHostAndCspHeaders(rejected, { method, pathname }, env);
|
|
538
|
+
await sendWebResponse(hooked, res);
|
|
527
539
|
return;
|
|
528
540
|
}
|
|
529
541
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soma-core",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"description": "SOMA — Semantic Oriented Modular Architecture. A declarative frontend framework runtime with no UI framework dependency; native host shell ships in host-native/.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"soma",
|