Skip to content

Commit

Permalink
chore: upload patch from internal repo
Browse files Browse the repository at this point in the history
  • Loading branch information
renbou committed Jun 7, 2024
1 parent 48f7029 commit 74ee127
Show file tree
Hide file tree
Showing 56 changed files with 9,506 additions and 666 deletions.
31 changes: 28 additions & 3 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package grpcbridge
import (
"log/slog"
"net/http"
"slices"
"strings"

"github.com/renbou/grpcbridge/bridgelog"
"github.com/renbou/grpcbridge/grpcadapter"
Expand Down Expand Up @@ -44,6 +46,8 @@ type BridgeOption interface {
type WebBridge struct {
transcodedHTTPBridge *webbridge.TranscodedHTTPBridge
transcodedWebSocketBridge *webbridge.TranscodedWebSocketBridge
gRPCWebHTTPBridge *webbridge.GRPCWebBridge
gRPCWebSocketBridge *webbridge.GRPCWebSocketBridge
}

// NewWebBridge constructs a new [*WebBridge] with the given router and options.
Expand Down Expand Up @@ -74,16 +78,37 @@ func NewWebBridge(router Router, opts ...BridgeOption) *WebBridge {
Forwarder: options.common.forwarder,
})

return &WebBridge{transcodedHTTPBridge: transcodedHTTPBridge, transcodedWebSocketBridge: transcodedWebSocketBridge}
gRPCWebHTTPBridge := webbridge.NewGRPCWebBridge(router, webbridge.GRPCWebBridgeOpts{
Logger: options.common.logger,
Forwarder: options.common.forwarder,
})

gRPCWebSocketBridge := webbridge.NewGRPCWebSocketBridge(router, webbridge.GRPCWebBridgeOpts{
Logger: options.common.logger,
Forwarder: options.common.forwarder,
})

return &WebBridge{transcodedHTTPBridge: transcodedHTTPBridge, transcodedWebSocketBridge: transcodedWebSocketBridge, gRPCWebHTTPBridge: gRPCWebHTTPBridge, gRPCWebSocketBridge: gRPCWebSocketBridge}
}

// ServeHTTP implements [net/http.Handler] and routes the request to the appropriate bridging handler according to these rules:
// 1. WebSocket upgrades (Connection: Upgrade and Upgrade: WebSocket) are handled by [webbridge.TranscodedWebSocketBridge].
// 2. All other requests are handled by [webbridge.TranscodedHTTPBridge].
// 2. WebSocket upgrades with the Sec-WebSocket-Protocol header containing grpc-websockets are handled by [webbridge.GRPCWebSocketBridge].
// 3. HTTP gRPC-Web (Content-Type: application/grpc-web) requests are handled by [webbridge.GRPCWebBridge].
// 4. All other requests are handled by [webbridge.TranscodedHTTPBridge].
func (b *WebBridge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Case-insensitive comparison as specified in the RFC https://datatracker.ietf.org/doc/html/rfc6455#section-4.2.1.
if ascii.EqualFold(r.Header.Get("Connection"), "upgrade") && ascii.EqualFold(r.Header.Get("Upgrade"), "websocket") {
b.transcodedWebSocketBridge.ServeHTTP(w, r)
if slices.Contains(r.Header.Values("Sec-WebSocket-Protocol"), "grpc-websockets") {
b.gRPCWebSocketBridge.ServeHTTP(w, r)
} else {
b.transcodedWebSocketBridge.ServeHTTP(w, r)

Check warning on line 105 in bridge.go

View check run for this annotation

Codecov / codecov/patch

bridge.go#L102-L105

Added lines #L102 - L105 were not covered by tests
}
return

Check warning on line 107 in bridge.go

View check run for this annotation

Codecov / codecov/patch

bridge.go#L107

Added line #L107 was not covered by tests
}

if strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc-web") {
b.gRPCWebHTTPBridge.ServeHTTP(w, r)

Check warning on line 111 in bridge.go

View check run for this annotation

Codecov / codecov/patch

bridge.go#L111

Added line #L111 was not covered by tests
return
}

Expand Down
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ coverage:

ignore:
- "internal/bridgetest"
- "internal/httprule"
- "webbridge/grpcweb.go"
- "internal/gwquery"
- "test"
1 change: 1 addition & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules/
1 change: 1 addition & 0 deletions e2e/empty.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("google-protobuf/google/protobuf/empty_pb.js");
54 changes: 54 additions & 0 deletions e2e/google/api/annotations_pb.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// source: google/api/annotations.proto
/**
* @fileoverview
* @enhanceable
* @suppress {missingRequire} reports error on implicit type usages.
* @suppress {messageConventions} JS Compiler reports an error if a variable or
* field starts with 'MSG_' and isn't a translatable message.
* @public
*/
// GENERATED CODE -- DO NOT EDIT!
/* eslint-disable */
// @ts-nocheck

var jspb = require('google-protobuf');
var goog = jspb;
var global = (function() {
if (this) { return this; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
if (typeof self !== 'undefined') { return self; }
return Function('return this')();
}.call(null));

var google_api_http_pb = require('../../google/api/http_pb.cjs');
goog.object.extend(proto, google_api_http_pb);
var google_protobuf_descriptor_pb = require('google-protobuf/google/protobuf/descriptor_pb.js');
goog.object.extend(proto, google_protobuf_descriptor_pb);
goog.exportSymbol('proto.google.api.http', null, global);

/**
* A tuple of {field number, class constructor} for the extension
* field named `http`.
* @type {!jspb.ExtensionFieldInfo<!proto.google.api.HttpRule>}
*/
proto.google.api.http = new jspb.ExtensionFieldInfo(
72295728,
{http: 0},
google_api_http_pb.HttpRule,
/** @type {?function((boolean|undefined),!jspb.Message=): !Object} */ (
google_api_http_pb.HttpRule.toObject),
0);

google_protobuf_descriptor_pb.MethodOptions.extensionsBinary[72295728] = new jspb.ExtensionFieldBinaryInfo(
proto.google.api.http,
jspb.BinaryReader.prototype.readMessage,
jspb.BinaryWriter.prototype.writeMessage,
google_api_http_pb.HttpRule.serializeBinaryToWriter,
google_api_http_pb.HttpRule.deserializeBinaryFromReader,
false);
// This registers the extension field with the extended class, so that
// toObject() will function correctly.
google_protobuf_descriptor_pb.MethodOptions.extensions[72295728] = proto.google.api.http;

goog.object.extend(exports, proto.google.api);
Loading

0 comments on commit 74ee127

Please sign in to comment.