@opens/rabbitmq 1.0.2 → 1.1.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/README.md +7 -7
- package/dist/index.d.ts +6 -3
- package/dist/index.js +20 -16
- package/dist/index.js.map +1 -1
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -6,12 +6,12 @@ This library is a lightweight wrapper around RabbitMQ that simplifies publishing
|
|
|
6
6
|
|
|
7
7
|
### Initialization
|
|
8
8
|
|
|
9
|
-
To start using `
|
|
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 {
|
|
12
|
+
import { rabbitmq } from './rabbitmq';
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
rabbitmq.start({
|
|
15
15
|
hostname: 'localhost',
|
|
16
16
|
port: 5672,
|
|
17
17
|
username: 'guest',
|
|
@@ -25,7 +25,7 @@ pubsub.start({
|
|
|
25
25
|
You can publish messages to a specific exchange with a routing key:
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
|
-
await
|
|
28
|
+
await rabbitmq.publish('my-exchange', 'my-routing-key', { message: 'Hello, World!' });
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
### Subscribing to Messages
|
|
@@ -33,7 +33,7 @@ await pubsub.publish('my-exchange', 'my-routing-key', { message: 'Hello, World!'
|
|
|
33
33
|
To subscribe to a queue and process messages:
|
|
34
34
|
|
|
35
35
|
```typescript
|
|
36
|
-
|
|
36
|
+
rabbitmq.subscribe({
|
|
37
37
|
name: 'my-queue',
|
|
38
38
|
exchange: 'my-exchange',
|
|
39
39
|
bindingKey: 'my-routing-key',
|
|
@@ -55,7 +55,7 @@ The library automatically acknowledges messages if processed successfully. If an
|
|
|
55
55
|
To set a TTL for messages in the queue:
|
|
56
56
|
|
|
57
57
|
```typescript
|
|
58
|
-
|
|
58
|
+
rabbitmq.subscribe({
|
|
59
59
|
name: 'expiring-queue',
|
|
60
60
|
exchange: 'my-exchange',
|
|
61
61
|
bindingKey: 'expiring-key',
|
|
@@ -73,7 +73,7 @@ pubsub.subscribe({
|
|
|
73
73
|
For a queue that only allows a single connection:
|
|
74
74
|
|
|
75
75
|
```typescript
|
|
76
|
-
|
|
76
|
+
rabbitmq.subscribe({
|
|
77
77
|
name: 'exclusive-queue',
|
|
78
78
|
exchange: 'my-exchange',
|
|
79
79
|
bindingKey: 'exclusive-key',
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BaseTracer } from '@opens/observability/dist/tracers/_base-tracer';
|
|
1
2
|
import { connect, ChannelWrapper } from 'amqp-connection-manager';
|
|
2
3
|
import { MessageFields, MessageProperties } from 'amqplib';
|
|
3
4
|
|
|
@@ -18,10 +19,11 @@ interface SubscribeParams {
|
|
|
18
19
|
up?: ConsumerFunc;
|
|
19
20
|
down?: ConsumerFunc;
|
|
20
21
|
}
|
|
21
|
-
declare class
|
|
22
|
+
declare class RabbitMQ {
|
|
22
23
|
private startConnection;
|
|
23
24
|
private connection;
|
|
24
25
|
private publisherChannel;
|
|
26
|
+
private telemetry;
|
|
25
27
|
constructor(startConnection: typeof connect);
|
|
26
28
|
start(params: {
|
|
27
29
|
hostname: string;
|
|
@@ -29,11 +31,12 @@ declare class PubSubMQ {
|
|
|
29
31
|
protocol: string;
|
|
30
32
|
username: string;
|
|
31
33
|
port: number;
|
|
34
|
+
telemetry?: BaseTracer;
|
|
32
35
|
}): void;
|
|
33
36
|
publish(exchange: string, routingKey: string, payload: any, options?: Object): Promise<boolean>;
|
|
34
37
|
subscribe(params: SubscribeParams): ChannelWrapper;
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
declare const
|
|
40
|
+
declare const rabbitmq: RabbitMQ;
|
|
38
41
|
|
|
39
|
-
export {
|
|
42
|
+
export { rabbitmq };
|
package/dist/index.js
CHANGED
|
@@ -20,18 +20,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
|
|
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
|
|
29
|
+
var import_observability = require("@opens/observability");
|
|
30
|
+
var RabbitMQ = class {
|
|
30
31
|
constructor(startConnection) {
|
|
31
32
|
this.startConnection = startConnection;
|
|
32
33
|
}
|
|
33
34
|
connection;
|
|
34
35
|
publisherChannel;
|
|
36
|
+
telemetry;
|
|
35
37
|
start(params) {
|
|
36
38
|
this.connection = this.startConnection(params);
|
|
37
39
|
this.publisherChannel = this.connection.createChannel({ json: true });
|
|
@@ -54,23 +56,25 @@ var PubSubMQ = class {
|
|
|
54
56
|
};
|
|
55
57
|
if (ttl) assertQueueOptions.arguments["x-message-ttl"] = ttl;
|
|
56
58
|
if (queueArguments) assertQueueOptions.arguments = { ...assertQueueOptions.arguments, ...queueArguments };
|
|
59
|
+
const fn = async (msg) => {
|
|
60
|
+
if (!msg) return;
|
|
61
|
+
try {
|
|
62
|
+
const data = JSON.parse(msg.content.toString());
|
|
63
|
+
const processor = consumer || up;
|
|
64
|
+
await processor(data, { fields: msg.fields, properties: msg.properties });
|
|
65
|
+
channel.ack(msg);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
channel.nack(msg, false, false);
|
|
68
|
+
} finally {
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const instrumentedHandler = this.telemetry?.instrumentFunc({ name: params.name, type: import_observability.TRACE_TYPE.MESSAGE }, fn);
|
|
57
72
|
await Promise.all([
|
|
58
73
|
channel.assertExchange(exchange, "topic", { durable: true }),
|
|
59
74
|
channel.assertQueue(name, assertQueueOptions),
|
|
60
75
|
channel.bindQueue(name, exchange, bindingKey),
|
|
61
76
|
channel.prefetch(prefetch || 1),
|
|
62
|
-
channel.consume(name,
|
|
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
|
-
})
|
|
77
|
+
channel.consume(name, instrumentedHandler ?? fn)
|
|
74
78
|
]);
|
|
75
79
|
}
|
|
76
80
|
});
|
|
@@ -79,9 +83,9 @@ var PubSubMQ = class {
|
|
|
79
83
|
};
|
|
80
84
|
|
|
81
85
|
// src/index.ts
|
|
82
|
-
var
|
|
86
|
+
var rabbitmq = new RabbitMQ(import_amqp_connection_manager.connect);
|
|
83
87
|
// Annotate the CommonJS export names for ESM import in node:
|
|
84
88
|
0 && (module.exports = {
|
|
85
|
-
|
|
89
|
+
rabbitmq
|
|
86
90
|
});
|
|
87
91
|
//# 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 {
|
|
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,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,kBAAM,UAAU,MAAM,EAAE,QAAQ,IAAI,QAAQ,YAAY,IAAI,WAAW,CAAC;AACxE,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,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,uBAAuB,EAAE;AAAA,QACjD,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AACF;;;AD3FO,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
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A wrapper around common message brokers",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -37,5 +37,13 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"amqp-connection-manager": "^4.1.14",
|
|
39
39
|
"amqplib": "^0.10.5"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@opens/observability": "^1.0.1"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"opens/observability": {
|
|
46
|
+
"optional": true
|
|
47
|
+
}
|
|
40
48
|
}
|
|
41
49
|
}
|