I get that grpc-js is a newer library, and there doesn't seem to be any detailed documentation or examples yet of how to use grpc-js so maybe I'm just doing something wrong. I essentially have some proto3 .proto files from an existing project and some gRPC definitions I'd like to use with Node v14 and TypeScript. This doesn't seem possible:
proto.Loader doesn't support types, so I need to generate static code files/typespbjs and pbts don't generate gRPC static codeproto-gen-ts plug doesn't generate code usable with grpc-js, replacing imports doesn't work./node_modules/.bin/grpc_tools_node_protoc \
-I ./protobuf \
--plugin="protoc-gen-ts=./node_modules/.bin/protoc-gen-ts" \
--ts_out="service=grpc-node:${OUT_DIR}" \
--grpc_out="generate_package_definition:${OUT_DIR}" \
/protobuf/rpc/services.proto
sed -i '' 's/\"grpc\"/\"\@grpc\/grpc-js\"/1' ${OUT_DIR}/rpc/services_grpc_pb.d.ts
Results in:
(node:6297) UnhandledPromiseRejectionWarning: TypeError: services_grpc_pb_1.RPCClient is not a constructor
at /Users/moloch/git/sliver-script/lib/client.js:452:23
at new Promise (<anonymous>)
at Client.connect (/Users/moloch/git/script/lib/client.js:451:16)
at /Users/moloch/git/sliver-script-test/index.js:50:49
at step (/Users/moloch/git/script-test/index.js:33:23)
at Object.next (/Users/moloch/git/script-test/index.js:14:53)
at fulfilled (/Users/moloch/git/script-test/index.js:5:58)
(Use `node --trace-warnings ...` to show where the warning was created)
@grpc/grpc-js exports the same API as grpc, so switching the imports should work. Have you checked that the result of that sed script is correct?
@moloch-- I've created an example repo you can refer to: https://github.com/badsyntax/grpc-js-types
@murgatroid99 any thoughts about https://github.com/grpc/grpc-node/pull/1380 ?
If that PR was merged, i'll then update ts-protoc-gen to use @grpc/grpc-js imports to avoid the sed hackyness.
I merged #1380. I think it's a reasonable change and I understand why some people would prefer it to the load_package_definition option. It will go out in the next release, which I can hopefully do soon.
Thanks @badsyntax @murgatroid99 was able to get my use case working with the sed hacks but looking forward to a release that doesn't require it!
I have published grpc-tools version 1.9.0 with that new option.
Most helpful comment
@moloch-- I've created an example repo you can refer to: https://github.com/badsyntax/grpc-js-types
@murgatroid99 any thoughts about https://github.com/grpc/grpc-node/pull/1380 ?
If that PR was merged, i'll then update
ts-protoc-gento use@grpc/grpc-jsimports to avoid thesedhackyness.