melperjs 14.1.0 → 16.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/LICENSE +20 -20
- package/README.md +22 -21
- package/docs/general.md +363 -0
- package/docs/index.md +27 -0
- package/docs/node.md +285 -0
- package/lib/cjs/index.cjs +351 -0
- package/lib/cjs/node.cjs +301 -0
- package/lib/esm/index.mjs +306 -0
- package/lib/esm/node.mjs +268 -0
- package/package.json +74 -55
- package/lib/cjs/index.js +0 -386
- package/lib/cjs/node.js +0 -250
- package/lib/cjs/package.json +0 -3
- package/lib/esm/index.js +0 -341
- package/lib/esm/node.js +0 -221
package/lib/cjs/index.js
DELETED
|
@@ -1,386 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.CONSTANTS = void 0;
|
|
7
|
-
exports.Exception = Exception;
|
|
8
|
-
exports.checkEmpty = checkEmpty;
|
|
9
|
-
exports.cookieDict = cookieDict;
|
|
10
|
-
exports.cookieHeader = cookieHeader;
|
|
11
|
-
exports.cookieStringToObject = cookieStringToObject;
|
|
12
|
-
exports.findKeyNode = findKeyNode;
|
|
13
|
-
exports.flipObject = flipObject;
|
|
14
|
-
exports.forever = forever;
|
|
15
|
-
exports.getResponseError = getResponseError;
|
|
16
|
-
exports.indexByTime = indexByTime;
|
|
17
|
-
exports.isInt32 = isInt32;
|
|
18
|
-
exports.isIntlHttpCode = isIntlHttpCode;
|
|
19
|
-
exports.isIntlHttpError = isIntlHttpError;
|
|
20
|
-
exports.isValidURL = isValidURL;
|
|
21
|
-
exports.limitString = limitString;
|
|
22
|
-
exports.modifyObjectKeys = modifyObjectKeys;
|
|
23
|
-
exports.objectStringify = objectStringify;
|
|
24
|
-
exports.parseIntFromObj = parseIntFromObj;
|
|
25
|
-
exports.parseNumFromObj = parseNumFromObj;
|
|
26
|
-
exports.pascalCase = pascalCase;
|
|
27
|
-
exports.promiseSilent = promiseSilent;
|
|
28
|
-
exports.promiseTimeout = promiseTimeout;
|
|
29
|
-
exports.randomElement = randomElement;
|
|
30
|
-
exports.randomHex = randomHex;
|
|
31
|
-
exports.randomInteger = randomInteger;
|
|
32
|
-
exports.randomString = randomString;
|
|
33
|
-
exports.randomUuid = randomUuid;
|
|
34
|
-
exports.randomWeighted = randomWeighted;
|
|
35
|
-
exports.retryFn = retryFn;
|
|
36
|
-
exports.safeString = safeString;
|
|
37
|
-
exports.shuffleObject = shuffleObject;
|
|
38
|
-
exports.shuffleString = shuffleString;
|
|
39
|
-
exports.sleep = sleep;
|
|
40
|
-
exports.sleepMs = sleepMs;
|
|
41
|
-
exports.splitClear = splitClear;
|
|
42
|
-
exports.time = time;
|
|
43
|
-
exports.titleCase = titleCase;
|
|
44
|
-
exports.waitForProperty = waitForProperty;
|
|
45
|
-
var _xss = _interopRequireDefault(require("xss"));
|
|
46
|
-
var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
|
|
47
|
-
var _camelCase = _interopRequireDefault(require("lodash/camelCase.js"));
|
|
48
|
-
var _upperFirst = _interopRequireDefault(require("lodash/upperFirst.js"));
|
|
49
|
-
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty.js"));
|
|
50
|
-
var _shuffle = _interopRequireDefault(require("lodash/shuffle.js"));
|
|
51
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
52
|
-
const CONSTANTS = exports.CONSTANTS = {
|
|
53
|
-
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
54
|
-
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
55
|
-
HEXADECIMAL: "0123456789abcdef",
|
|
56
|
-
NUMBERS: "0123456789",
|
|
57
|
-
INT32_MIN: -2147483648,
|
|
58
|
-
INT32_MAX: 2147483647
|
|
59
|
-
};
|
|
60
|
-
function Exception(message, response = {}, name = null) {
|
|
61
|
-
const error = new Error(message);
|
|
62
|
-
error.name = name || "Exception";
|
|
63
|
-
error.response = response;
|
|
64
|
-
if (checkEmpty(response)) {
|
|
65
|
-
error.response = {};
|
|
66
|
-
}
|
|
67
|
-
if (!error.response?.status) {
|
|
68
|
-
error.response.status = 400;
|
|
69
|
-
}
|
|
70
|
-
return error;
|
|
71
|
-
}
|
|
72
|
-
async function forever(cooldown, onSuccess, onError = null, onCompleted = null) {
|
|
73
|
-
const checkCooldown = value => value && !isNaN(value) && value > 0;
|
|
74
|
-
if (!checkCooldown(cooldown)) throw new Error("Cooldown must be a positive number");
|
|
75
|
-
while (true) {
|
|
76
|
-
try {
|
|
77
|
-
const value = await onSuccess();
|
|
78
|
-
if (checkCooldown(value)) cooldown = value;
|
|
79
|
-
} catch (e) {
|
|
80
|
-
const value = onError && (await onError(e));
|
|
81
|
-
if (checkCooldown(value)) cooldown = value;
|
|
82
|
-
} finally {
|
|
83
|
-
const value = onCompleted && (await onCompleted());
|
|
84
|
-
if (checkCooldown(value)) cooldown = value;
|
|
85
|
-
await sleepMs(cooldown);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function time() {
|
|
90
|
-
return Math.floor(Date.now() / 1000);
|
|
91
|
-
}
|
|
92
|
-
async function sleepMs(milliseconds) {
|
|
93
|
-
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
94
|
-
}
|
|
95
|
-
async function sleep(seconds) {
|
|
96
|
-
return await sleepMs(seconds * 1000);
|
|
97
|
-
}
|
|
98
|
-
function promiseTimeout(milliseconds, promise) {
|
|
99
|
-
return new Promise((resolve, reject) => {
|
|
100
|
-
const timer = setTimeout(() => {
|
|
101
|
-
reject(new Error('Promise timed out after ' + milliseconds + 'ms'));
|
|
102
|
-
}, milliseconds);
|
|
103
|
-
promise.then(value => {
|
|
104
|
-
clearTimeout(timer);
|
|
105
|
-
resolve(value);
|
|
106
|
-
}).catch(reason => {
|
|
107
|
-
clearTimeout(timer);
|
|
108
|
-
reject(reason);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
function promiseSilent(promise) {
|
|
113
|
-
return promise.then(() => {}).catch(() => {});
|
|
114
|
-
}
|
|
115
|
-
async function retryFn(fn, retries, errorFn = null) {
|
|
116
|
-
let result = null;
|
|
117
|
-
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
118
|
-
try {
|
|
119
|
-
result = await fn();
|
|
120
|
-
return result;
|
|
121
|
-
} catch (error) {
|
|
122
|
-
errorFn && (await errorFn(attempt, error, result));
|
|
123
|
-
if (attempt >= retries) {
|
|
124
|
-
throw error;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function isValidURL(url) {
|
|
130
|
-
try {
|
|
131
|
-
new URL(url);
|
|
132
|
-
return true;
|
|
133
|
-
} catch {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
function splitClear(rawText, separator = null) {
|
|
138
|
-
separator = separator ?? /\r?\n/;
|
|
139
|
-
return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
|
|
140
|
-
}
|
|
141
|
-
function checkEmpty(value) {
|
|
142
|
-
if (typeof value === "number") {
|
|
143
|
-
return value === 0;
|
|
144
|
-
} else {
|
|
145
|
-
return (0, _isEmpty.default)(value);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
function pascalCase(str) {
|
|
149
|
-
return (0, _upperFirst.default)((0, _camelCase.default)(str));
|
|
150
|
-
}
|
|
151
|
-
function titleCase(str, separator = " ") {
|
|
152
|
-
str = str || "";
|
|
153
|
-
const words = str.split(separator);
|
|
154
|
-
return words.map(word => (0, _upperFirst.default)(word)).join(separator);
|
|
155
|
-
}
|
|
156
|
-
function isInt32(value) {
|
|
157
|
-
return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
|
|
158
|
-
}
|
|
159
|
-
function parseNumFromObj(obj) {
|
|
160
|
-
for (let key in obj) {
|
|
161
|
-
let value = obj[key];
|
|
162
|
-
let number = parseFloat(value);
|
|
163
|
-
if (typeof value === 'string' && !isNaN(number) && !value.includes("_")) {
|
|
164
|
-
value = number;
|
|
165
|
-
}
|
|
166
|
-
obj[key] = value;
|
|
167
|
-
}
|
|
168
|
-
return obj;
|
|
169
|
-
}
|
|
170
|
-
function parseIntFromObj(obj) {
|
|
171
|
-
for (let key in obj) {
|
|
172
|
-
let value = obj[key];
|
|
173
|
-
let number = parseInt(value);
|
|
174
|
-
if (typeof value === 'string' && !isNaN(number) && value.length === number.toString().length) {
|
|
175
|
-
value = number;
|
|
176
|
-
}
|
|
177
|
-
obj[key] = value;
|
|
178
|
-
}
|
|
179
|
-
return obj;
|
|
180
|
-
}
|
|
181
|
-
function findKeyNode(key, node, pair = null) {
|
|
182
|
-
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
183
|
-
return node;
|
|
184
|
-
} else if (typeof node === 'object') {
|
|
185
|
-
for (let index in node) {
|
|
186
|
-
const result = findKeyNode(key, node[index], pair);
|
|
187
|
-
if (result) {
|
|
188
|
-
return result;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
|
|
195
|
-
return new Promise((resolve, reject) => {
|
|
196
|
-
const startTime = Date.now();
|
|
197
|
-
const checkProperty = setInterval(() => {
|
|
198
|
-
if (obj.hasOwnProperty(propertyName)) {
|
|
199
|
-
clearInterval(checkProperty);
|
|
200
|
-
resolve();
|
|
201
|
-
} else if (Date.now() - startTime > timeout) {
|
|
202
|
-
clearInterval(checkProperty);
|
|
203
|
-
reject(new Error(`Property ${propertyName} did not appear within ${timeout} milliseconds`));
|
|
204
|
-
}
|
|
205
|
-
}, interval);
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
function flipObject(obj) {
|
|
209
|
-
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
210
|
-
}
|
|
211
|
-
function shuffleObject(obj) {
|
|
212
|
-
const arr = Object.entries(obj);
|
|
213
|
-
for (let i = arr.length - 1; i > 0; i--) {
|
|
214
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
215
|
-
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
216
|
-
}
|
|
217
|
-
return Object.fromEntries(arr);
|
|
218
|
-
}
|
|
219
|
-
function objectStringify(obj) {
|
|
220
|
-
for (let key in obj) {
|
|
221
|
-
if (obj.hasOwnProperty(key)) {
|
|
222
|
-
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
223
|
-
objectStringify(obj[key]);
|
|
224
|
-
} else {
|
|
225
|
-
if (obj[key]?.toString) {
|
|
226
|
-
obj[key] = obj[key].toString();
|
|
227
|
-
} else {
|
|
228
|
-
obj[key] = String(obj[key]);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return obj;
|
|
234
|
-
}
|
|
235
|
-
function modifyObjectKeys(obj, callFn) {
|
|
236
|
-
return Object.keys(obj).reduce((acc, key) => {
|
|
237
|
-
acc[callFn(key)] = obj[key];
|
|
238
|
-
return acc;
|
|
239
|
-
}, {});
|
|
240
|
-
}
|
|
241
|
-
function limitString(str, limit = 35, omission = "...") {
|
|
242
|
-
str = str || "";
|
|
243
|
-
if (str.length <= limit) {
|
|
244
|
-
return str;
|
|
245
|
-
} else {
|
|
246
|
-
return str.substring(0, limit - omission.length) + omission;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
function safeString(str) {
|
|
250
|
-
str = str || "";
|
|
251
|
-
return (0, _xss.default)(str, {
|
|
252
|
-
whiteList: {},
|
|
253
|
-
stripIgnoreTag: true,
|
|
254
|
-
stripIgnoreTagBody: ["script"]
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
function shuffleString(str) {
|
|
258
|
-
const collection = str.split('');
|
|
259
|
-
const shuffled = (0, _shuffle.default)(collection);
|
|
260
|
-
return shuffled.join('');
|
|
261
|
-
}
|
|
262
|
-
function randomString(length, useNumbers = true, useUppercase = false) {
|
|
263
|
-
let characters = CONSTANTS.LOWER_CASE;
|
|
264
|
-
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
265
|
-
if (useNumbers) characters += CONSTANTS.NUMBERS;
|
|
266
|
-
let randomString = '';
|
|
267
|
-
for (let i = 0; i < length; i++) {
|
|
268
|
-
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
269
|
-
randomString += characters[randomIndex];
|
|
270
|
-
}
|
|
271
|
-
return randomString;
|
|
272
|
-
}
|
|
273
|
-
function randomHex(length) {
|
|
274
|
-
let result = '';
|
|
275
|
-
for (let i = 0; i < length; i++) {
|
|
276
|
-
const randomIndex = Math.floor(Math.random() * CONSTANTS.HEXADECIMAL.length);
|
|
277
|
-
result += CONSTANTS.HEXADECIMAL[randomIndex];
|
|
278
|
-
}
|
|
279
|
-
return result;
|
|
280
|
-
}
|
|
281
|
-
function randomInteger(min, max, callback) {
|
|
282
|
-
const minNotSpecified = typeof max === 'undefined' || typeof max === 'function';
|
|
283
|
-
if (minNotSpecified) {
|
|
284
|
-
callback = max;
|
|
285
|
-
max = min;
|
|
286
|
-
min = 0;
|
|
287
|
-
}
|
|
288
|
-
const isSync = typeof callback === 'undefined';
|
|
289
|
-
if (typeof min !== 'number' || typeof max !== 'number') {
|
|
290
|
-
throw new Error('min and max must be numerical values');
|
|
291
|
-
}
|
|
292
|
-
if (max <= min) {
|
|
293
|
-
throw new Error('max must be greater than min');
|
|
294
|
-
}
|
|
295
|
-
const randomNumber = Math.floor(Math.random() * (max - min)) + min;
|
|
296
|
-
if (isSync) {
|
|
297
|
-
return randomNumber;
|
|
298
|
-
} else {
|
|
299
|
-
if (typeof callback !== 'function') {
|
|
300
|
-
throw new Error('callback must be a function');
|
|
301
|
-
}
|
|
302
|
-
callback(randomNumber);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
function randomUuid(useDashes = true) {
|
|
306
|
-
let d = Date.now();
|
|
307
|
-
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
308
|
-
const r = (d + Math.random() * 16) % 16 | 0;
|
|
309
|
-
d = Math.floor(d / 16);
|
|
310
|
-
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16);
|
|
311
|
-
});
|
|
312
|
-
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
313
|
-
}
|
|
314
|
-
function randomWeighted(dict, randomFunc = null) {
|
|
315
|
-
randomFunc = randomFunc || (totalWeight => Math.random() * totalWeight);
|
|
316
|
-
let elements = Object.keys(dict);
|
|
317
|
-
let weights = Object.values(dict);
|
|
318
|
-
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
319
|
-
let randomNum = randomFunc(totalWeight);
|
|
320
|
-
let weightSum = 0;
|
|
321
|
-
for (let i = 0; i < elements.length; i++) {
|
|
322
|
-
weightSum += weights[i];
|
|
323
|
-
if (randomNum <= weightSum) {
|
|
324
|
-
return elements[i];
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
function randomElement(obj) {
|
|
329
|
-
if (Array.isArray(obj)) {
|
|
330
|
-
return obj[Math.floor(Math.random() * obj.length)];
|
|
331
|
-
} else {
|
|
332
|
-
return obj[randomElement(Object.keys(obj))];
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
function indexByTime(index) {
|
|
336
|
-
const date = new Date();
|
|
337
|
-
const hour = date.getHours();
|
|
338
|
-
const minute = date.getMinutes();
|
|
339
|
-
if (hour < 20) {
|
|
340
|
-
return (index + hour) % 10;
|
|
341
|
-
} else {
|
|
342
|
-
const totalMinutes = (hour - 20) * 60 + minute;
|
|
343
|
-
const minuteIndex = Math.floor(totalMinutes / 24);
|
|
344
|
-
return (index + minuteIndex) % 10;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
function cookieDict(res, decodeValues = false) {
|
|
348
|
-
let dict = {};
|
|
349
|
-
const cookies = _setCookieParser.default.parse(res, {
|
|
350
|
-
decodeValues: decodeValues
|
|
351
|
-
});
|
|
352
|
-
for (let cookie of cookies) {
|
|
353
|
-
dict[cookie.name] = cookie.value;
|
|
354
|
-
}
|
|
355
|
-
return dict;
|
|
356
|
-
}
|
|
357
|
-
function cookieHeader(cookieDict) {
|
|
358
|
-
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
359
|
-
}
|
|
360
|
-
function cookieStringToObject(cookieString) {
|
|
361
|
-
const cookies = {};
|
|
362
|
-
if (!cookieString) return cookies;
|
|
363
|
-
cookieString.split(';').forEach(cookie => {
|
|
364
|
-
const [key, ...valueParts] = cookie.trim().split('=');
|
|
365
|
-
if (key) {
|
|
366
|
-
cookies[key.trim()] = valueParts.join('=').trim();
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
return cookies;
|
|
370
|
-
}
|
|
371
|
-
function isIntlHttpCode(httpCode) {
|
|
372
|
-
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
373
|
-
}
|
|
374
|
-
function isIntlHttpError(e) {
|
|
375
|
-
const message = e?.message?.toLowerCase?.() || "";
|
|
376
|
-
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);
|
|
377
|
-
}
|
|
378
|
-
function getResponseError(e, limit = 115) {
|
|
379
|
-
let response;
|
|
380
|
-
if (e?.response?.status && e.response.data) {
|
|
381
|
-
response = `${e.response.status}|${e.response.data}`;
|
|
382
|
-
} else if (e?.response?.data) {
|
|
383
|
-
response = e.response.data;
|
|
384
|
-
}
|
|
385
|
-
return limitString(response || e.message, limit).trim();
|
|
386
|
-
}
|
package/lib/cjs/node.js
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.cleanDirectory = cleanDirectory;
|
|
7
|
-
exports.createNumDir = createNumDir;
|
|
8
|
-
exports.executeCommand = executeCommand;
|
|
9
|
-
exports.formatProxy = formatProxy;
|
|
10
|
-
exports.getVersion = getVersion;
|
|
11
|
-
exports.hash = hash;
|
|
12
|
-
exports.hashBcrypt = hashBcrypt;
|
|
13
|
-
exports.md5 = md5;
|
|
14
|
-
exports.proxyObject = proxyObject;
|
|
15
|
-
exports.proxyValue = proxyValue;
|
|
16
|
-
exports.readJsonFile = readJsonFile;
|
|
17
|
-
exports.readJsonFileSync = readJsonFileSync;
|
|
18
|
-
exports.serverIp = serverIp;
|
|
19
|
-
exports.sha256 = sha256;
|
|
20
|
-
exports.tokenElement = tokenElement;
|
|
21
|
-
exports.tokenHex = tokenHex;
|
|
22
|
-
exports.tokenInteger = tokenInteger;
|
|
23
|
-
exports.tokenString = tokenString;
|
|
24
|
-
exports.tokenUuid = tokenUuid;
|
|
25
|
-
exports.tokenWeighted = tokenWeighted;
|
|
26
|
-
exports.verifyBcrypt = verifyBcrypt;
|
|
27
|
-
exports.writeJsonFile = writeJsonFile;
|
|
28
|
-
exports.writeJsonFileSync = writeJsonFileSync;
|
|
29
|
-
var _fs = _interopRequireWildcard(require("fs"));
|
|
30
|
-
var _path = _interopRequireDefault(require("path"));
|
|
31
|
-
var _crypto = _interopRequireDefault(require("crypto"));
|
|
32
|
-
var _os = require("os");
|
|
33
|
-
var _child_process = require("child_process");
|
|
34
|
-
var _bcryptjs = _interopRequireDefault(require("bcryptjs"));
|
|
35
|
-
var _index = require("./index.js");
|
|
36
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
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); }
|
|
38
|
-
function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
39
|
-
const lowercaseChars = _index.CONSTANTS.LOWER_CASE;
|
|
40
|
-
const uppercaseChars = _index.CONSTANTS.UPPER_CASE;
|
|
41
|
-
const numbers = _index.CONSTANTS.NUMBERS;
|
|
42
|
-
let characters = lowercaseChars;
|
|
43
|
-
if (useUppercase) characters += uppercaseChars;
|
|
44
|
-
if (useNumbers) characters += numbers;
|
|
45
|
-
let randomString = '';
|
|
46
|
-
while (randomString.length < length) {
|
|
47
|
-
const byte = _crypto.default.randomBytes(1)[0];
|
|
48
|
-
const index = byte % characters.length;
|
|
49
|
-
if (byte < 256 - 256 % characters.length) {
|
|
50
|
-
randomString += characters[index];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return randomString;
|
|
54
|
-
}
|
|
55
|
-
function tokenHex(length) {
|
|
56
|
-
return _crypto.default.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
57
|
-
}
|
|
58
|
-
function tokenInteger(min, max) {
|
|
59
|
-
return _crypto.default.randomInt(min, max);
|
|
60
|
-
}
|
|
61
|
-
function tokenUuid(useDashes = true) {
|
|
62
|
-
const uuid = _crypto.default.randomUUID().toString();
|
|
63
|
-
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
64
|
-
}
|
|
65
|
-
function tokenWeighted(dict) {
|
|
66
|
-
return (0, _index.randomWeighted)(dict, _crypto.default.randomInt);
|
|
67
|
-
}
|
|
68
|
-
function tokenElement(obj) {
|
|
69
|
-
if (Array.isArray(obj)) {
|
|
70
|
-
return obj[_crypto.default.randomInt(0, obj.length)];
|
|
71
|
-
} else {
|
|
72
|
-
return obj[tokenElement(Object.keys(obj))];
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function executeCommand(command) {
|
|
76
|
-
return new Promise((resolve, reject) => {
|
|
77
|
-
(0, _child_process.exec)(command, (error, stdout, stderr) => {
|
|
78
|
-
if (error) {
|
|
79
|
-
reject(error);
|
|
80
|
-
} else if (stderr) {
|
|
81
|
-
reject(stderr);
|
|
82
|
-
} else {
|
|
83
|
-
resolve(stdout.trim());
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
function serverIp() {
|
|
89
|
-
const interfaces = (0, _os.networkInterfaces)();
|
|
90
|
-
for (const devName in interfaces) {
|
|
91
|
-
const interfaceValue = interfaces[devName];
|
|
92
|
-
for (let i = 0; i < interfaceValue.length; i++) {
|
|
93
|
-
const alias = interfaceValue[i];
|
|
94
|
-
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.address.startsWith("192.168.") && !alias.internal) return alias.address;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return '127.0.0.1';
|
|
98
|
-
}
|
|
99
|
-
function getVersion() {
|
|
100
|
-
try {
|
|
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);
|
|
106
|
-
const formatDatePart = value => value.toString().padStart(2, '0');
|
|
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());
|
|
112
|
-
return `${year}${month}${day}.${hour}${minute}`;
|
|
113
|
-
} catch {
|
|
114
|
-
return "1.0";
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
function createNumDir(mainDirectory, start = 0, end = 9) {
|
|
118
|
-
_fs.default.mkdirSync(mainDirectory, {
|
|
119
|
-
recursive: true
|
|
120
|
-
});
|
|
121
|
-
for (let i = start; i <= end; i++) {
|
|
122
|
-
try {
|
|
123
|
-
_fs.default.mkdirSync(_path.default.join(mainDirectory, i.toString()), {
|
|
124
|
-
recursive: true
|
|
125
|
-
});
|
|
126
|
-
} catch (e) {
|
|
127
|
-
console.error(`createNumDir:${i}`, e.message);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
function hash(algorithm, data) {
|
|
132
|
-
return _crypto.default.createHash(algorithm).update(data).digest("hex");
|
|
133
|
-
}
|
|
134
|
-
function md5(data) {
|
|
135
|
-
return hash("md5", data);
|
|
136
|
-
}
|
|
137
|
-
function sha256(data) {
|
|
138
|
-
return hash("sha256", data);
|
|
139
|
-
}
|
|
140
|
-
function hashBcrypt(plainText, encryptionKey = "", rounds = 12) {
|
|
141
|
-
return _bcryptjs.default.hashSync(plainText + encryptionKey, _bcryptjs.default.genSaltSync(rounds));
|
|
142
|
-
}
|
|
143
|
-
function verifyBcrypt(plainText, hash, encryptionKey = "") {
|
|
144
|
-
return _bcryptjs.default.compareSync(plainText + encryptionKey, hash);
|
|
145
|
-
}
|
|
146
|
-
function formatProxy(proxy, protocol = "http") {
|
|
147
|
-
proxy = proxy.trim();
|
|
148
|
-
const splitByProtocol = proxy.split("://");
|
|
149
|
-
if (1 < splitByProtocol.length) protocol = splitByProtocol[0];
|
|
150
|
-
proxy = splitByProtocol[splitByProtocol.length - 1];
|
|
151
|
-
if (!proxy.includes("@")) {
|
|
152
|
-
const proxyParts = proxy.split(":");
|
|
153
|
-
if (4 <= proxyParts.length) {
|
|
154
|
-
proxy = `${proxyParts[proxyParts.length - 2]}:${proxyParts[proxyParts.length - 1]}@`;
|
|
155
|
-
proxyParts.pop();
|
|
156
|
-
proxyParts.pop();
|
|
157
|
-
proxy += proxyParts.join(":");
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
const proxyParts = proxy.split(':');
|
|
161
|
-
const proxyEnd = parseInt(proxyParts[proxyParts.length - 1]);
|
|
162
|
-
const proxyStart = proxyParts[proxyParts.length - 2];
|
|
163
|
-
if (!proxyStart.includes(".")) {
|
|
164
|
-
proxyParts.pop();
|
|
165
|
-
proxyParts[proxyParts.length - 1] = _crypto.default.randomInt(parseInt(proxyStart), proxyEnd + 1).toString();
|
|
166
|
-
}
|
|
167
|
-
return protocol + "://" + proxyParts.join(':');
|
|
168
|
-
}
|
|
169
|
-
function proxyObject(...args) {
|
|
170
|
-
let proxy = formatProxy(...args);
|
|
171
|
-
const splitByProtocol = proxy.split('://');
|
|
172
|
-
const splitById = splitByProtocol[splitByProtocol.length - 1].split('@');
|
|
173
|
-
const splitByConn = splitById[splitById.length - 1].split(':');
|
|
174
|
-
proxy = {
|
|
175
|
-
protocol: splitByProtocol[0],
|
|
176
|
-
host: splitByConn[0],
|
|
177
|
-
port: parseInt(splitByConn[1])
|
|
178
|
-
};
|
|
179
|
-
if (1 < splitById.length) {
|
|
180
|
-
const splitByAuth = splitById[0].split(':');
|
|
181
|
-
proxy.auth = {
|
|
182
|
-
username: splitByAuth[0],
|
|
183
|
-
password: splitByAuth[1]
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
return proxy;
|
|
187
|
-
}
|
|
188
|
-
function proxyValue(proxies) {
|
|
189
|
-
let proxy;
|
|
190
|
-
proxies = proxies || "";
|
|
191
|
-
proxies = (0, _index.splitClear)(proxies);
|
|
192
|
-
if (proxies.length < 1) return null;
|
|
193
|
-
proxy = proxies[_crypto.default.randomInt(0, proxies.length)];
|
|
194
|
-
proxy = formatProxy(proxy);
|
|
195
|
-
proxy = proxy.replace("{SESSION}", tokenHex(8));
|
|
196
|
-
return proxy || null;
|
|
197
|
-
}
|
|
198
|
-
async function readJsonFile(filePath, defaultValue = {}) {
|
|
199
|
-
try {
|
|
200
|
-
const data = await _fs.promises.readFile(filePath, 'utf8');
|
|
201
|
-
return JSON.parse(data);
|
|
202
|
-
} catch (error) {
|
|
203
|
-
return defaultValue;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
function readJsonFileSync(filePath, defaultValue = {}) {
|
|
207
|
-
try {
|
|
208
|
-
const data = _fs.default.readFileSync(filePath, 'utf8');
|
|
209
|
-
return JSON.parse(data);
|
|
210
|
-
} catch (error) {
|
|
211
|
-
return defaultValue;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
async function writeJsonFile(filePath, data) {
|
|
215
|
-
const jsonData = JSON.stringify(data);
|
|
216
|
-
return await _fs.promises.writeFile(filePath, jsonData, 'utf8');
|
|
217
|
-
}
|
|
218
|
-
function writeJsonFileSync(filePath, data) {
|
|
219
|
-
const jsonData = JSON.stringify(data);
|
|
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
|
-
}
|
|
250
|
-
}
|
package/lib/cjs/package.json
DELETED