import * as sseChannel from 'sse-channel' ; import * as express from 'express' ; import { LoggerProxy as Logger } from 'n8n-workflow' ; import { IPushData , IPushDataType } from '.' ; export class Push { private channel : sseChannel ; private connections : { [ key ] : express . Response ; } = { } ; constructor ( ) { this . channel = new sseChannel ( { cors : { origins : [ 'http://localhost:8080' ] , } , } ) ; this . channel . on ( 'disconnect' , ( channel , res ) => { if ( res . req !== undefined ) { Logger . debug ( `Remove editor-UI session` , { sessionId : res . req . query . sessionId } ) ; delete this . connections [ res . req . query . sessionId as string ] ; } } ) ; } param { string } sessionId param { express . Request } req param { express . Response } res memberof add ( sessionId , req , res ) { Logger . debug ( `Add editor-UI session` , { sessionId } ) ; if ( this . connections [ sessionId ] !== undefined ) { this . connections [ sessionId ] . end ( ) ; this . channel . removeClient ( this . connections [ sessionId ] ) ; } this . connections [ sessionId ] = res ; this . channel . addClient ( req , res ) ; } param { string } sessionId param { string } type param { * } data memberof send ( type , data , sessionId ? ) { if ( sessionId !== undefined && this . connections [ sessionId ] === undefined ) { Logger . error ( `The session "${ sessionId }" is not registred.` , { sessionId } ) ; return ; } Logger . debug ( `Send data of type "${ type }" to editor-UI` , { dataType : type , sessionId } ) ; const sendData = { type , data , } ; if ( sessionId === undefined ) { this . channel . send ( JSON . stringify ( sendData ) ) ; } else { this . channel . send ( JSON . stringify ( sendData ) , [ this . connections [ sessionId ] ] ) ; } } } let activePushInstance ; export function getInstance ( ) { if ( activePushInstance === undefined ) { activePushInstance = new Push ( ) ; } return activePushInstance ; }