n8n
Version:
n8n Workflow Automation Tool
94 lines • 3.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageEventBusDestination = void 0;
const uuid_1 = require("uuid");
const typedi_1 = require("typedi");
const Logger_1 = require("../../Logger");
const eventDestinations_repository_1 = require("../../databases/repositories/eventDestinations.repository");
const License_1 = require("../../License");
class MessageEventBusDestination {
constructor(eventBusInstance, options) {
var _a, _b, _c, _d, _e;
this.credentials = {};
this.logger = typedi_1.Container.get(Logger_1.Logger);
this.license = typedi_1.Container.get(License_1.License);
this.eventBusInstance = eventBusInstance;
this.id = !options.id || options.id.length !== 36 ? (0, uuid_1.v4)() : options.id;
this.__type = (_a = options.__type) !== null && _a !== void 0 ? _a : "$$AbstractMessageEventBusDestination";
this.label = (_b = options.label) !== null && _b !== void 0 ? _b : 'Log Destination';
this.enabled = (_c = options.enabled) !== null && _c !== void 0 ? _c : false;
this.subscribedEvents = (_d = options.subscribedEvents) !== null && _d !== void 0 ? _d : [];
this.anonymizeAuditMessages = (_e = options.anonymizeAuditMessages) !== null && _e !== void 0 ? _e : false;
if (options.credentials)
this.credentials = options.credentials;
this.logger.debug(`${this.__type}(${this.id}) event destination constructed`);
}
startListening() {
if (this.enabled) {
this.eventBusInstance.on(this.getId(), async (msg, confirmCallback) => {
await this.receiveFromEventBus({ msg, confirmCallback });
});
this.logger.debug(`${this.id} listener started`);
}
}
stopListening() {
this.eventBusInstance.removeAllListeners(this.getId());
}
enable() {
this.enabled = true;
this.startListening();
}
disable() {
this.enabled = false;
this.stopListening();
}
getId() {
return this.id;
}
hasSubscribedToEvent(msg) {
if (!this.enabled)
return false;
for (const eventName of this.subscribedEvents) {
if (eventName === '*' || msg.eventName.startsWith(eventName)) {
return true;
}
}
return false;
}
async saveToDb() {
const data = {
id: this.getId(),
destination: this.serialize(),
};
const dbResult = await typedi_1.Container.get(eventDestinations_repository_1.EventDestinationsRepository).upsert(data, {
skipUpdateIfNoValuesChanged: true,
conflictPaths: ['id'],
});
return dbResult;
}
async deleteFromDb() {
return await MessageEventBusDestination.deleteFromDb(this.getId());
}
static async deleteFromDb(id) {
const dbResult = await typedi_1.Container.get(eventDestinations_repository_1.EventDestinationsRepository).delete({ id });
return dbResult;
}
serialize() {
return {
__type: this.__type,
id: this.getId(),
label: this.label,
enabled: this.enabled,
subscribedEvents: this.subscribedEvents,
anonymizeAuditMessages: this.anonymizeAuditMessages,
};
}
toString() {
return JSON.stringify(this.serialize());
}
close() {
this.stopListening();
}
}
exports.MessageEventBusDestination = MessageEventBusDestination;
//# sourceMappingURL=MessageEventBusDestination.ee.js.map