Skip to content

Commit

Permalink
refactor(core): bunch of small stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Jun 5, 2024
1 parent 6386fbb commit 9f3d7c5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion core/components/PlayerlistManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default class PlayerlistManager {
if (payload.event === 'playerJoining') {
try {
if (typeof payload.id !== 'number') throw new Error(`invalid player id`);
if (typeof this.#playerlist[payload.id] !== 'undefined') throw new Error(`duplicated player id`);
if (this.#playerlist[payload.id] !== undefined) throw new Error(`duplicated player id`);
//TODO: pass serverInstance instead of playerDatabase
const svPlayer = new ServerPlayer(payload.id, payload.player, this.#txAdmin.playerDatabase);
this.#playerlist[payload.id] = svPlayer;
Expand Down
4 changes: 2 additions & 2 deletions core/components/WebServer/wsRooms/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const console = consoleFactory(modulename);
/**
* Returns the fxserver's data
*/
const getinitialData = (txAdmin: TxAdmin): GlobalStatusType => {
const getInitialData = (txAdmin: TxAdmin): GlobalStatusType => {
// Check if the deployer is running or setup is pending
let configPendingStep: ServerConfigPendingStepType;
if (globals.deployer !== null) {
Expand Down Expand Up @@ -50,6 +50,6 @@ export default (txAdmin: TxAdmin): RoomType => ({
cumulativeBuffer: false,
outBuffer: null,
initialData: () => {
return getinitialData(txAdmin);
return getInitialData(txAdmin);
},
})
47 changes: 47 additions & 0 deletions core/extras/MemCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { cloneDeep } from 'lodash-es';

export default class MemCache<T = any> {
public readonly ttl: number;
public dataTimestamp: number | undefined;
private data: T | undefined;

constructor(ttlSeconds = 60) {
this.ttl = ttlSeconds * 1000; //converting to ms
}

/**
* Checks if the data is still valid or wipes it
*/
isValid() {
if (this.dataTimestamp === undefined) return false;
if (this.dataTimestamp < Date.now() - this.ttl) {
this.dataTimestamp = undefined;
this.data = undefined;
return false;
}
return true;
}

/**
* Sets the cache
*/
set(data: T) {
this.dataTimestamp = Date.now();
this.data = data;
}

/**
* Returns the cache if valid, or undefined
*/
get() {
if (this.dataTimestamp === undefined || this.data === undefined) {
return undefined;
}

if (this.isValid()) {
return cloneDeep<T>(this.data);
} else {
return undefined;
}
}
};
30 changes: 0 additions & 30 deletions core/extras/dataCache.js

This file was deleted.

6 changes: 3 additions & 3 deletions core/webroutes/diagnostics/page.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const modulename = 'WebServer:Diagnostics';
import { AuthedCtx } from '@core/components/WebServer/ctxTypes';
import Cache from '../../extras/dataCache';
import MemCache from '@extras/MemCache';
import * as diagnosticsFuncs from './diagnosticsFuncs';
import consoleFactory from '@extras/console';
const console = consoleFactory(modulename);
const cache = new Cache(5);
const cache = new MemCache(5);


/**
* Returns the output page containing the full report
*/
export default async function Diagnostics(ctx: AuthedCtx) {
const cachedData = cache.get();
if (cachedData !== false) {
if (cachedData) {
cachedData.message = 'This page was cached in the last 5 seconds';
return ctx.utils.render('main/diagnostics', cachedData);
}
Expand Down
15 changes: 6 additions & 9 deletions core/webroutes/diagnostics/sendReport.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
const modulename = 'WebServer:SendDiagnosticsReport';
import Logger from '@core/components/Logger';
import ConfigVault from '@core/components/ConfigVault';
import got from '@core/extras/got';
import { txEnv } from '@core/globalData';
import { GenericApiErrorResp } from '@shared/genericApiTypes';
import * as diagnosticsFuncs from './diagnosticsFuncs';
import AdminVault from '@core/components/AdminVault';
import { redactApiKeys } from '@core/extras/helpers';
import { getServerDataConfigs, getServerDataContent, ServerDataContentType, ServerDataConfigsType } from '@core/extras/serverDataScanner.js';
import PlayerDatabase from '@core/components/PlayerDatabase';
import Cache from '@core/extras/dataCache';
import { getChartData } from '../chartData';
import MemCache from '@extras/MemCache';
import consoleFactory, { getLogBuffer } from '@extras/console';
import { AuthedCtx } from '@core/components/WebServer/ctxTypes';
const console = consoleFactory(modulename);

//Consts & Helpers
const reportIdCache = new Cache(60);
const reportIdCache = new MemCache<string>(60);
const maskedKeywords = ['key', 'license', 'pass', 'private', 'secret', 'token'];
const maskString = (input: string) => input.replace(/\w/gi, 'x');
const maskIps = (input: string) => input.replace(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/gi, 'x.x.x.x');
Expand All @@ -41,8 +36,10 @@ export default async function SendDiagnosticsReport(ctx: AuthedCtx) {

//Rate limit (and cache) report submissions
const cachedReportId = reportIdCache.get();
if (cachedReportId !== false) {
return sendTypedResp({ error: `You can send at most one report per minute. Your last report ID was ${cachedReportId}.` });
if (cachedReportId) {
return sendTypedResp({
error: `You can send at most one report per minute. Your last report ID was ${cachedReportId}.`
});
}

//Diagnostics
Expand Down

0 comments on commit 9f3d7c5

Please sign in to comment.