message-nexus 1.1.1 → 1.1.3
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 +105 -43
- package/dist/index.cjs +1 -741
- package/dist/index.d.cts +82 -23
- package/dist/index.d.ts +82 -23
- package/dist/index.js +1 -699
- package/package.json +10 -1
package/dist/index.d.cts
CHANGED
|
@@ -78,16 +78,18 @@ interface LogEntry {
|
|
|
78
78
|
context?: string;
|
|
79
79
|
}
|
|
80
80
|
type LogHandler = (entry: LogEntry) => void;
|
|
81
|
-
interface
|
|
81
|
+
interface SimpleLogger {
|
|
82
|
+
debug(message: string, metadata?: Record<string, unknown>): void;
|
|
83
|
+
info(message: string, metadata?: Record<string, unknown>): void;
|
|
84
|
+
warn(message: string, metadata?: Record<string, unknown>): void;
|
|
85
|
+
error(message: string, metadata?: Record<string, unknown>): void;
|
|
86
|
+
}
|
|
87
|
+
interface LoggerInterface extends SimpleLogger {
|
|
82
88
|
addHandler(handler: LogHandler): void;
|
|
83
89
|
setMinLevel(level: LogLevel): void;
|
|
84
90
|
enable(): void;
|
|
85
91
|
disable(): void;
|
|
86
92
|
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
93
|
}
|
|
92
94
|
declare class Logger implements LoggerInterface {
|
|
93
95
|
private handlers;
|
|
@@ -143,21 +145,51 @@ declare function createEmitter(): Emitter<Record<EventType, Message>>;
|
|
|
143
145
|
interface MessageNexusOptions {
|
|
144
146
|
instanceId?: string;
|
|
145
147
|
timeout?: number;
|
|
146
|
-
logger?: LoggerInterface;
|
|
148
|
+
logger?: LoggerInterface | SimpleLogger;
|
|
147
149
|
loggerEnabled?: boolean;
|
|
150
|
+
logLevel?: LogLevel;
|
|
148
151
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Interface representing a method schema with parameters and results.
|
|
154
|
+
*/
|
|
155
|
+
interface MethodSchema {
|
|
156
|
+
params?: any;
|
|
157
|
+
result?: any;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Default registry for method schemas.
|
|
161
|
+
*/
|
|
162
|
+
type DefaultRegistry = Record<string, MethodSchema>;
|
|
163
|
+
/**
|
|
164
|
+
* Helper to extract params from a schema or return any if not present.
|
|
165
|
+
*/
|
|
166
|
+
type GetParams<T> = T extends {
|
|
167
|
+
params: infer P;
|
|
168
|
+
} ? P : any;
|
|
169
|
+
/**
|
|
170
|
+
* Helper to extract result from a schema or return any if not present.
|
|
171
|
+
*/
|
|
172
|
+
type GetResult<T> = T extends {
|
|
173
|
+
result: infer R;
|
|
174
|
+
} ? R : any;
|
|
175
|
+
/**
|
|
176
|
+
* Options for invoking a method.
|
|
177
|
+
*/
|
|
178
|
+
interface InvokeOptions<K extends string = string, P = any> {
|
|
179
|
+
method: K;
|
|
180
|
+
params?: P;
|
|
152
181
|
to?: string;
|
|
153
182
|
metadata?: Record<string, unknown>;
|
|
154
183
|
timeout?: number;
|
|
155
184
|
retryCount?: number;
|
|
156
185
|
retryDelay?: number;
|
|
157
186
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Options for sending a notification.
|
|
189
|
+
*/
|
|
190
|
+
interface NotificationOptions<K extends string = string, P = any> {
|
|
191
|
+
method: K;
|
|
192
|
+
params?: P;
|
|
161
193
|
to?: string;
|
|
162
194
|
metadata?: Record<string, unknown>;
|
|
163
195
|
}
|
|
@@ -167,9 +199,36 @@ interface InvokeContext {
|
|
|
167
199
|
to?: string;
|
|
168
200
|
metadata?: Record<string, unknown>;
|
|
169
201
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Handler for an invoked method.
|
|
204
|
+
*/
|
|
205
|
+
type InvokeHandler<P = any, R = any> = (params: P, context: InvokeContext) => R | Promise<R>;
|
|
206
|
+
/**
|
|
207
|
+
* Handler for a notification.
|
|
208
|
+
*/
|
|
209
|
+
type NotificationHandler<P = any> = (params: P, context: InvokeContext) => void;
|
|
210
|
+
/**
|
|
211
|
+
* Standard JSON-RPC 2.0 and Nexus-specific error codes.
|
|
212
|
+
*/
|
|
213
|
+
declare enum NexusErrorCode {
|
|
214
|
+
ParseError = -32700,
|
|
215
|
+
InvalidRequest = -32600,
|
|
216
|
+
MethodNotFound = -32601,
|
|
217
|
+
InvalidParams = -32602,
|
|
218
|
+
InternalError = -32603,
|
|
219
|
+
Timeout = -32001,
|
|
220
|
+
SendFailed = -32002,
|
|
221
|
+
InvalidResponse = -32003
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Custom error class for MessageNexus.
|
|
225
|
+
*/
|
|
226
|
+
declare class NexusError<D = any> extends Error {
|
|
227
|
+
readonly code: number;
|
|
228
|
+
readonly data?: D;
|
|
229
|
+
constructor(message: string, code?: number, data?: D);
|
|
230
|
+
}
|
|
231
|
+
type ErrorHandler = (error: Error | NexusError, context?: Record<string, unknown>) => void;
|
|
173
232
|
interface Metrics {
|
|
174
233
|
messagesSent: number;
|
|
175
234
|
messagesReceived: number;
|
|
@@ -180,7 +239,7 @@ interface Metrics {
|
|
|
180
239
|
averageLatency: number;
|
|
181
240
|
}
|
|
182
241
|
type MetricsCallback = (metrics: Metrics) => void;
|
|
183
|
-
declare class MessageNexus<
|
|
242
|
+
declare class MessageNexus<InvokeMap extends object = DefaultRegistry, NotificationMap extends object = Record<string, any>> {
|
|
184
243
|
driver: BaseDriver;
|
|
185
244
|
pendingTasks: Map<string, {
|
|
186
245
|
resolve: (value: any) => void;
|
|
@@ -201,23 +260,23 @@ declare class MessageNexus<GlobalRequestPayload = unknown, GlobalResponsePayload
|
|
|
201
260
|
private metrics;
|
|
202
261
|
private metricsCallbacks;
|
|
203
262
|
constructor(driver: BaseDriver, options?: MessageNexusOptions);
|
|
204
|
-
invoke<
|
|
263
|
+
invoke<K extends keyof InvokeMap>(methodOrOptions: K | InvokeOptions<K & string, GetParams<InvokeMap[K]>>): Promise<GetResult<InvokeMap[K]>>;
|
|
205
264
|
private _sendMessage;
|
|
206
265
|
onError(handler: ErrorHandler): () => void;
|
|
207
266
|
flushQueue(): void;
|
|
208
|
-
notify(methodOrOptions:
|
|
267
|
+
notify<K extends keyof NotificationMap>(methodOrOptions: K | NotificationOptions<K & string, NotificationMap[K]>): void;
|
|
209
268
|
private _validateMessage;
|
|
210
269
|
_handleIncoming(data: unknown): Promise<void>;
|
|
211
270
|
getMetrics(): Metrics;
|
|
212
271
|
onMetrics(callback: MetricsCallback): () => boolean;
|
|
213
272
|
private _notifyMetrics;
|
|
214
|
-
handle<
|
|
215
|
-
removeHandler(method:
|
|
216
|
-
onNotification<
|
|
217
|
-
offNotification(method:
|
|
273
|
+
handle<K extends keyof InvokeMap>(method: K, handler: InvokeHandler<GetParams<InvokeMap[K]>, GetResult<InvokeMap[K]>>): () => boolean;
|
|
274
|
+
removeHandler(method: keyof InvokeMap): void;
|
|
275
|
+
onNotification<K extends keyof NotificationMap>(method: K, handler: NotificationHandler<NotificationMap[K]>): () => void;
|
|
276
|
+
offNotification<K extends keyof NotificationMap>(method: K, handler: NotificationHandler<NotificationMap[K]>): void;
|
|
218
277
|
private _reply;
|
|
219
278
|
private _replyError;
|
|
220
279
|
destroy(): void;
|
|
221
280
|
}
|
|
222
281
|
|
|
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 };
|
|
282
|
+
export { BaseDriver, BroadcastDriver, type DefaultRegistry, type ErrorHandler, type InvokeContext, type InvokeHandler, type InvokeOptions, LogLevel, type LoggerInterface, type Message, type MessageNexusOptions, type MethodSchema, type Metrics, type MetricsCallback, MittDriver, NexusError, NexusErrorCode, type NotificationHandler, type NotificationOptions, PostMessageDriver, type SimpleLogger, WebSocketDriver, createEmitter, MessageNexus as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -78,16 +78,18 @@ interface LogEntry {
|
|
|
78
78
|
context?: string;
|
|
79
79
|
}
|
|
80
80
|
type LogHandler = (entry: LogEntry) => void;
|
|
81
|
-
interface
|
|
81
|
+
interface SimpleLogger {
|
|
82
|
+
debug(message: string, metadata?: Record<string, unknown>): void;
|
|
83
|
+
info(message: string, metadata?: Record<string, unknown>): void;
|
|
84
|
+
warn(message: string, metadata?: Record<string, unknown>): void;
|
|
85
|
+
error(message: string, metadata?: Record<string, unknown>): void;
|
|
86
|
+
}
|
|
87
|
+
interface LoggerInterface extends SimpleLogger {
|
|
82
88
|
addHandler(handler: LogHandler): void;
|
|
83
89
|
setMinLevel(level: LogLevel): void;
|
|
84
90
|
enable(): void;
|
|
85
91
|
disable(): void;
|
|
86
92
|
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
93
|
}
|
|
92
94
|
declare class Logger implements LoggerInterface {
|
|
93
95
|
private handlers;
|
|
@@ -143,21 +145,51 @@ declare function createEmitter(): Emitter<Record<EventType, Message>>;
|
|
|
143
145
|
interface MessageNexusOptions {
|
|
144
146
|
instanceId?: string;
|
|
145
147
|
timeout?: number;
|
|
146
|
-
logger?: LoggerInterface;
|
|
148
|
+
logger?: LoggerInterface | SimpleLogger;
|
|
147
149
|
loggerEnabled?: boolean;
|
|
150
|
+
logLevel?: LogLevel;
|
|
148
151
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Interface representing a method schema with parameters and results.
|
|
154
|
+
*/
|
|
155
|
+
interface MethodSchema {
|
|
156
|
+
params?: any;
|
|
157
|
+
result?: any;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Default registry for method schemas.
|
|
161
|
+
*/
|
|
162
|
+
type DefaultRegistry = Record<string, MethodSchema>;
|
|
163
|
+
/**
|
|
164
|
+
* Helper to extract params from a schema or return any if not present.
|
|
165
|
+
*/
|
|
166
|
+
type GetParams<T> = T extends {
|
|
167
|
+
params: infer P;
|
|
168
|
+
} ? P : any;
|
|
169
|
+
/**
|
|
170
|
+
* Helper to extract result from a schema or return any if not present.
|
|
171
|
+
*/
|
|
172
|
+
type GetResult<T> = T extends {
|
|
173
|
+
result: infer R;
|
|
174
|
+
} ? R : any;
|
|
175
|
+
/**
|
|
176
|
+
* Options for invoking a method.
|
|
177
|
+
*/
|
|
178
|
+
interface InvokeOptions<K extends string = string, P = any> {
|
|
179
|
+
method: K;
|
|
180
|
+
params?: P;
|
|
152
181
|
to?: string;
|
|
153
182
|
metadata?: Record<string, unknown>;
|
|
154
183
|
timeout?: number;
|
|
155
184
|
retryCount?: number;
|
|
156
185
|
retryDelay?: number;
|
|
157
186
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Options for sending a notification.
|
|
189
|
+
*/
|
|
190
|
+
interface NotificationOptions<K extends string = string, P = any> {
|
|
191
|
+
method: K;
|
|
192
|
+
params?: P;
|
|
161
193
|
to?: string;
|
|
162
194
|
metadata?: Record<string, unknown>;
|
|
163
195
|
}
|
|
@@ -167,9 +199,36 @@ interface InvokeContext {
|
|
|
167
199
|
to?: string;
|
|
168
200
|
metadata?: Record<string, unknown>;
|
|
169
201
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Handler for an invoked method.
|
|
204
|
+
*/
|
|
205
|
+
type InvokeHandler<P = any, R = any> = (params: P, context: InvokeContext) => R | Promise<R>;
|
|
206
|
+
/**
|
|
207
|
+
* Handler for a notification.
|
|
208
|
+
*/
|
|
209
|
+
type NotificationHandler<P = any> = (params: P, context: InvokeContext) => void;
|
|
210
|
+
/**
|
|
211
|
+
* Standard JSON-RPC 2.0 and Nexus-specific error codes.
|
|
212
|
+
*/
|
|
213
|
+
declare enum NexusErrorCode {
|
|
214
|
+
ParseError = -32700,
|
|
215
|
+
InvalidRequest = -32600,
|
|
216
|
+
MethodNotFound = -32601,
|
|
217
|
+
InvalidParams = -32602,
|
|
218
|
+
InternalError = -32603,
|
|
219
|
+
Timeout = -32001,
|
|
220
|
+
SendFailed = -32002,
|
|
221
|
+
InvalidResponse = -32003
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Custom error class for MessageNexus.
|
|
225
|
+
*/
|
|
226
|
+
declare class NexusError<D = any> extends Error {
|
|
227
|
+
readonly code: number;
|
|
228
|
+
readonly data?: D;
|
|
229
|
+
constructor(message: string, code?: number, data?: D);
|
|
230
|
+
}
|
|
231
|
+
type ErrorHandler = (error: Error | NexusError, context?: Record<string, unknown>) => void;
|
|
173
232
|
interface Metrics {
|
|
174
233
|
messagesSent: number;
|
|
175
234
|
messagesReceived: number;
|
|
@@ -180,7 +239,7 @@ interface Metrics {
|
|
|
180
239
|
averageLatency: number;
|
|
181
240
|
}
|
|
182
241
|
type MetricsCallback = (metrics: Metrics) => void;
|
|
183
|
-
declare class MessageNexus<
|
|
242
|
+
declare class MessageNexus<InvokeMap extends object = DefaultRegistry, NotificationMap extends object = Record<string, any>> {
|
|
184
243
|
driver: BaseDriver;
|
|
185
244
|
pendingTasks: Map<string, {
|
|
186
245
|
resolve: (value: any) => void;
|
|
@@ -201,23 +260,23 @@ declare class MessageNexus<GlobalRequestPayload = unknown, GlobalResponsePayload
|
|
|
201
260
|
private metrics;
|
|
202
261
|
private metricsCallbacks;
|
|
203
262
|
constructor(driver: BaseDriver, options?: MessageNexusOptions);
|
|
204
|
-
invoke<
|
|
263
|
+
invoke<K extends keyof InvokeMap>(methodOrOptions: K | InvokeOptions<K & string, GetParams<InvokeMap[K]>>): Promise<GetResult<InvokeMap[K]>>;
|
|
205
264
|
private _sendMessage;
|
|
206
265
|
onError(handler: ErrorHandler): () => void;
|
|
207
266
|
flushQueue(): void;
|
|
208
|
-
notify(methodOrOptions:
|
|
267
|
+
notify<K extends keyof NotificationMap>(methodOrOptions: K | NotificationOptions<K & string, NotificationMap[K]>): void;
|
|
209
268
|
private _validateMessage;
|
|
210
269
|
_handleIncoming(data: unknown): Promise<void>;
|
|
211
270
|
getMetrics(): Metrics;
|
|
212
271
|
onMetrics(callback: MetricsCallback): () => boolean;
|
|
213
272
|
private _notifyMetrics;
|
|
214
|
-
handle<
|
|
215
|
-
removeHandler(method:
|
|
216
|
-
onNotification<
|
|
217
|
-
offNotification(method:
|
|
273
|
+
handle<K extends keyof InvokeMap>(method: K, handler: InvokeHandler<GetParams<InvokeMap[K]>, GetResult<InvokeMap[K]>>): () => boolean;
|
|
274
|
+
removeHandler(method: keyof InvokeMap): void;
|
|
275
|
+
onNotification<K extends keyof NotificationMap>(method: K, handler: NotificationHandler<NotificationMap[K]>): () => void;
|
|
276
|
+
offNotification<K extends keyof NotificationMap>(method: K, handler: NotificationHandler<NotificationMap[K]>): void;
|
|
218
277
|
private _reply;
|
|
219
278
|
private _replyError;
|
|
220
279
|
destroy(): void;
|
|
221
280
|
}
|
|
222
281
|
|
|
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 };
|
|
282
|
+
export { BaseDriver, BroadcastDriver, type DefaultRegistry, type ErrorHandler, type InvokeContext, type InvokeHandler, type InvokeOptions, LogLevel, type LoggerInterface, type Message, type MessageNexusOptions, type MethodSchema, type Metrics, type MetricsCallback, MittDriver, NexusError, NexusErrorCode, type NotificationHandler, type NotificationOptions, PostMessageDriver, type SimpleLogger, WebSocketDriver, createEmitter, MessageNexus as default };
|