@palmetto/pubsub 1.0.3 → 1.0.5

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.
@@ -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
- (_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `Publishing message to ${(0, config_js_1.getExchangeName)(config)} - ${message} [starting]`);
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 ${(0, config_js_1.getExchangeName)(config)} - ${message} [${ok}]`);
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();
@@ -20,7 +20,17 @@ export declare class Subscriber {
20
20
  private readonly subscribedMessages;
21
21
  constructor(logger: Logger, providers?: SubscriberProvider[]);
22
22
  on<TU extends keyof SubscriberEvents>(event: TU, listener: SubscriberEvents[TU]): this;
23
- subscribe<TMessage extends BaseMessage>(config: PubSubConfiguration, onMessage: (msg: TMessage, context: MessageContext) => Promise<MessageResult> | MessageResult): Promise<void>;
23
+ /**
24
+ * Starts the subscriber for the given configuration. Depending on the protocol, the subscriber may not be started immediately.
25
+ *
26
+ * @param config The pubsub configuration to use for this subscriber
27
+ * @param onMessage The function to execute when a message arrives
28
+ * @returns An async function that will unsubscribe the subscriber when called
29
+ */
30
+ subscribe<TMessage extends BaseMessage>(config: PubSubConfiguration, onMessage: (msg: TMessage, context: MessageContext) => Promise<MessageResult> | MessageResult): Promise<() => Promise<void>>;
31
+ /**
32
+ * Unsubscribes all active subscribers.
33
+ */
24
34
  unsubscribe(): Promise<void>;
25
35
  addProvider(provider: SubscriberProvider): void;
26
36
  removeProvider(providerOrTransport: SubscriberProvider | string): boolean;
@@ -32,6 +32,13 @@ class Subscriber {
32
32
  this.events.on(event, listener);
33
33
  return this;
34
34
  }
35
+ /**
36
+ * Starts the subscriber for the given configuration. Depending on the protocol, the subscriber may not be started immediately.
37
+ *
38
+ * @param config The pubsub configuration to use for this subscriber
39
+ * @param onMessage The function to execute when a message arrives
40
+ * @returns An async function that will unsubscribe the subscriber when called
41
+ */
35
42
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
36
43
  subscribe(config, onMessage) {
37
44
  return __awaiter(this, void 0, void 0, function* () {
@@ -98,8 +105,17 @@ class Subscriber {
98
105
  subscribedMessage = new SubscribedMessage(yield provider.startSubscribe(config, transform));
99
106
  this.subscribedMessages.set(config, subscribedMessage);
100
107
  this.logger.log(`Started subscriber for ${config.transport}:${config.name}`);
108
+ return () => __awaiter(this, void 0, void 0, function* () {
109
+ this.logger.log(`Stopping subscriber for ${config.transport}:${config.name}`);
110
+ this.subscribedMessages.delete(config);
111
+ yield subscribedMessage.stop();
112
+ this.logger.log(`Stopped subscriber for ${config.transport}:${config.name}`);
113
+ });
101
114
  });
102
115
  }
116
+ /**
117
+ * Unsubscribes all active subscribers.
118
+ */
103
119
  unsubscribe() {
104
120
  return __awaiter(this, void 0, void 0, function* () {
105
121
  this.logger.log(`Stopping all ${this.subscribedMessages.size} subscribers`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@palmetto/pubsub",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "main": "./dist/main.js",
5
5
  "scripts": {
6
6
  "lint": "yarn run -T eslint --fix ./src",