grpc / grpc-dart

The Dart language implementation of gRPC.
https://pub.dev/packages/grpc
Apache License 2.0
861 stars 271 forks source link

Creating GrpcOrGrpcWebChannel always throws `UnsupportedError('not supported by gRPC-web') #721

Closed DmitryGaimaldinov closed 4 months ago

DmitryGaimaldinov commented 4 months ago
grpc-dart version: 3.2.4 ## Repro steps I've a remote grpc server and grpc-web-proxy which is listening http://localhost:8090. Everything work fine when I use ``` GrpcWebClientChannel.xhr( Uri.parse('http://localhost:8090'), ) ``` But when I use ``` GrpcOrGrpcWebClientChannel.grpc( 'localhost', port: 8090, options: ChannelOptions(credentials: ChannelCredentials.insecure()), ) ``` it always throws `Unsupported operation: not supported by gRPC-web`. The problem in `grpc_or_grpcweb_channel_web.dart` file. There's a typo inside constructor: ``` GrpcOrGrpcWebClientChannelInternal.grpc( Object host, { required int port, required ChannelOptions options, }) : super.xhr( Uri( host: host.toString(), port: port, scheme: options.credentials.isSecure ? 'https' : 'http'), ) { // Do not silently ignore options as caller may expect them to have effects. throw UnsupportedError('not supported by gRPC-web'); } Explanation: super.xhr calls GrpcWebClientChannel.xhr constructor which has 2 parameters: GrpcWebClientChannel.xhr(Uri uri, {void Function()? channelShutdownHandler}). I think 'the throw UnsupportedError('not supported by gRPC-web')' has been added with the intention to add channelShutdownHandler. But instead it's always called after constructor so UnsupportedError is always thrown. ``` The solution: ``` GrpcOrGrpcWebClientChannelInternal.grpc( Object host, { required int port, required ChannelOptions options, }) : super.xhr( Uri( host: host.toString(), port: port, scheme: options.credentials.isSecure ? 'https' : 'http', ), channelShutdownHandler: () { // Do not silently ignore options as caller may expect them to have effects. throw UnsupportedError('not supported by gRPC-web'); } ); ```
mraleph commented 4 months ago

You are not supposed to call that constructor on the Web - that's why it throws. It is only meaningful on non-Web.