Skip to content

Commit

Permalink
Merge pull request #13731 from JadenKim-dev/grpc-package-name
Browse files Browse the repository at this point in the history
feat(microservices): grpc package name can be specified with service name
  • Loading branch information
kamilmysliwiec authored Aug 12, 2024
2 parents 6e7216a + 27d01d6 commit ccfa23e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/microservices/server/server-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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
*
Expand Down
65 changes: 65 additions & 0 deletions packages/microservices/test/server/server-grpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ describe('ServerGrpc', () => {
.onFirstCall()
.returns('_invalid')
.onSecondCall()
.returns('_invalid')
.onThirdCall()
.returns('test2');

sinon.stub(server, 'createServiceMethod').callsFake(() => ({}) as any);
Expand All @@ -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';
Expand Down

0 comments on commit ccfa23e

Please sign in to comment.