Skip to content

Commit

Permalink
bugfix(@nestjs/websockets) lifecycle hooks multi execution
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed May 2, 2018
1 parent 154b865 commit 382d999
Show file tree
Hide file tree
Showing 32 changed files with 58 additions and 489 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
node_js:
- "7"
- "8"
- "10"
addons:
firefox: "latest"
before_script:
Expand Down
429 changes: 0 additions & 429 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"publish:beta": "./node_modules/.bin/lerna publish --npm-tag=beta -m \"chore(release) publish %s\""
},
"engines": {
"node": ">=6.11.0"
"node": ">= 8.9.0"
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export {
DynamicModule,
INestApplicationContext,
HttpServer,
Provider,
Type,
HttpServerFactory,
ArgumentsHost,
INestExpressApplication,
Expand Down
4 changes: 3 additions & 1 deletion packages/common/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ export * from './http/http-server.interface';
export * from './http/http-server-factory.interface';
export * from './features/arguments-host.interface';
export * from './nest-express-application.interface';
export * from './nest-fastify-application.interface';
export * from './nest-fastify-application.interface';
export * from './modules/provider.interface';
export * from './type.interface';
1 change: 1 addition & 0 deletions packages/common/interfaces/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './module-metadata.interface';
export * from './nest-module.interface';
export * from './on-init.interface';
export * from './dynamic-module.interface';
export * from './provider.interface';
2 changes: 1 addition & 1 deletion packages/common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"target": "es2017",
"sourceMap": false,
"allowJs": false,
"rootDir": "./",
Expand Down
10 changes: 5 additions & 5 deletions packages/core/router/router-execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,21 @@ export class RouterExecutionContext {
isResponseHandled,
httpStatusCode,
);
const handler = (args, req, res, next) => async () => {
fnApplyPipes && (await fnApplyPipes(args, req, res, next));
return callback.apply(instance, args);
};

return async (req, res, next) => {
const args = this.createNullArray(argsLength);
fnCanActivate && (await fnCanActivate([req, res]));

const handler = async () => {
fnApplyPipes && (await fnApplyPipes(args, req, res, next));
return callback.apply(instance, args);
};
const result = await this.interceptorsConsumer.intercept(
interceptors,
[req, res],
instance,
callback,
handler,
handler(args, req, res, next),
);
await fnHandleResponse(result, res);
};
Expand Down
16 changes: 6 additions & 10 deletions packages/core/router/router-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ export class RouterProxy {
targetCallback: RouterProxyCallback,
exceptionsHandler: ExceptionsHandler,
) {
return (req, res, next) => {
const host = new ExecutionContextHost([req, res]);
return async (req, res, next) => {
try {
Promise.resolve(targetCallback(req, res, next)).catch(e => {
exceptionsHandler.next(e, host);
});
await targetCallback(req, res, next);
} catch (e) {
const host = new ExecutionContextHost([req, res]);
exceptionsHandler.next(e, host);
}
};
Expand All @@ -24,13 +22,11 @@ export class RouterProxy {
targetCallback: (err, req, res, next) => void,
exceptionsHandler: ExceptionsHandler,
) {
return (err, req, res, next) => {
const host = new ExecutionContextHost([req, res]);
return async (err, req, res, next) => {
try {
Promise.resolve(targetCallback(err, req, res, next)).catch(e => {
exceptionsHandler.next(e, host);
});
await targetCallback(err, req, res, next);
} catch (e) {
const host = new ExecutionContextHost([req, res]);
exceptionsHandler.next(e, host);
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"target": "es2017",
"sourceMap": false,
"allowJs": false,
"rootDir": "./",
Expand Down
2 changes: 1 addition & 1 deletion packages/microservices/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"target": "es2017",
"sourceMap": false,
"allowJs": false,
"baseUrl": "./../../",
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"target": "es2017",
"sourceMap": false,
"allowJs": false,
"baseUrl": "./../../",
Expand Down
5 changes: 4 additions & 1 deletion packages/websockets/observable-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { ObservableSocketServer } from './interfaces/observable-socket-server.in

export class ObservableSocket {
public static create(server): ObservableSocketServer {
const init = new ReplaySubject();
init.next(server);

return {
init: new ReplaySubject(),
init,
connection: new Subject(),
disconnect: new Subject(),
server,
Expand Down
10 changes: 3 additions & 7 deletions packages/websockets/test/web-sockets-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ describe('WebSocketsController', () => {
(instance as any).subscribeDisconnectEvent = subscribeDisconnectEvent;
});

it('should call "next" method of server object with expected argument', () => {
instance.subscribeEvents(gateway, handlers, server as any);
expect(nextSpy.calledWith(server.server)).to.be.true;
});
it('should call "subscribeConnectionEvent" with expected arguments', () => {
instance.subscribeEvents(gateway, handlers, server as any);
expect(subscribeConnectionEvent.calledWith(gateway, server.connection)).to
Expand Down Expand Up @@ -242,7 +238,7 @@ describe('WebSocketsController', () => {

beforeEach(() => {
subscribe = sinon.spy();
event = { subscribe };
event = { subscribe, pipe: sinon.stub().returnsThis() };
});
it('should not call subscribe method when "afterInit" method not exists', () => {
instance.subscribeInitEvent(gateway, event);
Expand All @@ -260,7 +256,7 @@ describe('WebSocketsController', () => {

beforeEach(() => {
subscribe = sinon.spy();
event = { subscribe };
event = { subscribe, pipe: sinon.stub().returnsThis() };
});
it('should not call subscribe method when "handleConnection" method not exists', () => {
instance.subscribeConnectionEvent(gateway, event);
Expand All @@ -278,7 +274,7 @@ describe('WebSocketsController', () => {

beforeEach(() => {
subscribe = sinon.spy();
event = { subscribe };
event = { subscribe, pipe: sinon.stub().returnsThis() };
});
it('should not call subscribe method when "handleDisconnect" method not exists', () => {
instance.subscribeDisconnectEvent(gateway, event);
Expand Down
2 changes: 1 addition & 1 deletion packages/websockets/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"target": "es2017",
"sourceMap": false,
"allowJs": false,
"baseUrl": "./../../",
Expand Down
11 changes: 7 additions & 4 deletions packages/websockets/web-sockets-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { MiddlewareInjector } from './middleware-injector';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { WsContextCreator } from './context/ws-context-creator';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { mergeMap } from 'rxjs/operators';
import { mergeMap, distinctUntilChanged } from 'rxjs/operators';

export class WebSocketsController {
private readonly metadataExplorer = new GatewayMetadataExplorer(
Expand Down Expand Up @@ -85,7 +85,6 @@ export class WebSocketsController {
this.subscribeInitEvent(instance, init);
this.subscribeConnectionEvent(instance, connection);
this.subscribeDisconnectEvent(instance, disconnect);
init.next(server);

const handler = this.getConnectionHandler(
this,
Expand Down Expand Up @@ -123,13 +122,17 @@ export class WebSocketsController {

public subscribeConnectionEvent(instance: NestGateway, event: Subject<any>) {
if (instance.handleConnection) {
event.subscribe(instance.handleConnection.bind(instance));
event
.pipe(distinctUntilChanged())
.subscribe(instance.handleConnection.bind(instance));
}
}

public subscribeDisconnectEvent(instance: NestGateway, event: Subject<any>) {
if (instance.handleDisconnect) {
event.subscribe(instance.handleDisconnect.bind(instance));
event
.pipe(distinctUntilChanged())
.subscribe(instance.handleDisconnect.bind(instance));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
HttpStatus,
} from '@nestjs/common';
import { HttpException } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { _throw } from 'rxjs/observable/throw';

@Injectable()
export class ErrorsInterceptor implements NestInterceptor {
Expand All @@ -17,7 +16,7 @@ export class ErrorsInterceptor implements NestInterceptor {
): Observable<any> {
return call$.pipe(
catchError(err =>
_throw(new HttpException('Message', HttpStatus.BAD_GATEWAY)),
throwError(new HttpException('Message', HttpStatus.BAD_GATEWAY)),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { timeout } from 'rxjs/operators';

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface Response<T> {
Expand Down
3 changes: 1 addition & 2 deletions sample/02-gateways/src/events/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
WebSocketServer,
WsException,
} from '@nestjs/websockets';
import { Observable } from 'rxjs/Observable';
import { from } from 'rxjs/observable/from';
import { Observable, from } from 'rxjs';
import { map } from 'rxjs/operators';

@WebSocketGateway()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Catch, RpcExceptionFilter } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable, throwError } from 'rxjs';
import { RpcException } from '@nestjs/microservices';
import { _throw } from 'rxjs/observable/throw';

@Catch(RpcException)
export class ExceptionFilter implements RpcExceptionFilter {
catch(exception: RpcException): Observable<any> {
return _throw(exception.getError());
return throwError(exception.getError());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
Expand Down
2 changes: 1 addition & 1 deletion sample/03-microservices/src/math/math.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Transport,
MessagePattern,
} from '@nestjs/microservices';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';

@Controller()
export class MathController {
Expand Down
2 changes: 1 addition & 1 deletion sample/04-grpc/src/hero/hero.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
GrpcRoute,
ClientGrpc,
} from '@nestjs/microservices';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { grpcClientOptions } from './../grpc-client.options';
import { HeroById } from './interfaces/hero-by-id.interface';
import { Hero } from './interfaces/hero.interface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
HttpStatus,
} from '@nestjs/common';
import { HttpException } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { _throw } from 'rxjs/observable/throw';

@Injectable()
export class ExceptionInterceptor implements NestInterceptor {
Expand All @@ -17,7 +16,7 @@ export class ExceptionInterceptor implements NestInterceptor {
): Observable<any> {
return call$.pipe(
catchError(err =>
_throw(
throwError(
new HttpException(
'Exception interceptor message',
HttpStatus.BAD_GATEWAY,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
Expand Down
3 changes: 1 addition & 2 deletions sample/16-gateways-ws/src/events/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
WebSocketServer,
WsException,
} from '@nestjs/websockets';
import { Observable } from 'rxjs/Observable';
import { from } from 'rxjs/observable/from';
import { Observable, from } from 'rxjs';
import { map } from 'rxjs/operators';

@WebSocketGateway(8080)
Expand Down
2 changes: 1 addition & 1 deletion sample/19-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@nestjs/common": "^5.0.0",
"@nestjs/core": "^5.0.0",
"@nestjs/passport": "^1.0.2",
"@nestjs/passport": "^1.0.5",
"passport": "^0.4.0",
"passport-http-bearer": "^1.0.1",
"passport-jwt": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion sample/19-auth/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class AuthController {

@Get('data')
@UseGuards(AuthGuard('jwt'))
getSensitiveData() {
findAll() {
// this route is restricted
}
}
Loading

0 comments on commit 382d999

Please sign in to comment.