2022-12-07 06:13:36 -08:00
|
|
|
import SSEChannel from 'sse-channel';
|
|
|
|
import type { Request, Response } from 'express';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
import { LoggerProxy as Logger } from 'n8n-workflow';
|
2022-11-09 06:25:00 -08:00
|
|
|
import type { IPushData, IPushDataType } from '@/Interfaces';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
export class Push {
|
2022-12-07 06:13:36 -08:00
|
|
|
private channel = new SSEChannel();
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2022-12-07 06:13:36 -08:00
|
|
|
private connections: Record<string, Response> = {};
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
constructor() {
|
2022-12-07 06:13:36 -08:00
|
|
|
this.channel.on('disconnect', (channel: string, res: Response) => {
|
2019-06-23 03:35:23 -07:00
|
|
|
if (res.req !== undefined) {
|
2022-12-07 06:13:36 -08:00
|
|
|
const { sessionId } = res.req.query;
|
2022-12-29 03:20:43 -08:00
|
|
|
Logger.debug('Remove editor-UI session', { sessionId });
|
2022-12-07 06:13:36 -08:00
|
|
|
delete this.connections[sessionId as string];
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new push connection
|
|
|
|
*
|
|
|
|
* @param {string} sessionId The id of the session
|
2022-12-07 06:13:36 -08:00
|
|
|
* @param {Request} req The request
|
|
|
|
* @param {Response} res The response
|
2019-06-23 03:35:23 -07:00
|
|
|
*/
|
2022-12-07 06:13:36 -08:00
|
|
|
add(sessionId: string, req: Request, res: Response) {
|
2022-12-29 03:20:43 -08:00
|
|
|
Logger.debug('Add editor-UI session', { sessionId });
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
if (this.connections[sessionId] !== undefined) {
|
|
|
|
// Make sure to remove existing connection with the same session
|
|
|
|
// id if one exists already
|
|
|
|
this.connections[sessionId].end();
|
|
|
|
this.channel.removeClient(this.connections[sessionId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.connections[sessionId] = res;
|
|
|
|
this.channel.addClient(req, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends data to the client which is connected via a specific session
|
|
|
|
*
|
|
|
|
* @param {string} sessionId The session id of client to send data to
|
|
|
|
* @param {string} type Type of data to send
|
|
|
|
*/
|
2019-07-24 05:25:30 -07:00
|
|
|
|
2022-09-26 07:39:48 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-08-29 11:58:11 -07:00
|
|
|
send(type: IPushDataType, data: any, sessionId?: string) {
|
2019-07-24 05:25:30 -07:00
|
|
|
if (sessionId !== undefined && this.connections[sessionId] === undefined) {
|
2022-09-02 07:13:17 -07:00
|
|
|
Logger.error(`The session "${sessionId}" is not registered.`, { sessionId });
|
2019-06-23 03:35:23 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-01 20:43:01 -07:00
|
|
|
Logger.debug(`Send data of type "${type}" to editor-UI`, { dataType: type, sessionId });
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
const sendData: IPushData = {
|
|
|
|
type,
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
2019-06-23 03:35:23 -07:00
|
|
|
data,
|
|
|
|
};
|
|
|
|
|
2019-07-24 05:25:30 -07:00
|
|
|
if (sessionId === undefined) {
|
|
|
|
// Send to all connected clients
|
|
|
|
this.channel.send(JSON.stringify(sendData));
|
|
|
|
} else {
|
|
|
|
// Send only to a specific client
|
|
|
|
this.channel.send(JSON.stringify(sendData), [this.connections[sessionId]]);
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let activePushInstance: Push | undefined;
|
|
|
|
|
|
|
|
export function getInstance(): Push {
|
|
|
|
if (activePushInstance === undefined) {
|
|
|
|
activePushInstance = new Push();
|
|
|
|
}
|
|
|
|
|
|
|
|
return activePushInstance;
|
|
|
|
}
|