soma-core 2.1.2 → 2.1.3
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.
|
@@ -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 {
|
|
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
|
|
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 =
|
|
39
|
-
const createJiti = getJitiFactory();
|
|
40
|
-
|
|
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':
|
|
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
|
|
12
|
+
import nodeModule from 'node:module';
|
|
13
13
|
import path from 'node:path';
|
|
14
14
|
import { fileURLToPath } from 'node:url';
|
|
15
|
-
|
|
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
|
|
34
|
+
export function getJitiFactory(anchorDir?: string): JitiFactory {
|
|
19
35
|
if (cachedJiti === null) {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
214
|
+
function resolveSelfCoreDir(): string {
|
|
187
215
|
try {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
} catch {
|
|
193
|
-
|
|
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 =
|
|
212
|
-
const createJiti =
|
|
213
|
-
const
|
|
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':
|
|
287
|
+
'@core': coreDir,
|
|
219
288
|
...(serverOnlyEmpty !== undefined ? { 'server-only': serverOnlyEmpty } : {}),
|
|
220
289
|
},
|
|
221
290
|
});
|
|
@@ -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
|
-
*
|
|
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
|
|
46
|
+
export declare function ensureNodeServerOnlyInterop(anchorDir?: string): string | undefined;
|
|
46
47
|
/**
|
|
47
48
|
* 读 `<应用根>/soma.app.ts` 默认导出并等服务端面装配完成(`await app.server()`);无该文件 → null。
|
|
48
49
|
* 装配失败(SomaAppAssemblyError)原样上抛——拒绝启动,不静默回落。同一应用根只装配一次(并发调用共享同一 Promise)。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soma-core",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
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",
|