Skip to content

Commit

Permalink
[WIP] Initial implemenation of nodejs support
Browse files Browse the repository at this point in the history
Remaining issues:
* https://github.com/google/protobuf/issues/1591
  There's no way to unmarshal from JSON.
  • Loading branch information
yugui committed Feb 19, 2018
1 parent 17585fa commit e695fca
Show file tree
Hide file tree
Showing 8 changed files with 1,507 additions and 11 deletions.
14 changes: 14 additions & 0 deletions node/bin/greeter_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const greeter = require('../src/client');

async function main() {
const client = new greeter.GreeterClient('0.0.0.0:5000');
try {
console.log(await client.greet('a'));
} catch (err) {
console.error("Failed to call greeter: %s", err);
}
}

main();
16 changes: 16 additions & 0 deletions node/bin/greeter_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const greeter = require('../src/server');

const grpc = require('grpc');

function main() {
const server = new grpc.Server();
greeter.registerGreeterService(server);
server.bind('0.0.0.0:5000', grpc.ServerCredentials.createInsecure());

console.log(`listening on ${addr}`)
server.start();
}

main();
16 changes: 16 additions & 0 deletions node/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

var gulp = require('gulp');
var shell = require('gulp-shell');

gulp.task('compile', function(){
return gulp.src('../greeter.proto')
.pipe(shell([
'protoc', '-I..',
'--js_out=import_style=commonjs,binary:proto',
'--grpc_out=import_style=commonjs,binary:proto',
'--plugin=protoc-gen-grpc=`which grpc_node_plugin`',
'../greeter.proto',
].join(' ')))
.pipe(gulp.dest('proto'));
})
10 changes: 9 additions & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
"main": "index.js",
"repository": "https://github.com/yugui/grpc-custom-serializer",
"author": "Yuki Yugui Sonoda",
"license": "Apache 2.0",
"license": "Apache-2.0",
"dependencies": {
"google-protobuf": "^3.5.0",
"grpc": "^1.9.1"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-shell": "^0.6.5"
},
"scripts": {
"compile": "gulp compile"
}
}
2 changes: 2 additions & 0 deletions node/proto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!/.gitignore
40 changes: 40 additions & 0 deletions node/src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const messages = require('../proto/greeter_pb');
const services = require('../proto/greeter_grpc_pb');

const {promisify} = require('util');
const grpc = require('grpc');

/**
* Wrapper of the client stub of Greeter serivce.
*/
class GreeterClient {
constructor(addr) {
this.client = new services.GreeterClient(addr,
grpc.credentials.createInsecure());
}

/**
* @param {string} name - the name to greet to.
* @returns {Promise} Promise object represents the greeting message
* from the server.
*/
greet(name) {
const request = new messages.RequestProto();
request.setName(name);
return new Promise((resolve, reject) => {
this.client.greet(request, (err, response) => {
if (err) {
reject(err);
return;
}
resolve(response.getMessage());
});
});
}
};

module.exports = {
GreeterClient: GreeterClient,
};
29 changes: 29 additions & 0 deletions node/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const messages = require('../proto/greeter_pb');
const services = require('../proto/greeter_grpc_pb');

/**
* An implementation of Greeter service.
*/
class GreeterServer {
greet(call, callback) {
const response = new messages.ResponseProto();
response.setMessage(`Hello, ${call.request.getName()}`);
callback(null, response);
}
};

/**
* Registers an instance of GreeterServer to the given server.
* @param {grpc.Server} server - a grpc server.
*/
function registerGreeterService(server) {
server.addService(services.GreeterService, new GreeterServer());
}

module.exports = {
GreeterServer: GreeterServer,
registerGreeterService: registerGreeterService,
};

Loading

0 comments on commit e695fca

Please sign in to comment.