simpleReq retry with backoff
This commit is contained in:
parent
bd4cfe4e7a
commit
90e32a454d
3 changed files with 20 additions and 2 deletions
|
@ -20,7 +20,12 @@ export async function downloadFile(url: string, file: string, options: RequestOp
|
|||
);
|
||||
}
|
||||
|
||||
export function simpleReq(url: string, options: RequestOptions = {}): Promise<IncomingMessage> {
|
||||
export function simpleReq(
|
||||
url: string,
|
||||
options: RequestOptions = {},
|
||||
retries = 0,
|
||||
backoffDelay = 1000
|
||||
): Promise<IncomingMessage> {
|
||||
return new Promise<IncomingMessage>((resolve, reject) => {
|
||||
get(url, options, res => {
|
||||
const { statusCode, statusMessage, headers } = res;
|
||||
|
@ -30,7 +35,11 @@ export function simpleReq(url: string, options: RequestOptions = {}): Promise<In
|
|||
}
|
||||
resolve(res);
|
||||
}).on("error", err => {
|
||||
reject(new Error(`Network error: ${err.message}`));
|
||||
if (retries > 10) {
|
||||
reject(err);
|
||||
} else {
|
||||
setTimeout(() => resolve(simpleReq(url, options, retries + 1, backoffDelay * 2)), backoffDelay);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -46,6 +46,10 @@ export async function githubGet(endpoint: string) {
|
|||
export async function downloadVencordFiles() {
|
||||
const release = await githubGet("/repos/Vendicated/Vencord/releases/latest");
|
||||
|
||||
if (!release) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { assets } = JSON.parse(release.toString("utf-8")) as ReleaseData;
|
||||
|
||||
await Promise.all(
|
||||
|
|
|
@ -81,6 +81,11 @@ export async function checkUpdates() {
|
|||
|
||||
try {
|
||||
const raw = await githubGet("/repos/Vencord/Vesktop/releases/latest");
|
||||
|
||||
if (!raw) {
|
||||
throw new Error("Failed to retrieve update data.");
|
||||
}
|
||||
|
||||
const data = JSON.parse(raw.toString("utf-8")) as ReleaseData;
|
||||
|
||||
const oldVersion = app.getVersion();
|
||||
|
|
Reference in a new issue