@opens/rabbitmq 1.1.0 → 1.1.2

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 CHANGED
@@ -14,7 +14,7 @@ interface SubscribeParams {
14
14
  prefetch?: number;
15
15
  queueArguments?: Record<string, any>;
16
16
  exclusive?: boolean;
17
- bindingKey: string;
17
+ bindingKey: string | string[];
18
18
  consumer?: ConsumerFunc;
19
19
  up?: ConsumerFunc;
20
20
  down?: ConsumerFunc;
package/dist/index.js CHANGED
@@ -45,7 +45,7 @@ var RabbitMQ = class {
45
45
  subscribe(params) {
46
46
  const { consumer, up, durable, exchange, exclusive, ttl, queueArguments, name, bindingKey, prefetch } = params;
47
47
  if (!this.connection) throw new Error("Rabbit MQ hasn't started yet and won't be able to subscribe to events");
48
- if (!consumer || !up) throw new Error("No consumer function provided");
48
+ if (!consumer && !up) throw new Error("No consumer function provided");
49
49
  const channelWrapper = this.connection.createChannel({
50
50
  json: true,
51
51
  setup: async (channel) => {
@@ -60,8 +60,8 @@ var RabbitMQ = class {
60
60
  if (!msg) return;
61
61
  try {
62
62
  const data = JSON.parse(msg.content.toString());
63
- const processor = consumer || up;
64
- await processor(data, { fields: msg.fields, properties: msg.properties });
63
+ const processor = consumer ?? up;
64
+ if (processor) await processor(data, { fields: msg.fields, properties: msg.properties });
65
65
  channel.ack(msg);
66
66
  } catch (error) {
67
67
  channel.nack(msg, false, false);
@@ -69,10 +69,11 @@ var RabbitMQ = class {
69
69
  }
70
70
  };
71
71
  const instrumentedHandler = this.telemetry?.instrumentFunc({ name: params.name, type: import_observability.TRACE_TYPE.MESSAGE }, fn);
72
+ const bindingKeys = Array.isArray(bindingKey) ? bindingKey : [bindingKey];
72
73
  await Promise.all([
73
74
  channel.assertExchange(exchange, "topic", { durable: true }),
74
75
  channel.assertQueue(name, assertQueueOptions),
75
- channel.bindQueue(name, exchange, bindingKey),
76
+ ...bindingKeys.map((key) => channel.bindQueue(name, exchange, key)),
76
77
  channel.prefetch(prefetch || 1),
77
78
  channel.consume(name, instrumentedHandler ?? fn)
78
79
  ]);
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;\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 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 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, 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,M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,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 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,IA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C;AAAA,MACH;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AACF;;;AD7FO,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.0",
3
+ "version": "1.1.2",
4
4
  "description": "A wrapper around common message brokers",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",