@nka212bg/backend-utils 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
File without changes
package/db/dbUtils.js CHANGED
File without changes
package/db/mysql2.js CHANGED
File without changes
package/db/sqlite3.js CHANGED
File without changes
package/index.js CHANGED
File without changes
File without changes
File without changes
package/location/index.js CHANGED
File without changes
File without changes
File without changes
File without changes
package/markdown/index.js CHANGED
File without changes
File without changes
package/misc.js CHANGED
File without changes
package/os.js CHANGED
@@ -29,25 +29,42 @@ exports.rebootServer = ({ password } = {}) => {
29
29
  }
30
30
  };
31
31
 
32
- exports.backupSystem = async ({ basePath, archiveName, overrideExisting, items } = {}) => {
32
+ exports.backupSystem = async ({ basePath, archiveName, override, items, count = 5 } = {}) => {
33
33
  try {
34
34
  const startTime = Date.now();
35
35
 
36
36
  basePath = basePath || `${__basedir}/backups`;
37
- archiveName = archiveName || new Date().toISOString().split("T")[0];
38
- const fullPath = `${basePath}/${archiveName}`;
39
37
  !existsSync(basePath) && mkdirSync(basePath, { recursive: true });
40
-
38
+
39
+
41
40
  const availableBackups = readdirSync(basePath);
42
- if (availableBackups.length > 2) rmSync(`${basePath}/${availableBackups[0]}`, { recursive: true, force: true });
41
+ if (availableBackups.length > parseInt(count)) {
42
+ let oldestFile = null;
43
+ let oldestTime = Infinity;
44
+
45
+ availableBackups.forEach(file => {
46
+ file = `${basePath}/${file}`;
47
+ const stats = lstatSync(file);
48
+
49
+ if (stats.isFile() && stats.birthtimeMs < oldestTime) {
50
+ oldestTime = stats.birthtimeMs;
51
+ oldestFile = file;
52
+ }
53
+ });
54
+ rmSync(oldestFile, { recursive: true, force: true });
55
+ }
43
56
 
44
- if (!overrideExisting && existsSync(`${fullPath}.zip`)) return console.log("backupSystem info, file awready exists --> ", `${fullPath}.zip`);
57
+ archiveName = archiveName || new Date().toISOString().split("T")[0];
58
+ const fullPath = `${basePath}/${archiveName}`;
59
+
60
+ if (!override && existsSync(`${fullPath}.zip`)) return console.log("backupSystem info, file awready exists --> ", `${fullPath}.zip`);
45
61
 
46
62
  if (!items) throw "No items";
47
63
  if (typeof items == "string") items = [items];
48
64
 
49
65
  const zip = new AdmZip();
50
- items.forEach((e) => (lstatSync(e).isFile() ? zip.addLocalFile(e) : zip.addLocalFolder(e)));
66
+ items.forEach((e) => lstatSync(e).isFile() ? zip.addLocalFile(e) : zip.addLocalFolder(e, e.split('/').pop()));
67
+
51
68
  zip.writeZip(`${fullPath}.zip`);
52
69
 
53
70
  console.info(`System backup --> time: ${parseInt((Date.now() - startTime) / 1000)}`);
@@ -61,6 +78,7 @@ exports.osStatus = () => {
61
78
 
62
79
  try {
63
80
  return {
81
+ serverStart,
64
82
  serverUptime: secToTime((now - serverStart) / 1000),
65
83
  machineUptime: secToTime(os.uptime()),
66
84
  memoryUsage: shortCount(os.totalmem() - os.freemem(), "disk"),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.19",
4
+ "version": "0.1.21",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "@maxmind/geoip2-node": "^5.0.0",
package/session.js CHANGED
File without changes
package/socket.js CHANGED
@@ -1,40 +1,29 @@
1
1
  const WebSocket = require("ws");
2
- let http = null;
3
- let https = null;
4
2
  let socketsCount = 0;
3
+ let sockets = [];
5
4
 
6
- exports.socket = ({ httpServer, httpsServer }) => {
7
- if (httpServer) {
8
- http = new WebSocket.Server({ server: httpServer });
9
- http.on("connection", (ws, req) => {
10
- ws.params = JSON.parse(`{"${decodeURI(req.url.replace(/\/\?/g, "")).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);
11
- });
12
- } else if (httpsServer) {
13
- https = new WebSocket.Server({ server: httpsServer });
14
- https.on("connection", (ws, req) => {
15
- ws.params = JSON.parse(`{"${decodeURI(req.url.replace(/\/\?/g, "")).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);
16
- });
17
- }
5
+ exports.socket = (server) => {
6
+ const sl = sockets.length;
7
+ sockets[sl] = new WebSocket.Server({ server });
8
+ sockets[sl].on("connection", (ws, req) => {
9
+ ws.params = JSON.parse(`{"${decodeURI(req.url.replace(/\/\?/g, "")).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);
10
+ });
18
11
  };
19
12
 
20
13
  exports.getUsersStatuses = (users) => {
21
14
  if (typeof users == "string") users = users.split(/[, ]/g);
22
- if (!Array.isArray(users) || !https?.clients.length) return {};
15
+ if (!Array.isArray(users) || !sockets.length) return {};
23
16
 
24
17
  const usersObj = {};
25
18
  users.forEach((u) => {
26
19
  usersObj[u] = { online: false };
27
- for (const c of https.clients) {
28
- if (c.params.userId == u) {
29
- usersObj[u] = { online: true };
30
- return;
31
- }
32
- }
33
20
 
34
- for (const c of http.clients) {
35
- if (c.params.userId == u) {
36
- usersObj[u] = { online: true };
37
- return;
21
+ for (const socket of sockets) {
22
+ for (const client of socket.clients) {
23
+ if (client.params.userId == u) {
24
+ usersObj[u] = { online: true };
25
+ return;
26
+ }
38
27
  }
39
28
  }
40
29
  });
@@ -42,34 +31,11 @@ exports.getUsersStatuses = (users) => {
42
31
  return usersObj;
43
32
  };
44
33
 
45
- // exports.getUsersStatuses = (users = []) => {
46
- // users = users.map((u) => {
47
- // u = { id: u, online: false };
48
- // for (const c of https.clients) {
49
- // if (c.params.userId == u.id) {
50
- // u.online = true;
51
- // return u;
52
- // }
53
- // }
54
-
55
- // for (const c of http.clients) {
56
- // if (c.params.userId == u.id) {
57
- // u.online = true;
58
- // return u;
59
- // }
60
- // }
61
-
62
- // return u;
63
- // });
64
-
65
- // return users;
66
- // };
67
-
68
34
  exports.sendSocketMessage = ({ userId, skipId, skipToken, data, broadcast } = {}) => {
69
35
  let sentCount = 0;
70
36
  let tempCount = 0;
71
37
 
72
- if (!userId) return;
38
+ if ((!userId && !broadcast) || !sockets.length) return;
73
39
  userId = !Array.isArray(userId) ? [String(userId)] : userId.map((e) => String(e));
74
40
 
75
41
  if (skipId) skipId = !Array.isArray(skipId) ? [String(skipId)] : skipId.map((e) => String(e));
@@ -79,8 +45,9 @@ exports.sendSocketMessage = ({ userId, skipId, skipToken, data, broadcast } = {}
79
45
  if (typeof data != "string") data = JSON.stringify(data);
80
46
  } catch {}
81
47
 
82
- (https.clients || []).forEach(manageClient);
83
- (http.clients || []).forEach(manageClient);
48
+ sockets.forEach((socket) => {
49
+ (socket.clients || []).forEach(manageClient);
50
+ });
84
51
 
85
52
  socketsCount = tempCount;
86
53
  return sentCount;
package/stripe.js CHANGED
File without changes
package/systemLog.js CHANGED
File without changes