@fluyappgocore/commons-backend 1.0.202
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/build/classes/BacklogManager.d.ts +129 -0
- package/build/classes/BacklogManager.js +417 -0
- package/build/classes/BacklogMonitor.d.ts +102 -0
- package/build/classes/BacklogMonitor.js +222 -0
- package/build/classes/HttpResponse.d.ts +7 -0
- package/build/classes/HttpResponse.js +13 -0
- package/build/classes/index.d.ts +3 -0
- package/build/classes/index.js +15 -0
- package/build/dtos/file.dto.d.ts +5 -0
- package/build/dtos/file.dto.js +25 -0
- package/build/dtos/index.d.ts +1 -0
- package/build/dtos/index.js +13 -0
- package/build/events/BatchEnhancedListener.d.ts +106 -0
- package/build/events/BatchEnhancedListener.js +307 -0
- package/build/events/EnhancedListener.d.ts +75 -0
- package/build/events/EnhancedListener.js +185 -0
- package/build/events/baseListener.d.ts +19 -0
- package/build/events/baseListener.js +90 -0
- package/build/events/basePublisher.d.ts +14 -0
- package/build/events/basePublisher.js +84 -0
- package/build/events/config.d.ts +7 -0
- package/build/events/config.js +14 -0
- package/build/events/createClient.d.ts +38 -0
- package/build/events/createClient.js +416 -0
- package/build/events/index.d.ts +6 -0
- package/build/events/index.js +18 -0
- package/build/events/interfaces/agent.event.d.ts +7 -0
- package/build/events/interfaces/agent.event.js +2 -0
- package/build/events/interfaces/agentCrud.event.d.ts +7 -0
- package/build/events/interfaces/agentCrud.event.js +2 -0
- package/build/events/interfaces/agentTracking.event.d.ts +7 -0
- package/build/events/interfaces/agentTracking.event.js +2 -0
- package/build/events/interfaces/audit.event.d.ts +7 -0
- package/build/events/interfaces/audit.event.js +2 -0
- package/build/events/interfaces/branch.event.d.ts +7 -0
- package/build/events/interfaces/branch.event.js +2 -0
- package/build/events/interfaces/deleteTracking.event.d.ts +7 -0
- package/build/events/interfaces/deleteTracking.event.js +2 -0
- package/build/events/interfaces/department.event.d.ts +7 -0
- package/build/events/interfaces/department.event.js +2 -0
- package/build/events/interfaces/entity.event.d.ts +7 -0
- package/build/events/interfaces/entity.event.js +2 -0
- package/build/events/interfaces/index.d.ts +23 -0
- package/build/events/interfaces/index.js +35 -0
- package/build/events/interfaces/manualMatch.event.d.ts +7 -0
- package/build/events/interfaces/manualMatch.event.js +2 -0
- package/build/events/interfaces/match.event.d.ts +7 -0
- package/build/events/interfaces/match.event.js +2 -0
- package/build/events/interfaces/monitor.event.d.ts +7 -0
- package/build/events/interfaces/monitor.event.js +2 -0
- package/build/events/interfaces/notification.event.d.ts +7 -0
- package/build/events/interfaces/notification.event.js +2 -0
- package/build/events/interfaces/object.event.d.ts +7 -0
- package/build/events/interfaces/object.event.js +2 -0
- package/build/events/interfaces/payments.event.d.ts +7 -0
- package/build/events/interfaces/payments.event.js +2 -0
- package/build/events/interfaces/planning.event.d.ts +7 -0
- package/build/events/interfaces/planning.event.js +2 -0
- package/build/events/interfaces/service.event.d.ts +7 -0
- package/build/events/interfaces/service.event.js +2 -0
- package/build/events/interfaces/subscription.event.d.ts +7 -0
- package/build/events/interfaces/subscription.event.js +2 -0
- package/build/events/interfaces/ticket.event.d.ts +7 -0
- package/build/events/interfaces/ticket.event.js +2 -0
- package/build/events/interfaces/ticketNote.event.d.ts +7 -0
- package/build/events/interfaces/ticketNote.event.js +2 -0
- package/build/events/interfaces/ticketTracking.event.d.ts +7 -0
- package/build/events/interfaces/ticketTracking.event.js +2 -0
- package/build/events/interfaces/ticketxTopics.event.d.ts +7 -0
- package/build/events/interfaces/ticketxTopics.event.js +2 -0
- package/build/events/interfaces/topics.event.d.ts +7 -0
- package/build/events/interfaces/topics.event.js +2 -0
- package/build/events/interfaces/waitingList.event.d.ts +7 -0
- package/build/events/interfaces/waitingList.event.js +2 -0
- package/build/exceptions/AuthenticationTokenMissingException.d.ts +5 -0
- package/build/exceptions/AuthenticationTokenMissingException.js +27 -0
- package/build/exceptions/HttpException.d.ts +5 -0
- package/build/exceptions/HttpException.js +27 -0
- package/build/exceptions/MongoErrorException.d.ts +4 -0
- package/build/exceptions/MongoErrorException.js +27 -0
- package/build/exceptions/MulterException.d.ts +4 -0
- package/build/exceptions/MulterException.js +27 -0
- package/build/exceptions/QwizardException.d.ts +5 -0
- package/build/exceptions/QwizardException.js +27 -0
- package/build/exceptions/RecordingServerException.d.ts +5 -0
- package/build/exceptions/RecordingServerException.js +27 -0
- package/build/exceptions/UserNotFoundException.d.ts +4 -0
- package/build/exceptions/UserNotFoundException.js +27 -0
- package/build/exceptions/UserWithThatEmailAlreadyExistsException.d.ts +4 -0
- package/build/exceptions/UserWithThatEmailAlreadyExistsException.js +27 -0
- package/build/exceptions/WrongAnonymousAuthException.d.ts +5 -0
- package/build/exceptions/WrongAnonymousAuthException.js +27 -0
- package/build/exceptions/WrongAuthenticationTokenException.d.ts +5 -0
- package/build/exceptions/WrongAuthenticationTokenException.js +27 -0
- package/build/exceptions/WrongBusinessAuthException.d.ts +5 -0
- package/build/exceptions/WrongBusinessAuthException.js +27 -0
- package/build/exceptions/WrongCredentialsException.d.ts +4 -0
- package/build/exceptions/WrongCredentialsException.js +27 -0
- package/build/exceptions/WrongRoomException.d.ts +4 -0
- package/build/exceptions/WrongRoomException.js +27 -0
- package/build/exceptions/index.d.ts +16 -0
- package/build/exceptions/index.js +28 -0
- package/build/exceptions/invalidToken.exception.d.ts +4 -0
- package/build/exceptions/invalidToken.exception.js +27 -0
- package/build/exceptions/missingToken.exception.d.ts +4 -0
- package/build/exceptions/missingToken.exception.js +27 -0
- package/build/exceptions/wrongAuth.exception.d.ts +5 -0
- package/build/exceptions/wrongAuth.exception.js +27 -0
- package/build/index.d.ts +12 -0
- package/build/index.js +32 -0
- package/build/interfaces/controller.interface.d.ts +5 -0
- package/build/interfaces/controller.interface.js +2 -0
- package/build/interfaces/index.d.ts +3 -0
- package/build/interfaces/index.js +15 -0
- package/build/interfaces/requesWithUser.interface.d.ts +11 -0
- package/build/interfaces/requesWithUser.interface.js +2 -0
- package/build/interfaces/token.interface.d.ts +12 -0
- package/build/interfaces/token.interface.js +2 -0
- package/build/lib/ensureDatabase.d.ts +11 -0
- package/build/lib/ensureDatabase.js +97 -0
- package/build/lib/functions.d.ts +89 -0
- package/build/lib/functions.js +214 -0
- package/build/lib/helpers.d.ts +29 -0
- package/build/lib/helpers.js +33 -0
- package/build/lib/index.d.ts +4 -0
- package/build/lib/index.js +16 -0
- package/build/lib/logger.d.ts +2 -0
- package/build/lib/logger.js +41 -0
- package/build/middlewares/auth.middleware.d.ts +6 -0
- package/build/middlewares/auth.middleware.js +287 -0
- package/build/middlewares/error.middleware.d.ts +3 -0
- package/build/middlewares/error.middleware.js +14 -0
- package/build/middlewares/index.d.ts +3 -0
- package/build/middlewares/index.js +15 -0
- package/build/middlewares/validation.middleware.d.ts +2 -0
- package/build/middlewares/validation.middleware.js +22 -0
- package/package.json +45 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.Listener = exports.shouldRestart = void 0;
|
|
40
|
+
var kafkajs_1 = require("kafkajs");
|
|
41
|
+
function shouldRestart(error) {
|
|
42
|
+
var isNonRetriableError = error instanceof kafkajs_1.KafkaJSNonRetriableError;
|
|
43
|
+
var isNumberOfRetriesExceeded = error instanceof kafkajs_1.KafkaJSNumberOfRetriesExceeded;
|
|
44
|
+
return isNonRetriableError && !isNumberOfRetriesExceeded;
|
|
45
|
+
}
|
|
46
|
+
exports.shouldRestart = shouldRestart;
|
|
47
|
+
var Listener = /** @class */ (function () {
|
|
48
|
+
function Listener(client) {
|
|
49
|
+
this.client = client;
|
|
50
|
+
}
|
|
51
|
+
Listener.prototype.listen = function (fromBeginning) {
|
|
52
|
+
var _this = this;
|
|
53
|
+
var topic = this.topic;
|
|
54
|
+
var consumer = this.client.consumer({ groupId: this.groupName });
|
|
55
|
+
consumer
|
|
56
|
+
.connect()
|
|
57
|
+
.then(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
58
|
+
var _this = this;
|
|
59
|
+
return __generator(this, function (_a) {
|
|
60
|
+
switch (_a.label) {
|
|
61
|
+
case 0: return [4 /*yield*/, consumer.connect()];
|
|
62
|
+
case 1:
|
|
63
|
+
_a.sent();
|
|
64
|
+
return [4 /*yield*/, consumer.subscribe({ topic: topic, fromBeginning: fromBeginning })];
|
|
65
|
+
case 2:
|
|
66
|
+
_a.sent();
|
|
67
|
+
return [4 /*yield*/, consumer.run({
|
|
68
|
+
eachMessage: function (payload) {
|
|
69
|
+
var message = payload.message;
|
|
70
|
+
var parsedData = _this.parseMessage(message);
|
|
71
|
+
_this.onMessage(parsedData, message);
|
|
72
|
+
},
|
|
73
|
+
})];
|
|
74
|
+
case 3:
|
|
75
|
+
_a.sent();
|
|
76
|
+
return [2 /*return*/];
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}); })
|
|
80
|
+
.catch(function (error) {
|
|
81
|
+
console.log("ERROORRR KAFKA: ", error);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
Listener.prototype.parseMessage = function (msg) {
|
|
85
|
+
var data = msg.value || "{}";
|
|
86
|
+
return typeof data === "string" ? JSON.parse(data) : JSON.parse(data.toString("utf8"));
|
|
87
|
+
};
|
|
88
|
+
return Listener;
|
|
89
|
+
}());
|
|
90
|
+
exports.Listener = Listener;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TicketTopics, AgentTopics, EntityTreeTopics, MatchTopics, AgentCrudTopics, TicketTrackingTopics, AgentTrackingTopics, ManualMatchTopics, MonitorTopics, UserTopics, DeleteTrackingTopics, AuditTopics, NotificationTopics, TicketNoteTopics, TopicsTopics, TicketxTopicsTopics, DeviceTopics, PlanningTopics, WaitingTopics, PaymentsTopics, SubscriptionTopics } from "@fluyappgo/commons";
|
|
2
|
+
interface Event {
|
|
3
|
+
topic: TicketTopics | AgentTopics | EntityTreeTopics | AgentCrudTopics | MatchTopics | TicketTrackingTopics | AgentTrackingTopics | ManualMatchTopics | MonitorTopics | UserTopics | DeleteTrackingTopics | AuditTopics | NotificationTopics | TicketNoteTopics | TopicsTopics | TicketxTopicsTopics | DeviceTopics | PlanningTopics | WaitingTopics | PaymentsTopics | SubscriptionTopics;
|
|
4
|
+
data: any;
|
|
5
|
+
keyKafka: number;
|
|
6
|
+
groupName: string;
|
|
7
|
+
}
|
|
8
|
+
export declare abstract class Publisher<T extends Event> {
|
|
9
|
+
abstract topic: T["topic"];
|
|
10
|
+
private client;
|
|
11
|
+
constructor(client: any);
|
|
12
|
+
publish(data: T["data"], keyKafka: T["keyKafka"]): Promise<String>;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.Publisher = void 0;
|
|
40
|
+
var Publisher = /** @class */ (function () {
|
|
41
|
+
function Publisher(client) {
|
|
42
|
+
this.client = client;
|
|
43
|
+
}
|
|
44
|
+
Publisher.prototype.publish = function (data, keyKafka) {
|
|
45
|
+
var _this = this;
|
|
46
|
+
var topic = this.topic;
|
|
47
|
+
return new Promise(function (resolve, reject) {
|
|
48
|
+
var producer = _this.client.producer();
|
|
49
|
+
try {
|
|
50
|
+
(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
51
|
+
return __generator(this, function (_a) {
|
|
52
|
+
switch (_a.label) {
|
|
53
|
+
case 0: return [4 /*yield*/, producer.connect()];
|
|
54
|
+
case 1:
|
|
55
|
+
_a.sent();
|
|
56
|
+
return [4 /*yield*/, producer.send({
|
|
57
|
+
topic: topic,
|
|
58
|
+
messages: [
|
|
59
|
+
{
|
|
60
|
+
key: String(keyKafka),
|
|
61
|
+
value: JSON.stringify(data),
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
})];
|
|
65
|
+
case 2:
|
|
66
|
+
_a.sent();
|
|
67
|
+
resolve(topic + " done");
|
|
68
|
+
return [4 /*yield*/, producer.disconnect()];
|
|
69
|
+
case 3:
|
|
70
|
+
_a.sent();
|
|
71
|
+
return [2 /*return*/];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}); })();
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
console.log(e);
|
|
78
|
+
reject(topic + " " + e.message);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
return Publisher;
|
|
83
|
+
}());
|
|
84
|
+
exports.Publisher = Publisher;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useRabbitMQ = exports.rabbitmqUrl = exports.kafkaBrokers = void 0;
|
|
4
|
+
exports.kafkaBrokers = [
|
|
5
|
+
process.env.KAFKA_HOST,
|
|
6
|
+
process.env.KAFKA_HOST_2,
|
|
7
|
+
process.env.KAFKA_HOST_3
|
|
8
|
+
];
|
|
9
|
+
exports.rabbitmqUrl = process.env.RABBITMQ_URL || "amqp://guest:guest@localhost:5672";
|
|
10
|
+
/**
|
|
11
|
+
* Set USE_RABBITMQ=true to route all events through RabbitMQ instead of Kafka.
|
|
12
|
+
* Default: false (Kafka).
|
|
13
|
+
*/
|
|
14
|
+
exports.useRabbitMQ = process.env.USE_RABBITMQ === "true";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Kafka } from "kafkajs";
|
|
2
|
+
/**
|
|
3
|
+
* KafkaConnection — drop-in compatible.
|
|
4
|
+
*
|
|
5
|
+
* When USE_RABBITMQ=true, returns a RabbitMQ adapter that exposes
|
|
6
|
+
* the same .producer() and .consumer({ groupId }) API as kafkajs,
|
|
7
|
+
* so ALL existing publishers/listeners work without any change.
|
|
8
|
+
*
|
|
9
|
+
* When USE_RABBITMQ=false (default), returns the original Kafka client.
|
|
10
|
+
*/
|
|
11
|
+
export declare const KafkaConnection: (brokers: string[], clientId: string) => Kafka | {
|
|
12
|
+
producer(): {
|
|
13
|
+
connect(): Promise<void>;
|
|
14
|
+
send({ topic, messages }: {
|
|
15
|
+
topic: string;
|
|
16
|
+
messages: {
|
|
17
|
+
key: string;
|
|
18
|
+
value: string;
|
|
19
|
+
}[];
|
|
20
|
+
}): Promise<void>;
|
|
21
|
+
disconnect(): Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
consumer({ groupId }: {
|
|
24
|
+
groupId: string;
|
|
25
|
+
}): {
|
|
26
|
+
connect(): Promise<void>;
|
|
27
|
+
subscribe({ topic, fromBeginning: _fb }: {
|
|
28
|
+
topic: string;
|
|
29
|
+
fromBeginning: boolean;
|
|
30
|
+
}): Promise<void>;
|
|
31
|
+
run(opts: {
|
|
32
|
+
eachMessage?: ((payload: any) => any) | undefined;
|
|
33
|
+
eachBatch?: ((payload: any) => any) | undefined;
|
|
34
|
+
eachBatchAutoResolve?: boolean | undefined;
|
|
35
|
+
}): Promise<void>;
|
|
36
|
+
disconnect(): Promise<void>;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
39
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
40
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
41
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
42
|
+
r[k] = a[j];
|
|
43
|
+
return r;
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
exports.KafkaConnection = void 0;
|
|
47
|
+
var kafkajs_1 = require("kafkajs");
|
|
48
|
+
var config_1 = require("./config");
|
|
49
|
+
/**
|
|
50
|
+
* KafkaConnection — drop-in compatible.
|
|
51
|
+
*
|
|
52
|
+
* When USE_RABBITMQ=true, returns a RabbitMQ adapter that exposes
|
|
53
|
+
* the same .producer() and .consumer({ groupId }) API as kafkajs,
|
|
54
|
+
* so ALL existing publishers/listeners work without any change.
|
|
55
|
+
*
|
|
56
|
+
* When USE_RABBITMQ=false (default), returns the original Kafka client.
|
|
57
|
+
*/
|
|
58
|
+
var KafkaConnection = function (brokers, clientId) {
|
|
59
|
+
if (config_1.useRabbitMQ) {
|
|
60
|
+
console.log("[events] Using RabbitMQ for " + clientId);
|
|
61
|
+
return createRabbitMQAdapter(clientId);
|
|
62
|
+
}
|
|
63
|
+
var kafka = new kafkajs_1.Kafka({ clientId: clientId, brokers: brokers });
|
|
64
|
+
return kafka;
|
|
65
|
+
};
|
|
66
|
+
exports.KafkaConnection = KafkaConnection;
|
|
67
|
+
// ─── RabbitMQ Adapter ────────────────────────────────────────────────────────
|
|
68
|
+
function createRabbitMQAdapter(clientId) {
|
|
69
|
+
var _this = this;
|
|
70
|
+
// Use dynamic require to avoid type issues between amqplib versions
|
|
71
|
+
var amqp = require("amqplib");
|
|
72
|
+
var connection = null;
|
|
73
|
+
var connecting = null;
|
|
74
|
+
var getConnection = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
75
|
+
var _this = this;
|
|
76
|
+
return __generator(this, function (_a) {
|
|
77
|
+
if (connection)
|
|
78
|
+
return [2 /*return*/, connection];
|
|
79
|
+
if (connecting)
|
|
80
|
+
return [2 /*return*/, connecting];
|
|
81
|
+
connecting = (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
82
|
+
var conn;
|
|
83
|
+
return __generator(this, function (_a) {
|
|
84
|
+
switch (_a.label) {
|
|
85
|
+
case 0: return [4 /*yield*/, amqp.connect(config_1.rabbitmqUrl)];
|
|
86
|
+
case 1:
|
|
87
|
+
conn = _a.sent();
|
|
88
|
+
conn.on("error", function (err) {
|
|
89
|
+
console.error("[RabbitMQ:" + clientId + "] Connection error:", err.message);
|
|
90
|
+
connection = null;
|
|
91
|
+
connecting = null;
|
|
92
|
+
});
|
|
93
|
+
conn.on("close", function () {
|
|
94
|
+
console.log("[RabbitMQ:" + clientId + "] Connection closed");
|
|
95
|
+
connection = null;
|
|
96
|
+
connecting = null;
|
|
97
|
+
});
|
|
98
|
+
connection = conn;
|
|
99
|
+
connecting = null;
|
|
100
|
+
return [2 /*return*/, conn];
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}); })();
|
|
104
|
+
return [2 /*return*/, connecting];
|
|
105
|
+
});
|
|
106
|
+
}); };
|
|
107
|
+
return {
|
|
108
|
+
producer: function () {
|
|
109
|
+
var channel = null;
|
|
110
|
+
return {
|
|
111
|
+
connect: function () {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
+
var conn;
|
|
114
|
+
return __generator(this, function (_a) {
|
|
115
|
+
switch (_a.label) {
|
|
116
|
+
case 0: return [4 /*yield*/, getConnection()];
|
|
117
|
+
case 1:
|
|
118
|
+
conn = _a.sent();
|
|
119
|
+
return [4 /*yield*/, conn.createConfirmChannel()];
|
|
120
|
+
case 2:
|
|
121
|
+
channel = _a.sent();
|
|
122
|
+
return [2 /*return*/];
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
send: function (_a) {
|
|
128
|
+
var topic = _a.topic, messages = _a.messages;
|
|
129
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
130
|
+
var _i, messages_1, msg;
|
|
131
|
+
return __generator(this, function (_b) {
|
|
132
|
+
switch (_b.label) {
|
|
133
|
+
case 0:
|
|
134
|
+
if (!channel)
|
|
135
|
+
throw new Error("[RabbitMQ] Producer not connected");
|
|
136
|
+
return [4 /*yield*/, channel.assertExchange(topic, "fanout", { durable: true })];
|
|
137
|
+
case 1:
|
|
138
|
+
_b.sent();
|
|
139
|
+
for (_i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
|
|
140
|
+
msg = messages_1[_i];
|
|
141
|
+
channel.publish(topic, msg.key || "", Buffer.from(msg.value), {
|
|
142
|
+
persistent: true,
|
|
143
|
+
contentType: "application/json",
|
|
144
|
+
headers: { key: msg.key },
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return [4 /*yield*/, channel.waitForConfirms()];
|
|
148
|
+
case 2:
|
|
149
|
+
_b.sent();
|
|
150
|
+
return [2 /*return*/];
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
disconnect: function () {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
157
|
+
return __generator(this, function (_a) {
|
|
158
|
+
switch (_a.label) {
|
|
159
|
+
case 0:
|
|
160
|
+
if (!channel) return [3 /*break*/, 2];
|
|
161
|
+
return [4 /*yield*/, channel.close().catch(function () { })];
|
|
162
|
+
case 1:
|
|
163
|
+
_a.sent();
|
|
164
|
+
channel = null;
|
|
165
|
+
_a.label = 2;
|
|
166
|
+
case 2: return [2 /*return*/];
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
consumer: function (_a) {
|
|
174
|
+
var groupId = _a.groupId;
|
|
175
|
+
var channel = null;
|
|
176
|
+
var subscribedTopic = "";
|
|
177
|
+
var queueName = "";
|
|
178
|
+
return {
|
|
179
|
+
connect: function () {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
181
|
+
var conn;
|
|
182
|
+
return __generator(this, function (_a) {
|
|
183
|
+
switch (_a.label) {
|
|
184
|
+
case 0: return [4 /*yield*/, getConnection()];
|
|
185
|
+
case 1:
|
|
186
|
+
conn = _a.sent();
|
|
187
|
+
return [4 /*yield*/, conn.createChannel()];
|
|
188
|
+
case 2:
|
|
189
|
+
channel = _a.sent();
|
|
190
|
+
return [4 /*yield*/, channel.prefetch(1)];
|
|
191
|
+
case 3:
|
|
192
|
+
_a.sent();
|
|
193
|
+
return [2 /*return*/];
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
subscribe: function (_a) {
|
|
199
|
+
var topic = _a.topic, _fb = _a.fromBeginning;
|
|
200
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
201
|
+
return __generator(this, function (_b) {
|
|
202
|
+
switch (_b.label) {
|
|
203
|
+
case 0:
|
|
204
|
+
if (!channel)
|
|
205
|
+
throw new Error("[RabbitMQ] Consumer not connected");
|
|
206
|
+
subscribedTopic = topic;
|
|
207
|
+
return [4 /*yield*/, channel.assertExchange(topic, "fanout", { durable: true })];
|
|
208
|
+
case 1:
|
|
209
|
+
_b.sent();
|
|
210
|
+
queueName = topic + "." + groupId;
|
|
211
|
+
return [4 /*yield*/, channel.assertQueue(queueName, { durable: true })];
|
|
212
|
+
case 2:
|
|
213
|
+
_b.sent();
|
|
214
|
+
return [4 /*yield*/, channel.bindQueue(queueName, topic, "")];
|
|
215
|
+
case 3:
|
|
216
|
+
_b.sent();
|
|
217
|
+
return [2 /*return*/];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
run: function (opts) {
|
|
223
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
224
|
+
return __generator(this, function (_a) {
|
|
225
|
+
switch (_a.label) {
|
|
226
|
+
case 0:
|
|
227
|
+
if (!channel)
|
|
228
|
+
throw new Error("[RabbitMQ] Consumer not connected");
|
|
229
|
+
if (!opts.eachBatch) return [3 /*break*/, 2];
|
|
230
|
+
return [4 /*yield*/, runBatchMode(channel, queueName, subscribedTopic, opts.eachBatch, opts.eachBatchAutoResolve)];
|
|
231
|
+
case 1:
|
|
232
|
+
_a.sent();
|
|
233
|
+
return [3 /*break*/, 4];
|
|
234
|
+
case 2:
|
|
235
|
+
if (!opts.eachMessage) return [3 /*break*/, 4];
|
|
236
|
+
return [4 /*yield*/, runMessageMode(channel, queueName, subscribedTopic, opts.eachMessage)];
|
|
237
|
+
case 3:
|
|
238
|
+
_a.sent();
|
|
239
|
+
_a.label = 4;
|
|
240
|
+
case 4: return [2 /*return*/];
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
disconnect: function () {
|
|
246
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
247
|
+
return __generator(this, function (_a) {
|
|
248
|
+
switch (_a.label) {
|
|
249
|
+
case 0:
|
|
250
|
+
if (!channel) return [3 /*break*/, 2];
|
|
251
|
+
return [4 /*yield*/, channel.close().catch(function () { })];
|
|
252
|
+
case 1:
|
|
253
|
+
_a.sent();
|
|
254
|
+
channel = null;
|
|
255
|
+
_a.label = 2;
|
|
256
|
+
case 2: return [2 /*return*/];
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
// ─── Consumer modes ──────────────────────────────────────────────────────────
|
|
266
|
+
function runMessageMode(channel, queueName, topic, eachMessage) {
|
|
267
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
268
|
+
var _this = this;
|
|
269
|
+
return __generator(this, function (_a) {
|
|
270
|
+
switch (_a.label) {
|
|
271
|
+
case 0: return [4 /*yield*/, channel.consume(queueName, function (amqpMsg) { return __awaiter(_this, void 0, void 0, function () {
|
|
272
|
+
var message, _a;
|
|
273
|
+
var _this = this;
|
|
274
|
+
return __generator(this, function (_b) {
|
|
275
|
+
switch (_b.label) {
|
|
276
|
+
case 0:
|
|
277
|
+
if (!amqpMsg)
|
|
278
|
+
return [2 /*return*/];
|
|
279
|
+
message = toKafkaMessage(amqpMsg);
|
|
280
|
+
_b.label = 1;
|
|
281
|
+
case 1:
|
|
282
|
+
_b.trys.push([1, 3, , 4]);
|
|
283
|
+
return [4 /*yield*/, eachMessage({
|
|
284
|
+
topic: topic,
|
|
285
|
+
partition: 0,
|
|
286
|
+
message: message,
|
|
287
|
+
heartbeat: function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
288
|
+
return [2 /*return*/];
|
|
289
|
+
}); }); },
|
|
290
|
+
pause: function () { return function () { }; },
|
|
291
|
+
})];
|
|
292
|
+
case 2:
|
|
293
|
+
_b.sent();
|
|
294
|
+
channel.ack(amqpMsg);
|
|
295
|
+
return [3 /*break*/, 4];
|
|
296
|
+
case 3:
|
|
297
|
+
_a = _b.sent();
|
|
298
|
+
channel.nack(amqpMsg, false, true);
|
|
299
|
+
return [3 /*break*/, 4];
|
|
300
|
+
case 4: return [2 /*return*/];
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}); }, { noAck: false })];
|
|
304
|
+
case 1:
|
|
305
|
+
_a.sent();
|
|
306
|
+
return [2 /*return*/];
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
function runBatchMode(channel, queueName, topic, eachBatch, autoResolve) {
|
|
312
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
313
|
+
var BATCH_SIZE, BATCH_TIMEOUT_MS, batch, timer, flush;
|
|
314
|
+
var _this = this;
|
|
315
|
+
return __generator(this, function (_a) {
|
|
316
|
+
switch (_a.label) {
|
|
317
|
+
case 0:
|
|
318
|
+
BATCH_SIZE = 10;
|
|
319
|
+
BATCH_TIMEOUT_MS = 500;
|
|
320
|
+
batch = [];
|
|
321
|
+
timer = null;
|
|
322
|
+
flush = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
323
|
+
var current, resolved, payload, _i, current_1, m, _a, _b, current_2, m;
|
|
324
|
+
var _this = this;
|
|
325
|
+
return __generator(this, function (_c) {
|
|
326
|
+
switch (_c.label) {
|
|
327
|
+
case 0:
|
|
328
|
+
if (batch.length === 0)
|
|
329
|
+
return [2 /*return*/];
|
|
330
|
+
current = __spreadArrays(batch);
|
|
331
|
+
batch = [];
|
|
332
|
+
resolved = new Set();
|
|
333
|
+
payload = {
|
|
334
|
+
batch: {
|
|
335
|
+
messages: current.map(function (m) { return m.kafkaMsg; }),
|
|
336
|
+
topic: topic,
|
|
337
|
+
},
|
|
338
|
+
resolveOffset: function (offset) { return resolved.add(offset); },
|
|
339
|
+
heartbeat: function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
340
|
+
return [2 /*return*/];
|
|
341
|
+
}); }); },
|
|
342
|
+
isRunning: function () { return true; },
|
|
343
|
+
isStale: function () { return false; },
|
|
344
|
+
};
|
|
345
|
+
_c.label = 1;
|
|
346
|
+
case 1:
|
|
347
|
+
_c.trys.push([1, 3, , 4]);
|
|
348
|
+
return [4 /*yield*/, eachBatch(payload)];
|
|
349
|
+
case 2:
|
|
350
|
+
_c.sent();
|
|
351
|
+
for (_i = 0, current_1 = current; _i < current_1.length; _i++) {
|
|
352
|
+
m = current_1[_i];
|
|
353
|
+
if (autoResolve === false) {
|
|
354
|
+
if (resolved.has(m.kafkaMsg.offset)) {
|
|
355
|
+
channel.ack(m.amqpMsg);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
channel.nack(m.amqpMsg, false, true);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
channel.ack(m.amqpMsg);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return [3 /*break*/, 4];
|
|
366
|
+
case 3:
|
|
367
|
+
_a = _c.sent();
|
|
368
|
+
for (_b = 0, current_2 = current; _b < current_2.length; _b++) {
|
|
369
|
+
m = current_2[_b];
|
|
370
|
+
channel.nack(m.amqpMsg, false, true);
|
|
371
|
+
}
|
|
372
|
+
return [3 /*break*/, 4];
|
|
373
|
+
case 4: return [2 /*return*/];
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}); };
|
|
377
|
+
return [4 /*yield*/, channel.consume(queueName, function (amqpMsg) { return __awaiter(_this, void 0, void 0, function () {
|
|
378
|
+
return __generator(this, function (_a) {
|
|
379
|
+
switch (_a.label) {
|
|
380
|
+
case 0:
|
|
381
|
+
if (!amqpMsg)
|
|
382
|
+
return [2 /*return*/];
|
|
383
|
+
batch.push({ amqpMsg: amqpMsg, kafkaMsg: toKafkaMessage(amqpMsg) });
|
|
384
|
+
if (timer)
|
|
385
|
+
clearTimeout(timer);
|
|
386
|
+
if (!(batch.length >= BATCH_SIZE)) return [3 /*break*/, 2];
|
|
387
|
+
return [4 /*yield*/, flush()];
|
|
388
|
+
case 1:
|
|
389
|
+
_a.sent();
|
|
390
|
+
return [3 /*break*/, 3];
|
|
391
|
+
case 2:
|
|
392
|
+
timer = setTimeout(function () { return flush(); }, BATCH_TIMEOUT_MS);
|
|
393
|
+
_a.label = 3;
|
|
394
|
+
case 3: return [2 /*return*/];
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
}); }, { noAck: false })];
|
|
398
|
+
case 1:
|
|
399
|
+
_a.sent();
|
|
400
|
+
return [2 /*return*/];
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
// ─── Message conversion ──────────────────────────────────────────────────────
|
|
406
|
+
function toKafkaMessage(amqpMsg) {
|
|
407
|
+
var _a, _b, _c;
|
|
408
|
+
return {
|
|
409
|
+
key: ((_b = (_a = amqpMsg.properties) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b.key) ? Buffer.from(String(amqpMsg.properties.headers.key))
|
|
410
|
+
: null,
|
|
411
|
+
value: amqpMsg.content,
|
|
412
|
+
timestamp: String(Date.now()),
|
|
413
|
+
offset: amqpMsg.fields.deliveryTag.toString(),
|
|
414
|
+
headers: ((_c = amqpMsg.properties) === null || _c === void 0 ? void 0 : _c.headers) || {},
|
|
415
|
+
};
|
|
416
|
+
}
|