Skip to content

Commit

Permalink
Add proto-loader option support. Fixes #22. (#23)
Browse files Browse the repository at this point in the history
* Add proto-loader option support. Fixes #22. Bumb peerDependency to grpc@^1.13.0

* [skip ci] update comment
  • Loading branch information
bojand authored Aug 2, 2018
1 parent 99cb44c commit a1a49a1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,17 @@ The response status metadata.
**Kind**: instance property of [<code>Response</code>](#Response)
<a name="caller"></a>

### caller(host, proto, name, options) ⇒ <code>Object</code>
### caller(host, proto, name, credentials, options) ⇒ <code>Object</code>
Create client isntance.

**Kind**: global function

| Param | Type | Description |
| --- | --- | --- |
| host | <code>String</code> | The host to connect to |
| proto | <code>String</code> \| <code>Object</code> | Path to the protocol buffer definition file or Object specifying <code>root</code> directory and <code>file</code> to load or the static client constructor object itself |
| proto | <code>String</code> \| <code>Object</code> | Path to the protocol buffer definition file or Object specifying <code>file</code> to load <code>load</code> options for proto loader |
| name | <code>String</code> | In case of proto path the name of the service as defined in the proto definition. |
| credentials | <code>Object</code> | The credentials to use to connect. Defaults to `grpc.credentials.createInsecure()` |
| options | <code>Object</code> | Options to be passed to the gRPC client constructor |
| options.retry | <code>Object</code> | In addition to gRPC client constructor options, we accept a `retry` option. The retry option is identical to `async.retry` and is passed as is to it. This is used only for `UNARY` calls to add automatic retry capability. |

Expand All @@ -342,17 +343,18 @@ Create client isntance.
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
const client = caller('localhost:50051', PROTO_PATH, 'Greeter')

const root = path.join(__dirname, 'protos');
const file = 'helloworld.proto'
const client = caller('localhost:50051', { root, file }, 'Greeter')
const root = path.join(__dirname, 'protos')
const file = path.join(__dirname, 'helloworld.proto')
const load = { includeDirs: [ root ] }
const client = caller('localhost:50051', { file, load }, 'Greeter')
```
**Example** *(Create a static client)*
```js
const services = require('./static/helloworld_grpc_pb')
const client = caller('localhost:50051', services.GreeterClient)
```

* [caller(host, proto, name, options)](#caller) ⇒ <code>Object</code>
* [caller(host, proto, name, credentials, options)](#caller) ⇒ <code>Object</code>
* [.metadata](#caller.metadata)
* [.wrap](#caller.wrap)

Expand Down
18 changes: 9 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module.exports = caller
* Create client isntance.
* @param {String} host - The host to connect to
* @param {String|Object} proto Path to the protocol buffer definition file or
* Object specifying <code>root</code> directory and <code>file</code> to load or
* the static client constructor object itself
* Object specifying <code>file</code> to load and <code>load</code> options for proto loader.
* @param {String} name - In case of proto path the name of the service as defined in the proto definition.
* @param {Object} credentials - The credentials to use to connect. Defaults to `grpc.credentials.createInsecure()`
* @param {Object} options - Options to be passed to the gRPC client constructor
Expand All @@ -28,23 +27,24 @@ module.exports = caller
* const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
* const client = caller('localhost:50051', PROTO_PATH, 'Greeter')
*
* const root = path.join(__dirname, 'protos');
* const file = 'helloworld.proto'
* const client = caller('localhost:50051', { root, file }, 'Greeter')
* const root = path.join(__dirname, 'protos')
* const file = path.join(__dirname, 'helloworld.proto')
* const load = { includeDirs: [ root ] }
* const client = caller('localhost:50051', { file, load }, 'Greeter')
*
* @example <caption>Create a static client</caption>
* const services = require('./static/helloworld_grpc_pb')
* const client = caller('localhost:50051', services.GreeterClient)
*/
function caller (host, proto, name, credentials, options) {
let Ctor
if (_.isString(proto) || (_.isObject(proto) && proto.root && proto.file)) {
if (_.isString(proto) || (_.isObject(proto) && proto.file)) {
let protoFilePath = proto
const loadOptions = {}
let loadOptions = {}

if (_.isObject(proto) && proto.root && proto.file) {
if (_.isObject(proto) && proto.file) {
protoFilePath = proto.file
loadOptions.includeDirs = [proto.root]
loadOptions = proto.load || {}
}

const packageDefinition = protoLoader.loadSync(protoFilePath, loadOptions)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"promisify-call": "^2.0.0"
},
"peerDependencies": {
"grpc": "^1.0.0"
"grpc": "^1.13.0"
},
"devDependencies": {
"ava": "^0.25.0",
Expand Down
10 changes: 4 additions & 6 deletions test/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const protoLoader = require('@grpc/proto-loader')
const caller = require('../')

const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
const PROTO_ROOT = path.join(__dirname, 'protos')
const PROTO_FILE = 'helloworld.proto'

const packageDefinition = protoLoader.loadSync(PROTO_PATH)
const helloproto = grpc.loadPackageDefinition(packageDefinition).helloworld
Expand Down Expand Up @@ -67,9 +65,9 @@ test.cb('call dynamic service using callback', t => {
})
})

test.cb('call dynamic service using callback and root, file', t => {
test.cb('call dynamic service using callback and load options', t => {
t.plan(4)
const client = caller(DYNAMIC_HOST, { root: PROTO_ROOT, file: PROTO_FILE }, 'Greeter')
const client = caller(DYNAMIC_HOST, { load: {}, file: PROTO_PATH }, 'Greeter')
client.sayHello({ name: 'Root' }, (err, response) => {
t.ifError(err)
t.truthy(response)
Expand Down Expand Up @@ -109,9 +107,9 @@ test('call dynamic service using async', async t => {
t.is(response.message, 'Hello Bob')
})

test('call dynamic service using async and root, file', async t => {
test('call dynamic service using async and load options', async t => {
t.plan(3)
const client = caller(DYNAMIC_HOST, { root: PROTO_ROOT, file: PROTO_FILE }, 'Greeter')
const client = caller(DYNAMIC_HOST, { load: {}, file: PROTO_PATH }, 'Greeter')
const response = await client.sayHello({ name: 'Root' })
t.truthy(response)
t.truthy(response.message)
Expand Down

0 comments on commit a1a49a1

Please sign in to comment.