Skip to content

Commit 7ac42b0

Browse files
fix(): context id factory improvements
1 parent 5d24da3 commit 7ac42b0

4 files changed

Lines changed: 52 additions & 16 deletions

File tree

packages/core/helpers/context-id-factory.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ContextId } from '../injector/instance-wrapper';
2+
import { REQUEST_CONTEXT_ID } from '../router/request/request-constants';
23

34
export function createContextId(): ContextId {
45
/**
@@ -11,3 +12,31 @@ export function createContextId(): ContextId {
1112
*/
1213
return { id: Math.random() };
1314
}
15+
16+
export class ContextIdFactory {
17+
/**
18+
* Generates a context identifier based on the request object.
19+
*/
20+
public static create(): ContextId {
21+
return createContextId();
22+
}
23+
24+
/**
25+
* Generates a random identifier to track asynchronous execution context.
26+
* @param request request object
27+
*/
28+
public static getByRequest<T extends Record<any, any> = any>(
29+
request: T,
30+
): ContextId {
31+
if (!request) {
32+
return createContextId();
33+
}
34+
if (request[REQUEST_CONTEXT_ID as any]) {
35+
return request[REQUEST_CONTEXT_ID as any];
36+
}
37+
if (request.raw && request.raw[REQUEST_CONTEXT_ID]) {
38+
return request.raw[REQUEST_CONTEXT_ID];
39+
}
40+
return createContextId();
41+
}
42+
}

packages/core/middleware/middleware-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { isUndefined, validatePath } from '@nestjs/common/utils/shared.utils';
1111
import { ApplicationConfig } from '../application-config';
1212
import { InvalidMiddlewareException } from '../errors/exceptions/invalid-middleware.exception';
1313
import { RuntimeException } from '../errors/exceptions/runtime.exception';
14-
import { createContextId } from '../helpers/context-id-factory';
14+
import { ContextIdFactory } from '../helpers';
1515
import { ExecutionContextHost } from '../helpers/execution-context-host';
1616
import { STATIC_CONTEXT } from '../injector/constants';
1717
import { NestContainer } from '../injector/container';
@@ -205,7 +205,7 @@ export class MiddlewareModule {
205205
next: () => void,
206206
) => {
207207
try {
208-
const contextId = req[REQUEST_CONTEXT_ID] || createContextId();
208+
const contextId = ContextIdFactory.getByRequest(req);
209209
if (!req[REQUEST_CONTEXT_ID]) {
210210
Object.defineProperty(req, REQUEST_CONTEXT_ID, {
211211
value: contextId,

packages/core/router/router-explorer.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ApplicationConfig } from '../application-config';
1515
import { UnknownRequestMappingException } from '../errors/exceptions/unknown-request-mapping.exception';
1616
import { GuardsConsumer } from '../guards/guards-consumer';
1717
import { GuardsContextCreator } from '../guards/guards-context-creator';
18-
import { createContextId } from '../helpers/context-id-factory';
18+
import { ContextIdFactory } from '../helpers';
1919
import { ExecutionContextHost } from '../helpers/execution-context-host';
2020
import { ROUTE_MAPPED_MESSAGE } from '../helpers/messages';
2121
import { RouterMethodFactory } from '../helpers/router-method-factory';
@@ -287,8 +287,6 @@ export class RouterExplorer {
287287
) => {
288288
try {
289289
const contextId = this.getContextId(req);
290-
this.container.registerRequestProvider(req, contextId);
291-
292290
const contextInstance = await this.injector.loadPerContext(
293291
instance,
294292
module,
@@ -325,12 +323,16 @@ export class RouterExplorer {
325323
private getContextId<T extends Record<any, any> = any>(
326324
request: T,
327325
): ContextId {
328-
if (request[REQUEST_CONTEXT_ID as any]) {
329-
return request[REQUEST_CONTEXT_ID as any];
330-
}
331-
if (request.raw && request.raw[REQUEST_CONTEXT_ID]) {
332-
return request.raw[REQUEST_CONTEXT_ID];
326+
const contextId = ContextIdFactory.getByRequest(request);
327+
if (!request[REQUEST_CONTEXT_ID as any]) {
328+
Object.defineProperty(request, REQUEST_CONTEXT_ID, {
329+
value: contextId,
330+
enumerable: false,
331+
writable: false,
332+
configurable: false,
333+
});
334+
this.container.registerRequestProvider(request, contextId);
333335
}
334-
return createContextId();
336+
return contextId;
335337
}
336338
}

packages/microservices/listeners-controller.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
2-
import { createContextId } from '@nestjs/core/helpers/context-id-factory';
2+
import { ContextIdFactory } from '@nestjs/core';
33
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host';
44
import { STATIC_CONTEXT } from '@nestjs/core/injector/constants';
55
import { NestContainer } from '@nestjs/core/injector/container';
@@ -120,14 +120,19 @@ export class ListenersController {
120120
return async (...args: unknown[]) => {
121121
try {
122122
const [data, reqCtx] = args;
123-
const contextId = createContextId();
124-
125-
const req = RequestContextHost.create(
123+
const request = RequestContextHost.create(
126124
pattern,
127125
data,
128126
reqCtx as BaseRpcContext,
129127
);
130-
this.container.registerRequestProvider(req, contextId);
128+
const contextId = ContextIdFactory.getByRequest(request);
129+
/**Object.defineProperty(request, REQUEST_CONTEXT_ID, {
130+
value: contextId,
131+
enumerable: false,
132+
writable: false,
133+
configurable: false,
134+
});*/
135+
this.container.registerRequestProvider(request, contextId);
131136

132137
const contextInstance = await this.injector.loadPerContext(
133138
instance,

0 commit comments

Comments
 (0)