@hile/message-ws 2.0.4 → 3.0.0

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/AI.md ADDED
@@ -0,0 +1,295 @@
1
+ # AI Guide For @hile/message-ws
2
+
3
+
4
+
5
+ <!-- Generated by scripts/build-ai-context.mjs from docs/ai. Do not edit by hand. -->
6
+
7
+
8
+
9
+ Purpose: Use MessageModem over WebSocket connections.
10
+
11
+
12
+
13
+ Use this file when an AI agent installs the npm package and needs package-local examples, package selection rules, boundaries, and verification steps.
14
+
15
+
16
+
17
+ ## Package Selection
18
+
19
+
20
+
21
+ Use `@hile/message-ws` for: Use MessageModem over WebSocket connections.
22
+
23
+
24
+
25
+ # Messaging And Microservices
26
+
27
+ Packages: `@hile/message-modem`, `@hile/message-ws`, `@hile/message-ipc`, `@hile/message-worker-thread`, `@hile/message-loader`, `@hile/micro`, `@hile/micro-dynamic-configs`.
28
+
29
+ ## Copy-Paste Example
30
+
31
+ Message handler file:
32
+
33
+ ```ts
34
+ // src/messages/ping.msg.ts
35
+ import { defineMessage } from '@hile/message-loader'
36
+
37
+ export default defineMessage(async ({ data, params }) => {
38
+ return {
39
+ type: 'pong',
40
+ data,
41
+ params,
42
+ timestamp: Date.now(),
43
+ }
44
+ })
45
+ ```
46
+
47
+ Microservice boot file:
48
+
49
+ ```ts
50
+ // src/services/app.boot.ts
51
+ import { defineService } from '@hile/core'
52
+ import { Application } from '@hile/micro'
53
+
54
+ export default defineService('micro.app', async (shutdown) => {
55
+ const app = new Application({
56
+ namespace: process.env.MICRO_NAMESPACE ?? 'example.service',
57
+ registry: {
58
+ host: process.env.REGISTRY_HOST ?? '127.0.0.1',
59
+ port: Number(process.env.REGISTRY_PORT ?? 9876),
60
+ },
61
+ advertiseHost: process.env.HILE_ADVERTISE_HOST ?? '127.0.0.1',
62
+ })
63
+
64
+ await app.load(new URL('../messages', import.meta.url).pathname)
65
+ const stop = await app.listen(Number(process.env.MICRO_PORT ?? 0))
66
+ shutdown(stop)
67
+ return app
68
+ })
69
+ ```
70
+
71
+ Caller:
72
+
73
+ ```ts
74
+ const result = await app.call('example.service', '/ping', { hello: 'world' })
75
+ ```
76
+
77
+ ## More Examples
78
+
79
+ Streaming handler:
80
+
81
+ ```ts
82
+ // src/messages/events.msg.ts
83
+ import { defineMessage } from '@hile/message-loader'
84
+
85
+ export default defineMessage(async function* () {
86
+ for (let i = 0; i < 3; i++) {
87
+ yield { seq: i }
88
+ }
89
+ })
90
+ ```
91
+
92
+ Streaming caller:
93
+
94
+ ```ts
95
+ const stream = await app.stream('example.service', '/events', {})
96
+ for await (const chunk of stream) {
97
+ console.log(chunk)
98
+ }
99
+ ```
100
+
101
+ Custom WebSocket modem:
102
+
103
+ ```ts
104
+ import { MessageWs } from '@hile/message-ws'
105
+ import type WebSocket from 'ws'
106
+
107
+ class RpcWs extends MessageWs {
108
+ constructor(ws: WebSocket, private readonly dispatch: (url: string, data: unknown) => Promise<unknown>) {
109
+ super(ws)
110
+ }
111
+
112
+ protected exec(data: { url: string; data: unknown }) {
113
+ return this.dispatch(data.url, data.data)
114
+ }
115
+
116
+ request<T>(url: string, data: unknown, timeout = 30_000) {
117
+ return this._send<T>({ url, data }, { timeout })
118
+ }
119
+ }
120
+ ```
121
+
122
+ Notice that `request()` returns a `Promise<T>`. Await it directly.
123
+
124
+ ## Use When
125
+
126
+ Use the message packages for request/response messaging over WebSocket, process IPC, worker threads, file-system message handlers, service discovery, streaming RPC, and registry-backed pub/sub.
127
+
128
+ ## Do Not Use When
129
+
130
+ - Do not use `stream()` for normal single-result calls.
131
+ - Do not rely on message IDs for business idempotency. They are transport IDs.
132
+ - Do not bypass `defineMessage()` for file-loaded handlers.
133
+
134
+ ## Install
135
+
136
+ ```bash
137
+ pnpm add @hile/micro @hile/message-loader @hile/message-ws
138
+ ```
139
+
140
+ Use transport-specific packages only when you need to build custom IPC or worker-thread bridges.
141
+
142
+ ## Imports
143
+
144
+ ```ts
145
+ import { defineMessage, MessageLoader } from '@hile/message-loader'
146
+ import { Application, Registry, Server } from '@hile/micro'
147
+ import { MessageWs } from '@hile/message-ws'
148
+ import { MessageIpc } from '@hile/message-ipc'
149
+ import { MessageWorkerThread } from '@hile/message-worker-thread'
150
+ ```
151
+
152
+ ## Compose With
153
+
154
+ - `@hile/context` propagates context in micro message metadata.
155
+ - `@hile/redis-idempotency` protects retryable side effects in message handlers.
156
+ - `@hile/redis-stream-queue` is better for durable background jobs.
157
+
158
+ ## Runtime And Lifecycle Notes
159
+
160
+ - `MessageLoader` maps `*.msg.*` files to routes using `@hile/loader`.
161
+ - `MessageLoader.dispatch(path, data, extras?)` invokes the matched handler.
162
+ - `MessageModem._send()` returns a `Promise`.
163
+ - `MessageModem._stream()` returns a Node `Readable` in object mode.
164
+ - A stream request requires `exec()` to return an async iterable.
165
+ - `Application.call(namespace, url, data, options?)` returns a promise.
166
+ - `Application.stream(namespace, url, data, options?)` returns a readable stream.
167
+ - `Application.publish(topic, payload)` returns an object with `update()` and `unpublish()`.
168
+ - `Application.subscribe(topic, callback)` returns an unsubscribe function.
169
+ - `Registry` stores service addresses and retained config/topic state under `~/.registry`.
170
+
171
+ ## Anti-Patterns
172
+
173
+ - Appending a secondary response getter to `client.request('/x', data)`
174
+ - Returning a plain object from a handler called through `stream()`.
175
+ - Using pub/sub as a durable queue.
176
+ - Forgetting to register `shutdown(await app.listen(...))`.
177
+
178
+ ## Verification Checklist
179
+
180
+ - Message files default-export `defineMessage(...)`.
181
+ - RPC callers use `await app.call(...)`.
182
+ - Streaming handlers are async generators.
183
+ - Registry is started before application nodes need discovery.
184
+ - Micro apps use stable namespaces and advertise reachable hosts.
185
+
186
+
187
+
188
+ # Related Recipes
189
+
190
+
191
+
192
+ # Micro RPC With Message Loader
193
+
194
+ ## Complete Example
195
+
196
+ Provider handler:
197
+
198
+ ```ts
199
+ // src/messages/charge.msg.ts
200
+ import { defineMessage } from '@hile/message-loader'
201
+
202
+ export default defineMessage(async ({ data }) => {
203
+ return { charged: true, input: data }
204
+ })
205
+ ```
206
+
207
+ Provider boot:
208
+
209
+ ```ts
210
+ // src/services/app.boot.ts
211
+ import { defineService } from '@hile/core'
212
+ import { Application } from '@hile/micro'
213
+
214
+ export default defineService('billing.micro', async (shutdown) => {
215
+ const app = new Application({
216
+ namespace: 'billing',
217
+ registry: { host: '127.0.0.1', port: 9876 },
218
+ advertiseHost: '127.0.0.1',
219
+ })
220
+
221
+ await app.load(new URL('../messages', import.meta.url).pathname)
222
+ const stop = await app.listen(9101)
223
+ shutdown(stop)
224
+ return app
225
+ })
226
+ ```
227
+
228
+ Consumer:
229
+
230
+ ```ts
231
+ const result = await app.call('billing', '/charge', {
232
+ tenantId: 't1',
233
+ amount: 100,
234
+ })
235
+ ```
236
+
237
+ ## File Layout
238
+
239
+ ```text
240
+ provider/
241
+ src/messages/charge.msg.ts
242
+ src/services/app.boot.ts
243
+ consumer/
244
+ src/models/payments/pay.model.ts
245
+ ```
246
+
247
+ ## User Intent
248
+
249
+ Use this recipe when services communicate over Hile registry-backed RPC.
250
+
251
+ ## Packages To Use
252
+
253
+ - `@hile/micro`
254
+ - `@hile/message-loader`
255
+ - `@hile/context` when context must cross service boundaries
256
+ - `@hile/redis-idempotency` for retryable side effects
257
+
258
+ ## Implementation Steps
259
+
260
+ 1. Start a Registry with `hile registry`.
261
+ 2. Start providers with stable namespaces.
262
+ 3. Load `*.msg.ts` handlers through `app.load()`.
263
+ 4. Call providers with `await app.call(namespace, url, data)`.
264
+ 5. Use `app.stream()` only for async-generator handlers.
265
+
266
+ ## Failure And Cleanup Behavior
267
+
268
+ - `Application.call()` may retry; side-effecting handlers need idempotency.
269
+ - Registry disconnect triggers reconnect; apps re-declare topics and subscriptions.
270
+ - Circuit breaker excludes failing nodes for cooldown.
271
+
272
+ ## Verification Checklist
273
+
274
+ - Registry is reachable.
275
+ - Provider namespace matches consumer call.
276
+ - Handlers default-export `defineMessage()`.
277
+ - Consumer code awaits `app.call(...)` directly.
278
+
279
+
280
+
281
+ # Global Guardrails
282
+
283
+
284
+
285
+ ## Never Generate These Patterns
286
+
287
+ - Do not call `loadService()` at module top level; it starts resources during import.
288
+ - Do not default-export plain functions from `*.boot.*` files; `hile start` expects a Hile service.
289
+ - Do not set `ctx.body` and also return a controller value.
290
+ - Do not assume `@hile/http` Zod validation mutates or coerces `ctx.query`, `ctx.params`, or `ctx.request.body`.
291
+ - Do not put reusable business logic only in controllers, pages, queue workers, or message handlers.
292
+ - Do not use old message examples that append a secondary response getter; current request APIs return promises directly.
293
+ - Do not claim exactly-once delivery or execution from Redis locks, queues, idempotency, or rate limits.
294
+ - Do not use queue `jobId` as the only side-effect idempotency boundary.
295
+ - Do not log the entire async context by default.
package/README.md CHANGED
@@ -1,128 +1,90 @@
1
1
  # @hile/message-ws
2
2
 
3
- 基于 `@hile/message-modem` `ws` 模块的 WebSocket 通信抽象实现。让客户端与服务端之间的请求/响应通信像调用函数一样简单。
3
+ <!-- Generated by scripts/build-ai-context.mjs from docs/ai. Do not edit by hand. -->
4
4
 
5
- ## 安装
5
+ Use MessageModem over WebSocket connections.
6
6
 
7
- ```bash
8
- pnpm add @hile/message-ws
9
- ```
7
+ This README is intentionally short and example-first. The complete AI-facing guide ships in `AI.md` in this package.
10
8
 
11
- ## 核心特性
12
-
13
- - **双端支持** — 客户端和服务端各自包装 `WebSocket` 实例即可
14
- - **继承模式** — 继承 `MessageWs` 并实现 `exec` 方法
15
- - **JSON 传输** — 消息自动 JSON 序列化/反序列化
16
- - **请求/响应** — 继承 `MessageModem` 全部能力
17
- - **超时控制** — 默认 30 秒,可按请求自定义
18
- - **主动中止** — `abort()` 取消等待并通知对端
19
- - **错误传播** — `Exception` 带 status 透传,普通 Error 映射为 500
20
- - **连接状态检查** — 发送前检查 `readyState`
21
- - **资源清理** — `dispose()` 移除监听
22
-
23
- ## 快速开始
24
-
25
- ### 第一步:定义子类
26
-
27
- ```typescript
28
- import { MessageWs } from '@hile/message-ws';
29
- import { Exception } from '@hile/message-modem';
30
-
31
- class AppWs extends MessageWs {
32
- protected async exec(data: any): Promise<any> {
33
- switch (data?.action) {
34
- case 'getUser':
35
- return { id: data.id, name: 'Alice' };
36
- case 'restricted':
37
- throw new Exception(403, 'not allowed');
38
- default:
39
- return data;
40
- }
41
- }
9
+ ## When To Use
42
10
 
43
- public request<T = any>(data: T, timeout?: number) {
44
- return this._send(data, timeout);
45
- }
46
- }
47
- ```
48
-
49
- ### 第二步:服务端
11
+ Use the message packages for request/response messaging over WebSocket, process IPC, worker threads, file-system message handlers, service discovery, streaming RPC, and registry-backed pub/sub.
50
12
 
51
- ```typescript
52
- import { WebSocketServer } from 'ws';
13
+ ## Install
53
14
 
54
- const wss = new WebSocketServer({ port: 8080 });
55
- wss.on('connection', (ws) => {
56
- const modem = new AppWs(ws);
57
- // 自动处理客户端请求
58
- });
15
+ ```bash
16
+ pnpm add @hile/message-ws
59
17
  ```
60
18
 
61
- ### 第三步:客户端
19
+ ## Copy-Paste Example
62
20
 
63
- ```typescript
64
- import WebSocket from 'ws';
21
+ Message handler file:
65
22
 
66
- const ws = new WebSocket('ws://localhost:8080');
67
- ws.on('open', () => {
68
- const modem = new AppWs(ws);
23
+ ```ts
24
+ // src/messages/ping.msg.ts
25
+ import { defineMessage } from '@hile/message-loader'
69
26
 
70
- const user = await modem.request({ action: 'getUser', id: 1 }).response();
71
- console.log(user); // { id: 1, name: 'Alice' }
72
-
73
- modem.dispose();
74
- ws.close();
75
- });
27
+ export default defineMessage(async ({ data, params }) => {
28
+ return {
29
+ type: 'pong',
30
+ data,
31
+ params,
32
+ timestamp: Date.now(),
33
+ }
34
+ })
76
35
  ```
77
36
 
78
- ## API
79
-
80
- ### `MessageWs`(抽象类)
81
-
82
- | 方法 | 签名 | 说明 |
83
- |------|------|------|
84
- | `constructor` | `new SubClass(ws: WebSocket)` | 传入已连接的 WebSocket 实例 |
85
- | `exec` | `protected abstract exec(data: any): Promise<any>` | 子类实现:处理对端请求 |
86
- | `_send` | `protected _send<T>(data: T, timeout?: number)` | 发送双向请求(`twoway: true`),返回 `{ abort, response }`。子类自行暴露为 public |
87
- | `_push` | `protected _push<T>(data: T, timeout?: number)` | 发送单向推送(`twoway: false`),接收方不回复 RESPONSE |
88
- | `dispose` | `dispose(): void` | 移除消息监听,释放资源 |
89
-
90
- ### `_send` 返回值
37
+ Microservice boot file:
38
+
39
+ ```ts
40
+ // src/services/app.boot.ts
41
+ import { defineService } from '@hile/core'
42
+ import { Application } from '@hile/micro'
43
+
44
+ export default defineService('micro.app', async (shutdown) => {
45
+ const app = new Application({
46
+ namespace: process.env.MICRO_NAMESPACE ?? 'example.service',
47
+ registry: {
48
+ host: process.env.REGISTRY_HOST ?? '127.0.0.1',
49
+ port: Number(process.env.REGISTRY_PORT ?? 9876),
50
+ },
51
+ advertiseHost: process.env.HILE_ADVERTISE_HOST ?? '127.0.0.1',
52
+ })
53
+
54
+ await app.load(new URL('../messages', import.meta.url).pathname)
55
+ const stop = await app.listen(Number(process.env.MICRO_PORT ?? 0))
56
+ shutdown(stop)
57
+ return app
58
+ })
59
+ ```
91
60
 
92
- | 属性 | 类型 | 说明 |
93
- |------|------|------|
94
- | `abort` | `() => void` | 中止请求 |
95
- | `response` | `<U>() => Promise<U>` | 等待对端响应 |
61
+ Caller:
96
62
 
97
- ## 超时与中止
63
+ ```ts
64
+ const result = await app.call('example.service', '/ping', { hello: 'world' })
65
+ ```
98
66
 
99
- ```typescript
100
- import { AbortException, Exception } from '@hile/message-modem';
67
+ ## Boundaries
101
68
 
102
- // 5 秒超时
103
- const result = await modem.request(data, 5000).response();
69
+ - Do not use `stream()` for normal single-result calls.
70
+ - Do not rely on message IDs for business idempotency. They are transport IDs.
71
+ - Do not bypass `defineMessage()` for file-loaded handlers.
104
72
 
105
- // 主动中止
106
- const req = modem.request(data);
107
- setTimeout(() => req.abort(), 3000);
108
- try {
109
- await req.response();
110
- } catch (e) {
111
- if (e instanceof AbortException) {
112
- console.log('请求被中止或超时');
113
- } else if (e instanceof Exception) {
114
- console.log(`远端错误 [${e.status}]: ${e.message}`);
115
- }
116
- }
117
- ```
73
+ - Appending a secondary response getter to `client.request('/x', data)`
74
+ - Returning a plain object from a handler called through `stream()`.
75
+ - Using pub/sub as a durable queue.
76
+ - Forgetting to register `shutdown(await app.listen(...))`.
118
77
 
119
- ## 注意事项
78
+ ## Verify
120
79
 
121
- - `MessageWs` **抽象类**,不能直接实例化
122
- - 必须在 WebSocket 连接建立(`open` 事件)后再创建实例或发送请求
123
- - 消息通过 JSON 序列化传输,不支持 Buffer/Map/Set 等非 JSON 类型
124
- - 使用完毕后调用 `dispose()` + `ws.close()`
80
+ - Message files default-export `defineMessage(...)`.
81
+ - RPC callers use `await app.call(...)`.
82
+ - Streaming handlers are async generators.
83
+ - Registry is started before application nodes need discovery.
84
+ - Micro apps use stable namespaces and advertise reachable hosts.
125
85
 
126
- ## License
86
+ ## More Context
127
87
 
128
- MIT
88
+ - `AI.md` in this package: full package-local AI guide.
89
+ - Root `llms-full.txt`: full monorepo AI context.
90
+ - Root `references/`: source files copied from `docs/ai`.
package/dist/index.d.ts CHANGED
@@ -15,9 +15,10 @@ import type WebSocket from 'ws';
15
15
  * }
16
16
  *
17
17
  * const ws = new WebSocket('ws://localhost:8080');
18
- * ws.on('open', () => {
18
+ * ws.on('open', async () => {
19
19
  * const modem = new MyWs(ws);
20
- * modem.request('hello').response().then(console.log);
20
+ * const result = await modem.request('hello');
21
+ * console.log(result);
21
22
  * });
22
23
  */
23
24
  export declare abstract class MessageWs extends MessageModem {
package/dist/index.js CHANGED
@@ -14,9 +14,10 @@ import { MessageModem } from '@hile/message-modem';
14
14
  * }
15
15
  *
16
16
  * const ws = new WebSocket('ws://localhost:8080');
17
- * ws.on('open', () => {
17
+ * ws.on('open', async () => {
18
18
  * const modem = new MyWs(ws);
19
- * modem.request('hello').response().then(console.log);
19
+ * const result = await modem.request('hello');
20
+ * console.log(result);
20
21
  * });
21
22
  */
22
23
  export class MessageWs extends MessageModem {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/message-ws",
3
- "version": "2.0.4",
3
+ "version": "3.0.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -11,7 +11,7 @@
11
11
  "files": [
12
12
  "dist",
13
13
  "README.md",
14
- "SKILL.md"
14
+ "AI.md"
15
15
  ],
16
16
  "license": "MIT",
17
17
  "publishConfig": {
@@ -23,8 +23,8 @@
23
23
  "vitest": "^4.0.18"
24
24
  },
25
25
  "dependencies": {
26
- "@hile/message-modem": "^2.0.3",
26
+ "@hile/message-modem": "^3.0.0",
27
27
  "ws": "^8.21.0"
28
28
  },
29
- "gitHead": "a7615000fcb87e6bc0e573af760c814ec935bab2"
29
+ "gitHead": "0985b6f8abc1f4de0a36324063585fdc3ac1375b"
30
30
  }