americanexpress / fetchye

✨ If you know how to use Fetch, you know how to use Fetchye [fetch-yae]. Simple React Hooks, Centralized Cache, Infinitely Extensible.
Apache License 2.0
41 stars 21 forks source link

fix(defaultFetcher): Avoid known hang in node-fetch #45

Closed code-forger closed 3 years ago

code-forger commented 3 years ago

There is a known hang in node-fetch. We avoid it by avoiding the clone function.

Description

As per https://github.com/node-fetch/node-fetch#custom-highwatermark there is a known hang when consuming the body of a request with a response that is larger than the default buffer size.

In this PR I avoid this hang by removing the call to .clone(). Instead we always read the body as text, and then try and parse it ourselves.

Motivation and Context

This change prevents instances where requests in nodeJS (such as during SSR uses of fetchye) would indefinitely hang.

How Has This Been Tested?

Using fetchye as part of a One App module, I have created SSR requests that tests the correct behaviour for both json payloads and non-json payloads. I would have liked to have added a unit test that would have failed for the old code, however since we completely mock out node-fetch in the tests, there is no way to reproduce this hang. Ive modified the unit tests to ensure that the old behaviour has been maintained. This required more changes to tests than I would have liked, however since the 'happy' case now relies on the body of the request being parsable JSON these changes were needed.

Types of Changes

Checklist:

What is the Impact to Developers Using Fetchye?

There should be no impact for developers as there should be no change to the api.

CLAassistant commented 3 years ago

CLA assistant check
All committers have signed the CLA.

codesandbox-ci[bot] commented 3 years ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit be7627dcb9e19c1b0220cc508653800ece396e53:

Sandbox Source
quick-install Configuration
fetchye-provider-install Configuration
fetchye-redux-provider-install Configuration
nextjs-fetchye-ssr Configuration