Skip to content

Commit e0f5e1d

Browse files
performance(nestjs) context creator improvements
1 parent e8c815d commit e0f5e1d

5 files changed

Lines changed: 57 additions & 33 deletions

File tree

packages/core/router/router-execution-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class RouterExecutionContext {
199199
guards: any[],
200200
instance: Controller,
201201
callback: (...args) => any,
202-
) {
202+
): Function | null {
203203
const canActivateFn = async (args: any[]) => {
204204
const canActivate = await this.guardsConsumer.tryActivate(
205205
guards,

packages/microservices/context/rpc-context-creator.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class RpcContextCreator {
4242
callback,
4343
module,
4444
);
45+
const fnCanActivate = this.createGuardsFn(guards, instance, callback);
4546
const handler = (args: any[]) => async () => {
4647
const [data, ...params] = args;
4748
const result = await this.pipesConsumer.applyPipes(
@@ -53,15 +54,8 @@ export class RpcContextCreator {
5354
};
5455

5556
return this.rpcProxy.create(async (...args) => {
56-
const canActivate = await this.guardsConsumer.tryActivate(
57-
guards,
58-
args,
59-
instance,
60-
callback,
61-
);
62-
if (!canActivate) {
63-
throw new RpcException(FORBIDDEN_MESSAGE);
64-
}
57+
fnCanActivate && (await fnCanActivate(args));
58+
6559
return await this.interceptorsConsumer.intercept(
6660
interceptors,
6761
args,
@@ -83,4 +77,23 @@ export class RpcContextCreator {
8377
const paramtypes = this.reflectCallbackParamtypes(instance, callback);
8478
return paramtypes && paramtypes.length ? paramtypes[0] : null;
8579
}
80+
81+
public createGuardsFn(
82+
guards: any[],
83+
instance: Controller,
84+
callback: (...args) => any,
85+
): Function | null {
86+
const canActivateFn = async (args: any[]) => {
87+
const canActivate = await this.guardsConsumer.tryActivate(
88+
guards,
89+
args,
90+
instance,
91+
callback,
92+
);
93+
if (!canActivate) {
94+
throw new RpcException(FORBIDDEN_MESSAGE);
95+
}
96+
};
97+
return guards.length ? canActivateFn : null;
98+
}
8699
}

packages/microservices/context/rpc-proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context.host';
12
import { Observable } from 'rxjs';
23
import { RpcExceptionsHandler } from '../exceptions/rpc-exceptions-handler';
3-
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context.host';
44

55
export class RpcProxy {
66
public create(
77
targetCallback: (...args) => Promise<Observable<any>>,
88
exceptionsHandler: RpcExceptionsHandler,
99
): (...args) => Promise<Observable<any>> {
1010
return async (...args) => {
11-
const host = new ExecutionContextHost(args);
1211
try {
1312
return await targetCallback(...args);
1413
} catch (e) {
14+
const host = new ExecutionContextHost(args);
1515
return exceptionsHandler.handle(e, host);
1616
}
1717
};

packages/websockets/context/ws-context-creator.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { Observable } from 'rxjs';
2-
import { WsProxy } from './ws-proxy';
3-
import { WsExceptionsHandler } from '../exceptions/ws-exceptions-handler';
4-
import { ExceptionFiltersContext } from './exception-filters-context';
5-
import { Controller } from '@nestjs/common/interfaces';
6-
import { PipesContextCreator } from '@nestjs/core/pipes/pipes-context-creator';
7-
import { PipesConsumer } from '@nestjs/core/pipes/pipes-consumer';
81
import { PARAMTYPES_METADATA } from '@nestjs/common/constants';
9-
import { GuardsContextCreator } from '@nestjs/core/guards/guards-context-creator';
10-
import { GuardsConsumer } from '@nestjs/core/guards/guards-consumer';
2+
import { Controller } from '@nestjs/common/interfaces';
113
import { FORBIDDEN_MESSAGE } from '@nestjs/core/guards/constants';
12-
import { WsException } from '../exceptions/ws-exception';
4+
import { GuardsConsumer } from '@nestjs/core/guards/guards-consumer';
5+
import { GuardsContextCreator } from '@nestjs/core/guards/guards-context-creator';
136
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';
147
import { InterceptorsContextCreator } from '@nestjs/core/interceptors/interceptors-context-creator';
8+
import { PipesConsumer } from '@nestjs/core/pipes/pipes-consumer';
9+
import { PipesContextCreator } from '@nestjs/core/pipes/pipes-context-creator';
10+
import { WsException } from '../exceptions/ws-exception';
11+
import { ExceptionFiltersContext } from './exception-filters-context';
12+
import { WsProxy } from './ws-proxy';
1513

1614
export class WsContextCreator {
1715
constructor(
@@ -43,6 +41,7 @@ export class WsContextCreator {
4341
callback,
4442
module,
4543
);
44+
const fnCanActivate = this.createGuardsFn(guards, instance, callback);
4645
const handler = (args: any[]) => async () => {
4746
const [client, data, ...params] = args;
4847
const result = await this.pipesConsumer.applyPipes(
@@ -54,15 +53,8 @@ export class WsContextCreator {
5453
};
5554

5655
return this.wsProxy.create(async (...args) => {
57-
const canActivate = await this.guardsConsumer.tryActivate(
58-
guards,
59-
args,
60-
instance,
61-
callback,
62-
);
63-
if (!canActivate) {
64-
throw new WsException(FORBIDDEN_MESSAGE);
65-
}
56+
fnCanActivate && (await fnCanActivate(args));
57+
6658
return await this.interceptorsConsumer.intercept(
6759
interceptors,
6860
args,
@@ -84,4 +76,23 @@ export class WsContextCreator {
8476
const paramtypes = this.reflectCallbackParamtypes(instance, callback);
8577
return paramtypes && paramtypes.length ? paramtypes[1] : null;
8678
}
79+
80+
public createGuardsFn(
81+
guards: any[],
82+
instance: Controller,
83+
callback: (...args) => any,
84+
): Function | null {
85+
const canActivateFn = async (args: any[]) => {
86+
const canActivate = await this.guardsConsumer.tryActivate(
87+
guards,
88+
args,
89+
instance,
90+
callback,
91+
);
92+
if (!canActivate) {
93+
throw new WsException(FORBIDDEN_MESSAGE);
94+
}
95+
};
96+
return guards.length ? canActivateFn : null;
97+
}
8798
}

packages/websockets/context/ws-proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { WsExceptionsHandler } from './../exceptions/ws-exceptions-handler';
21
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context.host';
2+
import { WsExceptionsHandler } from './../exceptions/ws-exceptions-handler';
33

44
export class WsProxy {
55
public create(
66
targetCallback: (...args) => Promise<void>,
77
exceptionsHandler: WsExceptionsHandler,
88
): (...args) => Promise<void> {
99
return async (...args) => {
10-
const host = new ExecutionContextHost(args);
1110
try {
1211
return await targetCallback(...args);
1312
} catch (e) {
13+
const host = new ExecutionContextHost(args);
1414
exceptionsHandler.handle(e, host);
1515
}
1616
};

0 commit comments

Comments
 (0)