@palmetto/pubsub 1.0.4 → 1.0.6
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 +8 -0
- package/dist/rabbitmq/config.d.ts +7 -1
- package/dist/rabbitmq/publisher.d.ts +2 -0
- package/dist/rabbitmq/publisher.js +28 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -92,6 +92,14 @@ yarn add @palmetto/pubsub zod
|
|
|
92
92
|
await subscriber.startSubscribe(schemaConfig, onMessage);
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
+
### RabbitMQ Usage
|
|
96
|
+
|
|
97
|
+
For specific details about using RabbitMQ see [RabbitMQ README.md](src/rabbitmq/README.md)
|
|
98
|
+
|
|
99
|
+
### BullMQ Usage
|
|
100
|
+
|
|
101
|
+
For specific details about using BullMQ see [BullMQ README.md](src/bullmq/README.md)
|
|
102
|
+
|
|
95
103
|
### Defining schemas
|
|
96
104
|
|
|
97
105
|
All schemas must be defined using `zod/z4`. By convention, all messages should extend the `IdMetaSchema` schema, but it is not strictly required.
|
|
@@ -45,9 +45,15 @@ export interface RabbitQueueExchangeConfiguration extends PubSubConfiguration {
|
|
|
45
45
|
*/
|
|
46
46
|
queueType?: QueueType;
|
|
47
47
|
/**
|
|
48
|
-
* Override default queue & exchange names
|
|
48
|
+
* Override default queue & exchange names.
|
|
49
49
|
*/
|
|
50
50
|
overrides?: RabbitQueueExchangeCustomConfiguration;
|
|
51
|
+
/**
|
|
52
|
+
* Publish to a specific queue by name. Messages will be sent directly to the queue instead of going through an exchange. The queue must already exist.
|
|
53
|
+
*
|
|
54
|
+
* This is useful for topic exchanges where multiple queues exist for different subscribers, but you want to publish to only one of those queues.
|
|
55
|
+
*/
|
|
56
|
+
publishToSpecificQueue?: string;
|
|
51
57
|
}
|
|
52
58
|
export interface RabbitQueueExchangeNames {
|
|
53
59
|
queueName?: string;
|
|
@@ -25,5 +25,7 @@ export declare class RabbitMqPublisher implements PublisherProvider {
|
|
|
25
25
|
* @returns A promise that is completed when the message is published
|
|
26
26
|
*/
|
|
27
27
|
publish(config: RabbitQueueExchangeConfiguration, message: string): Promise<void>;
|
|
28
|
+
private publishToExchange;
|
|
29
|
+
private publishToQueue;
|
|
28
30
|
close(): Promise<void> | undefined;
|
|
29
31
|
}
|
|
@@ -80,21 +80,46 @@ class RabbitMqPublisher {
|
|
|
80
80
|
*/
|
|
81
81
|
publish(config, message) {
|
|
82
82
|
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
-
var _a, _b, _c, _d;
|
|
84
83
|
const channel = yield this.getChannel(config);
|
|
85
|
-
|
|
84
|
+
if (config.publishToSpecificQueue) {
|
|
85
|
+
yield this.publishToQueue(channel, config.publishToSpecificQueue, message);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
yield this.publishToExchange(channel, config, message);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
publishToExchange(channel, config, message) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
var _a, _b, _c, _d;
|
|
86
95
|
const exchangeName = (0, config_js_1.getExchangeName)(config);
|
|
96
|
+
(_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `Publishing message to ${exchangeName} - ${message} [starting]`);
|
|
87
97
|
const ok = yield channel.publish(exchangeName, (0, config_js_1.getRoutingKey)(config), Buffer.from(message, "utf8"), {
|
|
88
98
|
contentType: "application/json",
|
|
89
99
|
timestamp: new Date().valueOf(),
|
|
90
100
|
persistent: true,
|
|
91
101
|
});
|
|
92
|
-
(_d = (_c = this.logger).debug) === null || _d === void 0 ? void 0 : _d.call(_c, `Published message to ${
|
|
102
|
+
(_d = (_c = this.logger).debug) === null || _d === void 0 ? void 0 : _d.call(_c, `Published message to ${exchangeName} - ${message} [${ok}]`);
|
|
93
103
|
if (!ok) {
|
|
94
104
|
throw new errors_js_1.PublishError(`RabbitMq publish to ${exchangeName} failed`);
|
|
95
105
|
}
|
|
96
106
|
});
|
|
97
107
|
}
|
|
108
|
+
publishToQueue(channel, queue, message) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
var _a, _b, _c, _d;
|
|
111
|
+
(_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `Publishing message to queue:${queue} - ${message} [starting]`);
|
|
112
|
+
const ok = yield channel.sendToQueue(queue, Buffer.from(message, "utf8"), {
|
|
113
|
+
contentType: "application/json",
|
|
114
|
+
timestamp: new Date().valueOf(),
|
|
115
|
+
persistent: true,
|
|
116
|
+
});
|
|
117
|
+
(_d = (_c = this.logger).debug) === null || _d === void 0 ? void 0 : _d.call(_c, `Published message to queue:${queue} - ${message} [${ok}]`);
|
|
118
|
+
if (!ok) {
|
|
119
|
+
throw new errors_js_1.PublishError(`RabbitMq publish to queue:${queue} failed`);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
98
123
|
close() {
|
|
99
124
|
var _a;
|
|
100
125
|
return (_a = this.channel) === null || _a === void 0 ? void 0 : _a.close();
|