Skip to content

Response does not resolve until body fetched #1439

@chetbox

Description

@chetbox

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions