diff --git a/api/src/utils/constants/session-store.ts b/api/src/utils/constants/session-store.ts index 8f5593040..8754770cb 100644 --- a/api/src/utils/constants/session-store.ts +++ b/api/src/utils/constants/session-store.ts @@ -10,7 +10,7 @@ import MongoStore from 'connect-mongo'; import { config } from '@/config'; -let sessionStore: MongoStore = null; +let sessionStore: MongoStore | null = null; export const getSessionStore = () => { if (!sessionStore) { diff --git a/api/src/websocket/services/socket-event-dispatcher.service.ts b/api/src/websocket/services/socket-event-dispatcher.service.ts index 6c337a5df..bd25dd37b 100644 --- a/api/src/websocket/services/socket-event-dispatcher.service.ts +++ b/api/src/websocket/services/socket-event-dispatcher.service.ts @@ -13,11 +13,13 @@ import { OnModuleInit, } from '@nestjs/common'; import { ModulesContainer } from '@nestjs/core'; +import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { LoggerService } from '@/logger/logger.service'; import { SocketEventMetadataStorage } from '../storage/socket-event-metadata.storage'; +import { SocketRequest } from '../utils/socket-request'; import { SocketResponse } from '../utils/socket-response'; type Handler = (req: any, res: SocketResponse) => Promise; @@ -43,22 +45,23 @@ export class SocketEventDispatcherService implements OnModuleInit { async handleEvent( socketMethod: SocketMethod, path: string, - req: any, + req: SocketRequest, res: SocketResponse, ) { try { const handlers = this.routeHandlers[socketMethod]; - - const [_, handler] = Array.from(handlers.entries()).find(([key, _]) => { + const foundHandler = Array.from(handlers.entries()).find(([key, _]) => { const urlPathname = new URL(req.url, 'http://localhost').pathname; const keyUrlPathName = new URL(key, 'http://localhost').pathname; return urlPathname === keyUrlPathName; }); - if (!handler) { + if (!foundHandler) { return res.status(HttpStatus.NOT_FOUND).send({ message: 'Not Found' }); } + + const [_, handler] = foundHandler; return await handler(req, res); } catch (error) { return this.handleException(error, res); @@ -68,7 +71,10 @@ export class SocketEventDispatcherService implements OnModuleInit { onModuleInit() { const allProviders = Array.from(this.modulesContainer.values()) .map((module) => module.providers.values()) - .reduce((prev, curr) => prev.concat(Array.from(curr)), []) + .reduce( + (prev, curr) => prev.concat(Array.from(curr)), + [] as InstanceWrapper[], + ) .filter((provider) => !!provider.instance); for (const provider of allProviders) { diff --git a/api/src/websocket/utils/gateway-options.ts b/api/src/websocket/utils/gateway-options.ts index 1c0c7e155..1b4e9ce30 100644 --- a/api/src/websocket/utils/gateway-options.ts +++ b/api/src/websocket/utils/gateway-options.ts @@ -53,7 +53,7 @@ export const buildWebSocketGatewayOptions = (): Partial => { ...(config.sockets.onlyAllowOrigins && { cors: { origin: (origin, cb) => { - if (config.sockets.onlyAllowOrigins.includes(origin)) { + if (origin && config.sockets.onlyAllowOrigins.includes(origin)) { cb(null, true); } else { // eslint-disable-next-line no-console diff --git a/api/src/websocket/websocket.gateway.spec.ts b/api/src/websocket/websocket.gateway.spec.ts index c24407fcb..b47cedaca 100644 --- a/api/src/websocket/websocket.gateway.spec.ts +++ b/api/src/websocket/websocket.gateway.spec.ts @@ -66,27 +66,22 @@ describe('WebsocketGateway', () => { }); it('should connect successfully', async () => { - ioClient.connect(); - await new Promise((resolve) => { - ioClient.on('connect', () => { - expect(true).toBe(true); - resolve(); - }); + ioClient.on('connect', () => { + expect(true).toBe(true); }); + ioClient.connect(); ioClient.disconnect(); }); it('should emit "OK" on "healthcheck"', async () => { - ioClient.connect(); - await new Promise((resolve) => { - ioClient.on('connect', () => { - ioClient.emit('healthcheck', 'Hello world!'); - ioClient.on('event', (data) => { - expect(data).toBe('OK'); - resolve(); - }); + ioClient.on('connect', () => { + ioClient.emit('healthcheck', 'Hello world'); + + ioClient.on('event', (data) => { + expect(data).toBe('OK'); }); }); + ioClient.connect(); ioClient.disconnect(); }); });