@hile/micro 2.0.0 → 2.0.2

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 (2) hide show
  1. package/package.json +4 -4
  2. package/SKILL.md +0 -600
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/micro",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -23,11 +23,11 @@
23
23
  "vitest": "^4.0.18"
24
24
  },
25
25
  "dependencies": {
26
- "@hile/message-loader": "^1.0.9",
27
- "@hile/message-ws": "^2.0.0",
26
+ "@hile/message-loader": "^2.0.1",
27
+ "@hile/message-ws": "^2.0.2",
28
28
  "internal-ip": "^9.0.0",
29
29
  "ws": "^8.19.0",
30
30
  "yaml": "^2.9.0"
31
31
  },
32
- "gitHead": "d28b20bc22ee08c45cbf4596672705cb96e8e461"
32
+ "gitHead": "2c8011db01f2815e5ce34de964d5492640396828"
33
33
  }
package/SKILL.md DELETED
@@ -1,600 +0,0 @@
1
- ---
2
- name: micro
3
- description: Code generation and contribution rules for @hile/micro. Use when editing this package or when the user asks about @hile/micro API, types, patterns, or features.
4
- ---
5
-
6
- # @hile/micro — AI Skill Reference
7
-
8
- 本文档面向 **AI 编码模型**。在生成或修改 `@hile/micro` 代码前必读,保证与现有架构、API 约定、状态机、测试模式一致。
9
-
10
- ---
11
-
12
- ## 1. 架构总览
13
-
14
- ### 依赖链
15
-
16
- ```
17
- @hile/message-loader (路由: register/dispatch)
18
- └── @hile/message-ws (WebSocket 请求/响应, MessageModem timeout/abort)
19
- └── packages/micro
20
- ├── Server — WebSocket 监听 + 连接管理 + Client 生命周期
21
- ├── Client — 远端代理, request/push/dispose
22
- ├── Registry — 注册中心 (extends Server)
23
- └── Application — 应用服务 (extends Server)
24
- ```
25
-
26
- ### 分层职责
27
-
28
- | 类 | 文件 | 职责 | 关键约束 |
29
- |----|------|------|---------|
30
- | `Server` | `server.ts` | WebSocketServer 生命周期, 出入站连接, Client Map | 不感知 Registry |
31
- | `Client` | `client.ts` | 远端 Server 的 WebSocket 会话代理 | `dispose()` 必须关闭底层 socket |
32
- | `Registry` | `registry.ts` | namespace → Set\<host:port\>, 心跳检测, /-/find 随机返回, 环境变量管理 | 自动创建 `~/.registry/` 工作目录 |
33
- | `Application` | `application.ts` | 注册发现 + 熔断 + 重试 + 心跳 + 远程环境变量读取 | `listen()` 后自动连 Registry |
34
-
35
- ### 应用模型
36
-
37
- 一个 `Application` 实例同时是 provider 和 consumer。不要创建两个类来区分角色。
38
-
39
- ```
40
- Application:
41
- └─ register(url, handler) → provider 侧
42
- └─ get(namespace) → consumer 侧, 返回 Client
43
- └─ call(namespace, url, data) → consumer 侧, get+request+熔断+重试 一站式
44
- ```
45
-
46
- ---
47
-
48
- ## 2. 类型定义(代码生成时必须一致)
49
-
50
- ### Server (`server.ts`)
51
-
52
- ```typescript
53
- export type MicroServerProps = MessageLoaderProps & {
54
- advertiseHost?: string; // 缺省 getLocalIPv4(), 皆无则构造抛错
55
- };
56
-
57
- export class Server extends MessageLoader {
58
- protected readonly clients = new Map<string, Client>(); // key: "host:port"
59
- public readonly events = new EventEmitter(); // connect/disconnect
60
-
61
- constructor(namespace: string, props?: MicroServerProps);
62
- public listen(port: number): Promise<() => Promise<void>>; // 返回 teardown
63
- public setPort(port: number): this;
64
- protected connect(host: string, port: number, timeout?: number): Promise<Client>;
65
- // 继承自 MessageLoader:
66
- // register<T, E>(url, handler): () => void;
67
- // dispatch(url, data, extras): Promise<any>;
68
- }
69
- ```
70
-
71
- ### Client (`client.ts`)
72
-
73
- ```typescript
74
- export class Client extends MessageWs {
75
- public readonly host: string;
76
- public readonly port: number;
77
- public readonly events = new EventEmitter(); // connect/disconnect
78
-
79
- request(url: string, data: any, timeout?: number): {
80
- abort(): void;
81
- response<T = any>(): Promise<T>; // 超时或 abort 时 reject
82
- };
83
- push(url: string, data: any, timeout?: number): void;
84
- dispose(): void; // 关闭底层 WebSocket
85
- }
86
- ```
87
-
88
- ### Registry (`registry.ts`)
89
-
90
- ```typescript
91
- export interface RegistryFindData {
92
- namespace: string;
93
- exclude?: string[];
94
- }
95
-
96
- export function parseAddressKey(key: string): RegistryAddress | undefined;
97
- export function selectRandomRegistryAddress(keys: Iterable<string>): RegistryAddress | undefined;
98
- // 配置文件路径工具:
99
- export function getRegistryConfigsDir(): string; // 返回 ~/.registry/configs/
100
- export function namespaceToConfigFile(ns: string): string; // 返回 ~/.registry/configs/{ns}.config.yaml
101
- export function parseConfigFilename(filename: string): string | null; // 解析 ".config.yaml" 后缀,提取 namespace
102
-
103
- export class Registry extends Server {
104
- // heartbeat 常量:
105
- // HEARTBEAT_INTERVAL = 1000 (1s 轮询)
106
- // HEARTBEAT_TIMEOUT = 20000 (20s 未收到心跳则剔除)
107
- // 工作目录: ~/.registry/ (自动创建)
108
- // - configs/ 目录存放 *.config.yaml 配置文件
109
- // - watchEnvFile() 监听 configs/ 目录,兼容 vim 原子写入
110
- // - configs Map<string, any> 按 namespace 存储解析后的 YAML 内容
111
- // 内部路由:
112
- // /-/find — 按 namespace 随机返回地址 (支持 exclude)
113
- // /-/heartbeat — 更新实例心跳时间戳
114
- // /-/env/variables — 按 namespace + fields 返回配置 (通过 getEnvVariables 调用)
115
-
116
- constructor(props?: MicroServerProps);
117
- listen(port: number): Promise<() => Promise<void>>;
118
- onFind(): void; // 幂等,可重复调用
119
- watchEnvFile(): fs.FSWatcher | undefined; // 监听 ~/.registry/configs/ 目录
120
- }
121
- ```
122
-
123
- ### Application (`application.ts`)
124
-
125
- ```typescript
126
- export type ApplicationProps = {
127
- namespace: string;
128
- registry: RegistryAddress;
129
- registryLookupTimeoutMs?: number; // /-/find 超时, 默认 10_000
130
- requestTimeoutMs?: number; // 单次请求超时, 默认 30_000
131
- } & MicroServerProps;
132
-
133
- export class Application extends Server {
134
- // 内部常量:
135
- // HEARTBEAT_INTERVAL = 10000 (10s 向 Registry 推送心跳)
136
- // CB_COOLDOWN_MS = 30000 (熔断冷卻期 30s)
137
- // 内部状态:
138
- // namespaces: Map<ns, { host, port, status: IDLE|PENDING|READY, handlers }>
139
- // circuitBreakers: Map<ns, Map<peerKey, openedAt>>
140
-
141
- constructor(props: ApplicationProps);
142
- listen(port: number): Promise<() => Promise<void>>; // 自动连 Registry + 启心跳
143
-
144
- get(namespace: string, exclude?: string[]): Promise<Client>;
145
- // call() = get + request + response + 熔断 + 重试 + 超时
146
- call<T = any>(
147
- namespace: string,
148
- url: string,
149
- data: any,
150
- timeout?: number, // 单次超时, 默认 requestTimeoutMs
151
- retries?: number, // 重试次数, 默认 1
152
- ): Promise<T>;
153
-
154
- // 远程读取 Registry 的配置(强类型,按 namespace + fields)
155
- getEnvVariables<
156
- T extends Record<string, Record<string, any>>,
157
- const Requests extends readonly EnvRequest<T>[],
158
- >(...data: Requests): Promise<GetEnvVariablesResult<T, Requests>>;
159
-
160
- // 继承自 Server/MessageLoader:
161
- // register<T, E>(url, handler): () => void;
162
- // dispatch(url, data, extras): Promise<any>;
163
- }
164
- ```
165
-
166
- ---
167
-
168
- ## 3. 功能详解(含状态机与逻辑流)
169
-
170
- ### 3.1 服务发现 `get(namespace, exclude?)`
171
-
172
- ```
173
- get(ns, exclude?)
174
- ├─ namespaces 无此 ns → 创建 IDLE 条目
175
- ├─ status=READY + (client 已断连 or peer 被 exclude)
176
- │ └─ 重置为 IDLE, 清空 host/port
177
- ├─ status=READY + client 有效 + 未被 exclude
178
- │ └─ 直接返回缓存 Client (快路径)
179
- └─ 否则:
180
- └─ 新建 Promise, handler 入队
181
- └─ status=IDLE → PENDING → findFromRegistry(ns, exclude)
182
- ├─ Registry 返回地址 → connect → status=READY
183
- │ └─ 注册 disconnect → 清理 namespace 缓存
184
- │ └─ resolve 所有 handler
185
- └─ Registry 失败 or 无数据 →
186
- └─ catch: 检查旧缓存 (cachedHost/cachedPort)
187
- ├─ 缓存有效 → 降级: restore status=READY, resolve
188
- └─ 无缓存 → delete namespace, reject 所有 handler
189
- └─ finally: handlers.clear()
190
- ```
191
-
192
- **关键点:**
193
- - `cachedHost` / `cachedPort` 在缓存失效前保存,用于降级路径
194
- - 降级时恢复 `stack.host/port/status=READY`,使后续调用走快路径
195
- - 多并发 `get()` 共享同一个 Promise,handler 入队后统一 resolve/reject
196
-
197
- ### 3.2 熔断器 (Circuit Breaker)
198
-
199
- **数据结构:** `Map<namespace, Map<peerKey, openedAt>>`
200
-
201
- ```
202
- call() 失败:
203
- recordFailure(ns, host, port)
204
- → excludes.set("host:port", Date.now())
205
-
206
- call() 成功:
207
- recordSuccess(ns, host, port)
208
- → excludes.delete("host:port")
209
-
210
- getActiveExcludes(ns):
211
- → 遍历 excludes, 移除 Date.now() - openedAt >= 30000 的过期条目
212
- → 返回活跃的 exclude key 数组
213
- ```
214
-
215
- **生命周期:**
216
-
217
- ```
218
- peer 首次失败
219
- → circuitBreakers: { svc: { "127.0.0.1:8080": now } }
220
- → getActiveExcludes("svc") → ["127.0.0.1:8080"]
221
- → next call() 的 get() 带上 exclude, 排除该 peer
222
- → Registry 返回其他 peer (有则) 或 undefined (无则)
223
- → 如果所有 peer 都被排除, catch 块 delete circuitBreakers, 无 exclude 重试
224
- => "全排除 → 重置" 策略: 当 Registry 找不到未被排除的 peer, 熔断器清空, 从不安全全量重试
225
- ```
226
-
227
- **冷卻期:** `CB_COOLDOWN_MS = 30000` (30 秒)。到期后 `getActiveExcludes` 自动清除旧条目。
228
-
229
- ### 3.3 请求超时
230
-
231
- **配置链:**
232
-
233
- ```
234
- ApplicationProps.requestTimeoutMs (default 30_000)
235
- → call(timeout?) // 可选 override
236
- → client.request(url, data, timeout ?? this._requestTimeoutMs)
237
- → MessageModem._send({ url, data }, timeout)
238
- → MessageModem._write(data, timeout, twoway=true)
239
- → setTimeout(reject, timeout)
240
- → 超时: 发送 ABORT 消息 → 对端取消执行
241
- ```
242
-
243
- - 超时默认 30 秒(MessageModem 默认值)
244
- - 超时触发时向对端发送 **ABORT** 消息(不是仅仅本地 reject)
245
- - 支持每个调用单独覆盖
246
-
247
- ### 3.5 手动取消(abort)
248
-
249
- `client.request()` 返回的 `abort()` 函数可主动取消请求,**与超时共享同一套 ABORT 机制**:
250
-
251
- ```typescript
252
- const client = await app.get('svc');
253
- const { response, abort } = client.request('/api', data);
254
-
255
- // 主动取消
256
- abort();
257
- await response(); // → reject (AbortException)
258
-
259
- // 典型场景:竞态淘汰
260
- const req = client.request('/slow', data);
261
- // 如果其他条件满足,提前取消
262
- if (cached) abort();
263
- ```
264
-
265
- **实现原理:**
266
- - `request()` 内部通过 `MessageModem._write()` 创建 `AbortController`
267
- - `abort()` 调用 `controller.abort()` 触发 ABORT 消息发送
268
- - 超时到期也调用同一个 `controller.abort()`,机制完全相同
269
- - 区别仅在于触发源头:手动调用 vs 定时器到期
270
-
271
- **适用场景:** 用户取消操作、页面/组件卸载、竞态条件(先发请求后发先至时取消旧请求)
272
-
273
- ```typescript
274
- call(ns, url, data, timeout, retries = 1):
275
- get(ns, exclude) → client
276
- try:
277
- client.request(url, data, timeout) → response() → result
278
- recordSuccess(ns, host, port)
279
- return result
280
- catch err:
281
- recordFailure(ns, host, port)
282
- if retries > 0:
283
- return call(ns, url, data, timeout, retries - 1) // 递归
284
- throw err
285
- ```
286
-
287
- **重试语义:**
288
- - 首次失败 → peer 被排除
289
- - 递归 `call(retries-1)` → `getActiveExcludes` 包含已失败的 peer
290
- - `get()` 带上 exclude → Registry 返回其他 peer
291
- - 所有 peer 都失败 → 熔断全排除 → catch 块重置 → 无 exclude 重试
292
- - 每个 retry 共享同一个 timeout 值(不会延长总时间)
293
-
294
- ### 3.6 健康检查
295
-
296
- 在 `Application` 构造函数中注册:
297
-
298
- ```typescript
299
- this.register('/-/health', async () => ({
300
- status: 'ok' as const,
301
- registry: !!this.registry, // 是否连上 Registry
302
- uptime: process.uptime(), // 进程运行秒数
303
- namespaces: [...this.namespaces.keys()], // 已缓存的 namespace
304
- }));
305
- ```
306
-
307
- 只在 Application 上注册,Server 和 Registry 没有。
308
-
309
- ### 3.7 Registry 心跳
310
-
311
- ```
312
- Application (10s 间隔):
313
- startHeartbeat():
314
- setInterval(10000):
315
- registry.push('/-/heartbeat', {})
316
-
317
- Registry (1s 间隔检查, 20s 超时):
318
- 构造函数:
319
- events.on('connect', ...) → heartbeats.set(key, Date.now())
320
- register('/-/heartbeat', ...) → heartbeats.set(key, Date.now())
321
- listen():
322
- setInterval(1000):
323
- for each heartbeat entry:
324
- if now - lastTime >= 20000:
325
- clients.get(key).dispose() // 剔除死实例
326
- ```
327
-
328
- ### 3.8 Registry 重连
329
-
330
- ```
331
- listen():
332
- reconnectToRegistry() → connect + events.on('disconnect')
333
-
334
- disconnect 触发:
335
- registry = undefined
336
- reconnectToRegistry():
337
- ├─ 成功 → 恢复正常
338
- └─ 失败 → scheduleRegistryRetry():
339
- └─ setTimeout(3000) → reconnectToRegistry()
340
- └─ 失败 → scheduleRegistryRetry() (循环)
341
-
342
- listen() teardown 触发:
343
- stopped = true → 停止所有重连尝试
344
- ```
345
-
346
- ### 3.9 缓存降级
347
-
348
- **触发条件:** `get()` 的 registry lookup 失败,但 `this.clients` 中仍然有之前缓存的 Client(WebSocket 未断开)。
349
-
350
- **处理流程:**
351
- 1. `cachedHost` / `cachedPort` 在缓存失效前保存
352
- 2. Registry lookup 失败 → catch 块
353
- 3. `cachedHost && this.clients.has(cachedKey)` → 有效缓存
354
- 4. 恢复 `stack.host/port/status=READY`,resolve 所有 handler
355
- 5. 后续 `get()` 命中快路径,直接返回该 Client
356
-
357
- **不处理的情况:** 全新 namespace(无缓存)、缓存 Client 已断连。
358
-
359
- ### 3.10 配置管理
360
-
361
- **存储结构:**
362
-
363
- ```
364
- ~/.registry/
365
- └── configs/
366
- ├── service-a.config.yaml
367
- ├── service-b.config.yaml
368
- └── global.config.yaml
369
- ```
370
-
371
- **Registry 侧:**
372
-
373
- ```
374
- Registry 构造:
375
- 1. 创建 ~/.registry/ 目录 (自动)
376
-
377
- Registry listen():
378
- 1. 调用 watchEnvFile()
379
- └─ 读取 ~/.registry/configs/ 下所有 *.config.yaml → YAML.parse
380
- └─ 按 namespace 存入 this.configs Map
381
- └─ 监听 configs/ 目录 (兼容 vim 原子写入)
382
- └─ 文件变化 → 重新 YAML.parse 并更新 this.configs
383
-
384
- /-/env/variables 端点:
385
- register('/-/env/variables', async ({ data }) => {
386
- data.map(({ namespace, fields }) => {
387
- if (!this.configs.has(namespace))
388
- return { namespace, value: null }
389
- if (!fields?.length)
390
- return { namespace, value: this.configs.get(namespace) }
391
- // 按 fields 过滤
392
- return { namespace, value: filteredConfig }
393
- })
394
- })
395
- ```
396
-
397
- **Application 侧:**
398
-
399
- ```typescript
400
- // 强类型查询
401
- type EnvRequest<T> = {
402
- [N in keyof T]: { namespace: N; fields?: readonly (keyof T[N])[] };
403
- }[keyof T];
404
-
405
- type GetEnvVariablesResult<T, Requests> = UnionToIntersection<...>;
406
-
407
- getEnvVariables(...data: EnvRequest<T>[]): Promise<GetEnvVariablesResult<T, Requests>>
408
- ```
409
-
410
- **CLI 管理配置:**
411
-
412
- 通过 `hile registry configs` 子命令直接管理 `~/.registry/configs/` 下的 YAML 文件:
413
-
414
- ```
415
- hile registry configs # 列出所有 namespace
416
- hile registry configs get <namespace> # 查看配置(YAML/--json)
417
- hile registry configs set <namespace> <key>=<value> # 设置配置项
418
- hile registry configs del <namespace> [key] # 删除(带确认,-y 跳过)
419
- ```
420
-
421
- - CLI 直接读写 YAML 文件,运行中的 Registry 通过 `fs.watch` 自动感知
422
- - 值类型自动推断:`true/false` → boolean, `null` → null, 纯数字 → number, 以 `{`/`[` 开头 → JSON 解析为 object/array, 其余 → string
423
- - 实现代码在 `packages/cli/src/configs.ts`,工具函数在 `packages/micro/src/registry.ts`
424
-
425
- ---
426
-
427
- ## 4. 代码生成模板
428
-
429
- ### 4.1 三节点测试拓扑(所有 test 必须使用)
430
-
431
- ```typescript
432
- const registryPort = await getAvailablePort();
433
- const providerPort = await getAvailablePort();
434
- const consumerPort = await getAvailablePort();
435
-
436
- const registry = new Registry(testAdvertise);
437
- const provider = new Application({
438
- namespace: 'svc',
439
- registry: { host: '127.0.0.1', port: registryPort },
440
- ...testAdvertise,
441
- });
442
- const consumer = new Application({
443
- namespace: 'consumer',
444
- registry: { host: '127.0.0.1', port: registryPort },
445
- ...testAdvertise,
446
- });
447
-
448
- const disposeRegistry = await registry.listen(registryPort);
449
- const disposeProvider = await provider.listen(providerPort);
450
- const disposeConsumer = await consumer.listen(consumerPort);
451
- const unregister = provider.register('/echo', async ({ data }) => {
452
- return { value: data.value };
453
- });
454
-
455
- try {
456
- // ... test logic ...
457
- } finally {
458
- unregister();
459
- await disposeConsumer();
460
- await disposeProvider();
461
- await disposeRegistry();
462
- }
463
- ```
464
-
465
- ### 4.2 call() 的 timeout + retries 参数顺序
466
-
467
- ```typescript
468
- // signature: call(ns, url, data, timeout?, retries?)
469
- await app.call('svc', '/api', data); // 默认超时 + 1 次重试
470
- await app.call('svc', '/api', data, 5000); // 5s 超时 + 1 次重试
471
- await app.call('svc', '/api', data, 5000, 0); // 5s 超时 + 不重试
472
- await app.call('svc', '/api', data, undefined, 0); // 默认超时 + 不重试
473
- ```
474
-
475
- ### 4.3 超时测试 Handler
476
-
477
- ```typescript
478
- // handler 延迟 500ms, call 超时 50ms → 应 reject
479
- provider.register('/slow', async () => {
480
- await new Promise(resolve => setTimeout(resolve, 500));
481
- return { value: 'too-late' };
482
- });
483
- await expect(consumer.call('svc', '/slow', {}, 50, 0)).rejects.toThrow('Abort');
484
- ```
485
-
486
- ### 4.4 熔断测试模板
487
-
488
- ```typescript
489
- // 两个 provider 同 namespace, 一个失败, 排除后应选另一个
490
- // 见 circuit breaker test "excludes a failing peer and selects a different one"
491
-
492
- // 单个 peer 全排除后应重置并重试该 peer
493
- // 见 circuit breaker test "resets breaker when all peers are excluded"
494
- ```
495
-
496
- ### 4.5 缓存降级测试模板
497
-
498
- ```typescript
499
- // 1. 首次 call → 建立缓存
500
- // 2. 切换为失败 handler → call 失败 → peer 被排除
501
- // 3. 切回成功 handler → call 带 exclude → Registry find 返回 undefined
502
- // 4. catch 块降级 → 返回缓存 Client → 请求成功
503
- ```
504
-
505
- ---
506
-
507
- ## 5. 测试门禁(修改时必须遵守)
508
-
509
- ### 5.1 运行命令
510
-
511
- ```bash
512
- pnpm --filter @hile/micro build # 必须通过
513
- pnpm --filter @hile/micro test # 必须全部通过
514
- ```
515
-
516
- ### 5.2 测试覆盖要求
517
-
518
- 修改行为时至少覆盖:
519
-
520
- | 场景 | 测试位置 |
521
- |------|----------|
522
- | 服务发现端到端 | `application discovery > resolves a provider through the registry` |
523
- | listen teardown 后可重新 listen | `application discovery > allows listen again after teardown` |
524
- | Registry 不可达时 listen 回滚 | `application discovery > rolls back listen when registry is unreachable` |
525
- | 心跳保活 | `heartbeat > keeps client alive when heartbeats arrive on time` |
526
- | 心跳超时剔除 | `heartbeat > disconnects client that stops sending heartbeats` |
527
- | call() 基本调用 | `circuit breaker > call() returns data on success` |
528
- | 熔断排除 | `circuit breaker > excludes a failing peer and selects a different one` |
529
- | 全排除重置 | `circuit breaker > resets breaker when all peers are excluded` |
530
- | 健康检查 | `health endpoint > /-/health returns status and registry state` |
531
- | 超时 reject | `request timeout > rejects when request exceeds the timeout` |
532
- | 超时充足则成功 | `request timeout > succeeds when timeout is long enough` |
533
- | 缓存降级 | `cache degradation > uses cached client when registry lookup fails due to exclusion` |
534
- | YAML 配置加载 | `config file loading > loads yaml config files on watchEnvFile` |
535
- | YAML 配置热加载 | `config file loading > reloads config when yaml file changes` |
536
- | configs 目录不存在 | `config file loading > does not crash when configs directory does not exist` |
537
- | /-/env/variables 按字段过滤 | `/-/env/variables endpoint > returns requested config by namespace and fields` |
538
- | /-/env/variables 全字段 | `/-/env/variables endpoint > returns all config when fields not specified` |
539
- | 不存在的 namespace | `/-/env/variables endpoint > returns null value for non-existent namespace` |
540
- | /-/env/variables 空列表 | `/-/env/variables endpoint > handles empty data list` |
541
- | getEnvVariables 集成 | `Application.getEnvVariables > fetches config from Registry` |
542
- | getEnvVariables 不存在 namespace | `Application.getEnvVariables > returns null when namespace config does not exist` |
543
- | getRegistryConfigsDir | `config file utilities > getRegistryConfigsDir returns path ending with .registry/configs` |
544
- | namespaceToConfigFile | `config file utilities > namespaceToConfigFile returns path with .config.yaml suffix` |
545
- | parseConfigFilename 正例 | `config file utilities > parseConfigFilename extracts namespace from valid filename` |
546
- | parseConfigFilename 反例 | `config file utilities > parseConfigFilename returns null for non-config file` |
547
-
548
- ### 5.3 测试规范(必须遵守)
549
-
550
- - 端口必须使用 `getAvailablePort()` 获取
551
- - 所有清理必须放在 `finally` 块中
552
- - 清理顺序:`unregister()` → `disposeConsumer` → `disposeProvider` → `disposeRegistry`
553
- - 禁止使用真实定时等待代替事件驱动(心跳测试是唯一例外,因其依赖实时时钟)
554
- - 禁止 mock `Application`、`Registry`、`Server`、`Client` 的内部方法
555
- - 禁止共享可变状态(每个测试独立端口)
556
-
557
- ---
558
-
559
- ## 6. 反模式(禁止)
560
-
561
- 1. **不要修改 WebSocket URL 三段式约定**不同时更新 `Server.onConnected` 和 `Server.connect` 的拼接格式
562
- 2. **不要在 Registry 中按 Set 迭代顺序固定返回第一个**(破坏负载分散)
563
- 3. **不要在 `Client.dispose()` 中删除 `socket.close()`**(会导致 WebSocketServer.close 长时间等待)
564
- 4. **不要假设 `host:port` 可无损表达 IPv6** — 使用 `[IPv6]:port` 格式,`parseAddressKey` 按最后一个 `:` 切分
565
- 5. **不要传错 Registry 端口** — 丢失 Registry 连接时依赖 `reconnectToRegistry`,不要在外部缓存 registry Client
566
- 6. **不要在其他文件中重复 Registry 的 helper 函数** — `selectRandomRegistryAddress`、`parseAddressKey`、`getRegistryConfigsDir`、`namespaceToConfigFile`、`parseConfigFilename` 都在 `registry.ts` 中导出复用
567
- 7. **不要给 call() 增加非可选参数** — `timeout` 和 `retries` 都在尾部且保持可选,不影响现有调用
568
-
569
- ---
570
-
571
- ## 7. 文件改动范围
572
-
573
- | 文件 | 可修改 | 说明 |
574
- |------|--------|------|
575
- | `packages/micro/src/application.ts` | ✅ | 核心业务逻辑 |
576
- | `packages/micro/src/index.test.ts` | ✅ | 测试(主测试文件) |
577
- | `packages/micro/src/env-config.test.ts` | ✅ | 测试(环境变量配置测试) |
578
- | `packages/micro/src/server.ts` | ❌ | 底层协议,不动 |
579
- | `packages/micro/src/client.ts` | ❌ | 底层协议,不动 |
580
- | `packages/micro/src/registry.ts` | ✅ | 注册中心(配置管理、路径工具函数) |
581
- | `packages/micro/src/utils.ts` | ❌ | 工具函数,不动 |
582
- | `packages/micro/README.md` | ✅ | 用户文档 |
583
- | `packages/micro/SKILL.md` | ✅ | AI 参考文档 |
584
- | `packages/cli/src/index.ts` | ✅ | CLI 入口(registry configs 子命令组) |
585
- | `packages/cli/src/configs.ts` | ✅ | CLI 配置管理 handler(list/get/set/del) |
586
- | `packages/cli/src/start.ts` | ❌ | 启动逻辑,不动 |
587
- | `packages/cli/src/exitHook.ts` | ❌ | 退出钩子,不动 |
588
-
589
- ---
590
-
591
- ## 8. 参考文件
592
-
593
- | 文件 | 用途 |
594
- |------|------|
595
- | `packages/micro/src/registry.ts` | 注册中心(含配置管理、路径工具函数) |
596
- | `packages/micro/src/application.ts` | 应用服务(含 getEnvVariables) |
597
- | `packages/micro/src/index.test.ts` | 主测试文件(27 个用例) |
598
- | `packages/micro/src/env-config.test.ts` | 配置管理测试文件(13 个用例) |
599
- | `packages/cli/src/index.ts` | CLI 入口(含 registry configs 子命令组) |
600
- | `packages/cli/src/configs.ts` | CLI 配置管理 handler(list/get/set/del) |