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

Modify parsing of response for Connect unary requests #668

Merged
merged 12 commits into from
Jun 7, 2023
2 changes: 1 addition & 1 deletion packages/connect-web-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ it like a web server would usually do.

| code generator | bundle size | minified | compressed |
|----------------|-------------------:|-----------------------:|---------------------:|
| connect | 112,660 b | 49,405 b | 13,203 b |
| connect | 112,742 b | 49,450 b | 13,229 b |
| grpc-web | 414,906 b | 301,127 b | 53,279 b |
111 changes: 111 additions & 0 deletions packages/connect-web-test/src/fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { TestService } from "./gen/grpc/testing/test_connect.js";
import {
SimpleRequest,
SimpleResponse,
} from "./gen/grpc/testing/messages_pb.js";
import { createConnectTransport } from "@bufbuild/connect-web";

describe("custom fetch", function () {
describe("with Connect transport", () => {
it("should only call Response#json with the JSON format", async function () {
const response = new Response(
new SimpleResponse({ username: "donald" }).toJsonString(),
{
headers: {
"Content-Type": "application/json",
},
}
);
spyOn(response, "arrayBuffer").and.callThrough();
spyOn(response, "json").and.callThrough();
const transport = createConnectTransport({
baseUrl: "https://example.com",
fetch: () => Promise.resolve(response),
});
await transport.unary(
TestService,
TestService.methods.unaryCall,
undefined,
undefined,
undefined,
new SimpleRequest()
);
expect(response.json).toHaveBeenCalledTimes(1); // eslint-disable-line @typescript-eslint/unbound-method
expect(response.arrayBuffer).toHaveBeenCalledTimes(0); // eslint-disable-line @typescript-eslint/unbound-method
});
it("should only call Response#arrayBuffer with the binary format on the happy path", async function () {
const response = new Response(
new SimpleResponse({ username: "donald" }).toBinary(),
{
headers: {
"Content-Type": "application/proto",
},
}
);
spyOn(response, "arrayBuffer").and.callThrough();
spyOn(response, "json").and.callThrough();
const transport = createConnectTransport({
fetch: () => Promise.resolve(response),
baseUrl: "https://example.com",
useBinaryFormat: true,
});
await transport.unary(
TestService,
TestService.methods.unaryCall,
undefined,
undefined,
undefined,
new SimpleRequest()
);
expect(response.json).toHaveBeenCalledTimes(0); // eslint-disable-line @typescript-eslint/unbound-method
expect(response.arrayBuffer).toHaveBeenCalledTimes(1); // eslint-disable-line @typescript-eslint/unbound-method
});
it("should call Response#json with the binary format for an error response", async function () {
const response = new Response(
JSON.stringify({
code: "permission_denied",
message: "foobar",
}),
{
status: 403,
headers: {
"Content-Type": "application/json",
},
}
);
spyOn(response, "arrayBuffer").and.callThrough();
spyOn(response, "json").and.callThrough();
const transport = createConnectTransport({
fetch: () => Promise.resolve(response),
baseUrl: "https://example.com",
useBinaryFormat: true,
});
await expectAsync(
transport.unary(
TestService,
TestService.methods.unaryCall,
undefined,
undefined,
undefined,
new SimpleRequest()
)
).toBeRejectedWithError(/\[permission_denied] foobar/);
expect(response.json).toHaveBeenCalledTimes(1); // eslint-disable-line @typescript-eslint/unbound-method
expect(response.arrayBuffer).toHaveBeenCalledTimes(0); // eslint-disable-line @typescript-eslint/unbound-method
});
});
});
7 changes: 6 additions & 1 deletion packages/connect-web/src/connect-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ export function createConnectTransport(
service,
method,
header: demuxedHeader,
message: parse(new Uint8Array(await response.arrayBuffer())),
message: useBinaryFormat
? parse(new Uint8Array(await response.arrayBuffer()))
: method.O.fromJson(
(await response.json()) as JsonValue,
options.jsonOptions
),
trailer: demuxedTrailer,
};
},
Expand Down