soma-core 2.1.6 → 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.
Files changed (70) hide show
  1. package/README.md +2 -2
  2. package/bin/soma.cjs +1 -1
  3. package/bin/soma.ts +64 -15
  4. package/core/server/api/dispatch-service-api.ts +212 -0
  5. package/core/server/api/load-host-service.ts +230 -56
  6. package/core/server/api/service-path.ts +78 -14
  7. package/core/server/engines/style/candidate-guard.ts +57 -0
  8. package/core/server/engines/style/index.ts +1 -1
  9. package/core/server/engines/style/tailwind-adapter.ts +83 -16
  10. package/core/server/host/source-packages.cjs +96 -30
  11. package/core/server/host/source-packages.ts +12 -5
  12. package/core/server/index.ts +39 -17
  13. package/core/server/infra/cache/ServerFileCache.ts +138 -25
  14. package/core/server/node.ts +32 -8
  15. package/dist/cli/soma.js +68 -14
  16. package/dist/host-native/aot-builder.cjs +850 -0
  17. package/dist/host-native/native-server.cjs +15338 -0
  18. package/dist/types/server/api/dispatch-service-api.d.ts +18 -0
  19. package/dist/types/server/api/load-host-service.d.ts +7 -2
  20. package/dist/types/server/api/service-path.d.ts +21 -1
  21. package/dist/types/server/engines/style/candidate-guard.d.ts +10 -0
  22. package/dist/types/server/engines/style/index.d.ts +1 -1
  23. package/dist/types/server/engines/style/tailwind-adapter.d.ts +12 -0
  24. package/dist/types/server/host/source-packages.d.cts +13 -1
  25. package/dist/types/server/host/source-packages.d.ts +7 -4
  26. package/dist/types/server/index.d.ts +18 -17
  27. package/dist/types/server/infra/cache/ServerFileCache.d.ts +28 -5
  28. package/dist/types/server/node.d.ts +10 -7
  29. package/host-native/bin/host-native.cjs +204 -0
  30. package/host-native/client/browser-entry.ts +499 -0
  31. package/{host-next/app → host-native/client}/page-access.ts +12 -0
  32. package/host-native/client/router-bridge.ts +120 -0
  33. package/host-native/src/aot-builder.ts +486 -0
  34. package/host-native/src/csp-headers.ts +75 -0
  35. package/host-native/src/host-prepare.ts +123 -0
  36. package/{host-next/app/soma/manifest/route.ts → host-native/src/manifest-http.ts} +10 -10
  37. package/host-native/src/native-server.ts +691 -0
  38. package/host-native/src/page-http.ts +223 -0
  39. package/host-native/src/public-packages.cjs +167 -0
  40. package/{host-next/app/api/soma-registry/[...path]/route.ts → host-native/src/registry-handler.ts} +17 -19
  41. package/{host-next → host-native}/src/shell-routes.cjs +23 -13
  42. package/host-native/src/static-file.ts +172 -0
  43. package/host-native/src/workspace.ts +185 -0
  44. package/{host-next/app → host-native/styles}/globals.css +2 -2
  45. package/package.json +12 -31
  46. package/tsconfig.json +1 -1
  47. package/host-next/app/SomaPageClient.tsx +0 -359
  48. package/host-next/app/[...path]/page.tsx +0 -22
  49. package/host-next/app/api/[...path]/route.ts +0 -98
  50. package/host-next/app/components/SomaHostPage.tsx +0 -128
  51. package/host-next/app/components/SomaHydrationShell.tsx +0 -20
  52. package/host-next/app/components/SomaPageClientBoundary.tsx +0 -13
  53. package/host-next/app/components/SomaPageForbidden.tsx +0 -19
  54. package/host-next/app/favicon.ico/route.ts +0 -13
  55. package/host-next/app/layout.tsx +0 -27
  56. package/host-next/app/page.tsx +0 -9
  57. package/host-next/app/router-bridge.ts +0 -42
  58. package/host-next/app/soma/[...path]/route.ts +0 -9
  59. package/host-next/app/soma/_shared/host-prepare.ts +0 -33
  60. package/host-next/app/soma/_shared/page-http.ts +0 -86
  61. package/host-next/app/soma/page/route.ts +0 -36
  62. package/host-next/app/soma/route.ts +0 -9
  63. package/host-next/app/soma-runtime/[...file]/route.ts +0 -21
  64. package/host-next/app/soma-runtime/_shared/static-file.ts +0 -89
  65. package/host-next/bin/host-next.cjs +0 -60
  66. package/host-next/instrumentation.ts +0 -18
  67. package/host-next/next-env.d.ts +0 -7
  68. package/host-next/next.config.js +0 -410
  69. package/host-next/proxy.ts +0 -43
  70. package/host-next/src/workspace.ts +0 -543
@@ -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 'server-only';
10
+ import "server-only";
11
11
 
12
- export { ServerFileCache } from './infra/cache/ServerFileCache';
12
+ export { ServerFileCache } from "./infra/cache/ServerFileCache";
13
13
  export {
14
14
  withSomaServerRequestContext,
15
15
  type SomaServerRequestContextOptions,
16
- } from './infra/http/soma-server-request-context';
16
+ } from "./infra/http/soma-server-request-context";
17
17
 
18
- export type { IService, SomaServiceRequest } from './api/IService';
19
- export { loadHostService } from './api/load-host-service';
20
- export { resolveServiceFilePath, toPascalCase } from './api/service-path';
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 './host/invoke-host-request-hook';
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 './host/resolve-host-request-hook';
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 './host/content-security-policy';
66
+ } from "./host/content-security-policy";
47
67
  export {
48
68
  readSomaCspNonce,
49
69
  somaCspNonceMetadata,
50
70
  type SomaCspHeaderBag,
51
- } from './host/csp-nonce-metadata';
52
- export { loadHostAssembly, registerShellRoutes, resolveHostLoginPath, SOMA_HOST_ASSEMBLY_FILE } from './host/host-assembly';
53
- export type { SomaHostAssembly } from '../shared/types/host-assembly.types';
54
- export { loadPageMarkupConfig, renderColorSchemeMeta } from './host/page-markup-config';
55
- export type { SomaPageMarkup } from '../shared/types/component-tree.types';
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 './host/server-page-assets';
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 './host/source-packages';
89
+ } from "./host/source-packages";
68
90
  // runtime 空间(RT2 / E2):动态注入与地址绑定、按地址取页。
69
- export * from './runtime-space';
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
- import { MemoryCache } from '../../../client/infra/cache/MemoryCache';
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
- * 支持 fs.watch 监听文件变化,自动失效对应缓存条目。
19
+ * 支持有界文件数、总字节上限与目录缓存上限;支持 fs.watch 监听去重,自动失效对应缓存条目。
10
20
  */
11
21
  export class ServerFileCache {
12
22
  private static instance: ServerFileCache | null = null;
13
23
 
14
- private fileCache = new MemoryCache<string>();
15
- private dirCache = new MemoryCache<string[]>();
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
- private constructor() {}
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
- return cached;
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
- this.fileCache.set(key, content);
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.dirCache.set(dirKey, files);
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.dirCache.set(dirKey, names);
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(rootDir, { recursive: true }, (_event, filename) => {
180
+ const watcher = watch(resolvedRoot, { recursive: true }, (_event, filename) => {
91
181
  if (!filename) return;
92
182
 
93
- const fullPath = normalize(resolve(rootDir, filename));
94
- this.fileCache.delete(fullPath);
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
- * 清除所有缓存并关闭 watcher。
205
+ * 清除所有缓存。
113
206
  */
114
- dispose(): void {
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
- w.close();
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
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Node 安全的服务端子路径(`soma-core/server/node`,R-28 / C9)。
3
3
  *
4
- * 为什么单开一个口:`soma-core/server` 带 `import 'server-only'`,并连带 CSP 这类只在 Next 服务端成立的模块;
4
+ * 为什么单开一个口:`soma-core/server` 带 `import "server-only"`,并连带 CSP 这类只在 Next 服务端成立的模块;
5
5
  * CLI、testkit、经 jiti 在纯 Node 语境加载的 Host 服务一进来就会抛。这里只放「宿主资源定位 / 页面资产 / 块加载 /
6
6
  * Registry 写管线」这类纯 Node 能力,**不 import `server-only`,也不得(经任何传递依赖)加载
7
7
  * `server/host/content-security-policy` / `csp-nonce-metadata`**——由 `tests/unit/server/node-entry-purity.test.ts` 看守。
@@ -11,9 +11,11 @@
11
11
 
12
12
  // 宿主资源定位(源包 / 宿主根 / 页面入口 / 共享片段 / 配置根)
13
13
  export {
14
+ clearSomaAssetSourcesCache,
14
15
  collectIncludes,
15
16
  collectPageEntries,
16
17
  getSomaAssetSources,
18
+ getSomaHostApiRoot,
17
19
  getSomaHostConfigRoot,
18
20
  getSomaHostSourceRoot,
19
21
  listSomaSourcePackageNames,
@@ -26,7 +28,7 @@ export {
26
28
  type SomaAssetSource,
27
29
  type SomaEnvName,
28
30
  type SomaSourcePageEntry,
29
- } from './host/source-packages';
31
+ } from "./host/source-packages";
30
32
 
31
33
  // 页面资产与页面标记配置
32
34
  export {
@@ -34,11 +36,33 @@ export {
34
36
  resolveSomaPageSource,
35
37
  SomaPageAssetError,
36
38
  type SomaServerPageAssets,
37
- } from './host/server-page-assets';
38
- export { loadPageMarkupConfig, renderColorSchemeMeta } from './host/page-markup-config';
39
+ } from "./host/server-page-assets";
40
+ export { loadPageMarkupConfig, renderColorSchemeMeta } from "./host/page-markup-config";
39
41
 
40
- // 服务路径与外部块
41
- export { resolveServiceFilePath, toPascalCase } from './api/service-path';
42
+ // 服务路径与服务分发
43
+ export {
44
+ resolveHostServiceCandidate,
45
+ resolveServiceFilePath,
46
+ toPascalCase,
47
+ validateServiceSegments,
48
+ type ResolveHostServiceCandidateOptions,
49
+ } from "./api/service-path";
50
+ export {
51
+ clearHostServiceCache,
52
+ getAotServiceResolver,
53
+ loadHostService,
54
+ registerAotServiceResolver,
55
+ resolveServiceExport,
56
+ type AotServiceResolver,
57
+ } from "./api/load-host-service";
58
+ export {
59
+ dispatchHostServiceApiRequest,
60
+ polyfillServiceRequest,
61
+ type DispatchHostServiceApiOptions,
62
+ type RequestCookie,
63
+ type RequestCookiesAdapter,
64
+ } from "./api/dispatch-service-api";
65
+ export type { IService, SomaServiceRequest } from "./api/IService";
42
66
 
43
67
  // Registry 写管线(地址闸门 / 发布后绑定 / 下架解绑;限流复位给测试)
44
68
  export {
@@ -46,7 +70,7 @@ export {
46
70
  bindPublishedRegistryPage,
47
71
  resetRegistryWriteRateLimit,
48
72
  unbindArchivedRegistryPage,
49
- } from './registry/registry-write-pipeline';
73
+ } from "./registry/registry-write-pipeline";
50
74
 
51
75
  // 过期时间换算(信封 `expires` 的唯一算法)
52
- export { expiresToMinutes } from '../shared/page-envelope';
76
+ export { expiresToMinutes } from "../shared/page-envelope";
package/dist/cli/soma.js CHANGED
@@ -127,9 +127,9 @@ if (command === 'check') {
127
127
  process.exit(1);
128
128
  });
129
129
  }
130
- /** Next 宿主壳命令行:随 soma-core 包发布的 host-next/bin/host-next.cjs(包内相对定位,不去 node_modules 找外部包) */
131
- function locateHostNextCli() {
132
- const candidate = node_path_1.default.join(packageRoot, 'host-next', 'bin', 'host-next.cjs');
130
+ /** 原生宿主壳命令行:随 soma-core 包发布的 host-native/bin/host-native.cjs(包内相对定位,不去 node_modules 找外部包) */
131
+ function locateHostNativeCli() {
132
+ const candidate = node_path_1.default.join(packageRoot, 'host-native', 'bin', 'host-native.cjs');
133
133
  return node_fs_1.default.existsSync(candidate) ? candidate : null;
134
134
  }
135
135
  // check 的 runtime 阶段是异步收尾(上面 then 里 process.exit),不得落进下面的 dev/build/start 转调
@@ -138,20 +138,74 @@ if (command !== 'check') {
138
138
  console.error('Usage: soma <dev|build|start|codegen|sync|lsp|eject|check|runtime|doctor|扩展命令> [...args](soma help 查看说明)');
139
139
  process.exit(1);
140
140
  }
141
- // `soma dev|build|start`:Next 宿主壳(app/、next.config、proxy、Next 工作区装配)在包内 host-next/ 目录,
142
- // core 运行时(core/**)不 import 它、不含 Next / React;这里以子进程转调它的命令行,参数、环境变量与退出码透传。
141
+ // `soma dev|build|start`:原生宿主壳(AOT 预编译 + 原生单进程内核)在包内 host-native/ 目录,
142
+ // core 运行时(core/**)不 import 它;这里以子进程转调它的命令行,参数、环境变量与退出码透传。
143
143
  // Host 目录取当前目录,覆盖外部陈旧的 SOMA_HOST_DIR。
144
144
  const hostDir = node_path_1.default.resolve(process.cwd());
145
145
  process.env.SOMA_HOST_DIR = hostDir;
146
- const hostNextCli = locateHostNextCli();
147
- if (!hostNextCli) {
148
- console.error(`[soma ${command}] 找不到 Next 宿主壳入口 host-next/bin/host-next.cjs:soma-core 包不完整,请重新安装。`);
146
+ const restArgs = args.slice(1);
147
+ for (let i = 0; i < restArgs.length; i++) {
148
+ const arg = restArgs[i];
149
+ if (arg === '--web-dir' && i + 1 < restArgs.length) {
150
+ process.env.SOMA_WEB_DIR = node_path_1.default.resolve(process.cwd(), restArgs[i + 1]);
151
+ }
152
+ else if (arg.startsWith('--web-dir=')) {
153
+ process.env.SOMA_WEB_DIR = node_path_1.default.resolve(process.cwd(), arg.slice('--web-dir='.length));
154
+ }
155
+ else if (arg === '--api-dir' && i + 1 < restArgs.length) {
156
+ process.env.SOMA_API_DIR = node_path_1.default.resolve(process.cwd(), restArgs[i + 1]);
157
+ }
158
+ else if (arg.startsWith('--api-dir=')) {
159
+ process.env.SOMA_API_DIR = node_path_1.default.resolve(process.cwd(), arg.slice('--api-dir='.length));
160
+ }
161
+ }
162
+ const hostNativeCli = locateHostNativeCli();
163
+ if (!hostNativeCli) {
164
+ console.error(`[soma ${command}] 找不到原生宿主壳入口 host-native/bin/host-native.cjs:soma-core 包不完整,请重新安装。`);
149
165
  process.exit(1);
150
166
  }
151
- const child = (0, node_child_process_1.spawnSync)(process.execPath, [hostNextCli, command, ...args.slice(1)], {
152
- cwd: process.cwd(),
153
- stdio: 'inherit',
154
- env: { ...process.env, SOMA_HOST_DIR: hostDir },
155
- });
156
- process.exit(child.status === null ? 1 : child.status);
167
+ const hasHeapArg = process.execArgv.some((a) => a.includes('--max-old-space-size'))
168
+ || (process.env.NODE_OPTIONS || '').includes('--max-old-space-size');
169
+ const nodeArgs = [];
170
+ if (!hasHeapArg && !process.env.SOMA_DISABLE_HEAP_CAP) {
171
+ const defaultCap = command === 'dev' ? 1024 : 512;
172
+ const cap = process.env.SOMA_MAX_OLD_SPACE_MB
173
+ ? parseInt(process.env.SOMA_MAX_OLD_SPACE_MB, 10)
174
+ : defaultCap;
175
+ if (Number.isFinite(cap) && cap > 0) {
176
+ nodeArgs.push(`--max-old-space-size=${cap}`);
177
+ }
178
+ }
179
+ nodeArgs.push(hostNativeCli, command, ...args.slice(1));
180
+ const childEnv = {
181
+ ...process.env,
182
+ SOMA_HOST_DIR: hostDir,
183
+ SOMA_NATIVE_HEAP_APPLIED: '1',
184
+ };
185
+ if (command === 'build') {
186
+ const child = (0, node_child_process_1.spawnSync)(process.execPath, nodeArgs, {
187
+ cwd: process.cwd(),
188
+ stdio: 'inherit',
189
+ env: childEnv,
190
+ });
191
+ process.exit(child.status === null ? 1 : child.status);
192
+ }
193
+ else {
194
+ const child = (0, node_child_process_1.spawn)(process.execPath, nodeArgs, {
195
+ cwd: process.cwd(),
196
+ stdio: 'inherit',
197
+ env: childEnv,
198
+ });
199
+ const forward = (sig) => {
200
+ try {
201
+ child.kill(sig);
202
+ }
203
+ catch { }
204
+ };
205
+ process.on('SIGINT', () => forward('SIGINT'));
206
+ process.on('SIGTERM', () => forward('SIGTERM'));
207
+ child.on('exit', (code, signal) => {
208
+ process.exit(code ?? (signal ? 0 : 0));
209
+ });
210
+ }
157
211
  }