soma-core 1.0.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.
- package/app/api/page/route.ts +97 -0
- package/app/globals.css +3 -0
- package/app/layout.tsx +18 -0
- package/app/page.tsx +129 -0
- package/bin/soma.cjs +2 -0
- package/core/base/SomaApi.ts +14 -0
- package/core/base/SomaDict.ts +168 -0
- package/core/base/SomaEntity.ts +41 -0
- package/core/base/SomaHttpClient.ts +126 -0
- package/core/base/SomaLocaleOnlyPageRoot.ts +4 -0
- package/core/base/SomaPageRootBase.ts +54 -0
- package/core/base/SomaValidator.ts +147 -0
- package/core/base/index.ts +7 -0
- package/core/compiler/full-manifest-builder.ts +43 -0
- package/core/compiler/manifest-compiler.ts +237 -0
- package/core/compiler/route-builder.ts +31 -0
- package/core/compiler/soma-precompile.ts +48 -0
- package/core/compiler/soma-webpack-loader.js +35 -0
- package/core/compiler/vite-plugin-soma.ts +73 -0
- package/core/container/SomaContainerAdapter.ts +169 -0
- package/core/diagnostics/SomaDiagnostics.ts +186 -0
- package/core/engine/asset-resolver.ts +67 -0
- package/core/engine/dom-renderer.ts +366 -0
- package/core/engine/event-engine.ts +287 -0
- package/core/engine/lis.ts +60 -0
- package/core/engine/reactive-proxy.ts +225 -0
- package/core/engine/render-engine.ts +264 -0
- package/core/engine/runtime-builder.ts +100 -0
- package/core/engine/scope-resolver.ts +252 -0
- package/core/engine/soma-bootstrap.ts +244 -0
- package/core/index.ts +6 -0
- package/core/infra/auth/IAuthService.ts +13 -0
- package/core/infra/auth/TokenAuthService.ts +98 -0
- package/core/infra/cache/ICache.ts +13 -0
- package/core/infra/cache/MemoryCache.ts +95 -0
- package/core/infra/cache/ServerFileCache.ts +123 -0
- package/core/infra/config/IConfig.ts +9 -0
- package/core/infra/config/SomaConfig.ts +85 -0
- package/core/infra/event-bus/IEventBus.ts +9 -0
- package/core/infra/event-bus/SomaEventBus.ts +53 -0
- package/core/infra/http/IHttpClient.ts +21 -0
- package/core/infra/http/SomaHttpService.ts +141 -0
- package/core/infra/i18n/DefaultI18n.ts +41 -0
- package/core/infra/i18n/II18n.ts +7 -0
- package/core/infra/logger/ConsoleLogger.ts +81 -0
- package/core/infra/logger/ILogger.ts +19 -0
- package/core/infra/notify/DefaultNotifyService.ts +31 -0
- package/core/infra/notify/INotifyService.ts +14 -0
- package/core/infra/permission/IPermission.ts +10 -0
- package/core/infra/permission/SomaPermission.ts +48 -0
- package/core/infra/router/IRouter.ts +19 -0
- package/core/infra/router/SomaRouter.ts +66 -0
- package/core/infra/storage/IStorageService.ts +7 -0
- package/core/infra/storage/MemoryStorageService.ts +53 -0
- package/core/infra/timer/ITimer.ts +9 -0
- package/core/infra/timer/SomaTimer.ts +118 -0
- package/core/middleware/SomaMiddlewareChain.ts +29 -0
- package/core/parser/pipeline-parser.ts +276 -0
- package/core/parser/ui-config-parser.ts +336 -0
- package/core/parser/yaml-loader.ts +36 -0
- package/core/router/RouteHandler.ts +93 -0
- package/core/runtime/SomaSnapshotStore.ts +98 -0
- package/core/scheduler/SomaScheduler.ts +190 -0
- package/core/types/domain.types.ts +88 -0
- package/core/types/engine.types.ts +35 -0
- package/core/types/event.types.ts +57 -0
- package/core/types/keyboard.types.ts +18 -0
- package/core/types/manifest.types.ts +30 -0
- package/core/types/scheduler.types.ts +10 -0
- package/core/types/ui-config.types.ts +89 -0
- package/core/validation/contract-validator.ts +465 -0
- package/dist/cli/soma.js +49 -0
- package/global.d.ts +14 -0
- package/next.config.js +61 -0
- package/package.json +77 -0
- package/postcss.config.js +6 -0
- package/tailwind.config.ts +21 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
// core/parser/ui-config-parser.ts — SOMA UI 配置解析器
|
|
2
|
+
// 负责将 YAML 解析后的原始对象转换为类型安全的 UiConfig 结构
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
UiConfig,
|
|
6
|
+
ShowMapping,
|
|
7
|
+
ShowValue,
|
|
8
|
+
NestedShowMapping,
|
|
9
|
+
BindDeclaration,
|
|
10
|
+
ExtendedShowValue,
|
|
11
|
+
ParsedShowEntry,
|
|
12
|
+
} from '../types/ui-config.types';
|
|
13
|
+
|
|
14
|
+
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* UI 配置解析错误
|
|
18
|
+
* 携带错误路径信息以便定位问题
|
|
19
|
+
*/
|
|
20
|
+
export class SomaUiConfigError extends Error {
|
|
21
|
+
public readonly path: string;
|
|
22
|
+
|
|
23
|
+
constructor(message: string, path: string = '') {
|
|
24
|
+
const prefix = path ? ` (路径: ${path})` : '';
|
|
25
|
+
super(`[SOMA UiConfigParser] ${message}${prefix}`);
|
|
26
|
+
this.name = 'SomaUiConfigError';
|
|
27
|
+
this.path = path;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ─── 内部辅助函数 ─────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 校验 bind 声明是否合法
|
|
35
|
+
*
|
|
36
|
+
* 规则:
|
|
37
|
+
* - bind 必须是对象
|
|
38
|
+
* - bind.obj 必须是非空字符串
|
|
39
|
+
* - bind.var 必须是非空字符串
|
|
40
|
+
*
|
|
41
|
+
* @param bind - 待校验的 bind 对象
|
|
42
|
+
* @param path - 当前路径(用于错误信息)
|
|
43
|
+
* @throws SomaUiConfigError 校验失败时抛出
|
|
44
|
+
*/
|
|
45
|
+
function validateBind(bind: any, path: string): asserts bind is BindDeclaration {
|
|
46
|
+
if (!bind || typeof bind !== 'object') {
|
|
47
|
+
throw new SomaUiConfigError(`bind 必须是对象`, path);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!bind.obj || typeof bind.obj !== 'string') {
|
|
51
|
+
throw new SomaUiConfigError(
|
|
52
|
+
`bind.obj 必须是非空字符串,当前值: ${JSON.stringify(bind.obj)}`,
|
|
53
|
+
path,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!bind.var || typeof bind.var !== 'string') {
|
|
58
|
+
throw new SomaUiConfigError(
|
|
59
|
+
`bind.var 必须是非空字符串,当前值: ${JSON.stringify(bind.var)}`,
|
|
60
|
+
path,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 判断一个对象值是否为嵌套绑定(含 bind + show)
|
|
67
|
+
*/
|
|
68
|
+
function isNestedShowMapping(value: any): value is { bind: any; show: any } {
|
|
69
|
+
return (
|
|
70
|
+
typeof value === 'object' &&
|
|
71
|
+
value !== null &&
|
|
72
|
+
'bind' in value &&
|
|
73
|
+
'show' in value
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 判断一个对象值是否为扩展绑定(含 text / model / when / class / show 等)
|
|
79
|
+
*/
|
|
80
|
+
function isExtendedShowValue(value: any): boolean {
|
|
81
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
82
|
+
const extendedKeys = ['text', 'model', 'when', 'class', 'show'];
|
|
83
|
+
return extendedKeys.some((key) => key in value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 拆分 show 键中的 `somaId@attr` 语法
|
|
88
|
+
*
|
|
89
|
+
* 示例:
|
|
90
|
+
* - "title" → { somaId: "title" }
|
|
91
|
+
* - "avatar@src" → { somaId: "avatar", attrName: "src" }
|
|
92
|
+
* - "body@html" → { somaId: "body", attrName: "html" }
|
|
93
|
+
*
|
|
94
|
+
* @param key - show 映射的键
|
|
95
|
+
* @param path - 当前路径(用于错误信息)
|
|
96
|
+
* @returns 拆分后的 somaId 和可选的 attrName
|
|
97
|
+
* @throws SomaUiConfigError 键格式不合法时抛出
|
|
98
|
+
*/
|
|
99
|
+
function parseSomaKey(
|
|
100
|
+
key: string,
|
|
101
|
+
path: string,
|
|
102
|
+
): { somaId: string; attrName?: string } {
|
|
103
|
+
if (!key) {
|
|
104
|
+
throw new SomaUiConfigError(`show 键不能为空`, path);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const atIndex = key.indexOf('@');
|
|
108
|
+
|
|
109
|
+
if (atIndex === -1) {
|
|
110
|
+
// 普通 somaId,无属性绑定
|
|
111
|
+
return { somaId: key };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const somaId = key.substring(0, atIndex);
|
|
115
|
+
const attrName = key.substring(atIndex + 1);
|
|
116
|
+
|
|
117
|
+
if (!somaId) {
|
|
118
|
+
throw new SomaUiConfigError(
|
|
119
|
+
`show 键 "${key}" 中 @ 前的 somaId 部分为空`,
|
|
120
|
+
path,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!attrName) {
|
|
125
|
+
throw new SomaUiConfigError(
|
|
126
|
+
`show 键 "${key}" 中 @ 后的属性名部分为空`,
|
|
127
|
+
path,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return { somaId, attrName };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ─── show 映射解析 ────────────────────────────────────────────────────────────
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 递归解析 show 映射
|
|
138
|
+
*
|
|
139
|
+
* 处理以下值类型:
|
|
140
|
+
* - string:简单文本绑定("var.prop")
|
|
141
|
+
* - object with bind+show:嵌套绑定(对象/列表子作用域)
|
|
142
|
+
* - object with text/model/when/class:扩展绑定
|
|
143
|
+
*
|
|
144
|
+
* @param rawShow - YAML 解析后的原始 show 对象
|
|
145
|
+
* @param path - 当前路径(用于错误信息)
|
|
146
|
+
* @returns 类型安全的 ShowMapping
|
|
147
|
+
* @throws SomaUiConfigError 格式不合法时抛出
|
|
148
|
+
*/
|
|
149
|
+
function parseShowMapping(rawShow: any, path: string): ShowMapping {
|
|
150
|
+
if (!rawShow || typeof rawShow !== 'object') {
|
|
151
|
+
throw new SomaUiConfigError(`show 必须是对象`, path);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const result: ShowMapping = {};
|
|
155
|
+
|
|
156
|
+
for (const [key, value] of Object.entries(rawShow)) {
|
|
157
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
158
|
+
|
|
159
|
+
if (!key) {
|
|
160
|
+
throw new SomaUiConfigError(`show 键不能为空`, currentPath);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (typeof value === 'string') {
|
|
164
|
+
// 简单文本绑定:"var.prop" 或 "somaId@attr: var.prop"
|
|
165
|
+
result[key] = value;
|
|
166
|
+
} else if (isNestedShowMapping(value)) {
|
|
167
|
+
// 嵌套绑定:含 bind + show
|
|
168
|
+
const nestedPath = `${currentPath}.bind`;
|
|
169
|
+
validateBind(value.bind, nestedPath);
|
|
170
|
+
|
|
171
|
+
const nested: NestedShowMapping = {
|
|
172
|
+
bind: {
|
|
173
|
+
obj: value.bind.obj,
|
|
174
|
+
var: value.bind.var,
|
|
175
|
+
},
|
|
176
|
+
show: parseShowMapping(value.show, `${currentPath}.show`),
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
result[key] = nested;
|
|
180
|
+
} else if (typeof value === 'object' && value !== null && isExtendedShowValue(value)) {
|
|
181
|
+
// 扩展绑定:含 text / model / when / class / show
|
|
182
|
+
const extended: ExtendedShowValue = {};
|
|
183
|
+
|
|
184
|
+
if ('text' in value && value.text !== undefined) {
|
|
185
|
+
if (typeof value.text !== 'string') {
|
|
186
|
+
throw new SomaUiConfigError(
|
|
187
|
+
`text 必须是字符串,当前值: ${JSON.stringify(value.text)}`,
|
|
188
|
+
currentPath,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
extended.text = value.text;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if ('model' in value && value.model !== undefined) {
|
|
195
|
+
if (typeof value.model !== 'string') {
|
|
196
|
+
throw new SomaUiConfigError(
|
|
197
|
+
`model 必须是字符串,当前值: ${JSON.stringify(value.model)}`,
|
|
198
|
+
currentPath,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
extended.model = value.model;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if ('when' in value && value.when !== undefined) {
|
|
205
|
+
if (typeof value.when !== 'string') {
|
|
206
|
+
throw new SomaUiConfigError(
|
|
207
|
+
`when 必须是字符串,当前值: ${JSON.stringify(value.when)}`,
|
|
208
|
+
currentPath,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
extended.when = value.when;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if ('class' in value && value.class !== undefined) {
|
|
215
|
+
if (typeof value.class !== 'object' || value.class === null) {
|
|
216
|
+
throw new SomaUiConfigError(
|
|
217
|
+
`class 必须是对象({ "class-name": "condition-path" }),当前值: ${JSON.stringify(value.class)}`,
|
|
218
|
+
currentPath,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
// 校验 class 的每个条目
|
|
222
|
+
const classMap: Record<string, string> = {};
|
|
223
|
+
for (const [className, condition] of Object.entries(value.class)) {
|
|
224
|
+
if (typeof condition !== 'string') {
|
|
225
|
+
throw new SomaUiConfigError(
|
|
226
|
+
`class 的条件路径必须是字符串,键 "${className}" 的值: ${JSON.stringify(condition)}`,
|
|
227
|
+
`${currentPath}.class`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
classMap[className] = condition;
|
|
231
|
+
}
|
|
232
|
+
extended.class = classMap;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if ('show' in value && value.show !== undefined) {
|
|
236
|
+
extended.show = parseShowMapping(value.show, `${currentPath}.show`);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
result[key] = extended;
|
|
240
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
241
|
+
// 对象值但不符合已知格式 — 可能是纯嵌套 show(不带 bind 的简写形式)
|
|
242
|
+
// 当 value 有 bind 无 show,或有 show 无 bind,报错
|
|
243
|
+
if ('bind' in value && !('show' in value)) {
|
|
244
|
+
throw new SomaUiConfigError(
|
|
245
|
+
`嵌套绑定缺少 show 字段,bind 和 show 必须成对出现`,
|
|
246
|
+
currentPath,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if ('show' in value && !('bind' in value) && !isExtendedShowValue(value)) {
|
|
250
|
+
throw new SomaUiConfigError(
|
|
251
|
+
`嵌套绑定缺少 bind 字段,bind 和 show 必须成对出现`,
|
|
252
|
+
currentPath,
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 不认识的对象结构
|
|
257
|
+
throw new SomaUiConfigError(
|
|
258
|
+
`show 值的格式无法识别: ${JSON.stringify(value)}`,
|
|
259
|
+
currentPath,
|
|
260
|
+
);
|
|
261
|
+
} else {
|
|
262
|
+
throw new SomaUiConfigError(
|
|
263
|
+
`show 值必须是字符串或对象,当前类型: ${typeof value}`,
|
|
264
|
+
currentPath,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ─── 公共 API ─────────────────────────────────────────────────────────────────
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 将 YAML 解析后的原始对象转换为类型安全的 UiConfig
|
|
276
|
+
*
|
|
277
|
+
* 校验规则:
|
|
278
|
+
* - 顶层必须包含 bind 和 show
|
|
279
|
+
* - bind 必须有 obj 和 var(均为非空字符串)
|
|
280
|
+
* - show 键不能为空
|
|
281
|
+
* - 嵌套绑定的 bind 和 show 必须成对出现
|
|
282
|
+
* - 处理 somaId@attr 扩展语法
|
|
283
|
+
*
|
|
284
|
+
* @param raw - YAML 解析后的原始对象(通常来自 yaml-loader)
|
|
285
|
+
* @returns 类型安全的 UiConfig 对象
|
|
286
|
+
* @throws SomaUiConfigError 格式不合法时抛出
|
|
287
|
+
*/
|
|
288
|
+
export function parseUiConfig(raw: any): UiConfig {
|
|
289
|
+
if (!raw || typeof raw !== 'object') {
|
|
290
|
+
throw new SomaUiConfigError(`UI 配置必须是对象`);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// 校验顶层 bind
|
|
294
|
+
if (!('bind' in raw)) {
|
|
295
|
+
throw new SomaUiConfigError(`UI 配置缺少必填字段 bind`);
|
|
296
|
+
}
|
|
297
|
+
validateBind(raw.bind, 'bind');
|
|
298
|
+
|
|
299
|
+
// 校验顶层 show
|
|
300
|
+
if (!('show' in raw)) {
|
|
301
|
+
throw new SomaUiConfigError(`UI 配置缺少必填字段 show`);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const bind: BindDeclaration = {
|
|
305
|
+
obj: raw.bind.obj,
|
|
306
|
+
var: raw.bind.var,
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const show = parseShowMapping(raw.show, 'show');
|
|
310
|
+
|
|
311
|
+
return { bind, show };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 解析 show 键中的 somaId@attr 语法
|
|
316
|
+
*
|
|
317
|
+
* 遍历 ShowMapping 中的所有键,将 `somaId@attr` 拆分为 somaId + attrName。
|
|
318
|
+
* 用于引擎在绑定时判断是设置 textContent 还是 setAttribute。
|
|
319
|
+
*
|
|
320
|
+
* @param show - 已解析的 ShowMapping
|
|
321
|
+
* @returns ParsedShowEntry 数组
|
|
322
|
+
*/
|
|
323
|
+
export function parseShowKeys(show: ShowMapping): ParsedShowEntry[] {
|
|
324
|
+
const entries: ParsedShowEntry[] = [];
|
|
325
|
+
|
|
326
|
+
for (const [key, value] of Object.entries(show)) {
|
|
327
|
+
const { somaId, attrName } = parseSomaKey(key, key);
|
|
328
|
+
const entry: ParsedShowEntry = { somaId, value };
|
|
329
|
+
if (attrName) {
|
|
330
|
+
entry.attrName = attrName;
|
|
331
|
+
}
|
|
332
|
+
entries.push(entry);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return entries;
|
|
336
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// core/parser/yaml-loader.ts — YAML 文件加载和解析封装
|
|
2
|
+
// 封装 js-yaml 库,提供异步文件加载和同步字符串解析
|
|
3
|
+
|
|
4
|
+
import yaml from 'js-yaml';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 异步加载并解析 YAML 文件
|
|
8
|
+
*
|
|
9
|
+
* 使用 fetch API 获取文件内容,然后通过 js-yaml 解析为 JavaScript 对象。
|
|
10
|
+
* 适用于浏览器环境中加载 YAML 配置文件。
|
|
11
|
+
*
|
|
12
|
+
* @param path - YAML 文件的 URL 路径
|
|
13
|
+
* @returns 解析后的 JavaScript 对象
|
|
14
|
+
* @throws Error 文件加载失败或 YAML 解析错误时抛出
|
|
15
|
+
*/
|
|
16
|
+
export async function loadYamlFile(path: string): Promise<any> {
|
|
17
|
+
const res = await fetch(path);
|
|
18
|
+
if (!res.ok) {
|
|
19
|
+
throw new Error(`Failed to load YAML: ${path} (${res.status})`);
|
|
20
|
+
}
|
|
21
|
+
const text = await res.text();
|
|
22
|
+
return yaml.load(text);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 同步解析 YAML 字符串
|
|
27
|
+
*
|
|
28
|
+
* 将 YAML 格式的文本直接解析为 JavaScript 对象。
|
|
29
|
+
*
|
|
30
|
+
* @param text - YAML 格式的字符串
|
|
31
|
+
* @returns 解析后的 JavaScript 对象
|
|
32
|
+
* @throws YAMLException YAML 语法错误时抛出
|
|
33
|
+
*/
|
|
34
|
+
export function parseYamlString(text: string): any {
|
|
35
|
+
return yaml.load(text);
|
|
36
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// soma-core/core/router/RouteHandler.ts
|
|
2
|
+
// 路由处理器抽象基类 + 实现类
|
|
3
|
+
|
|
4
|
+
import { ServerFileCache } from '../infra/cache/ServerFileCache';
|
|
5
|
+
|
|
6
|
+
export interface RouteContext {
|
|
7
|
+
path: string;
|
|
8
|
+
req: Request;
|
|
9
|
+
hostRoot: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// ─── 抽象基类 ─────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
export abstract class RouteHandler {
|
|
15
|
+
abstract match(path: string): boolean;
|
|
16
|
+
abstract handle(ctx: RouteContext): Promise<Response>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ─── Page 处理器 ──────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
export class PageHandler extends RouteHandler {
|
|
22
|
+
match(path: string) {
|
|
23
|
+
return path.endsWith('.html');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async handle({ path, hostRoot }: RouteContext): Promise<Response> {
|
|
27
|
+
const { resolve } = await import('path');
|
|
28
|
+
const cache = ServerFileCache.getInstance();
|
|
29
|
+
|
|
30
|
+
const filePath = resolve(hostRoot, 'html', path.replace(/^\//, ''));
|
|
31
|
+
try {
|
|
32
|
+
const content = cache.readFile(filePath);
|
|
33
|
+
return new Response(content, {
|
|
34
|
+
headers: { 'Content-Type': 'text/html; charset=utf-8' },
|
|
35
|
+
});
|
|
36
|
+
} catch {
|
|
37
|
+
return new Response('Page not found', { status: 404 });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ─── API 处理器 ───────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
export class ApiHandler extends RouteHandler {
|
|
45
|
+
private static moduleCache = new Map<string, any>();
|
|
46
|
+
|
|
47
|
+
match(_path: string) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async handle({ path, req, hostRoot }: RouteContext): Promise<Response> {
|
|
52
|
+
const { resolve } = await import('path');
|
|
53
|
+
|
|
54
|
+
const segments = path.replace(/^\//, '').split('/');
|
|
55
|
+
const fileName = this.toPascalCase(segments[segments.length - 1]);
|
|
56
|
+
const dirSegments = segments.slice(0, -1);
|
|
57
|
+
const filePath = resolve(hostRoot, 'api', ...dirSegments, `${fileName}.ts`);
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
let mod = ApiHandler.moduleCache.get(filePath);
|
|
61
|
+
if (!mod) {
|
|
62
|
+
mod = await import(filePath);
|
|
63
|
+
ApiHandler.moduleCache.set(filePath, mod);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const HandlerClass = mod.default;
|
|
67
|
+
if (!HandlerClass) {
|
|
68
|
+
return Response.json({ error: 'No default export' }, { status: 500 });
|
|
69
|
+
}
|
|
70
|
+
const instance = new HandlerClass();
|
|
71
|
+
return await instance.handle(req);
|
|
72
|
+
} catch (e: any) {
|
|
73
|
+
if (e.code === 'MODULE_NOT_FOUND') {
|
|
74
|
+
return Response.json({ error: `API not found: ${path}` }, { status: 404 });
|
|
75
|
+
}
|
|
76
|
+
return Response.json({ error: e.message }, { status: 500 });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private toPascalCase(str: string): string {
|
|
81
|
+
return str
|
|
82
|
+
.split('-')
|
|
83
|
+
.map(s => s.charAt(0).toUpperCase() + s.slice(1))
|
|
84
|
+
.join('');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ─── 处理器链(顺序即优先级)────────────────────────────
|
|
89
|
+
|
|
90
|
+
export const ROUTE_HANDLERS: RouteHandler[] = [
|
|
91
|
+
new PageHandler(),
|
|
92
|
+
new ApiHandler(),
|
|
93
|
+
];
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
interface SomaSnapshot {
|
|
2
|
+
id: number;
|
|
3
|
+
state: Record<string, unknown>;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export class SomaSnapshotStore {
|
|
7
|
+
private readonly maxEntries: number;
|
|
8
|
+
private nextId = 1;
|
|
9
|
+
private snapshots: SomaSnapshot[] = [];
|
|
10
|
+
|
|
11
|
+
constructor(maxEntries: number = 20) {
|
|
12
|
+
this.maxEntries = maxEntries;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
capture(root: object): number {
|
|
16
|
+
const snapshot: SomaSnapshot = {
|
|
17
|
+
id: this.nextId++,
|
|
18
|
+
state: this.cloneState(root),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
this.snapshots.push(snapshot);
|
|
22
|
+
if (this.snapshots.length > this.maxEntries) {
|
|
23
|
+
this.snapshots.shift();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return snapshot.id;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
restore(id: number, root: Record<string, any>): void {
|
|
30
|
+
const snapshot = this.snapshots.find((item) => item.id === id);
|
|
31
|
+
if (!snapshot) {
|
|
32
|
+
throw new Error(`[SomaSnapshotStore] Snapshot "${id}" not found`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.applyState(root, snapshot.state);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private cloneState(source: unknown): Record<string, unknown> {
|
|
39
|
+
if (typeof source !== 'object' || source === null) {
|
|
40
|
+
return {};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const result: Record<string, unknown> = {};
|
|
44
|
+
for (const key of Object.keys(source)) {
|
|
45
|
+
const value = (source as Record<string, unknown>)[key];
|
|
46
|
+
if (typeof value === 'function') {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
result[key] = this.cloneValue(value);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private cloneValue(value: unknown): unknown {
|
|
55
|
+
if (Array.isArray(value)) {
|
|
56
|
+
return value.map((item) => this.cloneValue(item));
|
|
57
|
+
}
|
|
58
|
+
if (typeof value === 'object' && value !== null) {
|
|
59
|
+
const result: Record<string, unknown> = {};
|
|
60
|
+
for (const key of Object.keys(value)) {
|
|
61
|
+
const child = (value as Record<string, unknown>)[key];
|
|
62
|
+
if (typeof child === 'function') {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
result[key] = this.cloneValue(child);
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private applyState(target: Record<string, any>, source: Record<string, unknown>): void {
|
|
73
|
+
for (const key of Object.keys(target)) {
|
|
74
|
+
if (!(key in source) && typeof target[key] !== 'function') {
|
|
75
|
+
target[key] = undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (const [key, value] of Object.entries(source)) {
|
|
80
|
+
if (Array.isArray(value)) {
|
|
81
|
+
target[key] = value.map((item) => this.cloneValue(item));
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (typeof value === 'object' && value !== null) {
|
|
86
|
+
const current = target[key];
|
|
87
|
+
if (typeof current === 'object' && current !== null && !Array.isArray(current)) {
|
|
88
|
+
this.applyState(current, value as Record<string, unknown>);
|
|
89
|
+
} else {
|
|
90
|
+
target[key] = this.cloneValue(value);
|
|
91
|
+
}
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
target[key] = value;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|