@tim_madule/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/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +1 -0
- package/package.json +7 -3
- package/index.ts +0 -114
- package/tsconfig.json +0 -39
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type RabbitMQParameters = {
|
|
2
|
+
url?: string;
|
|
3
|
+
serviceName?: string;
|
|
4
|
+
queues?: string[];
|
|
5
|
+
};
|
|
6
|
+
declare class RabbitMQ {
|
|
7
|
+
private config;
|
|
8
|
+
private connection;
|
|
9
|
+
private channel;
|
|
10
|
+
constructor(config?: RabbitMQParameters);
|
|
11
|
+
connect(): Promise<void>;
|
|
12
|
+
sendToQueue(queue: string, message: any): Promise<boolean>;
|
|
13
|
+
consumeFromQueue(queue: string, callback: (message: any) => Promise<void>, options?: {
|
|
14
|
+
noAck?: boolean;
|
|
15
|
+
}): Promise<boolean>;
|
|
16
|
+
getChannel(): any;
|
|
17
|
+
isConnected(): boolean;
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export default RabbitMQ;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAEA,KAAK,kBAAkB,GAAG;IACtB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB,CAAC;AAEF,cAAM,QAAQ;IAGE,OAAO,CAAC,MAAM;IAF1B,OAAO,CAAC,UAAU,CAAM;IACxB,OAAO,CAAC,OAAO,CAAM;gBACD,MAAM,GAAE,kBAAuB;IAE7C,OAAO;IAqCP,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG;IAqBvC,gBAAgB,CAClB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,IAAI,CAAC,EACzC,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAqB;IA6BnD,UAAU;IAIV,WAAW;IAIL,KAAK;CAgBd;AAED,eAAe,QAAQ,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import amqp from 'amqplib';
|
|
2
|
+
class RabbitMQ {
|
|
3
|
+
constructor(config = {}) {
|
|
4
|
+
this.config = config;
|
|
5
|
+
}
|
|
6
|
+
async connect() {
|
|
7
|
+
const serviceName = this.config.serviceName || 'UnknownService';
|
|
8
|
+
const MAX_RETRIES = 5;
|
|
9
|
+
let retryCount = 0;
|
|
10
|
+
while (retryCount < MAX_RETRIES) {
|
|
11
|
+
try {
|
|
12
|
+
this.connection = await amqp.connect(this.config.url || 'amqp://localhost:5672');
|
|
13
|
+
this.channel = await this.connection.createChannel();
|
|
14
|
+
if (this.config.queues) {
|
|
15
|
+
for (const queue of this.config.queues) {
|
|
16
|
+
await this.channel.assertQueue(queue, {
|
|
17
|
+
durable: true,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
console.log(`Connected to RabbitMQ for service: ${serviceName}`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.error(`Error connecting to RabbitMQ for service ${serviceName}:`, error);
|
|
26
|
+
retryCount++;
|
|
27
|
+
if (retryCount < MAX_RETRIES) {
|
|
28
|
+
console.log(`Retrying connection to RabbitMQ for service ${serviceName} in 5 seconds...`);
|
|
29
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async sendToQueue(queue, message) {
|
|
35
|
+
try {
|
|
36
|
+
if (!this.channel) {
|
|
37
|
+
console.error('Channel is not initialized. Call connect() first.');
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
await this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), { persistent: true });
|
|
41
|
+
console.log(`Message sent to queue ${queue}:`, message);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.error('Error sending message to queue:', error);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async consumeFromQueue(queue, callback, options = { noAck: false }) {
|
|
50
|
+
try {
|
|
51
|
+
if (!this.channel) {
|
|
52
|
+
console.error('Channel is not initialized. Call connect() first.');
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
await this.channel.consume(queue, async (message) => {
|
|
56
|
+
if (message) {
|
|
57
|
+
try {
|
|
58
|
+
await callback(JSON.parse(message.content.toString()));
|
|
59
|
+
if (!options.noAck) {
|
|
60
|
+
await this.channel.ack(message);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.error('Error processing message:', error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
console.log(`Consuming from queue ${queue}`);
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.error('Error consuming from queue:', error);
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
getChannel() {
|
|
77
|
+
return this.channel;
|
|
78
|
+
}
|
|
79
|
+
isConnected() {
|
|
80
|
+
return !!this.connection && !!this.channel;
|
|
81
|
+
}
|
|
82
|
+
async close() {
|
|
83
|
+
try {
|
|
84
|
+
if (this.channel) {
|
|
85
|
+
await this.channel.close();
|
|
86
|
+
this.channel = null;
|
|
87
|
+
console.log('RabbitMQ channel closed.');
|
|
88
|
+
}
|
|
89
|
+
if (this.connection) {
|
|
90
|
+
await this.connection.close();
|
|
91
|
+
this.connection = null;
|
|
92
|
+
console.log('RabbitMQ connection closed.');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
console.error('Error closing RabbitMQ connection:', error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export default RabbitMQ;
|
|
101
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,SAAS,CAAC;AAQ3B,MAAM,QAAQ;IAGV,YAAoB,SAA6B,EAAE;QAA/B,WAAM,GAAN,MAAM,CAAyB;IAAG,CAAC;IAEvD,KAAK,CAAC,OAAO;QACT,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,IAAI,gBAAgB,CAAC;QAChE,MAAM,WAAW,GAAG,CAAC,CAAC;QACtB,IAAI,UAAU,GAAG,CAAC,CAAC;QACnB,OAAO,UAAU,GAAG,WAAW,EAAE,CAAC;YAC9B,IAAI,CAAC;gBACD,IAAI,CAAC,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAChC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,uBAAuB,CAC7C,CAAC;gBACF,IAAI,CAAC,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,aAAa,EAAE,CAAC;gBACrD,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;oBACrB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;wBACrC,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,EAAE;4BAClC,OAAO,EAAE,IAAI;yBAChB,CAAC,CAAC;oBACP,CAAC;gBACL,CAAC;gBACD,OAAO,CAAC,GAAG,CACP,sCAAs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C;YAC5C,CAAC;YACD,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBAClB,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;gBAC9B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;gBACvB,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;YAC/C,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;QAC/D,CAAC;IACL,CAAC;CACJ;AAED,eAAe,QAAQ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tim_madule/rabbitmq",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Shared RabbitMQ client for microservices",
|
|
6
|
-
"main": "index.
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
|
-
".": "./index.
|
|
9
|
+
".": "./dist/index.js"
|
|
9
10
|
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
10
14
|
"scripts": {
|
|
11
15
|
"build": "tsc"
|
|
12
16
|
},
|
package/index.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import amqp from 'amqplib';
|
|
2
|
-
|
|
3
|
-
type RabbitMQParameters = {
|
|
4
|
-
url?: string;
|
|
5
|
-
serviceName?: string;
|
|
6
|
-
queues?: string[];
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
class RabbitMQ {
|
|
10
|
-
private connection: any; // Placeholder for the actual connection type
|
|
11
|
-
private channel: any; // Placeholder for the actual channel type
|
|
12
|
-
constructor(private config: RabbitMQParameters = {}) {}
|
|
13
|
-
|
|
14
|
-
async connect() {
|
|
15
|
-
const serviceName = this.config.serviceName || 'UnknownService';
|
|
16
|
-
try {
|
|
17
|
-
this.connection = await amqp.connect(
|
|
18
|
-
this.config.url || 'amqp://localhost:5672',
|
|
19
|
-
);
|
|
20
|
-
this.channel = await this.connection.createChannel();
|
|
21
|
-
if (this.config.queues) {
|
|
22
|
-
for (const queue of this.config.queues) {
|
|
23
|
-
await this.channel.assertQueue(queue, { durable: true });
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
console.log(`Connected to RabbitMQ for service: ${serviceName}`);
|
|
27
|
-
} catch (error) {
|
|
28
|
-
console.error(
|
|
29
|
-
`Error connecting to RabbitMQ for service ${serviceName}:`,
|
|
30
|
-
error,
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async sendToQueue(queue: string, message: any) {
|
|
36
|
-
try {
|
|
37
|
-
if (!this.channel) {
|
|
38
|
-
console.error(
|
|
39
|
-
'Channel is not initialized. Call connect() first.',
|
|
40
|
-
);
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
await this.channel.sendToQueue(
|
|
44
|
-
queue,
|
|
45
|
-
Buffer.from(JSON.stringify(message)),
|
|
46
|
-
{ persistent: true },
|
|
47
|
-
);
|
|
48
|
-
console.log(`Message sent to queue ${queue}:`, message);
|
|
49
|
-
return true;
|
|
50
|
-
} catch (error) {
|
|
51
|
-
console.error('Error sending message to queue:', error);
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async consumeFromQueue(
|
|
57
|
-
queue: string,
|
|
58
|
-
callback: (message: any) => Promise<void>,
|
|
59
|
-
options: { noAck?: boolean } = { noAck: false },
|
|
60
|
-
) {
|
|
61
|
-
try {
|
|
62
|
-
if (!this.channel) {
|
|
63
|
-
console.error(
|
|
64
|
-
'Channel is not initialized. Call connect() first.',
|
|
65
|
-
);
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
await this.channel.consume(queue, async (message: any) => {
|
|
69
|
-
if (message) {
|
|
70
|
-
try {
|
|
71
|
-
await callback(JSON.parse(message.content.toString()));
|
|
72
|
-
if (!options.noAck) {
|
|
73
|
-
await this.channel.ack(message);
|
|
74
|
-
}
|
|
75
|
-
} catch (error) {
|
|
76
|
-
console.error('Error processing message:', error);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
console.log(`Consuming from queue ${queue}`);
|
|
81
|
-
return true;
|
|
82
|
-
} catch (error) {
|
|
83
|
-
console.error('Error consuming from queue:', error);
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
getChannel() {
|
|
89
|
-
return this.channel;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
isConnected() {
|
|
93
|
-
return !!this.connection && !!this.channel;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async close() {
|
|
97
|
-
try {
|
|
98
|
-
if (this.channel) {
|
|
99
|
-
await this.channel.close();
|
|
100
|
-
this.channel = null;
|
|
101
|
-
console.log('RabbitMQ channel closed.');
|
|
102
|
-
}
|
|
103
|
-
if (this.connection) {
|
|
104
|
-
await this.connection.close();
|
|
105
|
-
this.connection = null;
|
|
106
|
-
console.log('RabbitMQ connection closed.');
|
|
107
|
-
}
|
|
108
|
-
} catch (error) {
|
|
109
|
-
console.error('Error closing RabbitMQ connection:', error);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export default RabbitMQ;
|
package/tsconfig.json
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
// Output code version
|
|
4
|
-
"target": "es2020",
|
|
5
|
-
"module": "nodenext",
|
|
6
|
-
|
|
7
|
-
// Type Checking
|
|
8
|
-
"strict": true,
|
|
9
|
-
"noImplicitAny": true,
|
|
10
|
-
"noImplicitThis": true,
|
|
11
|
-
"noUnusedLocals": true,
|
|
12
|
-
"noUnusedParameters": true,
|
|
13
|
-
"strictNullChecks": true,
|
|
14
|
-
"noFallthroughCasesInSwitch": true,
|
|
15
|
-
"allowUnusedLabels": false,
|
|
16
|
-
"noUncheckedSideEffectImports": true,
|
|
17
|
-
"allowUnreachableCode": false,
|
|
18
|
-
"exactOptionalPropertyTypes": true,
|
|
19
|
-
|
|
20
|
-
// Module Resolution
|
|
21
|
-
"moduleResolution": "nodenext",
|
|
22
|
-
"esModuleInterop": true, // CommonJS/ES module interop
|
|
23
|
-
"resolveJsonModule": true,
|
|
24
|
-
"forceConsistentCasingInFileNames": true,
|
|
25
|
-
"isolatedModules": true,
|
|
26
|
-
"types": ["@types/node"],
|
|
27
|
-
// File Location
|
|
28
|
-
"rootDir": ".",
|
|
29
|
-
"outDir": "dist",
|
|
30
|
-
// Emit Options
|
|
31
|
-
"sourceMap": true, // Generate source maps
|
|
32
|
-
"removeComments": true, // Remove comments in output
|
|
33
|
-
"noEmitOnError": true, // Don't emit if there are errors
|
|
34
|
-
// Performance
|
|
35
|
-
"skipLibCheck": true // Skip type checking of declaration files
|
|
36
|
-
},
|
|
37
|
-
"include": ["index.ts"],
|
|
38
|
-
"exclude": ["node_modules"]
|
|
39
|
-
}
|