@jsnw/nestjs-rabbitmq 2.0.2 → 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.
|
@@ -88,34 +88,29 @@ class RabbitmqSubscriber {
|
|
|
88
88
|
* @private
|
|
89
89
|
*/
|
|
90
90
|
onMessage = async (message) => {
|
|
91
|
-
if (!message.body || (typeof message.body !== 'string' && !Buffer.isBuffer(message.body)))
|
|
91
|
+
if (!message.body || (typeof message.body !== 'string' && typeof message.body !== 'object' && !Buffer.isBuffer(message.body)))
|
|
92
92
|
return rabbitmq_client_1.ConsumerStatus.DROP;
|
|
93
|
-
|
|
94
|
-
? message.body.toString('utf-8')
|
|
95
|
-
: typeof message.body === 'string'
|
|
96
|
-
? message.body
|
|
97
|
-
: null;
|
|
98
|
-
if (!messageBody)
|
|
99
|
-
return rabbitmq_client_1.ConsumerStatus.DROP;
|
|
100
|
-
const isJSON = message.contentType === 'application/json';
|
|
101
|
-
let data;
|
|
93
|
+
let payload = null;
|
|
102
94
|
try {
|
|
103
|
-
|
|
95
|
+
payload = this.getMessageBody(message);
|
|
104
96
|
}
|
|
105
97
|
catch (e) {
|
|
106
|
-
|
|
98
|
+
if (e instanceof Error && e.name === 'SyntaxError')
|
|
99
|
+
this.logger.error('Failed to parse message body as JSON');
|
|
107
100
|
return rabbitmq_client_1.ConsumerStatus.DROP;
|
|
108
101
|
}
|
|
109
102
|
if (!!this.params.validation?.schema) {
|
|
110
|
-
const { data: parsed, error, success } = this.params.validation.schema.safeParse(
|
|
111
|
-
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)}`);
|
|
112
106
|
return (0, rabbitmq_helpers_1.mapRabbitmqResponseToConsumerStatus)(this.params.validation.onFail ?? 'drop');
|
|
113
|
-
|
|
107
|
+
}
|
|
108
|
+
payload = parsed;
|
|
114
109
|
}
|
|
115
110
|
try {
|
|
116
111
|
const response = await (typeof this.subscriber === 'function'
|
|
117
|
-
? this.subscriber(
|
|
118
|
-
: this.subscriber.instance[this.subscriber.methodName](
|
|
112
|
+
? this.subscriber(payload, message)
|
|
113
|
+
: this.subscriber.instance[this.subscriber.methodName](payload, message));
|
|
119
114
|
return (0, rabbitmq_helpers_1.mapRabbitmqResponseToConsumerStatus)(response);
|
|
120
115
|
}
|
|
121
116
|
catch (e) {
|
|
@@ -127,5 +122,30 @@ class RabbitmqSubscriber {
|
|
|
127
122
|
return !!this.params.requeue ? rabbitmq_client_1.ConsumerStatus.REQUEUE : rabbitmq_client_1.ConsumerStatus.DROP;
|
|
128
123
|
}
|
|
129
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
|
+
}
|
|
130
150
|
}
|
|
131
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
|
-
?
|
|
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
|
}
|