Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Rewrite updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Aug 14, 2024
1 parent 0db4960 commit d41601f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 59 deletions.
52 changes: 29 additions & 23 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -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[] = [];

Check warning on line 8 in src/Client.ts

View workflow job for this annotation

GitHub Actions / check linting (eslint)

'Client' was used before it was defined

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
Expand All @@ -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;
6 changes: 3 additions & 3 deletions src/Private/CacheHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}

Expand Down
31 changes: 0 additions & 31 deletions src/Private/CheckUpdates.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/Private/Requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
51 changes: 51 additions & 0 deletions src/Private/Updater.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<string | void> {
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;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions src/typings/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface ClientOptions {
rateLimit?: 'AUTO' | 'HARD' | 'NONE';
silent?: boolean;
checkForUpdates?: boolean;
checkForUpdatesInterval?: number;
}

0 comments on commit d41601f

Please sign in to comment.