@opens/rabbitmq 1.1.2 → 1.1.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/dist/index.js CHANGED
@@ -35,7 +35,13 @@ var RabbitMQ = class {
35
35
  publisherChannel;
36
36
  telemetry;
37
37
  start(params) {
38
- this.connection = this.startConnection(params);
38
+ this.connection = this.startConnection(params).on("connect", () => {
39
+ console.log("[@opens/rabbitmq] Connected to RabbitMQ");
40
+ }).on("connectFailed", (err) => {
41
+ console.error("[@opens/rabbitmq] Failed to connect to RabbitMQ:", err);
42
+ }).on("disconnect", (err) => {
43
+ console.error("[@opens/rabbitmq] Disconnected from RabbitMQ:", err);
44
+ });
39
45
  this.publisherChannel = this.connection.createChannel({ json: true });
40
46
  }
41
47
  async publish(exchange, routingKey, payload, options) {
@@ -77,6 +83,11 @@ var RabbitMQ = class {
77
83
  channel.prefetch(prefetch || 1),
78
84
  channel.consume(name, instrumentedHandler ?? fn)
79
85
  ]);
86
+ console.log(
87
+ `[@opens/rabbitmq] Subscribed to ${name} on exchange ${exchange} with binding keys: ${bindingKeys.join(
88
+ ", "
89
+ )}`
90
+ );
80
91
  }
81
92
  });
82
93
  return channelWrapper;
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 { RabbitMQ } from './rabbitmq';\nexport const rabbitmq = new RabbitMQ(connect);\n","import { TRACE_TYPE } from '@opens/observability';\nimport { BaseTracer } from '@opens/observability/dist/tracers/_base-tracer';\nimport { AmqpConnectionManager, connect, ChannelWrapper } from 'amqp-connection-manager';\nimport { Channel, ConsumeMessage, 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 | 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 private telemetry!: BaseTracer | undefined;\n\n constructor(private startConnection: typeof connect) {}\n public start(params: {\n hostname: string;\n password: string;\n protocol: string;\n username: string;\n port: number;\n telemetry?: BaseTracer;\n }) {\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 const fn = async (msg: ConsumeMessage | null) => {\n if (!msg) return;\n try {\n const data = JSON.parse(msg.content.toString());\n const processor = consumer ?? up;\n if (processor) 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 const instrumentedHandler = this.telemetry?.instrumentFunc({ name: params.name, type: TRACE_TYPE.MESSAGE }, fn);\n\n const bindingKeys = Array.isArray(bindingKey) ? bindingKey : [bindingKey];\n\n await Promise.all([\n channel.assertExchange(exchange, 'topic', { durable: true }),\n channel.assertQueue(name, assertQueueOptions),\n ...bindingKeys.map(key => channel.bindQueue(name, exchange, key)),\n channel.prefetch(prefetch || 1),\n channel.consume(name, instrumentedHandler ?? fn),\n ]);\n },\n });\n return channelWrapper;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qCAAwB;;;ACAxB,2BAA2B;AA2BpB,IAAM,WAAN,MAAe;AAAA,EAKpB,YAAoB,iBAAiC;AAAjC;AAAA,EAAkC;AAAA,EAJ9C;AAAA,EACA;AAAA,EACA;AAAA,EAGD,MAAM,QAOV;AACD,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,KAAK,OAAO,QAA+B;AAC/C,cAAI,CAAC,IAAK;AACV,cAAI;AACF,kBAAM,OAAO,KAAK,MAAM,IAAI,QAAQ,SAAS,CAAC;AAC9C,kBAAM,YAAY,YAAY;AAC9B,gBAAI,UAAW,OAAM,UAAU,MAAM,EAAE,QAAQ,IAAI,QAAQ,YAAY,IAAI,WAAW,CAAC;AACvF,oBAAQ,IAAI,GAAG;AAAA,UACjB,SAAS,OAAO;AACd,oBAAQ,KAAK,KAAK,OAAO,KAAK;AAAA,UAChC,UAAE;AAAA,UACF;AAAA,QACF;AAEA,cAAM,sBAAsB,KAAK,WAAW,eAAe,EAAE,MAAM,OAAO,MAAM,MAAM,gCAAW,QAAQ,GAAG,EAAE;AAE9G,cAAM,cAAc,MAAM,QAAQ,UAAU,IAAI,aAAa,CAAC,UAAU;AAExE,cAAM,QAAQ,IAAI;AAAA,UAChB,QAAQ,eAAe,UAAU,SAAS,EAAE,SAAS,KAAK,CAAC;AAAA,UAC3D,QAAQ,YAAY,MAAM,kBAAkB;AAAA,UAC5C,GAAG,YAAY,IAAI,SAAO,QAAQ,UAAU,MAAM,UAAU,GAAG,CAAC;AAAA,UAChE,QAAQ,SAAS,YAAY,CAAC;AAAA,UAC9B,QAAQ,QAAQ,MAAM,uBAAuB,EAAE;AAAA,QACjD,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AACF;;;AD7FO,IAAM,WAAW,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 { TRACE_TYPE } from '@opens/observability';\nimport { BaseTracer } from '@opens/observability/dist/tracers/_base-tracer';\nimport { AmqpConnectionManager, connect, ChannelWrapper } from 'amqp-connection-manager';\nimport { Channel, ConsumeMessage, 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 | 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 private telemetry!: BaseTracer | undefined;\n\n constructor(private startConnection: typeof connect) {}\n public start(params: {\n hostname: string;\n password: string;\n protocol: string;\n username: string;\n port: number;\n telemetry?: BaseTracer;\n }) {\n this.connection = this.startConnection(params)\n .on('connect', () => {\n console.log('[@opens/rabbitmq] Connected to RabbitMQ');\n })\n .on('connectFailed', (err) => {\n console.error('[@opens/rabbitmq] Failed to connect to RabbitMQ:', err);\n })\n .on('disconnect', (err) => {\n console.error('[@opens/rabbitmq] Disconnected from RabbitMQ:', err);\n });\n\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 const fn = async (msg: ConsumeMessage | null) => {\n if (!msg) return;\n try {\n const data = JSON.parse(msg.content.toString());\n const processor = consumer ?? up;\n if (processor) 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 const instrumentedHandler = this.telemetry?.instrumentFunc({ name: params.name, type: TRACE_TYPE.MESSAGE }, fn);\n\n const bindingKeys = Array.isArray(bindingKey) ? bindingKey : [bindingKey];\n\n await Promise.all([\n channel.assertExchange(exchange, 'topic', { durable: true }),\n channel.assertQueue(name, assertQueueOptions),\n ...bindingKeys.map((key) => channel.bindQueue(name, exchange, key)),\n channel.prefetch(prefetch || 1),\n channel.consume(name, instrumentedHandler ?? fn),\n ]);\n\n console.log(\n `[@opens/rabbitmq] Subscribed to ${name} on exchange ${exchange} with binding keys: ${bindingKeys.join(\n ', ',\n )}`,\n );\n },\n });\n return channelWrapper;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qCAAwB;;;ACAxB,2BAA2B;AA2BpB,IAAM,WAAN,MAAe;AAAA,EAKpB,YAAoB,iBAAiC;AAAjC;AAAA,EAAkC;AAAA,EAJ9C;AAAA,EACA;AAAA,EACA;AAAA,EAGD,MAAM,QAOV;AACD,SAAK,aAAa,KAAK,gBAAgB,MAAM,EAC1C,GAAG,WAAW,MAAM;AACnB,cAAQ,IAAI,yCAAyC;AAAA,IACvD,CAAC,EACA,GAAG,iBAAiB,CAAC,QAAQ;AAC5B,cAAQ,MAAM,oDAAoD,GAAG;AAAA,IACvE,CAAC,EACA,GAAG,cAAc,CAAC,QAAQ;AACzB,cAAQ,MAAM,iDAAiD,GAAG;AAAA,IACpE,CAAC;AAEH,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,YA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AAQ,QAAQ,UAAU,MAAM,UAAU,GAAG,CAAC;AAAA,UAClE,QAAQ,SAAS,YAAY,CAAC;AAAA,UAC9B,QAAQ,QAAQ,MAAM,uBAAuB,EAAE;AAAA,QACjD,CAAC;AAED,gBAAQ;AAAA,UACN,mCAAmC,IAAI,gBAAgB,QAAQ,uBAAuB,YAAY;AAAA,YAChG;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AACF;;;AD7GO,IAAM,WAAW,IAAI,SAAS,sCAAO;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opens/rabbitmq",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "A wrapper around common message brokers",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",