2023-11-07 07:26:45 -08:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { assert, jsonStringify } from 'n8n-workflow';
|
2023-02-10 06:02:47 -08:00
|
|
|
import type { IPushDataType } from '@/Interfaces';
|
2023-11-27 00:11:52 -08:00
|
|
|
import type { Logger } from '@/Logger';
|
2023-11-10 06:04:26 -08:00
|
|
|
import type { User } from '@db/entities/User';
|
2024-01-12 02:48:58 -08:00
|
|
|
import type { MultiMainSetup } from '@/services/orchestration/main/MultiMainSetup.ee';
|
2023-02-10 06:02:47 -08:00
|
|
|
|
2023-11-07 07:26:45 -08:00
|
|
|
/**
|
|
|
|
* Abstract class for two-way push communication.
|
|
|
|
* Keeps track of user sessions and enables sending messages.
|
|
|
|
*
|
|
|
|
* @emits message when a message is received from a client
|
|
|
|
*/
|
|
|
|
export abstract class AbstractPush<T> extends EventEmitter {
|
2023-02-10 06:02:47 -08:00
|
|
|
protected connections: Record<string, T> = {};
|
|
|
|
|
2023-11-07 07:26:45 -08:00
|
|
|
protected userIdBySessionId: Record<string, string> = {};
|
|
|
|
|
2023-02-10 06:02:47 -08:00
|
|
|
protected abstract close(connection: T): void;
|
2024-01-12 02:48:58 -08:00
|
|
|
protected abstract sendToOneConnection(connection: T, data: string): void;
|
2023-02-10 06:02:47 -08:00
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
constructor(
|
|
|
|
protected readonly logger: Logger,
|
|
|
|
private readonly multiMainSetup: MultiMainSetup,
|
|
|
|
) {
|
2023-11-07 07:26:45 -08:00
|
|
|
super();
|
|
|
|
}
|
2023-10-25 07:35:22 -07:00
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
protected add(sessionId: string, userId: User['id'], connection: T) {
|
2023-11-07 07:26:45 -08:00
|
|
|
const { connections, userIdBySessionId: userIdsBySessionId } = this;
|
2023-10-25 07:35:22 -07:00
|
|
|
this.logger.debug('Add editor-UI session', { sessionId });
|
2023-02-10 06:02:47 -08:00
|
|
|
|
|
|
|
const existingConnection = connections[sessionId];
|
2024-01-12 02:48:58 -08:00
|
|
|
|
2023-02-10 06:02:47 -08:00
|
|
|
if (existingConnection) {
|
2024-01-12 02:48:58 -08:00
|
|
|
// Make sure to remove existing connection with the same ID
|
2023-02-10 06:02:47 -08:00
|
|
|
this.close(existingConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
connections[sessionId] = connection;
|
2023-11-07 07:26:45 -08:00
|
|
|
userIdsBySessionId[sessionId] = userId;
|
|
|
|
}
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
protected onMessageReceived(sessionId: string, msg: unknown) {
|
2023-11-07 07:26:45 -08:00
|
|
|
this.logger.debug('Received message from editor-UI', { sessionId, msg });
|
2024-01-12 02:48:58 -08:00
|
|
|
|
2023-11-07 07:26:45 -08:00
|
|
|
const userId = this.userIdBySessionId[sessionId];
|
2024-01-12 02:48:58 -08:00
|
|
|
|
|
|
|
this.emit('message', { sessionId, userId, msg });
|
2023-02-10 06:02:47 -08:00
|
|
|
}
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
protected remove(sessionId?: string) {
|
|
|
|
if (!sessionId) return;
|
|
|
|
|
|
|
|
this.logger.debug('Removed editor-UI session', { sessionId });
|
|
|
|
|
|
|
|
delete this.connections[sessionId];
|
|
|
|
delete this.userIdBySessionId[sessionId];
|
2023-02-10 06:02:47 -08:00
|
|
|
}
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
private sendToSessions(type: IPushDataType, data: unknown, sessionIds: string[]) {
|
2023-11-07 07:26:45 -08:00
|
|
|
this.logger.debug(`Send data of type "${type}" to editor-UI`, {
|
|
|
|
dataType: type,
|
|
|
|
sessionIds: sessionIds.join(', '),
|
|
|
|
});
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
const stringifiedPayload = jsonStringify({ type, data }, { replaceCircularRefs: true });
|
2023-11-07 07:26:45 -08:00
|
|
|
|
|
|
|
for (const sessionId of sessionIds) {
|
|
|
|
const connection = this.connections[sessionId];
|
|
|
|
assert(connection);
|
2024-01-12 02:48:58 -08:00
|
|
|
this.sendToOneConnection(connection, stringifiedPayload);
|
2023-11-07 07:26:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
sendToAllSessions(type: IPushDataType, data?: unknown) {
|
2023-11-07 07:26:45 -08:00
|
|
|
this.sendToSessions(type, data, Object.keys(this.connections));
|
|
|
|
}
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
sendToOneSession(type: IPushDataType, data: unknown, sessionId: string) {
|
|
|
|
/**
|
|
|
|
* Multi-main setup: In a manual webhook execution, the main process that
|
|
|
|
* handles a webhook might not be the same as the main process that created
|
|
|
|
* the webhook. If so, the handler process commands the creator process to
|
|
|
|
* relay the former's execution lifecyle events to the creator's frontend.
|
|
|
|
*/
|
|
|
|
if (this.multiMainSetup.isEnabled && !this.hasSessionId(sessionId)) {
|
|
|
|
const payload = { type, args: data, sessionId };
|
|
|
|
|
|
|
|
void this.multiMainSetup.publish('relay-execution-lifecycle-event', payload);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.connections[sessionId] === undefined) {
|
2023-10-25 07:35:22 -07:00
|
|
|
this.logger.error(`The session "${sessionId}" is not registered.`, { sessionId });
|
2023-02-10 06:02:47 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-07 07:26:45 -08:00
|
|
|
this.sendToSessions(type, data, [sessionId]);
|
|
|
|
}
|
2023-02-10 06:02:47 -08:00
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
sendToUsers(type: IPushDataType, data: unknown, userIds: Array<User['id']>) {
|
2023-11-07 07:26:45 -08:00
|
|
|
const { connections } = this;
|
|
|
|
const userSessionIds = Object.keys(connections).filter((sessionId) =>
|
|
|
|
userIds.includes(this.userIdBySessionId[sessionId]),
|
|
|
|
);
|
2023-02-10 06:02:47 -08:00
|
|
|
|
2023-11-07 07:26:45 -08:00
|
|
|
this.sendToSessions(type, data, userSessionIds);
|
2023-02-10 06:02:47 -08:00
|
|
|
}
|
2023-12-22 02:39:58 -08:00
|
|
|
|
|
|
|
closeAllConnections() {
|
|
|
|
for (const sessionId in this.connections) {
|
|
|
|
// Signal the connection that we want to close it.
|
|
|
|
// We are not removing the sessions here because it should be
|
|
|
|
// the implementation's responsibility to do so once the connection
|
|
|
|
// has actually closed.
|
|
|
|
this.close(this.connections[sessionId]);
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 02:48:58 -08:00
|
|
|
|
|
|
|
hasSessionId(sessionId: string) {
|
|
|
|
return this.connections[sessionId] !== undefined;
|
|
|
|
}
|
2023-02-10 06:02:47 -08:00
|
|
|
}
|