From d41601f698235d374c136a1c437f37fa32031632 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 14 Aug 2024 08:50:19 +0800 Subject: [PATCH] Rewrite updater --- src/Client.ts | 52 +++++++++++++++++++++---------------- src/Private/CacheHandler.ts | 6 ++--- src/Private/CheckUpdates.ts | 31 ---------------------- src/Private/Requests.ts | 2 +- src/Private/Updater.ts | 51 ++++++++++++++++++++++++++++++++++++ src/index.ts | 2 +- src/typings/Client.d.ts | 1 + 7 files changed, 86 insertions(+), 59 deletions(-) delete mode 100644 src/Private/CheckUpdates.ts create mode 100644 src/Private/Updater.ts diff --git a/src/Client.ts b/src/Client.ts index 55ef4a28..434ab368 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1,39 +1,28 @@ -import CheckUpdates from './Private/CheckUpdates'; import CacheHandler from './Private/CacheHandler'; import { ClientOptions } from './typings/Client'; import Requests from './Private/Requests'; +import Updater from './Private/Updater'; import Errors from './Errors'; import API from './API'; -const clients: any[] = []; +const clients: Client[] = []; class Client { readonly key: string; declare requests: Requests; declare cacheHandler: CacheHandler; + declare updater: Updater; - declare cache: boolean; - declare cacheTime: number; - declare cacheMaxKeys: number; - declare cacheCheckPeriod: number; - declare rateLimit?: 'AUTO' | 'HARD' | 'NONE'; - declare silent: boolean; - declare checkForUpdates: boolean; + declare options: ClientOptions; constructor(key: string, options?: ClientOptions) { this.key = key; - this.cache = options?.cache ?? true; - this.cacheTime = options?.cacheTime ?? 300; - this.cacheMaxKeys = options?.cacheMaxKeys ?? -1; - this.cacheCheckPeriod = options?.cacheCheckPeriod ?? 180; - this.rateLimit = options?.rateLimit ?? 'AUTO'; - this.silent = options?.silent ?? false; - this.checkForUpdates = options?.checkForUpdates ?? true; - + this.options = this.parasOptions(options); this.requests = new Requests(this); this.cacheHandler = new CacheHandler(this); + this.updater = new Updater(); for (const func in API) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -47,18 +36,35 @@ class Client { if (clients.find((x) => x.key === key)) { // eslint-disable-next-line no-console console.warn(Errors.MULTIPLE_INSTANCES); - return clients.find((x) => x.key === key); + const found = clients.find((x) => x.key === key); + if (found) return found; } - if (this.checkForUpdates) { - CheckUpdates().catch(() => { - // eslint-disable-next-line no-console - if (!this.silent) console.warn(Errors.UPDATER_REQUEST_NOT_OK); - }); + if (this.options.checkForUpdates) { + setInterval( + () => { + this.updater.checkForUpdates(); + // 3600000 ms = 1 hour + }, + 1000 * 60 * (this.options.checkForUpdatesInterval ?? 60) + ); } clients.push(this); } + + private parasOptions(options?: ClientOptions): ClientOptions { + return { + cache: options?.cache ?? true, + cacheTime: options?.cacheTime ?? 300, + cacheMaxKeys: options?.cacheMaxKeys ?? -1, + cacheCheckPeriod: options?.cacheCheckPeriod ?? 180, + rateLimit: options?.rateLimit ?? 'AUTO', + silent: options?.silent ?? false, + checkForUpdates: options?.checkForUpdates ?? true, + checkForUpdatesInterval: options?.checkForUpdatesInterval ?? 60 + }; + } } export default Client; diff --git a/src/Private/CacheHandler.ts b/src/Private/CacheHandler.ts index 49d02245..3283c726 100644 --- a/src/Private/CacheHandler.ts +++ b/src/Private/CacheHandler.ts @@ -7,9 +7,9 @@ class CacheHandler { constructor(client: Client) { this.client = client; this.cache = new NodeCache({ - stdTTL: this.client.cacheTime, - maxKeys: this.client.cacheMaxKeys, - checkperiod: this.client.cacheCheckPeriod + stdTTL: this.client.options.cacheTime, + maxKeys: this.client.options.cacheMaxKeys, + checkperiod: this.client.options.cacheCheckPeriod }); } diff --git a/src/Private/CheckUpdates.ts b/src/Private/CheckUpdates.ts deleted file mode 100644 index 76306663..00000000 --- a/src/Private/CheckUpdates.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* eslint-disable no-console */ -import { version } from '../../package.json'; -import Errors from '../Errors'; -import axios from 'axios'; - -function compareVersions(a: string, b: string): boolean { - const pa = a.split('.'); - const pb = b.split('.'); - for (let i = 0; 3 > i; i++) { - const na = Number(pa[i]); - const nb = Number(pb[i]); - if (na > nb) return false; - if (nb > na) return true; - if (!isNaN(na) && isNaN(nb)) return false; - if (isNaN(na) && !isNaN(nb)) return true; - } - return false; -} - -export default async function (): Promise { - const request = await axios.get('https://registry.npmjs.org/hypixel-api-reborn'); - if (200 !== request.status) return console.log(Errors.UPDATER_REQUEST_NOT_OK); - const metadata = await request.data; - const latest = metadata['dist-tags'].latest; - const compare = compareVersions(version, latest); - if (compare) { - console.log( - `New version of hypixel-api-reborn is available! Current version: ${version}, Latest version: ${latest}` - ); - } -} diff --git a/src/Private/Requests.ts b/src/Private/Requests.ts index f44d0a62..c38a6327 100644 --- a/src/Private/Requests.ts +++ b/src/Private/Requests.ts @@ -46,7 +46,7 @@ class Requests { parsedRes._headers = res.headers; parsedRes.raw = Boolean(info.raw); if (info.noCache) return parsedRes; - if (this.client.cache && !info.raw) { + if (this.client.options.cache && !info.raw) { this.client.cacheHandler.set(endpoint, parsedRes); } diff --git a/src/Private/Updater.ts b/src/Private/Updater.ts new file mode 100644 index 00000000..682e26b1 --- /dev/null +++ b/src/Private/Updater.ts @@ -0,0 +1,51 @@ +/* eslint-disable no-console */ +import { version } from '../../package.json'; +import Errors from '../Errors'; +import axios from 'axios'; + +class Updater { + currentVersion: string; + latestVersion: string; + constructor() { + this.currentVersion = version; + this.latestVersion = '0.0.0'; + } + + async checkForUpdates(): Promise { + this.latestVersion = (await this.getLatestVersion()) ?? '0.0.0'; + const compare = this.compareVersions(this.currentVersion, this.latestVersion); + if (compare) { + console.log( + `New version of hypixel-api-reborn is available! Current version: ${ + this.currentVersion + }, Latest version: ${this.latestVersion}` + ); + } + } + + async getLatestVersion(): Promise { + const request = await axios.get('https://registry.npmjs.org/hypixel-api-reborn'); + if (200 !== request.status) { + console.log(Errors.UPDATER_REQUEST_NOT_OK); + return; + } + const metadata = await request.data; + return metadata['dist-tags'].latest; + } + + compareVersions(a: string, b: string): boolean { + const pa = a.split('.'); + const pb = b.split('.'); + for (let i = 0; 3 > i; i++) { + const na = Number(pa[i]); + const nb = Number(pb[i]); + if (na > nb) return false; + if (nb > na) return true; + if (!isNaN(na) && isNaN(nb)) return false; + if (isNaN(na) && !isNaN(nb)) return true; + } + return false; + } +} + +export default Updater; diff --git a/src/index.ts b/src/index.ts index 046b70ef..71cf5124 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ export * from './Client'; export * from './Errors'; export * from './Private/CacheHandler'; -export * from './Private/CheckUpdates'; +export * from './Private/Updater'; export * from './Private/Endpoint'; export * from './Private/Requests'; export * from './Private/uuidCache'; diff --git a/src/typings/Client.d.ts b/src/typings/Client.d.ts index b05a0f2b..447fb032 100644 --- a/src/typings/Client.d.ts +++ b/src/typings/Client.d.ts @@ -6,4 +6,5 @@ export interface ClientOptions { rateLimit?: 'AUTO' | 'HARD' | 'NONE'; silent?: boolean; checkForUpdates?: boolean; + checkForUpdatesInterval?: number; }