-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(microservices): add package definition option to server-grpc
PR #8465 added support for packageDefinition for ClientGrpc, this commit adds it to ServerGrpc. Exported common packageDefinition logic to getGrpcPackageDefinition and used it in both gRPC Server & Client, for consistency. This change means that protoPath is now optional - getGrpcPackageDefinition verifies that either protoPath or PackageDefinition exist. Also, fixed the error thrown from ServerGrpc.loadProto: was Error, now InvalidProtoDefinitionException, as it is in ClientGrpc
- Loading branch information
Showing
10 changed files
with
141 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...roservices/errors/invalid-grpc-package-definition-missing-package-definition.exception.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.exception'; | ||
|
||
export class InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException extends RuntimeException { | ||
constructor() { | ||
super('protoPath or packageDefinition must be defined'); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/microservices/errors/invalid-grpc-package-definition-mutex.exception.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.exception'; | ||
|
||
export class InvalidGrpcPackageDefinitionMutexException extends RuntimeException { | ||
constructor() { | ||
super( | ||
'Both protoPath and packageDefinition were provided, but only one can be defined', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { GrpcOptions } from '../interfaces'; | ||
import { InvalidGrpcPackageDefinitionMutexException } from '../errors/invalid-grpc-package-definition-mutex.exception'; | ||
import { InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException } from '../errors/invalid-grpc-package-definition-missing-package-definition.exception'; | ||
|
||
export function getGrpcPackageDefinition( | ||
options: GrpcOptions['options'], | ||
grpcProtoLoaderPackage: any, | ||
) { | ||
const file = options['protoPath']; | ||
const packageDefinition = options['packageDefinition']; | ||
|
||
if ([file, packageDefinition].every(x => x != undefined)) { | ||
throw new InvalidGrpcPackageDefinitionMutexException(); | ||
} | ||
|
||
if ([file, packageDefinition].every(x => x == undefined)) { | ||
throw new InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException(); | ||
} | ||
|
||
return ( | ||
packageDefinition || | ||
grpcProtoLoaderPackage.loadSync(file, options['loader']) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect } from 'chai'; | ||
import { getGrpcPackageDefinition } from '../../helpers/grpc-helpers'; | ||
import { InvalidGrpcPackageDefinitionMutexException } from '../../errors/invalid-grpc-package-definition-mutex.exception'; | ||
import { InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException } from '../../errors/invalid-grpc-package-definition-missing-package-definition.exception'; | ||
|
||
const grpcProtoLoaderPackage = { loadSync: (a, b) => 'withLoader' }; | ||
|
||
describe('getGrpcPackageDefinition', () => { | ||
it('missing both protoPath and packageDefinition', () => { | ||
expect(() => | ||
getGrpcPackageDefinition( | ||
{ | ||
package: 'somePackage', | ||
}, | ||
grpcProtoLoaderPackage, | ||
), | ||
).to.throw(InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException); | ||
}); | ||
|
||
it('got both protoPath and packageDefinition', () => { | ||
expect(() => | ||
getGrpcPackageDefinition( | ||
{ | ||
package: 'somePackage', | ||
protoPath: 'some/path', | ||
packageDefinition: {}, | ||
}, | ||
grpcProtoLoaderPackage, | ||
), | ||
).to.throw(InvalidGrpcPackageDefinitionMutexException); | ||
}); | ||
|
||
it('success with protoPath', () => { | ||
expect(() => | ||
getGrpcPackageDefinition( | ||
{ | ||
package: 'somePackage', | ||
protoPath: 'some/path', | ||
}, | ||
grpcProtoLoaderPackage, | ||
), | ||
).to.not.throw(Error); | ||
}); | ||
|
||
it('success with packageDef', () => { | ||
expect(() => | ||
getGrpcPackageDefinition( | ||
{ | ||
package: 'somePackage', | ||
packageDefinition: {}, | ||
}, | ||
grpcProtoLoaderPackage, | ||
), | ||
).to.not.throw(Error); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters