@hile/message-ipc 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 +3 -3
- package/SKILL.md +0 -167
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/message-ipc",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"vitest": "^4.0.18"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@hile/message-modem": "^2.0.
|
|
25
|
+
"@hile/message-modem": "^2.0.2"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "2c8011db01f2815e5ce34de964d5492640396828"
|
|
28
28
|
}
|
package/SKILL.md
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: message-ipc
|
|
3
|
-
description: Code generation and contribution rules for @hile/message-ipc. Use when editing this package or when the user asks about @hile/message-ipc patterns or API.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# @hile/message-ipc
|
|
7
|
-
|
|
8
|
-
本文档是面向 AI 编码模型和人类开发者的 **代码生成规范**,阅读后应能正确地使用本库编写符合架构规则的代码。
|
|
9
|
-
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
## 1. 架构总览
|
|
13
|
-
|
|
14
|
-
`@hile/message-ipc` 是 `@hile/message-modem` 的 **Node.js IPC 抽象实现**,用于父子进程间的请求/响应通信。
|
|
15
|
-
|
|
16
|
-
`MessageIpc` 本身是 **抽象类**,只实现了 `post`(通过 IPC 通道发送)和消息监听,`exec` 方法留给子类实现具体的请求处理逻辑。
|
|
17
|
-
|
|
18
|
-
继承链:
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
MessageModem (abstract) ← post + exec 均抽象
|
|
22
|
-
└── MessageIpc (abstract) ← 实现 post,exec 仍抽象
|
|
23
|
-
└── 用户子类 ← 实现 exec
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
---
|
|
27
|
-
|
|
28
|
-
## 2. 类型签名
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
import type { ChildProcess } from 'node:child_process';
|
|
32
|
-
import { MessageModem, type MessageTransferFormat } from '@hile/message-modem';
|
|
33
|
-
|
|
34
|
-
type IpcExecHandler = (data: any) => Promise<any>;
|
|
35
|
-
|
|
36
|
-
abstract class MessageIpc extends MessageModem {
|
|
37
|
-
constructor(channel?: ChildProcess);
|
|
38
|
-
|
|
39
|
-
// 子类必须实现
|
|
40
|
-
protected abstract exec(data: any): Promise<any>;
|
|
41
|
-
|
|
42
|
-
// 发送双向请求(protected,子类自行暴露)
|
|
43
|
-
protected _send<T = any>(data: T, timeout?: number): {
|
|
44
|
-
abort: () => void;
|
|
45
|
-
response: <U = any>() => Promise<U>;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// 发送单向推送(protected,子类自行暴露)
|
|
49
|
-
protected _push<T = any>(data: T, timeout?: number): {
|
|
50
|
-
abort: () => void;
|
|
51
|
-
response: <U = any>() => Promise<U>;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// 移除消息监听
|
|
55
|
-
public dispose(): void;
|
|
56
|
-
}
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
---
|
|
60
|
-
|
|
61
|
-
## 3. 代码生成模板与规则
|
|
62
|
-
|
|
63
|
-
### 3.1 基本子类模板
|
|
64
|
-
|
|
65
|
-
```typescript
|
|
66
|
-
import { MessageIpc } from '@hile/message-ipc';
|
|
67
|
-
|
|
68
|
-
class MyIpc extends MessageIpc {
|
|
69
|
-
protected async exec(data: any): Promise<any> {
|
|
70
|
-
// 处理对端发来的请求
|
|
71
|
-
switch (data?.action) {
|
|
72
|
-
case 'ping':
|
|
73
|
-
return 'pong';
|
|
74
|
-
default:
|
|
75
|
-
return data;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 自行暴露发送方法
|
|
80
|
-
public request<T = any>(data: T, timeout?: number) {
|
|
81
|
-
return this._send(data, timeout);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
### 3.2 父进程端模板
|
|
87
|
-
|
|
88
|
-
```typescript
|
|
89
|
-
import { fork } from 'node:child_process';
|
|
90
|
-
|
|
91
|
-
class ParentIpc extends MessageIpc {
|
|
92
|
-
protected async exec(data: any): Promise<any> {
|
|
93
|
-
// 处理子进程发来的请求
|
|
94
|
-
return { reply: 'from parent', query: data };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public request<T = any>(data: T, timeout?: number) {
|
|
98
|
-
return this._send(data, timeout);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const child = fork('./worker.js');
|
|
103
|
-
const ipc = new ParentIpc(child);
|
|
104
|
-
|
|
105
|
-
const result = await ipc.request({ action: 'compute', value: 42 }).response();
|
|
106
|
-
console.log(result);
|
|
107
|
-
|
|
108
|
-
ipc.dispose();
|
|
109
|
-
child.kill();
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### 3.3 子进程端模板
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
// worker.js
|
|
116
|
-
import { MessageIpc } from '@hile/message-ipc';
|
|
117
|
-
import { Exception } from '@hile/message-modem';
|
|
118
|
-
|
|
119
|
-
class WorkerIpc extends MessageIpc {
|
|
120
|
-
protected async exec(data: any): Promise<any> {
|
|
121
|
-
if (data.action === 'compute') return data.value * 2;
|
|
122
|
-
if (data.action === 'restricted') throw new Exception(403, 'not allowed');
|
|
123
|
-
return data;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public request<T = any>(data: T, timeout?: number) {
|
|
127
|
-
return this._send(data, timeout);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const ipc = new WorkerIpc(); // 无参数 → 使用 process
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### 3.4 强制规则
|
|
135
|
-
|
|
136
|
-
| 规则 | 说明 |
|
|
137
|
-
|------|------|
|
|
138
|
-
| **必须继承 `MessageIpc` 并实现 `exec`** | `MessageIpc` 是抽象类,不能直接实例化 |
|
|
139
|
-
| **父进程端必须传入 `ChildProcess`** | `fork()` 返回值 |
|
|
140
|
-
| **子进程端不传参数** | 自动使用 `process`,要求进程通过 `fork()` 启动 |
|
|
141
|
-
| **用完必须 `dispose()`** | 避免内存泄漏 |
|
|
142
|
-
| **`_send` 是 `protected`** | 子类需自行暴露为 public 方法(如 `request`) |
|
|
143
|
-
| **`_push` 是 `protected`** | 单向推送,接收方不回复 RESPONSE。子类自行暴露 |
|
|
144
|
-
|
|
145
|
-
### 3.5 反模式
|
|
146
|
-
|
|
147
|
-
```typescript
|
|
148
|
-
// ❌ 不能直接实例化 MessageIpc
|
|
149
|
-
const ipc = new MessageIpc(child); // TypeError: Cannot construct abstract class
|
|
150
|
-
|
|
151
|
-
// ✅ 继承并实现 exec,自行暴露 _send
|
|
152
|
-
class MyIpc extends MessageIpc {
|
|
153
|
-
protected async exec(data: any) { return data; }
|
|
154
|
-
public request<T = any>(data: T, timeout?: number) { return this._send(data, timeout); }
|
|
155
|
-
}
|
|
156
|
-
const ipc = new MyIpc(child);
|
|
157
|
-
|
|
158
|
-
// ❌ 不要用 spawn
|
|
159
|
-
const child = spawn('node', ['worker.js']);
|
|
160
|
-
// ✅ 用 fork
|
|
161
|
-
const child = fork('./worker.js');
|
|
162
|
-
|
|
163
|
-
// ❌ 不要忘记 dispose
|
|
164
|
-
// ✅ 用完清理
|
|
165
|
-
ipc.dispose();
|
|
166
|
-
child.kill();
|
|
167
|
-
```
|