@hile/message-modem 1.0.1 → 1.0.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.
- package/README.md +22 -6
- package/SKILL.md +9 -7
- package/dist/index.d.ts +16 -2
- package/dist/index.js +26 -5
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hile/message-modem
|
|
2
2
|
|
|
3
|
-
传输无关的请求/响应消息通信抽象层。将底层传输机制(WebSocket、postMessage、IPC 等)与业务逻辑解耦,提供统一的
|
|
3
|
+
传输无关的请求/响应消息通信抽象层。将底层传输机制(WebSocket、postMessage、IPC 等)与业务逻辑解耦,提供统一的 _send/receive 语义。
|
|
4
4
|
|
|
5
5
|
## 安装
|
|
6
6
|
|
|
@@ -11,6 +11,8 @@ pnpm add @hile/message-modem
|
|
|
11
11
|
## 核心特性
|
|
12
12
|
|
|
13
13
|
- **传输无关** — 子类只需实现 `post`(如何发送)和 `exec`(如何处理),即可运行于任何通信通道
|
|
14
|
+
- **双向请求/响应** — `_send` 发送请求并等待对端响应(`twoway: true`)
|
|
15
|
+
- **单向推送** — `_push` 发送消息无需对端响应(`twoway: false`)
|
|
14
16
|
- **请求/响应配对** — 自增 ID + Promise 栈,自动配对请求与响应
|
|
15
17
|
- **超时控制** — 默认 30 秒,可按请求自定义
|
|
16
18
|
- **主动中止** — 发送方可 abort 等待,接收方可取消正在执行的任务
|
|
@@ -42,7 +44,7 @@ class WebSocketModem extends MessageModem {
|
|
|
42
44
|
|
|
43
45
|
// 暴露发送方法
|
|
44
46
|
public request<T>(data: T, timeout?: number) {
|
|
45
|
-
return this.
|
|
47
|
+
return this._send(data, timeout);
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
```
|
|
@@ -89,10 +91,11 @@ try {
|
|
|
89
91
|
|------|--------|------|
|
|
90
92
|
| `post(data)` | `protected abstract` | 子类实现:如何将消息发送到远端 |
|
|
91
93
|
| `exec(data)` | `protected abstract` | 子类实现:如何处理收到的请求,返回 Promise |
|
|
92
|
-
| `
|
|
94
|
+
| `_send(data, timeout?)` | `protected` | 发送双向请求(`twoway: true`),返回 `{ abort, response }` |
|
|
95
|
+
| `_push(data, timeout?)` | `protected` | 发送单向推送(`twoway: false`),无返回值,接收方不回复 RESPONSE |
|
|
93
96
|
| `receive(msg)` | `public` | 接收消息入口,根据 mode 分发处理 |
|
|
94
97
|
|
|
95
|
-
### `
|
|
98
|
+
### `_send` 返回值
|
|
96
99
|
|
|
97
100
|
| 属性 | 类型 | 说明 |
|
|
98
101
|
|------|------|------|
|
|
@@ -136,11 +139,13 @@ interface MessageReturnFormat<T = any> {
|
|
|
136
139
|
|
|
137
140
|
## 消息流转
|
|
138
141
|
|
|
142
|
+
### 双向模式(`_send`)
|
|
143
|
+
|
|
139
144
|
```
|
|
140
145
|
发送方 接收方
|
|
141
146
|
│ │
|
|
142
|
-
│
|
|
143
|
-
│──── REQUEST
|
|
147
|
+
│ _send(data) │
|
|
148
|
+
│──── REQUEST (twoway) ──────►│
|
|
144
149
|
│ │ exec(data)
|
|
145
150
|
│ │
|
|
146
151
|
│◄──── RESPONSE ──────────────│
|
|
@@ -151,6 +156,17 @@ interface MessageReturnFormat<T = any> {
|
|
|
151
156
|
│ │ 取消 exec
|
|
152
157
|
```
|
|
153
158
|
|
|
159
|
+
### 单向模式(`_push`)
|
|
160
|
+
|
|
161
|
+
```
|
|
162
|
+
发送方 接收方
|
|
163
|
+
│ │
|
|
164
|
+
│ _push(data) │
|
|
165
|
+
│──── REQUEST (!twoway) ─────►│
|
|
166
|
+
│ │ exec(data)
|
|
167
|
+
│ │ (不回复 RESPONSE)
|
|
168
|
+
```
|
|
169
|
+
|
|
154
170
|
## 适用场景
|
|
155
171
|
|
|
156
172
|
- **iframe 通信** — 父子页面 postMessage
|
package/SKILL.md
CHANGED
|
@@ -11,7 +11,7 @@ description: Code generation and contribution rules for @hile/message-modem. Use
|
|
|
11
11
|
|
|
12
12
|
## 1. 架构总览
|
|
13
13
|
|
|
14
|
-
`@hile/message-modem` 是一个 **传输无关的请求/响应消息通信抽象层**。它将底层传输(WebSocket、postMessage、IPC 等)与业务逻辑解耦,提供统一的
|
|
14
|
+
`@hile/message-modem` 是一个 **传输无关的请求/响应消息通信抽象层**。它将底层传输(WebSocket、postMessage、IPC 等)与业务逻辑解耦,提供统一的 _send/receive 语义。
|
|
15
15
|
|
|
16
16
|
核心职责:
|
|
17
17
|
|
|
@@ -84,10 +84,11 @@ class AbortException extends Exception {
|
|
|
84
84
|
abstract class MessageModem {
|
|
85
85
|
protected abstract post<T>(data: MessageTransferFormat<T>): void;
|
|
86
86
|
protected abstract exec(data: any): Promise<any>;
|
|
87
|
-
protected
|
|
87
|
+
protected _send<T>(data: T, timeout?: number): {
|
|
88
88
|
abort: () => void;
|
|
89
89
|
response: <U = any>() => Promise<U>;
|
|
90
90
|
};
|
|
91
|
+
protected _push<T>(data: T, timeout?: number): void;
|
|
91
92
|
public receive(msg: MessageTransferFormat): void;
|
|
92
93
|
}
|
|
93
94
|
```
|
|
@@ -118,9 +119,9 @@ class WebSocketModem extends MessageModem {
|
|
|
118
119
|
return handleRequest(data);
|
|
119
120
|
}
|
|
120
121
|
|
|
121
|
-
// 暴露
|
|
122
|
-
public request<T
|
|
123
|
-
return this.
|
|
122
|
+
// 暴露 _send 为 public
|
|
123
|
+
public request<T>(data: T, timeout?: number) {
|
|
124
|
+
return this._send(data, timeout);
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
```
|
|
@@ -151,11 +152,12 @@ class IframeModem extends MessageModem {
|
|
|
151
152
|
| 规则 | 说明 |
|
|
152
153
|
|------|------|
|
|
153
154
|
| **必须实现 `post` 和 `exec`** | 两个 abstract 方法缺一不可 |
|
|
154
|
-
| **`
|
|
155
|
+
| **`_send` 是 `protected`** | 子类应自行决定暴露方式和命名 |
|
|
156
|
+
| **`_push` 是 `protected`** | 单向推送(`twoway: false`),接收方不回复 RESPONSE |
|
|
155
157
|
| **`receive` 是 `public`** | 必须由外部消息源(事件监听器)调用 |
|
|
156
158
|
| **传输格式必须保持原样** | `post` 发送的对象结构不可修改,对端的 `receive` 依赖完整的 `MessageTransferFormat` |
|
|
157
159
|
| **`exec` 抛出 `Exception` 时 status 会透传** | 其他 Error 一律映射为 500 |
|
|
158
|
-
| **timeout 默认 30s** | 可通过 `
|
|
160
|
+
| **timeout 默认 30s** | 可通过 `_send(data, ms)` 覆盖 |
|
|
159
161
|
| **abort 后 promise reject `AbortException`** | 不要 catch 后吞掉,保持语义清晰 |
|
|
160
162
|
|
|
161
163
|
### 3.4 反模式
|
package/dist/index.d.ts
CHANGED
|
@@ -49,10 +49,24 @@ export declare abstract class MessageModem {
|
|
|
49
49
|
* @param timeout - 超时时间
|
|
50
50
|
* @returns 消息响应
|
|
51
51
|
*/
|
|
52
|
-
protected
|
|
52
|
+
protected _send<T = any>(data: T, timeout?: number): {
|
|
53
53
|
abort: () => void;
|
|
54
54
|
response: <U = any>() => Promise<U>;
|
|
55
|
-
};
|
|
55
|
+
} | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* 推送消息
|
|
58
|
+
* @param data - 消息数据
|
|
59
|
+
* @param timeout - 超时时间
|
|
60
|
+
* @returns 消息响应
|
|
61
|
+
*/
|
|
62
|
+
protected _push<T = any>(data: T, timeout?: number): void;
|
|
63
|
+
/**
|
|
64
|
+
* 写入消息
|
|
65
|
+
* @param data - 消息数据
|
|
66
|
+
* @param timeout - 超时时间
|
|
67
|
+
* @returns 消息响应
|
|
68
|
+
*/
|
|
69
|
+
private _write;
|
|
56
70
|
/**
|
|
57
71
|
* 处理请求消息
|
|
58
72
|
* @param msg - 消息数据
|
package/dist/index.js
CHANGED
|
@@ -28,10 +28,10 @@ export class MessageModem {
|
|
|
28
28
|
* @param data - 消息数据
|
|
29
29
|
* @returns 消息数据
|
|
30
30
|
*/
|
|
31
|
-
createPostData(mode, data) {
|
|
31
|
+
createPostData(mode, data, twoway = true) {
|
|
32
32
|
const id = this.createIncrementId();
|
|
33
33
|
const state = {
|
|
34
|
-
id, twoway
|
|
34
|
+
id, twoway, data, mode,
|
|
35
35
|
};
|
|
36
36
|
if (mode === MESSAGE_MODEM_TYPE.ABORT) {
|
|
37
37
|
state.twoway = false;
|
|
@@ -44,12 +44,33 @@ export class MessageModem {
|
|
|
44
44
|
* @param timeout - 超时时间
|
|
45
45
|
* @returns 消息响应
|
|
46
46
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
_send(data, timeout = 30000) {
|
|
48
|
+
return this._write(data, timeout, true);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 推送消息
|
|
52
|
+
* @param data - 消息数据
|
|
53
|
+
* @param timeout - 超时时间
|
|
54
|
+
* @returns 消息响应
|
|
55
|
+
*/
|
|
56
|
+
_push(data, timeout = 30000) {
|
|
57
|
+
this._write(data, timeout, false);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 写入消息
|
|
61
|
+
* @param data - 消息数据
|
|
62
|
+
* @param timeout - 超时时间
|
|
63
|
+
* @returns 消息响应
|
|
64
|
+
*/
|
|
65
|
+
_write(data, timeout = 30000, twoway = false) {
|
|
49
66
|
// 创建请求消息数据
|
|
50
|
-
const state = this.createPostData(MESSAGE_MODEM_TYPE.REQUEST, data);
|
|
67
|
+
const state = this.createPostData(MESSAGE_MODEM_TYPE.REQUEST, data, twoway);
|
|
51
68
|
// 发送消息
|
|
52
69
|
this.post(state);
|
|
70
|
+
// 如果消息是单向的,则直接返回
|
|
71
|
+
if (!twoway)
|
|
72
|
+
return;
|
|
73
|
+
const controller = new AbortController();
|
|
53
74
|
return {
|
|
54
75
|
// 终止请求
|
|
55
76
|
abort: () => controller.abort(),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/message-modem",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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": "0e351166b8351d21e1a97543bc8583ef9513fb67"
|
|
25
25
|
}
|