@swarnami/rabbitmq-core 1.0.1 → 2.0.0
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.d.ts +5 -3
- package/dist/index.js +38 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,10 @@ type RabbitConnectionOptions = {
|
|
|
4
4
|
servername?: string;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
-
declare function
|
|
7
|
+
declare function initRabbitMQ(options?: RabbitConnectionOptions): void;
|
|
8
8
|
|
|
9
|
-
declare function
|
|
9
|
+
declare function publishMessage(queue: string, payload: unknown): Promise<void>;
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
declare function consumeMessage(queue: string, handler: (data: any) => Promise<void>): Promise<void>;
|
|
12
|
+
|
|
13
|
+
export { consumeMessage, initRabbitMQ, publishMessage };
|
package/dist/index.js
CHANGED
|
@@ -6899,13 +6899,13 @@ var require_connect = __commonJS({
|
|
|
6899
6899
|
user = url.username || "";
|
|
6900
6900
|
pass = url.password || "";
|
|
6901
6901
|
}
|
|
6902
|
-
var
|
|
6902
|
+
var config2 = {
|
|
6903
6903
|
locale: url.locale,
|
|
6904
6904
|
channelMax: url.channelMax,
|
|
6905
6905
|
frameMax: url.frameMax,
|
|
6906
6906
|
heartbeat: url.heartbeat
|
|
6907
6907
|
};
|
|
6908
|
-
fields = openFrames(url.vhost,
|
|
6908
|
+
fields = openFrames(url.vhost, config2, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
|
|
6909
6909
|
} else {
|
|
6910
6910
|
var parts = URL(url, true);
|
|
6911
6911
|
var host = parts.hostname.replace(/^\[|\]$/g, "");
|
|
@@ -7945,17 +7945,44 @@ var require_channel_api = __commonJS({
|
|
|
7945
7945
|
var index_exports = {};
|
|
7946
7946
|
__export(index_exports, {
|
|
7947
7947
|
consumeMessage: () => consumeMessage,
|
|
7948
|
+
initRabbitMQ: () => initRabbitMQ,
|
|
7948
7949
|
publishMessage: () => publishMessage
|
|
7949
7950
|
});
|
|
7950
7951
|
module.exports = __toCommonJS(index_exports);
|
|
7951
7952
|
|
|
7953
|
+
// src/config.ts
|
|
7954
|
+
var config = null;
|
|
7955
|
+
function setRabbitConfig(options) {
|
|
7956
|
+
config = options;
|
|
7957
|
+
}
|
|
7958
|
+
function getRabbitConfig() {
|
|
7959
|
+
if (!config) {
|
|
7960
|
+
throw new Error(
|
|
7961
|
+
"RabbitMQ not initialized. Call initRabbitMQ() first."
|
|
7962
|
+
);
|
|
7963
|
+
}
|
|
7964
|
+
return config;
|
|
7965
|
+
}
|
|
7966
|
+
|
|
7967
|
+
// src/init.ts
|
|
7968
|
+
function initRabbitMQ(options) {
|
|
7969
|
+
setRabbitConfig(
|
|
7970
|
+
options ?? {
|
|
7971
|
+
url: process.env.RABBITMQ_URL,
|
|
7972
|
+
caPath: process.env.RABBITMQ_CA_PATH,
|
|
7973
|
+
servername: process.env.RABBITMQ_SERVERNAME
|
|
7974
|
+
}
|
|
7975
|
+
);
|
|
7976
|
+
}
|
|
7977
|
+
|
|
7952
7978
|
// src/connection/rabbit.connection.ts
|
|
7953
7979
|
var amqp = __toESM(require_channel_api());
|
|
7954
7980
|
var import_fs = __toESM(require("fs"));
|
|
7955
7981
|
var connection = null;
|
|
7956
7982
|
var channel = null;
|
|
7957
|
-
async function getRabbitChannel(
|
|
7983
|
+
async function getRabbitChannel() {
|
|
7958
7984
|
if (channel) return channel;
|
|
7985
|
+
const options = getRabbitConfig();
|
|
7959
7986
|
const socketOptions = options.url.startsWith("amqps://") ? {
|
|
7960
7987
|
ca: options.caPath ? [import_fs.default.readFileSync(options.caPath)] : void 0,
|
|
7961
7988
|
servername: options.servername,
|
|
@@ -7981,19 +8008,17 @@ async function close() {
|
|
|
7981
8008
|
}
|
|
7982
8009
|
|
|
7983
8010
|
// src/producer/producer.ts
|
|
7984
|
-
async function publishMessage(
|
|
7985
|
-
const channel2 = await getRabbitChannel(
|
|
8011
|
+
async function publishMessage(queue, payload) {
|
|
8012
|
+
const channel2 = await getRabbitChannel();
|
|
7986
8013
|
await channel2.assertQueue(queue, { durable: true });
|
|
7987
|
-
channel2.sendToQueue(
|
|
7988
|
-
|
|
7989
|
-
|
|
7990
|
-
{ persistent: true }
|
|
7991
|
-
);
|
|
8014
|
+
channel2.sendToQueue(queue, Buffer.from(JSON.stringify(payload)), {
|
|
8015
|
+
persistent: true
|
|
8016
|
+
});
|
|
7992
8017
|
}
|
|
7993
8018
|
|
|
7994
8019
|
// src/consumer/consumer.ts
|
|
7995
|
-
async function consumeMessage(
|
|
7996
|
-
const channel2 = await getRabbitChannel(
|
|
8020
|
+
async function consumeMessage(queue, handler) {
|
|
8021
|
+
const channel2 = await getRabbitChannel();
|
|
7997
8022
|
await channel2.assertQueue(queue, { durable: true });
|
|
7998
8023
|
channel2.consume(queue, async (msg) => {
|
|
7999
8024
|
if (!msg) return;
|
|
@@ -8009,6 +8034,7 @@ async function consumeMessage(options, queue, handler) {
|
|
|
8009
8034
|
// Annotate the CommonJS export names for ESM import in node:
|
|
8010
8035
|
0 && (module.exports = {
|
|
8011
8036
|
consumeMessage,
|
|
8037
|
+
initRabbitMQ,
|
|
8012
8038
|
publishMessage
|
|
8013
8039
|
});
|
|
8014
8040
|
/*! Bundled license information:
|