From e37b9f7d8131f33253961fc7aaf0d66e70b98190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Sun, 20 May 2018 15:02:32 +0200 Subject: [PATCH] bugfix(@nestjs/core) support circular deps in dynamic modules (#678) --- bundle/core/package.json | 1 + package-lock.json | 5 +++++ package.json | 1 + packages/core/injector/module-token-factory.ts | 14 ++++++-------- .../test/injector/module-token-factory.spec.ts | 10 +++++----- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/bundle/core/package.json b/bundle/core/package.json index cb93901c698..5d6efaa0245 100644 --- a/bundle/core/package.json +++ b/bundle/core/package.json @@ -9,6 +9,7 @@ "cors": "2.8.4", "express": "4.16.3", "iterare": "0.0.8", + "object-hash": "1.3.0", "optional": "0.1.4" }, "peerDependencies": { diff --git a/package-lock.json b/package-lock.json index 76986fefdc0..3390efb5481 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12132,6 +12132,11 @@ } } }, + "object-hash": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-1.3.0.tgz", + "integrity": "sha512-05KzQ70lSeGSrZJQXE5wNDiTkBJDlUT/myi6RX9dVIvz7a7Qh4oH93BQdiPMn27nldYvVQCKMUaM83AfizZlsQ==" + }, "object-visit": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/object-visit/-/object-visit-1.0.1.tgz", diff --git a/package.json b/package.json index 9bd3e6456b6..a4ab0601bf3 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "mqtt": "^2.16.0", "multer": "^1.3.0", "nats": "^0.8.4", + "object-hash": "^1.3.0", "opencollective": "^1.0.3", "optional": "^0.1.4", "pump": "^3.0.0", diff --git a/packages/core/injector/module-token-factory.ts b/packages/core/injector/module-token-factory.ts index 76c7e9f973f..9b0eba0a16e 100644 --- a/packages/core/injector/module-token-factory.ts +++ b/packages/core/injector/module-token-factory.ts @@ -1,13 +1,14 @@ -import { Type } from '@nestjs/common/interfaces/type.interface'; -import { SHARED_MODULE_METADATA } from '@nestjs/common/constants'; import { DynamicModule } from '@nestjs/common'; +import { SHARED_MODULE_METADATA } from '@nestjs/common/constants'; +import { Type } from '@nestjs/common/interfaces/type.interface'; +import * as hash from 'object-hash'; export class ModuleTokenFactory { public create( metatype: Type, scope: Type[], dynamicModuleMetadata?: Partial | undefined, - ) { + ): string { const reflectedScope = this.reflectScope(metatype); const isSingleScoped = reflectedScope === true; const opaqueToken = { @@ -15,7 +16,7 @@ export class ModuleTokenFactory { dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata), scope: isSingleScoped ? this.getScopeStack(scope) : reflectedScope, }; - return JSON.stringify(opaqueToken); + return hash(opaqueToken); } public getDynamicMetadataToken( @@ -43,10 +44,7 @@ export class ModuleTokenFactory { } private reflectScope(metatype: Type) { - const scope = Reflect.getMetadata( - SHARED_MODULE_METADATA, - metatype, - ); + const scope = Reflect.getMetadata(SHARED_MODULE_METADATA, metatype); return scope ? scope : 'global'; } } diff --git a/packages/core/test/injector/module-token-factory.spec.ts b/packages/core/test/injector/module-token-factory.spec.ts index 2dc2009acf4..6371fd1978e 100644 --- a/packages/core/test/injector/module-token-factory.spec.ts +++ b/packages/core/test/injector/module-token-factory.spec.ts @@ -1,7 +1,7 @@ -import * as sinon from 'sinon'; import { expect } from 'chai'; -import { ModuleTokenFactory } from '../../injector/module-token-factory'; +import * as hash from 'object-hash'; import { SingleScope } from '../../../common'; +import { ModuleTokenFactory } from '../../injector/module-token-factory'; describe('ModuleTokenFactory', () => { let factory: ModuleTokenFactory; @@ -14,7 +14,7 @@ describe('ModuleTokenFactory', () => { const scope = 'global'; const token = factory.create(Module as any, [Module as any], undefined); expect(token).to.be.deep.eq( - JSON.stringify({ + hash({ module: Module.name, dynamic: '', scope, @@ -28,7 +28,7 @@ describe('ModuleTokenFactory', () => { undefined, ); expect(token).to.be.deep.eq( - JSON.stringify({ + hash({ module: Module.name, dynamic: '', scope: [Module.name], @@ -44,7 +44,7 @@ describe('ModuleTokenFactory', () => { } as any, ); expect(token).to.be.deep.eq( - JSON.stringify({ + hash({ module: Module.name, dynamic: JSON.stringify({ components: [{}],