@hile/message-modem 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.
- package/package.json +2 -2
- package/SKILL.md +0 -180
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/message-modem",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,5 +21,5 @@
|
|
|
21
21
|
"fix-esm-import-path": "^1.10.3",
|
|
22
22
|
"vitest": "^4.0.18"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "2c8011db01f2815e5ce34de964d5492640396828"
|
|
25
25
|
}
|
package/SKILL.md
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: message-modem
|
|
3
|
-
description: Code generation and contribution rules for @hile/message-modem. Use when editing this package or when the user asks about @hile/message-modem patterns or API.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# @hile/message-modem
|
|
7
|
-
|
|
8
|
-
本文档是面向 AI 编码模型和人类开发者的 **代码生成规范**,阅读后应能正确地使用本库编写符合架构规则的代码。
|
|
9
|
-
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
## 1. 架构总览
|
|
13
|
-
|
|
14
|
-
`@hile/message-modem` 是一个 **传输无关的请求/响应消息通信抽象层**。它将底层传输(WebSocket、postMessage、IPC 等)与业务逻辑解耦,提供统一的 _send/receive 语义。
|
|
15
|
-
|
|
16
|
-
核心职责:
|
|
17
|
-
|
|
18
|
-
- 自增 ID 管理与安全重置(超过 `MAX_SAFE_INTEGER` 时归零)
|
|
19
|
-
- 请求/响应配对(通过 ID + stacks Map)
|
|
20
|
-
- 超时控制(基于 `AbortController` + `setTimeout`)
|
|
21
|
-
- 主动中止(abort):发送方可中止等待,接收方可取消正在执行的任务
|
|
22
|
-
- 错误传播:`Exception` 携带 `status`;非 `Exception` 错误映射为 500
|
|
23
|
-
|
|
24
|
-
导出方式:
|
|
25
|
-
|
|
26
|
-
主入口 `index.ts` 通过 `export * from './exception'` 重新导出所有异常类,因此使用者可以从 `@hile/message-modem` 统一导入所有类型:
|
|
27
|
-
|
|
28
|
-
```typescript
|
|
29
|
-
import { MessageModem, Exception, AbortException, TimeoutException, MESSAGE_MODEM_TYPE } from '@hile/message-modem';
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
继承关系:
|
|
33
|
-
|
|
34
|
-
```
|
|
35
|
-
MessageModem (abstract)
|
|
36
|
-
├── post(data) — 子类实现:如何发送到远端
|
|
37
|
-
└── exec(data) — 子类实现:如何处理收到的请求
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
---
|
|
41
|
-
|
|
42
|
-
## 2. 类型签名
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
// ---- 枚举 ----
|
|
46
|
-
enum MESSAGE_MODEM_TYPE {
|
|
47
|
-
REQUEST, // 请求
|
|
48
|
-
RESPONSE, // 响应
|
|
49
|
-
ABORT, // 中止
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// ---- 传输格式 ----
|
|
53
|
-
interface MessageTransferFormat<T = any> {
|
|
54
|
-
id: number;
|
|
55
|
-
mode: MESSAGE_MODEM_TYPE;
|
|
56
|
-
twoway: boolean;
|
|
57
|
-
data?: T;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// ---- 响应格式 ----
|
|
61
|
-
interface MessageReturnFormat<T = any> {
|
|
62
|
-
status: string | number;
|
|
63
|
-
data: T;
|
|
64
|
-
message: string;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// ---- 异常 ----
|
|
68
|
-
class Exception extends Error {
|
|
69
|
-
readonly status: number | string;
|
|
70
|
-
constructor(status: number | string, msg: string);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
class TimeoutException extends Exception {
|
|
74
|
-
static readonly code = 'ETIMEDOUT';
|
|
75
|
-
constructor(msg?: string); // 默认 'Timeout'
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
class AbortException extends Exception {
|
|
79
|
-
static readonly code = 'ECONNABORTED';
|
|
80
|
-
constructor(msg?: string); // 默认 'Abort'
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// ---- 抽象基类 ----
|
|
84
|
-
abstract class MessageModem {
|
|
85
|
-
protected abstract post<T>(data: MessageTransferFormat<T>): void;
|
|
86
|
-
protected abstract exec(data: any): Promise<any>;
|
|
87
|
-
protected _send<T>(data: T, timeout?: number): {
|
|
88
|
-
abort: () => void;
|
|
89
|
-
response: <U = any>() => Promise<U>;
|
|
90
|
-
};
|
|
91
|
-
protected _push<T>(data: T, timeout?: number): void;
|
|
92
|
-
public receive(msg: MessageTransferFormat): void;
|
|
93
|
-
}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
---
|
|
97
|
-
|
|
98
|
-
## 3. 代码生成模板与规则
|
|
99
|
-
|
|
100
|
-
### 3.1 子类实现模板
|
|
101
|
-
|
|
102
|
-
```typescript
|
|
103
|
-
import { MessageModem, type MessageTransferFormat } from '@hile/message-modem';
|
|
104
|
-
|
|
105
|
-
class WebSocketModem extends MessageModem {
|
|
106
|
-
constructor(private ws: WebSocket) {
|
|
107
|
-
super();
|
|
108
|
-
ws.addEventListener('message', (e) => {
|
|
109
|
-
this.receive(JSON.parse(e.data));
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
protected post<T>(data: MessageTransferFormat<T>): void {
|
|
114
|
-
this.ws.send(JSON.stringify(data));
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
protected async exec(data: any): Promise<any> {
|
|
118
|
-
// 处理远端请求的业务逻辑
|
|
119
|
-
return handleRequest(data);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// 暴露 _send 为 public
|
|
123
|
-
public request<T>(data: T, timeout?: number) {
|
|
124
|
-
return this._send(data, timeout);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
### 3.2 postMessage 场景模板
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
class IframeModem extends MessageModem {
|
|
133
|
-
constructor(private target: Window, private origin: string) {
|
|
134
|
-
super();
|
|
135
|
-
window.addEventListener('message', (e) => {
|
|
136
|
-
if (e.origin === origin) this.receive(e.data);
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
protected post<T>(data: MessageTransferFormat<T>): void {
|
|
141
|
-
this.target.postMessage(data, this.origin);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
protected async exec(data: any): Promise<any> {
|
|
145
|
-
return handleIframeRequest(data);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
### 3.3 强制规则
|
|
151
|
-
|
|
152
|
-
| 规则 | 说明 |
|
|
153
|
-
|------|------|
|
|
154
|
-
| **必须实现 `post` 和 `exec`** | 两个 abstract 方法缺一不可 |
|
|
155
|
-
| **`_send` 是 `protected`** | 子类应自行决定暴露方式和命名 |
|
|
156
|
-
| **`_push` 是 `protected`** | 单向推送(`twoway: false`),接收方不回复 RESPONSE |
|
|
157
|
-
| **`receive` 是 `public`** | 必须由外部消息源(事件监听器)调用 |
|
|
158
|
-
| **传输格式必须保持原样** | `post` 发送的对象结构不可修改,对端的 `receive` 依赖完整的 `MessageTransferFormat` |
|
|
159
|
-
| **`exec` 抛出 `Exception` 时 status 会透传** | 其他 Error 一律映射为 500 |
|
|
160
|
-
| **timeout 默认 30s** | 可通过 `_send(data, ms)` 覆盖 |
|
|
161
|
-
| **abort 后 promise reject `AbortException`** | 不要 catch 后吞掉,保持语义清晰 |
|
|
162
|
-
|
|
163
|
-
### 3.4 反模式
|
|
164
|
-
|
|
165
|
-
```typescript
|
|
166
|
-
// ❌ 不要在 post 中做异步操作
|
|
167
|
-
protected async post(data) { await fetch(...); }
|
|
168
|
-
// ✅ post 应该是同步的,异步传输应缓冲
|
|
169
|
-
|
|
170
|
-
// ❌ 不要直接修改 MessageTransferFormat 结构
|
|
171
|
-
this.post({ ...data, extra: 'field' });
|
|
172
|
-
// ✅ 业务数据放在 data 字段内
|
|
173
|
-
|
|
174
|
-
// ❌ 不要忘记连接 receive
|
|
175
|
-
// ✅ 在构造函数中绑定消息事件 → this.receive(parsed)
|
|
176
|
-
|
|
177
|
-
// ❌ 不要在 exec 中吞掉错误
|
|
178
|
-
protected async exec(data) { try { ... } catch { return null; } }
|
|
179
|
-
// ✅ 让错误冒泡,框架会处理错误响应
|
|
180
|
-
```
|