@warlock.js/herald 4.0.158 → 4.0.162
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/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 +100 -0
- package/esm/communicators/communicator.d.ts.map +1 -0
- package/esm/communicators/communicator.js +105 -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 +186 -0
- package/esm/contracts/channel.contract.d.ts.map +1 -0
- package/esm/contracts/communicator-driver.contract.d.ts +196 -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/package.json +3 -3
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { CommunicatorRegistryEvent, CommunicatorRegistryListener } from "../types";
|
|
2
|
+
import { Communicator, type CommunicatorOptions } from "./communicator";
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown when a communicator is not found
|
|
5
|
+
*/
|
|
6
|
+
export declare class MissingCommunicatorError extends Error {
|
|
7
|
+
readonly communicatorName?: string;
|
|
8
|
+
constructor(message: string, communicatorName?: string);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Communicator Registry
|
|
12
|
+
*
|
|
13
|
+
* Maintains registry of named communicators.
|
|
14
|
+
* Similar to DataSourceRegistry in @warlock.js/cascade
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Register a communicator
|
|
19
|
+
* communicatorRegistry.register({
|
|
20
|
+
* name: "default",
|
|
21
|
+
* driver: rabbitMQDriver,
|
|
22
|
+
* isDefault: true,
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Get the default communicator
|
|
26
|
+
* const comm = communicatorRegistry.get();
|
|
27
|
+
*
|
|
28
|
+
* // Get a specific communicator by name
|
|
29
|
+
* const analytics = communicatorRegistry.get("analytics");
|
|
30
|
+
*
|
|
31
|
+
* // Listen for events
|
|
32
|
+
* communicatorRegistry.on("connected", (comm) => {
|
|
33
|
+
* console.log(`${comm.name} connected`);
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare class CommunicatorRegistry {
|
|
38
|
+
private readonly sources;
|
|
39
|
+
private defaultSource?;
|
|
40
|
+
private readonly events;
|
|
41
|
+
/**
|
|
42
|
+
* Register a new communicator
|
|
43
|
+
*
|
|
44
|
+
* Sets up event forwarding from the driver to the registry.
|
|
45
|
+
*
|
|
46
|
+
* @param options - Communicator configuration
|
|
47
|
+
* @returns The registered communicator instance
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const communicator = communicatorRegistry.register({
|
|
52
|
+
* name: "primary",
|
|
53
|
+
* driver: myDriver,
|
|
54
|
+
* isDefault: true,
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
register(options: CommunicatorOptions): Communicator;
|
|
59
|
+
/**
|
|
60
|
+
* Clear all registered communicators
|
|
61
|
+
*/
|
|
62
|
+
clear(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Listen for registry events
|
|
65
|
+
*
|
|
66
|
+
* @param event - Event to listen for
|
|
67
|
+
* @param listener - Callback function
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* communicatorRegistry.on("registered", (comm) => {
|
|
72
|
+
* console.log(`Communicator "${comm.name}" registered`);
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* communicatorRegistry.on("connected", (comm) => {
|
|
76
|
+
* console.log(`Communicator "${comm.name}" connected`);
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
on(event: CommunicatorRegistryEvent, listener: CommunicatorRegistryListener): void;
|
|
81
|
+
/**
|
|
82
|
+
* Listen for a registry event once
|
|
83
|
+
*
|
|
84
|
+
* @param event - Event to listen for
|
|
85
|
+
* @param listener - Callback function
|
|
86
|
+
*/
|
|
87
|
+
once(event: CommunicatorRegistryEvent, listener: CommunicatorRegistryListener): void;
|
|
88
|
+
/**
|
|
89
|
+
* Remove an event listener
|
|
90
|
+
*
|
|
91
|
+
* @param event - Event to stop listening for
|
|
92
|
+
* @param listener - Callback to remove
|
|
93
|
+
*/
|
|
94
|
+
off(event: CommunicatorRegistryEvent, listener: CommunicatorRegistryListener): void;
|
|
95
|
+
/**
|
|
96
|
+
* Get a communicator by name or the default one
|
|
97
|
+
*
|
|
98
|
+
* @param name - Optional communicator name
|
|
99
|
+
* @returns Communicator instance
|
|
100
|
+
* @throws MissingCommunicatorError if not found
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* // Get default communicator
|
|
105
|
+
* const comm = communicatorRegistry.get();
|
|
106
|
+
*
|
|
107
|
+
* // Get specific communicator
|
|
108
|
+
* const analytics = communicatorRegistry.get("analytics");
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
get(name?: string): Communicator;
|
|
112
|
+
/**
|
|
113
|
+
* Check if a communicator exists
|
|
114
|
+
*
|
|
115
|
+
* @param name - Communicator name to check
|
|
116
|
+
* @returns True if exists
|
|
117
|
+
*/
|
|
118
|
+
has(name: string): boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Check if any communicators are registered
|
|
121
|
+
*/
|
|
122
|
+
hasAny(): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Get all registered communicators
|
|
125
|
+
*
|
|
126
|
+
* @returns Array of all communicators
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* // Disconnect all communicators
|
|
131
|
+
* for (const comm of communicatorRegistry.getAll()) {
|
|
132
|
+
* await comm.disconnect();
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
getAll(): Communicator[];
|
|
137
|
+
/**
|
|
138
|
+
* Get all communicator names
|
|
139
|
+
*
|
|
140
|
+
* @returns Array of communicator names
|
|
141
|
+
*/
|
|
142
|
+
getNames(): string[];
|
|
143
|
+
/**
|
|
144
|
+
* Get the default communicator (if any)
|
|
145
|
+
*
|
|
146
|
+
* @returns Default communicator or undefined
|
|
147
|
+
*/
|
|
148
|
+
getDefault(): Communicator | undefined;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Global communicator registry instance
|
|
152
|
+
*/
|
|
153
|
+
export declare const communicatorRegistry: CommunicatorRegistry;
|
|
154
|
+
export {};
|
|
155
|
+
//# sourceMappingURL=communicator-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"communicator-registry.d.ts","sourceRoot":"","sources":["../../src/communicators/communicator-registry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,yBAAyB,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AACxF,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAExE;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IACjD,SAAgB,gBAAgB,CAAC,EAAE,MAAM,CAAC;gBAEvB,OAAO,EAAE,MAAM,EAAE,gBAAgB,CAAC,EAAE,MAAM;CAK9D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,cAAM,oBAAoB;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAmC;IAC3D,OAAO,CAAC,aAAa,CAAC,CAAe;IACrC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsB;IAE7C;;;;;;;;;;;;;;;;OAgBG;IACI,QAAQ,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY;IA6B3D;;OAEG;IACI,KAAK,IAAI,IAAI;IAKpB;;;;;;;;;;;;;;;;OAgBG;IACI,EAAE,CAAC,KAAK,EAAE,yBAAyB,EAAE,QAAQ,EAAE,4BAA4B,GAAG,IAAI;IAIzF;;;;;OAKG;IACI,IAAI,CAAC,KAAK,EAAE,yBAAyB,EAAE,QAAQ,EAAE,4BAA4B,GAAG,IAAI;IAI3F;;;;;OAKG;IACI,GAAG,CAAC,KAAK,EAAE,yBAAyB,EAAE,QAAQ,EAAE,4BAA4B,GAAG,IAAI;IAI1F;;;;;;;;;;;;;;;OAeG;IACI,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY;IAgBvC;;;;;OAKG;IACI,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIjC;;OAEG;IACI,MAAM,IAAI,OAAO;IAIxB;;;;;;;;;;;;OAYG;IACI,MAAM,IAAI,YAAY,EAAE;IAI/B;;;;OAIG;IACI,QAAQ,IAAI,MAAM,EAAE;IAI3B;;;;OAIG;IACI,UAAU,IAAI,YAAY,GAAG,SAAS;CAG9C;AAED;;GAEG;AACH,eAAO,MAAM,oBAAoB,sBAA6B,CAAC"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import {EventEmitter}from'node:events';import {Communicator}from'./communicator.js';/**
|
|
2
|
+
* Error thrown when a communicator is not found
|
|
3
|
+
*/
|
|
4
|
+
class MissingCommunicatorError extends Error {
|
|
5
|
+
communicatorName;
|
|
6
|
+
constructor(message, communicatorName) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "MissingCommunicatorError";
|
|
9
|
+
this.communicatorName = communicatorName;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Communicator Registry
|
|
14
|
+
*
|
|
15
|
+
* Maintains registry of named communicators.
|
|
16
|
+
* Similar to DataSourceRegistry in @warlock.js/cascade
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Register a communicator
|
|
21
|
+
* communicatorRegistry.register({
|
|
22
|
+
* name: "default",
|
|
23
|
+
* driver: rabbitMQDriver,
|
|
24
|
+
* isDefault: true,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Get the default communicator
|
|
28
|
+
* const comm = communicatorRegistry.get();
|
|
29
|
+
*
|
|
30
|
+
* // Get a specific communicator by name
|
|
31
|
+
* const analytics = communicatorRegistry.get("analytics");
|
|
32
|
+
*
|
|
33
|
+
* // Listen for events
|
|
34
|
+
* communicatorRegistry.on("connected", (comm) => {
|
|
35
|
+
* console.log(`${comm.name} connected`);
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
class CommunicatorRegistry {
|
|
40
|
+
sources = new Map();
|
|
41
|
+
defaultSource;
|
|
42
|
+
events = new EventEmitter();
|
|
43
|
+
/**
|
|
44
|
+
* Register a new communicator
|
|
45
|
+
*
|
|
46
|
+
* Sets up event forwarding from the driver to the registry.
|
|
47
|
+
*
|
|
48
|
+
* @param options - Communicator configuration
|
|
49
|
+
* @returns The registered communicator instance
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const communicator = communicatorRegistry.register({
|
|
54
|
+
* name: "primary",
|
|
55
|
+
* driver: myDriver,
|
|
56
|
+
* isDefault: true,
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
register(options) {
|
|
61
|
+
const communicator = new Communicator(options);
|
|
62
|
+
this.sources.set(communicator.name, communicator);
|
|
63
|
+
const isNewDefault = communicator.isDefault || !this.defaultSource;
|
|
64
|
+
if (isNewDefault) {
|
|
65
|
+
this.defaultSource = communicator;
|
|
66
|
+
}
|
|
67
|
+
// Emit registration events
|
|
68
|
+
this.events.emit("registered", communicator);
|
|
69
|
+
if (isNewDefault) {
|
|
70
|
+
this.events.emit("default-registered", communicator);
|
|
71
|
+
}
|
|
72
|
+
// Forward driver events to registry
|
|
73
|
+
communicator.driver.on("connected", () => {
|
|
74
|
+
this.events.emit("connected", communicator);
|
|
75
|
+
});
|
|
76
|
+
communicator.driver.on("disconnected", () => {
|
|
77
|
+
this.events.emit("disconnected", communicator);
|
|
78
|
+
});
|
|
79
|
+
return communicator;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Clear all registered communicators
|
|
83
|
+
*/
|
|
84
|
+
clear() {
|
|
85
|
+
this.defaultSource = undefined;
|
|
86
|
+
this.sources.clear();
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Listen for registry events
|
|
90
|
+
*
|
|
91
|
+
* @param event - Event to listen for
|
|
92
|
+
* @param listener - Callback function
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* communicatorRegistry.on("registered", (comm) => {
|
|
97
|
+
* console.log(`Communicator "${comm.name}" registered`);
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* communicatorRegistry.on("connected", (comm) => {
|
|
101
|
+
* console.log(`Communicator "${comm.name}" connected`);
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
on(event, listener) {
|
|
106
|
+
this.events.on(event, listener);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Listen for a registry event once
|
|
110
|
+
*
|
|
111
|
+
* @param event - Event to listen for
|
|
112
|
+
* @param listener - Callback function
|
|
113
|
+
*/
|
|
114
|
+
once(event, listener) {
|
|
115
|
+
this.events.once(event, listener);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Remove an event listener
|
|
119
|
+
*
|
|
120
|
+
* @param event - Event to stop listening for
|
|
121
|
+
* @param listener - Callback to remove
|
|
122
|
+
*/
|
|
123
|
+
off(event, listener) {
|
|
124
|
+
this.events.off(event, listener);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get a communicator by name or the default one
|
|
128
|
+
*
|
|
129
|
+
* @param name - Optional communicator name
|
|
130
|
+
* @returns Communicator instance
|
|
131
|
+
* @throws MissingCommunicatorError if not found
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* // Get default communicator
|
|
136
|
+
* const comm = communicatorRegistry.get();
|
|
137
|
+
*
|
|
138
|
+
* // Get specific communicator
|
|
139
|
+
* const analytics = communicatorRegistry.get("analytics");
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
get(name) {
|
|
143
|
+
if (name !== undefined) {
|
|
144
|
+
const source = this.sources.get(name);
|
|
145
|
+
if (!source) {
|
|
146
|
+
throw new MissingCommunicatorError(`Communicator "${name}" is not registered.`, name);
|
|
147
|
+
}
|
|
148
|
+
return source;
|
|
149
|
+
}
|
|
150
|
+
if (!this.defaultSource) {
|
|
151
|
+
throw new MissingCommunicatorError("No default communicator registered.");
|
|
152
|
+
}
|
|
153
|
+
return this.defaultSource;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Check if a communicator exists
|
|
157
|
+
*
|
|
158
|
+
* @param name - Communicator name to check
|
|
159
|
+
* @returns True if exists
|
|
160
|
+
*/
|
|
161
|
+
has(name) {
|
|
162
|
+
return this.sources.has(name);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Check if any communicators are registered
|
|
166
|
+
*/
|
|
167
|
+
hasAny() {
|
|
168
|
+
return this.sources.size > 0;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Get all registered communicators
|
|
172
|
+
*
|
|
173
|
+
* @returns Array of all communicators
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* // Disconnect all communicators
|
|
178
|
+
* for (const comm of communicatorRegistry.getAll()) {
|
|
179
|
+
* await comm.disconnect();
|
|
180
|
+
* }
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
getAll() {
|
|
184
|
+
return Array.from(this.sources.values());
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get all communicator names
|
|
188
|
+
*
|
|
189
|
+
* @returns Array of communicator names
|
|
190
|
+
*/
|
|
191
|
+
getNames() {
|
|
192
|
+
return Array.from(this.sources.keys());
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Get the default communicator (if any)
|
|
196
|
+
*
|
|
197
|
+
* @returns Default communicator or undefined
|
|
198
|
+
*/
|
|
199
|
+
getDefault() {
|
|
200
|
+
return this.defaultSource;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Global communicator registry instance
|
|
205
|
+
*/
|
|
206
|
+
const communicatorRegistry = new CommunicatorRegistry();export{MissingCommunicatorError,communicatorRegistry};//# sourceMappingURL=communicator-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"communicator-registry.js","sources":["../../src/communicators/communicator-registry.ts"],"sourcesContent":[null],"names":[],"mappings":"oFAIA;;AAEG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACjC,IAAA,gBAAgB,CAAU;IAE1C,WAAmB,CAAA,OAAe,EAAE,gBAAyB,EAAA;QAC3D,KAAK,CAAC,OAAO,CAAC,CAAC;AACf,QAAA,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;AACvC,QAAA,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;KAC1C;AACF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACH,MAAM,oBAAoB,CAAA;AACP,IAAA,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;AACnD,IAAA,aAAa,CAAgB;AACpB,IAAA,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;AAE7C;;;;;;;;;;;;;;;;AAgBG;AACI,IAAA,QAAQ,CAAC,OAA4B,EAAA;AAC1C,QAAA,MAAM,YAAY,GAAG,IAAI,YAAY,CAAC,OAAO,CAAC,C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AACU,MAAA,oBAAoB,GAAG,IAAI,oBAAoB"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { CommunicatorDriverContract } from "../contracts";
|
|
2
|
+
import type { ChannelContract } from "../contracts/channel.contract";
|
|
3
|
+
import { EventMessage } from "../message-managers/event-message";
|
|
4
|
+
import { EventConsumerClass } from "../message-managers/types";
|
|
5
|
+
import type { ChannelOptions } from "../types";
|
|
6
|
+
/**
|
|
7
|
+
* Options for creating a Communicator
|
|
8
|
+
*/
|
|
9
|
+
export interface CommunicatorOptions {
|
|
10
|
+
/** Unique name for this communicator */
|
|
11
|
+
name: string;
|
|
12
|
+
/** The underlying driver */
|
|
13
|
+
driver: CommunicatorDriverContract;
|
|
14
|
+
/** Whether this is the default communicator */
|
|
15
|
+
isDefault?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Communicator - wrapper around a driver with metadata
|
|
19
|
+
*
|
|
20
|
+
* Similar to DataSource in @warlock.js/cascade
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const communicator = new Communicator({
|
|
25
|
+
* name: "default",
|
|
26
|
+
* driver: rabbitMQDriver,
|
|
27
|
+
* isDefault: true,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Get a channel
|
|
31
|
+
* const channel = communicator.channel("user.created");
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class Communicator {
|
|
35
|
+
/** Unique name identifying this communicator */
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/** The underlying driver */
|
|
38
|
+
readonly driver: CommunicatorDriverContract;
|
|
39
|
+
/** Whether this is the default communicator */
|
|
40
|
+
readonly isDefault: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Create a new Communicator
|
|
43
|
+
*
|
|
44
|
+
* @param options - Communicator configuration
|
|
45
|
+
*/
|
|
46
|
+
constructor(options: CommunicatorOptions);
|
|
47
|
+
/**
|
|
48
|
+
* Subscribe the given consumer
|
|
49
|
+
*/
|
|
50
|
+
subscribe(consumer: EventConsumerClass<any>): () => void;
|
|
51
|
+
/**
|
|
52
|
+
* Publish the given event message
|
|
53
|
+
*/
|
|
54
|
+
publish<TPayload = Record<string, any>>(event: EventMessage<TPayload>): void;
|
|
55
|
+
/**
|
|
56
|
+
* Get or create a channel
|
|
57
|
+
*
|
|
58
|
+
* @param name - Channel name
|
|
59
|
+
* @param options - Channel options
|
|
60
|
+
* @returns Channel instance
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // Simple channel
|
|
65
|
+
* const channel = communicator.channel("notifications");
|
|
66
|
+
*
|
|
67
|
+
* // Typed channel with schema
|
|
68
|
+
* const orderChannel = communicator.channel<OrderPayload>("orders", {
|
|
69
|
+
* schema: OrderSchema,
|
|
70
|
+
* durable: true,
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
channel<TPayload = unknown>(name: string, options?: ChannelOptions<TPayload>): ChannelContract<TPayload>;
|
|
75
|
+
/**
|
|
76
|
+
* Check if the communicator is connected
|
|
77
|
+
*/
|
|
78
|
+
get isConnected(): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Connect the underlying driver
|
|
81
|
+
*/
|
|
82
|
+
connect(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Disconnect the underlying driver
|
|
85
|
+
*/
|
|
86
|
+
disconnect(): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Start consuming messages
|
|
89
|
+
*/
|
|
90
|
+
startConsuming(): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Stop consuming messages
|
|
93
|
+
*/
|
|
94
|
+
stopConsuming(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Health check
|
|
97
|
+
*/
|
|
98
|
+
healthCheck(): Promise<import("../types").HealthCheckResult>;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=communicator.d.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IAAI,OAAO,CAEhC;IAED;;OAEG;IACU,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIrC;;OAEG;IACU,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxC;;OAEG;IACU,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5C;;OAEG;IACU,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3C;;OAEG;IACU,WAAW;CAGzB"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Communicator - wrapper around a driver with metadata
|
|
3
|
+
*
|
|
4
|
+
* Similar to DataSource in @warlock.js/cascade
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const communicator = new Communicator({
|
|
9
|
+
* name: "default",
|
|
10
|
+
* driver: rabbitMQDriver,
|
|
11
|
+
* isDefault: true,
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* // Get a channel
|
|
15
|
+
* const channel = communicator.channel("user.created");
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
class Communicator {
|
|
19
|
+
/** Unique name identifying this communicator */
|
|
20
|
+
name;
|
|
21
|
+
/** The underlying driver */
|
|
22
|
+
driver;
|
|
23
|
+
/** Whether this is the default communicator */
|
|
24
|
+
isDefault;
|
|
25
|
+
/**
|
|
26
|
+
* Create a new Communicator
|
|
27
|
+
*
|
|
28
|
+
* @param options - Communicator configuration
|
|
29
|
+
*/
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.name = options.name;
|
|
32
|
+
this.driver = options.driver;
|
|
33
|
+
this.isDefault = Boolean(options.isDefault);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Subscribe the given consumer
|
|
37
|
+
*/
|
|
38
|
+
subscribe(consumer) {
|
|
39
|
+
return this.driver.subscribe(consumer);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Publish the given event message
|
|
43
|
+
*/
|
|
44
|
+
publish(event) {
|
|
45
|
+
this.driver.publish(event);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get or create a channel
|
|
49
|
+
*
|
|
50
|
+
* @param name - Channel name
|
|
51
|
+
* @param options - Channel options
|
|
52
|
+
* @returns Channel instance
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // Simple channel
|
|
57
|
+
* const channel = communicator.channel("notifications");
|
|
58
|
+
*
|
|
59
|
+
* // Typed channel with schema
|
|
60
|
+
* const orderChannel = communicator.channel<OrderPayload>("orders", {
|
|
61
|
+
* schema: OrderSchema,
|
|
62
|
+
* durable: true,
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
channel(name, options) {
|
|
67
|
+
return this.driver.channel(name, options);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Check if the communicator is connected
|
|
71
|
+
*/
|
|
72
|
+
get isConnected() {
|
|
73
|
+
return this.driver.isConnected;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Connect the underlying driver
|
|
77
|
+
*/
|
|
78
|
+
async connect() {
|
|
79
|
+
await this.driver.connect();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Disconnect the underlying driver
|
|
83
|
+
*/
|
|
84
|
+
async disconnect() {
|
|
85
|
+
await this.driver.disconnect();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Start consuming messages
|
|
89
|
+
*/
|
|
90
|
+
async startConsuming() {
|
|
91
|
+
await this.driver.startConsuming();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Stop consuming messages
|
|
95
|
+
*/
|
|
96
|
+
async stopConsuming() {
|
|
97
|
+
await this.driver.stopConsuming();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Health check
|
|
101
|
+
*/
|
|
102
|
+
async healthCheck() {
|
|
103
|
+
return this.driver.healthCheck();
|
|
104
|
+
}
|
|
105
|
+
}export{Communicator};//# sourceMappingURL=communicator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"communicator.js","sources":["../../src/communicators/communicator.ts"],"sourcesContent":[null],"names":[],"mappings":"AAkBA;;;;;;;;;;;;;;;;AAgBG;MACU,YAAY,CAAA;;AAEP,IAAA,IAAI,CAAS;;AAGb,IAAA,MAAM,CAA6B;;AAGnC,IAAA,SAAS,CAAU;AAEnC;;;;AAIG;AACH,IAAA,WAAA,CAAmB,OAA4B,EAAA;AAC7C,QAAA,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;AACzB,QAAA,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;KAC7C;AAED;;AAEG;AACI,IAAA,SAAS,CAAC,QAAiC,EAAA;QAChD,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;KACxC;AAED;;AAEG;AACI,IAAA,OAAO,CAAiC,KAA6B,EAAA;AAC1E,QAAA,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;KAC5B;AAED;;;;;;;;;;;;;;;;;;AAkBG;IACI,OAAO,CACZ,IAAY,EACZ,OAAkC,EAAA;QAElC,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAW,IAAI,EAAE,OAAO,CAAC,CAAC;KACrD;AAED;;AAEG;AACH,IAAA,IAAW,WAAW,GAAA;AACpB,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;KAChC;AAED;;AAEG;AACI,IAAA,MAAM,OAAO,GAAA;AAClB,QAAA,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;KAC7B;AAED;;AAEG;AACI,IAAA,MAAM,UAAU,GAAA;AACrB,QAAA,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;KAChC;AAED;;AAEG;AACI,IAAA,MAAM,cAAc,GAAA;AACzB,QAAA,MAAM,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;KACpC;AAED;;AAEG;AACI,IAAA,MAAM,aAAa,GAAA;AACxB,QAAA,MAAM,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;KACnC;AAED;;AAEG;AACI,IAAA,MAAM,WAAW,GAAA;AACtB,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;KAClC;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/communicators/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { ChannelOptions, ChannelStats, MessageHandler, PublishOptions, RequestOptions, ResponseHandler, SubscribeOptions, Subscription } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Channel contract - represents a queue (RabbitMQ) or topic (Kafka)
|
|
4
|
+
*
|
|
5
|
+
* Provides a unified pub/sub interface across different message brokers.
|
|
6
|
+
*
|
|
7
|
+
* @template TPayload - The typed payload for messages on this channel
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Get a typed channel
|
|
12
|
+
* const userChannel = communicators().channel<UserPayload>("user.created");
|
|
13
|
+
*
|
|
14
|
+
* // Publish
|
|
15
|
+
* await userChannel.publish({ userId: 1, email: "test@example.com" });
|
|
16
|
+
*
|
|
17
|
+
* // Subscribe
|
|
18
|
+
* userChannel.subscribe(async (message, ctx) => {
|
|
19
|
+
* console.log(message.payload.userId);
|
|
20
|
+
* await ctx.ack();
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export interface ChannelContract<TPayload = unknown> {
|
|
25
|
+
/**
|
|
26
|
+
* Channel name/routing key
|
|
27
|
+
*/
|
|
28
|
+
readonly name: string;
|
|
29
|
+
/**
|
|
30
|
+
* Channel options
|
|
31
|
+
*/
|
|
32
|
+
readonly options: ChannelOptions<TPayload>;
|
|
33
|
+
/**
|
|
34
|
+
* Publish a message to this channel
|
|
35
|
+
*
|
|
36
|
+
* @param payload - The message payload
|
|
37
|
+
* @param options - Optional publish options
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* await channel.publish({ orderId: 123 });
|
|
42
|
+
*
|
|
43
|
+
* // With options
|
|
44
|
+
* await channel.publish({ orderId: 123 }, {
|
|
45
|
+
* priority: 5,
|
|
46
|
+
* persistent: true,
|
|
47
|
+
* correlationId: "req-123",
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
publish(payload: TPayload, options?: PublishOptions): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Publish multiple messages in a batch
|
|
54
|
+
*
|
|
55
|
+
* More efficient than publishing one by one.
|
|
56
|
+
*
|
|
57
|
+
* @param messages - Array of payloads to publish
|
|
58
|
+
* @param options - Optional publish options (applied to all messages)
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* await channel.publishBatch([
|
|
63
|
+
* { event: "page_view", page: "/home" },
|
|
64
|
+
* { event: "page_view", page: "/products" },
|
|
65
|
+
* ]);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
publishBatch(messages: TPayload[], options?: PublishOptions): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Subscribe to messages on this channel
|
|
71
|
+
*
|
|
72
|
+
* @param handler - Function to handle incoming messages
|
|
73
|
+
* @param options - Optional subscribe options
|
|
74
|
+
* @returns Subscription object for managing the subscription
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const subscription = await channel.subscribe(
|
|
79
|
+
* async (message, ctx) => {
|
|
80
|
+
* await processOrder(message.payload);
|
|
81
|
+
* await ctx.ack();
|
|
82
|
+
* },
|
|
83
|
+
* {
|
|
84
|
+
* prefetch: 10,
|
|
85
|
+
* retry: { maxRetries: 3, delay: 1000 },
|
|
86
|
+
* }
|
|
87
|
+
* );
|
|
88
|
+
*
|
|
89
|
+
* // Later: unsubscribe
|
|
90
|
+
* await subscription.unsubscribe();
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
subscribe(handler: MessageHandler<TPayload>, options?: SubscribeOptions): Promise<Subscription>;
|
|
94
|
+
/**
|
|
95
|
+
* Unsubscribe a consumer by its ID
|
|
96
|
+
*
|
|
97
|
+
* @param consumerId - The consumer ID to unsubscribe
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* await channel.unsubscribeById("consumer-uuid");
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
unsubscribeById(consumerId: string): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Request-Response pattern (RPC)
|
|
107
|
+
*
|
|
108
|
+
* Publishes a message and waits for a response.
|
|
109
|
+
*
|
|
110
|
+
* @param payload - The request payload
|
|
111
|
+
* @param options - Request options including timeout
|
|
112
|
+
* @returns Promise resolving to the response
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const result = await channel.request<ProcessResult>(
|
|
117
|
+
* { imageUrl: "https://..." },
|
|
118
|
+
* { timeout: 30000 }
|
|
119
|
+
* );
|
|
120
|
+
* console.log(result.processedUrl);
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
request<TResponse = unknown>(payload: TPayload, options?: RequestOptions): Promise<TResponse>;
|
|
124
|
+
/**
|
|
125
|
+
* Register a response handler for RPC pattern
|
|
126
|
+
*
|
|
127
|
+
* The return value of the handler becomes the response.
|
|
128
|
+
*
|
|
129
|
+
* @param handler - Function to handle requests and return responses
|
|
130
|
+
* @returns Subscription for managing the responder
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* channel.respond(async (message, ctx) => {
|
|
135
|
+
* const result = await processImage(message.payload);
|
|
136
|
+
* return { processedUrl: result.url };
|
|
137
|
+
* });
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
respond<TResponse = unknown>(handler: ResponseHandler<TPayload, TResponse>): Promise<Subscription>;
|
|
141
|
+
/**
|
|
142
|
+
* Get channel statistics
|
|
143
|
+
*
|
|
144
|
+
* @returns Channel stats including message count and consumer count
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const stats = await channel.stats();
|
|
149
|
+
* console.log(`Messages: ${stats.messageCount}, Consumers: ${stats.consumerCount}`);
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
stats(): Promise<ChannelStats>;
|
|
153
|
+
/**
|
|
154
|
+
* Purge all messages from the channel
|
|
155
|
+
*
|
|
156
|
+
* Use with caution - this deletes all pending messages.
|
|
157
|
+
*
|
|
158
|
+
* @returns Number of messages purged
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const purgedCount = await channel.purge();
|
|
163
|
+
* console.log(`Purged ${purgedCount} messages`);
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
purge(): Promise<number>;
|
|
167
|
+
/**
|
|
168
|
+
* Check if the channel exists
|
|
169
|
+
*
|
|
170
|
+
* @returns True if the channel exists on the broker
|
|
171
|
+
*/
|
|
172
|
+
exists(): Promise<boolean>;
|
|
173
|
+
/**
|
|
174
|
+
* Delete the channel
|
|
175
|
+
*
|
|
176
|
+
* Use with caution - this removes the queue/topic entirely.
|
|
177
|
+
*/
|
|
178
|
+
delete(): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Assert/create the channel with its options
|
|
181
|
+
*
|
|
182
|
+
* Creates the channel if it doesn't exist, or verifies options match.
|
|
183
|
+
*/
|
|
184
|
+
assert(): Promise<void>;
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=channel.contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.contract.d.ts","sourceRoot":"","sources":["../../src/contracts/channel.contract.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EACZ,cAAc,EACd,cAAc,EACd,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACb,MAAM,UAAU,CAAC;AAElB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,eAAe,CAAC,QAAQ,GAAG,OAAO;IACjD;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC;IAE3C;;;;;;;;;;;;;;;;;OAiBG;IACH,OAAO,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEpE;;;;;;;;;;;;;;;OAeG;IACH,YAAY,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5E;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,SAAS,CAAC,OAAO,EAAE,cAAc,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAEhG;;;;;;;;;OASG;IACH,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnD;;;;;;;;;;;;;;;;;OAiBG;IACH,OAAO,CAAC,SAAS,GAAG,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAE9F;;;;;;;;;;;;;;;OAeG;IACH,OAAO,CAAC,SAAS,GAAG,OAAO,EACzB,OAAO,EAAE,eAAe,CAAC,QAAQ,EAAE,SAAS,CAAC,GAC5C,OAAO,CAAC,YAAY,CAAC,CAAC;IAEzB;;;;;;;;;;OAUG;IACH,KAAK,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC;IAE/B;;;;;;;;;;;;OAYG;IACH,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAEzB;;;;OAIG;IACH,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAE3B;;;;OAIG;IACH,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;;;OAIG;IACH,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { type EventMessage } from "../message-managers/event-message";
|
|
2
|
+
import { EventConsumerClass } from "../message-managers/types";
|
|
3
|
+
import type { ChannelOptions, CommunicatorDriverType, CommunicatorEvent, CommunicatorEventListener, HealthCheckResult } from "../types";
|
|
4
|
+
import type { ChannelContract } from "./channel.contract";
|
|
5
|
+
/**
|
|
6
|
+
* Communicator Driver Contract
|
|
7
|
+
*
|
|
8
|
+
* Base contract for all message bus drivers (RabbitMQ, Kafka, etc.)
|
|
9
|
+
* Similar to DriverContract in @warlock.js/cascade
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Driver implementation
|
|
14
|
+
* class RabbitMQDriver implements CommunicatorDriverContract {
|
|
15
|
+
* readonly name = "rabbitmq";
|
|
16
|
+
* // ...
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export interface CommunicatorDriverContract {
|
|
21
|
+
/**
|
|
22
|
+
* Driver name identifier
|
|
23
|
+
*
|
|
24
|
+
* @example "rabbitmq", "kafka", "redis-streams"
|
|
25
|
+
*/
|
|
26
|
+
readonly name: CommunicatorDriverType;
|
|
27
|
+
/**
|
|
28
|
+
* Whether currently connected to the message broker
|
|
29
|
+
*/
|
|
30
|
+
readonly isConnected: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Establish connection to the message broker
|
|
33
|
+
*
|
|
34
|
+
* @throws Error if connection fails
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* await driver.connect();
|
|
39
|
+
* console.log("Connected to RabbitMQ");
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
connect(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Close connection gracefully
|
|
45
|
+
*
|
|
46
|
+
* Ensures all pending operations complete before disconnecting.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* await driver.disconnect();
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
disconnect(): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Register event listeners for driver lifecycle events
|
|
56
|
+
*
|
|
57
|
+
* @param event - Event name to listen for
|
|
58
|
+
* @param listener - Callback function
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* driver.on("connected", () => {
|
|
63
|
+
* console.log("Connected to broker");
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* driver.on("disconnected", () => {
|
|
67
|
+
* console.log("Disconnected from broker");
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* driver.on("error", (error) => {
|
|
71
|
+
* console.error("Driver error:", error);
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* driver.on("reconnecting", (attempt) => {
|
|
75
|
+
* console.log(`Reconnection attempt ${attempt}`);
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
on(event: CommunicatorEvent, listener: CommunicatorEventListener): void;
|
|
80
|
+
/**
|
|
81
|
+
* Remove an event listener
|
|
82
|
+
*
|
|
83
|
+
* @param event - Event name
|
|
84
|
+
* @param listener - Callback to remove
|
|
85
|
+
*/
|
|
86
|
+
off(event: CommunicatorEvent, listener: CommunicatorEventListener): void;
|
|
87
|
+
/**
|
|
88
|
+
* Subscribe the given consumer class to the driver
|
|
89
|
+
*
|
|
90
|
+
* @param consumer - Consumer class to subscribe
|
|
91
|
+
* @returns Unsubscribe function
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* driver.subscribe(UserUpdatedConsumer);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
subscribe(consumer: EventConsumerClass): () => void;
|
|
98
|
+
/**
|
|
99
|
+
* Unsubscribe the given consumer class from the driver
|
|
100
|
+
*
|
|
101
|
+
* @param consumer - Consumer class to unsubscribe
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* driver.unsubscribe(UserUpdatedConsumer);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
unsubscribe(consumer: EventConsumerClass): void;
|
|
109
|
+
/**
|
|
110
|
+
* Publish the given event message
|
|
111
|
+
*/
|
|
112
|
+
publish<TPayload = Record<string, any>>(event: EventMessage<TPayload>): void;
|
|
113
|
+
/**
|
|
114
|
+
* Get or create a channel
|
|
115
|
+
*
|
|
116
|
+
* Channels are lazy-created and cached for reuse.
|
|
117
|
+
*
|
|
118
|
+
* @param name - Channel/queue/topic name
|
|
119
|
+
* @param options - Channel configuration
|
|
120
|
+
* @returns Channel instance
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* // Simple channel
|
|
125
|
+
* const channel = driver.channel("user.created");
|
|
126
|
+
*
|
|
127
|
+
* // With options
|
|
128
|
+
* const orderChannel = driver.channel("orders", {
|
|
129
|
+
* durable: true,
|
|
130
|
+
* deadLetter: { channel: "orders.failed" },
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* // Typed channel
|
|
134
|
+
* const typedChannel = driver.channel<OrderPayload>("orders", {
|
|
135
|
+
* schema: OrderSchema,
|
|
136
|
+
* });
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
channel<TPayload = unknown>(name: string, options?: ChannelOptions<TPayload>): ChannelContract<TPayload>;
|
|
140
|
+
/**
|
|
141
|
+
* Start consuming messages from all subscribed channels
|
|
142
|
+
*
|
|
143
|
+
* Call this after setting up all subscriptions to begin processing.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* // Set up subscriptions
|
|
148
|
+
* channel1.subscribe(handler1);
|
|
149
|
+
* channel2.subscribe(handler2);
|
|
150
|
+
*
|
|
151
|
+
* // Start consuming
|
|
152
|
+
* await driver.startConsuming();
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
startConsuming(): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
* Stop consuming messages gracefully
|
|
158
|
+
*
|
|
159
|
+
* Waits for currently processing messages to complete.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* await driver.stopConsuming();
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
stopConsuming(): Promise<void>;
|
|
167
|
+
/**
|
|
168
|
+
* Perform a health check on the connection
|
|
169
|
+
*
|
|
170
|
+
* @returns Health check result with status and optional latency
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const health = await driver.healthCheck();
|
|
175
|
+
* if (health.healthy) {
|
|
176
|
+
* console.log(`Healthy, latency: ${health.latency}ms`);
|
|
177
|
+
* } else {
|
|
178
|
+
* console.error(`Unhealthy: ${health.error}`);
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
healthCheck(): Promise<HealthCheckResult>;
|
|
183
|
+
/**
|
|
184
|
+
* Get list of all channels managed by this driver
|
|
185
|
+
*
|
|
186
|
+
* @returns Array of channel names
|
|
187
|
+
*/
|
|
188
|
+
getChannelNames(): string[];
|
|
189
|
+
/**
|
|
190
|
+
* Close and remove a specific channel
|
|
191
|
+
*
|
|
192
|
+
* @param name - Channel name to close
|
|
193
|
+
*/
|
|
194
|
+
closeChannel(name: string): Promise<void>;
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=communicator-driver.contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"communicator-driver.contract.d.ts","sourceRoot":"","sources":["../../src/contracts/communicator-driver.contract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EACtB,iBAAiB,EACjB,yBAAyB,EACzB,iBAAiB,EAClB,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE1D;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;IAEtC;;OAEG;IACH,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAE9B;;;;;;;;;;OAUG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB;;;;;;;;;OASG;IACH,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,EAAE,CAAC,KAAK,EAAE,iBAAiB,EAAE,QAAQ,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAExE;;;;;OAKG;IACH,GAAG,CAAC,KAAK,EAAE,iBAAiB,EAAE,QAAQ,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAEzE;;;;;;;;;OASG;IACH,SAAS,CAAC,QAAQ,EAAE,kBAAkB,GAAG,MAAM,IAAI,CAAC;IAEpD;;;;;;;;;OASG;IACH,WAAW,CAAC,QAAQ,EAAE,kBAAkB,GAAG,IAAI,CAAC;IAEhD;;OAEG;IACH,OAAO,CAAC,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,YAAY,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC;IAE7E;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,OAAO,CAAC,QAAQ,GAAG,OAAO,EACxB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,cAAc,CAAC,QAAQ,CAAC,GACjC,eAAe,CAAC,QAAQ,CAAC,CAAC;IAE7B;;;;;;;;;;;;;;OAcG;IACH,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhC;;;;;;;;;OASG;IACH,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE/B;;;;;;;;;;;;;;OAcG;IACH,WAAW,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAE1C;;;;OAIG;IACH,eAAe,IAAI,MAAM,EAAE,CAAC;IAE5B;;;;OAIG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/contracts/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@warlock.js/herald",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.162",
|
|
4
4
|
"description": "Message bus/communicators for RabbitMQ, Kafka, and more",
|
|
5
5
|
"main": "./cjs/index.js",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@mongez/copper": "^1.0.1",
|
|
8
8
|
"@mongez/events": "^2.1.0",
|
|
9
9
|
"@mongez/reinforcements": "^2.3.17",
|
|
10
|
-
"@warlock.js/logger": "4.0.
|
|
11
|
-
"@warlock.js/seal": "4.0.
|
|
10
|
+
"@warlock.js/logger": "4.0.162",
|
|
11
|
+
"@warlock.js/seal": "4.0.162"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
14
|
"update": "npx ncu -u",
|