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

Two body-related commits #121

Merged
merged 2 commits into from
Dec 14, 2022
Merged

Two body-related commits #121

merged 2 commits into from
Dec 14, 2022

Conversation

glasser
Copy link
Member

@glasser glasser commented Dec 14, 2022

Review individual commits.

…ode-fetch

This commit makes two changes whose implementation ended up related:

- Writing to the HTTP-header-sensitive cache should not block the
  response! We now do it in the background. If the caller wants to block
  (eg for tests), it should call `fetch` and `await` the
  `cacheWritePromise`. Errors writing to the cache are logged by
  default; override `catchCacheWritePromiseErrors` to change this.
- If you're using `node-fetch` (or another library that uses its
  `Headers` implementation) and the response has multiple separate
  headers with the same name, the `node-fetch` Headers object that we
  return now internally has those separate headers too, so the
  (`node-fetch`-specific) `response.headers.raw()` will let you see the
  duplicate-header structure. (Especially helpful for `set-cookie`
  headers.)

If you override `parseBody`, you should preserve its behavior of always
fully reading the response, or else for large responses the background
"write to cache" task can block and leak memory.

Fixes #75.
Previously, the deduplication logic shared the response body across both
responses; if you mutate that response, it will affect other
deduplicated responses. Now we clone the body. If you override
`parseBody` to return new data types, you should override
`cloneParsedBody` to properly clone them.

There's probably an optimization that can be done to skip the cloning if
there was no duplicate operation (and it's in
`deduplicate-during-request-lifetime` mode).

Fixes #18.
@codesandbox-ci
Copy link

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.

};
}

protected cloneParsedBody<TResult>(parsedBody: TResult) {
Copy link
Member

Choose a reason for hiding this comment

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

We'll need a README update for this

Copy link
Member Author

Choose a reason for hiding this comment

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

parseBody itself isn't documented. Will deal with that in another pass over all protected methods today.

@glasser glasser merged commit 4dc6b0c into main Dec 14, 2022
@glasser glasser deleted the glasser/body-work branch December 14, 2022 17:45
@github-actions github-actions bot mentioned this pull request Dec 14, 2022
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