soma-core 2.1.2 → 2.1.4

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 CHANGED
@@ -51,7 +51,7 @@ Host 文件页优先,Registry 页面在 Host 404 时兜底;存量 `/pages/<n
51
51
  - `soma CLI 与 Host 工作区.md` — CLI 与 Next Host 实施方案
52
52
  - `soma DSL 语法全书.md` — core 标准格式与 `std-grammar` 的实现级语法参考,自成一册(对外入口是 `external-docs/soma-core原生手册.md`;BDL 归 soma-ui、LCL 归 soma-lite)
53
53
 
54
- 全局架构见仓库 `doc/soma 架构白皮书.md`。
54
+ 全局架构见仓库 `docs/02-系统架构/SOMA全局系统架构.md`,模块类设计见 `docs/03-类与模块/soma-core类与模块设计.md`。
55
55
 
56
56
  ## 测试
57
57
 
package/bin/soma.cjs CHANGED
@@ -24,6 +24,9 @@ const fs = require('node:fs');
24
24
  const { spawnSync } = require('node:child_process');
25
25
 
26
26
  const command = process.argv[2];
27
+ if (process.env.SOMA_HOST_DIR && process.env.SOMA_HOST_DIR.trim() !== "") {
28
+ process.env.SOMA_HOST_DIR = path.resolve(process.env.SOMA_HOST_DIR);
29
+ }
27
30
 
28
31
  /** core 自己处理(或交 dist 处理)的命令名:不进扩展命令表查找 */
29
32
  const CORE_COMMANDS = new Set([
@@ -191,6 +194,9 @@ if (command === 'help' || command === '--help' || command === '-h') {
191
194
  } else if (command !== undefined && !CORE_COMMANDS.has(command) && runExtensionCommand(command, process.argv.slice(2))) {
192
195
  // 扩展命令已转调
193
196
  } else {
197
+ if (['dev', 'build', 'start'].includes(command)) {
198
+ process.env.SOMA_HOST_DIR = path.resolve(process.cwd());
199
+ }
194
200
  const distCli = path.resolve(__dirname, '..', 'dist', 'cli', 'soma.js');
195
201
 
196
202
  if (fs.existsSync(distCli)) {
package/bin/soma.ts CHANGED
@@ -153,6 +153,7 @@ if (command !== 'check') {
153
153
  // core 运行时(core/**)不 import 它、不含 Next / React;这里以子进程转调它的命令行,参数、环境变量与退出码透传。
154
154
  // Host 目录取当前目录,覆盖外部陈旧的 SOMA_HOST_DIR。
155
155
  const hostDir = path.resolve(process.cwd());
156
+ process.env.SOMA_HOST_DIR = hostDir;
156
157
  const hostNextCli = locateHostNextCli();
157
158
  if (!hostNextCli) {
158
159
  console.error(
@@ -4586,7 +4586,6 @@ function parseUiConfig(raw, options) {
4586
4586
  }
4587
4587
  var FRAGMENTS_ARTIFACT_KEYS = {
4588
4588
  format: true,
4589
- version: true,
4590
4589
  regions: true,
4591
4590
  scopes: true,
4592
4591
  css: true,
@@ -4606,9 +4605,9 @@ function parseFragmentsSection(raw, label = SOMA_FRAGMENTS_KEY) {
4606
4605
  );
4607
4606
  }
4608
4607
  shapeClosedKeys(entry, FRAGMENTS_ARTIFACT_KEYS, label, label);
4609
- if (entry.format !== SOMA_FRAGMENTS_FORMAT && entry.version !== 1) {
4608
+ if (entry.format !== SOMA_FRAGMENTS_FORMAT) {
4610
4609
  throw new SomaUiConfigError(
4611
- `${label} \u7684\u683C\u5F0F\u6807\u8BC6\u4E0D\u8BA4\u8BC6\uFF08format \u987B\u4E3A ${SOMA_FRAGMENTS_FORMAT}\uFF0C\u6216\u5B58\u91CF version \u4E3A 1\uFF09\uFF0C\u6536\u5230 format=${JSON.stringify(entry.format)} version=${JSON.stringify(entry.version)}\u2014\u2014\u683C\u5F0F\u5BF9\u4E0D\u4E0A\u7684\u4EA7\u7269\u6574\u4EFD\u4E0D\u53EF\u4FE1\uFF0C\u6309\u65E7\u5B57\u6BB5\u540D\u8BFB\u51FA\u6765\u7684\u754C\u9762\u662F\u8C01\u90FD\u6CA1\u8BBE\u8BA1\u8FC7\u7684\u6837\u5B50`,
4610
+ `${label} \u7684\u683C\u5F0F\u6807\u8BC6\u4E0D\u8BA4\u8BC6\uFF08format \u987B\u4E3A ${SOMA_FRAGMENTS_FORMAT}\uFF09\uFF0C\u6536\u5230 format=${JSON.stringify(entry.format)}\u2014\u2014\u683C\u5F0F\u5BF9\u4E0D\u4E0A\u7684\u4EA7\u7269\u6574\u4EFD\u4E0D\u53EF\u4FE1\uFF0C\u6309\u65E7\u5B57\u6BB5\u540D\u8BFB\u51FA\u6765\u7684\u754C\u9762\u662F\u8C01\u90FD\u6CA1\u8BBE\u8BA1\u8FC7\u7684\u6837\u5B50`,
4612
4611
  `${label}.format`
4613
4612
  );
4614
4613
  }
@@ -4621,10 +4620,9 @@ function parseFragmentsSection(raw, label = SOMA_FRAGMENTS_KEY) {
4621
4620
  }
4622
4621
  }
4623
4622
  const artifact = {
4623
+ format: SOMA_FRAGMENTS_FORMAT,
4624
4624
  regions: entry.regions
4625
4625
  };
4626
- if (entry.format === SOMA_FRAGMENTS_FORMAT) artifact.format = SOMA_FRAGMENTS_FORMAT;
4627
- if (entry.version === 1) artifact.version = 1;
4628
4626
  if (entry.degradedRegions !== void 0) {
4629
4627
  if (!Array.isArray(entry.degradedRegions)) {
4630
4628
  throw new SomaUiConfigError(`${label}.degradedRegions \u5FC5\u987B\u662F\u6570\u7EC4`, `${label}.degradedRegions`);
@@ -8799,17 +8797,13 @@ var SOMA_CORE_DIAGNOSTIC_CODES = [
8799
8797
  // runtime 空间根与存储(RT1)
8800
8798
  "CORE-RUNTIME-ROOT-INVALID",
8801
8799
  // runtime 根越界(宿主根 / html / ui / public 内、宿主祖先、文件系统根),拒绝启动
8802
- "CORE-RUNTIME-LEGACY-DIR",
8803
- // Registry 数据仍在旧目录,正在只读回退;提示运行 soma runtime migrate
8804
8800
  "CORE-RUNTIME-ROOT-UNSET",
8805
8801
  // 生产环境未设 SOMA_RUNTIME_DIR(soma doctor 告警)
8806
8802
  // 地址绑定与注入(RT2 / E2;《core 标准格式与 runtime 空间》§2、§4;《页面级描述与按 URL 注册》§4)
8807
8803
  "CORE-FORMAT-VERSION-UNSUPPORTED",
8808
8804
  // 注入载荷 format 不是 core 认识的主版本
8809
8805
  "CORE-FORMAT-VERSION-MISMATCH",
8810
- // 载荷 format 与 page 块 soma: 不一致
8811
- "CORE-FORMAT-VERSION-IMPLICIT",
8812
- // page 块未写 soma:,兼容期按 1 处理(advice)
8806
+ // 载荷 format 与 page 块 soma: 不一致(page 块缺 soma: 同此码)
8813
8807
  "CORE-INJECT-NONSTANDARD-KEY",
8814
8808
  // 兼容期结束后含方言段拒收
8815
8809
  "CORE-RUNTIME-ADDRESS-INVALID",
@@ -1,22 +1,9 @@
1
1
  import fs from 'node:fs';
2
- import { createRequire } from 'node:module';
3
2
  import path from 'node:path';
4
-
5
- type JitiFactory = (id: string, opts?: unknown) => (file: string) => unknown;
6
-
7
- let cachedJitiFactory: JitiFactory | null = null;
8
- function getJitiFactory(): JitiFactory {
9
- if (cachedJitiFactory === null) {
10
- const req = createRequire(import.meta.url);
11
- const jitiMod = req('jiti');
12
- cachedJitiFactory = (jitiMod.default ?? jitiMod) as JitiFactory;
13
- }
14
- return cachedJitiFactory;
15
- }
16
3
  import type { IService } from './IService';
17
4
  import { resolveServiceFilePath } from './service-path';
18
- import { getSomaAssetSources } from '../host/source-packages';
19
- import { resolveServerOnlyEmpty } from '../host/host-assembly';
5
+ import { getSomaAssetSources, getSomaHostSourceRoot } from '../host/source-packages';
6
+ import { ensureNodeServerOnlyInterop, getJitiFactory } from '../host/host-assembly';
20
7
 
21
8
  function isService(obj: unknown): obj is IService {
22
9
  if (!obj || typeof obj !== 'object') return false;
@@ -25,7 +12,7 @@ function isService(obj: unknown): obj is IService {
25
12
  }
26
13
 
27
14
  function getCoreDir(): string {
28
- // soma-core 根目录(route 运行于 next dev/build,cwd 即 soma-core)
15
+ // soma-core 根目录(route 运行于 next dev/build,cwd 即工作区,其下有 core)
29
16
  return path.resolve(process.cwd(), 'core');
30
17
  }
31
18
 
@@ -35,13 +22,14 @@ const instanceCache = new Map<string, IService>();
35
22
 
36
23
  function getJiti(hostDir: string): (file: string) => unknown {
37
24
  if (!jitiLoader || jitiHostDir !== hostDir) {
38
- const serverOnlyEmpty = resolveServerOnlyEmpty();
39
- const createJiti = getJitiFactory();
40
- jitiLoader = createJiti(import.meta.url, {
25
+ const serverOnlyEmpty = ensureNodeServerOnlyInterop(hostDir);
26
+ const createJiti = getJitiFactory(hostDir);
27
+ const hostSourceRoot = getSomaHostSourceRoot(hostDir);
28
+ jitiLoader = createJiti(path.resolve(hostDir, 'package.json'), {
41
29
  interopDefault: true,
42
30
  alias: {
43
31
  '@core': getCoreDir(),
44
- '@soma-host': hostDir,
32
+ '@soma-host': hostSourceRoot,
45
33
  ...(serverOnlyEmpty !== undefined ? { 'server-only': serverOnlyEmpty } : {}),
46
34
  },
47
35
  });
@@ -9,17 +9,45 @@
9
9
  // 不变式:形状不对当场抛(半套装配比没有更难查);同一应用根只加载一次(进程内缓存)。
10
10
 
11
11
  import fs from 'node:fs';
12
- import { createRequire } from 'node:module';
12
+ import nodeModule from 'node:module';
13
13
  import path from 'node:path';
14
14
  import { fileURLToPath } from 'node:url';
15
- type JitiFactory = (id: string, opts?: unknown) => (file: string) => unknown;
15
+
16
+ export type JitiFactory = (id: string, opts?: unknown) => (file: string) => unknown;
17
+
18
+ // 绕过 Webpack CommonJsImportsParserPlugin 对字面量 createRequire 的静态拦截(Webpack 会把字面量调用抹成 undefined)
19
+ const CREATE_REQUIRE_KEY = 'createRequire' as const;
20
+ const RESOLVE_FILENAME_KEY = '_resolveFilename' as const;
21
+ const SERVER_ONLY_HOOK_KEY = Symbol.for('soma.core.serverOnlyNodeHook');
22
+
23
+ export function createNodeRequire(anchorDir?: string): NodeRequire {
24
+ const createReq = (nodeModule as unknown as Record<string, (id: string) => NodeRequire>)[CREATE_REQUIRE_KEY];
25
+ const anchor = anchorDir
26
+ ? path.resolve(anchorDir, 'package.json')
27
+ : typeof import.meta.url === 'string' && import.meta.url.startsWith('file:')
28
+ ? import.meta.url
29
+ : path.resolve(process.cwd(), 'package.json');
30
+ return createReq(anchor);
31
+ }
16
32
 
17
33
  let cachedJiti: JitiFactory | null = null;
18
- function getJiti(): JitiFactory {
34
+ export function getJitiFactory(anchorDir?: string): JitiFactory {
19
35
  if (cachedJiti === null) {
20
- const req = createRequire(import.meta.url);
21
- const jitiMod = req('jiti');
22
- cachedJiti = (jitiMod.default ?? jitiMod) as JitiFactory;
36
+ const candidates = [anchorDir, process.cwd(), undefined];
37
+ let lastErr: unknown;
38
+ for (const dir of candidates) {
39
+ try {
40
+ const req = createNodeRequire(dir);
41
+ const jitiMod = req('jiti');
42
+ cachedJiti = ((jitiMod as { default?: JitiFactory }).default ?? jitiMod) as JitiFactory;
43
+ break;
44
+ } catch (e) {
45
+ lastErr = e;
46
+ }
47
+ }
48
+ if (cachedJiti === null) {
49
+ throw lastErr instanceof Error ? lastErr : new Error('Cannot load jiti in Node runtime');
50
+ }
23
51
  }
24
52
  return cachedJiti;
25
53
  }
@@ -183,15 +211,55 @@ export function loadHostAssemblySync(appRoot: string): SomaHostAssembly | null {
183
211
  * 会解析到抛错的 index.js("This module cannot be imported from a Client Component module")。
184
212
  * 将 server-only 别名指向 server-only/empty.js 可让经 jiti 加载的服务端代码正常运行。
185
213
  */
186
- export function resolveServerOnlyEmpty(): string | undefined {
214
+ function resolveSelfCoreDir(): string {
187
215
  try {
188
- const req = createRequire(import.meta.url);
189
- const serverOnlyPath = req.resolve('server-only');
190
- const emptyPath = path.join(path.dirname(serverOnlyPath), 'empty.js');
191
- return fs.existsSync(emptyPath) ? emptyPath : undefined;
192
- } catch {
193
- return undefined;
216
+ if (typeof import.meta.url === 'string' && import.meta.url.startsWith('file:')) {
217
+ const candidate = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../');
218
+ if (fs.existsSync(candidate)) return candidate;
219
+ }
220
+ } catch {}
221
+ return path.resolve(process.cwd(), 'core');
222
+ }
223
+
224
+ export function resolveServerOnlyEmpty(anchorDir?: string): string | undefined {
225
+ // 1. 优先取 soma-core 自带的内置空垫片(零外部包解析依赖、100% 确定性)
226
+ const selfCore = resolveSelfCoreDir();
227
+ const builtinNoop = path.join(selfCore, 'server', 'host', 'server-only-noop.cjs');
228
+ if (fs.existsSync(builtinNoop)) return builtinNoop;
229
+
230
+ // 2. 兜底从宿主或当前工作区解析 server-only/empty.js
231
+ for (const dir of [anchorDir, process.cwd(), undefined]) {
232
+ try {
233
+ const req = createNodeRequire(dir);
234
+ const serverOnlyPath = req.resolve('server-only');
235
+ const emptyPath = path.join(path.dirname(serverOnlyPath), 'empty.js');
236
+ if (fs.existsSync(emptyPath)) return emptyPath;
237
+ } catch {}
238
+ }
239
+ return undefined;
240
+ }
241
+
242
+ /**
243
+ * 在纯 Node 进程内安装一次幂等的 Module._resolveFilename 守卫:
244
+ * 不仅覆盖 jiti 直接转译的 TS 文件中的 `import 'server-only'`,还覆盖被间接加载的第三方已编译 CJS/ESM 包中的 `require('server-only')`。
245
+ */
246
+ export function ensureNodeServerOnlyInterop(anchorDir?: string): string | undefined {
247
+ if (typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge') return undefined;
248
+ const stub = resolveServerOnlyEmpty(anchorDir);
249
+ if (!stub) return undefined;
250
+ const g = globalThis as Record<symbol, unknown>;
251
+ if (!g[SERVER_ONLY_HOOK_KEY]) {
252
+ const modRecord = nodeModule as unknown as Record<string, unknown>;
253
+ const orig = modRecord[RESOLVE_FILENAME_KEY];
254
+ if (typeof orig === 'function') {
255
+ modRecord[RESOLVE_FILENAME_KEY] = function somaResolveServerOnly(request: string, ...rest: unknown[]) {
256
+ if (request === 'server-only') return stub;
257
+ return (orig as (...args: unknown[]) => unknown).call(this, request, ...rest);
258
+ };
259
+ g[SERVER_ONLY_HOOK_KEY] = stub;
260
+ }
194
261
  }
262
+ return stub;
195
263
  }
196
264
 
197
265
  function loadAppSync(appRoot: string): LoadedApp | null {
@@ -208,14 +276,15 @@ function loadAppSync(appRoot: string): LoadedApp | null {
208
276
  cache.set(key, null);
209
277
  return null;
210
278
  }
211
- const serverOnlyEmpty = resolveServerOnlyEmpty();
212
- const createJiti = getJiti();
213
- const loader = createJiti(import.meta.url, {
279
+ const serverOnlyEmpty = ensureNodeServerOnlyInterop(key);
280
+ const createJiti = getJitiFactory(key);
281
+ const coreDir = resolveSelfCoreDir();
282
+ const loader = createJiti(path.resolve(key, 'package.json'), {
214
283
  interopDefault: false,
215
284
  requireCache: false,
216
285
  alias: {
217
286
  '@soma-host': hostRoot,
218
- '@core': path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'),
287
+ '@core': coreDir,
219
288
  ...(serverOnlyEmpty !== undefined ? { 'server-only': serverOnlyEmpty } : {}),
220
289
  },
221
290
  });
@@ -0,0 +1,2 @@
1
+ // 纯 Node 运行时下供 jiti 与原生 CJS 加载器解析 'server-only' 的空垫片(避免 Next.js 外层 server-only/index.js 误判为客户端组件而抛错)。
2
+ module.exports = {};
package/dist/cli/soma.js CHANGED
@@ -142,6 +142,7 @@ if (command !== 'check') {
142
142
  // core 运行时(core/**)不 import 它、不含 Next / React;这里以子进程转调它的命令行,参数、环境变量与退出码透传。
143
143
  // Host 目录取当前目录,覆盖外部陈旧的 SOMA_HOST_DIR。
144
144
  const hostDir = node_path_1.default.resolve(process.cwd());
145
+ process.env.SOMA_HOST_DIR = hostDir;
145
146
  const hostNextCli = locateHostNextCli();
146
147
  if (!hostNextCli) {
147
148
  console.error(`[soma ${command}] 找不到 Next 宿主壳入口 host-next/bin/host-next.cjs:soma-core 包不完整,请重新安装。`);
@@ -1,3 +1,6 @@
1
+ export type JitiFactory = (id: string, opts?: unknown) => (file: string) => unknown;
2
+ export declare function createNodeRequire(anchorDir?: string): NodeRequire;
3
+ export declare function getJitiFactory(anchorDir?: string): JitiFactory;
1
4
  import type { SomaHostAssembly } from '../../shared/types/host-assembly.types';
2
5
  /** Host 装配文件相对应用根的位置 */
3
6
  export declare const SOMA_HOST_ASSEMBLY_FILE = "soma.app.ts";
@@ -35,14 +38,12 @@ export declare function resolveHostLoginPath(assembly?: {
35
38
  * 结果按应用根缓存。
36
39
  */
37
40
  export declare function loadHostAssemblySync(appRoot: string): SomaHostAssembly | null;
41
+ export declare function resolveServerOnlyEmpty(anchorDir?: string): string | undefined;
38
42
  /**
39
- * 解析 'server-only' 的空垫片路径(empty.js)。
40
- *
41
- * 为什么需要:Host 装配在纯 Node 里经 jiti 加载,Next.js 的 server-only 在纯 Node 默认导出条件下
42
- * 会解析到抛错的 index.js("This module cannot be imported from a Client Component module")。
43
- * 将 server-only 别名指向 server-only/empty.js 可让经 jiti 加载的服务端代码正常运行。
43
+ * 在纯 Node 进程内安装一次幂等的 Module._resolveFilename 守卫:
44
+ * 不仅覆盖 jiti 直接转译的 TS 文件中的 `import 'server-only'`,还覆盖被间接加载的第三方已编译 CJS/ESM 包中的 `require('server-only')`。
44
45
  */
45
- export declare function resolveServerOnlyEmpty(): string | undefined;
46
+ export declare function ensureNodeServerOnlyInterop(anchorDir?: string): string | undefined;
46
47
  /**
47
48
  * 读 `<应用根>/soma.app.ts` 默认导出并等服务端面装配完成(`await app.server()`);无该文件 → null。
48
49
  * 装配失败(SomaAppAssemblyError)原样上抛——拒绝启动,不静默回落。同一应用根只装配一次(并发调用共享同一 Promise)。
@@ -30,6 +30,7 @@ if (!nextBin) {
30
30
  const hostDir = path.resolve(process.env.SOMA_HOST_DIR && process.env.SOMA_HOST_DIR.trim() !== ''
31
31
  ? process.env.SOMA_HOST_DIR
32
32
  : process.cwd());
33
+ process.env.SOMA_HOST_DIR = hostDir;
33
34
  let nextWorkspace;
34
35
  try {
35
36
  nextWorkspace = ensureNextWorkspace(hostDir);
@@ -31,6 +31,7 @@ if (!rawHostDir || !String(rawHostDir).trim()) {
31
31
  );
32
32
  }
33
33
  const hostDir = path.resolve(rawHostDir);
34
+ process.env.SOMA_HOST_DIR = hostDir;
34
35
  const webpackCacheMode = readSomaWebpackCacheMode();
35
36
 
36
37
  // 壳自有路由(app/ 枚举):经环境变量交给页面入口并入装配的 reservedRoutes(app/soma/_shared/host-prepare.ts)。
@@ -278,6 +279,7 @@ const allowedDevOrigins = [
278
279
  const nextConfig = {
279
280
  env: {
280
281
  SOMA_SHELL_ROUTES: process.env.SOMA_SHELL_ROUTES,
282
+ SOMA_HOST_DIR: hostDir,
281
283
  },
282
284
 
283
285
  allowedDevOrigins,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soma-core",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "SOMA — Semantic Oriented Modular Architecture. A declarative frontend framework runtime with no UI framework dependency; the Next.js host shell ships in host-next/ (code-isolated from core/, next/react are optional peer dependencies).",
5
5
  "keywords": [
6
6
  "soma",
@@ -322,7 +322,7 @@
322
322
  "parse5": "7.3.0",
323
323
  "qrcode-generator": "2.0.4",
324
324
  "server-only": "0.0.1",
325
- "soma-foundation": "^0.1.0",
325
+ "soma-foundation": "^0.1.1",
326
326
  "temporal-polyfill": "0.3.2"
327
327
  },
328
328
  "peerDependencies": {