Files
n8n-enterprise-unlocked/packages/cli/src/push/sse.push.ts
कारतोफ्फेलस्क्रिप्ट™ 072c3db97d refactor(core): Rename push sessionId to pushRef (#8905)
2024-04-03 13:43:14 +02:00

39 lines
1.1 KiB
TypeScript

import SSEChannel from 'sse-channel';
import { Service } from 'typedi';
import { Logger } from '@/Logger';
import { AbstractPush } from './abstract.push';
import type { PushRequest, PushResponse } from './types';
import type { User } from '@db/entities/User';
import { OrchestrationService } from '@/services/orchestration.service';
type Connection = { req: PushRequest; res: PushResponse };
@Service()
export class SSEPush extends AbstractPush<Connection> {
readonly channel = new SSEChannel();
readonly connections: Record<string, Connection> = {};
constructor(logger: Logger, orchestrationService: OrchestrationService) {
super(logger, orchestrationService);
this.channel.on('disconnect', (_, { req }) => {
this.remove(req?.query?.pushRef);
});
}
add(pushRef: string, userId: User['id'], connection: Connection) {
super.add(pushRef, userId, connection);
this.channel.addClient(connection.req, connection.res);
}
protected close({ res }: Connection) {
res.end();
this.channel.removeClient(res);
}
protected sendToOneConnection(connection: Connection, data: string) {
this.channel.send(data, [connection.res]);
}
}