agreatfool / grpc_tools_node_protoc_ts

Generate TypeScript d.ts definitions for generated js files from grpc_tools_node_protoc
MIT License
498 stars 56 forks source link

Adds "from_object" parameter that adds fromObject() signatures #136

Open nicksieger opened 11 months ago

nicksieger commented 11 months ago

Currently, fromObject is only added by protoc-gen-js if you custom-build it (see protocolbuffers/protobuf-javascript#162 for an example patch).

agreatfool commented 11 months ago

Thanks a lot for your pr, let me have a look :)

agreatfool commented 11 months ago

Hi @nicksieger , I've made some investigation. Just like what you said, seems the release version has no such feature for now.

I tested the latest version with

$ protoc --version
libprotoc 3.20.3

$ grpc_tools_node_protoc --version
libprotoc 3.19.1

$ protoc \
--js_out=import_style=commonjs,binary,from_object:./src/grpcjs/proto \
--grpc_out=grpc_js:./src/grpcjs/proto \
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` \
-I ./proto \
proto/*.proto

Seems no fromObject methods generated in the js files.

I'd like to test and apply your pr when it's officially released. Or could you please tell me how to build the protoc-gen-js you mentioned manually? I'm not familiar with this.

nicksieger commented 11 months ago

Sure, here's a Dockerfile snippet, or you can run these commands manually on Debian or Ubuntu.

FROM node:18-bookworm
ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /root
RUN apt update && apt install -y bazel-bootstrap protobuf-compiler && rm -rf /var/lib/apt/lists/*
RUN curl -sL https://github.com/protocolbuffers/protobuf-javascript/archive/refs/tags/v3.21.2.tar.gz | tar zxf -
# Apply patch to enable 'fromClass' generation from https://github.com/protocolbuffers/protobuf-javascript/pull/162
RUN cd protobuf-javascript-3.21.2 && \
  curl -sL https://github.com/protocolbuffers/protobuf-javascript/commit/a3c4522bbdab5784de0361049437242980df07d0.diff | patch -u -p1 && \
  bazel build :plugin_files && cp bazel-bin/generator/protoc-gen-js /usr/local/bin
nicksieger commented 11 months ago

FYI, I'm using regular protoc instead of grpc_tools_node_protoc, the latter seems to bundle its own version of proto-gen-js.

protoc \
    --plugin=protoc-gen-js="$(which protoc-gen-js)" \
    --js_out=import_style=commonjs,binary:./protos \
    --plugin=protoc-gen-grpc="$(which grpc_tools_node_protoc_plugin)" \
    --grpc_out=grpc_js:./protos \
    --plugin=protoc-gen-ts="$(which protoc-gen-ts)" \
    --ts_out=grpc_js,from_object:./protos \
    my-service.proto
agreatfool commented 11 months ago

FYI, I'm using regular protoc instead of grpc_tools_node_protoc, the latter seems to bundle its own version of proto-gen-js.

protoc \
    --plugin=protoc-gen-js="$(which protoc-gen-js)" \
    --js_out=import_style=commonjs,binary:./protos \
    --plugin=protoc-gen-grpc="$(which grpc_tools_node_protoc_plugin)" \
    --grpc_out=grpc_js:./protos \
    --plugin=protoc-gen-ts="$(which protoc-gen-ts)" \
    --ts_out=grpc_js,from_object:./protos \
    my-service.proto

I have 2 questions:

nicksieger commented 11 months ago
  1. There is no option to enable fromObject in protoc-gen-js, the only way is to build a patched version from source. I added it as an opt-in option here in protoc-gen-ts since the fromObject methods won't be generated without the patched build.
  2. The last RUN command is the one that builds protoc-gen-js and copies it to /usr/local/bin.