Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
fix(server): beta.18 - fixed Decorate replacing parameter, removed di…
Browse files Browse the repository at this point in the history
…sconnecting event in sockets (#198)
  • Loading branch information
serhiisol authored Aug 31, 2023
1 parent eca468a commit 3423907
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 44 deletions.
7 changes: 1 addition & 6 deletions server/integration/sockets/events/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -17,11 +17,6 @@ export class AppController {
this.sequence.push('disconnect');
}

@Disconnecting()
disconnecting() {
this.sequence.push('disconnecting');
}

@Event('message')
message() {
this.sequence.push('event');
Expand Down
3 changes: 1 addition & 2 deletions server/integration/sockets/events/test/socket-io.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ describe('Socket.io :: Events', () => {
});
});

it('registers `disconnect` + `disconnecting` events', (done) => {
it('registers `disconnect` events', (done) => {
socket = connect(`http://localhost:${port}`);

socket.on('connect', () => {
jest.resetAllMocks();
socket.disconnect();

setTimeout(() => {
expect(seq.push).toBeCalledWith('disconnecting');
expect(seq.push).toBeCalledWith('disconnect');

done();
Expand Down
10 changes: 1 addition & 9 deletions server/integration/sockets/params/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions server/integration/sockets/params/test/socket-io.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ describe('Socket.io :: Params', () => {
});
});

it('registers `disconnect` + `disconnecting` events', (done) => {
it('registers `disconnect` events', (done) => {
socket = connect(`http://localhost:${port}`);

socket.on('connect', () => {
jest.resetAllMocks();
socket.disconnect();

setTimeout(() => {
expect(seq.push).toBeCalledWith('disconnecting: Server Socket');
expect(seq.push).toBeCalledWith('disconnect: Server Socket');

done();
Expand Down
19 changes: 15 additions & 4 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -148,5 +147,5 @@
]
}
},
"version": "1.0.0-beta.17"
"version": "1.0.0-beta.18"
}
4 changes: 3 additions & 1 deletion server/src/core/helpers/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
}
9 changes: 3 additions & 6 deletions server/src/core/helpers/param-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
4 changes: 4 additions & 0 deletions server/src/platforms/http/http.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class HttpModule {
return this.adapter.close();
}

getAdapter<Adapter extends HttpApplicationAdapter>() {
return this.adapter as Adapter;
}

getHttpServer() {
return this.server;
}
Expand Down
6 changes: 5 additions & 1 deletion server/src/platforms/socket-io/socket-io-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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));
});
Expand Down
7 changes: 0 additions & 7 deletions server/src/platforms/sockets/decorators/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export enum EventType {
CONNECTION = 'connection',
DISCONNECT = 'disconnect',
DISCONNECTING = 'disconnecting',
EVENT = 'event',
}
11 changes: 8 additions & 3 deletions server/src/platforms/sockets/helpers/event-handler.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -20,9 +20,10 @@ export class EventHandler extends HandlerCreator {
createHandler(
controller: InstanceType<any>,
methodName: string,
eventType: EventType,
params: ParamMetadata[],
pipes: ProcessPipe[],
): Handler {
) {
const handler = controller[methodName].bind(controller);

return async (...args: unknown[]) => {
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions server/src/platforms/sockets/helpers/event-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class EventResolver {
const handler = this.eventHandler.createHandler(
controller,
metadata.methodName,
metadata.type as EventType,
metadata.params,
eventPipes,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> | void;
abstract emit(socket: unknown, event: string, message: unknown): Promise<void> | void;
abstract events(events: AdapterEvent[]): void;
abstract getParam(type: ParameterType, index: number, ...args: any[]): Promise<() => unknown> | (() => unknown);
Expand Down
4 changes: 4 additions & 0 deletions server/src/platforms/sockets/sockets.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class SocketsModule {
return this.adapter.close();
}

getAdapter<Adapter extends SocketsApplicationAdapter>() {
return this.adapter as Adapter;
}

getHttpServer() {
return this.server;
}
Expand Down

0 comments on commit 3423907

Please sign in to comment.