Giter VIP home page Giter VIP logo

scalapb-grpcweb's Introduction

ScalaPB

ScalaPB runtime version support

Join the chat at https://gitter.im/ScalaPB/community

Build Status

ScalaPB is a protocol buffer compiler (protoc) plugin for Scala. It will generate Scala case classes, parsers and serializers for your protocol buffers.

ScalaPB generates case classes that can co-exist in the same project alongside the Java-generated code for ProtocolBuffer. This makes it easy to gradually migrate an existing project from the Java version of protocol buffers to Scala. This is achieved by having the ScalaPB generated code use the proto file as part of the package name (in contrast to Java which uses the file name in CamelCase as an outer class)

Each top-level message and enum is written to a separate Scala file. This results in a significant improvement in incremental compilations.

Another cool feature of ScalaPB is that it can optionally generate methods that convert a Java protocol buffer to a Scala protocol buffer and vice versa. This is useful if you are gradually migrating a large code base from Java protocol buffers to Scala. The optional Java conversion is required if you want to use fromAscii (parsing ASCII representation of a protocol buffer). The current implementation delegates to the Java version.

Highlights

  • Supports proto2 and proto3

  • Easily update nested structure in functional way using lenses

  • Scala.js integration

  • GRPC integration

  • Compatible with SparkSQL (through a helper library)

  • Conversion to and from JSON

  • Support user-defined options (since 0.5.29)

  • Support extensions (since 0.6.0)

Versions

Version Description
0.6.x Stable. Supports Protobuf 2.6.x to 3.5.x
0.5.x Supports Protobuf 2.6.x and Protobuf 3.1.x.
0.4.x Stable, unsupported. Works with Protobuf 2.6.x

Installing

To automatically generate Scala case classes for your messages add ScalaPB's sbt plugin to your project. Create a file named project/protoc.sbt containing the following line:

addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.6")

libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.11.3"

Add the following line to your build.sbt:

Compile / PB.targets := Seq(
  scalapb.gen() -> (Compile / sourceManaged).value / "scalapb"
)

For additional configuration options, see ScalaPB SBT Settings documentation

Using ScalaPB

Documentation is available at ScalaPB website.

Questions?

See contacting us.

Testing

ScalaPB uses ScalaCheck to aggressively test the generated code. The test generates many different sets of proto files. The sets are growing in complexity: number of files, references to messages from other protos, message nesting and so on. Then, test data is generated to populate this protocol schema, then we check that the ScalaPB generated code behaves exactly like the reference implementation in Java.

Running the tests:

$ sbt test

The tests take a few minutes to run. There is a smaller test suite called e2e that uses the sbt plugin to compile the protos and runs a series of ScalaChecks on the outputs. To run it:

$ ./e2e.sh

Sponsors

We are very thankful to our sponsors:

scalapb-grpcweb's People

Contributors

alexitc avatar dependabot[bot] avatar mineme0110 avatar povder avatar scala-steward avatar thesamet avatar timothyklim avatar xuwei-k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

scalapb-grpcweb's Issues

Failure(io.grpc.StatusRuntimeException: UNKNOWN: Http response at 400 or 500 level))

GrpcWebClientExampleCode

    val stub =
      RouteGuideGrpcWeb.stub(Channels.grpcwebChannel("https://localhost:8980"))

     stub.getFeature(Point(409146138, -746188906)).onComplete { f => println("Result" -> f) }

Error;

Hello world!
System.scala:391 (Unary,Failure(io.grpc.StatusRuntimeException: UNKNOWN: Http response at 400 or 500 level))
index.js:38          POST https://localhost:8980/routeguide.RouteGuide/GetFeature net::ERR_SSL_PROTOCOL_ERROR

Server is zio-grpc/routeguide example

Server Error;

Jan 11, 2023 12:52:23 PM io.grpc.netty.NettyServerTransport notifyTerminated
INFO: Transport failed
io.netty.handler.codec.http2.Http2Exception: HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 1603010200010001fc030308de3e44f88df68f6559205d4f
	at io.netty.handler.codec.http2.Http2Exception.connectionError(Http2Exception.java:109)
	at io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.readClientPrefaceString(Http2ConnectionHandler.java:321)
	at io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.decode(Http2ConnectionHandler.java:247)
	at io.netty.handler.codec.http2.Http2ConnectionHandler.decode(Http2ConnectionHandler.java:453)
	at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:510)
	at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:449)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:279)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:1589)

Allow sending metadata

Right now, the empty metadata is hardcoded, while it could be useful to support sending custom metadata.

On the scala client you can set up an interceptor which can add the metadata before a request is sent, this approach may be feasible on the scalajs client.

One idea to explore is to send cookies and try to setup envoy so that it transforms the cookies in grpc metadata, the drawback is that all request will include the same metadata, while sometimes it's useful to use different metadata per request.

Adding a header seems to remove all other headers

using version 0.4.1

I'm would like to pass a custom header to my webgrpc service, but when I do so following the example code, the call fails.
When executing the same call without the additional header param the call works just fine, looks like the simple act of adding a header breaks the request?

`
val headers: Metadata = Metadata(
"token" -> "MyToken"
)

val myStub = MyApiGrpcWeb.stub(
Channels.grpcwebChannel(url)
)

myStub.myStream(
Empty(),
headers, // adding this param breaks the request
so.streamObserver
)
`

The headers on the request do seem to include my custom header (but the call fails on envoy), but it seems to be missing a number of other header parameters when compared to the same call without my custom header parameter (which works fine).

Am I using this feature correctly?

Compiling the example client fails

Running sbt clean compile on the example folder leads to this log:

[info] Loading global plugins from /home/dell/.sbt/1.0/plugins
[info] Loading settings for project example-build from plugins.sbt ...
[info] Loading project definition from /home/dell/projects/github/scalapb-grpcweb/example/project
[info] Loading settings for project example from build.sbt ...
[info] Set current project to scalapb-grpcweb-example (in build file:/home/dell/projects/github/scalapb-grpcweb/example/)
[info] Executing in batch mode. For better performance use sbt's shell
[success] Total time: 0 s, completed Mar 26, 2020 4:59:38 PM
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[info] Compiling 1 protobuf files to /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main
[info] Compiling schema /home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf/myservice.proto
protoc-jar: protoc version: 3.7.1, detected platform: linux-x86_64 (linux/amd64)
protoc-jar: embedded: bin/3.7.1/protoc-3.7.1-linux-x86_64.exe
protoc-jar: executing: [/tmp/protocjar7048648934836311830/bin/protoc.exe, --plugin=protoc-gen-scala_0=/tmp/protocbridge9155978991884530541, --scala_0_out=grpc:/home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/protobuf_external, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/protobuf_external, /home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf/myservice.proto]
/home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/protobuf_external: warning: directory does not exist.
/home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/protobuf_external: warning: directory does not exist.
[info] Compiling 1 protobuf files to /home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/scala-2.12/src_managed/main
[info] Compiling schema /home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf/myservice.proto
protoc-jar: protoc version: 3.7.1, detected platform: linux-x86_64 (linux/amd64)
protoc-jar: embedded: bin/3.7.1/protoc-3.7.1-linux-x86_64.exe
protoc-jar: executing: [/tmp/protocjar887925211122755373/bin/protoc.exe, --plugin=protoc-gen-scala_0=/tmp/protocbridge964574253236628389, --scala_0_out=grpc:/home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/scala-2.12/src_managed/main, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/protobuf_external, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf, -I/home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/protobuf_external, /home/dell/projects/github/scalapb-grpcweb/example/protos/src/main/protobuf/myservice.proto]
/home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/protobuf_external: warning: directory does not exist.
/home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/protobuf_external: warning: directory does not exist.
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[info] Compiling protobuf
[info] Compiling protobuf
[info] Protoc target directory: /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main
[info] Protoc target directory: /home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/scala-2.12/src_managed/main
[info] Compiling 4 Scala sources to /home/dell/projects/github/scalapb-grpcweb/example/protos/.jvm/target/scala-2.12/classes ...
[info] Compiling 4 Scala sources to /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/classes ...
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:11:8: value setSchemaDescriptor is not a member of io.grpc.MethodDescriptor.Builder[scalapb.web.myservice.Req,scalapb.web.myservice.Res]
[error] possible cause: maybe a semicolon is missing before `value setSchemaDescriptor'?
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(0)))
[error]        ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:11:48: object ConcreteProtoMethodDescriptorSupplier is not a member of package scalapb.grpc
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(0)))
[error]                                                ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:11:178: value getMethods is not a member of com.google.protobuf.Descriptors.ServiceDescriptor
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(0)))
[error]                                                                                                                                                                                  ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:21:8: value setSchemaDescriptor is not a member of io.grpc.MethodDescriptor.Builder[scalapb.web.myservice.Req,scalapb.web.myservice.Res]
[error] possible cause: maybe a semicolon is missing before `value setSchemaDescriptor'?
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(1)))
[error]        ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:21:48: object ConcreteProtoMethodDescriptorSupplier is not a member of package scalapb.grpc
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(1)))
[error]                                                ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:21:178: value getMethods is not a member of com.google.protobuf.Descriptors.ServiceDescriptor
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(1)))
[error]                                                                                                                                                                                  ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:31:8: value setSchemaDescriptor is not a member of io.grpc.MethodDescriptor.Builder[scalapb.web.myservice.Req,scalapb.web.myservice.Res]
[error] possible cause: maybe a semicolon is missing before `value setSchemaDescriptor'?
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(2)))
[error]        ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:31:48: object ConcreteProtoMethodDescriptorSupplier is not a member of package scalapb.grpc
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(2)))
[error]                                                ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:31:178: value getMethods is not a member of com.google.protobuf.Descriptors.ServiceDescriptor
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(2)))
[error]                                                                                                                                                                                  ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:41:8: value setSchemaDescriptor is not a member of io.grpc.MethodDescriptor.Builder[scalapb.web.myservice.Req,scalapb.web.myservice.Res]
[error] possible cause: maybe a semicolon is missing before `value setSchemaDescriptor'?
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(3)))
[error]        ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:41:48: object ConcreteProtoMethodDescriptorSupplier is not a member of package scalapb.grpc
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(3)))
[error]                                                ^
[error] /home/dell/projects/github/scalapb-grpcweb/example/protos/.js/target/scala-2.12/src_managed/main/scalapb/web/myservice/TestServiceGrpc.scala:41:178: value getMethods is not a member of com.google.protobuf.Descriptors.ServiceDescriptor
[error]       .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(scalapb.web.myservice.MyserviceProto.javaDescriptor.getServices.get(0).getMethods.get(3)))
[error]                                                                                                                                                                                  ^
[error] 12 errors found
[info] Compiling 1 Scala source to /home/dell/projects/github/scalapb-grpcweb/example/server/target/scala-2.12/classes ...
[error] (protosJS / Compile / compileIncremental) Compilation failed
[error] Total time: 6 s, completed Mar 26, 2020 4:59:43 PM

[Feature Request] Trigger gRPC Web Call Without Node (java script)

Note: Even though it is not proper forum to make requests or ask questions, I didn't find any other proper channel

I used an example mentioned in https://github.com/scalapb/scalapb-grpcweb/tree/master/example to trigger an RPC call from a web browser towards an envoy (later it translates gRPC-web+proto http/1.1 to gRPC http/2 towards gRPC server)

Is there any way to trigger grpc-web related RPC towards envoy without using scala js (node) by simply using pure Scala. I can see in order generate Scala code from proto messages there were few scala js related dependencies and cross compilation involved in project. But would like to use library without any involvement of scala js. is it possible to do that ?

  • Compile proto files using your code generator without need of cross platform related stuff just like scalpb-runtime or scalapb-runtime-grpc
  • Use generic gRPC ways of creating a channel and making gRPC call using stubs without using npm and scala js

I got to know that "gRPC-Web is meant to be run from the browsers only" that's why your example also intended to run from browser, but would like mimic browser behavior of calling gRPC methods in pure Scala for my need

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.