melperjs 9.1.0 → 11.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 +16 -3
- package/lib/cjs/index.js +56 -4
- package/lib/cjs/node.js +26 -2
- package/lib/esm/index.js +50 -3
- package/lib/esm/node.js +24 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,9 +34,18 @@ try {
|
|
|
34
34
|
await helper.promiseTimeout(1000, helper.sleepMs(2000));
|
|
35
35
|
} catch (e) {
|
|
36
36
|
console.error(e.message);
|
|
37
|
-
console.log("Timeout, Internal Error ?", helper.
|
|
37
|
+
console.log("Timeout, Internal Error ?", helper.isIntlHttpError(e));
|
|
38
38
|
}
|
|
39
|
-
helper.
|
|
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"));
|
|
40
49
|
console.log(helper.splitClear(`
|
|
41
50
|
2.satır
|
|
42
51
|
|
|
@@ -88,8 +97,10 @@ console.log(helper.objectStringify({
|
|
|
88
97
|
f: [3, 4, 5],
|
|
89
98
|
g: false
|
|
90
99
|
}));
|
|
100
|
+
console.log(helper.modifyObjectKeys({"A": "B"}, (key) => key.toLowerCase()));
|
|
91
101
|
console.log(helper.limitString("LONG TEXT", 7));
|
|
92
102
|
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
103
|
+
console.log(helper.shuffleString("ABC123"));
|
|
93
104
|
console.log(helper.randomString(32, true, true));
|
|
94
105
|
console.log(helper.randomHex(8));
|
|
95
106
|
console.log(helper.randomInteger(100, 1000));
|
|
@@ -101,10 +112,12 @@ console.log(nodeHelper.tokenHex(8));
|
|
|
101
112
|
console.log(nodeHelper.tokenUuid(true));
|
|
102
113
|
console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
103
114
|
console.log(nodeHelper.tokenElement(["vA", "vB", "vC"]));
|
|
104
|
-
console.log(nodeHelper.
|
|
115
|
+
console.log(nodeHelper.hash("md5", "data"));
|
|
105
116
|
const password = nodeHelper.hashBcrypt("plain", "encryptionKey");
|
|
106
117
|
console.log(password)
|
|
107
118
|
console.log("passwordHash verified ?", nodeHelper.verifyBcrypt("plain", password, "encryptionKey"));
|
|
119
|
+
console.log(await nodeHelper.executeCommand("python --version"));
|
|
120
|
+
console.log(helper.indexByTime(5));
|
|
108
121
|
const cookies = helper.cookieDict(await axios.get("https://google.com"));
|
|
109
122
|
console.log(cookies);
|
|
110
123
|
console.log(helper.cookieHeader(cookies));
|
package/lib/cjs/index.js
CHANGED
|
@@ -10,9 +10,12 @@ exports.cookieDict = cookieDict;
|
|
|
10
10
|
exports.cookieHeader = cookieHeader;
|
|
11
11
|
exports.findKeyNode = findKeyNode;
|
|
12
12
|
exports.forever = forever;
|
|
13
|
-
exports.
|
|
13
|
+
exports.indexByTime = indexByTime;
|
|
14
14
|
exports.isIntlHttpCode = isIntlHttpCode;
|
|
15
|
+
exports.isIntlHttpError = isIntlHttpError;
|
|
16
|
+
exports.isValidURL = isValidURL;
|
|
15
17
|
exports.limitString = limitString;
|
|
18
|
+
exports.modifyObjectKeys = modifyObjectKeys;
|
|
16
19
|
exports.objectStringify = objectStringify;
|
|
17
20
|
exports.parseIntFromObj = parseIntFromObj;
|
|
18
21
|
exports.parseNumFromObj = parseNumFromObj;
|
|
@@ -25,7 +28,9 @@ exports.randomInteger = randomInteger;
|
|
|
25
28
|
exports.randomString = randomString;
|
|
26
29
|
exports.randomUuid = randomUuid;
|
|
27
30
|
exports.randomWeighted = randomWeighted;
|
|
31
|
+
exports.retryFn = retryFn;
|
|
28
32
|
exports.safeString = safeString;
|
|
33
|
+
exports.shuffleString = shuffleString;
|
|
29
34
|
exports.sleep = sleep;
|
|
30
35
|
exports.sleepMs = sleepMs;
|
|
31
36
|
exports.splitClear = splitClear;
|
|
@@ -36,6 +41,7 @@ var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
|
|
|
36
41
|
var _camelCase = _interopRequireDefault(require("lodash/camelCase.js"));
|
|
37
42
|
var _upperFirst = _interopRequireDefault(require("lodash/upperFirst.js"));
|
|
38
43
|
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty.js"));
|
|
44
|
+
var _shuffle = _interopRequireDefault(require("lodash/shuffle.js"));
|
|
39
45
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
40
46
|
const CONSTANTS = exports.CONSTANTS = {
|
|
41
47
|
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
@@ -97,6 +103,28 @@ function promiseTimeout(milliseconds, promise) {
|
|
|
97
103
|
function promiseSilent(promise) {
|
|
98
104
|
return promise.then(() => {}).catch(() => {});
|
|
99
105
|
}
|
|
106
|
+
async function retryFn(fn, retries, errorFn = null) {
|
|
107
|
+
let result = null;
|
|
108
|
+
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
109
|
+
try {
|
|
110
|
+
result = await fn();
|
|
111
|
+
return result;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
errorFn?.(attempt, error, result);
|
|
114
|
+
if (attempt >= retries) {
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function isValidURL(url) {
|
|
121
|
+
try {
|
|
122
|
+
new URL(url);
|
|
123
|
+
return true;
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
100
128
|
function splitClear(rawText, separator = null) {
|
|
101
129
|
separator = separator ?? /\r?\n/;
|
|
102
130
|
return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
|
|
@@ -166,6 +194,12 @@ function objectStringify(obj) {
|
|
|
166
194
|
}
|
|
167
195
|
return obj;
|
|
168
196
|
}
|
|
197
|
+
function modifyObjectKeys(obj, callFn) {
|
|
198
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
199
|
+
acc[callFn(key)] = obj[key];
|
|
200
|
+
return acc;
|
|
201
|
+
}, {});
|
|
202
|
+
}
|
|
169
203
|
function limitString(str, limit = 35, omission = "...") {
|
|
170
204
|
str = str || "";
|
|
171
205
|
if (str.length <= limit) {
|
|
@@ -182,6 +216,11 @@ function safeString(str) {
|
|
|
182
216
|
stripIgnoreTagBody: ["script"]
|
|
183
217
|
});
|
|
184
218
|
}
|
|
219
|
+
function shuffleString(str) {
|
|
220
|
+
const collection = str.split('');
|
|
221
|
+
const shuffled = (0, _shuffle.default)(collection);
|
|
222
|
+
return shuffled.join('');
|
|
223
|
+
}
|
|
185
224
|
function randomString(length, useNumbers = true, useUppercase = false) {
|
|
186
225
|
let characters = CONSTANTS.LOWER_CASE;
|
|
187
226
|
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
@@ -255,6 +294,18 @@ function randomElement(obj) {
|
|
|
255
294
|
return obj[randomElement(Object.keys(obj))];
|
|
256
295
|
}
|
|
257
296
|
}
|
|
297
|
+
function indexByTime(index) {
|
|
298
|
+
const date = new Date();
|
|
299
|
+
const hour = date.getHours();
|
|
300
|
+
const minute = date.getMinutes();
|
|
301
|
+
if (hour < 20) {
|
|
302
|
+
return (index + hour) % 10;
|
|
303
|
+
} else {
|
|
304
|
+
const totalMinutes = (hour - 20) * 60 + minute;
|
|
305
|
+
const minuteIndex = Math.floor(totalMinutes / 24);
|
|
306
|
+
return (index + minuteIndex) % 10;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
258
309
|
function cookieDict(res, decodeValues = false) {
|
|
259
310
|
let dict = {};
|
|
260
311
|
const cookies = _setCookieParser.default.parse(res, {
|
|
@@ -269,8 +320,9 @@ function cookieHeader(cookieDict) {
|
|
|
269
320
|
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
270
321
|
}
|
|
271
322
|
function isIntlHttpCode(httpCode) {
|
|
272
|
-
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
323
|
+
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
273
324
|
}
|
|
274
|
-
function
|
|
275
|
-
|
|
325
|
+
function isIntlHttpError(e) {
|
|
326
|
+
const message = e?.message?.toLowerCase?.() || "";
|
|
327
|
+
return message.includes("timeout") || message.includes("aborted") || message.includes("socket hang") || message.includes("proxy") || message.includes("tls connection") || message.includes("payment") || message.includes("expectation") || isIntlHttpCode(e?.response?.status);
|
|
276
328
|
}
|
package/lib/cjs/node.js
CHANGED
|
@@ -4,8 +4,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.createNumDir = createNumDir;
|
|
7
|
+
exports.executeCommand = executeCommand;
|
|
7
8
|
exports.formatProxy = formatProxy;
|
|
8
9
|
exports.getVersion = getVersion;
|
|
10
|
+
exports.hash = hash;
|
|
9
11
|
exports.hashBcrypt = hashBcrypt;
|
|
10
12
|
exports.md5 = md5;
|
|
11
13
|
exports.proxyObject = proxyObject;
|
|
@@ -13,6 +15,7 @@ exports.proxyValue = proxyValue;
|
|
|
13
15
|
exports.readJsonFile = readJsonFile;
|
|
14
16
|
exports.readJsonFileSync = readJsonFileSync;
|
|
15
17
|
exports.serverIp = serverIp;
|
|
18
|
+
exports.sha256 = sha256;
|
|
16
19
|
exports.tokenElement = tokenElement;
|
|
17
20
|
exports.tokenHex = tokenHex;
|
|
18
21
|
exports.tokenString = tokenString;
|
|
@@ -65,6 +68,19 @@ function tokenElement(obj) {
|
|
|
65
68
|
return obj[tokenElement(Object.keys(obj))];
|
|
66
69
|
}
|
|
67
70
|
}
|
|
71
|
+
function executeCommand(command) {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
(0, _child_process.exec)(command, (error, stdout, stderr) => {
|
|
74
|
+
if (error) {
|
|
75
|
+
reject(error);
|
|
76
|
+
} else if (stderr) {
|
|
77
|
+
reject(stderr);
|
|
78
|
+
} else {
|
|
79
|
+
resolve(stdout.trim());
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
68
84
|
function serverIp() {
|
|
69
85
|
const interfaces = (0, _os.networkInterfaces)();
|
|
70
86
|
for (const devName in interfaces) {
|
|
@@ -96,14 +112,22 @@ function createNumDir(mainDirectory) {
|
|
|
96
112
|
});
|
|
97
113
|
for (let i = 0; i <= 9; i++) {
|
|
98
114
|
try {
|
|
99
|
-
_fs.default.mkdirSync(_path.default.join(mainDirectory, i.toString())
|
|
115
|
+
_fs.default.mkdirSync(_path.default.join(mainDirectory, i.toString()), {
|
|
116
|
+
recursive: true
|
|
117
|
+
});
|
|
100
118
|
} catch (e) {
|
|
101
119
|
console.error(`createNumDir:${i}`, e.message);
|
|
102
120
|
}
|
|
103
121
|
}
|
|
104
122
|
}
|
|
123
|
+
function hash(algorithm, data) {
|
|
124
|
+
return _crypto.default.createHash(algorithm).update(data).digest("hex");
|
|
125
|
+
}
|
|
105
126
|
function md5(data) {
|
|
106
|
-
return
|
|
127
|
+
return hash("md5", data);
|
|
128
|
+
}
|
|
129
|
+
function sha256(data) {
|
|
130
|
+
return hash("sha256", data);
|
|
107
131
|
}
|
|
108
132
|
function hashBcrypt(plainText, encryptionKey = "") {
|
|
109
133
|
return _bcryptjs.default.hashSync(plainText + encryptionKey, _bcryptjs.default.genSaltSync(10));
|
package/lib/esm/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import setCookieParser from "set-cookie-parser";
|
|
|
3
3
|
import camelCase from "lodash/camelCase.js";
|
|
4
4
|
import upperFirst from "lodash/upperFirst.js";
|
|
5
5
|
import isEmpty from "lodash/isEmpty.js";
|
|
6
|
+
import shuffle from "lodash/shuffle.js";
|
|
6
7
|
export const CONSTANTS = {
|
|
7
8
|
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
8
9
|
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
@@ -63,6 +64,28 @@ export function promiseTimeout(milliseconds, promise) {
|
|
|
63
64
|
export function promiseSilent(promise) {
|
|
64
65
|
return promise.then(() => {}).catch(() => {});
|
|
65
66
|
}
|
|
67
|
+
export async function retryFn(fn, retries, errorFn = null) {
|
|
68
|
+
let result = null;
|
|
69
|
+
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
70
|
+
try {
|
|
71
|
+
result = await fn();
|
|
72
|
+
return result;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
errorFn?.(attempt, error, result);
|
|
75
|
+
if (attempt >= retries) {
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export function isValidURL(url) {
|
|
82
|
+
try {
|
|
83
|
+
new URL(url);
|
|
84
|
+
return true;
|
|
85
|
+
} catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
66
89
|
export function splitClear(rawText, separator = null) {
|
|
67
90
|
separator = separator ?? /\r?\n/;
|
|
68
91
|
return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
|
|
@@ -132,6 +155,12 @@ export function objectStringify(obj) {
|
|
|
132
155
|
}
|
|
133
156
|
return obj;
|
|
134
157
|
}
|
|
158
|
+
export function modifyObjectKeys(obj, callFn) {
|
|
159
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
160
|
+
acc[callFn(key)] = obj[key];
|
|
161
|
+
return acc;
|
|
162
|
+
}, {});
|
|
163
|
+
}
|
|
135
164
|
export function limitString(str, limit = 35, omission = "...") {
|
|
136
165
|
str = str || "";
|
|
137
166
|
if (str.length <= limit) {
|
|
@@ -148,6 +177,11 @@ export function safeString(str) {
|
|
|
148
177
|
stripIgnoreTagBody: ["script"]
|
|
149
178
|
});
|
|
150
179
|
}
|
|
180
|
+
export function shuffleString(str) {
|
|
181
|
+
const collection = str.split('');
|
|
182
|
+
const shuffled = shuffle(collection);
|
|
183
|
+
return shuffled.join('');
|
|
184
|
+
}
|
|
151
185
|
export function randomString(length, useNumbers = true, useUppercase = false) {
|
|
152
186
|
let characters = CONSTANTS.LOWER_CASE;
|
|
153
187
|
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
@@ -221,6 +255,18 @@ export function randomElement(obj) {
|
|
|
221
255
|
return obj[randomElement(Object.keys(obj))];
|
|
222
256
|
}
|
|
223
257
|
}
|
|
258
|
+
export function indexByTime(index) {
|
|
259
|
+
const date = new Date();
|
|
260
|
+
const hour = date.getHours();
|
|
261
|
+
const minute = date.getMinutes();
|
|
262
|
+
if (hour < 20) {
|
|
263
|
+
return (index + hour) % 10;
|
|
264
|
+
} else {
|
|
265
|
+
const totalMinutes = (hour - 20) * 60 + minute;
|
|
266
|
+
const minuteIndex = Math.floor(totalMinutes / 24);
|
|
267
|
+
return (index + minuteIndex) % 10;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
224
270
|
export function cookieDict(res, decodeValues = false) {
|
|
225
271
|
let dict = {};
|
|
226
272
|
const cookies = setCookieParser.parse(res, {
|
|
@@ -235,8 +281,9 @@ export function cookieHeader(cookieDict) {
|
|
|
235
281
|
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
236
282
|
}
|
|
237
283
|
export function isIntlHttpCode(httpCode) {
|
|
238
|
-
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
284
|
+
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
239
285
|
}
|
|
240
|
-
export function
|
|
241
|
-
|
|
286
|
+
export function isIntlHttpError(e) {
|
|
287
|
+
const message = e?.message?.toLowerCase?.() || "";
|
|
288
|
+
return message.includes("timeout") || message.includes("aborted") || message.includes("socket hang") || message.includes("proxy") || message.includes("tls connection") || message.includes("payment") || message.includes("expectation") || isIntlHttpCode(e?.response?.status);
|
|
242
289
|
}
|
package/lib/esm/node.js
CHANGED
|
@@ -3,7 +3,7 @@ import { promises as fsp } from "fs";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import crypto from "crypto";
|
|
5
5
|
import { networkInterfaces } from "os";
|
|
6
|
-
import { execSync } from "child_process";
|
|
6
|
+
import { exec, execSync } from "child_process";
|
|
7
7
|
import bcrypt from "bcryptjs";
|
|
8
8
|
import { CONSTANTS, randomWeighted, splitClear } from "./index.js";
|
|
9
9
|
export function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
@@ -40,6 +40,19 @@ export function tokenElement(obj) {
|
|
|
40
40
|
return obj[tokenElement(Object.keys(obj))];
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
export function executeCommand(command) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
exec(command, (error, stdout, stderr) => {
|
|
46
|
+
if (error) {
|
|
47
|
+
reject(error);
|
|
48
|
+
} else if (stderr) {
|
|
49
|
+
reject(stderr);
|
|
50
|
+
} else {
|
|
51
|
+
resolve(stdout.trim());
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
43
56
|
export function serverIp() {
|
|
44
57
|
const interfaces = networkInterfaces();
|
|
45
58
|
for (const devName in interfaces) {
|
|
@@ -71,14 +84,22 @@ export function createNumDir(mainDirectory) {
|
|
|
71
84
|
});
|
|
72
85
|
for (let i = 0; i <= 9; i++) {
|
|
73
86
|
try {
|
|
74
|
-
fs.mkdirSync(path.join(mainDirectory, i.toString())
|
|
87
|
+
fs.mkdirSync(path.join(mainDirectory, i.toString()), {
|
|
88
|
+
recursive: true
|
|
89
|
+
});
|
|
75
90
|
} catch (e) {
|
|
76
91
|
console.error(`createNumDir:${i}`, e.message);
|
|
77
92
|
}
|
|
78
93
|
}
|
|
79
94
|
}
|
|
95
|
+
export function hash(algorithm, data) {
|
|
96
|
+
return crypto.createHash(algorithm).update(data).digest("hex");
|
|
97
|
+
}
|
|
80
98
|
export function md5(data) {
|
|
81
|
-
return
|
|
99
|
+
return hash("md5", data);
|
|
100
|
+
}
|
|
101
|
+
export function sha256(data) {
|
|
102
|
+
return hash("sha256", data);
|
|
82
103
|
}
|
|
83
104
|
export function hashBcrypt(plainText, encryptionKey = "") {
|
|
84
105
|
return bcrypt.hashSync(plainText + encryptionKey, bcrypt.genSaltSync(10));
|