-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgyutil.js
More file actions
127 lines (99 loc) · 2.34 KB
/
Copy pathgyutil.js
File metadata and controls
127 lines (99 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
const fs = require('fs');
const Durmsec = {
sec : 1000,
min : (60 * 1000),
hour : (60 * 60 * 1000),
day : (24 * 60 * 60 * 1000),
week : (7 * 24 * 60 * 60 * 1000),
month : (30 * 24 * 60 * 60 * 1000),
year : (365 * 24 * 60 * 60 * 1000),
};
function getRandomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomFloat(min, max, ndecimal = 2)
{
let num = Math.random() * (max - min + 1) + min;
return Number(num.toFixed(ndecimal));
}
function delayExec(millisec)
{
return new Promise(resolve => setTimeout(resolve, millisec));
}
function safetypeof(val, arrayAsObject = false)
{
if (val === undefined) {
return 'undefined';
}
if (val === null) {
return 'null';
}
if (false === arrayAsObject && Array.isArray(val)) {
return 'array';
}
return typeof val;
}
function isEmptyObj(obj)
{
for (let x in obj) {
if (Object.prototype.hasOwnProperty.call(obj, x)) {
return false;
}
}
return true;
}
function splitAndTrim(strin, separator = ',')
{
return strin.split(separator).map((str) => str.trim()).filter((str) => str.length > 0);
}
function escapeHtml(unsafestr)
{
return unsafestr.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
function printResourceUsage(prefix = '')
{
const memuse = process.memoryUsage(), upsec = process.uptime();
const updays = upsec/(24 * 3600) | 0, uphrs = (upsec % (24 * 3600))/3600 | 0;
console.log(`Process ${prefix} PID ${process.pid} Memory Stats : Resident Memory RSS ${memuse.rss >> 20} MB, Heap Used ${memuse.heapUsed >> 20} MB, Process Uptime ${updays} day(s) ${uphrs} hour(s)\n`);
}
function logrotate(logfile, maxfilesz = 30 * 1024 * 1024)
{
try {
if (!logfile) {
return;
}
const stat = fs.statSync(logfile, { throwIfNoEntry : false });
if (!stat) {
return;
}
// console.log('Log file ', logfile, ' size is ', stat.size);
if (stat.size > maxfilesz) {
fs.copyFile(logfile, logfile + '.bak', (err) => {
try {
if (err) {
// Copy failed
}
fs.truncateSync(logfile, 0);
}
catch(error) {
}
});
}
}
catch (e) {
}
}
module.exports = {
Durmsec,
getRandomInt,
getRandomFloat,
delayExec,
safetypeof,
isEmptyObj,
splitAndTrim,
escapeHtml,
printResourceUsage,
logrotate,
};