Skip to content

Commit a9e0002

Browse files
fix(core): assing unique id to module token
1 parent 53ef6b2 commit a9e0002

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

packages/core/injector/module-token-factory.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import { DynamicModule } from '@nestjs/common';
22
import { SHARED_MODULE_METADATA } from '@nestjs/common/constants';
33
import { Type } from '@nestjs/common/interfaces/type.interface';
4+
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
45
import stringify from 'fast-safe-stringify';
56
import * as hash from 'object-hash';
67

78
export class ModuleTokenFactory {
9+
private readonly moduleIdsCache = new WeakMap<Type<unknown>, string>();
10+
811
public create(
9-
metatype: Type<any>,
10-
scope: Type<any>[],
12+
metatype: Type<unknown>,
13+
scope: Type<unknown>[],
1114
dynamicModuleMetadata?: Partial<DynamicModule> | undefined,
1215
): string {
16+
const moduleId = this.getModuleId(metatype);
1317
const moduleScope = this.reflectScope(metatype);
1418
const isSingleScoped = moduleScope === true;
1519
const opaqueToken = {
20+
id: moduleId,
1621
module: this.getModuleName(metatype),
1722
dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata),
1823
scope: isSingleScoped ? this.getScopeStack(scope) : moduleScope,
@@ -45,6 +50,16 @@ export class ModuleTokenFactory {
4550
return stack.map(module => module.name);
4651
}
4752

53+
public getModuleId(metatype: Type<unknown>): string {
54+
let moduleId = this.moduleIdsCache.get(metatype);
55+
if (moduleId) {
56+
return moduleId;
57+
}
58+
moduleId = randomStringGenerator();
59+
this.moduleIdsCache.set(metatype, moduleId);
60+
return moduleId;
61+
}
62+
4863
public getModuleName(metatype: Type<any>): string {
4964
return metatype.name;
5065
}

packages/core/test/injector/module-token-factory.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { expect } from 'chai';
22
import stringify from 'fast-safe-stringify';
33
import * as hash from 'object-hash';
4+
import * as sinon from 'sinon';
45
import { SingleScope } from '../../../common';
56
import { ModuleTokenFactory } from '../../injector/module-token-factory';
67

78
describe('ModuleTokenFactory', () => {
9+
const moduleId = 'constId';
810
let factory: ModuleTokenFactory;
11+
912
beforeEach(() => {
1013
factory = new ModuleTokenFactory();
14+
sinon.stub(factory, 'getModuleId').returns(moduleId);
1115
});
1216
describe('create', () => {
1317
class Module {}
@@ -16,6 +20,7 @@ describe('ModuleTokenFactory', () => {
1620
const token = factory.create(Module as any, [Module], undefined);
1721
expect(token).to.be.deep.eq(
1822
hash({
23+
id: moduleId,
1924
module: Module.name,
2025
dynamic: '',
2126
scope,
@@ -27,6 +32,7 @@ describe('ModuleTokenFactory', () => {
2732
const token = factory.create(type, [Module], undefined);
2833
expect(token).to.be.deep.eq(
2934
hash({
35+
id: moduleId,
3036
module: Module.name,
3137
dynamic: '',
3238
scope: [Module.name],
@@ -40,6 +46,7 @@ describe('ModuleTokenFactory', () => {
4046
} as any);
4147
expect(token).to.be.deep.eq(
4248
hash({
49+
id: moduleId,
4350
module: Module.name,
4451
dynamic: stringify({
4552
providers: [{}],

0 commit comments

Comments
 (0)