amqplib-init 1.2.1 → 1.2.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.
@@ -57,6 +57,13 @@ class MessageProcessor {
57
57
 
58
58
  this.processingMessages.add(deliveryTag);
59
59
 
60
+ // 检查消息内容有效性,对无效消息直接确认
61
+ if (!this._isValidMessageContent(msg)) {
62
+ log.log(`⚠️ 收到无效消息 ${deliveryTag},内容为空或无法解析,自动确认以防止阻塞`);
63
+ this._acknowledgeMessage(msg, channel, deliveryTag, 0); // 立即确认无效消息
64
+ return;
65
+ }
66
+
60
67
  try {
61
68
  const content = JSON.parse(msg.content.toString());
62
69
  log.log(`🪴 队列收到消息: ${JSON.stringify(content)}`);
@@ -79,6 +86,41 @@ class MessageProcessor {
79
86
  }
80
87
  }
81
88
 
89
+ /**
90
+ * 检查消息内容是否有效
91
+ * @param {Object} msg - RabbitMQ消息对象
92
+ * @returns {boolean} 消息内容是否有效
93
+ * @private
94
+ */
95
+ _isValidMessageContent(msg) {
96
+ try {
97
+ // 检查消息对象是否存在
98
+ if (!msg || !msg.content) {
99
+ return false;
100
+ }
101
+
102
+ // 获取消息内容字符串
103
+ const contentStr = msg.content.toString();
104
+
105
+ // 检查内容是否为空或只包含空白字符
106
+ if (!contentStr || contentStr.trim() === '') {
107
+ return false;
108
+ }
109
+
110
+ // 检查是否为null字符串
111
+ if (contentStr.toLowerCase() === 'null' || contentStr.toLowerCase() === 'undefined') {
112
+ return false;
113
+ }
114
+
115
+ // 尝试解析JSON,如果失败则认为无效
116
+ JSON.parse(contentStr);
117
+ return true;
118
+ } catch (error) {
119
+ // JSON解析失败,认为消息无效
120
+ return false;
121
+ }
122
+ }
123
+
82
124
  /**
83
125
  * 确认消息
84
126
  * @param {Object} msg - RabbitMQ消息对象
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amqplib-init",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "消息队列初始化 - 模块化架构版本 (支持15分钟长任务)",
5
5
  "main": "index.js",
6
6
  "scripts": {