diff --git a/packages/nocodb/src/services/socket.gateway.spec.ts b/packages/nocodb/src/gateways/socket.gateway.spec.ts similarity index 89% rename from packages/nocodb/src/services/socket.gateway.spec.ts rename to packages/nocodb/src/gateways/socket.gateway.spec.ts index f31b6b0929..d507bb553a 100644 --- a/packages/nocodb/src/services/socket.gateway.spec.ts +++ b/packages/nocodb/src/gateways/socket.gateway.spec.ts @@ -1,5 +1,5 @@ import { Test } from '@nestjs/testing'; -import { SocketGateway } from './socket.gateway'; +import { SocketGateway } from './socket.gateway'; import type { TestingModule } from '@nestjs/testing'; describe('SocketGateway', () => { diff --git a/packages/nocodb/src/services/socket.gateway.ts b/packages/nocodb/src/gateways/socket.gateway.ts similarity index 100% rename from packages/nocodb/src/services/socket.gateway.ts rename to packages/nocodb/src/gateways/socket.gateway.ts diff --git a/packages/nocodb/src/modules/global/global.module.ts b/packages/nocodb/src/modules/global/global.module.ts index 361355d87e..12dd650325 100644 --- a/packages/nocodb/src/modules/global/global.module.ts +++ b/packages/nocodb/src/modules/global/global.module.ts @@ -1,7 +1,7 @@ import { Global, Module } from '@nestjs/common'; import { JwtModule, JwtService } from '@nestjs/jwt'; import { ExtractJwt } from 'passport-jwt'; -import { SocketGateway } from '../../services/socket.gateway'; +import { SocketGateway } from '../../gateways/socket.gateway'; import { Connection } from '../../connection/connection'; import { GlobalGuard } from '../../guards/global/global.guard'; import { MetaService } from '../../meta/meta.service'; diff --git a/packages/nocodb/src/services/socket.service.spec.ts b/packages/nocodb/src/services/socket.service.spec.ts deleted file mode 100644 index f31b6b0929..0000000000 --- a/packages/nocodb/src/services/socket.service.spec.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Test } from '@nestjs/testing'; -import { SocketGateway } from './socket.gateway'; -import type { TestingModule } from '@nestjs/testing'; - -describe('SocketGateway', () => { - let gateway: SocketGateway; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [SocketGateway], - }).compile(); - - gateway = module.get(SocketGateway); - }); - - it('should be defined', () => { - expect(gateway).toBeDefined(); - }); -}); diff --git a/packages/nocodb/src/services/socket.service.ts b/packages/nocodb/src/services/socket.service.ts deleted file mode 100644 index 192c24caaa..0000000000 --- a/packages/nocodb/src/services/socket.service.ts +++ /dev/null @@ -1,67 +0,0 @@ -import crypto from 'crypto'; -import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; -import { Inject, Injectable } from '@nestjs/common'; -import { HttpAdapterHost } from '@nestjs/core'; -import { T } from 'nc-help'; -import { Server } from 'socket.io'; -import { AuthGuard } from '@nestjs/passport'; -import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host'; -import { JwtStrategy } from '../strategies/jwt.strategy'; -import type { OnModuleInit } from '@nestjs/common'; -import type { Socket } from 'socket.io'; - -function getHash(str) { - return crypto.createHash('md5').update(str).digest('hex'); -} - -@WebSocketGateway({ - cors: { - origin: '*', - allowedHeaders: ['xc-auth'], - credentials: true, - }, -}) -@Injectable() -export class SocketGateway implements OnModuleInit { - // private server: HttpServer; - private clients: { [id: string]: Socket } = {}; - - constructor( - private jwtStrategy: JwtStrategy, - @Inject(HttpAdapterHost) private httpAdapterHost: HttpAdapterHost, - ) {} - - @WebSocketServer() - server: Server; - - async onModuleInit() { - this.server - .use(async (socket, next) => { - try { - const context = new ExecutionContextHost([socket.handshake as any]); - const guard = new (AuthGuard('jwt'))(context); - await guard.canActivate(context); - } catch {} - - next(); - }) - .on('connection', (socket) => { - this.clients[socket.id] = socket; - const id = getHash( - (process.env.NC_SERVER_UUID || T.id) + - (socket?.handshake as any)?.user?.id, - ); - - socket.on('page', (args) => { - T.page({ ...args, id }); - }); - socket.on('event', (args) => { - T.event({ ...args, id }); - }); - }); - } - - public get io() { - return this.server; - } -}