soma-core 2.1.6 → 2.2.1
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 +2 -2
- package/bin/soma.cjs +1 -1
- package/bin/soma.ts +64 -15
- package/core/server/api/dispatch-service-api.ts +212 -0
- package/core/server/api/load-host-service.ts +230 -56
- package/core/server/api/service-path.ts +79 -14
- package/core/server/engines/style/candidate-guard.ts +57 -0
- package/core/server/engines/style/index.ts +1 -1
- package/core/server/engines/style/tailwind-adapter.ts +83 -16
- package/core/server/host/source-packages.cjs +98 -30
- package/core/server/host/source-packages.ts +12 -5
- package/core/server/index.ts +39 -17
- package/core/server/infra/cache/ServerFileCache.ts +138 -25
- package/core/server/node.ts +32 -8
- package/dist/cli/soma.js +68 -14
- package/dist/host-native/aot-builder.cjs +852 -0
- package/dist/host-native/native-server.cjs +15341 -0
- package/dist/types/server/api/dispatch-service-api.d.ts +18 -0
- package/dist/types/server/api/load-host-service.d.ts +7 -2
- package/dist/types/server/api/service-path.d.ts +21 -1
- package/dist/types/server/engines/style/candidate-guard.d.ts +10 -0
- package/dist/types/server/engines/style/index.d.ts +1 -1
- package/dist/types/server/engines/style/tailwind-adapter.d.ts +12 -0
- package/dist/types/server/host/source-packages.d.cts +13 -1
- package/dist/types/server/host/source-packages.d.ts +7 -4
- package/dist/types/server/index.d.ts +18 -17
- package/dist/types/server/infra/cache/ServerFileCache.d.ts +28 -5
- package/dist/types/server/node.d.ts +10 -7
- package/host-native/bin/host-native.cjs +204 -0
- package/host-native/client/browser-entry.ts +499 -0
- package/{host-next/app → host-native/client}/page-access.ts +12 -0
- package/host-native/client/router-bridge.ts +120 -0
- package/host-native/src/aot-builder.ts +486 -0
- package/host-native/src/csp-headers.ts +75 -0
- package/host-native/src/host-prepare.ts +123 -0
- package/{host-next/app/soma/manifest/route.ts → host-native/src/manifest-http.ts} +10 -10
- package/host-native/src/native-server.ts +691 -0
- package/host-native/src/page-http.ts +223 -0
- package/host-native/src/public-packages.cjs +167 -0
- package/{host-next/app/api/soma-registry/[...path]/route.ts → host-native/src/registry-handler.ts} +17 -19
- package/{host-next → host-native}/src/shell-routes.cjs +23 -13
- package/host-native/src/static-file.ts +172 -0
- package/host-native/src/workspace.ts +185 -0
- package/{host-next/app → host-native/styles}/globals.css +2 -2
- package/package.json +12 -31
- package/tsconfig.json +1 -1
- package/host-next/app/SomaPageClient.tsx +0 -359
- package/host-next/app/[...path]/page.tsx +0 -22
- package/host-next/app/api/[...path]/route.ts +0 -98
- package/host-next/app/components/SomaHostPage.tsx +0 -128
- package/host-next/app/components/SomaHydrationShell.tsx +0 -20
- package/host-next/app/components/SomaPageClientBoundary.tsx +0 -13
- package/host-next/app/components/SomaPageForbidden.tsx +0 -19
- package/host-next/app/favicon.ico/route.ts +0 -13
- package/host-next/app/layout.tsx +0 -27
- package/host-next/app/page.tsx +0 -9
- package/host-next/app/router-bridge.ts +0 -42
- package/host-next/app/soma/[...path]/route.ts +0 -9
- package/host-next/app/soma/_shared/host-prepare.ts +0 -33
- package/host-next/app/soma/_shared/page-http.ts +0 -86
- package/host-next/app/soma/page/route.ts +0 -36
- package/host-next/app/soma/route.ts +0 -9
- package/host-next/app/soma-runtime/[...file]/route.ts +0 -21
- package/host-next/app/soma-runtime/_shared/static-file.ts +0 -89
- package/host-next/bin/host-next.cjs +0 -60
- package/host-next/instrumentation.ts +0 -18
- package/host-next/next-env.d.ts +0 -7
- package/host-next/next.config.js +0 -410
- package/host-next/proxy.ts +0 -43
- package/host-next/src/workspace.ts +0 -543
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface RequestCookie {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly value: string;
|
|
4
|
+
}
|
|
5
|
+
export interface RequestCookiesAdapter {
|
|
6
|
+
get(name: string): RequestCookie | undefined;
|
|
7
|
+
getAll(name?: string): RequestCookie[];
|
|
8
|
+
has(name: string): boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface DispatchHostServiceApiOptions {
|
|
11
|
+
readonly segments?: readonly string[];
|
|
12
|
+
readonly hostDir?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function polyfillServiceRequest<T extends Request>(request: T): T & {
|
|
15
|
+
nextUrl: URL;
|
|
16
|
+
cookies: RequestCookiesAdapter;
|
|
17
|
+
};
|
|
18
|
+
export declare function dispatchHostServiceApiRequest(request: Request, segmentsOrOptions?: readonly string[] | DispatchHostServiceApiOptions): Promise<Response>;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import type { IService } from
|
|
1
|
+
import type { IService } from "./IService";
|
|
2
|
+
export type AotServiceResolver = (routeKey: string, segments: readonly string[]) => unknown | Promise<unknown>;
|
|
3
|
+
export declare function resolveServiceExport(loaded: unknown, filePath?: string): IService | null;
|
|
4
|
+
export declare function registerAotServiceResolver(hostDir: string, resolver: AotServiceResolver | null): void;
|
|
5
|
+
export declare function getAotServiceResolver(hostDir: string): AotServiceResolver | undefined;
|
|
6
|
+
export declare function clearHostServiceCache(hostDir?: string): void;
|
|
2
7
|
/**
|
|
3
|
-
* 按约定从宿主
|
|
8
|
+
* 按约定从宿主 apiRoot 加载 IService 实例(运行时按需加载,零手动 registry)。
|
|
4
9
|
*/
|
|
5
10
|
export declare function loadHostService(segments: string[]): IService | null;
|
|
@@ -3,11 +3,31 @@
|
|
|
3
3
|
* @example "user-profile" → "UserProfile", "oauth_token" → "OauthToken"
|
|
4
4
|
*/
|
|
5
5
|
export declare function toPascalCase(str: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* 校验服务路由片段的安全与合法性,并将末段解析为 PascalCase 类名。
|
|
8
|
+
* 拒收:空片段、"." / ".."、以 "_" 起头的私有模块、含 "/" / "\\" / "\0" 的穿越字符。
|
|
9
|
+
*/
|
|
10
|
+
export declare function validateServiceSegments(segments: readonly string[]): {
|
|
11
|
+
dirSegments: string[];
|
|
12
|
+
className: string;
|
|
13
|
+
} | null;
|
|
6
14
|
/**
|
|
7
15
|
* 从 /api/{...path} 片段解析宿主服务文件绝对路径。
|
|
8
16
|
*
|
|
9
17
|
* 规则(与文档 12.4 一致):
|
|
10
|
-
* - ["
|
|
18
|
+
* - ["products"] → {host}/api/service/Products.ts
|
|
11
19
|
* - ["user", "profile"] → {host}/api/service/user/Profile.ts
|
|
12
20
|
*/
|
|
13
21
|
export declare function resolveServiceFilePath(hostDir: string, segments: string[]): string | null;
|
|
22
|
+
export interface ResolveHostServiceCandidateOptions {
|
|
23
|
+
readonly allowDirectRoot?: boolean;
|
|
24
|
+
readonly extensions?: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 在给定后端根 apiRoot 下按顺序探测真实存在的服务文件(默认后缀顺序 [".ts", ".js", ".mjs", ".cjs"]):
|
|
28
|
+
* 1) <apiRoot>/api/service/<...dirSegments>/<ClassName><ext>
|
|
29
|
+
* 2) <apiRoot>/service/<...dirSegments>/<ClassName><ext>
|
|
30
|
+
* 3) 当 options?.allowDirectRoot === true 时额外探测 <apiRoot>/<...dirSegments>/<ClassName><ext>
|
|
31
|
+
* 每一步都必须校验 resolved.startsWith(normalizedAllowedBase + path.sep) 且 fs.statSync(resolved, { throwIfNoEntry: false })?.isFile() === true
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveHostServiceCandidate(apiRoot: string, segments: string[], options?: ResolveHostServiceCandidateOptions): string | null;
|
|
@@ -12,3 +12,13 @@ export declare function guardCandidates(candidates: readonly string[], allow: (c
|
|
|
12
12
|
export declare function escapeClassName(name: string): string;
|
|
13
13
|
/** 产出的样式表里是否有以该候选类为类选择器的规则(不把更长的类名当命中) */
|
|
14
14
|
export declare function hasRuleForCandidate(css: string, candidate: string): boolean;
|
|
15
|
+
/** 从 CSS 选择器片段单遍提取所有类名(已解码转义) */
|
|
16
|
+
export declare function extractClassesFromCss(css: string): Set<string>;
|
|
17
|
+
/**
|
|
18
|
+
* 批量判定编译产物中是否包含指定候选类的规则。
|
|
19
|
+
* O(1) 查表命中 99.9% 候选类,未命中时回退 hasRuleForCandidate 兜底复杂选择器,结果逐字等价。
|
|
20
|
+
*/
|
|
21
|
+
export declare function partitionCompiledCandidates(css: string, candidates: readonly string[]): {
|
|
22
|
+
matched: string[];
|
|
23
|
+
unmatched: string[];
|
|
24
|
+
};
|
|
@@ -3,7 +3,7 @@ import type { EngineLoadTelemetry } from '../../../client/engines/load';
|
|
|
3
3
|
export type { SomaStyleEngine, StyleCompileInput, StyleCompileRejection, StyleCompileResult, } from '../../../shared/engines/style/style-engine.types';
|
|
4
4
|
export { assertStyleEngineAdapter, assertThemeCss } from './port';
|
|
5
5
|
export { guardCandidates } from './candidate-guard';
|
|
6
|
-
export { createTailwindAdapter } from './tailwind-adapter';
|
|
6
|
+
export { createTailwindAdapter, clearTailwindCompilerCache } from './tailwind-adapter';
|
|
7
7
|
export interface StyleEnginePortOptions {
|
|
8
8
|
readonly telemetry?: EngineLoadTelemetry;
|
|
9
9
|
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import type { SomaStyleEngine } from '../../../shared/engines/style/style-engine.types';
|
|
2
2
|
import { type EngineLoadTelemetry } from '../../../client/engines/load';
|
|
3
|
+
export interface TailwindCompiler {
|
|
4
|
+
build(candidates: string[]): string;
|
|
5
|
+
}
|
|
6
|
+
export interface TailwindModule {
|
|
7
|
+
compile(css: string, options?: {
|
|
8
|
+
base?: string;
|
|
9
|
+
}): Promise<TailwindCompiler>;
|
|
10
|
+
}
|
|
3
11
|
export interface TailwindAdapterOptions {
|
|
4
12
|
readonly telemetry?: EngineLoadTelemetry;
|
|
13
|
+
readonly base?: string;
|
|
14
|
+
readonly loadTailwind?: () => Promise<TailwindModule>;
|
|
5
15
|
}
|
|
16
|
+
/** 清除 Tailwind 编译器与模块缓存(供内部测试重置) */
|
|
17
|
+
export declare function clearTailwindCompilerCache(): void;
|
|
6
18
|
export declare function createTailwindAdapter(adapterOptions?: TailwindAdapterOptions): SomaStyleEngine;
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
export type SomaEnv = Readonly<Record<string, string | undefined>>;
|
|
2
|
+
export type SomaAssetSourceItem = {
|
|
3
|
+
type: "host" | "package";
|
|
4
|
+
name: string;
|
|
5
|
+
assetRoot: string;
|
|
6
|
+
codeRoot: string;
|
|
7
|
+
apiRoot: string;
|
|
8
|
+
configRoot: string;
|
|
9
|
+
packageName?: string;
|
|
10
|
+
};
|
|
2
11
|
/** 环境变量:显式声明的源包名(逗号分隔),不论其 package.json 怎么写都当源包 */
|
|
3
12
|
export const SOMA_SOURCE_PACKAGES_ENV: "SOMA_SOURCE_PACKAGES";
|
|
4
|
-
export function getSomaHostSourceRoot(hostDir: any): string;
|
|
13
|
+
export function getSomaHostSourceRoot(hostDir: any, env?: NodeJS.ProcessEnv): string;
|
|
14
|
+
export function getSomaHostApiRoot(hostDir: any, env?: NodeJS.ProcessEnv): string;
|
|
15
|
+
export function clearSomaAssetSourcesCache(): void;
|
|
5
16
|
/** Host 自己 + 全部源包(依赖里声明的与环境变量显式声明的);Host 恒在第一位 */
|
|
6
17
|
/** @param {SomaEnv} [env] */
|
|
7
18
|
export function getSomaAssetSources(hostDir: any, env?: SomaEnv): {
|
|
@@ -9,6 +20,7 @@ export function getSomaAssetSources(hostDir: any, env?: SomaEnv): {
|
|
|
9
20
|
name: string;
|
|
10
21
|
assetRoot: string;
|
|
11
22
|
codeRoot: string;
|
|
23
|
+
apiRoot: string;
|
|
12
24
|
configRoot: any;
|
|
13
25
|
}[];
|
|
14
26
|
/** 源包名清单(next.config 的 transpilePackages 用) */
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
export interface SomaAssetSource {
|
|
2
|
-
type:
|
|
2
|
+
type: "host" | "package";
|
|
3
3
|
name: string;
|
|
4
4
|
assetRoot: string;
|
|
5
5
|
codeRoot: string;
|
|
6
|
+
apiRoot: string;
|
|
6
7
|
configRoot: string;
|
|
7
8
|
packageName?: string;
|
|
8
9
|
}
|
|
9
|
-
export { getSomaHostConfigRoot, normalizeSomaEnv, readSomaEnv, resolveAppConfigPath, resolveHostAppConfigPath, resolveHostConfigRoot, } from
|
|
10
|
-
export type { SomaEnvName } from
|
|
10
|
+
export { getSomaHostConfigRoot, normalizeSomaEnv, readSomaEnv, resolveAppConfigPath, resolveHostAppConfigPath, resolveHostConfigRoot, } from "./host-config-paths";
|
|
11
|
+
export type { SomaEnvName } from "./host-config-paths";
|
|
11
12
|
/** 实现只有 `source-packages.cjs` 一份(RT3 单实现);这里只补类型再导出,不另写第二份。 */
|
|
12
|
-
export declare const getSomaHostSourceRoot: (hostDir: string) => string;
|
|
13
|
+
export declare const getSomaHostSourceRoot: (hostDir: string, env?: Readonly<Record<string, string | undefined>>) => string;
|
|
14
|
+
export declare const getSomaHostApiRoot: (hostDir: string, env?: Readonly<Record<string, string | undefined>>) => string;
|
|
15
|
+
export declare const clearSomaAssetSourcesCache: () => void;
|
|
13
16
|
/** Host 自己 + 全部源包;Host 恒在第一位 */
|
|
14
17
|
export declare const getSomaAssetSources: (hostDir: string, env?: Readonly<Record<string, string | undefined>>) => SomaAssetSource[];
|
|
15
18
|
export declare const listSomaSourcePackageNames: (hostDir: string, env?: Readonly<Record<string, string | undefined>>) => string[];
|
|
@@ -7,20 +7,21 @@
|
|
|
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
|
|
11
|
-
export { ServerFileCache } from
|
|
12
|
-
export { withSomaServerRequestContext, type SomaServerRequestContextOptions, } from
|
|
13
|
-
export type { IService, SomaServiceRequest } from
|
|
14
|
-
export { loadHostService } from
|
|
15
|
-
export { resolveServiceFilePath, toPascalCase } from
|
|
16
|
-
export {
|
|
17
|
-
export {
|
|
18
|
-
export {
|
|
19
|
-
export {
|
|
20
|
-
export {
|
|
21
|
-
export
|
|
22
|
-
export {
|
|
23
|
-
export
|
|
24
|
-
export {
|
|
25
|
-
export {
|
|
26
|
-
export
|
|
10
|
+
import "server-only";
|
|
11
|
+
export { ServerFileCache } from "./infra/cache/ServerFileCache";
|
|
12
|
+
export { withSomaServerRequestContext, type SomaServerRequestContextOptions, } from "./infra/http/soma-server-request-context";
|
|
13
|
+
export type { IService, SomaServiceRequest } from "./api/IService";
|
|
14
|
+
export { clearHostServiceCache, getAotServiceResolver, loadHostService, registerAotServiceResolver, resolveServiceExport, type AotServiceResolver, } from "./api/load-host-service";
|
|
15
|
+
export { resolveHostServiceCandidate, resolveServiceFilePath, toPascalCase, validateServiceSegments, type ResolveHostServiceCandidateOptions, } from "./api/service-path";
|
|
16
|
+
export { dispatchHostServiceApiRequest, polyfillServiceRequest, type DispatchHostServiceApiOptions, type RequestCookie, type RequestCookiesAdapter, } from "./api/dispatch-service-api";
|
|
17
|
+
export { invokeHostRequestHook } from "./host/invoke-host-request-hook";
|
|
18
|
+
export { resolveHostRequestHook, type SomaHostRequestContext, type SomaHostRequestKind, type SomaRequestHook, } from "./host/resolve-host-request-hook";
|
|
19
|
+
export { createSomaCspHeader, createSomaCspNonce, buildSomaCspDirectives, buildSomaCspHeaderValue, resolveSomaCspMode, resolveSomaCspOptionsFromEnv, somaCspHeaderName, SOMA_CSP_MODE_ENV, SOMA_CSP_CONNECT_SRC_ENV, SOMA_CSP_IMG_SRC_ENV, SOMA_CSP_FRAME_SRC_ENV, SOMA_CSP_REPORT_URI_ENV, SOMA_CSP_NONCE_HEADER, type SomaCspMode, type SomaCspOptions, type SomaCspHeader, } from "./host/content-security-policy";
|
|
20
|
+
export { readSomaCspNonce, somaCspNonceMetadata, type SomaCspHeaderBag, } from "./host/csp-nonce-metadata";
|
|
21
|
+
export { loadHostAssembly, registerShellRoutes, resolveHostLoginPath, SOMA_HOST_ASSEMBLY_FILE } from "./host/host-assembly";
|
|
22
|
+
export type { SomaHostAssembly } from "../shared/types/host-assembly.types";
|
|
23
|
+
export { loadPageMarkupConfig, renderColorSchemeMeta } from "./host/page-markup-config";
|
|
24
|
+
export type { SomaPageMarkup } from "../shared/types/component-tree.types";
|
|
25
|
+
export { loadSomaServerPageAssets, resolveSomaPageSource, SomaPageAssetError, type SomaServerPageAssets, } from "./host/server-page-assets";
|
|
26
|
+
export { clearSomaAssetSourcesCache, getSomaAssetSources, getSomaHostApiRoot, getSomaHostSourceRoot, type SomaAssetSource, } from "./host/source-packages";
|
|
27
|
+
export * from "./runtime-space";
|
|
@@ -1,16 +1,30 @@
|
|
|
1
|
+
export interface ServerFileCacheOptions {
|
|
2
|
+
maxFiles?: number;
|
|
3
|
+
maxFileBytes?: number;
|
|
4
|
+
maxDirs?: number;
|
|
5
|
+
}
|
|
1
6
|
/**
|
|
2
|
-
*
|
|
7
|
+
* 服务端文件缓存单例(有界 LRU + 监听去重)
|
|
3
8
|
*
|
|
4
9
|
* 将磁盘文件内容和目录列表缓存到内存中,避免每次请求都执行 I/O。
|
|
5
|
-
*
|
|
10
|
+
* 支持有界文件数、总字节上限与目录缓存上限;支持 fs.watch 监听去重,自动失效对应缓存条目。
|
|
6
11
|
*/
|
|
7
12
|
export declare class ServerFileCache {
|
|
8
13
|
private static instance;
|
|
14
|
+
readonly maxFiles: number;
|
|
15
|
+
readonly maxFileBytes: number;
|
|
16
|
+
readonly maxDirs: number;
|
|
9
17
|
private fileCache;
|
|
10
18
|
private dirCache;
|
|
11
19
|
private watchers;
|
|
12
|
-
private
|
|
13
|
-
|
|
20
|
+
private watchedRoots;
|
|
21
|
+
private currentFileBytes;
|
|
22
|
+
constructor(options?: ServerFileCacheOptions);
|
|
23
|
+
static getInstance(options?: ServerFileCacheOptions): ServerFileCache;
|
|
24
|
+
get size(): number;
|
|
25
|
+
get fileBytes(): number;
|
|
26
|
+
get dirSize(): number;
|
|
27
|
+
get watchersCount(): number;
|
|
14
28
|
/**
|
|
15
29
|
* 读取文件内容,命中缓存直接返回,未命中则读磁盘并缓存。
|
|
16
30
|
*/
|
|
@@ -25,11 +39,20 @@ export declare class ServerFileCache {
|
|
|
25
39
|
*/
|
|
26
40
|
readDirFiles(dirPath: string, ext: string): string[];
|
|
27
41
|
readDirNames(dirPath: string, ext: string): string[];
|
|
42
|
+
private setDirCache;
|
|
28
43
|
/**
|
|
29
44
|
* 监听目录变化,自动失效缓存。
|
|
30
|
-
* 使用 fs.watch
|
|
45
|
+
* 使用 fs.watch 递归监听;已监听同一目录或其祖先目录时自动去重。
|
|
31
46
|
*/
|
|
32
47
|
watchAndInvalidate(rootDir: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* 清除所有缓存。
|
|
50
|
+
*/
|
|
51
|
+
clear(): void;
|
|
52
|
+
/**
|
|
53
|
+
* 使所有缓存失效。
|
|
54
|
+
*/
|
|
55
|
+
invalidate(): void;
|
|
33
56
|
/**
|
|
34
57
|
* 清除所有缓存并关闭 watcher。
|
|
35
58
|
*/
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Node 安全的服务端子路径(`soma-core/server/node`,R-28 / C9)。
|
|
3
3
|
*
|
|
4
|
-
* 为什么单开一个口:`soma-core/server` 带 `import
|
|
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` 看守。
|
|
8
8
|
*
|
|
9
9
|
* 与 `soma-core/server` 的关系:同名导出是同一份实现的再导出,不是第二份;Next 服务端代码继续用 `soma-core/server`。
|
|
10
10
|
*/
|
|
11
|
-
export { collectIncludes, collectPageEntries, getSomaAssetSources, getSomaHostConfigRoot, getSomaHostSourceRoot, listSomaSourcePackageNames, normalizeSomaEnv, orderSourcesForOverride, readSomaEnv, resolveAppConfigPath, resolveHostAppConfigPath, resolveHostConfigRoot, type SomaAssetSource, type SomaEnvName, type SomaSourcePageEntry, } from
|
|
12
|
-
export { loadSomaServerPageAssets, resolveSomaPageSource, SomaPageAssetError, type SomaServerPageAssets, } from
|
|
13
|
-
export { loadPageMarkupConfig, renderColorSchemeMeta } from
|
|
14
|
-
export { resolveServiceFilePath, toPascalCase } from
|
|
15
|
-
export {
|
|
16
|
-
export {
|
|
11
|
+
export { clearSomaAssetSourcesCache, collectIncludes, collectPageEntries, getSomaAssetSources, getSomaHostApiRoot, getSomaHostConfigRoot, getSomaHostSourceRoot, listSomaSourcePackageNames, normalizeSomaEnv, orderSourcesForOverride, readSomaEnv, resolveAppConfigPath, resolveHostAppConfigPath, resolveHostConfigRoot, type SomaAssetSource, type SomaEnvName, type SomaSourcePageEntry, } from "./host/source-packages";
|
|
12
|
+
export { loadSomaServerPageAssets, resolveSomaPageSource, SomaPageAssetError, type SomaServerPageAssets, } from "./host/server-page-assets";
|
|
13
|
+
export { loadPageMarkupConfig, renderColorSchemeMeta } from "./host/page-markup-config";
|
|
14
|
+
export { resolveHostServiceCandidate, resolveServiceFilePath, toPascalCase, validateServiceSegments, type ResolveHostServiceCandidateOptions, } from "./api/service-path";
|
|
15
|
+
export { clearHostServiceCache, getAotServiceResolver, loadHostService, registerAotServiceResolver, resolveServiceExport, type AotServiceResolver, } from "./api/load-host-service";
|
|
16
|
+
export { dispatchHostServiceApiRequest, polyfillServiceRequest, type DispatchHostServiceApiOptions, type RequestCookie, type RequestCookiesAdapter, } from "./api/dispatch-service-api";
|
|
17
|
+
export type { IService, SomaServiceRequest } from "./api/IService";
|
|
18
|
+
export { assertRegistryPublishAddress, bindPublishedRegistryPage, resetRegistryWriteRateLimit, unbindArchivedRegistryPage, } from "./registry/registry-write-pipeline";
|
|
19
|
+
export { expiresToMinutes } from "../shared/page-envelope";
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const { spawn } = require('node:child_process');
|
|
7
|
+
|
|
8
|
+
const args = process.argv.slice(2);
|
|
9
|
+
const command = args[0];
|
|
10
|
+
|
|
11
|
+
if (!['dev', 'build', 'start'].includes(command)) {
|
|
12
|
+
console.error('用法: host-native.cjs <dev|build|start> [选项]');
|
|
13
|
+
console.error('选项:');
|
|
14
|
+
console.error(' -p, --port <n> 端口号 (默认 3000)');
|
|
15
|
+
console.error(' -H, --hostname <host> 监听地址 (默认 0.0.0.0)');
|
|
16
|
+
console.error(' --web-dir <dir> 前端静态根目录');
|
|
17
|
+
console.error(' --api-dir <dir> 后端服务根目录');
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 1. 自适应 V8 堆内存收束 (防止内存无限暴涨,生产态默认 512MB,开发态默认 1024MB)
|
|
22
|
+
if (!process.env.SOMA_NATIVE_HEAP_APPLIED && !process.env.SOMA_DISABLE_HEAP_CAP) {
|
|
23
|
+
const hasHeapArg = process.execArgv.some((arg) => arg.includes('--max-old-space-size'));
|
|
24
|
+
if (!hasHeapArg) {
|
|
25
|
+
const defaultMb = command === 'dev' ? 1024 : 512;
|
|
26
|
+
const maxOldSpace = process.env.SOMA_MAX_OLD_SPACE_MB
|
|
27
|
+
? parseInt(process.env.SOMA_MAX_OLD_SPACE_MB, 10)
|
|
28
|
+
: defaultMb;
|
|
29
|
+
|
|
30
|
+
const child = spawn(
|
|
31
|
+
process.execPath,
|
|
32
|
+
[`--max-old-space-size=${maxOldSpace}`, __filename, ...args],
|
|
33
|
+
{
|
|
34
|
+
stdio: 'inherit',
|
|
35
|
+
env: {
|
|
36
|
+
...process.env,
|
|
37
|
+
SOMA_NATIVE_HEAP_APPLIED: '1',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const forward = (sig) => { try { child.kill(sig); } catch {} };
|
|
43
|
+
process.on('SIGINT', () => forward('SIGINT'));
|
|
44
|
+
process.on('SIGTERM', () => forward('SIGTERM'));
|
|
45
|
+
child.on('exit', (code, signal) => {
|
|
46
|
+
process.exit(code ?? (signal ? 0 : 0));
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 2. 参数解析
|
|
53
|
+
let port;
|
|
54
|
+
let hostname;
|
|
55
|
+
let webDir;
|
|
56
|
+
let apiDir;
|
|
57
|
+
|
|
58
|
+
for (let i = 1; i < args.length; i++) {
|
|
59
|
+
const arg = args[i];
|
|
60
|
+
if (arg === '-p' || arg === '--port') {
|
|
61
|
+
port = parseInt(args[++i], 10);
|
|
62
|
+
} else if (arg.startsWith('--port=')) {
|
|
63
|
+
port = parseInt(arg.slice('--port='.length), 10);
|
|
64
|
+
} else if (arg === '-H' || arg === '--hostname') {
|
|
65
|
+
hostname = args[++i];
|
|
66
|
+
} else if (arg.startsWith('--hostname=')) {
|
|
67
|
+
hostname = arg.slice('--hostname='.length);
|
|
68
|
+
} else if (arg === '--web-dir') {
|
|
69
|
+
webDir = args[++i];
|
|
70
|
+
} else if (arg.startsWith('--web-dir=')) {
|
|
71
|
+
webDir = arg.slice('--web-dir='.length);
|
|
72
|
+
} else if (arg === '--api-dir') {
|
|
73
|
+
apiDir = args[++i];
|
|
74
|
+
} else if (arg.startsWith('--api-dir=')) {
|
|
75
|
+
apiDir = arg.slice('--api-dir='.length);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const hostDir = path.resolve(
|
|
80
|
+
process.env.SOMA_HOST_DIR && process.env.SOMA_HOST_DIR.trim() !== ''
|
|
81
|
+
? process.env.SOMA_HOST_DIR
|
|
82
|
+
: process.cwd(),
|
|
83
|
+
);
|
|
84
|
+
process.env.SOMA_HOST_DIR = hostDir;
|
|
85
|
+
if (webDir) process.env.SOMA_WEB_DIR = path.resolve(hostDir, webDir);
|
|
86
|
+
if (apiDir) process.env.SOMA_API_DIR = path.resolve(hostDir, apiDir);
|
|
87
|
+
|
|
88
|
+
// 3. 准备运行时目录与静态分片同步
|
|
89
|
+
const { resolveSomaRuntimeLayout } = require('../../core/server/runtime-space/root.cjs');
|
|
90
|
+
const layout = resolveSomaRuntimeLayout(hostDir, process.env);
|
|
91
|
+
for (const dir of Object.values(layout.dirs)) {
|
|
92
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 同步浏览器核心运行包到 static/
|
|
96
|
+
const distBrowserDir = path.resolve(__dirname, '../../dist/browser');
|
|
97
|
+
if (fs.existsSync(distBrowserDir)) {
|
|
98
|
+
for (const file of fs.readdirSync(distBrowserDir)) {
|
|
99
|
+
const src = path.join(distBrowserDir, file);
|
|
100
|
+
const dst = path.join(layout.dirs.static, file);
|
|
101
|
+
if (fs.statSync(src).isFile()) {
|
|
102
|
+
fs.copyFileSync(src, dst);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 默认壳路由设置
|
|
108
|
+
if (!process.env.SOMA_SHELL_ROUTES) {
|
|
109
|
+
process.env.SOMA_SHELL_ROUTES = JSON.stringify(['/api', '/soma', '/soma-runtime', '/_soma']);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const { getSomaHostSourceRoot, getSomaHostApiRoot } = require('../../core/server/host/source-packages.cjs');
|
|
113
|
+
|
|
114
|
+
function loadJiti() {
|
|
115
|
+
const createJiti = require('jiti');
|
|
116
|
+
return createJiti(__filename, { interopDefault: true });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function run() {
|
|
120
|
+
if (command === 'start' && !process.env.NODE_ENV) {
|
|
121
|
+
process.env.NODE_ENV = 'production';
|
|
122
|
+
}
|
|
123
|
+
const webRoot = getSomaHostSourceRoot(hostDir, process.env);
|
|
124
|
+
const apiRoot = getSomaHostApiRoot(hostDir, process.env);
|
|
125
|
+
|
|
126
|
+
if (command === 'build') {
|
|
127
|
+
const startTime = Date.now();
|
|
128
|
+
console.log('[soma-native] 开始执行 AOT 全量预编译...');
|
|
129
|
+
const jiti = loadJiti();
|
|
130
|
+
const aotBuilder = jiti(path.resolve(__dirname, '../src/aot-builder.ts'));
|
|
131
|
+
const hostPrepare = jiti(path.resolve(__dirname, '../src/host-prepare.ts'));
|
|
132
|
+
|
|
133
|
+
await hostPrepare.prewarmHostPages(hostDir, process.env);
|
|
134
|
+
|
|
135
|
+
await aotBuilder.buildBrowserClient({
|
|
136
|
+
hostDir,
|
|
137
|
+
webRoot,
|
|
138
|
+
outDir: layout.dirs.static,
|
|
139
|
+
dev: false,
|
|
140
|
+
env: process.env,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
await aotBuilder.buildServerAot({
|
|
144
|
+
hostDir,
|
|
145
|
+
webRoot,
|
|
146
|
+
apiRoot,
|
|
147
|
+
outDir: layout.dirs.root,
|
|
148
|
+
env: process.env,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const elapsed = Date.now() - startTime;
|
|
152
|
+
console.log(`[soma-native] AOT 预编译完成!客户端与服务端产物已就绪 (耗时 ${elapsed}ms)`);
|
|
153
|
+
process.exit(0);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (command === 'start' || command === 'dev') {
|
|
157
|
+
const isDev = command === 'dev';
|
|
158
|
+
const precompiledServerPath = path.resolve(__dirname, '../../dist/host-native/native-server.cjs');
|
|
159
|
+
const nativeServer = (!isDev && fs.existsSync(precompiledServerPath))
|
|
160
|
+
? require(precompiledServerPath)
|
|
161
|
+
: loadJiti()(path.resolve(__dirname, '../src/native-server.ts'));
|
|
162
|
+
|
|
163
|
+
if (isDev) {
|
|
164
|
+
const hostPrepare = loadJiti()(path.resolve(__dirname, '../src/host-prepare.ts'));
|
|
165
|
+
try {
|
|
166
|
+
await hostPrepare.prewarmHostPages(hostDir, process.env);
|
|
167
|
+
} catch (e) {
|
|
168
|
+
console.warn('[soma-native] 初始预热执行异常:', e);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const instance = await nativeServer.createNativeServer({
|
|
173
|
+
hostDir,
|
|
174
|
+
webRoot,
|
|
175
|
+
apiRoot,
|
|
176
|
+
port,
|
|
177
|
+
hostname,
|
|
178
|
+
dev: isDev,
|
|
179
|
+
env: process.env,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const { url, port: actualPort } = await instance.start();
|
|
183
|
+
const modeText = isDev ? '开发态 (增量热更新)' : '生产态 (极速 AOT)';
|
|
184
|
+
|
|
185
|
+
console.log(`\n SOMA 极速原生内核已就绪 [${modeText}]`);
|
|
186
|
+
console.log(` > 本地地址: ${url}`);
|
|
187
|
+
console.log(` > 前端目录: ${webRoot}`);
|
|
188
|
+
console.log(` > 后端目录: ${apiRoot}\n`);
|
|
189
|
+
|
|
190
|
+
const cleanup = async () => {
|
|
191
|
+
console.log('\n[soma-native] 正在安全停止内核...');
|
|
192
|
+
await instance.close();
|
|
193
|
+
process.exit(0);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
process.on('SIGINT', cleanup);
|
|
197
|
+
process.on('SIGTERM', cleanup);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
run().catch((err) => {
|
|
202
|
+
console.error('[soma-native] 进程启动失败:', err);
|
|
203
|
+
process.exit(1);
|
|
204
|
+
});
|