diff options
author | 0neGal <mail@0negal.com> | 2024-12-20 19:03:41 +0100 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2024-12-20 19:03:41 +0100 |
commit | 010126567cca3a02073d71a29030ba86d96c195f (patch) | |
tree | 7cd2d18efd2489b5b60bd2c0f81859b6ead90ea3 /src/modules | |
parent | 1be8c774251c3a1e1e3559f628aa24b465ab8685 (diff) | |
download | Viper-010126567cca3a02073d71a29030ba86d96c195f.tar.gz Viper-010126567cca3a02073d71a29030ba86d96c195f.zip |
added requests.check() and uses of it
Very minimal uses of it, currently it simply checks every 30s and on
startup whether a set of domains/endpoints we use work, and if all fail,
then we assume something is wrong with the internet.
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/requests.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/modules/requests.js b/src/modules/requests.js index dac3a4f..ffa57c6 100644 --- a/src/modules/requests.js +++ b/src/modules/requests.js @@ -25,6 +25,15 @@ ipcMain.handle("request", async (e, ...args) => { return res; }) +ipcMain.handle("request-check", async (_, ...args) => { + let res = false; + + try { + res = await requests.check(...args); + }catch(err) {} + + return res; +}) // updates `cache_dir` and `cache_file` function set_paths() { @@ -244,4 +253,43 @@ requests.get = (host, path, cache_key, ignore_max_time_when_offline = true, max_ }) } +// checks whether a list of `endpoints` can be contacted +requests.check = async (endpoints) => { + // turn `endpoints` into an array, if it isn't already + if (typeof endpoints == "string") { + endpoints = [endpoints]; + } + + // list of what failed and succeeded, will be returned later + let res = { + failed: [], + succeeded: [] + } + + // run through all the endpoints + for (let endpoint of endpoints) { + let req; + + // attempt to do a request + try { + req = await fetch(endpoint); + } catch(err) { // something went wrong! + res.failed.push(endpoint); + continue; + } + + // if we're within the `200-299` response code range, we + // consider it a success + if (req.status < 300 && req.status >= 200) { + res.succeeded.push(endpoint); + continue; + } + + // we failed! + res.failed.push(endpoint); + } + + return res; +} + module.exports = requests; |