Skip to content

Commit

Permalink
feat: implementing grpc reflection client
Browse files Browse the repository at this point in the history
This change adds the ability to create GPRC Clients using
GRPC Reflection Protocol, which is pretty convenient to
make protobuf propagation easier, as it'll only be needed
in the server side! Also, some optional libraries that
were wrongly installed as dependencies, as changed to
devDependencies
  • Loading branch information
Farenheith committed Dec 16, 2023
1 parent 603c0a6 commit 3e7ff3e
Show file tree
Hide file tree
Showing 13 changed files with 18,742 additions and 187 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x]
node-version: [18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x]
node-version: [16.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12
16
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
npm i @codibre/grpc-base-client
```


## How to Use


```typescript

Expand All @@ -45,16 +45,45 @@ const grpcCLient = new gRPCCLient<Health>({
secure: true,
});



await grpcCLient.getInstance().Check({service: 'foo'}); // { status: 'SERVING' }

```


## How to retrieve client through reflection

Is your GRPC servers implements [GRPC Reflection Protobol](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md), then you don't need to have a copy of the proto client side and can just get it using it! This package implements a method to create the client using this feature that can be easily used like this:

```ts
import { Client as gRPCCLient } from '@codibre/grpc-base-client';

interface Health {
Check(props: { service: string }): Promise<any>;
}

const grpcCLient = gRPCCLient.getByReflection<Health>({
namespace: 'abc.def',
url: 'test.service',
maxConnections: 2,
service: 'Health',
secure: true,
});

await grpcCLient.getInstance().Check({service: 'foo'}); // { status: 'SERVING' }
```

Notice that you don't inform the protoFile in this call, as it is not needed. Also, this method don't offers a legacy library switch, as it'll only work with the new @grpc/grpc-js library.

If you want to find out how to implement it on your server, it really pretty easy! Just take a look at the official implementation for NodeJs: [@grpc/reflection](https://www.npmjs.com/package/@grpc/reflection). If you're using **NestJs**, another option is [nestjs-grpc-reflection](https://www.npmjs.com/package/nestjs-grpc-reflection), although the official one is preatty straight forward to use alongside with nestjs too.

To use **getByReflection**, you need to install two optional libraries:

* [grpc-js-reflection-client](https://www.npmjs.com/package/grpc-js-reflection-client)
* [google-protobuf](https://www.npmjs.com/package/google-protobuf)

## License



Licensed under [MIT](https://en.wikipedia.org/wiki/MIT_License).

Licensed under [MIT](https://en.wikipedia.org/wiki/MIT_License).
Loading

0 comments on commit 3e7ff3e

Please sign in to comment.