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

Response does not resolve until body fetched #1439

Open
chetbox opened this issue Mar 7, 2024 · 0 comments
Open

Response does not resolve until body fetched #1439

chetbox opened this issue Mar 7, 2024 · 0 comments

Comments

@chetbox
Copy link

chetbox commented Mar 7, 2024

We came across a bug in our code where we expected await fetch('https://example.com/a-large-file.zip', { method: 'HEAD' }) to just get the HTTP status code and not fetch the HTTP body data, however we hit a memory issue because all the data was downloaded and stored in RAM.

I believe I have observed two deviations from the way native fetch works in whatwg-fetch v3.6.20:

  1. await fetch(...) does not resolve until the entire body has been downloaded.
    It should be possible to check the status code before the download of the HTTP request body is complete. e.g
const response = await fetch('https://example.com/a-large-file.zip')
if (response.status === 200) {
  // I should be able to do something here before the file download has completed
  console.log('File exists! Downloading...')
  const data = await response.body()
}
  1. await fetch(..., { method: 'HEAD' }) exhibits the same behaviour as above but it should not fetch the HTTP body at all. e.g.
const response = await fetch('https://example.com/a-large-file.zip', { method: 'HEAD' })
if (response.status === 200) {
  // I should be able to do something here immediately
  // In practice we only reach here after the file has finished downloading
  console.log('File exists!')
}

We've now upgraded to Node 18 to switch to the native fetch method which does not exhibit the above behaviour.

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

No branches or pull requests

1 participant