soma-core 2.2.0 → 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.
@@ -81,10 +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"),
90
+ join(normalizedApiRoot, "..", "api", "service"),
86
91
  join(normalizedApiRoot, "service"),
87
- ...(options?.allowDirectRoot ? [normalizedApiRoot] : []),
92
+ ...(options?.allowDirectRoot && !hasLocalServiceSubdir ? [normalizedApiRoot] : []),
88
93
  ];
89
94
 
90
95
  for (const base of candidateBases) {
@@ -127,13 +127,15 @@ function toPackageSource(packageName, packageRoot) {
127
127
  const packageJson = readJson(path.join(packageRoot, "package.json"));
128
128
  const codeRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.hostRoot);
129
129
  const configRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.configRoot);
130
+ const hasRootApiService = fs.existsSync(path.join(packageRoot, "api", "service"));
131
+ const apiRoot = hasRootApiService ? packageRoot : codeRoot;
130
132
  return {
131
133
  type: "package",
132
134
  name: packageName,
133
135
  packageName,
134
136
  assetRoot: codeRoot,
135
137
  codeRoot,
136
- apiRoot: codeRoot,
138
+ apiRoot,
137
139
  configRoot,
138
140
  };
139
141
  }
@@ -356,13 +356,15 @@ var require_source_packages = __commonJS({
356
356
  const packageJson = readJson(path2.join(packageRoot, "package.json"));
357
357
  const codeRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.hostRoot);
358
358
  const configRoot = resolveSafeChildRoot(packageRoot, packageJson?.soma?.configRoot);
359
+ const hasRootApiService = fs2.existsSync(path2.join(packageRoot, "api", "service"));
360
+ const apiRoot = hasRootApiService ? packageRoot : codeRoot;
359
361
  return {
360
362
  type: "package",
361
363
  name: packageName,
362
364
  packageName,
363
365
  assetRoot: codeRoot,
364
366
  codeRoot,
365
- apiRoot: codeRoot,
367
+ apiRoot,
366
368
  configRoot
367
369
  };
368
370
  }
@@ -629,7 +631,7 @@ async function buildBrowserClient(options) {
629
631
  minify: !options.dev,
630
632
  sourcemap: options.dev ? "inline" : false,
631
633
  entryNames: options.dev ? "[name]" : "[name]-[hash]",
632
- chunkNames: options.dev ? "chunks/[name]" : "chunks/[name]-[hash]",
634
+ chunkNames: "chunks/[name]-[hash]",
633
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)),
634
636
  plugins: [plugin],
635
637
  define: {
@@ -809,8 +811,9 @@ function scanServiceFiles(apiRoot, webRoot) {
809
811
  baseCandidates.push({ base: apiRoot, isDirect: true });
810
812
  }
811
813
  for (const { base, isDirect } of baseCandidates) {
812
- if (!import_node_fs.default.existsSync(base)) continue;
814
+ if (!import_node_fs.default.existsSync(base) || !import_node_fs.default.statSync(base).isDirectory()) continue;
813
815
  walk(base, base, isDirect);
816
+ break;
814
817
  }
815
818
  function walk(currentDir, baseDir, isDirect) {
816
819
  let entries = [];
@@ -219,13 +219,15 @@ var require_source_packages = __commonJS({
219
219
  const packageJson = readJson2(path17.join(packageRoot, "package.json"));
220
220
  const codeRoot = resolveSafeChildRoot2(packageRoot, packageJson?.soma?.hostRoot);
221
221
  const configRoot = resolveSafeChildRoot2(packageRoot, packageJson?.soma?.configRoot);
222
+ const hasRootApiService = fs15.existsSync(path17.join(packageRoot, "api", "service"));
223
+ const apiRoot = hasRootApiService ? packageRoot : codeRoot;
222
224
  return {
223
225
  type: "package",
224
226
  name: packageName,
225
227
  packageName,
226
228
  assetRoot: codeRoot,
227
229
  codeRoot,
228
- apiRoot: codeRoot,
230
+ apiRoot,
229
231
  configRoot
230
232
  };
231
233
  }
@@ -13741,10 +13743,12 @@ function resolveHostServiceCandidate(apiRoot, segments, options) {
13741
13743
  const normalizedApiRoot = (0, import_node_path14.resolve)(apiRoot);
13742
13744
  const extensions = options?.extensions ?? DEFAULT_SERVICE_EXTENSIONS;
13743
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;
13744
13747
  const candidateBases = [
13745
13748
  (0, import_node_path14.join)(normalizedApiRoot, "api", "service"),
13749
+ (0, import_node_path14.join)(normalizedApiRoot, "..", "api", "service"),
13746
13750
  (0, import_node_path14.join)(normalizedApiRoot, "service"),
13747
- ...options?.allowDirectRoot ? [normalizedApiRoot] : []
13751
+ ...options?.allowDirectRoot && !hasLocalServiceSubdir ? [normalizedApiRoot] : []
13748
13752
  ];
13749
13753
  for (const base of candidateBases) {
13750
13754
  const normalizedAllowedBase = (0, import_node_path14.resolve)(base);
@@ -14743,9 +14747,14 @@ async function somaCspNonceMetadataAsync(readHeaders, env = process.env) {
14743
14747
 
14744
14748
  // host-native/src/csp-headers.ts
14745
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
+ });
14746
14755
  const csp = createSomaCspHeader(resolveSomaCspOptionsFromEnv(env));
14747
14756
  if (csp) {
14748
- response.headers.set(csp.name, csp.value);
14757
+ mutable.headers.set(csp.name, csp.value);
14749
14758
  }
14750
14759
  const extra = await resolveHostResponseHeaders({
14751
14760
  method: options.method,
@@ -14753,10 +14762,10 @@ async function applyHostAndCspHeaders(response, options, env = process.env) {
14753
14762
  });
14754
14763
  if (extra !== void 0) {
14755
14764
  for (const [name, value2] of Object.entries(extra)) {
14756
- response.headers.set(name, value2);
14765
+ mutable.headers.set(name, value2);
14757
14766
  }
14758
14767
  }
14759
- return response;
14768
+ return mutable;
14760
14769
  }
14761
14770
  async function readCspNonceMeta(nonce, env = process.env) {
14762
14771
  const meta = await somaCspNonceMetadataAsync(async () => ({
@@ -15032,7 +15041,7 @@ data: ${data || ""}
15032
15041
  res.end("Not found");
15033
15042
  return;
15034
15043
  }
15035
- const isHashed = /[-.][0-9a-f]{8,}\.[a-z0-9]+$/i.test(subfile);
15044
+ const isHashed = /[-.](?:[0-9a-f]{8,}|[0-9A-Z]{8})\.[a-z0-9]+$/.test(subfile);
15036
15045
  const etag = `W/"${(0, import_node_crypto9.createHash)("sha1").update(`${stat.size}:${stat.mtimeMs}`).digest("hex").slice(0, 20)}"`;
15037
15046
  const ifNoneMatch = req.headers["if-none-match"];
15038
15047
  if (ifNoneMatch && ifNoneMatch.includes(etag)) {
@@ -15173,9 +15182,15 @@ data: ${data || ""}
15173
15182
  });
15174
15183
  if (rejected) {
15175
15184
  if (rejected.status >= 300 && rejected.status < 400 && rejected.headers.get("location")) {
15176
- res.statusCode = rejected.status;
15177
- res.setHeader("Location", rejected.headers.get("location"));
15178
- res.end();
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);
15179
15194
  return;
15180
15195
  }
15181
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: options.dev ? 'chunks/[name]' : 'chunks/[name]-[hash]',
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
- response.headers.set(csp.name, csp.value);
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
- response.headers.set(name, value);
36
+ mutable.headers.set(name, value);
31
37
  }
32
38
  }
33
39
 
34
- return response;
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]+$/i.test(subfile);
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
- res.statusCode = rejected.status;
525
- res.setHeader('Location', rejected.headers.get('location')!);
526
- res.end();
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.0",
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",