@hile/message-ws 2.0.0 → 2.0.1

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 +3 -3
  2. package/SKILL.md +0 -145
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/message-ws",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -23,8 +23,8 @@
23
23
  "vitest": "^4.0.18"
24
24
  },
25
25
  "dependencies": {
26
- "@hile/message-modem": "^2.0.0",
26
+ "@hile/message-modem": "^2.0.1",
27
27
  "ws": "^8.19.0"
28
28
  },
29
- "gitHead": "d28b20bc22ee08c45cbf4596672705cb96e8e461"
29
+ "gitHead": "8e0fd1f78b5a8abd21218d1f596ada2533a0c8e7"
30
30
  }
package/SKILL.md DELETED
@@ -1,145 +0,0 @@
1
- ---
2
- name: message-ws
3
- description: Code generation and contribution rules for @hile/message-ws. Use when editing this package or when the user asks about @hile/message-ws patterns or API.
4
- ---
5
-
6
- # @hile/message-ws
7
-
8
- 本文档是面向 AI 编码模型和人类开发者的 **代码生成规范**,阅读后应能正确地使用本库编写符合架构规则的代码。
9
-
10
- ---
11
-
12
- ## 1. 架构总览
13
-
14
- `@hile/message-ws` 是 `@hile/message-modem` 的 **WebSocket(ws 模块)抽象实现**,用于客户端与服务端之间的请求/响应通信。
15
-
16
- `MessageWs` 本身是 **抽象类**,实现了 `post`(通过 `ws.send` 发送 JSON),`exec` 方法留给子类实现。
17
-
18
- 继承链:
19
-
20
- ```
21
- MessageModem (abstract) ← post + exec 均抽象
22
- └── MessageWs (abstract) ← 实现 post(JSON + ws.send),exec 仍抽象
23
- └── 用户子类 ← 实现 exec
24
- ```
25
-
26
- 消息通过 JSON 序列化/反序列化传输。
27
-
28
- ---
29
-
30
- ## 2. 类型签名
31
-
32
- ```typescript
33
- import { MessageModem, type MessageTransferFormat } from '@hile/message-modem';
34
- import type WebSocket from 'ws';
35
-
36
- abstract class MessageWs extends MessageModem {
37
- constructor(ws: WebSocket);
38
-
39
- protected abstract exec(data: any): Promise<any>;
40
-
41
- // 发送双向请求(protected,子类自行暴露)
42
- protected _send<T = any>(data: T, timeout?: number): {
43
- abort: () => void;
44
- response: <U = any>() => Promise<U>;
45
- };
46
-
47
- // 发送单向推送(protected,子类自行暴露)
48
- protected _push<T = any>(data: T, timeout?: number): {
49
- abort: () => void;
50
- response: <U = any>() => Promise<U>;
51
- };
52
-
53
- public dispose(): void;
54
- }
55
- ```
56
-
57
- ---
58
-
59
- ## 3. 代码生成模板与规则
60
-
61
- ### 3.1 基本子类模板
62
-
63
- ```typescript
64
- import { MessageWs } from '@hile/message-ws';
65
-
66
- class MyWs extends MessageWs {
67
- protected async exec(data: any): Promise<any> {
68
- switch (data?.action) {
69
- case 'ping': return 'pong';
70
- default: return data;
71
- }
72
- }
73
-
74
- public request<T = any>(data: T, timeout?: number) {
75
- return this._send(data, timeout);
76
- }
77
- }
78
- ```
79
-
80
- ### 3.2 客户端模板
81
-
82
- ```typescript
83
- import WebSocket from 'ws';
84
-
85
- const ws = new WebSocket('ws://localhost:8080');
86
- ws.on('open', () => {
87
- const modem = new MyWs(ws);
88
-
89
- modem.request({ action: 'getUser', id: 1 })
90
- .response<User>()
91
- .then(console.log);
92
- });
93
- ```
94
-
95
- ### 3.3 服务端模板
96
-
97
- ```typescript
98
- import { WebSocketServer } from 'ws';
99
-
100
- const wss = new WebSocketServer({ port: 8080 });
101
- wss.on('connection', (ws) => {
102
- const modem = new MyWs(ws);
103
- // modem 自动监听 ws 消息并处理请求
104
- });
105
- ```
106
-
107
- ### 3.4 强制规则
108
-
109
- | 规则 | 说明 |
110
- |------|------|
111
- | **必须继承并实现 `exec`** | `MessageWs` 是抽象类 |
112
- | **必须传入已连接的 `WebSocket`** | 或在 `open` 事件后创建 |
113
- | **消息通过 JSON 传输** | `post` 内部 `JSON.stringify`,收到消息 `JSON.parse` |
114
- | **`readyState !== OPEN` 时 `post` 会抛错** | 确保连接已建立 |
115
- | **用完必须 `dispose()`** | 避免内存泄漏 |
116
-
117
- ### 3.5 反模式
118
-
119
- ```typescript
120
- // ❌ 连接未建立就创建 modem 并发送
121
- const ws = new WebSocket('ws://...');
122
- const modem = new MyWs(ws);
123
- modem.request('hi'); // readyState 不是 OPEN → 抛错
124
-
125
- // ✅ 等待 open 事件
126
- ws.on('open', () => {
127
- const modem = new MyWs(ws);
128
- modem.request('hi');
129
- });
130
-
131
- // ❌ 不能直接实例化
132
- const modem = new MessageWs(ws); // abstract
133
-
134
- // ✅ 继承并实现 exec,自行暴露 _send
135
- class MyWs extends MessageWs {
136
- protected async exec(data: any) { return data; }
137
- public request<T = any>(data: T, timeout?: number) { return this._send(data, timeout); }
138
- }
139
-
140
- // ❌ 传输非 JSON 安全的数据
141
- modem.request(new Map()); // Map 序列化会丢失
142
-
143
- // ✅ 只传 JSON 安全数据
144
- modem.request({ key: 'value' });
145
- ```