message-nexus 1.0.0 → 1.1.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/README.md +311 -260
- package/dist/index.cjs +259 -90
- package/dist/index.d.cts +78 -30
- package/dist/index.d.ts +78 -30
- package/dist/index.js +259 -90
- package/package.json +9 -9
package/dist/index.d.cts
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
import { Emitter } from 'mitt';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
type JsonRpcId = string | number | null;
|
|
4
|
+
interface JsonRpcRequest {
|
|
5
|
+
jsonrpc: '2.0';
|
|
6
|
+
method: string;
|
|
7
|
+
params?: unknown;
|
|
8
|
+
id: JsonRpcId;
|
|
9
|
+
}
|
|
10
|
+
interface JsonRpcNotification {
|
|
11
|
+
jsonrpc: '2.0';
|
|
12
|
+
method: string;
|
|
13
|
+
params?: unknown;
|
|
14
|
+
}
|
|
15
|
+
interface JsonRpcResponse {
|
|
16
|
+
jsonrpc: '2.0';
|
|
17
|
+
result?: unknown;
|
|
18
|
+
error?: {
|
|
19
|
+
code: number;
|
|
20
|
+
message: string;
|
|
21
|
+
data?: unknown;
|
|
22
|
+
};
|
|
23
|
+
id: JsonRpcId;
|
|
24
|
+
}
|
|
25
|
+
interface NexusEnvelope<T = JsonRpcRequest | JsonRpcResponse | JsonRpcNotification> {
|
|
7
26
|
from: string;
|
|
8
27
|
to?: string;
|
|
9
28
|
metadata?: Record<string, unknown>;
|
|
10
|
-
|
|
11
|
-
error?: unknown;
|
|
29
|
+
payload: T;
|
|
12
30
|
}
|
|
31
|
+
type Message = NexusEnvelope;
|
|
13
32
|
declare class BaseDriver {
|
|
14
33
|
onMessage: ((data: Message) => void) | null;
|
|
15
34
|
constructor();
|
|
@@ -59,11 +78,26 @@ interface LogEntry {
|
|
|
59
78
|
context?: string;
|
|
60
79
|
}
|
|
61
80
|
type LogHandler = (entry: LogEntry) => void;
|
|
62
|
-
|
|
81
|
+
interface LoggerInterface {
|
|
82
|
+
addHandler(handler: LogHandler): void;
|
|
83
|
+
setMinLevel(level: LogLevel): void;
|
|
84
|
+
enable(): void;
|
|
85
|
+
disable(): void;
|
|
86
|
+
isEnabled(): boolean;
|
|
87
|
+
debug(message: string, metadata?: Record<string, unknown>): void;
|
|
88
|
+
info(message: string, metadata?: Record<string, unknown>): void;
|
|
89
|
+
warn(message: string, metadata?: Record<string, unknown>): void;
|
|
90
|
+
error(message: string, metadata?: Record<string, unknown>): void;
|
|
91
|
+
}
|
|
92
|
+
declare class Logger implements LoggerInterface {
|
|
63
93
|
private handlers;
|
|
64
94
|
private context;
|
|
65
95
|
private minLevel;
|
|
66
|
-
|
|
96
|
+
private enabled;
|
|
97
|
+
constructor(context: string, minLevel?: LogLevel, enabled?: boolean);
|
|
98
|
+
enable(): void;
|
|
99
|
+
disable(): void;
|
|
100
|
+
isEnabled(): boolean;
|
|
67
101
|
addHandler(handler: LogHandler): void;
|
|
68
102
|
setMinLevel(level: LogLevel): void;
|
|
69
103
|
private shouldLog;
|
|
@@ -83,6 +117,7 @@ interface WebSocketDriverOptions {
|
|
|
83
117
|
url: string;
|
|
84
118
|
reconnect?: boolean | ReconnectOptions;
|
|
85
119
|
logger?: Logger;
|
|
120
|
+
onStatusChange?: (status: 'connecting' | 'connected' | 'disconnected' | 'error') => void;
|
|
86
121
|
}
|
|
87
122
|
declare class WebSocketDriver extends BaseDriver {
|
|
88
123
|
private url;
|
|
@@ -94,6 +129,7 @@ declare class WebSocketDriver extends BaseDriver {
|
|
|
94
129
|
private reconnectTimer;
|
|
95
130
|
private isManuallyClosed;
|
|
96
131
|
private logger;
|
|
132
|
+
private onStatusChange?;
|
|
97
133
|
constructor(options: WebSocketDriverOptions);
|
|
98
134
|
private connect;
|
|
99
135
|
private scheduleReconnect;
|
|
@@ -104,24 +140,35 @@ declare class WebSocketDriver extends BaseDriver {
|
|
|
104
140
|
|
|
105
141
|
declare function createEmitter(): Emitter<Record<string, Message>>;
|
|
106
142
|
|
|
107
|
-
interface
|
|
143
|
+
interface MessageNexusOptions {
|
|
108
144
|
instanceId?: string;
|
|
109
145
|
timeout?: number;
|
|
110
|
-
logger?:
|
|
146
|
+
logger?: LoggerInterface;
|
|
147
|
+
loggerEnabled?: boolean;
|
|
111
148
|
}
|
|
112
|
-
interface
|
|
113
|
-
|
|
114
|
-
|
|
149
|
+
interface InvokeOptions {
|
|
150
|
+
method: string;
|
|
151
|
+
params?: unknown;
|
|
115
152
|
to?: string;
|
|
116
153
|
metadata?: Record<string, unknown>;
|
|
117
154
|
timeout?: number;
|
|
118
155
|
retryCount?: number;
|
|
119
156
|
retryDelay?: number;
|
|
120
157
|
}
|
|
121
|
-
interface
|
|
122
|
-
|
|
123
|
-
|
|
158
|
+
interface NotificationOptions {
|
|
159
|
+
method: string;
|
|
160
|
+
params?: unknown;
|
|
161
|
+
to?: string;
|
|
162
|
+
metadata?: Record<string, unknown>;
|
|
163
|
+
}
|
|
164
|
+
interface InvokeContext {
|
|
165
|
+
messageId?: string;
|
|
166
|
+
from: string;
|
|
167
|
+
to?: string;
|
|
168
|
+
metadata?: Record<string, unknown>;
|
|
124
169
|
}
|
|
170
|
+
type InvokeHandler<Params = any, Result = any> = (params: Params, context: InvokeContext) => Result | Promise<Result>;
|
|
171
|
+
type NotificationHandler<Params = any> = (params: Params, context: InvokeContext) => void;
|
|
125
172
|
type ErrorHandler = (error: Error, context?: Record<string, unknown>) => void;
|
|
126
173
|
interface Metrics {
|
|
127
174
|
messagesSent: number;
|
|
@@ -133,21 +180,17 @@ interface Metrics {
|
|
|
133
180
|
averageLatency: number;
|
|
134
181
|
}
|
|
135
182
|
type MetricsCallback = (metrics: Metrics) => void;
|
|
136
|
-
declare class
|
|
183
|
+
declare class MessageNexus<GlobalRequestPayload = unknown, GlobalResponsePayload = unknown> {
|
|
137
184
|
driver: BaseDriver;
|
|
138
185
|
pendingTasks: Map<string, {
|
|
139
|
-
resolve: (value:
|
|
186
|
+
resolve: (value: any) => void;
|
|
140
187
|
reject: (reason?: unknown) => void;
|
|
141
188
|
timer: ReturnType<typeof setTimeout>;
|
|
142
189
|
to?: string;
|
|
143
190
|
timestamp: number;
|
|
144
191
|
}>;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
type: string;
|
|
148
|
-
timestamp: number;
|
|
149
|
-
}>;
|
|
150
|
-
messageHandlers: Set<(data: CommandMessage) => void>;
|
|
192
|
+
invokeHandlers: Map<string, InvokeHandler>;
|
|
193
|
+
notificationHandlers: Map<string, Set<NotificationHandler>>;
|
|
151
194
|
timeout: number;
|
|
152
195
|
instanceId: string;
|
|
153
196
|
private cleanupInterval;
|
|
@@ -157,19 +200,24 @@ declare class MessageBridge<RequestPayload = unknown, ResponsePayload = unknown>
|
|
|
157
200
|
private logger;
|
|
158
201
|
private metrics;
|
|
159
202
|
private metricsCallbacks;
|
|
160
|
-
constructor(driver: BaseDriver, options?:
|
|
161
|
-
|
|
203
|
+
constructor(driver: BaseDriver, options?: MessageNexusOptions);
|
|
204
|
+
invoke<T = GlobalResponsePayload>(methodOrOptions: string | InvokeOptions): Promise<T>;
|
|
162
205
|
private _sendMessage;
|
|
163
206
|
onError(handler: ErrorHandler): () => void;
|
|
164
207
|
flushQueue(): void;
|
|
208
|
+
notify(methodOrOptions: string | NotificationOptions): void;
|
|
165
209
|
private _validateMessage;
|
|
166
|
-
_handleIncoming(data: unknown): void
|
|
210
|
+
_handleIncoming(data: unknown): Promise<void>;
|
|
167
211
|
getMetrics(): Metrics;
|
|
168
212
|
onMetrics(callback: MetricsCallback): () => boolean;
|
|
169
213
|
private _notifyMetrics;
|
|
170
|
-
|
|
171
|
-
|
|
214
|
+
handle<Params = any, Result = any>(method: string, handler: InvokeHandler<Params, Result>): () => boolean;
|
|
215
|
+
removeHandler(method: string): void;
|
|
216
|
+
onNotification<Params = any>(method: string, handler: NotificationHandler<Params>): () => void;
|
|
217
|
+
offNotification(method: string, handler: NotificationHandler<any>): void;
|
|
218
|
+
private _reply;
|
|
219
|
+
private _replyError;
|
|
172
220
|
destroy(): void;
|
|
173
221
|
}
|
|
174
222
|
|
|
175
|
-
export { BaseDriver, BroadcastDriver, type
|
|
223
|
+
export { BaseDriver, BroadcastDriver, type ErrorHandler, type InvokeContext, type InvokeHandler, type InvokeOptions, type LoggerInterface, type Message, type MessageNexusOptions, type Metrics, type MetricsCallback, MittDriver, type NotificationHandler, type NotificationOptions, PostMessageDriver, WebSocketDriver, createEmitter, MessageNexus as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
import { Emitter } from 'mitt';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
type JsonRpcId = string | number | null;
|
|
4
|
+
interface JsonRpcRequest {
|
|
5
|
+
jsonrpc: '2.0';
|
|
6
|
+
method: string;
|
|
7
|
+
params?: unknown;
|
|
8
|
+
id: JsonRpcId;
|
|
9
|
+
}
|
|
10
|
+
interface JsonRpcNotification {
|
|
11
|
+
jsonrpc: '2.0';
|
|
12
|
+
method: string;
|
|
13
|
+
params?: unknown;
|
|
14
|
+
}
|
|
15
|
+
interface JsonRpcResponse {
|
|
16
|
+
jsonrpc: '2.0';
|
|
17
|
+
result?: unknown;
|
|
18
|
+
error?: {
|
|
19
|
+
code: number;
|
|
20
|
+
message: string;
|
|
21
|
+
data?: unknown;
|
|
22
|
+
};
|
|
23
|
+
id: JsonRpcId;
|
|
24
|
+
}
|
|
25
|
+
interface NexusEnvelope<T = JsonRpcRequest | JsonRpcResponse | JsonRpcNotification> {
|
|
7
26
|
from: string;
|
|
8
27
|
to?: string;
|
|
9
28
|
metadata?: Record<string, unknown>;
|
|
10
|
-
|
|
11
|
-
error?: unknown;
|
|
29
|
+
payload: T;
|
|
12
30
|
}
|
|
31
|
+
type Message = NexusEnvelope;
|
|
13
32
|
declare class BaseDriver {
|
|
14
33
|
onMessage: ((data: Message) => void) | null;
|
|
15
34
|
constructor();
|
|
@@ -59,11 +78,26 @@ interface LogEntry {
|
|
|
59
78
|
context?: string;
|
|
60
79
|
}
|
|
61
80
|
type LogHandler = (entry: LogEntry) => void;
|
|
62
|
-
|
|
81
|
+
interface LoggerInterface {
|
|
82
|
+
addHandler(handler: LogHandler): void;
|
|
83
|
+
setMinLevel(level: LogLevel): void;
|
|
84
|
+
enable(): void;
|
|
85
|
+
disable(): void;
|
|
86
|
+
isEnabled(): boolean;
|
|
87
|
+
debug(message: string, metadata?: Record<string, unknown>): void;
|
|
88
|
+
info(message: string, metadata?: Record<string, unknown>): void;
|
|
89
|
+
warn(message: string, metadata?: Record<string, unknown>): void;
|
|
90
|
+
error(message: string, metadata?: Record<string, unknown>): void;
|
|
91
|
+
}
|
|
92
|
+
declare class Logger implements LoggerInterface {
|
|
63
93
|
private handlers;
|
|
64
94
|
private context;
|
|
65
95
|
private minLevel;
|
|
66
|
-
|
|
96
|
+
private enabled;
|
|
97
|
+
constructor(context: string, minLevel?: LogLevel, enabled?: boolean);
|
|
98
|
+
enable(): void;
|
|
99
|
+
disable(): void;
|
|
100
|
+
isEnabled(): boolean;
|
|
67
101
|
addHandler(handler: LogHandler): void;
|
|
68
102
|
setMinLevel(level: LogLevel): void;
|
|
69
103
|
private shouldLog;
|
|
@@ -83,6 +117,7 @@ interface WebSocketDriverOptions {
|
|
|
83
117
|
url: string;
|
|
84
118
|
reconnect?: boolean | ReconnectOptions;
|
|
85
119
|
logger?: Logger;
|
|
120
|
+
onStatusChange?: (status: 'connecting' | 'connected' | 'disconnected' | 'error') => void;
|
|
86
121
|
}
|
|
87
122
|
declare class WebSocketDriver extends BaseDriver {
|
|
88
123
|
private url;
|
|
@@ -94,6 +129,7 @@ declare class WebSocketDriver extends BaseDriver {
|
|
|
94
129
|
private reconnectTimer;
|
|
95
130
|
private isManuallyClosed;
|
|
96
131
|
private logger;
|
|
132
|
+
private onStatusChange?;
|
|
97
133
|
constructor(options: WebSocketDriverOptions);
|
|
98
134
|
private connect;
|
|
99
135
|
private scheduleReconnect;
|
|
@@ -104,24 +140,35 @@ declare class WebSocketDriver extends BaseDriver {
|
|
|
104
140
|
|
|
105
141
|
declare function createEmitter(): Emitter<Record<string, Message>>;
|
|
106
142
|
|
|
107
|
-
interface
|
|
143
|
+
interface MessageNexusOptions {
|
|
108
144
|
instanceId?: string;
|
|
109
145
|
timeout?: number;
|
|
110
|
-
logger?:
|
|
146
|
+
logger?: LoggerInterface;
|
|
147
|
+
loggerEnabled?: boolean;
|
|
111
148
|
}
|
|
112
|
-
interface
|
|
113
|
-
|
|
114
|
-
|
|
149
|
+
interface InvokeOptions {
|
|
150
|
+
method: string;
|
|
151
|
+
params?: unknown;
|
|
115
152
|
to?: string;
|
|
116
153
|
metadata?: Record<string, unknown>;
|
|
117
154
|
timeout?: number;
|
|
118
155
|
retryCount?: number;
|
|
119
156
|
retryDelay?: number;
|
|
120
157
|
}
|
|
121
|
-
interface
|
|
122
|
-
|
|
123
|
-
|
|
158
|
+
interface NotificationOptions {
|
|
159
|
+
method: string;
|
|
160
|
+
params?: unknown;
|
|
161
|
+
to?: string;
|
|
162
|
+
metadata?: Record<string, unknown>;
|
|
163
|
+
}
|
|
164
|
+
interface InvokeContext {
|
|
165
|
+
messageId?: string;
|
|
166
|
+
from: string;
|
|
167
|
+
to?: string;
|
|
168
|
+
metadata?: Record<string, unknown>;
|
|
124
169
|
}
|
|
170
|
+
type InvokeHandler<Params = any, Result = any> = (params: Params, context: InvokeContext) => Result | Promise<Result>;
|
|
171
|
+
type NotificationHandler<Params = any> = (params: Params, context: InvokeContext) => void;
|
|
125
172
|
type ErrorHandler = (error: Error, context?: Record<string, unknown>) => void;
|
|
126
173
|
interface Metrics {
|
|
127
174
|
messagesSent: number;
|
|
@@ -133,21 +180,17 @@ interface Metrics {
|
|
|
133
180
|
averageLatency: number;
|
|
134
181
|
}
|
|
135
182
|
type MetricsCallback = (metrics: Metrics) => void;
|
|
136
|
-
declare class
|
|
183
|
+
declare class MessageNexus<GlobalRequestPayload = unknown, GlobalResponsePayload = unknown> {
|
|
137
184
|
driver: BaseDriver;
|
|
138
185
|
pendingTasks: Map<string, {
|
|
139
|
-
resolve: (value:
|
|
186
|
+
resolve: (value: any) => void;
|
|
140
187
|
reject: (reason?: unknown) => void;
|
|
141
188
|
timer: ReturnType<typeof setTimeout>;
|
|
142
189
|
to?: string;
|
|
143
190
|
timestamp: number;
|
|
144
191
|
}>;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
type: string;
|
|
148
|
-
timestamp: number;
|
|
149
|
-
}>;
|
|
150
|
-
messageHandlers: Set<(data: CommandMessage) => void>;
|
|
192
|
+
invokeHandlers: Map<string, InvokeHandler>;
|
|
193
|
+
notificationHandlers: Map<string, Set<NotificationHandler>>;
|
|
151
194
|
timeout: number;
|
|
152
195
|
instanceId: string;
|
|
153
196
|
private cleanupInterval;
|
|
@@ -157,19 +200,24 @@ declare class MessageBridge<RequestPayload = unknown, ResponsePayload = unknown>
|
|
|
157
200
|
private logger;
|
|
158
201
|
private metrics;
|
|
159
202
|
private metricsCallbacks;
|
|
160
|
-
constructor(driver: BaseDriver, options?:
|
|
161
|
-
|
|
203
|
+
constructor(driver: BaseDriver, options?: MessageNexusOptions);
|
|
204
|
+
invoke<T = GlobalResponsePayload>(methodOrOptions: string | InvokeOptions): Promise<T>;
|
|
162
205
|
private _sendMessage;
|
|
163
206
|
onError(handler: ErrorHandler): () => void;
|
|
164
207
|
flushQueue(): void;
|
|
208
|
+
notify(methodOrOptions: string | NotificationOptions): void;
|
|
165
209
|
private _validateMessage;
|
|
166
|
-
_handleIncoming(data: unknown): void
|
|
210
|
+
_handleIncoming(data: unknown): Promise<void>;
|
|
167
211
|
getMetrics(): Metrics;
|
|
168
212
|
onMetrics(callback: MetricsCallback): () => boolean;
|
|
169
213
|
private _notifyMetrics;
|
|
170
|
-
|
|
171
|
-
|
|
214
|
+
handle<Params = any, Result = any>(method: string, handler: InvokeHandler<Params, Result>): () => boolean;
|
|
215
|
+
removeHandler(method: string): void;
|
|
216
|
+
onNotification<Params = any>(method: string, handler: NotificationHandler<Params>): () => void;
|
|
217
|
+
offNotification(method: string, handler: NotificationHandler<any>): void;
|
|
218
|
+
private _reply;
|
|
219
|
+
private _replyError;
|
|
172
220
|
destroy(): void;
|
|
173
221
|
}
|
|
174
222
|
|
|
175
|
-
export { BaseDriver, BroadcastDriver, type
|
|
223
|
+
export { BaseDriver, BroadcastDriver, type ErrorHandler, type InvokeContext, type InvokeHandler, type InvokeOptions, type LoggerInterface, type Message, type MessageNexusOptions, type Metrics, type MetricsCallback, MittDriver, type NotificationHandler, type NotificationOptions, PostMessageDriver, WebSocketDriver, createEmitter, MessageNexus as default };
|