Skip to content

grpcweb-transport: Guard access of response.type to support Cloudflare Workers #329

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

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
### unreleased changes

none

Bug fixes:

- Guard access of response.type to support Cloudflare Workers, see #321
Thanks to @mikeylemmon for the fix!


### v2.6.0
Expand Down
10 changes: 9 additions & 1 deletion packages/grpcweb-transport/src/grpc-web-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,21 @@ export function readGrpcWebResponseHeader(headers: HttpHeaders, httpStatus: numb
export function readGrpcWebResponseHeader(headersOrFetchResponse: HttpHeaders | Response, httpStatus?: number, httpStatusText?: string): [GrpcStatusCode, string | undefined, RpcMetadata] {
if (arguments.length === 1) {
let fetchResponse = headersOrFetchResponse as Response;
switch (fetchResponse.type) {

// Cloudflare Workers throw when the type property of a fetch response
// is accessed, so wrap access with try/catch. See:
// * https://developers.cloudflare.com/workers/runtime-apis/response/#properties
// * https://github.com/cloudflare/miniflare/blob/72f046e/packages/core/src/standards/http.ts#L646
let responseType
try { responseType = fetchResponse.type } catch {}
switch (responseType) {
case "error":
case "opaque":
case "opaqueredirect":
// see https://developer.mozilla.org/en-US/docs/Web/API/Response/type
throw new RpcError(`fetch response type ${fetchResponse.type}`, GrpcStatusCode[GrpcStatusCode.UNKNOWN]);
}

return readGrpcWebResponseHeader(
fetchHeadersToHttp(fetchResponse.headers),
fetchResponse.status,
Expand Down
8 changes: 7 additions & 1 deletion packages/twirp-transport/src/twirp-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export class TwirpFetchTransport implements RpcTransport {

defHeader.resolve(parseMetadataFromResponseHeaders(fetchResponse.headers));

switch (fetchResponse.type) {
// Cloudflare Workers throw when the type property of a fetch response
// is accessed, so wrap access with try/catch. See:
// * https://developers.cloudflare.com/workers/runtime-apis/response/#properties
// * https://github.com/cloudflare/miniflare/blob/72f046e/packages/core/src/standards/http.ts#L646
let responseType
try { responseType = fetchResponse.type } catch {}
switch (responseType) {
case "error":
case "opaque":
case "opaqueredirect":
Expand Down