Skip to content

Commit ddba642

Browse files
authored
Merge pull request #1 from stackb/grpc-js
Migrate to @grpc-grpc.js
2 parents 7f4dffe + 2eab1a3 commit ddba642

6 files changed

Lines changed: 56 additions & 523 deletions

File tree

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![NPM version](https://img.shields.io/npm/v/@stackb/bzl-sdk-node.svg)](https://www.npmjs.com/package/@stackb/bzl-sdk-node)
44

5-
This repository contains the generated protobuf definitions for the
5+
This repository contains the generated protobuf/grpc-js definitions for the
66
[Bzl](https://build.bzl.io) gRPC API as well as a more developer-friendly
77
index.js entrypoint.
88

@@ -17,18 +17,23 @@ npm install @stackb/bzl-sdk-node
1717
Given a running process (e.g. `bzl serve`), connect to the server and retrieve metadata:
1818

1919
```js
20+
const grpc = require('@grpc/grpc-js');
2021
const v1beta1 = require('@stackb/bzl-sdk-node').v1beta;
2122

22-
// Create a grpc client to the Application service
23-
const appClient = v1beta1.newApplicationClient('localhost:1080');
23+
const client = new v1beta1.ApplicationClient(
24+
'localhost:1080',
25+
grpc.credentials.createInsecure());
2426

25-
// Wait for the connection to be ready and perform a call to the unary GetApplicationMetata rpc endpoint.
26-
appClient.waitForReady(4000, () => {
27-
appClient.getApplicationMetadata(
28-
new v1beta1.pb.ApplicationMetadataRequest(),
29-
(err, metadata) => {
27+
client.waitForReady(4000, () => printMetadata);
28+
29+
function printMetadata() {
30+
const request = new v1beta1.GetApplicationMetadataRequest();
31+
client.getApplicationMetadata(request, (err, metadata) => {
32+
if (err) {
33+
console.warn('could not get metadata', err);
34+
} else {
3035
console.log(`Connected to Bzl ${metadata.getVersion()}`);
3136
}
32-
)
33-
});
37+
});
38+
}
3439
```

build/stack/bzl/v1beta1/application_grpc_pb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// GENERATED CODE -- DO NOT EDIT!
22

33
'use strict';
4-
var grpc = require('grpc');
4+
var grpc = require('@grpc/grpc-js');
55
var build_stack_bzl_v1beta1_application_pb = require('../../../../build/stack/bzl/v1beta1/application_pb.js');
66
var google_protobuf_timestamp_pb = require('google-protobuf/google/protobuf/timestamp_pb.js');
77

index.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
const grpc = require('grpc');
1+
const grpc = require('@grpc/grpc-js');
22
const pb = require('./build/stack/bzl/v1beta1/application_pb');
33
const grpc_pb = require('./build/stack/bzl/v1beta1/application_grpc_pb');
44

5-
module.exports.v1beta = {
6-
pb: pb,
7-
grpc_pb: grpc_pb,
8-
newApplicationClient: function(addr, creds) {
9-
if (!creds) {
10-
creds = grpc.credentials.createInsecure();
11-
}
12-
return new grpc_pb.ApplicationClient(addr, creds);
13-
}
5+
module.exports = {
6+
v1beta1: Object.assign(pb, grpc_pb),
147
};

index.test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
const v1beta1 = require('./index').v1beta;
1+
const grpc = require('@grpc/grpc-js');
2+
const bzl = require('./index');
3+
// console.log('bzl', bzl);
4+
5+
const v1beta1 = bzl.v1beta1;
6+
// console.log('v1beta1', v1beta1);
7+
8+
9+
function newInsecureClient(address) {
10+
return new grpc.Client(address, grpc.credentials.createInsecure());
11+
}
12+
13+
function newApplicationClient(address) {
14+
return new v1beta1.ApplicationClient(address, grpc.credentials.createInsecure());
15+
}
216

317
test('require() dependencies are satisfied', () => {
4-
const client = v1beta1.newApplicationClient('localhost:1080');
18+
const client = newApplicationClient('localhost:1080');
519
expect(client).toBeDefined();
620
});
721

822
// this test is skipped as it currently requires a bzl instance to be running
923
test.skip('connects', done => {
1024
try {
11-
const client = v1beta1.newApplicationClient('localhost:1080');
25+
const client = newApplicationClient('localhost:1080');
1226
client.waitForReady(4000, () => {
1327
client.getApplicationMetadata(
14-
new v1beta1.pb.GetApplicationMetadataRequest(),
28+
new v1beta1.GetApplicationMetadataRequest(),
1529
(err, metadata) => {
1630
expect(err).toBeNull();
1731
expect(metadata).toBeDefined();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@stackb/bzl-sdk-node",
33
"displayName": "Bzl SDK for Node",
44
"description": "SDK for the Bzl API (nodejs)",
5-
"version": "0.0.2",
5+
"version": "0.0.3",
66
"categories": [
77
"Other"
88
],
@@ -12,7 +12,7 @@
1212
},
1313
"dependencies": {
1414
"google-protobuf": "^4.0.0-rc.1",
15-
"grpc": "^1.24.3"
15+
"@grpc/grpc-js": "1.1.2"
1616
},
1717
"devDependencies": {
1818
"jest": "^26.1.0"

0 commit comments

Comments
 (0)