-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Initial implemenation of nodejs support
Remaining issues: * https://github.com/google/protobuf/issues/1591 There's no way to unmarshal from JSON.
- Loading branch information
Showing
8 changed files
with
1,507 additions
and
11 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
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(); |
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,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(); |
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,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')); | ||
}) |
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,2 @@ | ||
* | ||
!/.gitignore |
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,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, | ||
}; |
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,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, | ||
}; | ||
|
Oops, something went wrong.