mirror of
https://github.com/MCSManager/MCSManager.git
synced 2025-04-06 17:10:29 +08:00
Refactor: common code
This commit is contained in:
parent
ea88552aec
commit
b516b690b8
@ -1,19 +0,0 @@
|
||||
export default class GlobalVariable {
|
||||
public static readonly map = new Map<string, any>();
|
||||
|
||||
public static set(k: string, v: any) {
|
||||
GlobalVariable.map.set(k, v);
|
||||
}
|
||||
|
||||
public static get(k: string, def?: any) {
|
||||
if (GlobalVariable.map.has(k)) {
|
||||
return GlobalVariable.map.get(k);
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static del(k: string) {
|
||||
return GlobalVariable.map.delete(k);
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
import { Socket } from "socket.io";
|
||||
|
||||
// Application instance data stream forwarding adapter
|
||||
|
||||
export default class InstanceStreamListener {
|
||||
// Instance uuid -> Socket[]
|
||||
public readonly listenMap = new Map<string, Socket[]>();
|
||||
|
||||
public requestForward(socket: Socket, instanceUuid: string) {
|
||||
if (this.listenMap.has(instanceUuid)) {
|
||||
const sockets = this.listenMap.get(instanceUuid);
|
||||
for (const iterator of sockets)
|
||||
if (iterator.id === socket.id)
|
||||
throw new Error(
|
||||
`This Socket ${socket.id} already exists in the specified instance listening table`
|
||||
);
|
||||
sockets.push(socket);
|
||||
} else {
|
||||
this.listenMap.set(instanceUuid, [socket]);
|
||||
}
|
||||
}
|
||||
|
||||
public cannelForward(socket: Socket, instanceUuid: string) {
|
||||
if (!this.listenMap.has(instanceUuid))
|
||||
throw new Error(`The specified ${instanceUuid} does not exist in the listening table`);
|
||||
const socketList = this.listenMap.get(instanceUuid);
|
||||
socketList.forEach((v, index) => {
|
||||
if (v.id === socket.id) socketList.splice(index, 1);
|
||||
});
|
||||
}
|
||||
|
||||
public forward(instanceUuid: string, data: any) {
|
||||
const sockets = this.listenMap.get(instanceUuid);
|
||||
sockets.forEach((socket) => {
|
||||
if (socket && socket.connected) socket.emit("instance/stdout", data);
|
||||
});
|
||||
}
|
||||
|
||||
public forwardViaCallback(instanceUuid: string, callback: (socket: Socket) => void) {
|
||||
if (this.listenMap.has(instanceUuid)) {
|
||||
const sockets = this.listenMap.get(instanceUuid);
|
||||
sockets.forEach((socket) => {
|
||||
if (socket && socket.connected) callback(socket);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public hasListenInstance(instanceUuid: string) {
|
||||
return this.listenMap.has(instanceUuid) && this.listenMap.get(instanceUuid).length > 0;
|
||||
}
|
||||
}
|
@ -1,116 +0,0 @@
|
||||
interface IMap {
|
||||
size: number;
|
||||
forEach: (value: any, key?: any) => void;
|
||||
}
|
||||
|
||||
interface Page<T> {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
maxPage: number;
|
||||
total: number;
|
||||
data: T[];
|
||||
}
|
||||
|
||||
// Provide the MAP query interface used by the routing layer
|
||||
export class QueryMapWrapper {
|
||||
constructor(public map: IMap) {}
|
||||
|
||||
select<T>(condition: (v: T) => boolean): T[] {
|
||||
const result: T[] = [];
|
||||
this.map.forEach((v: T) => {
|
||||
if (condition(v)) result.push(v);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
page<T>(data: T[], page = 1, pageSize = 10) {
|
||||
const start = (page - 1) * pageSize;
|
||||
const end = start + pageSize;
|
||||
let size = data.length;
|
||||
let maxPage = 0;
|
||||
while (size > 0) {
|
||||
size -= pageSize;
|
||||
maxPage++;
|
||||
}
|
||||
return {
|
||||
page,
|
||||
pageSize,
|
||||
maxPage,
|
||||
data: data.slice(start, end)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Data source interface for QueryWrapper to use
|
||||
export interface IDataSource<T> {
|
||||
selectPage: (condition: any, page: number, pageSize: number) => Page<T>;
|
||||
select: (condition: any) => any[];
|
||||
update: (condition: any, data: any) => void;
|
||||
delete: (condition: any) => void;
|
||||
insert: (data: any) => void;
|
||||
}
|
||||
|
||||
// MYSQL data source
|
||||
export class MySqlSource<T> implements IDataSource<T> {
|
||||
selectPage: (condition: any, page: number, pageSize: number) => Page<T>;
|
||||
select: (condition: any) => any[];
|
||||
update: (condition: any, data: any) => void;
|
||||
delete: (condition: any) => void;
|
||||
insert: (data: any) => void;
|
||||
}
|
||||
|
||||
// local file data source (embedded microdatabase)
|
||||
export class LocalFileSource<T> implements IDataSource<T> {
|
||||
constructor(public data: any) {}
|
||||
|
||||
selectPage(condition: any, page = 1, pageSize = 10) {
|
||||
const result: T[] = [];
|
||||
this.data.forEach((v: any) => {
|
||||
for (const key in condition) {
|
||||
const dataValue = v[key];
|
||||
const targetValue = condition[key];
|
||||
if (targetValue[0] == "%") {
|
||||
if (!dataValue.includes(targetValue.slice(1, targetValue.length - 1))) return false;
|
||||
} else {
|
||||
if (targetValue !== dataValue) return false;
|
||||
}
|
||||
}
|
||||
result.push(v);
|
||||
});
|
||||
return this.page(result, page, pageSize);
|
||||
}
|
||||
|
||||
page(data: T[], page = 1, pageSize = 10) {
|
||||
const start = (page - 1) * pageSize;
|
||||
const end = start + pageSize;
|
||||
let size = data.length;
|
||||
let maxPage = 0;
|
||||
while (size > 0) {
|
||||
size -= pageSize;
|
||||
maxPage++;
|
||||
}
|
||||
return {
|
||||
page,
|
||||
pageSize,
|
||||
maxPage,
|
||||
total: data.length,
|
||||
data: data.slice(start, end)
|
||||
};
|
||||
}
|
||||
|
||||
select(condition: any): any[] {
|
||||
return null;
|
||||
}
|
||||
update(condition: any, data: any) {}
|
||||
delete(condition: any) {}
|
||||
insert(data: any) {}
|
||||
}
|
||||
|
||||
// Provide the unified data query interface used by the routing layer
|
||||
export class QueryWrapper<T> {
|
||||
constructor(public dataSource: IDataSource<T>) {}
|
||||
|
||||
selectPage(condition: any, page = 1, pageSize = 10) {
|
||||
return this.dataSource.selectPage(condition, page, pageSize);
|
||||
}
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
import os from "os";
|
||||
import osUtils from "os-utils";
|
||||
import fs from "fs";
|
||||
// import systeminformation from "systeminformation";
|
||||
|
||||
interface IInfoTable {
|
||||
[key: string]: number;
|
||||
}
|
||||
|
||||
interface ISystemInfo {
|
||||
cpuUsage: number;
|
||||
memUsage: number;
|
||||
totalmem: number;
|
||||
freemem: number;
|
||||
type: string;
|
||||
hostname: string;
|
||||
platform: string;
|
||||
release: string;
|
||||
uptime: number;
|
||||
cwd: string;
|
||||
processCpu: number;
|
||||
processMem: number;
|
||||
loadavg: number[];
|
||||
}
|
||||
|
||||
// System details are updated every time
|
||||
const info: ISystemInfo = {
|
||||
type: os.type(),
|
||||
hostname: os.hostname(),
|
||||
platform: os.platform(),
|
||||
release: os.release(),
|
||||
uptime: os.uptime(),
|
||||
cwd: process.cwd(),
|
||||
loadavg: os.loadavg(),
|
||||
freemem: 0,
|
||||
cpuUsage: 0,
|
||||
memUsage: 0,
|
||||
totalmem: 0,
|
||||
processCpu: 0,
|
||||
processMem: 0
|
||||
};
|
||||
|
||||
// periodically refresh the cache
|
||||
setInterval(() => {
|
||||
if (os.platform() === "linux") {
|
||||
return setLinuxSystemInfo();
|
||||
}
|
||||
if (os.platform() === "win32") {
|
||||
return setWindowsSystemInfo();
|
||||
}
|
||||
return otherSystemInfo();
|
||||
}, 3000);
|
||||
|
||||
function otherSystemInfo() {
|
||||
info.freemem = os.freemem();
|
||||
info.totalmem = os.totalmem();
|
||||
info.memUsage = (os.totalmem() - os.freemem()) / os.totalmem();
|
||||
osUtils.cpuUsage((p) => (info.cpuUsage = p));
|
||||
}
|
||||
|
||||
function setWindowsSystemInfo() {
|
||||
info.freemem = os.freemem();
|
||||
info.totalmem = os.totalmem();
|
||||
info.memUsage = (os.totalmem() - os.freemem()) / os.totalmem();
|
||||
osUtils.cpuUsage((p) => (info.cpuUsage = p));
|
||||
}
|
||||
|
||||
function setLinuxSystemInfo() {
|
||||
try {
|
||||
// read memory data based on /proc/meminfo
|
||||
const data = fs.readFileSync("/proc/meminfo", { encoding: "utf-8" });
|
||||
const list = data.split("\n");
|
||||
const infoTable: IInfoTable = {};
|
||||
list.forEach((line) => {
|
||||
const kv = line.split(":");
|
||||
if (kv.length === 2) {
|
||||
const k = kv[0].replace(/ /gim, "").replace(/\t/gim, "").trim().toLowerCase();
|
||||
let v = kv[1].replace(/ /gim, "").replace(/\t/gim, "").trim().toLowerCase();
|
||||
v = v.replace(/kb/gim, "").replace(/mb/gim, "").replace(/gb/gim, "");
|
||||
let vNumber = parseInt(v);
|
||||
if (isNaN(vNumber)) vNumber = 0;
|
||||
infoTable[k] = vNumber;
|
||||
}
|
||||
});
|
||||
const memAvailable = infoTable["memavailable"] ?? infoTable["memfree"];
|
||||
const memTotal = infoTable["memtotal"];
|
||||
info.freemem = memAvailable * 1024;
|
||||
info.totalmem = memTotal * 1024;
|
||||
info.memUsage = (info.totalmem - info.freemem) / info.totalmem;
|
||||
osUtils.cpuUsage((p) => (info.cpuUsage = p));
|
||||
} catch (error) {
|
||||
// If the reading is wrong, the default general reading method is automatically used
|
||||
otherSystemInfo();
|
||||
}
|
||||
}
|
||||
|
||||
export function systemInfo() {
|
||||
return info;
|
||||
}
|
@ -1,109 +1,2 @@
|
||||
import path from "path";
|
||||
import fs from "fs-extra";
|
||||
|
||||
class StorageSubsystem {
|
||||
public static readonly DATA_PATH = path.normalize(path.join(process.cwd(), "data"));
|
||||
public static readonly INDEX_PATH = path.normalize(path.join(process.cwd(), "data", "index"));
|
||||
|
||||
private checkFileName(name: string) {
|
||||
const blackList = ["\\", "/", ".."];
|
||||
for (const ch of blackList) {
|
||||
if (name.includes(ch)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public writeFile(name: string, data: string) {
|
||||
const targetPath = path.normalize(path.join(StorageSubsystem.DATA_PATH, name));
|
||||
fs.writeFileSync(targetPath, data, { encoding: "utf-8" });
|
||||
}
|
||||
|
||||
public readFile(name: string) {
|
||||
const targetPath = path.normalize(path.join(StorageSubsystem.DATA_PATH, name));
|
||||
return fs.readFileSync(targetPath, { encoding: "utf-8" });
|
||||
}
|
||||
|
||||
public deleteFile(name: string) {
|
||||
const targetPath = path.normalize(path.join(StorageSubsystem.DATA_PATH, name));
|
||||
fs.removeSync(targetPath);
|
||||
}
|
||||
|
||||
public fileExists(name: string) {
|
||||
const targetPath = path.normalize(path.join(StorageSubsystem.DATA_PATH, name));
|
||||
return fs.existsSync(targetPath);
|
||||
}
|
||||
|
||||
// Stored in local file based on class definition and identifier
|
||||
public store(category: string, uuid: string, object: any) {
|
||||
const dirPath = path.join(StorageSubsystem.DATA_PATH, category);
|
||||
if (!fs.existsSync(dirPath)) fs.mkdirsSync(dirPath);
|
||||
if (!this.checkFileName(uuid))
|
||||
throw new Error(`UUID ${uuid} does not conform to specification`);
|
||||
const filePath = path.join(dirPath, `${uuid}.json`);
|
||||
const data = JSON.stringify(object, null, 4);
|
||||
fs.writeFileSync(filePath, data, { encoding: "utf-8" });
|
||||
}
|
||||
|
||||
// deep copy of the primitive type with the copy target as the prototype
|
||||
protected defineAttr(target: any, object: any): any {
|
||||
for (const v of Object.keys(target)) {
|
||||
const objectValue = object[v];
|
||||
if (objectValue === undefined) continue;
|
||||
if (objectValue instanceof Array) {
|
||||
target[v] = objectValue;
|
||||
continue;
|
||||
}
|
||||
if (objectValue instanceof Object && typeof objectValue === "object") {
|
||||
this.defineAttr(target[v], objectValue);
|
||||
continue;
|
||||
}
|
||||
target[v] = objectValue;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate an object based on the class definition and identifier
|
||||
*/
|
||||
public load(category: string, classz: any, uuid: string) {
|
||||
const dirPath = path.join(StorageSubsystem.DATA_PATH, category);
|
||||
if (!fs.existsSync(dirPath)) fs.mkdirsSync(dirPath);
|
||||
if (!this.checkFileName(uuid))
|
||||
throw new Error(`UUID ${uuid} does not conform to specification`);
|
||||
const filePath = path.join(dirPath, `${uuid}.json`);
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const data = fs.readFileSync(filePath, { encoding: "utf-8" });
|
||||
const dataObject = JSON.parse(data);
|
||||
const target = new classz();
|
||||
// for (const v of Object. keys(target)) {
|
||||
// if (dataObject[v] !== undefined) target[v] = dataObject[v];
|
||||
// }
|
||||
// deep object copy
|
||||
return this.defineAttr(target, dataObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all identifiers related to this class through the class definition
|
||||
*/
|
||||
public list(category: string) {
|
||||
const dirPath = path.join(StorageSubsystem.DATA_PATH, category);
|
||||
if (!fs.existsSync(dirPath)) fs.mkdirsSync(dirPath);
|
||||
const files = fs.readdirSync(dirPath);
|
||||
const result = new Array<string>();
|
||||
files.forEach((name) => {
|
||||
result.push(name.replace(path.extname(name), ""));
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an identifier instance of the specified type through the class definition
|
||||
*/
|
||||
public delete(category: string, uuid: string) {
|
||||
const filePath = path.join(StorageSubsystem.DATA_PATH, category, `${uuid}.json`);
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
fs.removeSync(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
import { StorageSubsystem } from "common";
|
||||
export default new StorageSubsystem();
|
||||
|
@ -1,75 +0,0 @@
|
||||
export function configureEntityParams(self: any, args: any, key: string, typeFn?: Function): any {
|
||||
const selfDefaultValue = self[key] ?? null;
|
||||
const v = args[key] != null ? args[key] : selfDefaultValue;
|
||||
|
||||
if (typeFn === Number) {
|
||||
if (v === "" || v == null) {
|
||||
self[key] = null;
|
||||
} else {
|
||||
if (isNaN(Number(v)))
|
||||
throw new Error(
|
||||
`ConfigureEntityParams Error: Expected type to be Number, but got ${typeof v}`
|
||||
);
|
||||
self[key] = Number(v);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeFn === String) {
|
||||
if (v == null) {
|
||||
self[key] = null;
|
||||
} else {
|
||||
self[key] = String(v);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeFn === Boolean) {
|
||||
if (v == null) {
|
||||
self[key] = false;
|
||||
} else {
|
||||
self[key] = Boolean(v);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeFn === Array) {
|
||||
if (v == null) return (self[key] = null);
|
||||
if (!(v instanceof Array))
|
||||
throw new Error(
|
||||
`ConfigureEntityParams Error: Expected type to be Array, but got ${typeof v}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeFn) {
|
||||
self[key] = typeFn(v);
|
||||
} else {
|
||||
self[key] = v;
|
||||
}
|
||||
}
|
||||
|
||||
export function toText(v: any) {
|
||||
if (isEmpty(v)) return null;
|
||||
return String(v);
|
||||
}
|
||||
|
||||
export function toNumber(v: any) {
|
||||
if (isEmpty(v)) return null;
|
||||
if (isNaN(Number(v))) return null;
|
||||
return Number(v);
|
||||
}
|
||||
|
||||
export function toBoolean(v: any) {
|
||||
if (isEmpty(v)) return null;
|
||||
return Boolean(v);
|
||||
}
|
||||
|
||||
export function isEmpty(v: any) {
|
||||
return v === null || v === undefined;
|
||||
}
|
||||
|
||||
export function supposeValue(v: any, def: any = null) {
|
||||
if (isEmpty(v)) return def;
|
||||
return v;
|
||||
}
|
@ -2,7 +2,7 @@ import { io, Socket, SocketOptions, ManagerOptions } from "socket.io-client";
|
||||
import { RemoteServiceConfig } from "./entity_interface";
|
||||
import { logger } from "../service/log";
|
||||
import RemoteRequest from "../service/remote_command";
|
||||
import InstanceStreamListener from "../common/instance_stream";
|
||||
import { InstanceStreamListener } from "common";
|
||||
import { $t, i18next } from "../i18n";
|
||||
|
||||
export default class RemoteService {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Koa from "koa";
|
||||
import GlobalVariable from "../common/global_variable";
|
||||
import { GlobalVariable } from "common";
|
||||
import userSystem from "../service/system_user";
|
||||
import { getUuidByApiKey, ILLEGAL_ACCESS_KEY, isAjax, logout } from "../service/passport_service";
|
||||
import { $t } from "../i18n";
|
||||
|
@ -7,7 +7,7 @@ import RemoteRequest from "../../service/remote_command";
|
||||
import os from "os";
|
||||
import { systemInfo } from "common";
|
||||
import { getVersion, specifiedDaemonVersion } from "../../version";
|
||||
import GlobalVariable from "../../common/global_variable";
|
||||
import { GlobalVariable } from "common";
|
||||
import {
|
||||
LOGIN_FAILED_KEY,
|
||||
ILLEGAL_ACCESS_KEY,
|
||||
|
@ -8,9 +8,7 @@ import { getUserUuid } from "../../service/passport_service";
|
||||
import { isHaveInstanceByUuid } from "../../service/permission_service";
|
||||
import { $t } from "../../i18n";
|
||||
import { isTopPermissionByUuid } from "../../service/permission_service";
|
||||
import { isEmpty, toText } from "../../../app/common/typecheck";
|
||||
import { toBoolean } from "../../../app/common/typecheck";
|
||||
import { toNumber } from "../../../app/common/typecheck";
|
||||
import { isEmpty, toText, toBoolean, toNumber } from "common";
|
||||
|
||||
const router = new Router({ prefix: "/protected_instance" });
|
||||
|
||||
|
@ -10,7 +10,7 @@ import userSystem from "../../service/system_user";
|
||||
import { logger } from "../../service/log";
|
||||
import { $t } from "../../i18n";
|
||||
import axios from "axios";
|
||||
import GlobalVariable from "../../common/global_variable";
|
||||
import { GlobalVariable } from "common";
|
||||
const router = new Router({ prefix: "/auth" });
|
||||
|
||||
// [Public Permission]
|
||||
|
@ -1,7 +1,7 @@
|
||||
import Koa from "koa";
|
||||
import userSystem from "./system_user";
|
||||
import { timeUuid } from "./password";
|
||||
import GlobalVariable from "../common/global_variable";
|
||||
import { GlobalVariable } from "common";
|
||||
import { systemConfig } from "../setting";
|
||||
import { logger } from "./log";
|
||||
import { User } from "../entity/user";
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
import SystemConfig from "./entity/setting";
|
||||
import StorageSystem from "./common/system_storage";
|
||||
import GlobalVariable from "./common/global_variable";
|
||||
import { GlobalVariable } from "common";
|
||||
import { $t, i18next } from "./i18n";
|
||||
let systemConfig: SystemConfig = null;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as fs from "fs-extra";
|
||||
import GlobalVariable from "./common/global_variable";
|
||||
import { GlobalVariable } from "common";
|
||||
import { logger } from "./service/log";
|
||||
import path from "path";
|
||||
import storage from "./common/system_storage";
|
||||
|
Loading…
x
Reference in New Issue
Block a user