@kaushverse/rabbitmq-core 1.0.6 → 1.0.8
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/dist/index.js +27 -17
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -61,39 +61,50 @@ function loadCA(caPath) {
|
|
|
61
61
|
// src/connection.ts
|
|
62
62
|
var connection = null;
|
|
63
63
|
var channel = null;
|
|
64
|
-
var connected = false;
|
|
65
64
|
var rabbitEvents = new import_events.EventEmitter();
|
|
65
|
+
rabbitEvents.setMaxListeners(20);
|
|
66
66
|
async function connectRabbitMQ(options) {
|
|
67
|
-
if (
|
|
67
|
+
if (connection && channel) return channel;
|
|
68
68
|
try {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
const isSecure = options.url.startsWith("amqps://");
|
|
70
|
+
if (!isSecure && options.tls) {
|
|
71
|
+
console.warn("\u26A0\uFE0F TLS options ignored for amqp:// connection");
|
|
72
|
+
}
|
|
73
|
+
const socketOptions = isSecure ? {
|
|
74
|
+
ca: loadCA(options.tls?.caPath),
|
|
72
75
|
servername: options.tls?.servername,
|
|
73
76
|
rejectUnauthorized: options.tls?.rejectUnauthorized ?? true
|
|
74
|
-
}
|
|
77
|
+
} : void 0;
|
|
78
|
+
connection = await import_amqplib.default.connect(options.url, socketOptions);
|
|
75
79
|
channel = await connection.createChannel();
|
|
76
|
-
connected = true;
|
|
77
80
|
rabbitEvents.emit("connected");
|
|
78
81
|
connection.on("close", () => {
|
|
79
|
-
|
|
82
|
+
connection = null;
|
|
80
83
|
channel = null;
|
|
81
84
|
rabbitEvents.emit("disconnected");
|
|
82
85
|
});
|
|
83
86
|
connection.on("error", (err) => {
|
|
84
|
-
|
|
87
|
+
rabbitEvents.emit("error", err);
|
|
88
|
+
});
|
|
89
|
+
channel.on("close", () => {
|
|
90
|
+
channel = null;
|
|
91
|
+
});
|
|
92
|
+
channel.on("error", (err) => {
|
|
85
93
|
rabbitEvents.emit("error", err);
|
|
86
94
|
});
|
|
87
95
|
return channel;
|
|
88
96
|
} catch (err) {
|
|
89
|
-
|
|
97
|
+
connection = null;
|
|
98
|
+
channel = null;
|
|
90
99
|
rabbitEvents.emit("error", err);
|
|
91
100
|
throw err;
|
|
92
101
|
}
|
|
93
102
|
}
|
|
94
103
|
function getChannel() {
|
|
95
104
|
if (!channel) {
|
|
96
|
-
throw new Error(
|
|
105
|
+
throw new Error(
|
|
106
|
+
"\u274C RabbitMQ channel not available. Call connectRabbitMQ()"
|
|
107
|
+
);
|
|
97
108
|
}
|
|
98
109
|
return channel;
|
|
99
110
|
}
|
|
@@ -104,12 +115,11 @@ async function closeRabbitMQ() {
|
|
|
104
115
|
} finally {
|
|
105
116
|
channel = null;
|
|
106
117
|
connection = null;
|
|
107
|
-
connected = false;
|
|
108
118
|
rabbitEvents.emit("disconnected");
|
|
109
119
|
}
|
|
110
120
|
}
|
|
111
121
|
function isRabbitConnected() {
|
|
112
|
-
return
|
|
122
|
+
return Boolean(connection && channel);
|
|
113
123
|
}
|
|
114
124
|
|
|
115
125
|
// src/consumer.ts
|
|
@@ -231,7 +241,7 @@ async function bootstrap({
|
|
|
231
241
|
process.on("SIGTERM", shutdown);
|
|
232
242
|
} catch (err) {
|
|
233
243
|
logger.error("\u{1F4A5} Startup failed", {
|
|
234
|
-
error: err instanceof Error ? err
|
|
244
|
+
error: err instanceof Error ? err : err
|
|
235
245
|
});
|
|
236
246
|
process.exit(1);
|
|
237
247
|
}
|
|
@@ -239,12 +249,12 @@ async function bootstrap({
|
|
|
239
249
|
|
|
240
250
|
// src/health/index.ts
|
|
241
251
|
function rabbitHealth() {
|
|
242
|
-
const
|
|
252
|
+
const connected = isRabbitConnected();
|
|
243
253
|
return {
|
|
244
254
|
"\u{1F407}": {
|
|
245
255
|
service: "rabbitmq",
|
|
246
|
-
status:
|
|
247
|
-
healthy:
|
|
256
|
+
status: connected ? "\u{1F7E2} up" : "\u{1F534} down",
|
|
257
|
+
healthy: connected
|
|
248
258
|
}
|
|
249
259
|
};
|
|
250
260
|
}
|