@jsnw/nestjs-rabbitmq 2.0.1 → 2.0.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.
@@ -46,6 +46,16 @@ class RabbitmqSubscriber {
46
46
  this._isActive = !params.autoStart;
47
47
  this.consumer = this.connection.createConsumer({
48
48
  queue: params.queue.name,
49
+ queueOptions: {
50
+ queue: params.queue.name,
51
+ durable: params.queue.durable,
52
+ autoDelete: params.queue.autoDelete,
53
+ arguments: {
54
+ ...params.queue.arguments,
55
+ 'x-dead-letter-exchange': params.queue.deadLetterExchange?.name ?? undefined,
56
+ 'x-dead-letter-routing-key': params.queue.deadLetterRoutingKey ?? undefined
57
+ }
58
+ },
49
59
  requeue: !!params.requeue,
50
60
  qos: {
51
61
  prefetchSize: params.prefetchSize ?? 0,
@@ -78,34 +88,29 @@ class RabbitmqSubscriber {
78
88
  * @private
79
89
  */
80
90
  onMessage = async (message) => {
81
- if (!message.body || (typeof message.body !== 'string' && !Buffer.isBuffer(message.body)))
82
- return rabbitmq_client_1.ConsumerStatus.DROP;
83
- const messageBody = Buffer.isBuffer(message.body)
84
- ? message.body.toString('utf-8')
85
- : typeof message.body === 'string'
86
- ? message.body
87
- : null;
88
- if (!messageBody)
91
+ if (!message.body || (typeof message.body !== 'string' && typeof message.body !== 'object' && !Buffer.isBuffer(message.body)))
89
92
  return rabbitmq_client_1.ConsumerStatus.DROP;
90
- const isJSON = message.contentType === 'application/json';
91
- let data;
93
+ let payload = null;
92
94
  try {
93
- data = isJSON ? JSON.parse(messageBody) : messageBody;
95
+ payload = this.getMessageBody(message);
94
96
  }
95
97
  catch (e) {
96
- this.logger.error(`Failed to parse message body as JSON`);
98
+ if (e instanceof Error && e.name === 'SyntaxError')
99
+ this.logger.error('Failed to parse message body as JSON');
97
100
  return rabbitmq_client_1.ConsumerStatus.DROP;
98
101
  }
99
102
  if (!!this.params.validation?.schema) {
100
- const { data: parsed, error, success } = this.params.validation.schema.safeParse(data);
101
- if (error || !success)
103
+ const { data: parsed, error, success } = this.params.validation.schema.safeParse(payload);
104
+ if (error || !success) {
105
+ this.logger.error(`Failed to validate message. Error:\n${zod_1.z.prettifyError(error)}`);
102
106
  return (0, rabbitmq_helpers_1.mapRabbitmqResponseToConsumerStatus)(this.params.validation.onFail ?? 'drop');
103
- data = parsed;
107
+ }
108
+ payload = parsed;
104
109
  }
105
110
  try {
106
111
  const response = await (typeof this.subscriber === 'function'
107
- ? this.subscriber(data, message)
108
- : this.subscriber.instance[this.subscriber.methodName](data, message));
112
+ ? this.subscriber(payload, message)
113
+ : this.subscriber.instance[this.subscriber.methodName](payload, message));
109
114
  return (0, rabbitmq_helpers_1.mapRabbitmqResponseToConsumerStatus)(response);
110
115
  }
111
116
  catch (e) {
@@ -117,5 +122,30 @@ class RabbitmqSubscriber {
117
122
  return !!this.params.requeue ? rabbitmq_client_1.ConsumerStatus.REQUEUE : rabbitmq_client_1.ConsumerStatus.DROP;
118
123
  }
119
124
  };
125
+ /**
126
+ * @param {AsyncMessage} message
127
+ * @return {string | object | null}
128
+ * @private
129
+ */
130
+ getMessageBody(message) {
131
+ if (!message.body || (typeof message.body !== 'string'
132
+ && typeof message.body !== 'object'
133
+ && !Buffer.isBuffer(message.body)))
134
+ return null;
135
+ if (message.contentType === 'application/json') {
136
+ if (Buffer.isBuffer(message.body) || typeof message.body === 'string') {
137
+ const jsonString = Buffer.isBuffer(message.body)
138
+ ? message.body.toString('utf-8')
139
+ : message.body;
140
+ return JSON.parse(jsonString);
141
+ }
142
+ return message.body;
143
+ }
144
+ else {
145
+ if (Buffer.isBuffer(message.body))
146
+ return message.body.toString('utf-8');
147
+ return message.body;
148
+ }
149
+ }
120
150
  }
121
151
  exports.RabbitmqSubscriber = RabbitmqSubscriber;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Rabbitmq = void 0;
4
+ const node_crypto_1 = require("node:crypto");
4
5
  const rabbitmq_client_1 = require("rabbitmq-client");
5
6
  const common_1 = require("@nestjs/common");
6
7
  const rabbitmq_subscriber_1 = require("./rabbitmq-subscriber");
@@ -119,7 +120,7 @@ class Rabbitmq {
119
120
  */
120
121
  async subscribe(params, subscriber) {
121
122
  const id = params.id ?? (typeof subscriber === 'function'
122
- ? Math.random().toString()
123
+ ? (0, node_crypto_1.randomUUID)()
123
124
  : subscriber.instance.constructor.name + '.' + subscriber.methodName);
124
125
  await this.declareQueue(params.queue);
125
126
  if (this.subscribers.has(id))
@@ -40,4 +40,10 @@ export declare class RabbitmqSubscriber {
40
40
  * @private
41
41
  */
42
42
  protected onMessage: (message: AsyncMessage) => Promise<ConsumerStatus>;
43
+ /**
44
+ * @param {AsyncMessage} message
45
+ * @return {string | object | null}
46
+ * @private
47
+ */
48
+ private getMessageBody;
43
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsnw/nestjs-rabbitmq",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "NestJS module for RabbitMQ integration",
5
5
  "main": "./dist/lib/index.js",
6
6
  "types": "./dist/types/index.d.ts",