@only-chat/rabbitmq-queue 0.2.0-beta.10
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 +2 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import amqp from 'amqplib';
|
|
2
|
+
import type { MessageQueue } from '@only-chat/types/queue.js';
|
|
3
|
+
export type { Options } from 'amqplib';
|
|
4
|
+
export interface Config {
|
|
5
|
+
url: string | amqp.Options.Connect;
|
|
6
|
+
exchange?: string;
|
|
7
|
+
exchangeType?: string;
|
|
8
|
+
exchangeOptions?: amqp.Options.AssertExchange;
|
|
9
|
+
routingKey?: string;
|
|
10
|
+
queue?: string;
|
|
11
|
+
queueOptions?: amqp.Options.AssertQueue;
|
|
12
|
+
}
|
|
13
|
+
export declare function initialize(config: Config): Promise<MessageQueue>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import amqp from 'amqplib';
|
|
2
|
+
const acceptTypes = ['connected', 'disconnected', 'joined', 'left', 'closed', 'deleted', 'updated', 'message-updated', 'message-deleted', 'text', 'file'];
|
|
3
|
+
export async function initialize(config) {
|
|
4
|
+
const c = await amqp.connect(config.url);
|
|
5
|
+
const channel = await c.createChannel();
|
|
6
|
+
const q = await channel.assertQueue(config.queue ?? '', config.queueOptions);
|
|
7
|
+
if (config.exchange) {
|
|
8
|
+
const e = await channel.assertExchange(config.exchange, config.exchangeType ?? 'fanout', config.exchangeOptions);
|
|
9
|
+
const _ = await channel.bindQueue(q.queue, e.exchange, config.routingKey ?? '');
|
|
10
|
+
}
|
|
11
|
+
const subscribers = [];
|
|
12
|
+
const _ = channel.consume(q.queue, async function (msg) {
|
|
13
|
+
if (!msg) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const deserialized = JSON.parse(msg.content.toString());
|
|
17
|
+
const type = deserialized.type?.toString();
|
|
18
|
+
if (!acceptTypes.includes(type)) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const m = {
|
|
22
|
+
type,
|
|
23
|
+
id: deserialized.id?.toString(),
|
|
24
|
+
clientMessageId: deserialized.id?.toString(),
|
|
25
|
+
instanceId: deserialized.instanceId?.toString(),
|
|
26
|
+
conversationId: deserialized.conversationId?.toString(),
|
|
27
|
+
participants: Array.isArray(deserialized.participants) ? deserialized.participants : undefined,
|
|
28
|
+
connectionId: deserialized.connectionId?.toString(),
|
|
29
|
+
fromId: deserialized.fromId?.toString(),
|
|
30
|
+
data: typeof deserialized.data === 'object' ? deserialized.data : null,
|
|
31
|
+
createdAt: new Date(deserialized.createdAt),
|
|
32
|
+
updatedAt: deserialized.updatedAt ? new Date(deserialized.updatedAt) : undefined,
|
|
33
|
+
deletedAt: deserialized.deletedAt ? new Date(deserialized.deletedAt) : undefined,
|
|
34
|
+
};
|
|
35
|
+
await Promise.all(subscribers.map(c => c(m)));
|
|
36
|
+
channel.ack(msg);
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
acceptTypes,
|
|
40
|
+
async publish(msg) {
|
|
41
|
+
return acceptTypes.includes(msg.type) && channel.sendToQueue(q.queue, Buffer.from(JSON.stringify(msg)));
|
|
42
|
+
},
|
|
43
|
+
async subscribe(callback) {
|
|
44
|
+
return !!subscribers.push(callback);
|
|
45
|
+
},
|
|
46
|
+
async unsubscribe(callback) {
|
|
47
|
+
const index = subscribers.lastIndexOf(callback);
|
|
48
|
+
if (index < 0) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return !!subscribers.splice(index, 1);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],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AEH,OAAO;QACH,WAAW;QAEX,KAAK,CAAC,OAAO,CAAC,GAAG;YACb,OAAO,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5G,CAAC;QAED,KAAK,CAAC,SAAS,CAAC,QAAQ;YACpB,OAAO,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACxC,CAAC;QAED,KAAK,CAAC,WAAW,CAAC,QAAQ;YACtB,MAAM,KAAK,GAAG,WAAW,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;YAChD,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACZ,OAAO,KAAK,CAAC;YACjB,CAAC;YAED,OAAO,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAC1C,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@only-chat/rabbitmq-queue",
|
|
3
|
+
"version": "0.2.0-beta.10",
|
|
4
|
+
"description": "RabbitMQ queue for only-chat",
|
|
5
|
+
"homepage": "https://github.com/only-chat/node-backend/packages/queues/rabbitmq",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/only-chat/node-backend.git",
|
|
12
|
+
"directory": "packages/queues/rabbitmq"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc"
|
|
16
|
+
},
|
|
17
|
+
"author": "only-chat",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@only-chat/types": "0.2.0-beta.10",
|
|
21
|
+
"@types/amqplib": "^0.10.5",
|
|
22
|
+
"typescript": "^5.4.5"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist/"
|
|
26
|
+
],
|
|
27
|
+
"keywords": [
|
|
28
|
+
"node",
|
|
29
|
+
"only-chat",
|
|
30
|
+
"rabbitmq"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"amqplib": "^0.10.4"
|
|
34
|
+
}
|
|
35
|
+
}
|