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

twirp-transport: Guard access of response.type to support Cloudflare Workers #321

Merged
merged 1 commit into from
Jun 3, 2022
Merged

twirp-transport: Guard access of response.type to support Cloudflare Workers #321

merged 1 commit into from
Jun 3, 2022

Conversation

mikeylemmon
Copy link
Contributor

Fetch responses in Cloudflare Workers currently throw an error on access of the "type" property12. This PR guards access of the property with a try/catch so that twirp clients can be used inside of Cloudflare Workers.

Footnotes

  1. https://developers.cloudflare.com/workers/runtime-apis/response/#properties

  2. https://github.com/cloudflare/miniflare/blob/72f046e/packages/core/src/standards/http.ts#L646

Fetch responses in Cloudflare Workers currently throw an error on access
of the "type" property[^1][^2]. This commit guards access of the type
property with a try/catch so that twirp clients can be used inside of
Cloudflare Workers.

[^1]: https://developers.cloudflare.com/workers/runtime-apis/response/#properties
[^2]: https://github.com/cloudflare/miniflare/blob/72f046e/packages/core/src/standards/http.ts#L646
@mikeylemmon
Copy link
Contributor Author

It's unclear to me why the CI run failed, but it doesn't appear to be related to the code I changed — the twirp-transport tests all pass, error is in @protobuf-ts/test-generated (which doesn't fail for me when I run it locally):

@protobuf-ts/test-generated: Jasmine started
@protobuf-ts/test-generated:   generated client style call
@protobuf-ts/test-generated:     server-streaming
@protobuf-ts/test-generated:       ✗ should reject results on abort
@protobuf-ts/test-generated:         - Expected a promise to be rejected but it was resolved.
@protobuf-ts/test-generated:         /home/circleci/project/packages/test-generated/spec/generated-client-style-call.spec.ts:194:45
@protobuf-ts/test-generated:                     }, 5)
@protobuf-ts/test-generated:                     const call = client.serverStream({value: "abc"}, {abort: abort.signal});
@protobuf-ts/test-generated:                     await expectAsync(call.headers).toBeRejectedWithError("user cancel")
@protobuf-ts/test-generated:                                                     ~
@protobuf-ts/test-generated:                     await expectAsync(call.status).toBeRejectedWithError("user cancel")
@protobuf-ts/test-generated:                     await expectAsync(call.trailers).toBeRejectedWithError("user cancel")
@protobuf-ts/test-generated:         /home/circleci/project/packages/test-generated/spec/generated-client-style-call.spec.ts:4:12
@protobuf-ts/test-generated:         import {AbortController} from "abort-controller";
@protobuf-ts/test-generated:         import {Int32Value, StringValue} from "../ts-out/google/protobuf/wrappers";
@protobuf-ts/test-generated:         import {AllStyleServiceClient} from "../ts-out/service-style-all.client";
@protobuf-ts/test-generated:                    ~
@protobuf-ts/test-generated:         globalThis.AbortController = AbortController; // AbortController polyfill via https://github.com/mysticatea/abort-controller
@protobuf-ts/test-generated:         /home/circleci/project/packages/test-generated/spec/generated-client-style-call.spec.ts:238:20
@protobuf-ts/test-generated: **************************************************
@protobuf-ts/test-generated: *                    Failures                    *
@protobuf-ts/test-generated: **************************************************
@protobuf-ts/test-generated: 1) generated client style call server-streaming should reject results on abort
@protobuf-ts/test-generated:   - Expected a promise to be rejected but it was resolved.
@protobuf-ts/test-generated: Executed 196 of 196 specs (1 FAILED) in 1 sec.
@protobuf-ts/test-generated: Randomized with seed 73784.
@protobuf-ts/test-generated: Makefile:45: recipe for target 'test-spec' failed
@protobuf-ts/test-generated: make[2]: Leaving directory '/home/circleci/project/packages/test-generated'
@protobuf-ts/test-generated: make[2]: *** [test-spec] Error 1
@protobuf-ts/test-generated: make[1]: *** [default] Error 2
@protobuf-ts/test-generated: Makefile:10: recipe for target 'default' failed
@protobuf-ts/test-generated: make[1]: Leaving directory '/home/circleci/project/packages/test-generated'

https://app.circleci.com/pipelines/github/timostamm/protobuf-ts/684/workflows/4c523394-5990-474c-a143-55f67ed04cbc/jobs/621

timostamm added a commit that referenced this pull request Jun 3, 2022
This is #321, but applied to the grpcweb-transport as well.

All tests pass locally.
@timostamm
Copy link
Owner

Hey @mikeylemmon, I tried it locally and don't see the test failure either. CI must have been a timing-related fluke 🤔

The code looks solid, thank you for this!

@timostamm timostamm merged commit 2addcf2 into timostamm:master Jun 3, 2022
@timostamm
Copy link
Owner

Applied the same workaround to the grpcweb-transport in #329

@timostamm
Copy link
Owner

Released in v2.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants