diff --git a/jest.config.js b/jest.config.js index 90bc9fd..47e9393 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,10 +1,10 @@ module.exports = { transform: { - "^.+\\.(ts|tsx)$": ["ts-jest", {tsconfig: "test/tsconfig.json"}] + '^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: 'test/tsconfig.json' }] }, - moduleFileExtensions: ["ts", "js"], - coverageDirectory: "coverage", - collectCoverageFrom: ["src/**/*.ts", "src/**/*.js"], - testMatch: ["**/*.spec.(ts)"], - testEnvironment: "node", + moduleFileExtensions: ['ts', 'js'], + coverageDirectory: 'coverage', + collectCoverageFrom: ['src/**/*.ts', 'src/**/*.js', '!src/migrations/**'], + testMatch: ['**/*.spec.(ts)'], + testEnvironment: 'node' } diff --git a/src/controllers/handlers/ws-handler.ts b/src/controllers/handlers/ws-handler.ts index b372f37..b000c85 100644 --- a/src/controllers/handlers/ws-handler.ts +++ b/src/controllers/handlers/ws-handler.ts @@ -4,6 +4,7 @@ import { verify } from '@dcl/platform-crypto-middleware' import { AppComponents, WsUserData } from '../../types' import { normalizeAddress } from '../../utils/address' import { IUWebSocketEventMap, UWebSocketTransport } from '../../utils/UWebSocketTransport' +import { isNotAuthenticated } from '../../utils/wsUserData' const textDecoder = new TextDecoder() @@ -38,7 +39,7 @@ export async function registerWsHandler( logger.debug('ws open') const data = ws.getUserData() // just for type assertion - if (!data.auth) { + if (isNotAuthenticated(data)) { data.timeout = setTimeout(() => { try { logger.error('closing connection, no authchain received') @@ -53,7 +54,7 @@ export async function registerWsHandler( if (data.auth) { data.eventEmitter.emit('message', message) - } else { + } else if (isNotAuthenticated(data)) { clearTimeout(data.timeout) data.timeout = undefined diff --git a/src/types.ts b/src/types.ts index 337ca2b..80bda23 100644 --- a/src/types.ts +++ b/src/types.ts @@ -58,18 +58,20 @@ export type IHandler = { f: (res: HttpResponse, req: HttpRequest) => Promise } -export type WsUserData = - | { - isConnected: boolean - auth: false - timeout?: NodeJS.Timeout - } - | { - isConnected: boolean - eventEmitter: Emitter - auth: true - address: string - } +export type WsAuthenticatedUserData = { + isConnected: boolean + eventEmitter: Emitter + auth: true + address: string +} + +export type WsNotAuthenticatedUserData = { + isConnected: boolean + auth: false + timeout?: NodeJS.Timeout +} + +export type WsUserData = WsAuthenticatedUserData | WsNotAuthenticatedUserData export type InternalWebSocket = WebSocket diff --git a/src/utils/wsUserData.ts b/src/utils/wsUserData.ts new file mode 100644 index 0000000..cd4e57b --- /dev/null +++ b/src/utils/wsUserData.ts @@ -0,0 +1,5 @@ +import { WsNotAuthenticatedUserData, WsUserData } from '../types' + +export function isNotAuthenticated(data: WsUserData): data is WsNotAuthenticatedUserData { + return !data.auth +} diff --git a/test/unit/utils/wsUserData.spec.ts b/test/unit/utils/wsUserData.spec.ts new file mode 100644 index 0000000..fca803e --- /dev/null +++ b/test/unit/utils/wsUserData.spec.ts @@ -0,0 +1,28 @@ +import { WsUserData, WsNotAuthenticatedUserData } from '../../../src/types' +import { isNotAuthenticated } from '../../../src/utils/wsUserData' +import { IUWebSocketEventMap } from '../../../src/utils/UWebSocketTransport' +import { Emitter } from 'mitt' + +describe('wsUserData', () => { + describe('isNotAuthenticated', () => { + it('should return false if the user is authenticated', () => { + const data: WsUserData = { + auth: true, + isConnected: false, + eventEmitter: { emit: jest.fn() } as unknown as Emitter, + address: '0x123' + } + + expect(isNotAuthenticated(data)).toBe(false) + }) + + it('should return true with the correct type if the user is not authenticated', () => { + const data: WsUserData = { + auth: false, + isConnected: false + } + + expect(isNotAuthenticated(data)).toBe(true) + }) + }) +})