@warlock.js/herald 4.0.100
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 +364 -0
- package/cjs/communicators/communicator-registry.d.ts +155 -0
- package/cjs/communicators/communicator-registry.d.ts.map +1 -0
- package/cjs/communicators/communicator-registry.js +206 -0
- package/cjs/communicators/communicator-registry.js.map +1 -0
- package/cjs/communicators/communicator.d.ts +90 -0
- package/cjs/communicators/communicator.d.ts.map +1 -0
- package/cjs/communicators/communicator.js +93 -0
- package/cjs/communicators/communicator.js.map +1 -0
- package/cjs/communicators/index.d.ts +3 -0
- package/cjs/communicators/index.d.ts.map +1 -0
- package/cjs/contracts/channel.contract.d.ts +175 -0
- package/cjs/contracts/channel.contract.d.ts.map +1 -0
- package/cjs/contracts/communicator-driver.contract.d.ts +168 -0
- package/cjs/contracts/communicator-driver.contract.d.ts.map +1 -0
- package/cjs/contracts/index.d.ts +3 -0
- package/cjs/contracts/index.d.ts.map +1 -0
- package/cjs/drivers/index.d.ts +2 -0
- package/cjs/drivers/index.d.ts.map +1 -0
- package/cjs/drivers/rabbitmq/index.d.ts +3 -0
- package/cjs/drivers/rabbitmq/index.d.ts.map +1 -0
- package/cjs/drivers/rabbitmq/rabbitmq-channel.d.ts +70 -0
- package/cjs/drivers/rabbitmq/rabbitmq-channel.d.ts.map +1 -0
- package/cjs/drivers/rabbitmq/rabbitmq-channel.js +400 -0
- package/cjs/drivers/rabbitmq/rabbitmq-channel.js.map +1 -0
- package/cjs/drivers/rabbitmq/rabbitmq-driver.d.ts +100 -0
- package/cjs/drivers/rabbitmq/rabbitmq-driver.d.ts.map +1 -0
- package/cjs/drivers/rabbitmq/rabbitmq-driver.js +299 -0
- package/cjs/drivers/rabbitmq/rabbitmq-driver.js.map +1 -0
- package/cjs/index.d.ts +45 -0
- package/cjs/index.d.ts.map +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -0
- package/cjs/types.d.ts +396 -0
- package/cjs/types.d.ts.map +1 -0
- package/cjs/utils/connect-to-communicator.d.ts +86 -0
- package/cjs/utils/connect-to-communicator.d.ts.map +1 -0
- package/cjs/utils/connect-to-communicator.js +122 -0
- package/cjs/utils/connect-to-communicator.js.map +1 -0
- package/cjs/utils/index.d.ts +2 -0
- package/cjs/utils/index.d.ts.map +1 -0
- package/esm/communicators/communicator-registry.d.ts +155 -0
- package/esm/communicators/communicator-registry.d.ts.map +1 -0
- package/esm/communicators/communicator-registry.js +206 -0
- package/esm/communicators/communicator-registry.js.map +1 -0
- package/esm/communicators/communicator.d.ts +90 -0
- package/esm/communicators/communicator.d.ts.map +1 -0
- package/esm/communicators/communicator.js +93 -0
- package/esm/communicators/communicator.js.map +1 -0
- package/esm/communicators/index.d.ts +3 -0
- package/esm/communicators/index.d.ts.map +1 -0
- package/esm/contracts/channel.contract.d.ts +175 -0
- package/esm/contracts/channel.contract.d.ts.map +1 -0
- package/esm/contracts/communicator-driver.contract.d.ts +168 -0
- package/esm/contracts/communicator-driver.contract.d.ts.map +1 -0
- package/esm/contracts/index.d.ts +3 -0
- package/esm/contracts/index.d.ts.map +1 -0
- package/esm/drivers/index.d.ts +2 -0
- package/esm/drivers/index.d.ts.map +1 -0
- package/esm/drivers/rabbitmq/index.d.ts +3 -0
- package/esm/drivers/rabbitmq/index.d.ts.map +1 -0
- package/esm/drivers/rabbitmq/rabbitmq-channel.d.ts +70 -0
- package/esm/drivers/rabbitmq/rabbitmq-channel.d.ts.map +1 -0
- package/esm/drivers/rabbitmq/rabbitmq-channel.js +400 -0
- package/esm/drivers/rabbitmq/rabbitmq-channel.js.map +1 -0
- package/esm/drivers/rabbitmq/rabbitmq-driver.d.ts +100 -0
- package/esm/drivers/rabbitmq/rabbitmq-driver.d.ts.map +1 -0
- package/esm/drivers/rabbitmq/rabbitmq-driver.js +299 -0
- package/esm/drivers/rabbitmq/rabbitmq-driver.js.map +1 -0
- package/esm/index.d.ts +45 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -0
- package/esm/types.d.ts +396 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/utils/connect-to-communicator.d.ts +86 -0
- package/esm/utils/connect-to-communicator.d.ts.map +1 -0
- package/esm/utils/connect-to-communicator.js +122 -0
- package/esm/utils/connect-to-communicator.js.map +1 -0
- package/esm/utils/index.d.ts +2 -0
- package/esm/utils/index.d.ts.map +1 -0
- package/package.json +47 -0
package/cjs/types.d.ts
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { BaseValidator } from "@warlock.js/seal";
|
|
3
|
+
/**
|
|
4
|
+
* Message metadata that accompanies every message
|
|
5
|
+
*/
|
|
6
|
+
export type MessageMetadata = {
|
|
7
|
+
/** Unique message ID (auto-generated) */
|
|
8
|
+
messageId: string;
|
|
9
|
+
/** Correlation ID for request-response patterns */
|
|
10
|
+
correlationId?: string;
|
|
11
|
+
/** Reply-to channel for RPC patterns */
|
|
12
|
+
replyTo?: string;
|
|
13
|
+
/** Message priority (0-9, where 9 is highest) */
|
|
14
|
+
priority?: number;
|
|
15
|
+
/** Timestamp when message was published */
|
|
16
|
+
timestamp: Date;
|
|
17
|
+
/** Custom headers */
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
/** Retry count (incremented on each retry) */
|
|
20
|
+
retryCount?: number;
|
|
21
|
+
/** Original queue/channel name */
|
|
22
|
+
originalChannel?: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* A message received from a channel
|
|
26
|
+
*
|
|
27
|
+
* @template TPayload - The typed payload
|
|
28
|
+
*/
|
|
29
|
+
export type Message<TPayload = unknown> = {
|
|
30
|
+
/** Message metadata */
|
|
31
|
+
readonly metadata: MessageMetadata;
|
|
32
|
+
/** Typed payload data */
|
|
33
|
+
readonly payload: TPayload;
|
|
34
|
+
/** Raw message data from the driver */
|
|
35
|
+
readonly raw?: unknown;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Options when publishing a message
|
|
39
|
+
*/
|
|
40
|
+
export type PublishOptions = {
|
|
41
|
+
/** Message priority (0-9, where 9 is highest) */
|
|
42
|
+
priority?: number;
|
|
43
|
+
/** Time-to-live in milliseconds */
|
|
44
|
+
ttl?: number;
|
|
45
|
+
/** Delay before message is available (delayed messaging) */
|
|
46
|
+
delay?: number;
|
|
47
|
+
/** Custom headers */
|
|
48
|
+
headers?: Record<string, string>;
|
|
49
|
+
/** Enable message persistence (survives broker restart) */
|
|
50
|
+
persistent?: boolean;
|
|
51
|
+
/** Correlation ID for tracking related messages */
|
|
52
|
+
correlationId?: string;
|
|
53
|
+
/** Expiration time in milliseconds */
|
|
54
|
+
expiration?: number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Retry configuration for failed message processing
|
|
58
|
+
*/
|
|
59
|
+
export type RetryOptions = {
|
|
60
|
+
/** Maximum number of retry attempts */
|
|
61
|
+
maxRetries: number;
|
|
62
|
+
/** Delay between retries in ms, or function for custom backoff */
|
|
63
|
+
delay: number | ((attempt: number) => number);
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Dead-letter configuration for messages that exceed retry attempts
|
|
67
|
+
*/
|
|
68
|
+
export type DeadLetterOptions = {
|
|
69
|
+
/** Dead-letter queue/channel name */
|
|
70
|
+
channel: string;
|
|
71
|
+
/** Whether to preserve original message metadata */
|
|
72
|
+
preserveOriginal?: boolean;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Options when subscribing to a channel
|
|
76
|
+
*/
|
|
77
|
+
export type SubscribeOptions = {
|
|
78
|
+
/** Consumer group/tag name */
|
|
79
|
+
group?: string;
|
|
80
|
+
/** Number of messages to prefetch (concurrency) */
|
|
81
|
+
prefetch?: number;
|
|
82
|
+
/** Auto-acknowledge messages (default: false for manual ack) */
|
|
83
|
+
autoAck?: boolean;
|
|
84
|
+
/** Retry configuration for failed messages */
|
|
85
|
+
retry?: RetryOptions;
|
|
86
|
+
/** Dead-letter configuration */
|
|
87
|
+
deadLetter?: DeadLetterOptions;
|
|
88
|
+
/** Exclusive consumer (only one consumer on this queue) */
|
|
89
|
+
exclusive?: boolean;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Options for request-response (RPC) pattern
|
|
93
|
+
*/
|
|
94
|
+
export type RequestOptions = PublishOptions & {
|
|
95
|
+
/** Timeout in milliseconds to wait for response */
|
|
96
|
+
timeout?: number;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Context provided to message handlers for controlling message flow
|
|
100
|
+
*/
|
|
101
|
+
export type MessageContext = {
|
|
102
|
+
/** Acknowledge the message (mark as successfully processed) */
|
|
103
|
+
ack(): Promise<void>;
|
|
104
|
+
/** Negative acknowledge (requeue or send to dead-letter) */
|
|
105
|
+
nack(requeue?: boolean): Promise<void>;
|
|
106
|
+
/** Reject the message (don't requeue) */
|
|
107
|
+
reject(): Promise<void>;
|
|
108
|
+
/** Reply to a message (for RPC patterns) */
|
|
109
|
+
reply<T>(payload: T): Promise<void>;
|
|
110
|
+
/** Manually retry the message with optional delay */
|
|
111
|
+
retry(delay?: number): Promise<void>;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Represents an active subscription to a channel
|
|
115
|
+
*/
|
|
116
|
+
export type Subscription = {
|
|
117
|
+
/** Unique subscription ID */
|
|
118
|
+
readonly id: string;
|
|
119
|
+
/** Channel name this subscription is for */
|
|
120
|
+
readonly channel: string;
|
|
121
|
+
/** Consumer tag (driver-specific identifier) */
|
|
122
|
+
readonly consumerTag?: string;
|
|
123
|
+
/** Unsubscribe from the channel */
|
|
124
|
+
unsubscribe(): Promise<void>;
|
|
125
|
+
/** Pause message consumption */
|
|
126
|
+
pause(): Promise<void>;
|
|
127
|
+
/** Resume message consumption */
|
|
128
|
+
resume(): Promise<void>;
|
|
129
|
+
/** Check if subscription is active */
|
|
130
|
+
isActive(): boolean;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Handler function for processing messages
|
|
134
|
+
*
|
|
135
|
+
* @template TPayload - The typed payload
|
|
136
|
+
*/
|
|
137
|
+
export type MessageHandler<TPayload = unknown> = (message: Message<TPayload>, ctx: MessageContext) => Promise<void> | void;
|
|
138
|
+
/**
|
|
139
|
+
* Handler function for request-response (RPC) pattern
|
|
140
|
+
*
|
|
141
|
+
* @template TPayload - The request payload type
|
|
142
|
+
* @template TResponse - The response payload type
|
|
143
|
+
*/
|
|
144
|
+
export type ResponseHandler<TPayload = unknown, TResponse = unknown> = (message: Message<TPayload>, ctx: MessageContext) => Promise<TResponse> | TResponse;
|
|
145
|
+
/**
|
|
146
|
+
* Channel/queue configuration options
|
|
147
|
+
*/
|
|
148
|
+
export type ChannelOptions<TPayload = unknown> = {
|
|
149
|
+
/** Channel type (queue for point-to-point, topic for pub/sub, fanout for broadcast) */
|
|
150
|
+
type?: "queue" | "topic" | "fanout";
|
|
151
|
+
/** Durable channel (survives broker restart) */
|
|
152
|
+
durable?: boolean;
|
|
153
|
+
/** Auto-delete when last consumer disconnects */
|
|
154
|
+
autoDelete?: boolean;
|
|
155
|
+
/** Exclusive channel (single connection only) */
|
|
156
|
+
exclusive?: boolean;
|
|
157
|
+
/** Dead-letter configuration */
|
|
158
|
+
deadLetter?: DeadLetterOptions;
|
|
159
|
+
/** Maximum message size in bytes */
|
|
160
|
+
maxMessageSize?: number;
|
|
161
|
+
/** Default message TTL in milliseconds */
|
|
162
|
+
messageTtl?: number;
|
|
163
|
+
/** Maximum number of messages in queue */
|
|
164
|
+
maxLength?: number;
|
|
165
|
+
/** Seal schema for validation */
|
|
166
|
+
schema?: BaseValidator;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Statistics for a channel
|
|
170
|
+
*/
|
|
171
|
+
export type ChannelStats = {
|
|
172
|
+
/** Number of messages in the queue */
|
|
173
|
+
messageCount: number;
|
|
174
|
+
/** Number of active consumers */
|
|
175
|
+
consumerCount: number;
|
|
176
|
+
/** Channel name */
|
|
177
|
+
name: string;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Supported communicator driver types
|
|
181
|
+
*/
|
|
182
|
+
export type CommunicatorDriverType = "rabbitmq" | "kafka" | "redis-streams" | "sqs";
|
|
183
|
+
/**
|
|
184
|
+
* Events emitted by communicator drivers
|
|
185
|
+
*/
|
|
186
|
+
export type CommunicatorEvent = "connected" | "disconnected" | "error" | "reconnecting";
|
|
187
|
+
/**
|
|
188
|
+
* Listener function for communicator events
|
|
189
|
+
*/
|
|
190
|
+
export type CommunicatorEventListener = (...args: unknown[]) => void;
|
|
191
|
+
/**
|
|
192
|
+
* Result of a health check
|
|
193
|
+
*/
|
|
194
|
+
export type HealthCheckResult = {
|
|
195
|
+
/** Whether the communicator is healthy */
|
|
196
|
+
healthy: boolean;
|
|
197
|
+
/** Latency in milliseconds */
|
|
198
|
+
latency?: number;
|
|
199
|
+
/** Error message if unhealthy */
|
|
200
|
+
error?: string;
|
|
201
|
+
/** Additional details */
|
|
202
|
+
details?: Record<string, unknown>;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Base connection options shared across all drivers
|
|
206
|
+
*/
|
|
207
|
+
export type BaseConnectionOptions = {
|
|
208
|
+
/** Unique name for this communicator */
|
|
209
|
+
name?: string;
|
|
210
|
+
/** Whether this should be the default communicator */
|
|
211
|
+
isDefault?: boolean;
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Native amqplib socket options
|
|
215
|
+
* These are passed directly to the underlying socket connection
|
|
216
|
+
*/
|
|
217
|
+
export type RabbitMQSocketOptions = {
|
|
218
|
+
/** Keep-alive delay in milliseconds */
|
|
219
|
+
keepAlive?: boolean | number;
|
|
220
|
+
/** Disable Nagle's algorithm */
|
|
221
|
+
noDelay?: boolean;
|
|
222
|
+
/** Socket timeout in milliseconds */
|
|
223
|
+
timeout?: number;
|
|
224
|
+
/** Custom certificate authority (for TLS) */
|
|
225
|
+
ca?: string | Buffer | Array<string | Buffer>;
|
|
226
|
+
/** Client certificate (for TLS) */
|
|
227
|
+
cert?: string | Buffer;
|
|
228
|
+
/** Client private key (for TLS) */
|
|
229
|
+
key?: string | Buffer;
|
|
230
|
+
/** Passphrase for the private key */
|
|
231
|
+
passphrase?: string;
|
|
232
|
+
/** Server name for SNI (TLS) */
|
|
233
|
+
servername?: string;
|
|
234
|
+
/** Reject unauthorized certificates (default: true) */
|
|
235
|
+
rejectUnauthorized?: boolean;
|
|
236
|
+
};
|
|
237
|
+
/**
|
|
238
|
+
* RabbitMQ client options (native amqplib options)
|
|
239
|
+
* These are passed directly to amqplib.connect()
|
|
240
|
+
*/
|
|
241
|
+
export type RabbitMQClientOptions = {
|
|
242
|
+
/** Frame max size in bytes */
|
|
243
|
+
frameMax?: number;
|
|
244
|
+
/** Channel max (0 = unlimited) */
|
|
245
|
+
channelMax?: number;
|
|
246
|
+
/** Locale (default: 'en_US') */
|
|
247
|
+
locale?: string;
|
|
248
|
+
/** Socket options for the underlying connection */
|
|
249
|
+
socket?: RabbitMQSocketOptions;
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* RabbitMQ-specific connection options
|
|
253
|
+
*
|
|
254
|
+
* @template TClientOptions - Native amqplib client options
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* const config: RabbitMQConnectionOptions = {
|
|
259
|
+
* driver: "rabbitmq",
|
|
260
|
+
* host: "localhost",
|
|
261
|
+
* port: 5672,
|
|
262
|
+
* username: "guest",
|
|
263
|
+
* password: "guest",
|
|
264
|
+
*
|
|
265
|
+
* // Native amqplib options
|
|
266
|
+
* clientOptions: {
|
|
267
|
+
* frameMax: 0,
|
|
268
|
+
* channelMax: 0,
|
|
269
|
+
* socket: {
|
|
270
|
+
* keepAlive: true,
|
|
271
|
+
* noDelay: true,
|
|
272
|
+
* },
|
|
273
|
+
* },
|
|
274
|
+
* };
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
export type RabbitMQConnectionOptions<TClientOptions extends RabbitMQClientOptions = RabbitMQClientOptions> = BaseConnectionOptions & {
|
|
278
|
+
driver: "rabbitmq";
|
|
279
|
+
/** Host address */
|
|
280
|
+
host?: string;
|
|
281
|
+
/** Port number */
|
|
282
|
+
port?: number;
|
|
283
|
+
/** Username for authentication */
|
|
284
|
+
username?: string;
|
|
285
|
+
/** Password for authentication */
|
|
286
|
+
password?: string;
|
|
287
|
+
/** Virtual host */
|
|
288
|
+
vhost?: string;
|
|
289
|
+
/** Connection URI (alternative to host/port) */
|
|
290
|
+
uri?: string;
|
|
291
|
+
/** Heartbeat interval in seconds */
|
|
292
|
+
heartbeat?: number;
|
|
293
|
+
/** Connection timeout in milliseconds */
|
|
294
|
+
connectionTimeout?: number;
|
|
295
|
+
/** Enable automatic reconnection */
|
|
296
|
+
reconnect?: boolean;
|
|
297
|
+
/** Delay between reconnection attempts in milliseconds */
|
|
298
|
+
reconnectDelay?: number;
|
|
299
|
+
/** Default prefetch count for consumers */
|
|
300
|
+
prefetch?: number;
|
|
301
|
+
/**
|
|
302
|
+
* Native amqplib client options
|
|
303
|
+
*
|
|
304
|
+
* These options are passed directly to amqplib.connect()
|
|
305
|
+
* for low-level configuration like frame size, TLS, etc.
|
|
306
|
+
*/
|
|
307
|
+
clientOptions?: TClientOptions;
|
|
308
|
+
};
|
|
309
|
+
/**
|
|
310
|
+
* Kafka client options (native kafkajs options)
|
|
311
|
+
*/
|
|
312
|
+
export type KafkaClientOptions = {
|
|
313
|
+
/** Retry configuration */
|
|
314
|
+
retry?: {
|
|
315
|
+
initialRetryTime?: number;
|
|
316
|
+
retries?: number;
|
|
317
|
+
maxRetryTime?: number;
|
|
318
|
+
factor?: number;
|
|
319
|
+
multiplier?: number;
|
|
320
|
+
};
|
|
321
|
+
/** Log level */
|
|
322
|
+
logLevel?: number;
|
|
323
|
+
/** Custom logger */
|
|
324
|
+
logCreator?: any;
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* Kafka-specific connection options
|
|
328
|
+
*
|
|
329
|
+
* @template TClientOptions - Native kafkajs client options
|
|
330
|
+
*/
|
|
331
|
+
export type KafkaConnectionOptions<TClientOptions extends KafkaClientOptions = KafkaClientOptions> = BaseConnectionOptions & {
|
|
332
|
+
driver: "kafka";
|
|
333
|
+
/** Broker addresses */
|
|
334
|
+
brokers: string[];
|
|
335
|
+
/** Client ID */
|
|
336
|
+
clientId?: string;
|
|
337
|
+
/** Connection timeout in milliseconds */
|
|
338
|
+
connectionTimeout?: number;
|
|
339
|
+
/** Request timeout in milliseconds */
|
|
340
|
+
requestTimeout?: number;
|
|
341
|
+
/** SSL configuration */
|
|
342
|
+
ssl?: boolean | object;
|
|
343
|
+
/** SASL authentication */
|
|
344
|
+
sasl?: {
|
|
345
|
+
mechanism: "plain" | "scram-sha-256" | "scram-sha-512";
|
|
346
|
+
username: string;
|
|
347
|
+
password: string;
|
|
348
|
+
};
|
|
349
|
+
/**
|
|
350
|
+
* Native kafkajs client options
|
|
351
|
+
*/
|
|
352
|
+
clientOptions?: TClientOptions;
|
|
353
|
+
};
|
|
354
|
+
/**
|
|
355
|
+
* Union type for all connection options
|
|
356
|
+
*/
|
|
357
|
+
export type ConnectionOptions = RabbitMQConnectionOptions | KafkaConnectionOptions;
|
|
358
|
+
/**
|
|
359
|
+
* Communicator configuration options
|
|
360
|
+
*
|
|
361
|
+
* This is the main type for configuring communicators in your application.
|
|
362
|
+
* Similar to ConnectionOptions in @warlock.js/cascade.
|
|
363
|
+
*
|
|
364
|
+
* @template TClientOptions - Native client options (amqplib, kafkajs, etc.)
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```typescript
|
|
368
|
+
* // RabbitMQ with client options
|
|
369
|
+
* const config: CommunicatorConfigurations<RabbitMQClientOptions> = {
|
|
370
|
+
* driver: "rabbitmq",
|
|
371
|
+
* host: "localhost",
|
|
372
|
+
* clientOptions: {
|
|
373
|
+
* socket: { keepAlive: true },
|
|
374
|
+
* },
|
|
375
|
+
* };
|
|
376
|
+
*
|
|
377
|
+
* // Kafka
|
|
378
|
+
* const kafkaConfig: CommunicatorConfigurations<KafkaClientOptions> = {
|
|
379
|
+
* driver: "kafka",
|
|
380
|
+
* brokers: ["localhost:9092"],
|
|
381
|
+
* clientOptions: {
|
|
382
|
+
* retry: { retries: 5 },
|
|
383
|
+
* },
|
|
384
|
+
* };
|
|
385
|
+
* ```
|
|
386
|
+
*/
|
|
387
|
+
export type CommunicatorConfigurations<TClientOptions = any> = RabbitMQConnectionOptions<TClientOptions extends RabbitMQClientOptions ? TClientOptions : RabbitMQClientOptions> | KafkaConnectionOptions<TClientOptions extends KafkaClientOptions ? TClientOptions : KafkaClientOptions>;
|
|
388
|
+
/**
|
|
389
|
+
* Events emitted by the communicator registry
|
|
390
|
+
*/
|
|
391
|
+
export type CommunicatorRegistryEvent = "registered" | "default-registered" | "connected" | "disconnected";
|
|
392
|
+
/**
|
|
393
|
+
* Listener for registry events
|
|
394
|
+
*/
|
|
395
|
+
export type CommunicatorRegistryListener = (communicator: unknown) => void;
|
|
396
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAMtD;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,yCAAyC;IACzC,SAAS,EAAE,MAAM,CAAC;IAClB,mDAAmD;IACnD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wCAAwC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,2CAA2C;IAC3C,SAAS,EAAE,IAAI,CAAC;IAChB,qBAAqB;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM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GAAG,cAAc,GAAG,qBAAqB,CACtF,GACD,sBAAsB,CACpB,cAAc,SAAS,kBAAkB,GAAG,cAAc,GAAG,kBAAkB,CAChF,CAAC;AAMN;;GAEG;AACH,MAAM,MAAM,yBAAyB,GACjC,YAAY,GACZ,oBAAoB,GACpB,WAAW,GACX,cAAc,CAAC;AAEnB;;GAEG;AACH,MAAM,MAAM,4BAA4B,GAAG,CAAC,YAAY,EAAE,OAAO,KAAK,IAAI,CAAC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Communicator } from "../communicators";
|
|
2
|
+
import type { ConnectionOptions } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* Connect to a message broker and register the communicator.
|
|
5
|
+
*
|
|
6
|
+
* This is a high-level utility function that simplifies connection setup
|
|
7
|
+
* for most projects. It handles driver instantiation, connection,
|
|
8
|
+
* communicator creation, and automatic registration.
|
|
9
|
+
*
|
|
10
|
+
* **Supported Drivers:**
|
|
11
|
+
* - `rabbitmq` (default) - RabbitMQ/AMQP driver
|
|
12
|
+
* - `kafka` - Apache Kafka driver (coming soon)
|
|
13
|
+
*
|
|
14
|
+
* @param options - Connection configuration options
|
|
15
|
+
* @returns A connected and registered Communicator instance
|
|
16
|
+
* @throws {Error} If connection fails or driver is not implemented
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // RabbitMQ connection
|
|
21
|
+
* const communicator = await connectToCommunicator({
|
|
22
|
+
* driver: "rabbitmq",
|
|
23
|
+
* host: "localhost",
|
|
24
|
+
* port: 5672,
|
|
25
|
+
* username: "guest",
|
|
26
|
+
* password: "guest",
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Use the communicator
|
|
30
|
+
* await communicator.channel("user.created").publish({ userId: 1 });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Multiple communicators
|
|
36
|
+
* await connectToCommunicator({
|
|
37
|
+
* driver: "rabbitmq",
|
|
38
|
+
* name: "notifications",
|
|
39
|
+
* isDefault: true,
|
|
40
|
+
* host: process.env.RABBITMQ_HOST,
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* await connectToCommunicator({
|
|
44
|
+
* driver: "rabbitmq",
|
|
45
|
+
* name: "analytics",
|
|
46
|
+
* host: process.env.ANALYTICS_RABBITMQ_HOST,
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // Use default communicator
|
|
50
|
+
* communicators().channel("notifications").publish({ ... });
|
|
51
|
+
*
|
|
52
|
+
* // Use specific communicator
|
|
53
|
+
* communicators("analytics").channel("events").publish({ ... });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function connectToCommunicator(options: ConnectionOptions): Promise<Communicator>;
|
|
57
|
+
/**
|
|
58
|
+
* Get a communicator by name or the default one.
|
|
59
|
+
*
|
|
60
|
+
* This is the main entry point for using communicators in your application.
|
|
61
|
+
*
|
|
62
|
+
* @param name - Optional communicator name (uses default if not provided)
|
|
63
|
+
* @returns Communicator instance
|
|
64
|
+
* @throws MissingCommunicatorError if communicator not found
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* // Get default communicator
|
|
69
|
+
* const channel = communicators().channel("user.created");
|
|
70
|
+
* await channel.publish({ userId: 1 });
|
|
71
|
+
*
|
|
72
|
+
* // Get specific communicator
|
|
73
|
+
* const analyticsChannel = communicators("analytics").channel("events");
|
|
74
|
+
* await analyticsChannel.publish({ event: "page_view" });
|
|
75
|
+
*
|
|
76
|
+
* // Subscribe to messages
|
|
77
|
+
* communicators()
|
|
78
|
+
* .channel<UserPayload>("user.created")
|
|
79
|
+
* .subscribe(async (message, ctx) => {
|
|
80
|
+
* console.log("User created:", message.payload);
|
|
81
|
+
* await ctx.ack();
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function communicators(name?: string): Communicator;
|
|
86
|
+
//# sourceMappingURL=connect-to-communicator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-to-communicator.d.ts","sourceRoot":"","sources":["../../src/utils/connect-to-communicator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAwB,MAAM,kBAAkB,CAAC;AAEtE,OAAO,KAAK,EAAE,iBAAiB,EAA6B,MAAM,UAAU,CAAC;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,YAAY,CAAC,CA+C7F;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,CAEzD"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';var communicatorRegistry=require('../communicators/communicator-registry.js');/**
|
|
2
|
+
* Connect to a message broker and register the communicator.
|
|
3
|
+
*
|
|
4
|
+
* This is a high-level utility function that simplifies connection setup
|
|
5
|
+
* for most projects. It handles driver instantiation, connection,
|
|
6
|
+
* communicator creation, and automatic registration.
|
|
7
|
+
*
|
|
8
|
+
* **Supported Drivers:**
|
|
9
|
+
* - `rabbitmq` (default) - RabbitMQ/AMQP driver
|
|
10
|
+
* - `kafka` - Apache Kafka driver (coming soon)
|
|
11
|
+
*
|
|
12
|
+
* @param options - Connection configuration options
|
|
13
|
+
* @returns A connected and registered Communicator instance
|
|
14
|
+
* @throws {Error} If connection fails or driver is not implemented
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // RabbitMQ connection
|
|
19
|
+
* const communicator = await connectToCommunicator({
|
|
20
|
+
* driver: "rabbitmq",
|
|
21
|
+
* host: "localhost",
|
|
22
|
+
* port: 5672,
|
|
23
|
+
* username: "guest",
|
|
24
|
+
* password: "guest",
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Use the communicator
|
|
28
|
+
* await communicator.channel("user.created").publish({ userId: 1 });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Multiple communicators
|
|
34
|
+
* await connectToCommunicator({
|
|
35
|
+
* driver: "rabbitmq",
|
|
36
|
+
* name: "notifications",
|
|
37
|
+
* isDefault: true,
|
|
38
|
+
* host: process.env.RABBITMQ_HOST,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* await connectToCommunicator({
|
|
42
|
+
* driver: "rabbitmq",
|
|
43
|
+
* name: "analytics",
|
|
44
|
+
* host: process.env.ANALYTICS_RABBITMQ_HOST,
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* // Use default communicator
|
|
48
|
+
* communicators().channel("notifications").publish({ ... });
|
|
49
|
+
*
|
|
50
|
+
* // Use specific communicator
|
|
51
|
+
* communicators("analytics").channel("events").publish({ ... });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
async function connectToCommunicator(options) {
|
|
55
|
+
// Default values
|
|
56
|
+
const driverType = options.driver ?? "rabbitmq";
|
|
57
|
+
const communicatorName = options.name ?? "default";
|
|
58
|
+
const isDefault = options.isDefault ?? true;
|
|
59
|
+
// Create driver based on type
|
|
60
|
+
let driver;
|
|
61
|
+
switch (driverType) {
|
|
62
|
+
case "rabbitmq": {
|
|
63
|
+
const rabbitOptions = options;
|
|
64
|
+
// Dynamic import to avoid requiring amqplib if not used
|
|
65
|
+
const { RabbitMQDriver } = await Promise.resolve().then(function(){return require('../drivers/rabbitmq/rabbitmq-driver.js')});
|
|
66
|
+
driver = new RabbitMQDriver(rabbitOptions);
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case "kafka": {
|
|
70
|
+
// const kafkaOptions = options as KafkaConnectionOptions;
|
|
71
|
+
// Dynamic import to avoid requiring kafkajs if not used
|
|
72
|
+
throw new Error("Kafka driver is not yet implemented. Coming soon! For now, please use RabbitMQ.");
|
|
73
|
+
}
|
|
74
|
+
default:
|
|
75
|
+
throw new Error(`Unknown driver: "${driverType}". Supported drivers: rabbitmq, kafka`);
|
|
76
|
+
}
|
|
77
|
+
// Create communicator
|
|
78
|
+
const communicator = communicatorRegistry.communicatorRegistry.register({
|
|
79
|
+
name: communicatorName,
|
|
80
|
+
driver,
|
|
81
|
+
isDefault,
|
|
82
|
+
});
|
|
83
|
+
// Connect to the message broker
|
|
84
|
+
try {
|
|
85
|
+
await driver.connect();
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw new Error(`Failed to connect to ${driverType}: ${error instanceof Error ? error.message : String(error)}`);
|
|
89
|
+
}
|
|
90
|
+
return communicator;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Get a communicator by name or the default one.
|
|
94
|
+
*
|
|
95
|
+
* This is the main entry point for using communicators in your application.
|
|
96
|
+
*
|
|
97
|
+
* @param name - Optional communicator name (uses default if not provided)
|
|
98
|
+
* @returns Communicator instance
|
|
99
|
+
* @throws MissingCommunicatorError if communicator not found
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // Get default communicator
|
|
104
|
+
* const channel = communicators().channel("user.created");
|
|
105
|
+
* await channel.publish({ userId: 1 });
|
|
106
|
+
*
|
|
107
|
+
* // Get specific communicator
|
|
108
|
+
* const analyticsChannel = communicators("analytics").channel("events");
|
|
109
|
+
* await analyticsChannel.publish({ event: "page_view" });
|
|
110
|
+
*
|
|
111
|
+
* // Subscribe to messages
|
|
112
|
+
* communicators()
|
|
113
|
+
* .channel<UserPayload>("user.created")
|
|
114
|
+
* .subscribe(async (message, ctx) => {
|
|
115
|
+
* console.log("User created:", message.payload);
|
|
116
|
+
* await ctx.ack();
|
|
117
|
+
* });
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
function communicators(name) {
|
|
121
|
+
return communicatorRegistry.communicatorRegistry.get(name);
|
|
122
|
+
}exports.communicators=communicators;exports.connectToCommunicator=connectToCommunicator;//# sourceMappingURL=connect-to-communicator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-to-communicator.js","sources":["../../src/utils/connect-to-communicator.ts"],"sourcesContent":[null],"names":["communicatorRegistry"],"mappings":"2FAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDG;AACI,eAAe,qBAAqB,CAAC,OAA0B,EAAA;;AAEpE,IAAA,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,IAAI,UAAU,CAAC;AAChD,IAAA,MAAM,gBAAgB,GAAG,OAAO,CAAC,IAAI,IAAI,SAAS,CAAC;AACnD,IAAA,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC;;AAG5C,IAAA,IAAI,MAAkC,CAAC;AAEvC,IAAA,QAAQ,UAAU;QAChB,KAAK,UAAU,EAAE;YACf,MAAM,aAAa,GAAG,OAAoC,CAAC;;YAE3D,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,iDAAO,wCAAqC,GAAC,CAAC;AAC/E,YAAA,MAAM,GAAG,IAAI,cAAc,CAAC,aAAa,CAAC,CAAC;YAC3C,MAAM;AACP,SAAA;QAED,KAAK,OAAO,EAAE;;;AAGZ,YAAA,MAAM,IAAI,KAAK,CACb,iFAAiF,CAClF,CAAC;AACH,SAAA;AAED,QAAA;AACE,YAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,UAAU,CAAA,qCAAA,CAAuC,CAAC,CAAC;AAC1F,KAAA;;AAGD,IAAA,MAAM,YAAY,GAAGA,yCAAoB,CAAC,QAAQ,CAAC;AACjD,QAAA,IAAI,EAAE,gBAAgB;QACtB,MAAM;QACN,SAAS;AACV,KAAA,CAAC,CAAC;;IAGH,IAAI;AACF,QAA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