melperjs 8.0.0 → 9.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 +28 -72
- package/lib/cjs/index.js +38 -1
- package/lib/cjs/node.js +39 -1
- package/lib/esm/index.js +91 -97
- package/lib/esm/node.js +78 -52
- 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;
|
|
@@ -90,6 +94,9 @@ function promiseTimeout(milliseconds, promise) {
|
|
|
90
94
|
});
|
|
91
95
|
});
|
|
92
96
|
}
|
|
97
|
+
function promiseSilent(promise) {
|
|
98
|
+
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,13 +10,18 @@ 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");
|
|
@@ -24,6 +29,8 @@ var _child_process = require("child_process");
|
|
|
24
29
|
var _bcryptjs = _interopRequireDefault(require("bcryptjs"));
|
|
25
30
|
var _index = require("./index.js");
|
|
26
31
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
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 && Object.prototype.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) {
|
|
@@ -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
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
2
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
3
1
|
import xss from "xss";
|
|
4
2
|
import setCookieParser from "set-cookie-parser";
|
|
5
3
|
import camelCase from "lodash/camelCase.js";
|
|
6
4
|
import upperFirst from "lodash/upperFirst.js";
|
|
7
5
|
import isEmpty from "lodash/isEmpty.js";
|
|
8
|
-
export
|
|
6
|
+
export const CONSTANTS = {
|
|
9
7
|
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
10
8
|
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
11
9
|
HEXADECIMAL: "0123456789abcdef",
|
|
12
10
|
NUMBERS: "0123456789"
|
|
13
11
|
};
|
|
14
|
-
export function Exception(message) {
|
|
15
|
-
var response = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
16
|
-
var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
12
|
+
export function Exception(message, response = {}, name = null) {
|
|
17
13
|
class ExceptionClass extends Error {
|
|
18
14
|
constructor(message, response, name) {
|
|
19
15
|
super(message);
|
|
@@ -24,55 +20,35 @@ export function Exception(message) {
|
|
|
24
20
|
}
|
|
25
21
|
return new ExceptionClass(message, response, name);
|
|
26
22
|
}
|
|
27
|
-
export function forever(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var _value = onError && (yield onError(e));
|
|
42
|
-
if (checkCooldown(_value)) cooldown = _value;
|
|
43
|
-
} finally {
|
|
44
|
-
var _value2 = onCompleted && (yield onCompleted());
|
|
45
|
-
if (checkCooldown(_value2)) cooldown = _value2;
|
|
46
|
-
yield sleepMs(cooldown);
|
|
47
|
-
}
|
|
23
|
+
export async function forever(cooldown, onSuccess, onError = null, onCompleted = null) {
|
|
24
|
+
const checkCooldown = value => value && !isNaN(value) && value > 0;
|
|
25
|
+
if (!checkCooldown(cooldown)) throw new Error("Cooldown must be a positive number");
|
|
26
|
+
while (true) {
|
|
27
|
+
try {
|
|
28
|
+
const value = await onSuccess();
|
|
29
|
+
if (checkCooldown(value)) cooldown = value;
|
|
30
|
+
} catch (e) {
|
|
31
|
+
const value = onError && (await onError(e));
|
|
32
|
+
if (checkCooldown(value)) cooldown = value;
|
|
33
|
+
} finally {
|
|
34
|
+
const value = onCompleted && (await onCompleted());
|
|
35
|
+
if (checkCooldown(value)) cooldown = value;
|
|
36
|
+
await sleepMs(cooldown);
|
|
48
37
|
}
|
|
49
|
-
}
|
|
50
|
-
return _forever.apply(this, arguments);
|
|
38
|
+
}
|
|
51
39
|
}
|
|
52
40
|
export function time() {
|
|
53
41
|
return Math.floor(Date.now() / 1000);
|
|
54
42
|
}
|
|
55
|
-
export function sleepMs(
|
|
56
|
-
return
|
|
57
|
-
}
|
|
58
|
-
function _sleepMs() {
|
|
59
|
-
_sleepMs = _asyncToGenerator(function* (milliseconds) {
|
|
60
|
-
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
61
|
-
});
|
|
62
|
-
return _sleepMs.apply(this, arguments);
|
|
63
|
-
}
|
|
64
|
-
export function sleep(_x4) {
|
|
65
|
-
return _sleep.apply(this, arguments);
|
|
43
|
+
export async function sleepMs(milliseconds) {
|
|
44
|
+
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
66
45
|
}
|
|
67
|
-
function
|
|
68
|
-
|
|
69
|
-
return yield sleepMs(seconds * 1000);
|
|
70
|
-
});
|
|
71
|
-
return _sleep.apply(this, arguments);
|
|
46
|
+
export async function sleep(seconds) {
|
|
47
|
+
return await sleepMs(seconds * 1000);
|
|
72
48
|
}
|
|
73
49
|
export function promiseTimeout(milliseconds, promise) {
|
|
74
50
|
return new Promise((resolve, reject) => {
|
|
75
|
-
|
|
51
|
+
const timer = setTimeout(() => {
|
|
76
52
|
reject(new Error('Promise timed out after ' + milliseconds + 'ms'));
|
|
77
53
|
}, milliseconds);
|
|
78
54
|
promise.then(value => {
|
|
@@ -84,19 +60,19 @@ export function promiseTimeout(milliseconds, promise) {
|
|
|
84
60
|
});
|
|
85
61
|
});
|
|
86
62
|
}
|
|
87
|
-
export function
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
63
|
+
export function promiseSilent(promise) {
|
|
64
|
+
promise.then(() => {}).catch(() => {});
|
|
65
|
+
}
|
|
66
|
+
export function splitClear(rawText, separator = null) {
|
|
67
|
+
separator = separator ?? /\r?\n/;
|
|
91
68
|
return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
|
|
92
69
|
}
|
|
93
|
-
export function findKeyNode(key, node) {
|
|
94
|
-
var pair = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
70
|
+
export function findKeyNode(key, node, pair = null) {
|
|
95
71
|
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
96
72
|
return node;
|
|
97
73
|
} else if (typeof node === 'object') {
|
|
98
|
-
for (
|
|
99
|
-
|
|
74
|
+
for (let index in node) {
|
|
75
|
+
const result = findKeyNode(key, node[index], pair);
|
|
100
76
|
if (result) {
|
|
101
77
|
return result;
|
|
102
78
|
}
|
|
@@ -118,14 +94,35 @@ export function titleCase(str) {
|
|
|
118
94
|
str = str || "";
|
|
119
95
|
return str.replace(/\b\w/g, char => char.toUpperCase());
|
|
120
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
|
+
}
|
|
121
119
|
export function objectStringify(obj) {
|
|
122
|
-
for (
|
|
120
|
+
for (let key in obj) {
|
|
123
121
|
if (obj.hasOwnProperty(key)) {
|
|
124
122
|
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
125
123
|
objectStringify(obj[key]);
|
|
126
124
|
} else {
|
|
127
|
-
|
|
128
|
-
if ((_obj$key = obj[key]) !== null && _obj$key !== void 0 && _obj$key.toString) {
|
|
125
|
+
if (obj[key]?.toString) {
|
|
129
126
|
obj[key] = obj[key].toString();
|
|
130
127
|
} else {
|
|
131
128
|
obj[key] = String(obj[key]);
|
|
@@ -135,9 +132,7 @@ export function objectStringify(obj) {
|
|
|
135
132
|
}
|
|
136
133
|
return obj;
|
|
137
134
|
}
|
|
138
|
-
export function limitString(str) {
|
|
139
|
-
var limit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 35;
|
|
140
|
-
var omission = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "...";
|
|
135
|
+
export function limitString(str, limit = 35, omission = "...") {
|
|
141
136
|
str = str || "";
|
|
142
137
|
if (str.length <= limit) {
|
|
143
138
|
return str;
|
|
@@ -153,42 +148,40 @@ export function safeString(str) {
|
|
|
153
148
|
stripIgnoreTagBody: ["script"]
|
|
154
149
|
});
|
|
155
150
|
}
|
|
156
|
-
export function randomString(length) {
|
|
157
|
-
|
|
158
|
-
var useUppercase = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
159
|
-
var characters = CONSTANTS.LOWER_CASE;
|
|
151
|
+
export function randomString(length, useNumbers = true, useUppercase = false) {
|
|
152
|
+
let characters = CONSTANTS.LOWER_CASE;
|
|
160
153
|
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
161
154
|
if (useNumbers) characters += CONSTANTS.NUMBERS;
|
|
162
|
-
|
|
163
|
-
for (
|
|
164
|
-
|
|
155
|
+
let randomString = '';
|
|
156
|
+
for (let i = 0; i < length; i++) {
|
|
157
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
165
158
|
randomString += characters[randomIndex];
|
|
166
159
|
}
|
|
167
160
|
return randomString;
|
|
168
161
|
}
|
|
169
162
|
export function randomHex(length) {
|
|
170
|
-
|
|
171
|
-
for (
|
|
172
|
-
|
|
163
|
+
let result = '';
|
|
164
|
+
for (let i = 0; i < length; i++) {
|
|
165
|
+
const randomIndex = Math.floor(Math.random() * CONSTANTS.HEXADECIMAL.length);
|
|
173
166
|
result += CONSTANTS.HEXADECIMAL[randomIndex];
|
|
174
167
|
}
|
|
175
168
|
return result;
|
|
176
169
|
}
|
|
177
170
|
export function randomInteger(min, max, callback) {
|
|
178
|
-
|
|
171
|
+
const minNotSpecified = typeof max === 'undefined' || typeof max === 'function';
|
|
179
172
|
if (minNotSpecified) {
|
|
180
173
|
callback = max;
|
|
181
174
|
max = min;
|
|
182
175
|
min = 0;
|
|
183
176
|
}
|
|
184
|
-
|
|
177
|
+
const isSync = typeof callback === 'undefined';
|
|
185
178
|
if (typeof min !== 'number' || typeof max !== 'number') {
|
|
186
179
|
throw new Error('min and max must be numerical values');
|
|
187
180
|
}
|
|
188
181
|
if (max <= min) {
|
|
189
182
|
throw new Error('max must be greater than min');
|
|
190
183
|
}
|
|
191
|
-
|
|
184
|
+
const randomNumber = Math.floor(Math.random() * (max - min)) + min;
|
|
192
185
|
if (isSync) {
|
|
193
186
|
return randomNumber;
|
|
194
187
|
} else {
|
|
@@ -198,51 +191,52 @@ export function randomInteger(min, max, callback) {
|
|
|
198
191
|
callback(randomNumber);
|
|
199
192
|
}
|
|
200
193
|
}
|
|
201
|
-
export function randomUuid() {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
var r = (d + Math.random() * 16) % 16 | 0;
|
|
194
|
+
export function randomUuid(useDashes = true) {
|
|
195
|
+
let d = Date.now();
|
|
196
|
+
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
197
|
+
const r = (d + Math.random() * 16) % 16 | 0;
|
|
206
198
|
d = Math.floor(d / 16);
|
|
207
199
|
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16);
|
|
208
200
|
});
|
|
209
201
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
210
202
|
}
|
|
211
|
-
export function randomWeighted(dict) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
for (
|
|
203
|
+
export function randomWeighted(dict, randomFunc = null) {
|
|
204
|
+
randomFunc = randomFunc || (totalWeight => Math.random() * totalWeight);
|
|
205
|
+
let elements = Object.keys(dict);
|
|
206
|
+
let weights = Object.values(dict);
|
|
207
|
+
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
208
|
+
let randomNum = randomFunc(totalWeight);
|
|
209
|
+
let weightSum = 0;
|
|
210
|
+
for (let i = 0; i < elements.length; i++) {
|
|
219
211
|
weightSum += weights[i];
|
|
220
212
|
if (randomNum <= weightSum) {
|
|
221
213
|
return elements[i];
|
|
222
214
|
}
|
|
223
215
|
}
|
|
224
216
|
}
|
|
225
|
-
export function
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
+
}
|
|
224
|
+
export function cookieDict(res, decodeValues = false) {
|
|
225
|
+
let dict = {};
|
|
226
|
+
const cookies = setCookieParser.parse(res, {
|
|
229
227
|
decodeValues: decodeValues
|
|
230
228
|
});
|
|
231
|
-
for (
|
|
229
|
+
for (let cookie of cookies) {
|
|
232
230
|
dict[cookie.name] = cookie.value;
|
|
233
231
|
}
|
|
234
232
|
return dict;
|
|
235
233
|
}
|
|
236
234
|
export function cookieHeader(cookieDict) {
|
|
237
|
-
return Object.entries(cookieDict).map(
|
|
238
|
-
var [key, value] = _ref;
|
|
239
|
-
return "".concat(key, "=").concat(value);
|
|
240
|
-
}).join(';');
|
|
235
|
+
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
241
236
|
}
|
|
242
237
|
export function isIntlHttpCode(httpCode) {
|
|
243
238
|
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
244
239
|
}
|
|
245
240
|
export function isIntlError(e) {
|
|
246
|
-
|
|
247
|
-
return (e === null || e === void 0 || (_e$message = e.message) === null || _e$message === void 0 || (_e$message$toLowerCas = _e$message.toLowerCase) === null || _e$message$toLowerCas === void 0 || (_e$message$toLowerCas = _e$message$toLowerCas.call(_e$message)) === null || _e$message$toLowerCas === void 0 || (_e$message$toLowerCas2 = _e$message$toLowerCas.includes) === null || _e$message$toLowerCas2 === void 0 ? void 0 : _e$message$toLowerCas2.call(_e$message$toLowerCas, "timeout")) || (e === null || e === void 0 || (_e$message2 = e.message) === null || _e$message2 === void 0 || (_e$message2$toLowerCa = _e$message2.toLowerCase) === null || _e$message2$toLowerCa === void 0 || (_e$message2$toLowerCa = _e$message2$toLowerCa.call(_e$message2)) === null || _e$message2$toLowerCa === void 0 || (_e$message2$toLowerCa2 = _e$message2$toLowerCa.includes) === null || _e$message2$toLowerCa2 === void 0 ? void 0 : _e$message2$toLowerCa2.call(_e$message2$toLowerCa, "aborted")) || (e === null || e === void 0 || (_e$message3 = e.message) === null || _e$message3 === void 0 || (_e$message3$toLowerCa = _e$message3.toLowerCase) === null || _e$message3$toLowerCa === void 0 || (_e$message3$toLowerCa = _e$message3$toLowerCa.call(_e$message3)) === null || _e$message3$toLowerCa === void 0 || (_e$message3$toLowerCa2 = _e$message3$toLowerCa.includes) === null || _e$message3$toLowerCa2 === void 0 ? void 0 : _e$message3$toLowerCa2.call(_e$message3$toLowerCa, "tls connection")) || (e === null || e === void 0 || (_e$message4 = e.message) === null || _e$message4 === void 0 || (_e$message4$toLowerCa = _e$message4.toLowerCase) === null || _e$message4$toLowerCa === void 0 || (_e$message4$toLowerCa = _e$message4$toLowerCa.call(_e$message4)) === null || _e$message4$toLowerCa === void 0 || (_e$message4$toLowerCa2 = _e$message4$toLowerCa.includes) === null || _e$message4$toLowerCa2 === void 0 ? void 0 : _e$message4$toLowerCa2.call(_e$message4$toLowerCa, "socket hang")) || isIntlHttpCode(e === null || e === void 0 || (_e$response = e.response) === null || _e$response === void 0 ? void 0 : _e$response.status);
|
|
241
|
+
return e?.message?.toLowerCase?.()?.includes?.("timeout") || e?.message?.toLowerCase?.()?.includes?.("aborted") || e?.message?.toLowerCase?.()?.includes?.("tls connection") || e?.message?.toLowerCase?.()?.includes?.("socket hang") || isIntlHttpCode(e?.response?.status);
|
|
248
242
|
}
|
package/lib/esm/node.js
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
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";
|
|
5
6
|
import { execSync } from "child_process";
|
|
6
7
|
import bcrypt from "bcryptjs";
|
|
7
8
|
import { CONSTANTS, randomWeighted, splitClear } from "./index.js";
|
|
8
|
-
export function tokenString(length) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var numbers = CONSTANTS.NUMBERS;
|
|
14
|
-
var characters = lowercaseChars;
|
|
9
|
+
export function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
10
|
+
const lowercaseChars = CONSTANTS.LOWER_CASE;
|
|
11
|
+
const uppercaseChars = CONSTANTS.UPPER_CASE;
|
|
12
|
+
const numbers = CONSTANTS.NUMBERS;
|
|
13
|
+
let characters = lowercaseChars;
|
|
15
14
|
if (useUppercase) characters += uppercaseChars;
|
|
16
15
|
if (useNumbers) characters += numbers;
|
|
17
|
-
|
|
16
|
+
let randomString = '';
|
|
18
17
|
while (randomString.length < length) {
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
const byte = crypto.randomBytes(1)[0];
|
|
19
|
+
const index = byte % characters.length;
|
|
21
20
|
if (byte < 256 - 256 % characters.length) {
|
|
22
21
|
randomString += characters[index];
|
|
23
22
|
}
|
|
@@ -27,20 +26,26 @@ export function tokenString(length) {
|
|
|
27
26
|
export function tokenHex(length) {
|
|
28
27
|
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
29
28
|
}
|
|
30
|
-
export function tokenUuid() {
|
|
31
|
-
|
|
32
|
-
var uuid = crypto.randomUUID().toString();
|
|
29
|
+
export function tokenUuid(useDashes = true) {
|
|
30
|
+
const uuid = crypto.randomUUID().toString();
|
|
33
31
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
34
32
|
}
|
|
35
33
|
export function tokenWeighted(dict) {
|
|
36
34
|
return randomWeighted(dict, crypto.randomInt);
|
|
37
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
|
+
}
|
|
38
43
|
export function serverIp() {
|
|
39
|
-
|
|
40
|
-
for (
|
|
41
|
-
|
|
42
|
-
for (
|
|
43
|
-
|
|
44
|
+
const interfaces = networkInterfaces();
|
|
45
|
+
for (const devName in interfaces) {
|
|
46
|
+
const interfaceValue = interfaces[devName];
|
|
47
|
+
for (let i = 0; i < interfaceValue.length; i++) {
|
|
48
|
+
const alias = interfaceValue[i];
|
|
44
49
|
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.address.startsWith("192.168.") && !alias.internal) return alias.address;
|
|
45
50
|
}
|
|
46
51
|
}
|
|
@@ -48,15 +53,15 @@ export function serverIp() {
|
|
|
48
53
|
}
|
|
49
54
|
export function getVersion() {
|
|
50
55
|
try {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return parseFloat(
|
|
59
|
-
} catch
|
|
56
|
+
const date = new Date(execSync('git show -s --format=%ci HEAD').toString().trim());
|
|
57
|
+
const formatDatePart = value => value.toString().padStart(2, '0');
|
|
58
|
+
const year = date.getFullYear().toString().slice(-2);
|
|
59
|
+
const month = formatDatePart(date.getMonth() + 1);
|
|
60
|
+
const day = formatDatePart(date.getDate());
|
|
61
|
+
const hour = formatDatePart(date.getHours());
|
|
62
|
+
const minute = formatDatePart(date.getMinutes());
|
|
63
|
+
return parseFloat(`${year}${month}${day}.${hour}${minute}`);
|
|
64
|
+
} catch {
|
|
60
65
|
return 1.0;
|
|
61
66
|
}
|
|
62
67
|
}
|
|
@@ -64,61 +69,58 @@ export function createNumDir(mainDirectory) {
|
|
|
64
69
|
fs.mkdirSync(mainDirectory, {
|
|
65
70
|
recursive: true
|
|
66
71
|
});
|
|
67
|
-
for (
|
|
72
|
+
for (let i = 0; i <= 9; i++) {
|
|
68
73
|
try {
|
|
69
74
|
fs.mkdirSync(path.join(mainDirectory, i.toString()));
|
|
70
75
|
} catch (e) {
|
|
71
|
-
console.error(
|
|
76
|
+
console.error(`createNumDir:${i}`, e.message);
|
|
72
77
|
}
|
|
73
78
|
}
|
|
74
79
|
}
|
|
75
80
|
export function md5(data) {
|
|
76
81
|
return crypto.createHash('md5').update(data).digest("hex");
|
|
77
82
|
}
|
|
78
|
-
export function hashBcrypt(plainText) {
|
|
79
|
-
var encryptionKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
|
83
|
+
export function hashBcrypt(plainText, encryptionKey = "") {
|
|
80
84
|
return bcrypt.hashSync(plainText + encryptionKey, bcrypt.genSaltSync(10));
|
|
81
85
|
}
|
|
82
|
-
export function verifyBcrypt(plainText, hash) {
|
|
83
|
-
var encryptionKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "";
|
|
86
|
+
export function verifyBcrypt(plainText, hash, encryptionKey = "") {
|
|
84
87
|
return bcrypt.compareSync(plainText + encryptionKey, hash);
|
|
85
88
|
}
|
|
86
|
-
export function formatProxy(proxy) {
|
|
87
|
-
var protocol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "http";
|
|
89
|
+
export function formatProxy(proxy, protocol = "http") {
|
|
88
90
|
proxy = proxy.trim();
|
|
89
|
-
|
|
91
|
+
const splitByProtocol = proxy.split("://");
|
|
90
92
|
if (1 < splitByProtocol.length) protocol = splitByProtocol[0];
|
|
91
93
|
proxy = splitByProtocol[splitByProtocol.length - 1];
|
|
92
94
|
if (!proxy.includes("@")) {
|
|
93
|
-
|
|
94
|
-
if (4 <=
|
|
95
|
-
proxy =
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
proxy +=
|
|
95
|
+
const proxyParts = proxy.split(":");
|
|
96
|
+
if (4 <= proxyParts.length) {
|
|
97
|
+
proxy = `${proxyParts[proxyParts.length - 2]}:${proxyParts[proxyParts.length - 1]}@`;
|
|
98
|
+
proxyParts.pop();
|
|
99
|
+
proxyParts.pop();
|
|
100
|
+
proxy += proxyParts.join(":");
|
|
99
101
|
}
|
|
100
102
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
const proxyParts = proxy.split(':');
|
|
104
|
+
const proxyEnd = parseInt(proxyParts[proxyParts.length - 1]);
|
|
105
|
+
const proxyStart = proxyParts[proxyParts.length - 2];
|
|
104
106
|
if (!proxyStart.includes(".")) {
|
|
105
107
|
proxyParts.pop();
|
|
106
108
|
proxyParts[proxyParts.length - 1] = crypto.randomInt(parseInt(proxyStart), proxyEnd + 1).toString();
|
|
107
109
|
}
|
|
108
110
|
return protocol + "://" + proxyParts.join(':');
|
|
109
111
|
}
|
|
110
|
-
export function proxyObject() {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
export function proxyObject(...args) {
|
|
113
|
+
let proxy = formatProxy(...args);
|
|
114
|
+
const splitByProtocol = proxy.split('://');
|
|
115
|
+
const splitById = splitByProtocol[splitByProtocol.length - 1].split('@');
|
|
116
|
+
const splitByConn = splitById[splitById.length - 1].split(':');
|
|
115
117
|
proxy = {
|
|
116
118
|
protocol: splitByProtocol[0],
|
|
117
119
|
host: splitByConn[0],
|
|
118
120
|
port: parseInt(splitByConn[1])
|
|
119
121
|
};
|
|
120
122
|
if (1 < splitById.length) {
|
|
121
|
-
|
|
123
|
+
const splitByAuth = splitById[0].split(':');
|
|
122
124
|
proxy.auth = {
|
|
123
125
|
username: splitByAuth[0],
|
|
124
126
|
password: splitByAuth[1]
|
|
@@ -127,7 +129,7 @@ export function proxyObject() {
|
|
|
127
129
|
return proxy;
|
|
128
130
|
}
|
|
129
131
|
export function proxyValue(proxies) {
|
|
130
|
-
|
|
132
|
+
let proxy;
|
|
131
133
|
proxies = proxies || "";
|
|
132
134
|
proxies = splitClear(proxies);
|
|
133
135
|
if (proxies.length < 1) return null;
|
|
@@ -135,4 +137,28 @@ export function proxyValue(proxies) {
|
|
|
135
137
|
proxy = formatProxy(proxy);
|
|
136
138
|
proxy = proxy.replace("{SESSION}", tokenHex(8));
|
|
137
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');
|
|
138
164
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melperjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.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
|
}
|