Skip to content

Commit

Permalink
Merge pull request #490 from Hexastack/fix/socketio-strict-null-check
Browse files Browse the repository at this point in the history
fix: socketio types
  • Loading branch information
marrouchi authored Dec 30, 2024
2 parents cea41c2 + 38cabf5 commit fa8a433
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/src/utils/constants/session-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 11 additions & 5 deletions api/src/websocket/services/socket-event-dispatcher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
Expand All @@ -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);
Expand All @@ -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<unknown>[],
)
.filter((provider) => !!provider.instance);

for (const provider of allProviders) {
Expand Down
2 changes: 1 addition & 1 deletion api/src/websocket/utils/gateway-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const buildWebSocketGatewayOptions = (): Partial<ServerOptions> => {
...(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
Expand Down
23 changes: 9 additions & 14 deletions api/src/websocket/websocket.gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,22 @@ describe('WebsocketGateway', () => {
});

it('should connect successfully', async () => {
ioClient.connect();
await new Promise<void>((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<void>((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();
});
});

0 comments on commit fa8a433

Please sign in to comment.