From a84ef95158bea2290576d8e2a1fb6da1b79c6490 Mon Sep 17 00:00:00 2001 From: SaltedFish Date: Tue, 19 Dec 2023 17:49:09 +0800 Subject: [PATCH 1/4] fix: Fix applyLazyDecorator return when metadataList is empty --- src/auto-aspect-executor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auto-aspect-executor.ts b/src/auto-aspect-executor.ts index aa0fc4a..21a6152 100644 --- a/src/auto-aspect-executor.ts +++ b/src/auto-aspect-executor.ts @@ -61,7 +61,7 @@ export class AutoAspectExecutor implements OnModuleInit { target[methodName], ); if (!metadataList) { - return; + continue; } for (const aopMetadata of metadataList) { From 065f63ac1c15ba9f2b3cae6f7eef4c95e3a2dd37 Mon Sep 17 00:00:00 2001 From: SaltedFish Date: Tue, 19 Dec 2023 18:38:04 +0800 Subject: [PATCH 2/4] fix: Fix Reflection fail caused by invalid target method --- src/auto-aspect-executor.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/auto-aspect-executor.ts b/src/auto-aspect-executor.ts index 21a6152..a8f5100 100644 --- a/src/auto-aspect-executor.ts +++ b/src/auto-aspect-executor.ts @@ -15,7 +15,7 @@ export class AutoAspectExecutor implements OnModuleInit { private readonly discoveryService: DiscoveryService, private readonly metadataScanner: MetadataScanner, private readonly reflector: Reflector, - ) {} + ) { } onModuleInit() { this.bootstrapLazyDecorators(); @@ -56,9 +56,16 @@ export class AutoAspectExecutor implements OnModuleInit { const metadataKey = this.reflector.get(ASPECT, lazyDecorator.constructor); // instance에 method names 를 순회하면서 lazyDecorator.wrap을 적용함 for (const methodName of methodNames) { + // the target method is must be object or function + // @see: https://github.com/rbuckton/reflect-metadata/blob/9562d6395cc3901eaafaf8a6ed8bc327111853d5/Reflect.ts#L938 + const targetMethod = target[methodName]; + if (!targetMethod || (typeof targetMethod !== "object" && typeof targetMethod !== "function")) { + continue; + } + const metadataList: AopMetadata[] = this.reflector.get( metadataKey, - target[methodName], + targetMethod, ); if (!metadataList) { continue; From f32aee9926b8cd77027b008738744022c09f74d7 Mon Sep 17 00:00:00 2001 From: SaltedFish Date: Wed, 20 Dec 2023 16:16:13 +0800 Subject: [PATCH 3/4] chore: rename methodNames to propertyKeys at applyLazyDecorator --- src/auto-aspect-executor.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/auto-aspect-executor.ts b/src/auto-aspect-executor.ts index a8f5100..91c5ad2 100644 --- a/src/auto-aspect-executor.ts +++ b/src/auto-aspect-executor.ts @@ -47,7 +47,7 @@ export class AutoAspectExecutor implements OnModuleInit { : instanceWrapper.metatype.prototype; // Use scanFromPrototype for support nestjs 8 - const methodNames = this.metadataScanner.scanFromPrototype( + const propertyKeys = this.metadataScanner.scanFromPrototype( target, instanceWrapper.isDependencyTreeStatic() ? Object.getPrototypeOf(target) : target, (name) => name, @@ -55,24 +55,24 @@ export class AutoAspectExecutor implements OnModuleInit { const metadataKey = this.reflector.get(ASPECT, lazyDecorator.constructor); // instance에 method names 를 순회하면서 lazyDecorator.wrap을 적용함 - for (const methodName of methodNames) { + for (const propertyKey of propertyKeys) { // the target method is must be object or function // @see: https://github.com/rbuckton/reflect-metadata/blob/9562d6395cc3901eaafaf8a6ed8bc327111853d5/Reflect.ts#L938 - const targetMethod = target[methodName]; - if (!targetMethod || (typeof targetMethod !== "object" && typeof targetMethod !== "function")) { + const targetProperty = target[propertyKey]; + if (!targetProperty || (typeof targetProperty !== "object" && typeof targetProperty !== "function")) { continue; } const metadataList: AopMetadata[] = this.reflector.get( metadataKey, - targetMethod, + targetProperty, ); if (!metadataList) { continue; } for (const aopMetadata of metadataList) { - this.wrapMethod({ lazyDecorator, aopMetadata, methodName, target }); + this.wrapMethod({ lazyDecorator, aopMetadata, methodName: propertyKey, target }); } } } From 73e23819d013ce66e14814fc28417f2bf0b1f3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=A7=80=ED=9B=88?= Date: Wed, 20 Dec 2023 18:12:01 +0900 Subject: [PATCH 4/4] Update src/auto-aspect-executor.ts --- src/auto-aspect-executor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auto-aspect-executor.ts b/src/auto-aspect-executor.ts index 91c5ad2..7198a50 100644 --- a/src/auto-aspect-executor.ts +++ b/src/auto-aspect-executor.ts @@ -15,7 +15,7 @@ export class AutoAspectExecutor implements OnModuleInit { private readonly discoveryService: DiscoveryService, private readonly metadataScanner: MetadataScanner, private readonly reflector: Reflector, - ) { } + ) {} onModuleInit() { this.bootstrapLazyDecorators();