diff --git a/server/integration/sockets/events/src/app.controller.ts b/server/integration/sockets/events/src/app.controller.ts index 7db8ea7..1dfdb33 100644 --- a/server/integration/sockets/events/src/app.controller.ts +++ b/server/integration/sockets/events/src/app.controller.ts @@ -1,5 +1,5 @@ import { Controller } from '@server'; -import { Connection, Disconnect, Disconnecting, Event } from '@server/sockets'; +import { Connection, Disconnect, Event } from '@server/sockets'; import { Sequence } from './sequence'; @@ -17,11 +17,6 @@ export class AppController { this.sequence.push('disconnect'); } - @Disconnecting() - disconnecting() { - this.sequence.push('disconnecting'); - } - @Event('message') message() { this.sequence.push('event'); diff --git a/server/integration/sockets/events/test/socket-io.spec.ts b/server/integration/sockets/events/test/socket-io.spec.ts index ed18b04..f2c5808 100644 --- a/server/integration/sockets/events/test/socket-io.spec.ts +++ b/server/integration/sockets/events/test/socket-io.spec.ts @@ -46,7 +46,7 @@ describe('Socket.io :: Events', () => { }); }); - it('registers `disconnect` + `disconnecting` events', (done) => { + it('registers `disconnect` events', (done) => { socket = connect(`http://localhost:${port}`); socket.on('connect', () => { @@ -54,7 +54,6 @@ describe('Socket.io :: Events', () => { socket.disconnect(); setTimeout(() => { - expect(seq.push).toBeCalledWith('disconnecting'); expect(seq.push).toBeCalledWith('disconnect'); done(); diff --git a/server/integration/sockets/params/src/app.controller.ts b/server/integration/sockets/params/src/app.controller.ts index c07c004..d14105b 100644 --- a/server/integration/sockets/params/src/app.controller.ts +++ b/server/integration/sockets/params/src/app.controller.ts @@ -1,5 +1,5 @@ import { Controller } from '@server'; -import { Connection, Disconnect, Disconnecting, Event, Param, Server, Socket } from '@server/sockets'; +import { Connection, Disconnect, Event, Param, Server, Socket } from '@server/sockets'; import { Sequence } from './sequence'; @@ -23,14 +23,6 @@ export class AppController { this.sequence.push(`disconnect: ${server.constructor.name} ${socket.constructor.name}`); } - @Disconnecting() - disconnecting( - @Server() server: object, - @Socket() socket: object, - ) { - this.sequence.push(`disconnecting: ${server.constructor.name} ${socket.constructor.name}`); - } - @Event('message') message( @Server() server: object, diff --git a/server/integration/sockets/params/test/socket-io.spec.ts b/server/integration/sockets/params/test/socket-io.spec.ts index a68a82f..bf5611f 100644 --- a/server/integration/sockets/params/test/socket-io.spec.ts +++ b/server/integration/sockets/params/test/socket-io.spec.ts @@ -46,7 +46,7 @@ describe('Socket.io :: Params', () => { }); }); - it('registers `disconnect` + `disconnecting` events', (done) => { + it('registers `disconnect` events', (done) => { socket = connect(`http://localhost:${port}`); socket.on('connect', () => { @@ -54,7 +54,6 @@ describe('Socket.io :: Params', () => { socket.disconnect(); setTimeout(() => { - expect(seq.push).toBeCalledWith('disconnecting: Server Socket'); expect(seq.push).toBeCalledWith('disconnect: Server Socket'); done(); diff --git a/server/package-lock.json b/server/package-lock.json index 2b6859e..1dafc0d 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -1,12 +1,12 @@ { "name": "@decorators/server", - "version": "1.0.0-beta.17", + "version": "1.0.0-beta.18", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@decorators/server", - "version": "1.0.0-beta.17", + "version": "1.0.0-beta.18", "license": "MIT", "devDependencies": { "@decorators/di": "../di", @@ -26,7 +26,6 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "cookie-parser": "^1.4.6", - "ejs": "^3.1.9", "eslint": "^8.37.0", "eslint-plugin-import": "^2.27.5", "eslint-plugin-jsdoc": "^40.1.1", @@ -2765,7 +2764,9 @@ "version": "3.2.4", "resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz", "integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/asynckit": { "version": "0.4.0", @@ -3672,6 +3673,8 @@ "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.9.tgz", "integrity": "sha512-rC+QVNMJWv+MtPgkt0y+0rVEIdbtxVADApW9JXrUVlzHetgcyczP/E7DJmWJ4fJCZF2cPcBk0laWO9ZHMG3DmQ==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "jake": "^10.8.5" }, @@ -4634,6 +4637,8 @@ "resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz", "integrity": "sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "minimatch": "^5.0.1" } @@ -4643,6 +4648,8 @@ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "balanced-match": "^1.0.0" } @@ -4652,6 +4659,8 @@ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "brace-expansion": "^2.0.1" }, @@ -5823,6 +5832,8 @@ "resolved": "https://registry.npmjs.org/jake/-/jake-10.8.7.tgz", "integrity": "sha512-ZDi3aP+fG/LchyBzUM804VjddnwfSfsdeYkwt8NcbKRvo4rFkjhs456iLFn3k2ZUWvNe4i48WACDbza8fhq2+w==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "async": "^3.2.3", "chalk": "^4.0.2", diff --git a/server/package.json b/server/package.json index 9da620d..77693e4 100644 --- a/server/package.json +++ b/server/package.json @@ -22,7 +22,6 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "cookie-parser": "^1.4.6", - "ejs": "^3.1.9", "eslint": "^8.37.0", "eslint-plugin-import": "^2.27.5", "eslint-plugin-jsdoc": "^40.1.1", @@ -148,5 +147,5 @@ ] } }, - "version": "1.0.0-beta.17" + "version": "1.0.0-beta.18" } diff --git a/server/src/core/helpers/decorators.ts b/server/src/core/helpers/decorators.ts index 6c0b9d4..28d8b9f 100644 --- a/server/src/core/helpers/decorators.ts +++ b/server/src/core/helpers/decorators.ts @@ -90,6 +90,8 @@ export function Decorate(key: string, value: unknown) { Reflect.defineMetadata(key, value, metaTarget, descriptor ? undefined : propertyKey); - return descriptor ?? target; + if (descriptor) { + return descriptor; + } }; } diff --git a/server/src/core/helpers/param-validator.ts b/server/src/core/helpers/param-validator.ts index b123033..f34dcfa 100644 --- a/server/src/core/helpers/param-validator.ts +++ b/server/src/core/helpers/param-validator.ts @@ -42,13 +42,10 @@ export class ParamValidator { private async useMetadataValidator(type: ClassConstructor, meta: ParamMetadata, arg: any) { const instance = plainToInstance(type, arg); + const errors = await validate(instance, { validationError: { target: false } }); - if (this.hasDecorators(type)) { - const errors = await validate(instance, { validationError: { target: false } }); - - if (errors.length) { - throw new BadRequestError(`Invalid param "${meta.argName}".`, errors); - } + if (errors.length) { + throw new BadRequestError(`Invalid param "${meta.argName}".`, errors); } } } diff --git a/server/src/platforms/http/http.module.ts b/server/src/platforms/http/http.module.ts index c70f59a..6703185 100644 --- a/server/src/platforms/http/http.module.ts +++ b/server/src/platforms/http/http.module.ts @@ -34,6 +34,10 @@ export class HttpModule { return this.adapter.close(); } + getAdapter() { + return this.adapter as Adapter; + } + getHttpServer() { return this.server; } diff --git a/server/src/platforms/socket-io/socket-io-adapter.ts b/server/src/platforms/socket-io/socket-io-adapter.ts index 4f2b7ef..9ea10c0 100644 --- a/server/src/platforms/socket-io/socket-io-adapter.ts +++ b/server/src/platforms/socket-io/socket-io-adapter.ts @@ -22,6 +22,10 @@ export class SocketIoAdapter implements SocketsApplicationAdapter { } } + disconnect(socket: Socket) { + socket.disconnect(); + } + emit(socket: Socket, event: string, message: unknown) { socket.emit(event, message); } @@ -34,7 +38,7 @@ export class SocketIoAdapter implements SocketsApplicationAdapter { ns.on(EventType.CONNECTION, socket => event.handler(socket, EventType.CONNECTION)); } - if ([EventType.DISCONNECT, EventType.DISCONNECTING].includes(event.type as EventType)) { + if (event.type === EventType.DISCONNECT) { ns.on(EventType.CONNECTION, socket => { socket.on(event.type, (...args) => event.handler(socket, event.type, ...args)); }); diff --git a/server/src/platforms/sockets/decorators/events.ts b/server/src/platforms/sockets/decorators/events.ts index 56d9b77..6100c2c 100644 --- a/server/src/platforms/sockets/decorators/events.ts +++ b/server/src/platforms/sockets/decorators/events.ts @@ -15,13 +15,6 @@ export function Disconnect() { }); } -export function Disconnecting() { - return methodDecoratorFactory({ - source: SOURCE_TYPE, - type: EventType.DISCONNECTING, - }); -} - export function Event(event: string) { return methodDecoratorFactory({ event, diff --git a/server/src/platforms/sockets/helpers/constants/event-type.ts b/server/src/platforms/sockets/helpers/constants/event-type.ts index d955357..1b433c6 100644 --- a/server/src/platforms/sockets/helpers/constants/event-type.ts +++ b/server/src/platforms/sockets/helpers/constants/event-type.ts @@ -1,6 +1,5 @@ export enum EventType { CONNECTION = 'connection', DISCONNECT = 'disconnect', - DISCONNECTING = 'disconnecting', EVENT = 'event', } diff --git a/server/src/platforms/sockets/helpers/event-handler.ts b/server/src/platforms/sockets/helpers/event-handler.ts index bc14403..2b21aaf 100644 --- a/server/src/platforms/sockets/helpers/event-handler.ts +++ b/server/src/platforms/sockets/helpers/event-handler.ts @@ -1,8 +1,8 @@ import { Inject, Injectable, Optional } from '@decorators/di'; -import { GLOBAL_PIPE, Handler, HandlerCreator, ParamMetadata, ParamValidator, Pipeline, ProcessPipe } from '../../../core'; +import { GLOBAL_PIPE, HandlerCreator, ParamMetadata, ParamValidator, Pipeline, ProcessPipe } from '../../../core'; import { AckFunction } from '../types'; -import { ParameterType, SOCKETS_ADAPTER } from './constants'; +import { EventType, ParameterType, SOCKETS_ADAPTER } from './constants'; import { SocketsApplicationAdapter } from './sockets-application-adapter'; import { SocketsContext } from './sockets-context'; @@ -20,9 +20,10 @@ export class EventHandler extends HandlerCreator { createHandler( controller: InstanceType, methodName: string, + eventType: EventType, params: ParamMetadata[], pipes: ProcessPipe[], - ): Handler { + ) { const handler = controller[methodName].bind(controller); return async (...args: unknown[]) => { @@ -63,6 +64,10 @@ export class EventHandler extends HandlerCreator { if (message instanceof Error) { await context.emit('error', this.message(message)); + if (eventType === EventType.CONNECTION) { + await this.adapter.disconnect(socket()); + } + return; } diff --git a/server/src/platforms/sockets/helpers/event-resolver.ts b/server/src/platforms/sockets/helpers/event-resolver.ts index 3cf0bf2..367b1fe 100644 --- a/server/src/platforms/sockets/helpers/event-resolver.ts +++ b/server/src/platforms/sockets/helpers/event-resolver.ts @@ -31,6 +31,7 @@ export class EventResolver { const handler = this.eventHandler.createHandler( controller, metadata.methodName, + metadata.type as EventType, metadata.params, eventPipes, ); diff --git a/server/src/platforms/sockets/helpers/sockets-application-adapter.ts b/server/src/platforms/sockets/helpers/sockets-application-adapter.ts index 8962bfa..f087ddc 100644 --- a/server/src/platforms/sockets/helpers/sockets-application-adapter.ts +++ b/server/src/platforms/sockets/helpers/sockets-application-adapter.ts @@ -6,6 +6,7 @@ export abstract class SocketsApplicationAdapter { abstract type: string; abstract attachServer(server: Server): void; abstract close(): void; + abstract disconnect(socket: unknown): Promise | void; abstract emit(socket: unknown, event: string, message: unknown): Promise | void; abstract events(events: AdapterEvent[]): void; abstract getParam(type: ParameterType, index: number, ...args: any[]): Promise<() => unknown> | (() => unknown); diff --git a/server/src/platforms/sockets/sockets.module.ts b/server/src/platforms/sockets/sockets.module.ts index f6dd86a..fbab782 100644 --- a/server/src/platforms/sockets/sockets.module.ts +++ b/server/src/platforms/sockets/sockets.module.ts @@ -34,6 +34,10 @@ export class SocketsModule { return this.adapter.close(); } + getAdapter() { + return this.adapter as Adapter; + } + getHttpServer() { return this.server; }