@opens/rabbitmq 1.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.js ADDED
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ pubsub: () => pubsub
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var import_amqp_connection_manager = require("amqp-connection-manager");
27
+
28
+ // src/rabbitmq.ts
29
+ var PubSubMQ = class {
30
+ constructor(startConnection) {
31
+ this.startConnection = startConnection;
32
+ }
33
+ connection;
34
+ publisherChannel;
35
+ start(params) {
36
+ this.connection = this.startConnection(params);
37
+ this.publisherChannel = this.connection.createChannel({ json: true });
38
+ }
39
+ async publish(exchange, routingKey, payload, options) {
40
+ if (!this.publisherChannel) throw Error("No publisher channel open");
41
+ return this.publisherChannel.publish(exchange, routingKey, payload, options);
42
+ }
43
+ subscribe(params) {
44
+ const { consumer, up, durable, exchange, exclusive, ttl, queueArguments, name, bindingKey, prefetch } = params;
45
+ if (!this.connection) throw new Error("Rabbit MQ hasn't started yet and won't be able to subscribe to events");
46
+ if (!consumer || !up) throw new Error("No consumer function provided");
47
+ const channelWrapper = this.connection.createChannel({
48
+ json: true,
49
+ setup: async (channel) => {
50
+ const assertQueueOptions = {
51
+ durable,
52
+ exclusive,
53
+ arguments: {}
54
+ };
55
+ if (ttl) assertQueueOptions.arguments["x-message-ttl"] = ttl;
56
+ if (queueArguments) assertQueueOptions.arguments = { ...assertQueueOptions.arguments, ...queueArguments };
57
+ await Promise.all([
58
+ channel.assertExchange(exchange, "topic", { durable: true }),
59
+ channel.assertQueue(name, assertQueueOptions),
60
+ channel.bindQueue(name, exchange, bindingKey),
61
+ channel.prefetch(prefetch || 1),
62
+ channel.consume(name, async (msg) => {
63
+ if (!msg) return;
64
+ try {
65
+ const data = JSON.parse(msg.content.toString());
66
+ const processor = consumer || up;
67
+ await processor(data, { fields: msg.fields, properties: msg.properties });
68
+ channel.ack(msg);
69
+ } catch (error) {
70
+ channel.nack(msg, false, false);
71
+ } finally {
72
+ }
73
+ })
74
+ ]);
75
+ }
76
+ });
77
+ return channelWrapper;
78
+ }
79
+ };
80
+
81
+ // src/index.ts
82
+ var pubsub = new PubSubMQ(import_amqp_connection_manager.connect);
83
+ // Annotate the CommonJS export names for ESM import in node:
84
+ 0 && (module.exports = {
85
+ pubsub
86
+ });
87
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/rabbitmq.ts"],"sourcesContent":["import { connect } from 'amqp-connection-manager';\nimport { PubSubMQ } from './rabbitmq';\nexport const pubsub = new PubSubMQ(connect);\n","import { AmqpConnectionManager, connect, ChannelWrapper } from 'amqp-connection-manager';\nimport { Channel, MessageFields, MessageProperties } from 'amqplib';\n\ntype ConsumerFunc = (\n data: any,\n metadata: {\n fields: MessageFields;\n properties: MessageProperties;\n },\n) => Promise<void>;\n\nexport interface SubscribeParams {\n ttl?: number;\n name: string;\n exchange: string;\n durable?: boolean;\n prefetch?: number;\n queueArguments?: Record<string, any>;\n exclusive?: boolean;\n bindingKey: string;\n consumer?: ConsumerFunc;\n up?: ConsumerFunc;\n down?: ConsumerFunc;\n}\n\nexport class PubSubMQ {\n private connection: AmqpConnectionManager | undefined;\n private publisherChannel: ChannelWrapper | undefined;\n\n constructor(private startConnection: typeof connect) {}\n public start(params: { hostname: string; password: string; protocol: string; username: string; port: number }) {\n this.connection = this.startConnection(params);\n this.publisherChannel = this.connection.createChannel({ json: true });\n }\n\n public async publish(exchange: string, routingKey: string, payload: any, options?: Object) {\n if (!this.publisherChannel) throw Error('No publisher channel open');\n return this.publisherChannel.publish(exchange, routingKey, payload, options);\n }\n\n public subscribe(params: SubscribeParams): ChannelWrapper {\n const { consumer, up, durable, exchange, exclusive, ttl, queueArguments, name, bindingKey, prefetch } = params;\n if (!this.connection) throw new Error(\"Rabbit MQ hasn't started yet and won't be able to subscribe to events\");\n if (!consumer || !up) throw new Error('No consumer function provided');\n\n const channelWrapper: ChannelWrapper = this.connection.createChannel({\n json: true,\n setup: async (channel: Channel) => {\n const assertQueueOptions: any = {\n durable,\n exclusive,\n arguments: {},\n };\n\n if (ttl) assertQueueOptions.arguments['x-message-ttl'] = ttl;\n if (queueArguments) assertQueueOptions.arguments = { ...assertQueueOptions.arguments, ...queueArguments };\n\n await Promise.all([\n channel.assertExchange(exchange, 'topic', { durable: true }),\n channel.assertQueue(name, assertQueueOptions),\n channel.bindQueue(name, exchange, bindingKey),\n channel.prefetch(prefetch || 1),\n channel.consume(name, async (msg) => {\n if (!msg) return;\n try {\n const data = JSON.parse(msg.content.toString());\n const processor = consumer || up;\n await processor(data, { fields: msg.fields, properties: msg.properties });\n channel.ack(msg);\n } catch (error) {\n channel.nack(msg, false, false);\n } finally {\n }\n }),\n ]);\n },\n });\n return channelWrapper;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qCAAwB;;;ACyBjB,IAAM,WAAN,MAAe;AAAA,EAIpB,YAAoB,iBAAiC;AAAjC;AAAA,EAAkC;AAAA,EAH9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AM,UAAU,MAAM,EAAE,QAAQ,IAAI,QAAQ,YAAY,IAAI,WAAW,CAAC;AACxE,sBAAQ,IAAI,GAAG;AAAA,YACjB,SAAS,OAAO;AACd,sBAAQ,KAAK,KAAK,OAAO,KAAK;AAAA,YAChC,UAAE;AAAA,YACF;AAAA,UACF,CAAC;AAAA,QACH,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AACF;;;AD7EO,IAAM,SAAS,IAAI,SAAS,sCAAO;","names":[]}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@opens/rabbitmq",
3
+ "version": "1.0.0",
4
+ "description": "A wrapper around common message brokers",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup",
13
+ "dev": "tsup --watch",
14
+ "test": "vitest run",
15
+ "test:watch": "vitest"
16
+ },
17
+ "tsup": {
18
+ "entry": [
19
+ "src/index.ts"
20
+ ],
21
+ "splitting": false,
22
+ "sourcemap": true,
23
+ "clean": true
24
+ },
25
+ "author": "Joao Victor Clementino",
26
+ "license": "ISC",
27
+ "devDependencies": {
28
+ "@types/amqplib": "^0.10.6",
29
+ "tsup": "^8.3.5",
30
+ "typescript": "^5.7.3",
31
+ "vitest": "^3.0.4"
32
+ },
33
+ "private": false,
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "dependencies": {
38
+ "amqp-connection-manager": "^4.1.14",
39
+ "amqplib": "^0.10.5"
40
+ }
41
+ }