From ce0b4fbd881a5ed6ef0e9bb8ad70fe7c1c5eddcb Mon Sep 17 00:00:00 2001 From: JadenKim-dev Date: Sun, 30 Jun 2024 22:31:11 +0900 Subject: [PATCH 1/2] feat: grpc package name can be specified with service name --- packages/microservices/server/server-grpc.ts | 31 ++++++--- .../test/server/server-grpc.spec.ts | 65 +++++++++++++++++++ 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/packages/microservices/server/server-grpc.ts b/packages/microservices/server/server-grpc.ts index b7e1c037264..732dc004114 100644 --- a/packages/microservices/server/server-grpc.ts +++ b/packages/microservices/server/server-grpc.ts @@ -125,7 +125,6 @@ export class ServerGrpc extends Server implements CustomTransportStrategy { const service = {}; for (const methodName in grpcService.prototype) { - let pattern = ''; let methodHandler = null; let streamingType = GrpcMethodStreamingType.NO_STREAMING; @@ -135,32 +134,32 @@ export class ServerGrpc extends Server implements CustomTransportStrategy { if (!isUndefined(methodReqStreaming) && methodReqStreaming) { // Try first pattern to be presented, RX streaming pattern would be // a preferable pattern to select among a few defined - pattern = this.createPattern( + methodHandler = this.getMessageHandler( name, methodName, GrpcMethodStreamingType.RX_STREAMING, + methodFunction, ); - methodHandler = this.messageHandlers.get(pattern); streamingType = GrpcMethodStreamingType.RX_STREAMING; // If first pattern didn't match to any of handlers then try // pass-through handler to be presented if (!methodHandler) { - pattern = this.createPattern( + methodHandler = this.getMessageHandler( name, methodName, GrpcMethodStreamingType.PT_STREAMING, + methodFunction, ); - methodHandler = this.messageHandlers.get(pattern); streamingType = GrpcMethodStreamingType.PT_STREAMING; } } else { - pattern = this.createPattern( + // Select handler if any presented for No-Streaming pattern + methodHandler = this.getMessageHandler( name, methodName, GrpcMethodStreamingType.NO_STREAMING, + methodFunction, ); - // Select handler if any presented for No-Streaming pattern - methodHandler = this.messageHandlers.get(pattern); streamingType = GrpcMethodStreamingType.NO_STREAMING; } if (!methodHandler) { @@ -175,6 +174,22 @@ export class ServerGrpc extends Server implements CustomTransportStrategy { return service; } + getMessageHandler( + serviceName: string, + methodName: string, + streaming: GrpcMethodStreamingType, + grpcMethod: { path?: string }, + ) { + let pattern = this.createPattern(serviceName, methodName, streaming); + let methodHandler = this.messageHandlers.get(pattern); + if (!methodHandler) { + const packageServiceName = grpcMethod.path?.split('/')[1]; + pattern = this.createPattern(packageServiceName, methodName, streaming); + methodHandler = this.messageHandlers.get(pattern); + } + return methodHandler; + } + /** * Will create a string of a JSON serialized format * diff --git a/packages/microservices/test/server/server-grpc.spec.ts b/packages/microservices/test/server/server-grpc.spec.ts index d09f62529b7..af683a23787 100644 --- a/packages/microservices/test/server/server-grpc.spec.ts +++ b/packages/microservices/test/server/server-grpc.spec.ts @@ -281,6 +281,8 @@ describe('ServerGrpc', () => { .onFirstCall() .returns('_invalid') .onSecondCall() + .returns('_invalid') + .onThirdCall() .returns('test2'); sinon.stub(server, 'createServiceMethod').callsFake(() => ({}) as any); @@ -306,6 +308,69 @@ describe('ServerGrpc', () => { }); }); + describe('getMessageHandler', () => { + it('should return handler when service name specified', () => { + const testPattern = server.createPattern( + 'test', + 'TestMethod', + GrpcMethodStreamingType.NO_STREAMING, + ); + const handlers = new Map([[testPattern, () => ({})]]); + console.log(handlers.entries()); + (server as any).messageHandlers = handlers; + + expect( + server.getMessageHandler( + 'test', + 'TestMethod', + GrpcMethodStreamingType.NO_STREAMING, + {}, + ), + ).not.to.be.undefined; + }); + it('should return handler when package name specified with service name', () => { + const testPattern = server.createPattern( + 'package.example.test', + 'TestMethod', + GrpcMethodStreamingType.NO_STREAMING, + ); + const handlers = new Map([[testPattern, () => ({})]]); + (server as any).messageHandlers = handlers; + + expect( + server.getMessageHandler( + 'test', + 'TestMethod', + GrpcMethodStreamingType.NO_STREAMING, + { + path: '/package.example.test/TestMethod', + }, + ), + ).not.to.be.undefined; + }); + + it('should return undefined when method name is unknown', () => { + const testPattern = server.createPattern( + 'package.example.test', + 'unknown', + GrpcMethodStreamingType.NO_STREAMING, + ); + const handlers = new Map([[testPattern, () => ({})]]); + (server as any).messageHandlers = handlers; + + expect( + server.getMessageHandler( + 'test', + 'TestMethod', + GrpcMethodStreamingType.NO_STREAMING, + { + path: '/package.example.test/TestMethod', + }, + ), + ).to.be.undefined; + }); + }); + describe('createPattern', () => { it('should return pattern', () => { const service = 'test'; From 27d01d6ed285d5b4aa6fe926ef5ee276cb2a2703 Mon Sep 17 00:00:00 2001 From: JadenKim-dev Date: Sun, 30 Jun 2024 22:49:37 +0900 Subject: [PATCH 2/2] feat: added optional chaining --- packages/microservices/server/server-grpc.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/microservices/server/server-grpc.ts b/packages/microservices/server/server-grpc.ts index 732dc004114..f27e0e7217e 100644 --- a/packages/microservices/server/server-grpc.ts +++ b/packages/microservices/server/server-grpc.ts @@ -183,7 +183,7 @@ export class ServerGrpc extends Server implements CustomTransportStrategy { let pattern = this.createPattern(serviceName, methodName, streaming); let methodHandler = this.messageHandlers.get(pattern); if (!methodHandler) { - const packageServiceName = grpcMethod.path?.split('/')[1]; + const packageServiceName = grpcMethod.path?.split?.('/')[1]; pattern = this.createPattern(packageServiceName, methodName, streaming); methodHandler = this.messageHandlers.get(pattern); }