soma-core 2.0.1 → 2.1.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 (161) hide show
  1. package/README.md +2 -2
  2. package/bin/eject.cjs +5 -5
  3. package/bin/soma-runtime.cjs +5 -123
  4. package/bin/soma.cjs +2 -2
  5. package/bin/soma.ts +8 -20
  6. package/core/build/compiler/page-declaration-compiler.ts +5 -6
  7. package/core/build/lsp/soma-declaration.cjs +0 -6
  8. package/core/client/container/SomaContainerAdapter.ts +1 -1
  9. package/core/client/data/region-scope-views.ts +1 -1
  10. package/core/client/engine/event-engine.ts +1 -6
  11. package/core/client/engine/soma-bootstrap.ts +5 -13
  12. package/core/client/host-bridge/soma-bootstrap-from-app.ts +2 -2
  13. package/core/client/infra/i18n/II18n.ts +5 -6
  14. package/core/client/infra/i18n/locale-catalog.ts +8 -5
  15. package/core/client/infra/identity/identity-port.ts +1 -1
  16. package/core/client/keyboard/index.ts +0 -1
  17. package/core/client/keyboard/wiring.ts +0 -3
  18. package/core/client/parser/ui-config-parser.ts +6 -9
  19. package/core/client/registry/declaration-renderer.ts +1 -1
  20. package/core/client/runtime/SomaRuntimeServices.ts +1 -1
  21. package/core/inspect/check-runtime.ts +2 -3
  22. package/core/server/host/content-security-policy.ts +2 -11
  23. package/core/server/host/host-assembly-entry.ts +10 -2
  24. package/core/server/host/host-assembly.ts +28 -10
  25. package/core/server/host/host-caller.ts +52 -0
  26. package/core/server/host/host-response-headers.ts +49 -0
  27. package/core/server/host/locale-yaml.ts +7 -5
  28. package/core/server/host/published-page-assets.ts +1 -1
  29. package/core/server/host/resolve-host-request-hook.ts +2 -11
  30. package/core/server/host/resolve-host-tree-context.ts +7 -41
  31. package/core/server/host/server-page-assets.ts +0 -22
  32. package/core/server/host/source-packages.cjs +3 -4
  33. package/core/server/host/virtual-pages.ts +12 -70
  34. package/core/server/index.ts +1 -3
  35. package/core/server/node.ts +0 -1
  36. package/core/server/page-assets.ts +2 -20
  37. package/core/server/page-declaration.ts +1 -1
  38. package/core/server/registry/api-registry-store.ts +5 -25
  39. package/core/server/registry/asset-store.ts +5 -23
  40. package/core/server/registry/page-registry-repository.ts +9 -13
  41. package/core/server/registry/page-registry-store.ts +44 -82
  42. package/core/server/registry/publish-asset-pin.ts +3 -4
  43. package/core/server/registry/registry-subjects.ts +6 -6
  44. package/core/server/registry/rollback-plan.ts +5 -8
  45. package/core/server/registry/source/version-store.ts +8 -30
  46. package/core/server/registry/standard-product.ts +1 -1
  47. package/core/server/runtime-space/bindings.ts +1 -4
  48. package/core/server/runtime-space/gc.ts +2 -3
  49. package/core/server/runtime-space/inject.ts +4 -5
  50. package/core/server/runtime-space/resolve.ts +13 -27
  51. package/core/server/runtime-space/root.cjs +9 -100
  52. package/core/server/runtime-space/root.ts +3 -23
  53. package/core/server/runtime-space/types.ts +0 -11
  54. package/core/shared/artifact-compatibility.ts +6 -16
  55. package/core/shared/component-tree-artifact.ts +4 -8
  56. package/core/shared/core-diagnostic-codes.ts +1 -3
  57. package/core/shared/pipeline-error-codes-view.ts +1 -2
  58. package/core/shared/published-page-route.ts +1 -6
  59. package/core/shared/registry-generated/diagnostic-registry-index.ts +0 -2
  60. package/core/shared/registry-generated/diagnostic-registry.ts +0 -14
  61. package/core/shared/source-file-suffix.ts +8 -0
  62. package/core/shared/types/component-tree-artifact.types.ts +2 -15
  63. package/core/shared/types/host-assembly.types.ts +31 -2
  64. package/core/shared/types/locale.types.ts +3 -3
  65. package/core/shared/types/region-scope.types.ts +1 -1
  66. package/core/shared/types/ui-config.types.ts +2 -7
  67. package/core/shared/types/write.types.ts +1 -1
  68. package/dist/browser/soma-runtime.js +20 -36
  69. package/dist/cli/soma.js +8 -21
  70. package/dist/core/index.cjs +20 -38
  71. package/dist/lsp/component-tree.cjs +7 -14
  72. package/dist/types/client/container/SomaContainerAdapter.d.ts +1 -1
  73. package/dist/types/client/engine/event-engine.d.ts +0 -2
  74. package/dist/types/client/engine/soma-bootstrap.d.ts +2 -3
  75. package/dist/types/client/host-bridge/soma-bootstrap-from-app.d.ts +2 -2
  76. package/dist/types/client/infra/i18n/II18n.d.ts +5 -6
  77. package/dist/types/client/infra/i18n/locale-catalog.d.ts +3 -3
  78. package/dist/types/client/infra/identity/identity-port.d.ts +1 -1
  79. package/dist/types/client/keyboard/index.d.ts +1 -1
  80. package/dist/types/client/keyboard/wiring.d.ts +0 -2
  81. package/dist/types/client/registry/declaration-renderer.d.ts +1 -1
  82. package/dist/types/client/runtime/SomaRuntimeServices.d.ts +1 -1
  83. package/dist/types/server/host/content-security-policy.d.ts +0 -5
  84. package/dist/types/server/host/host-assembly-entry.d.ts +5 -2
  85. package/dist/types/server/host/host-assembly.d.ts +9 -4
  86. package/dist/types/server/host/host-caller.d.ts +10 -0
  87. package/dist/types/server/host/host-response-headers.d.ts +16 -0
  88. package/dist/types/server/host/published-page-assets.d.ts +1 -1
  89. package/dist/types/server/host/resolve-host-request-hook.d.ts +0 -4
  90. package/dist/types/server/host/resolve-host-tree-context.d.ts +1 -1
  91. package/dist/types/server/host/server-page-assets.d.ts +0 -2
  92. package/dist/types/server/host/virtual-pages.d.ts +4 -14
  93. package/dist/types/server/index.d.ts +1 -2
  94. package/dist/types/server/node.d.ts +1 -1
  95. package/dist/types/server/page-assets.d.ts +1 -4
  96. package/dist/types/server/page-declaration.d.ts +1 -1
  97. package/dist/types/server/registry/api-registry-store.d.ts +1 -6
  98. package/dist/types/server/registry/asset-store.d.ts +2 -11
  99. package/dist/types/server/registry/page-registry-repository.d.ts +4 -4
  100. package/dist/types/server/registry/page-registry-store.d.ts +22 -30
  101. package/dist/types/server/registry/registry-subjects.d.ts +2 -2
  102. package/dist/types/server/registry/rollback-plan.d.ts +0 -3
  103. package/dist/types/server/registry/source/version-store.d.ts +1 -9
  104. package/dist/types/server/registry/standard-product.d.ts +1 -1
  105. package/dist/types/server/runtime-space/resolve.d.ts +0 -3
  106. package/dist/types/server/runtime-space/root.d.cts +6 -29
  107. package/dist/types/server/runtime-space/root.d.ts +2 -7
  108. package/dist/types/server/runtime-space/types.d.ts +0 -9
  109. package/dist/types/shared/artifact-compatibility.d.ts +1 -3
  110. package/dist/types/shared/core-diagnostic-codes.d.ts +1 -1
  111. package/dist/types/shared/published-page-route.d.ts +1 -3
  112. package/dist/types/shared/registry-generated/diagnostic-registry.d.ts +0 -12
  113. package/dist/types/shared/types/component-tree-artifact.types.d.ts +2 -15
  114. package/dist/types/shared/types/host-assembly.types.d.ts +27 -1
  115. package/dist/types/shared/types/locale.types.d.ts +1 -1
  116. package/dist/types/shared/types/region-scope.types.d.ts +1 -1
  117. package/dist/types/shared/types/ui-config.types.d.ts +2 -7
  118. package/dist/types/shared/types/write.types.d.ts +1 -1
  119. package/global.d.ts +0 -9
  120. package/host-next/app/SomaPageClient.tsx +359 -0
  121. package/host-next/app/[...path]/page.tsx +22 -0
  122. package/host-next/app/api/[...path]/route.ts +98 -0
  123. package/host-next/app/api/soma-registry/[...path]/route.ts +79 -0
  124. package/host-next/app/components/SomaHostPage.tsx +128 -0
  125. package/host-next/app/components/SomaHydrationShell.tsx +20 -0
  126. package/host-next/app/components/SomaPageClientBoundary.tsx +13 -0
  127. package/host-next/app/components/SomaPageForbidden.tsx +19 -0
  128. package/host-next/app/favicon.ico/route.ts +13 -0
  129. package/host-next/app/globals.css +177 -0
  130. package/host-next/app/layout.tsx +27 -0
  131. package/host-next/app/page-access.ts +47 -0
  132. package/host-next/app/page.tsx +9 -0
  133. package/host-next/app/router-bridge.ts +42 -0
  134. package/host-next/app/soma/[...path]/route.ts +9 -0
  135. package/host-next/app/soma/_shared/host-prepare.ts +33 -0
  136. package/host-next/app/soma/_shared/page-http.ts +86 -0
  137. package/host-next/app/soma/manifest/route.ts +98 -0
  138. package/host-next/app/soma/page/route.ts +36 -0
  139. package/host-next/app/soma/route.ts +9 -0
  140. package/host-next/app/soma-runtime/[...file]/route.ts +21 -0
  141. package/host-next/app/soma-runtime/_shared/static-file.ts +89 -0
  142. package/host-next/bin/host-next.cjs +59 -0
  143. package/host-next/instrumentation.ts +13 -0
  144. package/host-next/next-env.d.ts +7 -0
  145. package/host-next/next.config.js +382 -0
  146. package/host-next/proxy.ts +43 -0
  147. package/host-next/src/shell-routes.cjs +51 -0
  148. package/host-next/src/workspace.ts +543 -0
  149. package/package.json +29 -4
  150. package/tsconfig.json +2 -1
  151. package/core/server/host/page-response-mode.cjs +0 -96
  152. package/core/server/host/page-response-request.ts +0 -33
  153. package/core/server/host/request-origin.ts +0 -18
  154. package/core/server/host/soma-request-hook-host.stub.ts +0 -4
  155. package/core/server/runtime-space/migrate-sources.ts +0 -124
  156. package/core/shared/source-compat.ts +0 -9
  157. package/dist/types/server/host/page-response-request.d.ts +0 -8
  158. package/dist/types/server/host/request-origin.d.ts +0 -1
  159. package/dist/types/server/host/soma-request-hook-host.stub.d.ts +0 -2
  160. package/dist/types/server/runtime-space/migrate-sources.d.ts +0 -19
  161. /package/dist/types/shared/{source-compat.d.ts → source-file-suffix.d.ts} +0 -0
@@ -0,0 +1,98 @@
1
+ import { loadPrecompiledManifest, findPrecompiledPage } from '../../../core/server/page-assets';
2
+ import { listBindings } from '../../../core/server/runtime-space';
3
+ import { resolveSomaRuntimeLayout } from '../../../core/server/runtime-space/root';
4
+ import { SomaRuntimeStore } from '../../../core/server/runtime-space/store';
5
+ import { normalizeAddress } from '../../../core/shared/runtime-address';
6
+ import type { SomaRuntimeBinding } from '../../../core/server/runtime-space/types';
7
+
8
+ export const runtime = 'nodejs';
9
+ export const dynamic = 'force-dynamic';
10
+
11
+ /** 绑定的公开视图:只有地址、类别、来源类别、缓存、代次与摘要(不带 caller / source / 到期等产生方信息) */
12
+ function bindingView(binding: SomaRuntimeBinding) {
13
+ return {
14
+ address: binding.address,
15
+ kind: binding.kind,
16
+ origin: binding.origin,
17
+ cache: binding.cache,
18
+ purpose: binding.purpose,
19
+ generation: binding.generation,
20
+ digest: binding.digest,
21
+ };
22
+ }
23
+
24
+ function readPrewarmEntries(hostDir: string): Readonly<Record<string, string>> {
25
+ try {
26
+ return new SomaRuntimeStore(resolveSomaRuntimeLayout(hostDir, process.env).dirs, { cacheEntries: 1 }).readPrewarmIndex().entries;
27
+ } catch {
28
+ return {};
29
+ }
30
+ }
31
+
32
+ /** 精确地址命中:manifest 只服务「已绑定的精确地址」,模式 / 前缀绑定的页面按请求解析,没有构建期产物 */
33
+ function findExactBinding(bindings: readonly SomaRuntimeBinding[], address: string): SomaRuntimeBinding | null {
34
+ return bindings.find((binding) => binding.kind === 'exact' && binding.address === address) ?? null;
35
+ }
36
+
37
+ /**
38
+ * GET /soma/manifest?path=/items - 预编译页面配置(不跑 Host 钩子)。
39
+ * 以 runtime 空间的绑定表(`listBindings()`)为准:地址未绑定 404;绑定声明 no-store 或用途是预览的页面不出预编译产物(404,
40
+ * 客户端走 /soma/page)。命中后产物来自预热(`<root>/prewarm/`)。
41
+ * GET /soma/manifest?index=1 - 绑定清单 + 预热索引(地址 → 摘要)。
42
+ */
43
+ export async function GET(request: Request): Promise<Response> {
44
+ const hostDir = process.env.SOMA_HOST_DIR;
45
+ if (!hostDir) {
46
+ return Response.json({ error: 'SOMA_HOST_DIR not set' }, { status: 500 });
47
+ }
48
+
49
+ const url = new URL(request.url);
50
+ const bindings = listBindings();
51
+
52
+ if (url.searchParams.get('index') === '1') {
53
+ return Response.json(
54
+ { bindings: bindings.map(bindingView), prewarm: readPrewarmEntries(hostDir) },
55
+ { headers: { 'cache-control': 'no-store' } },
56
+ );
57
+ }
58
+
59
+ const rawPath = url.searchParams.get('path') ?? '/';
60
+ const pagePath = normalizeAddress(rawPath);
61
+ if (pagePath === null) {
62
+ return Response.json({ error: `invalid page path: ${rawPath}` }, { status: 400 });
63
+ }
64
+ const binding = findExactBinding(bindings, pagePath);
65
+ if (binding === null) {
66
+ return Response.json({ error: `page not bound: ${pagePath}` }, { status: 404 });
67
+ }
68
+ if (binding.cache !== 'store' || binding.purpose === 'preview') {
69
+ return Response.json({ error: `page has no precompiled artifact: ${pagePath}` }, { status: 404 });
70
+ }
71
+
72
+ const manifest = loadPrecompiledManifest(hostDir);
73
+ if (!manifest) {
74
+ return Response.json({ error: 'manifest not found' }, { status: 404 });
75
+ }
76
+
77
+ const page = findPrecompiledPage(manifest, pagePath);
78
+ if (!page) {
79
+ return Response.json({ error: `page not in manifest: ${pagePath}` }, { status: 404 });
80
+ }
81
+
82
+ return Response.json({
83
+ path: page.path,
84
+ sourceMarkup: page.sourceMarkup,
85
+ html: page.html,
86
+ artifact: page.artifact,
87
+ uiConfig: page.uiConfig,
88
+ eventConfig: page.eventConfig,
89
+ triggers: page.triggers,
90
+ timers: page.timers ?? [],
91
+ styleConfig: page.styleConfig,
92
+ dataConfig: page.dataConfig,
93
+ locale: manifest.locale,
94
+ appConfig: page.appConfig ?? manifest.appConfig,
95
+ binding: bindingView(binding),
96
+ manifestVersion: '1.0.0',
97
+ });
98
+ }
@@ -0,0 +1,36 @@
1
+ import { NextRequest } from 'next/server';
2
+ import { loadPageByAddress } from '../../../core/server/runtime-space';
3
+ import { pageErrorResponse, pageSuccessResponse } from '../_shared/page-http';
4
+ import { prepareHostAssembly } from '../_shared/host-prepare';
5
+ import { withHostResponseHeaders } from '../../../core/server/host/host-response-headers';
6
+
7
+ export const runtime = 'nodejs';
8
+ export const dynamic = 'force-dynamic';
9
+
10
+ /**
11
+ * SOMA internal page asset endpoint. Host request hooks must never run here.
12
+ * 只调 `loadPageByAddress`(runtime 空间的唯一取页入口):未命中 404,同址冲突 409,参数类型不合 404,
13
+ * 访问门 401(附登录路径,页面客户端跳转)/ 403(附词条键 `soma.page.forbidden`)。
14
+ * 响应在原运行时资产之上带 binding / params / query / nav / purpose;缓存头按绑定的 `cache` 设(见 `_shared/page-http.ts`)。
15
+ */
16
+ export async function GET(request: NextRequest): Promise<Response> {
17
+ return withHostResponseHeaders(await loadPageResponse(request), {
18
+ method: request.method,
19
+ pathname: request.nextUrl.pathname,
20
+ });
21
+ }
22
+
23
+ async function loadPageResponse(request: NextRequest): Promise<Response> {
24
+ const pagePath = request.nextUrl.searchParams.get('path') || '/';
25
+ let assembly: Awaited<ReturnType<typeof prepareHostAssembly>> = null;
26
+ try {
27
+ // Host 请求钩子不在这里跑,但编译点同样依赖 soma.app.ts 的服务端面已装配完:先等它(装配失败即拒绝启动,原样 500)
28
+ assembly = await prepareHostAssembly(process.env.SOMA_HOST_DIR);
29
+ const page = await loadPageByAddress(pagePath, request, {
30
+ hostDir: process.env.SOMA_HOST_DIR,
31
+ });
32
+ return pageSuccessResponse(page, page, request);
33
+ } catch (error) {
34
+ return pageErrorResponse(error, assembly);
35
+ }
36
+ }
@@ -0,0 +1,9 @@
1
+ function somaRouteNotFound(): Response {
2
+ return Response.json({ error: 'SOMA internal endpoint not found' }, { status: 404 });
3
+ }
4
+
5
+ export const GET = somaRouteNotFound;
6
+ export const POST = somaRouteNotFound;
7
+ export const PUT = somaRouteNotFound;
8
+ export const PATCH = somaRouteNotFound;
9
+ export const DELETE = somaRouteNotFound;
@@ -0,0 +1,21 @@
1
+ import { staticResponse } from '../_shared/static-file';
2
+
3
+ export const runtime = 'nodejs';
4
+ export const dynamic = 'force-dynamic';
5
+
6
+ /** GET|HEAD /soma-runtime/<文件> - 读 `<runtime 根>/static/` 下的静态文件(浏览器运行包等);其余方法 405 */
7
+ export async function GET(request: Request, context: { params: Promise<{ file: string[] }> }): Promise<Response> {
8
+ const { file } = await context.params;
9
+ return staticResponse(file ?? [], request);
10
+ }
11
+
12
+ export const HEAD = GET;
13
+
14
+ function methodNotAllowed(): Response {
15
+ return new Response('Method not allowed', { status: 405, headers: { allow: 'GET, HEAD', 'cache-control': 'no-store' } });
16
+ }
17
+
18
+ export const POST = methodNotAllowed;
19
+ export const PUT = methodNotAllowed;
20
+ export const PATCH = methodNotAllowed;
21
+ export const DELETE = methodNotAllowed;
@@ -0,0 +1,89 @@
1
+ // /soma-runtime/* 静态路由的文件解析与响应:读 `<runtime 根>/static/` 下的文件(浏览器运行包等),只读。
2
+ // 安全:路径段逐个校验(拒 `..`、`.` 起首、反斜杠、空段、控制字符与 NUL);解析后做 realpath,落在 static 根之外
3
+ // (符号链接逃逸)一律 404;只服务普通文件。不泄漏 runtime 根的绝对路径。
4
+ import fs from 'node:fs';
5
+ import path from 'node:path';
6
+ import { createHash } from 'node:crypto';
7
+ import { resolveSomaRuntimeLayout } from '../../../core/server/runtime-space/root';
8
+
9
+ const CONTENT_TYPES: Readonly<Record<string, string>> = {
10
+ '.js': 'text/javascript; charset=utf-8',
11
+ '.mjs': 'text/javascript; charset=utf-8',
12
+ '.map': 'application/json; charset=utf-8',
13
+ '.json': 'application/json; charset=utf-8',
14
+ '.css': 'text/css; charset=utf-8',
15
+ '.txt': 'text/plain; charset=utf-8',
16
+ '.wasm': 'application/wasm',
17
+ '.svg': 'image/svg+xml',
18
+ '.png': 'image/png',
19
+ '.jpg': 'image/jpeg',
20
+ '.jpeg': 'image/jpeg',
21
+ '.webp': 'image/webp',
22
+ '.woff': 'font/woff',
23
+ '.woff2': 'font/woff2',
24
+ };
25
+
26
+ /** 文件名带内容哈希(`name-3fa9c0de.js`)才可 immutable;其余短缓存 + 条件请求 */
27
+ const HASHED_NAME = /[-.][0-9a-f]{8,}\.[a-z0-9]+$/i;
28
+
29
+ export type StaticResolution =
30
+ | { readonly ok: true; readonly file: string; readonly size: number; readonly mtimeMs: number }
31
+ | { readonly ok: false };
32
+
33
+ export function isSafeSegments(segments: readonly string[]): boolean {
34
+ return segments.length > 0 && segments.every((segment) => (
35
+ segment !== '' && !segment.startsWith('.') && !/[\\/\u0000-\u001f]/.test(segment)
36
+ ));
37
+ }
38
+
39
+ export function resolveStaticFile(staticRoot: string, segments: readonly string[]): StaticResolution {
40
+ if (!isSafeSegments(segments)) return { ok: false };
41
+ try {
42
+ const rootReal = fs.realpathSync(staticRoot);
43
+ const candidate = path.join(rootReal, ...segments);
44
+ const real = fs.realpathSync(candidate);
45
+ if (real !== rootReal && !real.startsWith(rootReal + path.sep)) return { ok: false };
46
+ const stat = fs.statSync(real);
47
+ if (!stat.isFile()) return { ok: false };
48
+ return { ok: true, file: real, size: stat.size, mtimeMs: stat.mtimeMs };
49
+ } catch {
50
+ return { ok: false };
51
+ }
52
+ }
53
+
54
+ export function staticRootOf(hostDir: string, env: Readonly<Record<string, string | undefined>> = process.env): string {
55
+ return resolveSomaRuntimeLayout(hostDir, env).dirs.static;
56
+ }
57
+
58
+ export function staticResponse(
59
+ segments: readonly string[],
60
+ request: { readonly method: string; readonly headers: { get(name: string): string | null } },
61
+ env: Readonly<Record<string, string | undefined>> = process.env,
62
+ ): Response {
63
+ const notFound = () => new Response('Not found', { status: 404, headers: { 'cache-control': 'no-store', 'content-type': 'text/plain; charset=utf-8' } });
64
+ const hostDir = env.SOMA_HOST_DIR;
65
+ if (!hostDir) return notFound();
66
+ let root: string;
67
+ try {
68
+ root = staticRootOf(hostDir, env);
69
+ } catch {
70
+ return notFound(); // 越界的 runtime 根由 core 启动期拒绝;这里不透露细节
71
+ }
72
+ const resolved = resolveStaticFile(root, segments);
73
+ if (!resolved.ok) return notFound();
74
+
75
+ const etag = `W/"${createHash('sha1').update(`${resolved.size}:${resolved.mtimeMs}`).digest('hex').slice(0, 20)}"`;
76
+ const hashed = HASHED_NAME.test(path.basename(resolved.file));
77
+ const headers: Record<string, string> = {
78
+ 'content-type': CONTENT_TYPES[path.extname(resolved.file).toLowerCase()] ?? 'application/octet-stream',
79
+ 'cache-control': hashed ? 'public, max-age=31536000, immutable' : 'public, max-age=300, must-revalidate',
80
+ etag,
81
+ 'x-content-type-options': 'nosniff',
82
+ };
83
+ if (request.headers.get('if-none-match')?.split(',').some((value) => value.trim() === etag)) {
84
+ return new Response(null, { status: 304, headers });
85
+ }
86
+ headers['content-length'] = String(resolved.size);
87
+ if (request.method.toUpperCase() === 'HEAD') return new Response(null, { status: 200, headers });
88
+ return new Response(new Uint8Array(fs.readFileSync(resolved.file)), { status: 200, headers });
89
+ }
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Next 宿主壳命令行:`host-next.cjs <dev|build|start> [next 参数...]`(soma-core 包内,不单独发布成 bin)。
4
+ *
5
+ * 在 Host 目录(SOMA_HOST_DIR,缺省当前目录)下生成 Next 工作区(src/workspace.ts 的 ensureNextWorkspace),
6
+ * 再以子进程跑 Next,标准流与退出码透传。`soma dev|build|start`(bin/soma.ts)经子进程转调本入口。
7
+ * 源码是 TypeScript,经运行时依赖 jiti 直接加载。
8
+ */
9
+ const path = require('node:path');
10
+ const { spawnSync } = require('node:child_process');
11
+
12
+ const packageRoot = path.resolve(__dirname, '..');
13
+ const [command, ...rest] = process.argv.slice(2);
14
+
15
+ if (!['dev', 'build', 'start'].includes(command)) {
16
+ console.error('Usage: host-next.cjs <dev|build|start> [...next args]');
17
+ process.exit(1);
18
+ }
19
+
20
+ const createJiti = require('jiti');
21
+ const load = createJiti(__filename, { interopDefault: true });
22
+ const { ensureNextWorkspace, resolveNextBin } = load(path.join(packageRoot, 'src', 'workspace.ts'));
23
+
24
+ const nextBin = resolveNextBin();
25
+ if (!nextBin) {
26
+ console.error('[soma] next not found. Install next, react and react-dom next to soma-core (soma-core lists them as optional peer dependencies).');
27
+ process.exit(1);
28
+ }
29
+
30
+ const hostDir = path.resolve(process.env.SOMA_HOST_DIR && process.env.SOMA_HOST_DIR.trim() !== ''
31
+ ? process.env.SOMA_HOST_DIR
32
+ : process.cwd());
33
+ let nextWorkspace;
34
+ try {
35
+ nextWorkspace = ensureNextWorkspace(hostDir);
36
+ } catch (error) {
37
+ console.error(error instanceof Error ? error.message : String(error));
38
+ process.exit(1);
39
+ }
40
+
41
+ const child = spawnSync(process.execPath, [
42
+ nextBin,
43
+ command,
44
+ nextWorkspace,
45
+ ...((command === 'dev' || command === 'build') ? ['--webpack'] : []),
46
+ ...rest,
47
+ ], {
48
+ cwd: nextWorkspace,
49
+ stdio: 'inherit',
50
+ env: {
51
+ ...process.env,
52
+ SOMA_HOST_DIR: hostDir,
53
+ ...(process.env.SOMA_NEXT_WORKSPACE_DIR === undefined
54
+ ? {}
55
+ : { SOMA_NEXT_WORKSPACE_DIR: nextWorkspace }),
56
+ },
57
+ });
58
+
59
+ process.exit(child.status === null ? 1 : child.status);
@@ -0,0 +1,13 @@
1
+ // Next instrumentation:服务启动时执行 Host 装配的 `startup()`(一次),失败即拒绝启动。
2
+ //
3
+ // 时机:`soma dev` / `soma start` 的服务进程启动时(Node 运行时);`soma build` 不执行(build 只跑装配的 `prewarm`)。
4
+ // 这里加载 `soma.app.ts` 并等服务端面(`server()`)装配完,再 await `startup()`;装配或启动钩子抛错,`register` 随之抛错,
5
+ // 服务不进入可用状态(`soma start` 进程报错退出)。没有 `soma.app.ts` 或装配没写 `startup` 时只完成装配(无副作用)。
6
+ export async function register(): Promise<void> {
7
+ if (process.env.NEXT_RUNTIME !== 'nodejs') return;
8
+ if (process.env.NEXT_PHASE === 'phase-production-build') return;
9
+ const hostDir = process.env.SOMA_HOST_DIR;
10
+ if (!hostDir || !hostDir.trim()) return;
11
+ const { runHostStartup } = await import('./core/server/host/host-assembly');
12
+ await runHostStartup(hostDir);
13
+ }
@@ -0,0 +1,7 @@
1
+ /// <reference types="next" />
2
+ /// <reference types="next/image-types/global" />
3
+ import "./.next/types/routes.d.ts";
4
+ import "./.next/types/root-params.d.ts";
5
+
6
+ // NOTE: This file should not be edited
7
+ // see https://nextjs.org/docs/app/api-reference/config/typescript for more information.