Skip to content

Commit

Permalink
fix(core): fix durable context id resolution for enhancers (#11305)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 21, 2023
1 parent b106733 commit aa6baa4
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 11 deletions.
21 changes: 21 additions & 0 deletions integration/scopes/src/durable/durable.guard.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
if (typeof this.durableService === 'undefined') {
throw new Error('Durable service is undefined');
}
this.instanceCounter++;
return true;
}
}
10 changes: 9 additions & 1 deletion integration/scopes/src/durable/durable.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
2 changes: 1 addition & 1 deletion packages/core/exceptions/base-exception-filter-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions packages/core/guards/guards-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 13 additions & 1 deletion packages/core/helpers/context-creator.ts
Original file line number Diff line number Diff line change
@@ -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<T extends any[], R extends any[]>(
Expand Down Expand Up @@ -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;
}
}
9 changes: 7 additions & 2 deletions packages/core/interceptors/interceptors-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions packages/core/pipes/pipes-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions packages/microservices/context/exception-filters-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit aa6baa4

Please sign in to comment.