diff --git a/integration/scopes/src/durable/durable.guard.ts b/integration/scopes/src/durable/durable.guard.ts new file mode 100644 index 00000000000..8868af8096a --- /dev/null +++ b/integration/scopes/src/durable/durable.guard.ts @@ -0,0 +1,21 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + Scope, +} from '@nestjs/common'; +import { DurableService } from './durable.service'; + +@Injectable({ scope: Scope.REQUEST, durable: true }) +export class DurableGuard implements CanActivate { + public instanceCounter = 0; + constructor(private readonly durableService: DurableService) {} + + public async canActivate(context: ExecutionContext): Promise { + if (typeof this.durableService === 'undefined') { + throw new Error('Durable service is undefined'); + } + this.instanceCounter++; + return true; + } +} diff --git a/integration/scopes/src/durable/durable.module.ts b/integration/scopes/src/durable/durable.module.ts index ed312900f28..d749135bdc9 100644 --- a/integration/scopes/src/durable/durable.module.ts +++ b/integration/scopes/src/durable/durable.module.ts @@ -1,9 +1,17 @@ import { Module } from '@nestjs/common'; +import { APP_GUARD } from '@nestjs/core'; import { DurableController } from './durable.controller'; +import { DurableGuard } from './durable.guard'; import { DurableService } from './durable.service'; @Module({ controllers: [DurableController], - providers: [DurableService], + providers: [ + DurableService, + { + provide: APP_GUARD, + useClass: DurableGuard, + }, + ], }) export class DurableModule {} diff --git a/packages/core/exceptions/base-exception-filter-context.ts b/packages/core/exceptions/base-exception-filter-context.ts index aa99712277d..40ff0a156d2 100644 --- a/packages/core/exceptions/base-exception-filter-context.ts +++ b/packages/core/exceptions/base-exception-filter-context.ts @@ -50,7 +50,7 @@ export class BaseExceptionFilterContext extends ContextCreator { return null; } const instanceHost = instanceWrapper.getInstanceByContextId( - contextId, + this.getContextId(contextId, instanceWrapper), inquirerId, ); return instanceHost && instanceHost.instance; diff --git a/packages/core/guards/guards-context-creator.ts b/packages/core/guards/guards-context-creator.ts index e82a8fb04d5..7c9963b3a93 100644 --- a/packages/core/guards/guards-context-creator.ts +++ b/packages/core/guards/guards-context-creator.ts @@ -69,7 +69,7 @@ export class GuardsContextCreator extends ContextCreator { return null; } const instanceHost = instanceWrapper.getInstanceByContextId( - contextId, + this.getContextId(contextId, instanceWrapper), inquirerId, ); return instanceHost && instanceHost.instance; @@ -104,7 +104,12 @@ export class GuardsContextCreator extends ContextCreator { const scopedGuardWrappers = this.config.getGlobalRequestGuards() as InstanceWrapper[]; const scopedGuards = iterate(scopedGuardWrappers) - .map(wrapper => wrapper.getInstanceByContextId(contextId, inquirerId)) + .map(wrapper => + wrapper.getInstanceByContextId( + this.getContextId(contextId, wrapper), + inquirerId, + ), + ) .filter(host => !!host) .map(host => host.instance) .toArray(); diff --git a/packages/core/helpers/context-creator.ts b/packages/core/helpers/context-creator.ts index 5028cc9733b..fa8c9c4ac4b 100644 --- a/packages/core/helpers/context-creator.ts +++ b/packages/core/helpers/context-creator.ts @@ -1,6 +1,6 @@ import { Controller } from '@nestjs/common/interfaces'; import { STATIC_CONTEXT } from '../injector/constants'; -import { ContextId } from '../injector/instance-wrapper'; +import { ContextId, InstanceWrapper } from '../injector/instance-wrapper'; export abstract class ContextCreator { public abstract createConcreteContext( @@ -51,4 +51,16 @@ export abstract class ContextCreator { ): T { return Reflect.getMetadata(metadataKey, callback); } + + protected getContextId( + contextId: ContextId, + instanceWrapper: InstanceWrapper, + ): ContextId { + return contextId.getParent + ? contextId.getParent({ + token: instanceWrapper.token, + isTreeDurable: instanceWrapper.isDependencyTreeDurable(), + }) + : contextId; + } } diff --git a/packages/core/interceptors/interceptors-context-creator.ts b/packages/core/interceptors/interceptors-context-creator.ts index abc7dc90376..b05dcb445b8 100644 --- a/packages/core/interceptors/interceptors-context-creator.ts +++ b/packages/core/interceptors/interceptors-context-creator.ts @@ -74,7 +74,7 @@ export class InterceptorsContextCreator extends ContextCreator { return null; } const instanceHost = instanceWrapper.getInstanceByContextId( - contextId, + this.getContextId(contextId, instanceWrapper), inquirerId, ); return instanceHost && instanceHost.instance; @@ -108,7 +108,12 @@ export class InterceptorsContextCreator extends ContextCreator { const scopedInterceptorWrappers = this.config.getGlobalRequestInterceptors() as InstanceWrapper[]; const scopedInterceptors = iterate(scopedInterceptorWrappers) - .map(wrapper => wrapper.getInstanceByContextId(contextId, inquirerId)) + .map(wrapper => + wrapper.getInstanceByContextId( + this.getContextId(contextId, wrapper), + inquirerId, + ), + ) .filter(host => !!host) .map(host => host.instance) .toArray(); diff --git a/packages/core/pipes/pipes-context-creator.ts b/packages/core/pipes/pipes-context-creator.ts index 7776f855299..fa81402372b 100644 --- a/packages/core/pipes/pipes-context-creator.ts +++ b/packages/core/pipes/pipes-context-creator.ts @@ -64,7 +64,7 @@ export class PipesContextCreator extends ContextCreator { return null; } const instanceHost = instanceWrapper.getInstanceByContextId( - contextId, + this.getContextId(contextId, instanceWrapper), inquirerId, ); return instanceHost && instanceHost.instance; @@ -98,7 +98,12 @@ export class PipesContextCreator extends ContextCreator { const scopedPipeWrappers = this.config.getGlobalRequestPipes() as InstanceWrapper[]; const scopedPipes = iterate(scopedPipeWrappers) - .map(wrapper => wrapper.getInstanceByContextId(contextId, inquirerId)) + .map(wrapper => + wrapper.getInstanceByContextId( + this.getContextId(contextId, wrapper), + inquirerId, + ), + ) .filter(host => !!host) .map(host => host.instance) .toArray(); diff --git a/packages/microservices/context/exception-filters-context.ts b/packages/microservices/context/exception-filters-context.ts index c1ad4c1fc96..14d42da915e 100644 --- a/packages/microservices/context/exception-filters-context.ts +++ b/packages/microservices/context/exception-filters-context.ts @@ -6,9 +6,9 @@ import { BaseExceptionFilterContext } from '@nestjs/core/exceptions/base-excepti import { STATIC_CONTEXT } from '@nestjs/core/injector/constants'; import { NestContainer } from '@nestjs/core/injector/container'; import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; +import { iterate } from 'iterare'; import { Observable } from 'rxjs'; import { RpcExceptionsHandler } from '../exceptions/rpc-exceptions-handler'; -import { iterate } from 'iterare'; /** * @publicApi @@ -56,7 +56,12 @@ export class ExceptionFiltersContext extends BaseExceptionFilterContext { const scopedFilterWrappers = this.config.getGlobalRequestFilters() as InstanceWrapper[]; const scopedFilters = iterate(scopedFilterWrappers) - .map(wrapper => wrapper.getInstanceByContextId(contextId, inquirerId)) + .map(wrapper => + wrapper.getInstanceByContextId( + this.getContextId(contextId, wrapper), + inquirerId, + ), + ) .filter(host => !!host) .map(host => host.instance) .toArray();