@opens/rabbitmq 1.0.1 → 1.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.
package/README.md CHANGED
@@ -6,14 +6,12 @@ This library is a lightweight wrapper around RabbitMQ that simplifies publishing
6
6
 
7
7
  ### Initialization
8
8
 
9
- To start using `PubSubMQ`, create an instance and initialize the connection to RabbitMQ:
9
+ To start using `rabbitmqMQ`, create an instance and initialize the connection to RabbitMQ. This must be done first:
10
10
 
11
11
  ```typescript
12
- import { connect } from 'amqp-connection-manager';
13
- import { pubsub } from './rabbitmq';
12
+ import { rabbitmq } from './rabbitmq';
14
13
 
15
-
16
- pubsub.start({
14
+ rabbitmq.start({
17
15
  hostname: 'localhost',
18
16
  port: 5672,
19
17
  username: 'guest',
@@ -27,7 +25,7 @@ pubsub.start({
27
25
  You can publish messages to a specific exchange with a routing key:
28
26
 
29
27
  ```typescript
30
- await pubsub.publish('my-exchange', 'my-routing-key', { message: 'Hello, World!' });
28
+ await rabbitmq.publish('my-exchange', 'my-routing-key', { message: 'Hello, World!' });
31
29
  ```
32
30
 
33
31
  ### Subscribing to Messages
@@ -35,15 +33,12 @@ await pubsub.publish('my-exchange', 'my-routing-key', { message: 'Hello, World!'
35
33
  To subscribe to a queue and process messages:
36
34
 
37
35
  ```typescript
38
- pubsub.subscribe({
36
+ rabbitmq.subscribe({
39
37
  name: 'my-queue',
40
38
  exchange: 'my-exchange',
41
39
  bindingKey: 'my-routing-key',
42
40
  durable: true,
43
41
  prefetch: 1,
44
- consumer: async (data, metadata) => {
45
- console.log('Received:', data);
46
- },
47
42
  up: async () => console.log('Consumer is up'),
48
43
  down: async () => console.log('Consumer is down'),
49
44
  });
@@ -60,7 +55,7 @@ The library automatically acknowledges messages if processed successfully. If an
60
55
  To set a TTL for messages in the queue:
61
56
 
62
57
  ```typescript
63
- pubsub.subscribe({
58
+ rabbitmq.subscribe({
64
59
  name: 'expiring-queue',
65
60
  exchange: 'my-exchange',
66
61
  bindingKey: 'expiring-key',
@@ -78,7 +73,7 @@ pubsub.subscribe({
78
73
  For a queue that only allows a single connection:
79
74
 
80
75
  ```typescript
81
- pubsub.subscribe({
76
+ rabbitmq.subscribe({
82
77
  name: 'exclusive-queue',
83
78
  exchange: 'my-exchange',
84
79
  bindingKey: 'exclusive-key',
package/dist/index.d.ts CHANGED
@@ -18,7 +18,7 @@ interface SubscribeParams {
18
18
  up?: ConsumerFunc;
19
19
  down?: ConsumerFunc;
20
20
  }
21
- declare class PubSubMQ {
21
+ declare class RabbitMQ {
22
22
  private startConnection;
23
23
  private connection;
24
24
  private publisherChannel;
@@ -34,6 +34,6 @@ declare class PubSubMQ {
34
34
  subscribe(params: SubscribeParams): ChannelWrapper;
35
35
  }
36
36
 
37
- declare const pubsub: PubSubMQ;
37
+ declare const rabbitmq: RabbitMQ;
38
38
 
39
- export { pubsub };
39
+ export { rabbitmq };
package/dist/index.js CHANGED
@@ -20,13 +20,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- pubsub: () => pubsub
23
+ rabbitmq: () => rabbitmq
24
24
  });
25
25
  module.exports = __toCommonJS(index_exports);
26
26
  var import_amqp_connection_manager = require("amqp-connection-manager");
27
27
 
28
28
  // src/rabbitmq.ts
29
- var PubSubMQ = class {
29
+ var RabbitMQ = class {
30
30
  constructor(startConnection) {
31
31
  this.startConnection = startConnection;
32
32
  }
@@ -79,9 +79,9 @@ var PubSubMQ = class {
79
79
  };
80
80
 
81
81
  // src/index.ts
82
- var pubsub = new PubSubMQ(import_amqp_connection_manager.connect);
82
+ var rabbitmq = new RabbitMQ(import_amqp_connection_manager.connect);
83
83
  // Annotate the CommonJS export names for ESM import in node:
84
84
  0 && (module.exports = {
85
- pubsub
85
+ rabbitmq
86
86
  });
87
87
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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,EAH9C;AAAA,EACA;AAAA,EAGD,MAAM,QAAkG;AAC7G,SAAK,aAAa,KAAK,gBAAgB,MAAM;AAC7C,SAAK,mBAAmB,KAAK,WAAW,cAAc,EAAE,MAAM,KAAK,CAAC;AAAA,EACtE;AAAA,EAEA,MAAa,QAAQ,UAAkB,YAAoB,SAAc,SAAkB;AACzF,QAAI,CAAC,KAAK,iBAAkB,OAAM,MAAM,2BAA2B;AACnE,WAAO,KAAK,iBAAiB,QAAQ,UAAU,YAAY,SAAS,OAAO;AAAA,EAC7E;AAAA,EAEO,UAAU,QAAyC;AACxD,UAAM,EAAE,UAAU,IAAI,SAAS,UAAU,WAAW,KAAK,gBAAgB,MAAM,YAAY,SAAS,IAAI;AACxG,QAAI,CAAC,KAAK,WAAY,OAAM,IAAI,MAAM,uEAAuE;AAC7G,QAAI,CAAC,YAAY,CAAC,GAAI,OAAM,IAAI,MAAM,+BAA+B;AAErE,UAAM,iBAAiC,KAAK,WAAW,cAAc;AAAA,MACnE,MAAM;AAAA,MACN,OAAO,OAAO,YAAqB;AACjC,cAAM,qBAA0B;AAAA,UAC9B;AAAA,UACA;AAAA,UACA,WAAW,CAAC;AAAA,QACd;AAEA,YAAI,IAAK,oBAAmB,UAAU,eAAe,IAAI;AACzD,YAAI,eAAgB,oBAAmB,YAAY,EAAE,GAAG,mBAAmB,WAAW,GAAG,eAAe;AAExG,cAAM,QAAQ,IAAI;AAAA,UAChB,QAAQ,eAAe,UAAU,SAAS,EAAE,SAAS,KAAK,CAAC;AAAA,UAC3D,QAAQ,YAAY,MAAM,kBAAkB;AAAA,UAC5C,QAAQ,UAAU,MAAM,UAAU,UAAU;AAAA,UAC5C,QAAQ,SAAS,YAAY,CAAC;AAAA,UAC9B,QAAQ,QAAQ,MAAM,OAAO,QAAQ;AACnC,gBAAI,CAAC,IAAK;AACV,gBAAI;AACF,oBAAM,OAAO,KAAK,MAAM,IAAI,QAAQ,SAAS,CAAC;AAC9C,oBAAM,YAAY,YAAY;AAC9B,oBAAM,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":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/rabbitmq.ts"],"sourcesContent":["import { connect } from 'amqp-connection-manager';\nimport { RabbitMQ } from './rabbitmq';\nexport const rabbitmq = new RabbitMQ(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 RabbitMQ {\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,EAH9C;AAAA,EACA;AAAA,EAGD,MAAM,QAAkG;AAC7G,SAAK,aAAa,KAAK,gBAAgB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,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,WAAW,IAAI,SAAS,sCAAO;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opens/rabbitmq",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A wrapper around common message brokers",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",