n8n
Version:
n8n Workflow Automation Tool
78 lines • 3.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractPush = void 0;
const events_1 = require("events");
const n8n_workflow_1 = require("n8n-workflow");
class AbstractPush extends events_1.EventEmitter {
constructor(logger, orchestrationService) {
super();
this.logger = logger;
this.orchestrationService = orchestrationService;
this.connections = {};
this.userIdByPushRef = {};
}
add(pushRef, userId, connection) {
const { connections, userIdByPushRef } = this;
this.logger.debug('Add editor-UI session', { pushRef });
const existingConnection = connections[pushRef];
if (existingConnection) {
this.close(existingConnection);
}
connections[pushRef] = connection;
userIdByPushRef[pushRef] = userId;
}
onMessageReceived(pushRef, msg) {
this.logger.debug('Received message from editor-UI', { pushRef, msg });
const userId = this.userIdByPushRef[pushRef];
this.emit('message', { pushRef, userId, msg });
}
remove(pushRef) {
if (!pushRef)
return;
this.logger.debug('Removed editor-UI session', { pushRef });
delete this.connections[pushRef];
delete this.userIdByPushRef[pushRef];
}
sendTo(type, data, pushRefs) {
this.logger.debug(`Send data of type "${type}" to editor-UI`, {
dataType: type,
pushRefs: pushRefs.join(', '),
});
const stringifiedPayload = (0, n8n_workflow_1.jsonStringify)({ type, data }, { replaceCircularRefs: true });
for (const pushRef of pushRefs) {
const connection = this.connections[pushRef];
(0, n8n_workflow_1.assert)(connection);
this.sendToOneConnection(connection, stringifiedPayload);
}
}
sendToAll(type, data) {
this.sendTo(type, data, Object.keys(this.connections));
}
sendToOneSession(type, data, pushRef) {
if (this.orchestrationService.isMultiMainSetupEnabled && !this.hasPushRef(pushRef)) {
const payload = { type, args: data, pushRef };
void this.orchestrationService.publish('relay-execution-lifecycle-event', payload);
return;
}
if (this.connections[pushRef] === undefined) {
this.logger.error(`The session "${pushRef}" is not registered.`, { pushRef });
return;
}
this.sendTo(type, data, [pushRef]);
}
sendToUsers(type, data, userIds) {
const { connections } = this;
const userPushRefs = Object.keys(connections).filter((pushRef) => userIds.includes(this.userIdByPushRef[pushRef]));
this.sendTo(type, data, userPushRefs);
}
closeAllConnections() {
for (const pushRef in this.connections) {
this.close(this.connections[pushRef]);
}
}
hasPushRef(pushRef) {
return this.connections[pushRef] !== undefined;
}
}
exports.AbstractPush = AbstractPush;
//# sourceMappingURL=abstract.push.js.map