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' ;
81import { 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' ;
113import { 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' ;
136import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer' ;
147import { 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
1614export 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}
0 commit comments