From 90e32a454d3806399385d295b7f74d8e42015ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Vani=C5=A1?= Date: Sun, 10 Mar 2024 16:06:47 +0100 Subject: [PATCH] simpleReq retry with backoff --- src/main/utils/http.ts | 13 +++++++++++-- src/main/utils/vencordLoader.ts | 4 ++++ src/updater/main.ts | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/utils/http.ts b/src/main/utils/http.ts index 12e14a0..6070b3b 100644 --- a/src/main/utils/http.ts +++ b/src/main/utils/http.ts @@ -20,7 +20,12 @@ export async function downloadFile(url: string, file: string, options: RequestOp ); } -export function simpleReq(url: string, options: RequestOptions = {}): Promise { +export function simpleReq( + url: string, + options: RequestOptions = {}, + retries = 0, + backoffDelay = 1000 +): Promise { return new Promise((resolve, reject) => { get(url, options, res => { const { statusCode, statusMessage, headers } = res; @@ -30,7 +35,11 @@ export function simpleReq(url: string, options: RequestOptions = {}): Promise { - reject(new Error(`Network error: ${err.message}`)); + if (retries > 10) { + reject(err); + } else { + setTimeout(() => resolve(simpleReq(url, options, retries + 1, backoffDelay * 2)), backoffDelay); + } }); }); } diff --git a/src/main/utils/vencordLoader.ts b/src/main/utils/vencordLoader.ts index 293654a..3b79715 100644 --- a/src/main/utils/vencordLoader.ts +++ b/src/main/utils/vencordLoader.ts @@ -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( diff --git a/src/updater/main.ts b/src/updater/main.ts index 059afb9..e48ed27 100644 --- a/src/updater/main.ts +++ b/src/updater/main.ts @@ -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();