Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for @grpc/grpc-js #236

Merged
merged 10 commits into from
Sep 10, 2020

Conversation

badsyntax
Copy link
Contributor

@badsyntax badsyntax commented Jun 16, 2020

Refs #226, #234, grpc/grpc-node#1380

Add support for generating files that import @grpc/grpc-js instead of grpc. This does not change the default behaviour of the package, but instead introduces a new param allowing you to opt-in to using @grpc/grpc-js.

Changes

  • Add new mode param, for example:
--ts_out=service=grpc-node,mode=grpc-js
  • Add generated examples
  • Update README

Verification

I've only tested it via the generated examples. It's very similar to service=grpc-node other than changing the import paths.

@badsyntax badsyntax marked this pull request as ready for review June 16, 2020 07:22
Add mode=grpc-js example
@badsyntax badsyntax mentioned this pull request Jun 16, 2020
@laike9m
Copy link

laike9m commented Jul 3, 2020

BTW, I tried installing the grpc-node-mode branch using

npm install badsyntax/ts-protoc-gen#grpc-node-mode

ran it, and got this error:

internal/modules/cjs/loader.js:969
  throw err;
  ^

Error: Cannot find module '../lib/index'

It seems the lib directory does not exist in ts-protoc-gen under node_modules.

@badsyntax
Copy link
Contributor Author

The lib directory is only built on prepublishOnly (before the package is published to npm), so installing via git won't work, see:

"prepublishOnly": "npm run build"

@laike9m
Copy link

laike9m commented Jul 3, 2020

Thanks for the quick response. Any suggestions on what I should do to make it work? Or should I stick with sed for now like https://github.com/badsyntax/grpc-js-types?

@avdeev
Copy link

avdeev commented Jul 3, 2020

Quick fix:

sed -i -- 's/import \* as grpc from "grpc";/import \* as grpc from "@grpc\/grpc-js";/g' *

@badsyntax
Copy link
Contributor Author

if you want to continue using this package, then you're going to have to stick with sed. but as mentioned here grpc/grpc-node#1380 (comment), I would suggest to try the new typescript generator offered by the author of grpc-js

@andreyctkn
Copy link

Hi @badsyntax, Any plans to merge this PR?

@badsyntax
Copy link
Contributor Author

I can't merge as I'm not the owner of this repo. Not sure why I haven't got a review yet :(

@andreyctkn
Copy link

@MarcusLongmuir Can you please help?

Copy link
Contributor

@MarcusLongmuir MarcusLongmuir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Apologies for the long delay in waiting for a review. This had gone under the radar.

Only a few minor things 👍

@@ -4,12 +4,12 @@ import {FileDescriptorProto} from "google-protobuf/google/protobuf/descriptor_pb
import {CodeGeneratorResponse} from "google-protobuf/google/protobuf/compiler/plugin_pb";
import {createFile, RPCDescriptor, GrpcServiceDescriptor, RPCMethodDescriptor} from "./common";

export function generateGrpcNodeService(filename: string, descriptor: FileDescriptorProto, exportMap: ExportMap): CodeGeneratorResponse.File {
export function generateGrpcNodeService(filename: string, descriptor: FileDescriptorProto, exportMap: ExportMap, modeParameter: string | undefined): CodeGeneratorResponse.File {
Copy link
Contributor

@MarcusLongmuir MarcusLongmuir Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only called from the index.ts file so the argument could be an enum rather than an optional string to make the mode explicit.

src/index.ts Outdated

if (parameter === "service=true") {
if (service === "true") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service parameter should probably have more validation and helpful warnings, but this additional parameter makes that even clearer.

Can you add checks for the supported service and mode values and change this to throw exceptions if they're unrecognised to help users more quickly identify misconfigurations please?

@badsyntax
Copy link
Contributor Author

badsyntax commented Sep 10, 2020

I've made those changes:

Running:

protoc --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" --plugin=protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH} --js_out="import_style=commonjs,binary:${OUT_DIR}" --ts_out="service=grpc-nodde:${OUT_DIR}" --grpc_out="${OUT_DIR}" proto/examplecom/simple.proto

Shows:

protoc-gen-ts error: Error: Unrecognised service parameter: grpc-nodde
    at getServiceParameter (/Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/util.js:155:19)
    at Object.getParameterEnums (/Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/util.js:173:18)
    at /Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/index.js:19:25
    at Socket.<anonymous> (/Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/util.js:74:9)
    at Socket.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1183:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

--ts_out: protoc-gen-ts: Plugin failed with status code 1.

Running:

protoc --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" --plugin=protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH} --js_out="import_style=commonjs,binary:${OUT_DIR}" --ts_out="service=grpc-node,mode=grpc-jss:${OUT_DIR}" --grpc_out="${OUT_DIR}" proto/examplecom/simple.proto

Shows

protoc-gen-ts error: Error: Unrecognised mode parameter: grpc-jss
    at getModeParameter (/Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/util.js:166:19)
    at Object.getParameterEnums (/Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/util.js:174:15)
    at /Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/index.js:19:25
    at Socket.<anonymous> (/Users/richardwillis/Projects/badsyntax/ts-protoc-gen/lib/util.js:74:9)
    at Socket.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1183:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

--ts_out: protoc-gen-ts: Plugin failed with status code 1.

Copy link
Contributor

@MarcusLongmuir MarcusLongmuir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for making those changes

@improbable-prow-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: avdeev, MarcusLongmuir, qingzhenyun
To complete the pull request process, please assign
You can assign the PR to them by writing /assign in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@MarcusLongmuir MarcusLongmuir merged commit 6383416 into improbable-eng:master Sep 10, 2020
@badsyntax badsyntax deleted the grpc-node-mode branch September 10, 2020 19:57
@badsyntax
Copy link
Contributor Author

Awesome, thanks!

@avdeev
Copy link

avdeev commented Sep 10, 2020

@badsyntax Thanks! Could you release it?

@andreyctkn
Copy link

thanks !

@MarcusLongmuir
Copy link
Contributor

Unfortunately the repo and org setup appears to make that quite difficult without another maintainer so I'll have to do it in the morning when others come online.

If you want to validate usage in the meantime there is now a published version on npm: https://www.npmjs.com/package/ts-protoc-gen/v/0.12.1-pre.6383416.

I'll comment on this PR when the release goes out.

@MarcusLongmuir
Copy link
Contributor

0.13.0 is now out: https://www.npmjs.com/package/ts-protoc-gen/v/0.13.0

@badsyntax
Copy link
Contributor Author

Thanks for the release. I've tested the change in my project and it lgtm 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants