• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nestjs / nest / 313c391e-2e75-4829-9f2c-92b11880aea0

27 Mar 2024 04:51AM UTC coverage: 92.146%. Remained the same
313c391e-2e75-4829-9f2c-92b11880aea0

Pull #13366

circleci

breeeew
fix(core): break reference chain to instance object

Reference caused in getContextFactory, and it stored in handlerMetadataStorage in Map, that never be purged
Pull Request #13366: fix(core): break reference chain to instance object

2561 of 3080 branches covered (83.15%)

2 of 3 new or added lines in 1 file covered. (66.67%)

6734 of 7308 relevant lines covered (92.15%)

17.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.0
/packages/core/helpers/context-utils.ts
1
import { ParamData } from '@nestjs/common';
2
import {
1✔
3
  PARAMTYPES_METADATA,
4
  RESPONSE_PASSTHROUGH_METADATA,
5
} from '@nestjs/common/constants';
6
import {
7
  ContextType,
8
  Controller,
9
  PipeTransform,
10
  Type,
11
} from '@nestjs/common/interfaces';
12
import { isFunction } from '@nestjs/common/utils/shared.utils';
1✔
13
import { ExecutionContextHost } from './execution-context-host';
1✔
14

15
export interface ParamProperties<T = any, IExtractor extends Function = any> {
16
  index: number;
17
  type: T | string;
18
  data: ParamData;
19
  pipes: PipeTransform[];
20
  extractValue: IExtractor;
21
}
22

23
export class ContextUtils {
1✔
24
  public mapParamType(key: string): string {
25
    const keyPair = key.split(':');
51✔
26
    return keyPair[0];
51✔
27
  }
28

29
  public reflectCallbackParamtypes(
30
    instance: Controller,
31
    methodName: string,
32
  ): any[] {
33
    return Reflect.getMetadata(PARAMTYPES_METADATA, instance, methodName);
18✔
34
  }
35

36
  public reflectCallbackMetadata<T = any>(
37
    instance: Controller,
38
    methodName: string,
39
    metadataKey: string,
40
  ): T {
41
    return Reflect.getMetadata(metadataKey, instance.constructor, methodName);
19✔
42
  }
43

44
  public reflectPassthrough(instance: Controller, methodName: string): boolean {
45
    return Reflect.getMetadata(
10✔
46
      RESPONSE_PASSTHROUGH_METADATA,
47
      instance.constructor,
48
      methodName,
49
    );
50
  }
51

52
  public getArgumentsLength<T>(keys: string[], metadata: T): number {
53
    return keys.length
25✔
54
      ? Math.max(...keys.map(key => metadata[key].index)) + 1
29✔
55
      : 0;
56
  }
57

58
  public createNullArray(length: number): any[] {
59
    const a = new Array(length);
11✔
60
    for (let i = 0; i < length; ++i) a[i] = undefined;
21✔
61
    return a;
11✔
62
  }
63

64
  public mergeParamsMetatypes(
65
    paramsProperties: ParamProperties[],
66
    paramtypes: any[],
67
  ): (ParamProperties & { metatype?: any })[] {
68
    if (!paramtypes) {
21✔
69
      return paramsProperties;
4✔
70
    }
71
    return paramsProperties.map(param => ({
21✔
72
      ...param,
73
      metatype: paramtypes[param.index],
74
    }));
75
  }
76

77
  public getCustomFactory(
78
    factory: (...args: unknown[]) => void,
79
    data: unknown,
80
    contextFactory: (args: unknown[]) => ExecutionContextHost,
81
  ): (...args: unknown[]) => unknown {
82
    return isFunction(factory)
6✔
83
      ? (...args: unknown[]) => factory(data, contextFactory(args))
1✔
84
      : () => null;
1✔
85
  }
86

87
  public getContextFactory<TContext extends string = ContextType>(
88
    contextType: TContext,
89
    instance?: object,
90
    callback?: Function,
91
  ): (args: unknown[]) => ExecutionContextHost {
92
    const type = instance && (instance.constructor as Type<unknown>);
25✔
93
    return (args: unknown[]) => {
25✔
NEW
94
      const ctx = new ExecutionContextHost(args, type, callback);
×
95
      ctx.setType(contextType);
×
96
      return ctx;
×
97
    };
98
  }
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc