melperjs 11.0.0 → 13.0.0
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 +2 -130
- package/lib/cjs/index.js +49 -26
- package/lib/cjs/node.js +51 -14
- package/lib/esm/index.js +47 -26
- package/lib/esm/node.js +48 -12
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -14,136 +14,8 @@ To install use npm:
|
|
|
14
14
|
npm i melperjs
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
##
|
|
18
|
-
|
|
19
|
-
```javascript
|
|
20
|
-
// const helper = require("melperjs");
|
|
21
|
-
// const nodeHelper = require("melperjs/node");
|
|
22
|
-
// const axios = require("axios");
|
|
23
|
-
import * as helper from "melperjs";
|
|
24
|
-
import * as nodeHelper from "melperjs/node";
|
|
25
|
-
import axios from "axios";
|
|
26
|
-
|
|
27
|
-
console.log(helper.CONSTANTS);
|
|
28
|
-
console.log(helper.time());
|
|
29
|
-
await helper.sleepMs(1000);
|
|
30
|
-
console.log(helper.time());
|
|
31
|
-
await helper.sleep(1);
|
|
32
|
-
console.log(helper.time());
|
|
33
|
-
try {
|
|
34
|
-
await helper.promiseTimeout(1000, helper.sleepMs(2000));
|
|
35
|
-
} catch (e) {
|
|
36
|
-
console.error(e.message);
|
|
37
|
-
console.log("Timeout, Internal Error ?", helper.isIntlHttpError(e));
|
|
38
|
-
}
|
|
39
|
-
const errorPronePromise = helper.retryFn(async () => {
|
|
40
|
-
console.log("Retry this function");
|
|
41
|
-
throw new Error("error")
|
|
42
|
-
}, 5, (attempt, error, result) => {
|
|
43
|
-
if (attempt % 2 === 0) {
|
|
44
|
-
console.error("Even attempt error");
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
helper.promiseSilent(errorPronePromise);
|
|
48
|
-
console.log("Is valid URL ?", helper.isValidURL("https://google.com"));
|
|
49
|
-
console.log(helper.splitClear(`
|
|
50
|
-
2.satır
|
|
51
|
-
|
|
52
|
-
4.satır
|
|
53
|
-
`))
|
|
54
|
-
console.log(helper.findKeyNode("c", {
|
|
55
|
-
a: {
|
|
56
|
-
b: {
|
|
57
|
-
x: 1,
|
|
58
|
-
y: 2,
|
|
59
|
-
c: {
|
|
60
|
-
d: true
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}));
|
|
65
|
-
console.log("'' empty ?", helper.checkEmpty(''));
|
|
66
|
-
console.log("1 empty ?", helper.checkEmpty(1));
|
|
67
|
-
console.log("0 empty ?", helper.checkEmpty(1));
|
|
68
|
-
console.log("[] empty ?", helper.checkEmpty([]));
|
|
69
|
-
console.log(helper.pascalCase("pascal case"));
|
|
70
|
-
console.log(helper.titleCase("THIS mUsT be Title"));
|
|
71
|
-
console.log(helper.parseNumFromObj({
|
|
72
|
-
a: "123",
|
|
73
|
-
b: 456,
|
|
74
|
-
c: "789.01",
|
|
75
|
-
d: "hello",
|
|
76
|
-
e: "0",
|
|
77
|
-
f: true,
|
|
78
|
-
g: "5000"
|
|
79
|
-
}));
|
|
80
|
-
console.log(helper.parseIntFromObj({
|
|
81
|
-
a: "123",
|
|
82
|
-
b: 456,
|
|
83
|
-
c: "789.01",
|
|
84
|
-
d: "hello",
|
|
85
|
-
e: "0",
|
|
86
|
-
f: true,
|
|
87
|
-
g: "5000"
|
|
88
|
-
}))
|
|
89
|
-
console.log(helper.objectStringify({
|
|
90
|
-
a: "hello",
|
|
91
|
-
b: 1,
|
|
92
|
-
c: undefined,
|
|
93
|
-
d: null,
|
|
94
|
-
e: {
|
|
95
|
-
ea: 2
|
|
96
|
-
},
|
|
97
|
-
f: [3, 4, 5],
|
|
98
|
-
g: false
|
|
99
|
-
}));
|
|
100
|
-
console.log(helper.modifyObjectKeys({"A": "B"}, (key) => key.toLowerCase()));
|
|
101
|
-
console.log(helper.limitString("LONG TEXT", 7));
|
|
102
|
-
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
103
|
-
console.log(helper.shuffleString("ABC123"));
|
|
104
|
-
console.log(helper.randomString(32, true, true));
|
|
105
|
-
console.log(helper.randomHex(8));
|
|
106
|
-
console.log(helper.randomInteger(100, 1000));
|
|
107
|
-
console.log(helper.randomUuid(true));
|
|
108
|
-
console.log(helper.randomWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
109
|
-
console.log(helper.randomElement({a: "vA", b: "vB", c: "vC"}));
|
|
110
|
-
console.log(nodeHelper.tokenString(32, true, true));
|
|
111
|
-
console.log(nodeHelper.tokenHex(8));
|
|
112
|
-
console.log(nodeHelper.tokenUuid(true));
|
|
113
|
-
console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
114
|
-
console.log(nodeHelper.tokenElement(["vA", "vB", "vC"]));
|
|
115
|
-
console.log(nodeHelper.hash("md5", "data"));
|
|
116
|
-
const password = nodeHelper.hashBcrypt("plain", "encryptionKey");
|
|
117
|
-
console.log(password)
|
|
118
|
-
console.log("passwordHash verified ?", nodeHelper.verifyBcrypt("plain", password, "encryptionKey"));
|
|
119
|
-
console.log(await nodeHelper.executeCommand("python --version"));
|
|
120
|
-
console.log(helper.indexByTime(5));
|
|
121
|
-
const cookies = helper.cookieDict(await axios.get("https://google.com"));
|
|
122
|
-
console.log(cookies);
|
|
123
|
-
console.log(helper.cookieHeader(cookies));
|
|
124
|
-
const proxy = nodeHelper.formatProxy("127.0.0.1:8080:id:pw-{SESSION}");
|
|
125
|
-
console.log(proxy);
|
|
126
|
-
console.log(nodeHelper.proxyObject(proxy));
|
|
127
|
-
console.log(nodeHelper.proxyValue(proxy));
|
|
128
|
-
console.log(nodeHelper.serverIp());
|
|
129
|
-
console.log("HTTP CODE: 400 (Bad Request) ?", helper.isIntlHttpCode(401));
|
|
130
|
-
console.log("HTTP CODE: 407 (Failed Proxy Auth) ?", helper.isIntlHttpCode(407));
|
|
131
|
-
nodeHelper.createNumDir("test");
|
|
132
|
-
await nodeHelper.writeJsonFile("test.json", {test: "test json file"});
|
|
133
|
-
console.log(nodeHelper.readJsonFileSync("test.json"));
|
|
134
|
-
console.log("VERSIONED BY .GIT", "v" + nodeHelper.getVersion());
|
|
135
|
-
console.log(helper.Exception("something went wrong", {status: 400}, "bad request error"));
|
|
136
|
-
await helper.forever(1000, async () => {
|
|
137
|
-
await helper.sleep(1);
|
|
138
|
-
console.log("Do something!")
|
|
139
|
-
}, (e) => {
|
|
140
|
-
console.error("Error!", e);
|
|
141
|
-
return 5000
|
|
142
|
-
}, () => {
|
|
143
|
-
console.log("Run this process forever!")
|
|
144
|
-
}
|
|
145
|
-
);
|
|
146
|
-
```
|
|
17
|
+
## Documentation
|
|
18
|
+
The documentation is available at [docs folder](docs/index.md).
|
|
147
19
|
|
|
148
20
|
## License
|
|
149
21
|
|
package/lib/cjs/index.js
CHANGED
|
@@ -9,6 +9,7 @@ exports.checkEmpty = checkEmpty;
|
|
|
9
9
|
exports.cookieDict = cookieDict;
|
|
10
10
|
exports.cookieHeader = cookieHeader;
|
|
11
11
|
exports.findKeyNode = findKeyNode;
|
|
12
|
+
exports.flipObject = flipObject;
|
|
12
13
|
exports.forever = forever;
|
|
13
14
|
exports.indexByTime = indexByTime;
|
|
14
15
|
exports.isIntlHttpCode = isIntlHttpCode;
|
|
@@ -36,6 +37,7 @@ exports.sleepMs = sleepMs;
|
|
|
36
37
|
exports.splitClear = splitClear;
|
|
37
38
|
exports.time = time;
|
|
38
39
|
exports.titleCase = titleCase;
|
|
40
|
+
exports.waitForProperty = waitForProperty;
|
|
39
41
|
var _xss = _interopRequireDefault(require("xss"));
|
|
40
42
|
var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
|
|
41
43
|
var _camelCase = _interopRequireDefault(require("lodash/camelCase.js"));
|
|
@@ -47,18 +49,18 @@ const CONSTANTS = exports.CONSTANTS = {
|
|
|
47
49
|
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
48
50
|
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
49
51
|
HEXADECIMAL: "0123456789abcdef",
|
|
50
|
-
NUMBERS: "0123456789"
|
|
52
|
+
NUMBERS: "0123456789",
|
|
53
|
+
INT32_MIN: -2147483648,
|
|
54
|
+
INT32_MAX: 2147483647
|
|
51
55
|
};
|
|
52
56
|
function Exception(message, response = {}, name = null) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.name = name ? pascalCase(name) : "Exception";
|
|
59
|
-
}
|
|
57
|
+
const error = new Error(message);
|
|
58
|
+
error.name = name || "Exception";
|
|
59
|
+
error.response = response;
|
|
60
|
+
if (!error.response?.status && typeof error.response === "object") {
|
|
61
|
+
error.response.status = 400;
|
|
60
62
|
}
|
|
61
|
-
return
|
|
63
|
+
return error;
|
|
62
64
|
}
|
|
63
65
|
async function forever(cooldown, onSuccess, onError = null, onCompleted = null) {
|
|
64
66
|
const checkCooldown = value => value && !isNaN(value) && value > 0;
|
|
@@ -110,7 +112,7 @@ async function retryFn(fn, retries, errorFn = null) {
|
|
|
110
112
|
result = await fn();
|
|
111
113
|
return result;
|
|
112
114
|
} catch (error) {
|
|
113
|
-
errorFn
|
|
115
|
+
errorFn && (await errorFn(attempt, error, result));
|
|
114
116
|
if (attempt >= retries) {
|
|
115
117
|
throw error;
|
|
116
118
|
}
|
|
@@ -129,19 +131,6 @@ function splitClear(rawText, separator = null) {
|
|
|
129
131
|
separator = separator ?? /\r?\n/;
|
|
130
132
|
return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
|
|
131
133
|
}
|
|
132
|
-
function findKeyNode(key, node, pair = null) {
|
|
133
|
-
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
134
|
-
return node;
|
|
135
|
-
} else if (typeof node === 'object') {
|
|
136
|
-
for (let index in node) {
|
|
137
|
-
const result = findKeyNode(key, node[index], pair);
|
|
138
|
-
if (result) {
|
|
139
|
-
return result;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
134
|
function checkEmpty(value) {
|
|
146
135
|
if (typeof value === "number") {
|
|
147
136
|
return value === 0;
|
|
@@ -152,15 +141,19 @@ function checkEmpty(value) {
|
|
|
152
141
|
function pascalCase(str) {
|
|
153
142
|
return (0, _upperFirst.default)((0, _camelCase.default)(str));
|
|
154
143
|
}
|
|
155
|
-
function titleCase(str) {
|
|
144
|
+
function titleCase(str, separator = " ") {
|
|
156
145
|
str = str || "";
|
|
157
|
-
|
|
146
|
+
const words = str.split(separator);
|
|
147
|
+
return words.map(word => (0, _upperFirst.default)(word)).join(separator);
|
|
148
|
+
}
|
|
149
|
+
function isInt32(value) {
|
|
150
|
+
return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
|
|
158
151
|
}
|
|
159
152
|
function parseNumFromObj(obj) {
|
|
160
153
|
for (let key in obj) {
|
|
161
154
|
let value = obj[key];
|
|
162
155
|
let number = parseFloat(value);
|
|
163
|
-
if (typeof value === 'string' && !isNaN(number)) {
|
|
156
|
+
if (typeof value === 'string' && !isNaN(number) && !value.includes("_")) {
|
|
164
157
|
value = number;
|
|
165
158
|
}
|
|
166
159
|
obj[key] = value;
|
|
@@ -178,6 +171,36 @@ function parseIntFromObj(obj) {
|
|
|
178
171
|
}
|
|
179
172
|
return obj;
|
|
180
173
|
}
|
|
174
|
+
function findKeyNode(key, node, pair = null) {
|
|
175
|
+
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
176
|
+
return node;
|
|
177
|
+
} else if (typeof node === 'object') {
|
|
178
|
+
for (let index in node) {
|
|
179
|
+
const result = findKeyNode(key, node[index], pair);
|
|
180
|
+
if (result) {
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
function flipObject(obj) {
|
|
188
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
189
|
+
}
|
|
190
|
+
function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
const startTime = Date.now();
|
|
193
|
+
const checkProperty = setInterval(() => {
|
|
194
|
+
if (obj.hasOwnProperty(propertyName)) {
|
|
195
|
+
clearInterval(checkProperty);
|
|
196
|
+
resolve();
|
|
197
|
+
} else if (Date.now() - startTime > timeout) {
|
|
198
|
+
clearInterval(checkProperty);
|
|
199
|
+
reject(new Error(`Property ${propertyName} did not appear within ${timeout} milliseconds`));
|
|
200
|
+
}
|
|
201
|
+
}, interval);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
181
204
|
function objectStringify(obj) {
|
|
182
205
|
for (let key in obj) {
|
|
183
206
|
if (obj.hasOwnProperty(key)) {
|
package/lib/cjs/node.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.cleanDirectory = cleanDirectory;
|
|
6
7
|
exports.createNumDir = createNumDir;
|
|
7
8
|
exports.executeCommand = executeCommand;
|
|
8
9
|
exports.formatProxy = formatProxy;
|
|
@@ -18,6 +19,7 @@ exports.serverIp = serverIp;
|
|
|
18
19
|
exports.sha256 = sha256;
|
|
19
20
|
exports.tokenElement = tokenElement;
|
|
20
21
|
exports.tokenHex = tokenHex;
|
|
22
|
+
exports.tokenInteger = tokenInteger;
|
|
21
23
|
exports.tokenString = tokenString;
|
|
22
24
|
exports.tokenUuid = tokenUuid;
|
|
23
25
|
exports.tokenWeighted = tokenWeighted;
|
|
@@ -32,8 +34,7 @@ var _child_process = require("child_process");
|
|
|
32
34
|
var _bcryptjs = _interopRequireDefault(require("bcryptjs"));
|
|
33
35
|
var _index = require("./index.js");
|
|
34
36
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
35
|
-
function
|
|
36
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
37
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
37
38
|
function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
38
39
|
const lowercaseChars = _index.CONSTANTS.LOWER_CASE;
|
|
39
40
|
const uppercaseChars = _index.CONSTANTS.UPPER_CASE;
|
|
@@ -54,6 +55,9 @@ function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
|
54
55
|
function tokenHex(length) {
|
|
55
56
|
return _crypto.default.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
56
57
|
}
|
|
58
|
+
function tokenInteger(min, max) {
|
|
59
|
+
return _crypto.default.randomInt(min, max);
|
|
60
|
+
}
|
|
57
61
|
function tokenUuid(useDashes = true) {
|
|
58
62
|
const uuid = _crypto.default.randomUUID().toString();
|
|
59
63
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
@@ -94,23 +98,27 @@ function serverIp() {
|
|
|
94
98
|
}
|
|
95
99
|
function getVersion() {
|
|
96
100
|
try {
|
|
97
|
-
const
|
|
101
|
+
const timestamp = parseInt((0, _child_process.execSync)('git show -s --format=%ct HEAD').toString().trim());
|
|
102
|
+
if (isNaN(timestamp)) {
|
|
103
|
+
return "1.0";
|
|
104
|
+
}
|
|
105
|
+
const date = new Date(timestamp * 1000);
|
|
98
106
|
const formatDatePart = value => value.toString().padStart(2, '0');
|
|
99
|
-
const year = date.
|
|
100
|
-
const month = formatDatePart(date.
|
|
101
|
-
const day = formatDatePart(date.
|
|
102
|
-
const hour = formatDatePart(date.
|
|
103
|
-
const minute = formatDatePart(date.
|
|
107
|
+
const year = date.getUTCFullYear().toString().slice(-2);
|
|
108
|
+
const month = formatDatePart(date.getUTCMonth() + 1);
|
|
109
|
+
const day = formatDatePart(date.getUTCDate());
|
|
110
|
+
const hour = formatDatePart(date.getUTCHours());
|
|
111
|
+
const minute = formatDatePart(date.getUTCMinutes());
|
|
104
112
|
return `${year}${month}${day}.${hour}${minute}`;
|
|
105
113
|
} catch {
|
|
106
114
|
return "1.0";
|
|
107
115
|
}
|
|
108
116
|
}
|
|
109
|
-
function createNumDir(mainDirectory) {
|
|
117
|
+
function createNumDir(mainDirectory, start = 0, end = 9) {
|
|
110
118
|
_fs.default.mkdirSync(mainDirectory, {
|
|
111
119
|
recursive: true
|
|
112
120
|
});
|
|
113
|
-
for (let i =
|
|
121
|
+
for (let i = start; i <= end; i++) {
|
|
114
122
|
try {
|
|
115
123
|
_fs.default.mkdirSync(_path.default.join(mainDirectory, i.toString()), {
|
|
116
124
|
recursive: true
|
|
@@ -187,20 +195,20 @@ function proxyValue(proxies) {
|
|
|
187
195
|
proxy = proxy.replace("{SESSION}", tokenHex(8));
|
|
188
196
|
return proxy || null;
|
|
189
197
|
}
|
|
190
|
-
async function readJsonFile(filePath) {
|
|
198
|
+
async function readJsonFile(filePath, defaultValue = {}) {
|
|
191
199
|
try {
|
|
192
200
|
const data = await _fs.promises.readFile(filePath, 'utf8');
|
|
193
201
|
return JSON.parse(data);
|
|
194
202
|
} catch (error) {
|
|
195
|
-
return
|
|
203
|
+
return defaultValue;
|
|
196
204
|
}
|
|
197
205
|
}
|
|
198
|
-
function readJsonFileSync(filePath) {
|
|
206
|
+
function readJsonFileSync(filePath, defaultValue = {}) {
|
|
199
207
|
try {
|
|
200
208
|
const data = _fs.default.readFileSync(filePath, 'utf8');
|
|
201
209
|
return JSON.parse(data);
|
|
202
210
|
} catch (error) {
|
|
203
|
-
return
|
|
211
|
+
return defaultValue;
|
|
204
212
|
}
|
|
205
213
|
}
|
|
206
214
|
async function writeJsonFile(filePath, data) {
|
|
@@ -210,4 +218,33 @@ async function writeJsonFile(filePath, data) {
|
|
|
210
218
|
function writeJsonFileSync(filePath, data) {
|
|
211
219
|
const jsonData = JSON.stringify(data);
|
|
212
220
|
return _fs.default.writeFileSync(filePath, jsonData, 'utf8');
|
|
221
|
+
}
|
|
222
|
+
async function cleanDirectory(directoryPath, keepDir = true) {
|
|
223
|
+
try {
|
|
224
|
+
const stats = await _fs.promises.stat(directoryPath).catch(() => null);
|
|
225
|
+
if (!stats) {
|
|
226
|
+
if (keepDir) {
|
|
227
|
+
await _fs.promises.mkdir(directoryPath, {
|
|
228
|
+
recursive: true
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const files = await _fs.promises.readdir(directoryPath);
|
|
234
|
+
for (const file of files) {
|
|
235
|
+
const filePath = _path.default.join(directoryPath, file);
|
|
236
|
+
const fileStat = await _fs.promises.stat(filePath);
|
|
237
|
+
if (fileStat.isDirectory()) {
|
|
238
|
+
await cleanDirectory(filePath, false);
|
|
239
|
+
} else {
|
|
240
|
+
await _fs.promises.unlink(filePath);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (!keepDir) {
|
|
244
|
+
await _fs.promises.rmdir(directoryPath);
|
|
245
|
+
}
|
|
246
|
+
return true;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
213
250
|
}
|
package/lib/esm/index.js
CHANGED
|
@@ -8,18 +8,18 @@ export const CONSTANTS = {
|
|
|
8
8
|
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
9
9
|
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
10
10
|
HEXADECIMAL: "0123456789abcdef",
|
|
11
|
-
NUMBERS: "0123456789"
|
|
11
|
+
NUMBERS: "0123456789",
|
|
12
|
+
INT32_MIN: -2147483648,
|
|
13
|
+
INT32_MAX: 2147483647
|
|
12
14
|
};
|
|
13
15
|
export function Exception(message, response = {}, name = null) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.name = name ? pascalCase(name) : "Exception";
|
|
20
|
-
}
|
|
16
|
+
const error = new Error(message);
|
|
17
|
+
error.name = name || "Exception";
|
|
18
|
+
error.response = response;
|
|
19
|
+
if (!error.response?.status && typeof error.response === "object") {
|
|
20
|
+
error.response.status = 400;
|
|
21
21
|
}
|
|
22
|
-
return
|
|
22
|
+
return error;
|
|
23
23
|
}
|
|
24
24
|
export async function forever(cooldown, onSuccess, onError = null, onCompleted = null) {
|
|
25
25
|
const checkCooldown = value => value && !isNaN(value) && value > 0;
|
|
@@ -71,7 +71,7 @@ export async function retryFn(fn, retries, errorFn = null) {
|
|
|
71
71
|
result = await fn();
|
|
72
72
|
return result;
|
|
73
73
|
} catch (error) {
|
|
74
|
-
errorFn
|
|
74
|
+
errorFn && (await errorFn(attempt, error, result));
|
|
75
75
|
if (attempt >= retries) {
|
|
76
76
|
throw error;
|
|
77
77
|
}
|
|
@@ -90,19 +90,6 @@ export function splitClear(rawText, separator = null) {
|
|
|
90
90
|
separator = separator ?? /\r?\n/;
|
|
91
91
|
return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
|
|
92
92
|
}
|
|
93
|
-
export function findKeyNode(key, node, pair = null) {
|
|
94
|
-
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
95
|
-
return node;
|
|
96
|
-
} else if (typeof node === 'object') {
|
|
97
|
-
for (let index in node) {
|
|
98
|
-
const result = findKeyNode(key, node[index], pair);
|
|
99
|
-
if (result) {
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
93
|
export function checkEmpty(value) {
|
|
107
94
|
if (typeof value === "number") {
|
|
108
95
|
return value === 0;
|
|
@@ -113,15 +100,19 @@ export function checkEmpty(value) {
|
|
|
113
100
|
export function pascalCase(str) {
|
|
114
101
|
return upperFirst(camelCase(str));
|
|
115
102
|
}
|
|
116
|
-
export function titleCase(str) {
|
|
103
|
+
export function titleCase(str, separator = " ") {
|
|
117
104
|
str = str || "";
|
|
118
|
-
|
|
105
|
+
const words = str.split(separator);
|
|
106
|
+
return words.map(word => upperFirst(word)).join(separator);
|
|
107
|
+
}
|
|
108
|
+
function isInt32(value) {
|
|
109
|
+
return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
|
|
119
110
|
}
|
|
120
111
|
export function parseNumFromObj(obj) {
|
|
121
112
|
for (let key in obj) {
|
|
122
113
|
let value = obj[key];
|
|
123
114
|
let number = parseFloat(value);
|
|
124
|
-
if (typeof value === 'string' && !isNaN(number)) {
|
|
115
|
+
if (typeof value === 'string' && !isNaN(number) && !value.includes("_")) {
|
|
125
116
|
value = number;
|
|
126
117
|
}
|
|
127
118
|
obj[key] = value;
|
|
@@ -139,6 +130,36 @@ export function parseIntFromObj(obj) {
|
|
|
139
130
|
}
|
|
140
131
|
return obj;
|
|
141
132
|
}
|
|
133
|
+
export function findKeyNode(key, node, pair = null) {
|
|
134
|
+
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
135
|
+
return node;
|
|
136
|
+
} else if (typeof node === 'object') {
|
|
137
|
+
for (let index in node) {
|
|
138
|
+
const result = findKeyNode(key, node[index], pair);
|
|
139
|
+
if (result) {
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
export function flipObject(obj) {
|
|
147
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
148
|
+
}
|
|
149
|
+
export function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const startTime = Date.now();
|
|
152
|
+
const checkProperty = setInterval(() => {
|
|
153
|
+
if (obj.hasOwnProperty(propertyName)) {
|
|
154
|
+
clearInterval(checkProperty);
|
|
155
|
+
resolve();
|
|
156
|
+
} else if (Date.now() - startTime > timeout) {
|
|
157
|
+
clearInterval(checkProperty);
|
|
158
|
+
reject(new Error(`Property ${propertyName} did not appear within ${timeout} milliseconds`));
|
|
159
|
+
}
|
|
160
|
+
}, interval);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
142
163
|
export function objectStringify(obj) {
|
|
143
164
|
for (let key in obj) {
|
|
144
165
|
if (obj.hasOwnProperty(key)) {
|
package/lib/esm/node.js
CHANGED
|
@@ -26,6 +26,9 @@ export function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
|
26
26
|
export function tokenHex(length) {
|
|
27
27
|
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
28
28
|
}
|
|
29
|
+
export function tokenInteger(min, max) {
|
|
30
|
+
return crypto.randomInt(min, max);
|
|
31
|
+
}
|
|
29
32
|
export function tokenUuid(useDashes = true) {
|
|
30
33
|
const uuid = crypto.randomUUID().toString();
|
|
31
34
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
@@ -66,23 +69,27 @@ export function serverIp() {
|
|
|
66
69
|
}
|
|
67
70
|
export function getVersion() {
|
|
68
71
|
try {
|
|
69
|
-
const
|
|
72
|
+
const timestamp = parseInt(execSync('git show -s --format=%ct HEAD').toString().trim());
|
|
73
|
+
if (isNaN(timestamp)) {
|
|
74
|
+
return "1.0";
|
|
75
|
+
}
|
|
76
|
+
const date = new Date(timestamp * 1000);
|
|
70
77
|
const formatDatePart = value => value.toString().padStart(2, '0');
|
|
71
|
-
const year = date.
|
|
72
|
-
const month = formatDatePart(date.
|
|
73
|
-
const day = formatDatePart(date.
|
|
74
|
-
const hour = formatDatePart(date.
|
|
75
|
-
const minute = formatDatePart(date.
|
|
78
|
+
const year = date.getUTCFullYear().toString().slice(-2);
|
|
79
|
+
const month = formatDatePart(date.getUTCMonth() + 1);
|
|
80
|
+
const day = formatDatePart(date.getUTCDate());
|
|
81
|
+
const hour = formatDatePart(date.getUTCHours());
|
|
82
|
+
const minute = formatDatePart(date.getUTCMinutes());
|
|
76
83
|
return `${year}${month}${day}.${hour}${minute}`;
|
|
77
84
|
} catch {
|
|
78
85
|
return "1.0";
|
|
79
86
|
}
|
|
80
87
|
}
|
|
81
|
-
export function createNumDir(mainDirectory) {
|
|
88
|
+
export function createNumDir(mainDirectory, start = 0, end = 9) {
|
|
82
89
|
fs.mkdirSync(mainDirectory, {
|
|
83
90
|
recursive: true
|
|
84
91
|
});
|
|
85
|
-
for (let i =
|
|
92
|
+
for (let i = start; i <= end; i++) {
|
|
86
93
|
try {
|
|
87
94
|
fs.mkdirSync(path.join(mainDirectory, i.toString()), {
|
|
88
95
|
recursive: true
|
|
@@ -159,20 +166,20 @@ export function proxyValue(proxies) {
|
|
|
159
166
|
proxy = proxy.replace("{SESSION}", tokenHex(8));
|
|
160
167
|
return proxy || null;
|
|
161
168
|
}
|
|
162
|
-
export async function readJsonFile(filePath) {
|
|
169
|
+
export async function readJsonFile(filePath, defaultValue = {}) {
|
|
163
170
|
try {
|
|
164
171
|
const data = await fsp.readFile(filePath, 'utf8');
|
|
165
172
|
return JSON.parse(data);
|
|
166
173
|
} catch (error) {
|
|
167
|
-
return
|
|
174
|
+
return defaultValue;
|
|
168
175
|
}
|
|
169
176
|
}
|
|
170
|
-
export function readJsonFileSync(filePath) {
|
|
177
|
+
export function readJsonFileSync(filePath, defaultValue = {}) {
|
|
171
178
|
try {
|
|
172
179
|
const data = fs.readFileSync(filePath, 'utf8');
|
|
173
180
|
return JSON.parse(data);
|
|
174
181
|
} catch (error) {
|
|
175
|
-
return
|
|
182
|
+
return defaultValue;
|
|
176
183
|
}
|
|
177
184
|
}
|
|
178
185
|
export async function writeJsonFile(filePath, data) {
|
|
@@ -182,4 +189,33 @@ export async function writeJsonFile(filePath, data) {
|
|
|
182
189
|
export function writeJsonFileSync(filePath, data) {
|
|
183
190
|
const jsonData = JSON.stringify(data);
|
|
184
191
|
return fs.writeFileSync(filePath, jsonData, 'utf8');
|
|
192
|
+
}
|
|
193
|
+
export async function cleanDirectory(directoryPath, keepDir = true) {
|
|
194
|
+
try {
|
|
195
|
+
const stats = await fsp.stat(directoryPath).catch(() => null);
|
|
196
|
+
if (!stats) {
|
|
197
|
+
if (keepDir) {
|
|
198
|
+
await fsp.mkdir(directoryPath, {
|
|
199
|
+
recursive: true
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const files = await fsp.readdir(directoryPath);
|
|
205
|
+
for (const file of files) {
|
|
206
|
+
const filePath = path.join(directoryPath, file);
|
|
207
|
+
const fileStat = await fsp.stat(filePath);
|
|
208
|
+
if (fileStat.isDirectory()) {
|
|
209
|
+
await cleanDirectory(filePath, false);
|
|
210
|
+
} else {
|
|
211
|
+
await fsp.unlink(filePath);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (!keepDir) {
|
|
215
|
+
await fsp.rmdir(directoryPath);
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
218
|
+
} catch (error) {
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
185
221
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melperjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.0.0",
|
|
4
4
|
"description": "Javascript module to use predefined common functions and utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"melperjs",
|
|
@@ -39,17 +39,17 @@
|
|
|
39
39
|
"test": "node test/script.js"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"bcryptjs": "^
|
|
42
|
+
"bcryptjs": "^3.0.2",
|
|
43
43
|
"hpagent": "^1.2.0",
|
|
44
44
|
"lodash": "^4.17.21",
|
|
45
|
-
"set-cookie-parser": "^2.
|
|
45
|
+
"set-cookie-parser": "^2.7.1",
|
|
46
46
|
"xss": "^1.0.15"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@babel/cli": "^7.
|
|
50
|
-
"@babel/core": "^7.
|
|
51
|
-
"@babel/preset-env": "^7.
|
|
52
|
-
"axios": "^1.
|
|
49
|
+
"@babel/cli": "^7.27.2",
|
|
50
|
+
"@babel/core": "^7.27.1",
|
|
51
|
+
"@babel/preset-env": "^7.27.2",
|
|
52
|
+
"axios": "^1.9.0",
|
|
53
53
|
"cross-env": "^7.0.3"
|
|
54
54
|
}
|
|
55
55
|
}
|