message-nexus 1.0.0 → 1.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.
- package/README.md +77 -55
- package/dist/index.cjs +5 -5
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,21 +28,24 @@ pnpm add message-nexus
|
|
|
28
28
|
### 1. 进程内通信(Mitt)
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
|
-
import
|
|
32
|
-
|
|
31
|
+
import MessageNexus, { MittDriver, createEmitter } from 'message-nexus'
|
|
32
|
+
|
|
33
|
+
// 共享的 emitter
|
|
34
|
+
const emitter = createEmitter()
|
|
33
35
|
|
|
34
|
-
const emitter = mitt()
|
|
35
36
|
const driver = new MittDriver(emitter)
|
|
36
|
-
const
|
|
37
|
+
const nexus = new MessageNexus(driver)
|
|
37
38
|
|
|
38
39
|
// 发送请求
|
|
39
|
-
const response = await
|
|
40
|
+
const response = await nexus.request('GET_DATA', { id: 123 })
|
|
40
41
|
console.log(response)
|
|
41
42
|
|
|
42
43
|
// 监听命令
|
|
43
|
-
const
|
|
44
|
+
const receiverDriver = new MittDriver(emitter)
|
|
45
|
+
const receiverNexus = new MessageNexus(receiverDriver)
|
|
46
|
+
const unsubscribe = receiverNexus.onCommand((data) => {
|
|
44
47
|
if (data.type === 'GET_DATA') {
|
|
45
|
-
|
|
48
|
+
receiverNexus.reply(data.id, { name: 'test', value: 42 })
|
|
46
49
|
}
|
|
47
50
|
})
|
|
48
51
|
```
|
|
@@ -50,22 +53,22 @@ const unsubscribe = bridge.onCommand((data) => {
|
|
|
50
53
|
### 2. iframe/Window 通信(PostMessage)
|
|
51
54
|
|
|
52
55
|
```typescript
|
|
53
|
-
import { PostMessageDriver
|
|
56
|
+
import MessageNexus, { PostMessageDriver } from 'message-nexus'
|
|
54
57
|
|
|
55
58
|
// 发送方
|
|
56
59
|
const driver = new PostMessageDriver(window.parent, 'https://example.com')
|
|
57
|
-
const
|
|
60
|
+
const nexus = new MessageNexus(driver)
|
|
58
61
|
|
|
59
|
-
const response = await
|
|
62
|
+
const response = await nexus.request('PING')
|
|
60
63
|
console.log('Pong:', response)
|
|
61
64
|
|
|
62
65
|
// 接收方
|
|
63
66
|
const iframeDriver = new PostMessageDriver(iframe.contentWindow, 'https://example.com')
|
|
64
|
-
const
|
|
67
|
+
const iframeNexus = new MessageNexus(iframeDriver)
|
|
65
68
|
|
|
66
|
-
|
|
69
|
+
iframeNexus.onCommand((data) => {
|
|
67
70
|
if (data.type === 'PING') {
|
|
68
|
-
|
|
71
|
+
iframeNexus.reply(data.id, { time: Date.now() })
|
|
69
72
|
}
|
|
70
73
|
})
|
|
71
74
|
```
|
|
@@ -73,32 +76,37 @@ iframeBridge.onCommand((data) => {
|
|
|
73
76
|
### 3. 跨标签页通信(BroadcastChannel)
|
|
74
77
|
|
|
75
78
|
```typescript
|
|
76
|
-
import { BroadcastDriver
|
|
79
|
+
import MessageNexus, { BroadcastDriver } from 'message-nexus'
|
|
77
80
|
|
|
78
81
|
// 创建 BroadcastDriver,指定频道名称
|
|
79
82
|
const driver = new BroadcastDriver({ channel: 'my-app-channel' })
|
|
80
|
-
const
|
|
83
|
+
const nexus = new MessageNexus(driver)
|
|
81
84
|
|
|
82
85
|
// 监听命令
|
|
83
|
-
|
|
86
|
+
nexus.onCommand((data) => {
|
|
84
87
|
console.log('Received:', data.type, data.payload)
|
|
85
|
-
|
|
88
|
+
nexus.reply(data.id, { result: 'success' })
|
|
86
89
|
})
|
|
87
90
|
|
|
88
91
|
// 发送请求(会在所有同频道的标签页中广播)
|
|
89
|
-
const response = await
|
|
92
|
+
const response = await nexus.request({
|
|
90
93
|
type: 'SYNC_STATE',
|
|
91
94
|
payload: { state: '...' },
|
|
92
95
|
})
|
|
93
96
|
|
|
94
|
-
//
|
|
95
|
-
|
|
97
|
+
// 接收方
|
|
98
|
+
const receiverDriver = new BroadcastDriver({ channel: 'my-app-channel' })
|
|
99
|
+
const receiverNexus = new MessageNexus(receiverDriver)
|
|
100
|
+
receiverNexus.onCommand((data) => {
|
|
101
|
+
console.log('Received:', data.type, data.payload)
|
|
102
|
+
receiverNexus.reply(data.id, { result: 'success' })
|
|
103
|
+
})
|
|
96
104
|
```
|
|
97
105
|
|
|
98
106
|
### 4. WebSocket 通信
|
|
99
107
|
|
|
100
108
|
```typescript
|
|
101
|
-
import { WebSocketDriver
|
|
109
|
+
import MessageNexus, { WebSocketDriver } from 'message-nexus'
|
|
102
110
|
|
|
103
111
|
// 自动重连配置
|
|
104
112
|
const driver = new WebSocketDriver({
|
|
@@ -109,28 +117,42 @@ const driver = new WebSocketDriver({
|
|
|
109
117
|
},
|
|
110
118
|
})
|
|
111
119
|
|
|
112
|
-
const
|
|
120
|
+
const nexus = new MessageNexus(driver)
|
|
113
121
|
|
|
114
122
|
// 发送请求
|
|
115
|
-
const response = await
|
|
123
|
+
const response = await nexus.request({
|
|
116
124
|
type: 'GET_USER',
|
|
117
125
|
payload: { userId: 123 },
|
|
118
126
|
timeout: 5000,
|
|
119
127
|
retryCount: 3, // 失败重试 3 次
|
|
120
128
|
retryDelay: 1000, // 重试延迟
|
|
121
129
|
})
|
|
130
|
+
|
|
131
|
+
// 接收方
|
|
132
|
+
const receiverDriver = new WebSocketDriver({
|
|
133
|
+
url: 'wss://api.example.com/ws',
|
|
134
|
+
reconnect: {
|
|
135
|
+
maxRetries: 5, // 最大重试次数
|
|
136
|
+
retryInterval: 3000, // 重试间隔(毫秒)
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
const receiverNexus = new MessageNexus(receiverDriver)
|
|
140
|
+
receiverNexus.onCommand((data) => {
|
|
141
|
+
console.log('Received:', data.type, data.payload)
|
|
142
|
+
receiverNexus.reply(data.id, { result: 'success' })
|
|
143
|
+
})
|
|
122
144
|
```
|
|
123
145
|
|
|
124
146
|
## API 文档
|
|
125
147
|
|
|
126
|
-
###
|
|
148
|
+
### MessageNexus
|
|
127
149
|
|
|
128
150
|
#### 构造函数
|
|
129
151
|
|
|
130
152
|
```typescript
|
|
131
|
-
new
|
|
153
|
+
new MessageNexus<RequestPayload, ResponsePayload>(
|
|
132
154
|
driver: BaseDriver,
|
|
133
|
-
options?:
|
|
155
|
+
options?: MessageNexusOptions
|
|
134
156
|
)
|
|
135
157
|
```
|
|
136
158
|
|
|
@@ -149,7 +171,7 @@ new MessageBridge<RequestPayload, ResponsePayload>(
|
|
|
149
171
|
发送请求并等待响应。
|
|
150
172
|
|
|
151
173
|
```typescript
|
|
152
|
-
|
|
174
|
+
nexus.request(typeOrOptions: string | RequestOptions): Promise<ResponsePayload>
|
|
153
175
|
```
|
|
154
176
|
|
|
155
177
|
**Options:**
|
|
@@ -168,10 +190,10 @@ bridge.request(typeOrOptions: string | RequestOptions): Promise<ResponsePayload>
|
|
|
168
190
|
|
|
169
191
|
```typescript
|
|
170
192
|
// 简单请求
|
|
171
|
-
const result = await
|
|
193
|
+
const result = await nexus.request('FETCH_DATA')
|
|
172
194
|
|
|
173
195
|
// 完整配置
|
|
174
|
-
const result = await
|
|
196
|
+
const result = await nexus.request({
|
|
175
197
|
type: 'FETCH_DATA',
|
|
176
198
|
payload: { id: 123 },
|
|
177
199
|
to: 'target-instance',
|
|
@@ -186,7 +208,7 @@ const result = await bridge.request({
|
|
|
186
208
|
注册消息处理器。
|
|
187
209
|
|
|
188
210
|
```typescript
|
|
189
|
-
|
|
211
|
+
nexus.onCommand(handler: (data: CommandMessage) => void): () => void
|
|
190
212
|
```
|
|
191
213
|
|
|
192
214
|
**返回值:** 取消监听的函数
|
|
@@ -194,11 +216,11 @@ bridge.onCommand(handler: (data: CommandMessage) => void): () => void
|
|
|
194
216
|
**示例:**
|
|
195
217
|
|
|
196
218
|
```typescript
|
|
197
|
-
const unsubscribe =
|
|
219
|
+
const unsubscribe = nexus.onCommand((data) => {
|
|
198
220
|
console.log('Received:', data.type, data.payload)
|
|
199
221
|
|
|
200
222
|
if (data.type === 'ECHO') {
|
|
201
|
-
|
|
223
|
+
nexus.reply(data.id, { echoed: data.payload })
|
|
202
224
|
}
|
|
203
225
|
})
|
|
204
226
|
|
|
@@ -211,14 +233,14 @@ unsubscribe()
|
|
|
211
233
|
回复传入消息。
|
|
212
234
|
|
|
213
235
|
```typescript
|
|
214
|
-
|
|
236
|
+
nexus.reply(messageId: string, payload: unknown, error?: unknown)
|
|
215
237
|
```
|
|
216
238
|
|
|
217
239
|
**示例:**
|
|
218
240
|
|
|
219
241
|
```typescript
|
|
220
|
-
|
|
221
|
-
|
|
242
|
+
nexus.reply('message-id-123', { success: true })
|
|
243
|
+
nexus.reply('message-id-456', null, new Error('Invalid request'))
|
|
222
244
|
```
|
|
223
245
|
|
|
224
246
|
##### onError()
|
|
@@ -226,13 +248,13 @@ bridge.reply('message-id-456', null, new Error('Invalid request'))
|
|
|
226
248
|
注册错误处理器。
|
|
227
249
|
|
|
228
250
|
```typescript
|
|
229
|
-
|
|
251
|
+
nexus.onError(handler: ErrorHandler): () => void
|
|
230
252
|
```
|
|
231
253
|
|
|
232
254
|
**示例:**
|
|
233
255
|
|
|
234
256
|
```typescript
|
|
235
|
-
|
|
257
|
+
nexus.onError((error, context) => {
|
|
236
258
|
console.error('Bridge error:', error.message, context)
|
|
237
259
|
// 发送到错误追踪服务
|
|
238
260
|
Sentry.captureException(error, { extra: context })
|
|
@@ -244,7 +266,7 @@ bridge.onError((error, context) => {
|
|
|
244
266
|
获取监控指标。
|
|
245
267
|
|
|
246
268
|
```typescript
|
|
247
|
-
|
|
269
|
+
nexus.getMetrics(): Metrics
|
|
248
270
|
```
|
|
249
271
|
|
|
250
272
|
**返回值:**
|
|
@@ -264,7 +286,7 @@ bridge.getMetrics(): Metrics
|
|
|
264
286
|
**示例:**
|
|
265
287
|
|
|
266
288
|
```typescript
|
|
267
|
-
const metrics =
|
|
289
|
+
const metrics = nexus.getMetrics()
|
|
268
290
|
console.log(`Avg latency: ${metrics.averageLatency}ms`)
|
|
269
291
|
console.log(
|
|
270
292
|
`Success rate: ${((metrics.messagesReceived / metrics.messagesSent) * 100).toFixed(2)}%`,
|
|
@@ -276,13 +298,13 @@ console.log(
|
|
|
276
298
|
注册指标变更回调。
|
|
277
299
|
|
|
278
300
|
```typescript
|
|
279
|
-
|
|
301
|
+
nexus.onMetrics(callback: MetricsCallback): () => void
|
|
280
302
|
```
|
|
281
303
|
|
|
282
304
|
**示例:**
|
|
283
305
|
|
|
284
306
|
```typescript
|
|
285
|
-
const unsubscribe =
|
|
307
|
+
const unsubscribe = nexus.onMetrics((metrics) => {
|
|
286
308
|
// 发送到监控系统
|
|
287
309
|
metricsService.report(metrics)
|
|
288
310
|
})
|
|
@@ -293,7 +315,7 @@ const unsubscribe = bridge.onMetrics((metrics) => {
|
|
|
293
315
|
刷新消息队列,发送所有缓存的消息。
|
|
294
316
|
|
|
295
317
|
```typescript
|
|
296
|
-
|
|
318
|
+
nexus.flushQueue()
|
|
297
319
|
```
|
|
298
320
|
|
|
299
321
|
##### destroy()
|
|
@@ -301,7 +323,7 @@ bridge.flushQueue()
|
|
|
301
323
|
销毁实例,清理资源。
|
|
302
324
|
|
|
303
325
|
```typescript
|
|
304
|
-
|
|
326
|
+
nexus.destroy()
|
|
305
327
|
```
|
|
306
328
|
|
|
307
329
|
**注意**: `destroy()` 方法会自动调用驱动的 `destroy()` 方法来清理事件监听器等资源。建议在组件卸载时调用此方法以避免内存泄漏。
|
|
@@ -409,25 +431,25 @@ new BroadcastDriver(options: BroadcastDriverOptions)
|
|
|
409
431
|
**示例:**
|
|
410
432
|
|
|
411
433
|
```typescript
|
|
412
|
-
import { BroadcastDriver,
|
|
434
|
+
import { BroadcastDriver, MessageNexus } from 'message-nexus'
|
|
413
435
|
|
|
414
436
|
const driver = new BroadcastDriver({ channel: 'my-app-channel' })
|
|
415
|
-
const
|
|
437
|
+
const nexus = new MessageNexus(driver)
|
|
416
438
|
|
|
417
439
|
// 监听来自其他标签页的消息
|
|
418
|
-
|
|
440
|
+
nexus.onCommand((data) => {
|
|
419
441
|
console.log('Received from another tab:', data)
|
|
420
|
-
|
|
442
|
+
nexus.reply(data.id, { received: true })
|
|
421
443
|
})
|
|
422
444
|
|
|
423
445
|
// 清理资源
|
|
424
|
-
|
|
446
|
+
nexus.destroy()
|
|
425
447
|
```
|
|
426
448
|
|
|
427
449
|
**特性:**
|
|
428
450
|
|
|
429
451
|
- 同一源下的多个标签页可以通过相同频道名进行通信
|
|
430
|
-
- 自动添加协议标识符,过滤非
|
|
452
|
+
- 自动添加协议标识符,过滤非 MessageNexus 消息
|
|
431
453
|
- 支持动态切换频道
|
|
432
454
|
|
|
433
455
|
## Logger 日志
|
|
@@ -471,14 +493,14 @@ logger.setMinLevel(LogLevel.WARN) // 只输出 WARN 和 ERROR
|
|
|
471
493
|
import { Logger } from 'message-nexus/utils/logger'
|
|
472
494
|
|
|
473
495
|
const logger = new Logger('MyBridge')
|
|
474
|
-
const
|
|
496
|
+
const nexus = new MessageNexus(driver, { logger })
|
|
475
497
|
```
|
|
476
498
|
|
|
477
499
|
## 设计亮点
|
|
478
500
|
|
|
479
501
|
### 1. 类型安全
|
|
480
502
|
|
|
481
|
-
|
|
503
|
+
MessageNexus 使用 TypeScript 泛型提供完整的类型推断:
|
|
482
504
|
|
|
483
505
|
```typescript
|
|
484
506
|
interface UserRequest {
|
|
@@ -490,10 +512,10 @@ interface UserResponse {
|
|
|
490
512
|
name: string
|
|
491
513
|
}
|
|
492
514
|
|
|
493
|
-
const
|
|
515
|
+
const nexus = new MessageNexus<UserRequest, UserResponse>(driver)
|
|
494
516
|
|
|
495
517
|
// 完整的类型推断
|
|
496
|
-
const response = await
|
|
518
|
+
const response = await nexus.request({
|
|
497
519
|
type: 'GET_USER',
|
|
498
520
|
payload: { userId: 123 }, // 类型: UserRequest
|
|
499
521
|
})
|
|
@@ -521,7 +543,7 @@ console.log(response.name)
|
|
|
521
543
|
### 4. 安全加固
|
|
522
544
|
|
|
523
545
|
- **PostMessage**: 禁止使用 `'*'` 作为 targetOrigin,必须明确指定源地址
|
|
524
|
-
- **BroadcastChannel**: 使用协议标识符 `__messageBridge` 区分
|
|
546
|
+
- **BroadcastChannel**: 使用协议标识符 `__messageBridge` 区分 MessageNexus 消息和用户自定义消息
|
|
525
547
|
- **消息验证**: 运行时验证消息格式,防止非法消息导致崩溃
|
|
526
548
|
- **来源过滤**: 自动过滤非目标消息
|
|
527
549
|
|
|
@@ -530,7 +552,7 @@ console.log(response.name)
|
|
|
530
552
|
内置监控指标,便于生产环境监控:
|
|
531
553
|
|
|
532
554
|
```typescript
|
|
533
|
-
const metrics =
|
|
555
|
+
const metrics = nexus.getMetrics()
|
|
534
556
|
|
|
535
557
|
console.log(`Messages: ${metrics.messagesSent} sent, ${metrics.messagesReceived} received`)
|
|
536
558
|
console.log(
|
package/dist/index.cjs
CHANGED
|
@@ -36,7 +36,7 @@ __export(index_exports, {
|
|
|
36
36
|
PostMessageDriver: () => PostMessageDriver,
|
|
37
37
|
WebSocketDriver: () => WebSocketDriver,
|
|
38
38
|
createEmitter: () => createEmitter,
|
|
39
|
-
default: () =>
|
|
39
|
+
default: () => MessageNexus
|
|
40
40
|
});
|
|
41
41
|
module.exports = __toCommonJS(index_exports);
|
|
42
42
|
|
|
@@ -320,7 +320,7 @@ function createEmitter() {
|
|
|
320
320
|
}
|
|
321
321
|
|
|
322
322
|
// src/index.ts
|
|
323
|
-
var
|
|
323
|
+
var MessageNexus = class {
|
|
324
324
|
constructor(driver, options) {
|
|
325
325
|
this.cleanupInterval = null;
|
|
326
326
|
this.messageQueue = [];
|
|
@@ -339,14 +339,14 @@ var MessageBridge = class {
|
|
|
339
339
|
this.driver = driver;
|
|
340
340
|
this.instanceId = options?.instanceId || crypto.randomUUID();
|
|
341
341
|
this.timeout = options?.timeout ?? 1e4;
|
|
342
|
-
this.logger = options?.logger || new Logger("
|
|
342
|
+
this.logger = options?.logger || new Logger("MessageNexus");
|
|
343
343
|
this.logger.addHandler(createConsoleHandler());
|
|
344
344
|
this.pendingTasks = /* @__PURE__ */ new Map();
|
|
345
345
|
this.incomingMessages = /* @__PURE__ */ new Map();
|
|
346
346
|
this.messageHandlers = /* @__PURE__ */ new Set();
|
|
347
347
|
this.cleanupInterval = null;
|
|
348
348
|
this.driver.onMessage = (data) => this._handleIncoming(data);
|
|
349
|
-
this.logger.info("
|
|
349
|
+
this.logger.info("MessageNexus initialized", {
|
|
350
350
|
instanceId: this.instanceId,
|
|
351
351
|
timeout: this.timeout
|
|
352
352
|
});
|
|
@@ -546,7 +546,7 @@ var MessageBridge = class {
|
|
|
546
546
|
this.incomingMessages.delete(messageId);
|
|
547
547
|
}
|
|
548
548
|
destroy() {
|
|
549
|
-
this.logger.info("
|
|
549
|
+
this.logger.info("MessageNexus destroying", {
|
|
550
550
|
instanceId: this.instanceId,
|
|
551
551
|
pendingMessages: this.pendingTasks.size,
|
|
552
552
|
queuedMessages: this.messageQueue.length,
|
package/dist/index.d.cts
CHANGED
|
@@ -104,7 +104,7 @@ declare class WebSocketDriver extends BaseDriver {
|
|
|
104
104
|
|
|
105
105
|
declare function createEmitter(): Emitter<Record<string, Message>>;
|
|
106
106
|
|
|
107
|
-
interface
|
|
107
|
+
interface MessageNexusOptions {
|
|
108
108
|
instanceId?: string;
|
|
109
109
|
timeout?: number;
|
|
110
110
|
logger?: Logger;
|
|
@@ -133,7 +133,7 @@ interface Metrics {
|
|
|
133
133
|
averageLatency: number;
|
|
134
134
|
}
|
|
135
135
|
type MetricsCallback = (metrics: Metrics) => void;
|
|
136
|
-
declare class
|
|
136
|
+
declare class MessageNexus<RequestPayload = unknown, ResponsePayload = unknown> {
|
|
137
137
|
driver: BaseDriver;
|
|
138
138
|
pendingTasks: Map<string, {
|
|
139
139
|
resolve: (value: ResponsePayload) => void;
|
|
@@ -157,7 +157,7 @@ declare class MessageBridge<RequestPayload = unknown, ResponsePayload = unknown>
|
|
|
157
157
|
private logger;
|
|
158
158
|
private metrics;
|
|
159
159
|
private metricsCallbacks;
|
|
160
|
-
constructor(driver: BaseDriver, options?:
|
|
160
|
+
constructor(driver: BaseDriver, options?: MessageNexusOptions);
|
|
161
161
|
request(typeOrOptions: string | RequestOptions): Promise<ResponsePayload>;
|
|
162
162
|
private _sendMessage;
|
|
163
163
|
onError(handler: ErrorHandler): () => void;
|
|
@@ -172,4 +172,4 @@ declare class MessageBridge<RequestPayload = unknown, ResponsePayload = unknown>
|
|
|
172
172
|
destroy(): void;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
export { BaseDriver, BroadcastDriver, type CommandMessage, type ErrorHandler, type Message, type
|
|
175
|
+
export { BaseDriver, BroadcastDriver, type CommandMessage, type ErrorHandler, type Message, type MessageNexusOptions, type Metrics, type MetricsCallback, MittDriver, PostMessageDriver, type RequestOptions, WebSocketDriver, createEmitter, MessageNexus as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -104,7 +104,7 @@ declare class WebSocketDriver extends BaseDriver {
|
|
|
104
104
|
|
|
105
105
|
declare function createEmitter(): Emitter<Record<string, Message>>;
|
|
106
106
|
|
|
107
|
-
interface
|
|
107
|
+
interface MessageNexusOptions {
|
|
108
108
|
instanceId?: string;
|
|
109
109
|
timeout?: number;
|
|
110
110
|
logger?: Logger;
|
|
@@ -133,7 +133,7 @@ interface Metrics {
|
|
|
133
133
|
averageLatency: number;
|
|
134
134
|
}
|
|
135
135
|
type MetricsCallback = (metrics: Metrics) => void;
|
|
136
|
-
declare class
|
|
136
|
+
declare class MessageNexus<RequestPayload = unknown, ResponsePayload = unknown> {
|
|
137
137
|
driver: BaseDriver;
|
|
138
138
|
pendingTasks: Map<string, {
|
|
139
139
|
resolve: (value: ResponsePayload) => void;
|
|
@@ -157,7 +157,7 @@ declare class MessageBridge<RequestPayload = unknown, ResponsePayload = unknown>
|
|
|
157
157
|
private logger;
|
|
158
158
|
private metrics;
|
|
159
159
|
private metricsCallbacks;
|
|
160
|
-
constructor(driver: BaseDriver, options?:
|
|
160
|
+
constructor(driver: BaseDriver, options?: MessageNexusOptions);
|
|
161
161
|
request(typeOrOptions: string | RequestOptions): Promise<ResponsePayload>;
|
|
162
162
|
private _sendMessage;
|
|
163
163
|
onError(handler: ErrorHandler): () => void;
|
|
@@ -172,4 +172,4 @@ declare class MessageBridge<RequestPayload = unknown, ResponsePayload = unknown>
|
|
|
172
172
|
destroy(): void;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
export { BaseDriver, BroadcastDriver, type CommandMessage, type ErrorHandler, type Message, type
|
|
175
|
+
export { BaseDriver, BroadcastDriver, type CommandMessage, type ErrorHandler, type Message, type MessageNexusOptions, type Metrics, type MetricsCallback, MittDriver, PostMessageDriver, type RequestOptions, WebSocketDriver, createEmitter, MessageNexus as default };
|
package/dist/index.js
CHANGED
|
@@ -278,7 +278,7 @@ function createEmitter() {
|
|
|
278
278
|
}
|
|
279
279
|
|
|
280
280
|
// src/index.ts
|
|
281
|
-
var
|
|
281
|
+
var MessageNexus = class {
|
|
282
282
|
constructor(driver, options) {
|
|
283
283
|
this.cleanupInterval = null;
|
|
284
284
|
this.messageQueue = [];
|
|
@@ -297,14 +297,14 @@ var MessageBridge = class {
|
|
|
297
297
|
this.driver = driver;
|
|
298
298
|
this.instanceId = options?.instanceId || crypto.randomUUID();
|
|
299
299
|
this.timeout = options?.timeout ?? 1e4;
|
|
300
|
-
this.logger = options?.logger || new Logger("
|
|
300
|
+
this.logger = options?.logger || new Logger("MessageNexus");
|
|
301
301
|
this.logger.addHandler(createConsoleHandler());
|
|
302
302
|
this.pendingTasks = /* @__PURE__ */ new Map();
|
|
303
303
|
this.incomingMessages = /* @__PURE__ */ new Map();
|
|
304
304
|
this.messageHandlers = /* @__PURE__ */ new Set();
|
|
305
305
|
this.cleanupInterval = null;
|
|
306
306
|
this.driver.onMessage = (data) => this._handleIncoming(data);
|
|
307
|
-
this.logger.info("
|
|
307
|
+
this.logger.info("MessageNexus initialized", {
|
|
308
308
|
instanceId: this.instanceId,
|
|
309
309
|
timeout: this.timeout
|
|
310
310
|
});
|
|
@@ -504,7 +504,7 @@ var MessageBridge = class {
|
|
|
504
504
|
this.incomingMessages.delete(messageId);
|
|
505
505
|
}
|
|
506
506
|
destroy() {
|
|
507
|
-
this.logger.info("
|
|
507
|
+
this.logger.info("MessageNexus destroying", {
|
|
508
508
|
instanceId: this.instanceId,
|
|
509
509
|
pendingMessages: this.pendingTasks.size,
|
|
510
510
|
queuedMessages: this.messageQueue.length,
|
|
@@ -526,5 +526,5 @@ export {
|
|
|
526
526
|
PostMessageDriver,
|
|
527
527
|
WebSocketDriver,
|
|
528
528
|
createEmitter,
|
|
529
|
-
|
|
529
|
+
MessageNexus as default
|
|
530
530
|
};
|