melperjs 8.1.0 → 9.1.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 +28 -72
- package/lib/cjs/index.js +39 -2
- package/lib/cjs/node.js +42 -4
- package/lib/esm/index.js +34 -1
- package/lib/esm/node.js +34 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
|
|
2
|
+
[](https://badge.fury.io/js/melperjs)
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
1
5
|
# MELPERJS
|
|
2
6
|
|
|
3
7
|
Javascript module to use predefined common functions and utilities
|
|
@@ -21,7 +25,6 @@ import * as nodeHelper from "melperjs/node";
|
|
|
21
25
|
import axios from "axios";
|
|
22
26
|
|
|
23
27
|
console.log(helper.CONSTANTS);
|
|
24
|
-
console.log(helper.Exception("something went wrong", {status: 400}, "bad request error"));
|
|
25
28
|
console.log(helper.time());
|
|
26
29
|
await helper.sleepMs(1000);
|
|
27
30
|
console.log(helper.time());
|
|
@@ -33,6 +36,7 @@ try {
|
|
|
33
36
|
console.error(e.message);
|
|
34
37
|
console.log("Timeout, Internal Error ?", helper.isIntlError(e));
|
|
35
38
|
}
|
|
39
|
+
helper.promiseSilent(helper.sleep(5))
|
|
36
40
|
console.log(helper.splitClear(`
|
|
37
41
|
2.satır
|
|
38
42
|
|
|
@@ -55,6 +59,24 @@ console.log("0 empty ?", helper.checkEmpty(1));
|
|
|
55
59
|
console.log("[] empty ?", helper.checkEmpty([]));
|
|
56
60
|
console.log(helper.pascalCase("pascal case"));
|
|
57
61
|
console.log(helper.titleCase("THIS mUsT be Title"));
|
|
62
|
+
console.log(helper.parseNumFromObj({
|
|
63
|
+
a: "123",
|
|
64
|
+
b: 456,
|
|
65
|
+
c: "789.01",
|
|
66
|
+
d: "hello",
|
|
67
|
+
e: "0",
|
|
68
|
+
f: true,
|
|
69
|
+
g: "5000"
|
|
70
|
+
}));
|
|
71
|
+
console.log(helper.parseIntFromObj({
|
|
72
|
+
a: "123",
|
|
73
|
+
b: 456,
|
|
74
|
+
c: "789.01",
|
|
75
|
+
d: "hello",
|
|
76
|
+
e: "0",
|
|
77
|
+
f: true,
|
|
78
|
+
g: "5000"
|
|
79
|
+
}))
|
|
58
80
|
console.log(helper.objectStringify({
|
|
59
81
|
a: "hello",
|
|
60
82
|
b: 1,
|
|
@@ -73,10 +95,12 @@ console.log(helper.randomHex(8));
|
|
|
73
95
|
console.log(helper.randomInteger(100, 1000));
|
|
74
96
|
console.log(helper.randomUuid(true));
|
|
75
97
|
console.log(helper.randomWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
98
|
+
console.log(helper.randomElement({a: "vA", b: "vB", c: "vC"}));
|
|
76
99
|
console.log(nodeHelper.tokenString(32, true, true));
|
|
77
100
|
console.log(nodeHelper.tokenHex(8));
|
|
78
101
|
console.log(nodeHelper.tokenUuid(true));
|
|
79
102
|
console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
103
|
+
console.log(nodeHelper.tokenElement(["vA", "vB", "vC"]));
|
|
80
104
|
console.log(nodeHelper.md5("data"));
|
|
81
105
|
const password = nodeHelper.hashBcrypt("plain", "encryptionKey");
|
|
82
106
|
console.log(password)
|
|
@@ -92,7 +116,10 @@ console.log(nodeHelper.serverIp());
|
|
|
92
116
|
console.log("HTTP CODE: 400 (Bad Request) ?", helper.isIntlHttpCode(401));
|
|
93
117
|
console.log("HTTP CODE: 407 (Failed Proxy Auth) ?", helper.isIntlHttpCode(407));
|
|
94
118
|
nodeHelper.createNumDir("test");
|
|
119
|
+
await nodeHelper.writeJsonFile("test.json", {test: "test json file"});
|
|
120
|
+
console.log(nodeHelper.readJsonFileSync("test.json"));
|
|
95
121
|
console.log("VERSIONED BY .GIT", "v" + nodeHelper.getVersion());
|
|
122
|
+
console.log(helper.Exception("something went wrong", {status: 400}, "bad request error"));
|
|
96
123
|
await helper.forever(1000, async () => {
|
|
97
124
|
await helper.sleep(1);
|
|
98
125
|
console.log("Do something!")
|
|
@@ -103,77 +130,6 @@ await helper.forever(1000, async () => {
|
|
|
103
130
|
console.log("Run this process forever!")
|
|
104
131
|
}
|
|
105
132
|
);
|
|
106
|
-
|
|
107
|
-
/*
|
|
108
|
-
{
|
|
109
|
-
LOWER_CASE: 'abcdefghijklmnopqrstuvwxyz',
|
|
110
|
-
UPPER_CASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
111
|
-
HEXADECIMAL: '0123456789abcdef',
|
|
112
|
-
NUMBERS: '0123456789'
|
|
113
|
-
}
|
|
114
|
-
{
|
|
115
|
-
name: 'BadRequestError',
|
|
116
|
-
message: 'something went wrong',
|
|
117
|
-
response: { status: 400 }
|
|
118
|
-
}
|
|
119
|
-
1701697141
|
|
120
|
-
1701697142
|
|
121
|
-
1701697143
|
|
122
|
-
Promise timed out after 1000ms
|
|
123
|
-
Timeout, Internal Error ? true
|
|
124
|
-
[ '2.satır', '4.satır' ]
|
|
125
|
-
{ x: 1, y: 2, c: { d: true } }
|
|
126
|
-
'' empty ? true
|
|
127
|
-
1 empty ? false
|
|
128
|
-
0 empty ? false
|
|
129
|
-
[] empty ? true
|
|
130
|
-
PascalCase
|
|
131
|
-
First letter upper
|
|
132
|
-
first Letter Lower
|
|
133
|
-
This Must Be Title
|
|
134
|
-
{
|
|
135
|
-
a: 'hello',
|
|
136
|
-
b: '1',
|
|
137
|
-
c: 'undefined',
|
|
138
|
-
d: 'null',
|
|
139
|
-
e: { ea: '2' },
|
|
140
|
-
f: [ '3', '4', '5' ],
|
|
141
|
-
g: 'false'
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
LONG...
|
|
145
|
-
SAFE TEXT
|
|
146
|
-
sP3jTNwe1rRrW1TVAPb4HAXNFjJB2mWb
|
|
147
|
-
f70f7212
|
|
148
|
-
376
|
|
149
|
-
f07fe6b1-46d5-4f30-8138-f263f4916e65
|
|
150
|
-
strongProbability
|
|
151
|
-
JT4tXSI7YdIYDGbzLmHkItZ32vgi5aos
|
|
152
|
-
52c0da20
|
|
153
|
-
3e4374f4-11d9-4174-b337-dbf8d7fa2d41
|
|
154
|
-
strongProbability
|
|
155
|
-
8d777f385d3dfec8815d20f7496026dc
|
|
156
|
-
$2b$10$DTITmEyk1IcWfG1qaVEvyOgLReqOI97X/LufbbV/nvOU8DspBNMOS
|
|
157
|
-
passwordHash verified ? true
|
|
158
|
-
{
|
|
159
|
-
'1P_JAR': '2023-12-04-13',
|
|
160
|
-
AEC: 'Ackid1Q9P_jk5EM2S_PU_QT-RUEu0syeyPMuCOLYbtlkX5gvB1zRTPytuw',
|
|
161
|
-
NID: '511=jBXxJSukq7Ku4449skx8tmFlqkM-nKwhaQ4hukE0F-jntKrI8daHyoAS6npvlujAMKU966ZMNGE6wu8xYc2PciilTQrKxgRyJv1QsdNIc6y_mlrLfuOfLXkwDuf0YWdS0Or3Aq6wHR87o0paAAcYAntlopexVF7NpQ6yifGe57c'
|
|
162
|
-
}
|
|
163
|
-
1P_JAR=2023-12-04-13;AEC=Ackid1Q9P_jk5EM2S_PU_QT-RUEu0syeyPMuCOLYbtlkX5gvB1zRTPytuw;NID=511=jBXxJSukq7Ku4449skx8tmFlqkM-nKwhaQ4hukE0F-jntKrI8daHyoAS6npvlujAMKU966ZMNGE6wu8xYc2PciilTQrKxgRyJv1QsdNIc6y_mlrLfuOfLXkwDuf0YWdS0Or3Aq6wHR87o0paAAcYAntlopexVF7NpQ6yifGe57c
|
|
164
|
-
http://id:pw-{SESSION}@127.0.0.1:8080
|
|
165
|
-
{
|
|
166
|
-
protocol: 'http',
|
|
167
|
-
host: '127.0.0.1',
|
|
168
|
-
port: 8080,
|
|
169
|
-
auth: { username: 'id', password: 'pw-{SESSION}' }
|
|
170
|
-
}
|
|
171
|
-
http://id:pw-749756be@127.0.0.1:8080
|
|
172
|
-
127.0.0.1
|
|
173
|
-
HTTP CODE: 400 (Bad Request) ? false
|
|
174
|
-
HTTP CODE: 407 (Failed Proxy Auth) ? true
|
|
175
|
-
VERSIONED BY .GIT v2310.15182
|
|
176
|
-
*/
|
|
177
133
|
```
|
|
178
134
|
|
|
179
135
|
## License
|
package/lib/cjs/index.js
CHANGED
|
@@ -14,8 +14,12 @@ exports.isIntlError = isIntlError;
|
|
|
14
14
|
exports.isIntlHttpCode = isIntlHttpCode;
|
|
15
15
|
exports.limitString = limitString;
|
|
16
16
|
exports.objectStringify = objectStringify;
|
|
17
|
+
exports.parseIntFromObj = parseIntFromObj;
|
|
18
|
+
exports.parseNumFromObj = parseNumFromObj;
|
|
17
19
|
exports.pascalCase = pascalCase;
|
|
20
|
+
exports.promiseSilent = promiseSilent;
|
|
18
21
|
exports.promiseTimeout = promiseTimeout;
|
|
22
|
+
exports.randomElement = randomElement;
|
|
19
23
|
exports.randomHex = randomHex;
|
|
20
24
|
exports.randomInteger = randomInteger;
|
|
21
25
|
exports.randomString = randomString;
|
|
@@ -32,7 +36,7 @@ var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
|
|
|
32
36
|
var _camelCase = _interopRequireDefault(require("lodash/camelCase.js"));
|
|
33
37
|
var _upperFirst = _interopRequireDefault(require("lodash/upperFirst.js"));
|
|
34
38
|
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty.js"));
|
|
35
|
-
function _interopRequireDefault(
|
|
39
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
36
40
|
const CONSTANTS = exports.CONSTANTS = {
|
|
37
41
|
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
38
42
|
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
@@ -90,6 +94,9 @@ function promiseTimeout(milliseconds, promise) {
|
|
|
90
94
|
});
|
|
91
95
|
});
|
|
92
96
|
}
|
|
97
|
+
function promiseSilent(promise) {
|
|
98
|
+
return promise.then(() => {}).catch(() => {});
|
|
99
|
+
}
|
|
93
100
|
function splitClear(rawText, separator = null) {
|
|
94
101
|
separator = separator ?? /\r?\n/;
|
|
95
102
|
return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
|
|
@@ -121,6 +128,28 @@ function titleCase(str) {
|
|
|
121
128
|
str = str || "";
|
|
122
129
|
return str.replace(/\b\w/g, char => char.toUpperCase());
|
|
123
130
|
}
|
|
131
|
+
function parseNumFromObj(obj) {
|
|
132
|
+
for (let key in obj) {
|
|
133
|
+
let value = obj[key];
|
|
134
|
+
let number = parseFloat(value);
|
|
135
|
+
if (typeof value === 'string' && !isNaN(number)) {
|
|
136
|
+
value = number;
|
|
137
|
+
}
|
|
138
|
+
obj[key] = value;
|
|
139
|
+
}
|
|
140
|
+
return obj;
|
|
141
|
+
}
|
|
142
|
+
function parseIntFromObj(obj) {
|
|
143
|
+
for (let key in obj) {
|
|
144
|
+
let value = obj[key];
|
|
145
|
+
let number = parseInt(value);
|
|
146
|
+
if (typeof value === 'string' && !isNaN(number) && value.length === number.toString().length) {
|
|
147
|
+
value = number;
|
|
148
|
+
}
|
|
149
|
+
obj[key] = value;
|
|
150
|
+
}
|
|
151
|
+
return obj;
|
|
152
|
+
}
|
|
124
153
|
function objectStringify(obj) {
|
|
125
154
|
for (let key in obj) {
|
|
126
155
|
if (obj.hasOwnProperty(key)) {
|
|
@@ -205,7 +234,8 @@ function randomUuid(useDashes = true) {
|
|
|
205
234
|
});
|
|
206
235
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
207
236
|
}
|
|
208
|
-
function randomWeighted(dict, randomFunc =
|
|
237
|
+
function randomWeighted(dict, randomFunc = null) {
|
|
238
|
+
randomFunc = randomFunc || (totalWeight => Math.random() * totalWeight);
|
|
209
239
|
let elements = Object.keys(dict);
|
|
210
240
|
let weights = Object.values(dict);
|
|
211
241
|
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
@@ -218,6 +248,13 @@ function randomWeighted(dict, randomFunc = totalWeight => Math.random() * totalW
|
|
|
218
248
|
}
|
|
219
249
|
}
|
|
220
250
|
}
|
|
251
|
+
function randomElement(obj) {
|
|
252
|
+
if (Array.isArray(obj)) {
|
|
253
|
+
return obj[Math.floor(Math.random() * obj.length)];
|
|
254
|
+
} else {
|
|
255
|
+
return obj[randomElement(Object.keys(obj))];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
221
258
|
function cookieDict(res, decodeValues = false) {
|
|
222
259
|
let dict = {};
|
|
223
260
|
const cookies = _setCookieParser.default.parse(res, {
|
package/lib/cjs/node.js
CHANGED
|
@@ -10,20 +10,27 @@ exports.hashBcrypt = hashBcrypt;
|
|
|
10
10
|
exports.md5 = md5;
|
|
11
11
|
exports.proxyObject = proxyObject;
|
|
12
12
|
exports.proxyValue = proxyValue;
|
|
13
|
+
exports.readJsonFile = readJsonFile;
|
|
14
|
+
exports.readJsonFileSync = readJsonFileSync;
|
|
13
15
|
exports.serverIp = serverIp;
|
|
16
|
+
exports.tokenElement = tokenElement;
|
|
14
17
|
exports.tokenHex = tokenHex;
|
|
15
18
|
exports.tokenString = tokenString;
|
|
16
19
|
exports.tokenUuid = tokenUuid;
|
|
17
20
|
exports.tokenWeighted = tokenWeighted;
|
|
18
21
|
exports.verifyBcrypt = verifyBcrypt;
|
|
19
|
-
|
|
22
|
+
exports.writeJsonFile = writeJsonFile;
|
|
23
|
+
exports.writeJsonFileSync = writeJsonFileSync;
|
|
24
|
+
var _fs = _interopRequireWildcard(require("fs"));
|
|
20
25
|
var _path = _interopRequireDefault(require("path"));
|
|
21
26
|
var _crypto = _interopRequireDefault(require("crypto"));
|
|
22
27
|
var _os = require("os");
|
|
23
28
|
var _child_process = require("child_process");
|
|
24
29
|
var _bcryptjs = _interopRequireDefault(require("bcryptjs"));
|
|
25
30
|
var _index = require("./index.js");
|
|
26
|
-
function _interopRequireDefault(
|
|
31
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
32
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
33
|
+
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; }
|
|
27
34
|
function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
28
35
|
const lowercaseChars = _index.CONSTANTS.LOWER_CASE;
|
|
29
36
|
const uppercaseChars = _index.CONSTANTS.UPPER_CASE;
|
|
@@ -51,6 +58,13 @@ function tokenUuid(useDashes = true) {
|
|
|
51
58
|
function tokenWeighted(dict) {
|
|
52
59
|
return (0, _index.randomWeighted)(dict, _crypto.default.randomInt);
|
|
53
60
|
}
|
|
61
|
+
function tokenElement(obj) {
|
|
62
|
+
if (Array.isArray(obj)) {
|
|
63
|
+
return obj[_crypto.default.randomInt(0, obj.length)];
|
|
64
|
+
} else {
|
|
65
|
+
return obj[tokenElement(Object.keys(obj))];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
54
68
|
function serverIp() {
|
|
55
69
|
const interfaces = (0, _os.networkInterfaces)();
|
|
56
70
|
for (const devName in interfaces) {
|
|
@@ -71,9 +85,9 @@ function getVersion() {
|
|
|
71
85
|
const day = formatDatePart(date.getDate());
|
|
72
86
|
const hour = formatDatePart(date.getHours());
|
|
73
87
|
const minute = formatDatePart(date.getMinutes());
|
|
74
|
-
return
|
|
88
|
+
return `${year}${month}${day}.${hour}${minute}`;
|
|
75
89
|
} catch {
|
|
76
|
-
return 1.0;
|
|
90
|
+
return "1.0";
|
|
77
91
|
}
|
|
78
92
|
}
|
|
79
93
|
function createNumDir(mainDirectory) {
|
|
@@ -148,4 +162,28 @@ function proxyValue(proxies) {
|
|
|
148
162
|
proxy = formatProxy(proxy);
|
|
149
163
|
proxy = proxy.replace("{SESSION}", tokenHex(8));
|
|
150
164
|
return proxy || null;
|
|
165
|
+
}
|
|
166
|
+
async function readJsonFile(filePath) {
|
|
167
|
+
try {
|
|
168
|
+
const data = await _fs.promises.readFile(filePath, 'utf8');
|
|
169
|
+
return JSON.parse(data);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return {};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function readJsonFileSync(filePath) {
|
|
175
|
+
try {
|
|
176
|
+
const data = _fs.default.readFileSync(filePath, 'utf8');
|
|
177
|
+
return JSON.parse(data);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
return {};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async function writeJsonFile(filePath, data) {
|
|
183
|
+
const jsonData = JSON.stringify(data);
|
|
184
|
+
return await _fs.promises.writeFile(filePath, jsonData, 'utf8');
|
|
185
|
+
}
|
|
186
|
+
function writeJsonFileSync(filePath, data) {
|
|
187
|
+
const jsonData = JSON.stringify(data);
|
|
188
|
+
return _fs.default.writeFileSync(filePath, jsonData, 'utf8');
|
|
151
189
|
}
|
package/lib/esm/index.js
CHANGED
|
@@ -60,6 +60,9 @@ export function promiseTimeout(milliseconds, promise) {
|
|
|
60
60
|
});
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
|
+
export function promiseSilent(promise) {
|
|
64
|
+
return promise.then(() => {}).catch(() => {});
|
|
65
|
+
}
|
|
63
66
|
export function splitClear(rawText, separator = null) {
|
|
64
67
|
separator = separator ?? /\r?\n/;
|
|
65
68
|
return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
|
|
@@ -91,6 +94,28 @@ export function titleCase(str) {
|
|
|
91
94
|
str = str || "";
|
|
92
95
|
return str.replace(/\b\w/g, char => char.toUpperCase());
|
|
93
96
|
}
|
|
97
|
+
export function parseNumFromObj(obj) {
|
|
98
|
+
for (let key in obj) {
|
|
99
|
+
let value = obj[key];
|
|
100
|
+
let number = parseFloat(value);
|
|
101
|
+
if (typeof value === 'string' && !isNaN(number)) {
|
|
102
|
+
value = number;
|
|
103
|
+
}
|
|
104
|
+
obj[key] = value;
|
|
105
|
+
}
|
|
106
|
+
return obj;
|
|
107
|
+
}
|
|
108
|
+
export function parseIntFromObj(obj) {
|
|
109
|
+
for (let key in obj) {
|
|
110
|
+
let value = obj[key];
|
|
111
|
+
let number = parseInt(value);
|
|
112
|
+
if (typeof value === 'string' && !isNaN(number) && value.length === number.toString().length) {
|
|
113
|
+
value = number;
|
|
114
|
+
}
|
|
115
|
+
obj[key] = value;
|
|
116
|
+
}
|
|
117
|
+
return obj;
|
|
118
|
+
}
|
|
94
119
|
export function objectStringify(obj) {
|
|
95
120
|
for (let key in obj) {
|
|
96
121
|
if (obj.hasOwnProperty(key)) {
|
|
@@ -175,7 +200,8 @@ export function randomUuid(useDashes = true) {
|
|
|
175
200
|
});
|
|
176
201
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
177
202
|
}
|
|
178
|
-
export function randomWeighted(dict, randomFunc =
|
|
203
|
+
export function randomWeighted(dict, randomFunc = null) {
|
|
204
|
+
randomFunc = randomFunc || (totalWeight => Math.random() * totalWeight);
|
|
179
205
|
let elements = Object.keys(dict);
|
|
180
206
|
let weights = Object.values(dict);
|
|
181
207
|
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
@@ -188,6 +214,13 @@ export function randomWeighted(dict, randomFunc = totalWeight => Math.random() *
|
|
|
188
214
|
}
|
|
189
215
|
}
|
|
190
216
|
}
|
|
217
|
+
export function randomElement(obj) {
|
|
218
|
+
if (Array.isArray(obj)) {
|
|
219
|
+
return obj[Math.floor(Math.random() * obj.length)];
|
|
220
|
+
} else {
|
|
221
|
+
return obj[randomElement(Object.keys(obj))];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
191
224
|
export function cookieDict(res, decodeValues = false) {
|
|
192
225
|
let dict = {};
|
|
193
226
|
const cookies = setCookieParser.parse(res, {
|
package/lib/esm/node.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
|
+
import { promises as fsp } from "fs";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import crypto from "crypto";
|
|
4
5
|
import { networkInterfaces } from "os";
|
|
@@ -32,6 +33,13 @@ export function tokenUuid(useDashes = true) {
|
|
|
32
33
|
export function tokenWeighted(dict) {
|
|
33
34
|
return randomWeighted(dict, crypto.randomInt);
|
|
34
35
|
}
|
|
36
|
+
export function tokenElement(obj) {
|
|
37
|
+
if (Array.isArray(obj)) {
|
|
38
|
+
return obj[crypto.randomInt(0, obj.length)];
|
|
39
|
+
} else {
|
|
40
|
+
return obj[tokenElement(Object.keys(obj))];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
35
43
|
export function serverIp() {
|
|
36
44
|
const interfaces = networkInterfaces();
|
|
37
45
|
for (const devName in interfaces) {
|
|
@@ -52,9 +60,9 @@ export function getVersion() {
|
|
|
52
60
|
const day = formatDatePart(date.getDate());
|
|
53
61
|
const hour = formatDatePart(date.getHours());
|
|
54
62
|
const minute = formatDatePart(date.getMinutes());
|
|
55
|
-
return
|
|
63
|
+
return `${year}${month}${day}.${hour}${minute}`;
|
|
56
64
|
} catch {
|
|
57
|
-
return 1.0;
|
|
65
|
+
return "1.0";
|
|
58
66
|
}
|
|
59
67
|
}
|
|
60
68
|
export function createNumDir(mainDirectory) {
|
|
@@ -129,4 +137,28 @@ export function proxyValue(proxies) {
|
|
|
129
137
|
proxy = formatProxy(proxy);
|
|
130
138
|
proxy = proxy.replace("{SESSION}", tokenHex(8));
|
|
131
139
|
return proxy || null;
|
|
140
|
+
}
|
|
141
|
+
export async function readJsonFile(filePath) {
|
|
142
|
+
try {
|
|
143
|
+
const data = await fsp.readFile(filePath, 'utf8');
|
|
144
|
+
return JSON.parse(data);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
return {};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
export function readJsonFileSync(filePath) {
|
|
150
|
+
try {
|
|
151
|
+
const data = fs.readFileSync(filePath, 'utf8');
|
|
152
|
+
return JSON.parse(data);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
return {};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
export async function writeJsonFile(filePath, data) {
|
|
158
|
+
const jsonData = JSON.stringify(data);
|
|
159
|
+
return await fsp.writeFile(filePath, jsonData, 'utf8');
|
|
160
|
+
}
|
|
161
|
+
export function writeJsonFileSync(filePath, data) {
|
|
162
|
+
const jsonData = JSON.stringify(data);
|
|
163
|
+
return fs.writeFileSync(filePath, jsonData, 'utf8');
|
|
132
164
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melperjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"description": "Javascript module to use predefined common functions and utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"melperjs",
|
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
"xss": "^1.0.15"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@babel/cli": "^7.
|
|
50
|
-
"@babel/core": "^7.
|
|
51
|
-
"@babel/preset-env": "^7.
|
|
49
|
+
"@babel/cli": "^7.24.7",
|
|
50
|
+
"@babel/core": "^7.24.7",
|
|
51
|
+
"@babel/preset-env": "^7.24.7",
|
|
52
52
|
"axios": "^1.6.8",
|
|
53
53
|
"cross-env": "^7.0.3"
|
|
54
54
|
}
|