Skip to content

Commit

Permalink
Merge pull request grpc#5445 from murgatroid99/node_protoc_plugin
Browse files Browse the repository at this point in the history
Create a protoc plugin for Node.js gRPC
  • Loading branch information
jtattermusch committed Apr 26, 2016
2 parents c3d869e + d354f11 commit a49e730
Show file tree
Hide file tree
Showing 30 changed files with 2,257 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dist/
*.egg

# Node installation output
node_modules/
^node_modules
src/node/extension_binary/

# gcov coverage data
Expand Down
15 changes: 15 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,8 @@ cc_library(
"src/compiler/csharp_generator.h",
"src/compiler/csharp_generator_helpers.h",
"src/compiler/generator_helpers.h",
"src/compiler/node_generator.h",
"src/compiler/node_generator_helpers.h",
"src/compiler/objective_c_generator.h",
"src/compiler/objective_c_generator_helpers.h",
"src/compiler/python_generator.h",
Expand All @@ -1145,6 +1147,7 @@ cc_library(
"src/compiler/ruby_generator_string-inl.h",
"src/compiler/cpp_generator.cc",
"src/compiler/csharp_generator.cc",
"src/compiler/node_generator.cc",
"src/compiler/objective_c_generator.cc",
"src/compiler/python_generator.cc",
"src/compiler/ruby_generator.cc",
Expand Down Expand Up @@ -1666,6 +1669,18 @@ cc_binary(
)


cc_binary(
name = "grpc_node_plugin",
srcs = [
"src/compiler/node_plugin.cc",
],
deps = [
"//external:protobuf_compiler",
":grpc_plugin_support",
],
)


cc_binary(
name = "grpc_objective_c_plugin",
srcs = [
Expand Down
37 changes: 36 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ endif

.SECONDARY = %.pb.h %.pb.cc

PROTOC_PLUGINS = $(BINDIR)/$(CONFIG)/grpc_cpp_plugin $(BINDIR)/$(CONFIG)/grpc_csharp_plugin $(BINDIR)/$(CONFIG)/grpc_objective_c_plugin $(BINDIR)/$(CONFIG)/grpc_python_plugin $(BINDIR)/$(CONFIG)/grpc_ruby_plugin
PROTOC_PLUGINS = $(BINDIR)/$(CONFIG)/grpc_cpp_plugin $(BINDIR)/$(CONFIG)/grpc_csharp_plugin $(BINDIR)/$(CONFIG)/grpc_node_plugin $(BINDIR)/$(CONFIG)/grpc_objective_c_plugin $(BINDIR)/$(CONFIG)/grpc_python_plugin $(BINDIR)/$(CONFIG)/grpc_ruby_plugin
ifeq ($(DEP_MISSING),)
all: static shared plugins
dep_error:
Expand Down Expand Up @@ -1013,6 +1013,7 @@ generic_end2end_test: $(BINDIR)/$(CONFIG)/generic_end2end_test
grpc_cli: $(BINDIR)/$(CONFIG)/grpc_cli
grpc_cpp_plugin: $(BINDIR)/$(CONFIG)/grpc_cpp_plugin
grpc_csharp_plugin: $(BINDIR)/$(CONFIG)/grpc_csharp_plugin
grpc_node_plugin: $(BINDIR)/$(CONFIG)/grpc_node_plugin
grpc_objective_c_plugin: $(BINDIR)/$(CONFIG)/grpc_objective_c_plugin
grpc_python_plugin: $(BINDIR)/$(CONFIG)/grpc_python_plugin
grpc_ruby_plugin: $(BINDIR)/$(CONFIG)/grpc_ruby_plugin
Expand Down Expand Up @@ -2245,6 +2246,8 @@ else
$(Q) $(INSTALL) -d $(prefix)/bin
$(Q) $(INSTALL) $(BINDIR)/$(CONFIG)/grpc_csharp_plugin $(prefix)/bin/grpc_csharp_plugin
$(Q) $(INSTALL) -d $(prefix)/bin
$(Q) $(INSTALL) $(BINDIR)/$(CONFIG)/grpc_node_plugin $(prefix)/bin/grpc_node_plugin
$(Q) $(INSTALL) -d $(prefix)/bin
$(Q) $(INSTALL) $(BINDIR)/$(CONFIG)/grpc_objective_c_plugin $(prefix)/bin/grpc_objective_c_plugin
$(Q) $(INSTALL) -d $(prefix)/bin
$(Q) $(INSTALL) $(BINDIR)/$(CONFIG)/grpc_python_plugin $(prefix)/bin/grpc_python_plugin
Expand Down Expand Up @@ -3611,6 +3614,7 @@ endif
LIBGRPC_PLUGIN_SUPPORT_SRC = \
src/compiler/cpp_generator.cc \
src/compiler/csharp_generator.cc \
src/compiler/node_generator.cc \
src/compiler/objective_c_generator.cc \
src/compiler/python_generator.cc \
src/compiler/ruby_generator.cc \
Expand Down Expand Up @@ -10545,6 +10549,37 @@ ifneq ($(NO_DEPS),true)
endif


GRPC_NODE_PLUGIN_SRC = \
src/compiler/node_plugin.cc \

GRPC_NODE_PLUGIN_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(GRPC_NODE_PLUGIN_SRC))))



ifeq ($(NO_PROTOBUF),true)

# You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+.

$(BINDIR)/$(CONFIG)/grpc_node_plugin: protobuf_dep_error

else

$(BINDIR)/$(CONFIG)/grpc_node_plugin: $(PROTOBUF_DEP) $(GRPC_NODE_PLUGIN_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_plugin_support.a
$(E) "[HOSTLD] Linking $@"
$(Q) mkdir -p `dirname $@`
$(Q) $(HOST_LDXX) $(HOST_LDFLAGS) $(GRPC_NODE_PLUGIN_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_plugin_support.a $(HOST_LDLIBSXX) $(HOST_LDLIBS_PROTOC) $(HOST_LDLIBS) $(HOST_LDLIBS_PROTOC) -o $(BINDIR)/$(CONFIG)/grpc_node_plugin

endif

$(OBJDIR)/$(CONFIG)/src/compiler/node_plugin.o: $(LIBDIR)/$(CONFIG)/libgrpc_plugin_support.a

deps_grpc_node_plugin: $(GRPC_NODE_PLUGIN_OBJS:.o=.dep)

ifneq ($(NO_DEPS),true)
-include $(GRPC_NODE_PLUGIN_OBJS:.o=.dep)
endif


GRPC_OBJECTIVE_C_PLUGIN_SRC = \
src/compiler/objective_c_plugin.cc \

Expand Down
12 changes: 12 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ libs:
- src/compiler/csharp_generator.h
- src/compiler/csharp_generator_helpers.h
- src/compiler/generator_helpers.h
- src/compiler/node_generator.h
- src/compiler/node_generator_helpers.h
- src/compiler/objective_c_generator.h
- src/compiler/objective_c_generator_helpers.h
- src/compiler/python_generator.h
Expand All @@ -948,6 +950,7 @@ libs:
src:
- src/compiler/cpp_generator.cc
- src/compiler/csharp_generator.cc
- src/compiler/node_generator.cc
- src/compiler/objective_c_generator.cc
- src/compiler/python_generator.cc
- src/compiler/ruby_generator.cc
Expand Down Expand Up @@ -2582,6 +2585,15 @@ targets:
secure: false
vs_config_type: Application
vs_project_guid: '{3C813052-A49A-4662-B90A-1ADBEC7EE453}'
- name: grpc_node_plugin
build: protoc
language: c++
src:
- src/compiler/node_plugin.cc
deps:
- grpc_plugin_support
secure: false
vs_config_type: Application
- name: grpc_objective_c_plugin
build: protoc
language: c++
Expand Down
22 changes: 15 additions & 7 deletions examples/node/greeter_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,30 @@
*
*/

var PROTO_PATH = __dirname + '/../protos/helloworld.proto';

var grpc = require('grpc');
var hello_proto = grpc.load(PROTO_PATH).helloworld;

var hello_messages = require('./helloworld_pb');
var hello_service = require('./helloworld_grpc_pb');

function main() {
var client = new hello_proto.Greeter('localhost:50051',
grpc.credentials.createInsecure());
var client = new hello_service.GreeterClient('localhost:50051',
grpc.credentials.createInsecure());
var user;
if (process.argv.length >= 3) {
user = process.argv[2];
} else {
user = 'world';
}
client.sayHello({name: user}, function(err, response) {
console.log('Greeting:', response.message);

var request = new hello_messages.HelloRequest();
request.setName(user);

client.sayHello(request, function(err, response) {
if (err) {
debugger;
throw err;
}
console.log('Greeting:', response.getMessage());
});
}

Expand Down
12 changes: 7 additions & 5 deletions examples/node/greeter_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
*
*/

var PROTO_PATH = __dirname + '/../protos/helloworld.proto';

var grpc = require('grpc');
var hello_proto = grpc.load(PROTO_PATH).helloworld;

var hello_messages = require('./helloworld_pb');
var hello_service = require('./helloworld_grpc_pb');

/**
* Implements the SayHello RPC method.
*/
function sayHello(call, callback) {
callback(null, {message: 'Hello ' + call.request.name});
var reply = new hello_messages.HelloReply();
reply.setMessage("Hello " + call.request.getName());
callback(null, reply);
}

/**
Expand All @@ -49,7 +51,7 @@ function sayHello(call, callback) {
*/
function main() {
var server = new grpc.Server();
server.addProtoService(hello_proto.Greeter.service, {sayHello: sayHello});
server.addService(hello_service.GreeterService, {sayHello: sayHello});
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
server.start();
}
Expand Down
39 changes: 39 additions & 0 deletions examples/node/helloworld_grpc_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// GENERATED CODE -- DO NOT EDIT!

var grpc = require('grpc');
var helloworld_pb = require('./helloworld_pb.js');

function serialize_HelloReply(arg) {
if (!(arg instanceof helloworld_pb.HelloReply)) {
throw new Error('Expected argument of type HelloReply');
}
return new Buffer(arg.serializeBinary());
}
function deserialize_HelloReply(buffer_arg) {
return helloworld_pb.HelloReply.deserializeBinary(new Uint8Array(buffer_arg));
}
function serialize_HelloRequest(arg) {
if (!(arg instanceof helloworld_pb.HelloRequest)) {
throw new Error('Expected argument of type HelloRequest');
}
return new Buffer(arg.serializeBinary());
}
function deserialize_HelloRequest(buffer_arg) {
return helloworld_pb.HelloRequest.deserializeBinary(new Uint8Array(buffer_arg));
}

var GreeterService = exports.GreeterService = {
sayHello: {
path: '/helloworld.Greeter/SayHello',
requestStream: false,
responseStream: false,
requestType: helloworld_pb.HelloRequest,
responseType: helloworld_pb.HelloReply,
requestSerialize: serialize_HelloRequest,
requestDeserialize: deserialize_HelloRequest,
responseSerialize: serialize_HelloReply,
responseDeserialize: deserialize_HelloReply,
},
};

exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService);
Loading

0 comments on commit a49e730

Please sign in to comment.