Optimize requests

This commit is contained in:
Zef Hemel
2023-08-08 15:00:18 +02:00
parent 7498cc1ecb
commit b7b666ee1d
4 changed files with 34 additions and 13 deletions
+8 -2
View File
@@ -140,9 +140,12 @@ export class HttpSpacePrimitives implements SpacePrimitives {
const res = await this.authenticatedFetch(
`${this.url}/${encodeURI(name)}`,
// This used to use HEAD, but it seems that Safari on iOS is blocking cookies/credentials to be sent along with HEAD requests
// so we'll use GET instead and not use the body. A bit wasteful, but it works.
// so we'll use GET instead with a magic header which the server may or may not use to omit the body.
{
method: "GET",
headers: {
"X-Get-Meta": "true",
},
},
);
if (res.status === 404) {
@@ -154,7 +157,10 @@ export class HttpSpacePrimitives implements SpacePrimitives {
private responseToMeta(name: string, res: Response): FileMeta {
return {
name,
size: +res.headers.get("Content-Length")!,
// The server may set a custom X-Content-Length header in case a GET request was sent with X-Get-Meta, in which case the body may be omitted
size: res.headers.has("X-Content-Length")
? +res.headers.get("X-Content-Length")!
: +res.headers.get("Content-Length")!,
contentType: res.headers.get("Content-type")!,
lastModified: +(res.headers.get("X-Last-Modified") || "0"),
perm: (res.headers.get("X-Permission") as "rw" | "ro") || "rw",