mirror of
https://github.com/hexastack/hexabot
synced 2025-04-16 21:56:00 +00:00
fix: socketio types
This commit is contained in:
parent
cea41c20bc
commit
38cabf5d2c
@ -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) {
|
||||
|
@ -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>;
|
||||
@ -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<unknown>[],
|
||||
)
|
||||
.filter((provider) => !!provider.instance);
|
||||
|
||||
for (const provider of allProviders) {
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user