2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
2019-06-23 03:35:23 -07:00
|
|
|
// @ts-ignore
|
2022-04-08 14:32:08 -07:00
|
|
|
import sseChannel from 'sse-channel';
|
|
|
|
import express 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';
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
|
|
|
import { IPushData, IPushDataType } from '.';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
export class Push {
|
|
|
|
private channel: sseChannel;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
private connections: {
|
|
|
|
[key: string]: express.Response;
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
constructor() {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, new-cap
|
2019-06-23 03:35:23 -07:00
|
|
|
this.channel = new sseChannel({
|
|
|
|
cors: {
|
|
|
|
// Allow access also from frontend when developing
|
|
|
|
origins: ['http://localhost:8080'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
2019-06-23 03:35:23 -07:00
|
|
|
this.channel.on('disconnect', (channel: string, res: express.Response) => {
|
|
|
|
if (res.req !== undefined) {
|
2021-05-01 20:43:01 -07:00
|
|
|
Logger.debug(`Remove editor-UI session`, { sessionId: res.req.query.sessionId });
|
2020-04-09 11:23:33 -07:00
|
|
|
delete this.connections[res.req.query.sessionId as string];
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new push connection
|
|
|
|
*
|
|
|
|
* @param {string} sessionId The id of the session
|
|
|
|
* @param {express.Request} req The request
|
|
|
|
* @param {express.Response} res The response
|
|
|
|
* @memberof Push
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2019-06-23 03:35:23 -07:00
|
|
|
add(sessionId: string, req: express.Request, res: express.Response) {
|
2021-05-01 20:43:01 -07:00
|
|
|
Logger.debug(`Add editor-UI session`, { sessionId });
|
|
|
|
|
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
|
|
|
|
* @param {*} data
|
|
|
|
* @memberof Push
|
|
|
|
*/
|
2019-07-24 05:25:30 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
|
|
|
|
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;
|
|
|
|
}
|