@tmlmobilidade/rabbitmq 20251202.1817.5
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 +37 -0
- package/dist/index.js +97 -0
- package/package.json +50 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Options, Replies } from 'amqplib';
|
|
2
|
+
export interface RabbitMQConfig {
|
|
3
|
+
socketOptions?: any;
|
|
4
|
+
uri: Options.Connect | string;
|
|
5
|
+
}
|
|
6
|
+
export declare class RabbitMQConnector {
|
|
7
|
+
private channel?;
|
|
8
|
+
private config;
|
|
9
|
+
private connection?;
|
|
10
|
+
constructor(config: RabbitMQConfig);
|
|
11
|
+
/**
|
|
12
|
+
* Connects to RabbitMQ and creates a channel.
|
|
13
|
+
*/
|
|
14
|
+
connect(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Disconnects from RabbitMQ.
|
|
17
|
+
*/
|
|
18
|
+
disconnect(): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Publishes a message to a queue.
|
|
21
|
+
*/
|
|
22
|
+
publish(queue: string, message: Buffer | string, options?: Options.Publish): Promise<Replies.Empty>;
|
|
23
|
+
/**
|
|
24
|
+
* Subscribes to a queue and processes messages.
|
|
25
|
+
*/
|
|
26
|
+
subscribe(queue: string, callback: (message: Buffer | string) => void): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Singleton class for RabbitMQConnector.
|
|
30
|
+
*/
|
|
31
|
+
declare class RabbitMQService extends RabbitMQConnector {
|
|
32
|
+
private static _instance;
|
|
33
|
+
private constructor();
|
|
34
|
+
static getInstance(): Promise<RabbitMQService>;
|
|
35
|
+
}
|
|
36
|
+
export declare const rabbitMQ: RabbitMQService;
|
|
37
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
3
|
+
import { connect as amqpConnect } from 'amqplib';
|
|
4
|
+
export class RabbitMQConnector {
|
|
5
|
+
channel;
|
|
6
|
+
config;
|
|
7
|
+
connection;
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.config = config;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Connects to RabbitMQ and creates a channel.
|
|
13
|
+
*/
|
|
14
|
+
async connect() {
|
|
15
|
+
try {
|
|
16
|
+
this.connection = await amqpConnect(this.config.uri, this.config.socketOptions);
|
|
17
|
+
this.channel = await this.connection.createChannel();
|
|
18
|
+
console.log('Connected to RabbitMQ.');
|
|
19
|
+
this.channel.on('close', () => {
|
|
20
|
+
console.warn('Channel closed, reconnecting...');
|
|
21
|
+
setTimeout(this.connect.bind(this), 5000);
|
|
22
|
+
});
|
|
23
|
+
this.channel.on('error', (err) => {
|
|
24
|
+
console.error('Channel error:', err);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error('Failed to connect to RabbitMQ:', error);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Disconnects from RabbitMQ.
|
|
34
|
+
*/
|
|
35
|
+
async disconnect() {
|
|
36
|
+
if (this.channel) {
|
|
37
|
+
await this.channel.close();
|
|
38
|
+
this.channel = undefined;
|
|
39
|
+
}
|
|
40
|
+
if (this.connection) {
|
|
41
|
+
await this.connection.close();
|
|
42
|
+
this.connection = undefined;
|
|
43
|
+
}
|
|
44
|
+
console.log('Disconnected from RabbitMQ.');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Publishes a message to a queue.
|
|
48
|
+
*/
|
|
49
|
+
async publish(queue, message, options) {
|
|
50
|
+
if (!this.channel) {
|
|
51
|
+
throw new Error('Channel is not initialized. Call connect() first.');
|
|
52
|
+
}
|
|
53
|
+
await this.channel.assertQueue(queue, { durable: true });
|
|
54
|
+
const sent = this.channel.sendToQueue(queue, Buffer.isBuffer(message) ? message : Buffer.from(message), options);
|
|
55
|
+
if (!sent) {
|
|
56
|
+
throw new Error('Failed to send message to queue.');
|
|
57
|
+
}
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Subscribes to a queue and processes messages.
|
|
62
|
+
*/
|
|
63
|
+
async subscribe(queue, callback) {
|
|
64
|
+
if (!this.channel) {
|
|
65
|
+
throw new Error('Channel is not initialized. Call connect() first.');
|
|
66
|
+
}
|
|
67
|
+
await this.channel.assertQueue(queue, { durable: true });
|
|
68
|
+
await this.channel.consume(queue, (message) => {
|
|
69
|
+
if (message) {
|
|
70
|
+
callback(message.content);
|
|
71
|
+
this.channel?.ack(message);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/* * */
|
|
77
|
+
/**
|
|
78
|
+
* Singleton class for RabbitMQConnector.
|
|
79
|
+
*/
|
|
80
|
+
class RabbitMQService extends RabbitMQConnector {
|
|
81
|
+
static _instance;
|
|
82
|
+
constructor() {
|
|
83
|
+
if (!process.env.RABBITMQ_URI) {
|
|
84
|
+
throw new Error('Missing "RABBITMQ_URI" environment variable');
|
|
85
|
+
}
|
|
86
|
+
super({ uri: process.env.RABBITMQ_URI });
|
|
87
|
+
}
|
|
88
|
+
static async getInstance() {
|
|
89
|
+
if (!RabbitMQService._instance) {
|
|
90
|
+
const instance = new RabbitMQService();
|
|
91
|
+
await instance.connect();
|
|
92
|
+
RabbitMQService._instance = instance;
|
|
93
|
+
}
|
|
94
|
+
return RabbitMQService._instance;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export const rabbitMQ = AsyncSingletonProxy(RabbitMQService);
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tmlmobilidade/rabbitmq",
|
|
3
|
+
"version": "20251202.1817.5",
|
|
4
|
+
"author": {
|
|
5
|
+
"email": "iso@tmlmobilidade.pt",
|
|
6
|
+
"name": "TML-ISO"
|
|
7
|
+
},
|
|
8
|
+
"license": "AGPL-3.0-or-later",
|
|
9
|
+
"homepage": "https://github.com/tmlmobilidade/go#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/tmlmobilidade/go/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/tmlmobilidade/go.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"public transit",
|
|
19
|
+
"tml",
|
|
20
|
+
"transportes metropolitanos de lisboa",
|
|
21
|
+
"go"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc && resolve-tspaths",
|
|
34
|
+
"lint": "eslint ./src/ && tsc --noEmit",
|
|
35
|
+
"lint:fix": "eslint ./src/ --fix",
|
|
36
|
+
"watch": "tsc-watch --onSuccess 'resolve-tspaths'"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@tmlmobilidade/utils": "*",
|
|
40
|
+
"amqplib": "0.10.9"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@tmlmobilidade/tsconfig": "*",
|
|
44
|
+
"@types/amqplib": "0.10.8",
|
|
45
|
+
"@types/node": "24.10.1",
|
|
46
|
+
"resolve-tspaths": "0.8.23",
|
|
47
|
+
"tsc-watch": "7.2.0",
|
|
48
|
+
"typescript": "5.9.3"
|
|
49
|
+
}
|
|
50
|
+
}
|