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

Defer resolving globalThis.fetch until calling endpoint #692

Merged
merged 5 commits into from
Jun 27, 2023

Conversation

paul-sachs
Copy link
Contributor

@paul-sachs paul-sachs commented Jun 26, 2023

When using a polyfilled (or patched) fetch, we need to make sure the fetch is retrieved at call site, rather than being cached earlier. This allows tools like Sentry to properly detect fetch calls.

Copy link
Member

@timostamm timostamm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, but left a couple of comments. You have to run make bench to update the code size benchmark too.

packages/connect-web/src/grpc-web-transport.ts Outdated Show resolved Hide resolved
packages/connect-web-test/src/fetch.spec.ts Outdated Show resolved Hide resolved
@paul-sachs paul-sachs changed the title Make sure patched fetch is used Defer resolving fetch until calling endpoint Jun 26, 2023
@paul-sachs
Copy link
Contributor Author

You have to run make bench to update the code size benchmark too.

I ran make bench and updated the readme but CI continues to fail due to a change in the size. Is there a specific version of node I need to run it with?

Copy link
Member

@timostamm timostamm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For manipulating globalThis.fetch in the tests, it looks like it would be useful to use a setup and teardown, so that a test failure doesn't affect all other tests:

    let originFetch: typeof fetch;
    beforeEach(() => (originFetch = globalThis.fetch));
    afterEach(() => (globalThis.fetch = originFetch));

For the gRPC-web test, mocking the response will be difficult, because the transport uses the body as a stream.

Since we do not care which parts of the response are used in this test, you can reject from your fetch instead of resolving:

const transport = createGrpcWebTransport({
  baseUrl: "https://example.com",
});
// Patch globalThis.fetch to mimic a polyfill or patch
globalThis.fetch = () => Promise.reject("test-error-raised-from-patched-fetch");
await expectAsync(
  transport.unary(
    TestService,
    TestService.methods.unaryCall,
    undefined,
    undefined,
    undefined,
    new SimpleRequest()
  )
).toBeRejectedWithError(/test-error-raised-from-patched-fetch/);

@paul-sachs
Copy link
Contributor Author

@timostamm Yeah, that makes a lot of sense. I was seeing that weirdness and just started to mess with beforeEach/afterEach but was looking into the GRPC thing first. I was kinda struggling to mock it out so the error idea is super helpful, thanks!

@timostamm timostamm changed the title Defer resolving fetch until calling endpoint Defer resolving globalThis.fetch until calling endpoint Jun 27, 2023
@paul-sachs paul-sachs merged commit 29436a0 into main Jun 27, 2023
@paul-sachs paul-sachs deleted the psachs/fix-sentry-hooks branch June 27, 2023 13:31
@smaye81 smaye81 mentioned this pull request Jun 28, 2023
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