mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Merge pull request #11025 from nestjs/revert-11018-revert-10809-fix/durable-payload-regression
Revert "Revert "fix(core,microservices): inject the context when the tree is not durable""
This commit is contained in:
@@ -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 { InstanceWrapper } from '../injector/instance-wrapper';
|
||||
import { ContextId, InstanceWrapper } from '../injector/instance-wrapper';
|
||||
import { InstanceToken, Module } from '../injector/module';
|
||||
import { GraphInspector } from '../inspector/graph-inspector';
|
||||
import {
|
||||
@@ -250,6 +250,9 @@ export class MiddlewareModule<
|
||||
const proxy = await this.createProxy(instance);
|
||||
return this.registerHandler(applicationRef, routeInfo, proxy);
|
||||
}
|
||||
|
||||
const isTreeDurable = wrapper.isDependencyTreeDurable();
|
||||
|
||||
await this.registerHandler(
|
||||
applicationRef,
|
||||
routeInfo,
|
||||
@@ -259,19 +262,7 @@ export class MiddlewareModule<
|
||||
next: () => void,
|
||||
) => {
|
||||
try {
|
||||
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 contextId = this.getContextId(req, isTreeDurable);
|
||||
const contextInstance = await this.injector.loadPerContext(
|
||||
instance,
|
||||
moduleRef,
|
||||
@@ -369,4 +360,20 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,13 +355,16 @@ export class RouterExplorer {
|
||||
) {
|
||||
const { instance } = instanceWrapper;
|
||||
const collection = moduleRef.controllers;
|
||||
|
||||
const isTreeDurable = instanceWrapper.isDependencyTreeDurable();
|
||||
|
||||
return async <TRequest extends Record<any, any>, TResponse>(
|
||||
req: TRequest,
|
||||
res: TResponse,
|
||||
next: () => void,
|
||||
) => {
|
||||
try {
|
||||
const contextId = this.getContextId(req);
|
||||
const contextId = this.getContextId(req, isTreeDurable);
|
||||
const contextInstance = await this.injector.loadPerContext(
|
||||
instance,
|
||||
moduleRef,
|
||||
@@ -397,6 +400,7 @@ export class RouterExplorer {
|
||||
|
||||
private getContextId<T extends Record<any, unknown> = any>(
|
||||
request: T,
|
||||
isTreeDurable: boolean,
|
||||
): ContextId {
|
||||
const contextId = ContextIdFactory.getByRequest(request);
|
||||
if (!request[REQUEST_CONTEXT_ID as any]) {
|
||||
@@ -406,10 +410,9 @@ export class RouterExplorer {
|
||||
writable: false,
|
||||
configurable: false,
|
||||
});
|
||||
this.container.registerRequestProvider(
|
||||
contextId.getParent ? contextId.payload : request,
|
||||
contextId,
|
||||
);
|
||||
|
||||
const requestProviderValue = isTreeDurable ? contextId.payload : request;
|
||||
this.container.registerRequestProvider(requestProviderValue, contextId);
|
||||
}
|
||||
return contextId;
|
||||
}
|
||||
|
||||
@@ -205,13 +205,15 @@ 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);
|
||||
contextId = this.getContextId(dataOrContextHost, isTreeDurable);
|
||||
args.shift();
|
||||
} else {
|
||||
const [data, reqCtx] = args;
|
||||
@@ -220,11 +222,7 @@ export class ListenersController {
|
||||
data,
|
||||
reqCtx as BaseRpcContext,
|
||||
);
|
||||
contextId = this.getContextId(request);
|
||||
this.container.registerRequestProvider(
|
||||
contextId.getParent ? contextId.payload : request,
|
||||
contextId,
|
||||
);
|
||||
contextId = this.getContextId(request, isTreeDurable);
|
||||
dataOrContextHost = request;
|
||||
}
|
||||
const contextInstance = await this.injector.loadPerContext(
|
||||
@@ -270,7 +268,10 @@ export class ListenersController {
|
||||
return requestScopedHandler;
|
||||
}
|
||||
|
||||
private getContextId<T extends RequestContext = any>(request: T): ContextId {
|
||||
private getContextId<T extends RequestContext = any>(
|
||||
request: T,
|
||||
isTreeDurable: boolean,
|
||||
): ContextId {
|
||||
const contextId = ContextIdFactory.getByRequest(request);
|
||||
if (!request[REQUEST_CONTEXT_ID as any]) {
|
||||
Object.defineProperty(request, REQUEST_CONTEXT_ID, {
|
||||
@@ -279,10 +280,9 @@ export class ListenersController {
|
||||
writable: false,
|
||||
configurable: false,
|
||||
});
|
||||
this.container.registerRequestProvider(
|
||||
contextId.getParent ? contextId.payload : request,
|
||||
contextId,
|
||||
);
|
||||
|
||||
const requestProviderValue = isTreeDurable ? contextId.payload : request;
|
||||
this.container.registerRequestProvider(requestProviderValue, contextId);
|
||||
}
|
||||
return contextId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user