set brace style to Stroustrup

This commit is contained in:
Arthur Lu 2023-06-29 22:09:57 +00:00
parent 1e8a4a8c29
commit bc1757c9c3
4 changed files with 51 additions and 22 deletions

View File

@ -10,8 +10,10 @@
}, },
"rules": { "rules": {
"no-tabs": [ "no-tabs": [
"error", "error",
{ "allowIndentationTabs": true } {
"allowIndentationTabs": true
}
], ],
"indent": [ "indent": [
"error", "error",
@ -28,6 +30,13 @@
"semi": [ "semi": [
"error", "error",
"always" "always"
],
"brace-style": [
"error",
"stroustrup",
{
"allowSingleLine": false
}
] ]
} }
} }

View File

@ -7,7 +7,8 @@ class LocalDB {
constructor () { constructor () {
try { try {
this.load(this.#filename); this.load(this.#filename);
} catch { }
catch {
console.log("Error: localdb.json was not found. Please follow the directions in the README to initialize localdb.json."); console.log("Error: localdb.json was not found. Please follow the directions in the README to initialize localdb.json.");
exit(1); exit(1);
} }
@ -32,7 +33,8 @@ class LocalDB {
getUserConfig (username) { getUserConfig (username) {
if (this.#data.users[username]) { if (this.#data.users[username]) {
return this.#data.users[username]; return this.#data.users[username];
} else { }
else {
return null; return null;
} }
} }

View File

@ -14,7 +14,8 @@ export async function requestPVE (path, method, cookies, body = null, token = nu
if (token) { if (token) {
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`; content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`;
} else if (cookies) { }
else if (cookies) {
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken; content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`; content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
} }
@ -26,7 +27,8 @@ export async function requestPVE (path, method, cookies, body = null, token = nu
try { try {
const response = await axios.request(url, content); const response = await axios.request(url, content);
return response; return response;
} catch (error) { }
catch (error) {
return error.response; return error.response;
} }
} }
@ -46,14 +48,16 @@ export async function handleResponse (node, result, res) {
result.log = taskLog.data.data; result.log = taskLog.data.data;
res.status(200).send(result); res.status(200).send(result);
res.end(); res.end();
} else { }
else {
const result = taskStatus.data.data; const result = taskStatus.data.data;
const taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", null, null, pveAPIToken); const taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", null, null, pveAPIToken);
result.log = taskLog.data.data; result.log = taskLog.data.data;
res.status(500).send(result); res.status(500).send(result);
res.end(); res.end();
} }
} else { }
else {
res.status(result.status).send(result.data); res.status(result.status).send(result.data);
res.end(); res.end();
} }
@ -69,9 +73,11 @@ export async function getUsedResources (req, resourceMeta) {
for (const diskPrefix of resourceMeta[resourceName].disks) { for (const diskPrefix of resourceMeta[resourceName].disks) {
diskprefixes.push(diskPrefix); diskprefixes.push(diskPrefix);
} }
} else if (resourceMeta[resourceName].type === "list") { }
else if (resourceMeta[resourceName].type === "list") {
used[resourceName] = []; used[resourceName] = [];
} else { }
else {
used[resourceName] = 0; used[resourceName] = 0;
} }
} }
@ -82,14 +88,17 @@ export async function getUsedResources (req, resourceMeta) {
for (const key of Object.keys(config)) { for (const key of Object.keys(config)) {
if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") { if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") {
used[key] += Number(config[key]); used[key] += Number(config[key]);
} else if (diskprefixes.some(prefix => key.startsWith(prefix))) { }
else if (diskprefixes.some(prefix => key.startsWith(prefix))) {
const diskInfo = await getDiskInfo(instance.node, instance.type, instance.vmid, key); const diskInfo = await getDiskInfo(instance.node, instance.type, instance.vmid, key);
if (diskInfo) { // only count if disk exists if (diskInfo) { // only count if disk exists
used[diskInfo.storage] += Number(diskInfo.size); used[diskInfo.storage] += Number(diskInfo.size);
} }
} else if (key.startsWith("net") && config[key].includes("rate=")) { // only count net instances with a rate limit }
else if (key.startsWith("net") && config[key].includes("rate=")) { // only count net instances with a rate limit
used.network += Number(config[key].split("rate=")[1].split(",")[0]); used.network += Number(config[key].split("rate=")[1].split(",")[0]);
} else if (key.startsWith("hostpci")) { }
else if (key.startsWith("hostpci")) {
const deviceInfo = await getDeviceInfo(instance.node, instance.type, instance.vmid, config[key].split(",")[0]); const deviceInfo = await getDeviceInfo(instance.node, instance.type, instance.vmid, config[key].split(",")[0]);
if (deviceInfo) { // only count if device exists if (deviceInfo) { // only count if device exists
used.pci.push(deviceInfo.device_name); used.pci.push(deviceInfo.device_name);
@ -109,7 +118,8 @@ export async function getDiskInfo (node, type, vmid, disk) {
const volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", null, null, pveAPIToken); const volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", null, null, pveAPIToken);
volInfo.data.data.storage = storageID; volInfo.data.data.storage = storageID;
return volInfo.data.data; return volInfo.data.data;
} catch { }
catch {
return null; return null;
} }
} }
@ -123,11 +133,14 @@ export async function getDeviceInfo (node, type, vmid, qid) {
deviceData.push(element); deviceData.push(element);
} }
}); });
deviceData.sort((a, b) => { return a.id < b.id; }); deviceData.sort((a, b) => {
return a.id < b.id;
});
const device = deviceData[0]; const device = deviceData[0];
device.subfn = structuredClone(deviceData.slice(1)); device.subfn = structuredClone(deviceData.slice(1));
return device; return device;
} catch { }
catch {
return null; return null;
} }
} }

View File

@ -14,7 +14,8 @@ export async function checkAuth (cookies, res, vmpath = null) {
if (vmpath) { if (vmpath) {
const result = await requestPVE(`/${vmpath}/config`, "GET", cookies); const result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
auth = result.status === 200; auth = result.status === 200;
} else { // if no path is specified, then do a simple authentication }
else { // if no path is specified, then do a simple authentication
const result = await requestPVE("/version", "GET", cookies); const result = await requestPVE("/version", "GET", cookies);
auth = result.status === 200; auth = result.status === 200;
} }
@ -38,7 +39,8 @@ export async function getUserResources (req, username) {
const index = avail[k].findIndex((maxElement) => usedDeviceName.includes(maxElement)); const index = avail[k].findIndex((maxElement) => usedDeviceName.includes(maxElement));
avail[k].splice(index, 1); avail[k].splice(index, 1);
}); });
} else { }
else {
avail[k] = max[k] - used[k]; avail[k] = max[k] - used[k];
} }
}); });
@ -53,14 +55,17 @@ export async function approveResources (req, username, request) {
Object.keys(request).forEach((key) => { Object.keys(request).forEach((key) => {
if (!(key in avail)) { // if requested resource is not in avail, block if (!(key in avail)) { // if requested resource is not in avail, block
approved = false; approved = false;
} else if (resources[key].type === "list") { }
else if (resources[key].type === "list") {
const inAvail = avail[key].some(availElem => request[key].includes(availElem)); const inAvail = avail[key].some(availElem => request[key].includes(availElem));
if (inAvail !== resources[key].whitelist) { if (inAvail !== resources[key].whitelist) {
approved = false; approved = false;
} }
} else if (isNaN(avail[key]) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block }
else if (isNaN(avail[key]) || isNaN(request[key])) { // if either the requested or avail resource is NaN, block
approved = false; approved = false;
} else if (avail[key] - request[key] < 0) { // if the avail resources is less than the requested resources, block }
else if (avail[key] - request[key] < 0) { // if the avail resources is less than the requested resources, block
approved = false; approved = false;
} }
}); });