@uppy/companion: remove redundant HEAD request for file size (#5648)

* Always get size from stream response headers

because i don't really think we need a separate request for it

* Remove explicit size request

Remove `size` method from providers that don't have a backup method for getting size (that is better than GET download response header)
assumption: GET response content-length is as good as (or better than) HEAD response content-length,
meaning it is not necessary to call getURLMeta when we already tried the original GET response content-length

this reduces the chance of hitting the race condition where the size of a URL changes between the GET and HEAD requests are sent
This commit is contained in:
Mikael Finstad 2025-02-17 22:09:02 +08:00 committed by GitHub
parent 4f04568681
commit 834b01300a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 35 additions and 113 deletions

View file

@ -61,26 +61,14 @@ class MyCustomProvider {
},
})
if (!resp.ok) {
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`)
}
return { stream: Readable.fromWeb(resp.body) }
}
// eslint-disable-next-line class-methods-use-this
async size ({ id, token }) {
const resp = await fetch(`${BASE_URL}/photos/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
const contentLengthStr = resp.headers['content-length']
const contentLength = parseInt(contentLengthStr, 10);
const size = !Number.isNaN(contentLength) && contentLength >= 0 ? contentLength : undefined;
if (!resp.ok) {
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`)
}
const { size } = await resp.json()
return size
return { stream: Readable.fromWeb(resp.body), size }
}
}