Skip to content

Create a protoc plugin for Node.js gRPC#5445

Merged
jtattermusch merged 8 commits intogrpc:masterfrom
murgatroid99:node_protoc_plugin
Apr 26, 2016
Merged

Create a protoc plugin for Node.js gRPC#5445
jtattermusch merged 8 commits intogrpc:masterfrom
murgatroid99:node_protoc_plugin

Conversation

@murgatroid99
Copy link
Copy Markdown
Member

This also rewrites the hello world example to use the generated code. This resolves #5418.

Note:

  • Code generated here will only work if using a copy of protoc patched with Fix CommonJS relative require generation, and test it protocolbuffers/protobuf#1274.
  • The modified example depends on the "google-protobuf" npm package, which has not yet been published.
  • Code generated by this generator is not API-compatible with code dynamically generated by gRPC using Protobuf.js. Most importantly, request and response objects must be Protobuf message objects, not plain JavaScript objects.
  • I chose a couple of names kind of arbitrarily:
    • Service code from "filename.proto" is generated in "filename_grpc_pb.js"
    • If "name.proto" contains a service "Greeter", then the client constructor is require('name_grpc_pb.js').GreeterClient and the service definition is require('name_grpc_pb.js').GreeterService.

@stanley-cheung
Copy link
Copy Markdown
Contributor

LGTM.

  1. Sorry for the delay. Do you want to rebase on master because it has been so long? - totally my fault for not getting back to this sooner.
  2. Do you want to keep the old examples/node/greeter_client.js and examples/node/greeter_server.js? I think keeping the old example with the dynamic stuff will be useful.

Tested:

  • installing the grpc_node_plugin
  • generating two .js files per .proto via the protoc --js_out command
  • installing the google-protobuf npm module
  • running the new static codegen helloworld example

Notes for future references:

  • installed node 4.x, protobuf pre-reqs like autoconf, automake, libtool, etc, and java
  • cloned protobuf github source, master branch
  • cloned grpc github source, this PR branch
  • compiled and installed protoc from source (master branch)
  • compiled and installed grpc from source (this PR branch), mostly for the node protoc plugin
  • ran protoc to generate two .js stub files per .proto: protoc --js_out=import_style=commonjs,binary:. --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_node_plugin helloworld.proto
  • need to do npm install and gulp dist from the ~/protobuf/js source directory to generate the google-protobuf.js main file
  • in grpc/examples/node, need to npm install ../../../protobuf/js to install the google-protobuf npm module from source
  • in grpc/examples/node, run nodejs greeter_server.js and nodejs greeter_client.js

@stanley-cheung
Copy link
Copy Markdown
Contributor

I tried to run this against math.proto but I got an error when I run a client:

/home/stanleycheung/grpc/src/node/test/math_grpc_pb.js:54
    requestSerialize: serialize_FibArgs,
                      ^
ReferenceError: serialize_FibArgs is not defined
    at Object.<anonymous> (/home/stanleycheung/grpc/src/node/test/math_grpc_pb.js:54:23)

I looked at the generated math_grpc_pb.js, the serialize_FibArgs function is indeed missing, as with any xxxx_FibArgs functions actually. There are only 2 functions (serialize/deserialize) each for the DivArgs and DivReply message, none for the FibArgs, Num nor FibReply message.

I used this command to generate the .js code as before: protoc --js_out=import_style=commonjs,binary:. --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_node_plugin math.proto

@murgatroid99
Copy link
Copy Markdown
Member Author

I fixed that problem. It was a simple loop variable bug. I also rewrote the test math client and server to test the generated code (in addition to what they were already doing).

@stanley-cheung
Copy link
Copy Markdown
Contributor

LGTM, and verified. Re-tested the codegen part, and the math_client_test.js script. Thanks for the fix.

@stanley-cheung
Copy link
Copy Markdown
Contributor

Tested the latest commit as well. LGTM. Yes I did have to hack the generated code a bit to import grpc directly before and now I don't.

@ctiller ctiller modified the milestones: 2016/04/01 Milestone, GA Apr 21, 2016
@murgatroid99
Copy link
Copy Markdown
Member Author

The Node tests are finally passing

@stanley-cheung
Copy link
Copy Markdown
Contributor

Nice!

@lock lock bot locked as resolved and limited conversation to collaborators Jan 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Node.js protoc plugin

5 participants