mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-28 17:38:31 +00:00
web/workers/fetch: retry 5 more times before throwing an error
hopium should probably add a timeout too
This commit is contained in:
parent
066a47c82d
commit
9225b31986
@ -1,21 +1,34 @@
|
|||||||
import { OPFSStorage } from "$lib/storage";
|
import { OPFSStorage } from "$lib/storage";
|
||||||
|
|
||||||
const error = (code: string) => {
|
let attempts = 0;
|
||||||
// TODO: return proper errors and code here
|
|
||||||
self.postMessage({
|
|
||||||
cobaltFetchWorker: {
|
|
||||||
error: code,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchFile = async (url: string) => {
|
const fetchFile = async (url: string) => {
|
||||||
|
const error = async (code: string) => {
|
||||||
|
attempts++;
|
||||||
|
|
||||||
|
if (attempts <= 5) {
|
||||||
|
// try 5 more times before actually failing
|
||||||
|
|
||||||
|
console.log("fetch attempt", attempts);
|
||||||
|
await fetchFile(url);
|
||||||
|
} else {
|
||||||
|
// if it still fails, then throw an error and quit
|
||||||
|
self.postMessage({
|
||||||
|
cobaltFetchWorker: {
|
||||||
|
// TODO: return proper error code here
|
||||||
|
// (error.code and not just random shit i typed up)
|
||||||
|
error: code,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
self.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
error("file response wasn't ok");
|
return error("file response wasn't ok");
|
||||||
return self.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentType = response.headers.get('Content-Type') || 'application/octet-stream';
|
const contentType = response.headers.get('Content-Type') || 'application/octet-stream';
|
||||||
@ -27,8 +40,7 @@ const fetchFile = async (url: string) => {
|
|||||||
const storage = await OPFSStorage.init();
|
const storage = await OPFSStorage.init();
|
||||||
|
|
||||||
if (!reader) {
|
if (!reader) {
|
||||||
error("no reader");
|
return error("no reader");
|
||||||
return self.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let receivedBytes = 0;
|
let receivedBytes = 0;
|
||||||
@ -51,14 +63,13 @@ const fetchFile = async (url: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (receivedBytes === 0) {
|
if (receivedBytes === 0) {
|
||||||
error("tunnel is broken");
|
return error("tunnel is broken");
|
||||||
return self.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const file = await storage.res();
|
const file = await storage.res();
|
||||||
|
|
||||||
if (Number(contentLength) !== file.size) {
|
if (Number(contentLength) !== file.size) {
|
||||||
error("file is not downloaded fully");
|
return error("file was not downloaded fully");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.postMessage({
|
self.postMessage({
|
||||||
@ -71,8 +82,7 @@ const fetchFile = async (url: string) => {
|
|||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
error("error when downloading the file");
|
return error("error when downloading the file");
|
||||||
return self.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user