From 9141d245a8b2b1094cc79bfe3a21186f69510ea7 Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Thu, 2 Feb 2023 13:06:19 +0100 Subject: [PATCH] Revert "fix(core,microservices): inject the context when the tree is not durable" --- packages/core/middleware/middleware-module.ts | 35 ++++++++----------- packages/core/router/router-explorer.ts | 13 +++---- .../microservices/listeners-controller.ts | 22 ++++++------ 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/packages/core/middleware/middleware-module.ts b/packages/core/middleware/middleware-module.ts index ed736abb9..3885bab2d 100644 --- a/packages/core/middleware/middleware-module.ts +++ b/packages/core/middleware/middleware-module.ts @@ -18,7 +18,7 @@ import { ExecutionContextHost } from '../helpers/execution-context-host'; import { STATIC_CONTEXT } from '../injector/constants'; import { NestContainer } from '../injector/container'; import { Injector } from '../injector/injector'; -import { ContextId, InstanceWrapper } from '../injector/instance-wrapper'; +import { InstanceWrapper } from '../injector/instance-wrapper'; import { InstanceToken, Module } from '../injector/module'; import { GraphInspector } from '../inspector/graph-inspector'; import { @@ -250,9 +250,6 @@ export class MiddlewareModule< const proxy = await this.createProxy(instance); return this.registerHandler(applicationRef, routeInfo, proxy); } - - const isTreeDurable = wrapper.isDependencyTreeDurable(); - await this.registerHandler( applicationRef, routeInfo, @@ -262,7 +259,19 @@ export class MiddlewareModule< next: () => void, ) => { try { - const contextId = this.getContextId(req, isTreeDurable); + const contextId = ContextIdFactory.getByRequest(req); + if (!req[REQUEST_CONTEXT_ID]) { + Object.defineProperty(req, REQUEST_CONTEXT_ID, { + value: contextId, + enumerable: false, + writable: false, + configurable: false, + }); + this.container.registerRequestProvider( + contextId.getParent ? contextId.payload : req, + contextId, + ); + } const contextInstance = await this.injector.loadPerContext( instance, moduleRef, @@ -360,20 +369,4 @@ export class MiddlewareModule< router(path, middlewareFunction); } - - private getContextId(request: unknown, isTreeDurable: boolean): ContextId { - const contextId = ContextIdFactory.getByRequest(request); - if (!request[REQUEST_CONTEXT_ID]) { - Object.defineProperty(request, REQUEST_CONTEXT_ID, { - value: contextId, - enumerable: false, - writable: false, - configurable: false, - }); - - const requestProviderValue = isTreeDurable ? contextId.payload : request; - this.container.registerRequestProvider(requestProviderValue, contextId); - } - return contextId; - } } diff --git a/packages/core/router/router-explorer.ts b/packages/core/router/router-explorer.ts index 843ca5d2d..bddc4ae94 100644 --- a/packages/core/router/router-explorer.ts +++ b/packages/core/router/router-explorer.ts @@ -355,16 +355,13 @@ export class RouterExplorer { ) { const { instance } = instanceWrapper; const collection = moduleRef.controllers; - - const isTreeDurable = instanceWrapper.isDependencyTreeDurable(); - return async , TResponse>( req: TRequest, res: TResponse, next: () => void, ) => { try { - const contextId = this.getContextId(req, isTreeDurable); + const contextId = this.getContextId(req); const contextInstance = await this.injector.loadPerContext( instance, moduleRef, @@ -400,7 +397,6 @@ export class RouterExplorer { private getContextId = any>( request: T, - isTreeDurable: boolean, ): ContextId { const contextId = ContextIdFactory.getByRequest(request); if (!request[REQUEST_CONTEXT_ID as any]) { @@ -410,9 +406,10 @@ export class RouterExplorer { writable: false, configurable: false, }); - - const requestProviderValue = isTreeDurable ? contextId.payload : request; - this.container.registerRequestProvider(requestProviderValue, contextId); + this.container.registerRequestProvider( + contextId.getParent ? contextId.payload : request, + contextId, + ); } return contextId; } diff --git a/packages/microservices/listeners-controller.ts b/packages/microservices/listeners-controller.ts index d6a43c989..6fa85d93a 100644 --- a/packages/microservices/listeners-controller.ts +++ b/packages/microservices/listeners-controller.ts @@ -205,15 +205,13 @@ export class ListenersController { const collection = moduleRef.controllers; const { instance } = wrapper; - const isTreeDurable = wrapper.isDependencyTreeDurable(); - const requestScopedHandler: MessageHandler = async (...args: unknown[]) => { try { let contextId: ContextId; let [dataOrContextHost] = args; if (dataOrContextHost instanceof RequestContextHost) { - contextId = this.getContextId(dataOrContextHost, isTreeDurable); + contextId = this.getContextId(dataOrContextHost); args.shift(); } else { const [data, reqCtx] = args; @@ -222,7 +220,11 @@ export class ListenersController { data, reqCtx as BaseRpcContext, ); - contextId = this.getContextId(request, isTreeDurable); + contextId = this.getContextId(request); + this.container.registerRequestProvider( + contextId.getParent ? contextId.payload : request, + contextId, + ); dataOrContextHost = request; } const contextInstance = await this.injector.loadPerContext( @@ -268,10 +270,7 @@ export class ListenersController { return requestScopedHandler; } - private getContextId( - request: T, - isTreeDurable: boolean, - ): ContextId { + private getContextId(request: T): ContextId { const contextId = ContextIdFactory.getByRequest(request); if (!request[REQUEST_CONTEXT_ID as any]) { Object.defineProperty(request, REQUEST_CONTEXT_ID, { @@ -280,9 +279,10 @@ export class ListenersController { writable: false, configurable: false, }); - - const requestProviderValue = isTreeDurable ? contextId.payload : request; - this.container.registerRequestProvider(requestProviderValue, contextId); + this.container.registerRequestProvider( + contextId.getParent ? contextId.payload : request, + contextId, + ); } return contextId; }