melperjs 17.0.0 → 17.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 +2 -6
- package/dist/general-CAhFOkv8.cjs +655 -0
- package/dist/general.cjs +44 -0
- package/dist/general.mjs +370 -0
- package/dist/node.cjs +170 -0
- package/dist/node.mjs +142 -0
- package/docs/{docs.md → README.md} +4 -5
- package/docs/{index.md → general.md} +3 -3
- package/docs/node.md +1 -1
- package/package.json +25 -24
- package/lib/cjs/index.cjs +0 -474
- package/lib/cjs/node.cjs +0 -199
- package/lib/esm/index.mjs +0 -426
- package/lib/esm/node.mjs +0 -169
package/dist/general.cjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_general = require("./general-CAhFOkv8.cjs");
|
|
3
|
+
exports.CONSTANTS = require_general.CONSTANTS;
|
|
4
|
+
exports.Exception = require_general.Exception;
|
|
5
|
+
exports.checkEmpty = require_general.checkEmpty;
|
|
6
|
+
exports.coerceObjectIntegers = require_general.coerceObjectIntegers;
|
|
7
|
+
exports.coerceObjectNumbers = require_general.coerceObjectNumbers;
|
|
8
|
+
exports.cookiesFromHeader = require_general.cookiesFromHeader;
|
|
9
|
+
exports.cookiesFromResponse = require_general.cookiesFromResponse;
|
|
10
|
+
exports.cookiesToHeader = require_general.cookiesToHeader;
|
|
11
|
+
exports.findNodeByKey = require_general.findNodeByKey;
|
|
12
|
+
exports.forever = require_general.forever;
|
|
13
|
+
exports.getResponseError = require_general.getResponseError;
|
|
14
|
+
exports.isInt32 = require_general.isInt32;
|
|
15
|
+
exports.isPositiveNumber = require_general.isPositiveNumber;
|
|
16
|
+
exports.isTransientHttpCode = require_general.isTransientHttpCode;
|
|
17
|
+
exports.isValidURL = require_general.isValidURL;
|
|
18
|
+
exports.limitString = require_general.limitString;
|
|
19
|
+
exports.mulberry32 = require_general.mulberry32;
|
|
20
|
+
exports.normalizeProxy = require_general.normalizeProxy;
|
|
21
|
+
exports.objectStringify = require_general.objectStringify;
|
|
22
|
+
exports.parseProxy = require_general.parseProxy;
|
|
23
|
+
exports.pascalCase = require_general.pascalCase;
|
|
24
|
+
exports.promiseSilent = require_general.promiseSilent;
|
|
25
|
+
exports.promiseTimeout = require_general.promiseTimeout;
|
|
26
|
+
exports.proxyValue = require_general.proxyValue;
|
|
27
|
+
exports.randomBoolean = require_general.randomBoolean;
|
|
28
|
+
exports.randomElement = require_general.randomElement;
|
|
29
|
+
exports.randomHex = require_general.randomHex;
|
|
30
|
+
exports.randomInteger = require_general.randomInteger;
|
|
31
|
+
exports.randomString = require_general.randomString;
|
|
32
|
+
exports.randomUuid = require_general.randomUuid;
|
|
33
|
+
exports.randomWeighted = require_general.randomWeighted;
|
|
34
|
+
exports.retry = require_general.retry;
|
|
35
|
+
exports.safeString = require_general.safeString;
|
|
36
|
+
exports.seedHex = require_general.seedHex;
|
|
37
|
+
exports.shuffleObject = require_general.shuffleObject;
|
|
38
|
+
exports.shuffleString = require_general.shuffleString;
|
|
39
|
+
exports.sleep = require_general.sleep;
|
|
40
|
+
exports.sleepMs = require_general.sleepMs;
|
|
41
|
+
exports.splitTrim = require_general.splitTrim;
|
|
42
|
+
exports.time = require_general.time;
|
|
43
|
+
exports.titleCase = require_general.titleCase;
|
|
44
|
+
exports.waitForProperty = require_general.waitForProperty;
|
package/dist/general.mjs
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import xss from "xss";
|
|
2
|
+
import setCookieParser from "set-cookie-parser";
|
|
3
|
+
import camelCase from "lodash/camelCase.js";
|
|
4
|
+
import upperFirst from "lodash/upperFirst.js";
|
|
5
|
+
import isEmpty from "lodash/isEmpty.js";
|
|
6
|
+
import shuffle from "lodash/shuffle.js";
|
|
7
|
+
//#region src/general.js
|
|
8
|
+
const CONSTANTS = {
|
|
9
|
+
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
10
|
+
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
11
|
+
HEXADECIMAL: "0123456789abcdef",
|
|
12
|
+
NUMBERS: "0123456789",
|
|
13
|
+
INT32_MIN: -2147483648,
|
|
14
|
+
INT32_MAX: 2147483647
|
|
15
|
+
};
|
|
16
|
+
const NUMBER_PATTERN = /^-?\d+(\.\d+)?(e[+-]?\d+)?$/i;
|
|
17
|
+
const INTEGER_PATTERN = /^-?\d+$/;
|
|
18
|
+
function Exception(message, response = {}, name = null) {
|
|
19
|
+
const error = new Error(message);
|
|
20
|
+
error.name = name || "Exception";
|
|
21
|
+
error.response = response;
|
|
22
|
+
if (checkEmpty(response)) error.response = {};
|
|
23
|
+
return error;
|
|
24
|
+
}
|
|
25
|
+
function time() {
|
|
26
|
+
return Math.floor(Date.now() / 1e3);
|
|
27
|
+
}
|
|
28
|
+
function sleepMs(milliseconds) {
|
|
29
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
30
|
+
}
|
|
31
|
+
function sleep(seconds) {
|
|
32
|
+
return sleepMs(seconds * 1e3);
|
|
33
|
+
}
|
|
34
|
+
function promiseTimeout(milliseconds, promise) {
|
|
35
|
+
let timer;
|
|
36
|
+
const timeout = new Promise((_, reject) => {
|
|
37
|
+
timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`Promise timed out after ${milliseconds}ms`)), milliseconds);
|
|
38
|
+
});
|
|
39
|
+
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
|
40
|
+
}
|
|
41
|
+
function promiseSilent(promise) {
|
|
42
|
+
return promise?.then(() => {})?.catch(() => {});
|
|
43
|
+
}
|
|
44
|
+
async function forever(delayMs, task, onError = null, onFinally = null) {
|
|
45
|
+
if (!isPositiveNumber(delayMs)) throw new Error("delayMs must be a positive number");
|
|
46
|
+
const update = (value) => {
|
|
47
|
+
if (isPositiveNumber(value)) delayMs = value;
|
|
48
|
+
};
|
|
49
|
+
while (true) try {
|
|
50
|
+
update(await task());
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (onError) update(await onError(error));
|
|
53
|
+
} finally {
|
|
54
|
+
if (onFinally) try {
|
|
55
|
+
update(await onFinally());
|
|
56
|
+
} catch {}
|
|
57
|
+
await sleepMs(delayMs);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function retry(task, maxAttempts = 1, onError = null, { delayMs = 0, backoffFactor = 1 } = {}) {
|
|
61
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) try {
|
|
62
|
+
return await task();
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (onError) await onError(error, attempt);
|
|
65
|
+
if (attempt >= maxAttempts) throw error;
|
|
66
|
+
if (delayMs > 0) await sleepMs(delayMs * backoffFactor ** (attempt - 1));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function isValidURL(url) {
|
|
70
|
+
try {
|
|
71
|
+
new URL(url);
|
|
72
|
+
return true;
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function splitTrim(string, separator = null) {
|
|
78
|
+
return string.split(separator ?? /\r?\n/).map((item) => item.trim()).filter(Boolean);
|
|
79
|
+
}
|
|
80
|
+
function checkEmpty(value) {
|
|
81
|
+
if (typeof value === "number") return value === 0;
|
|
82
|
+
return isEmpty(value);
|
|
83
|
+
}
|
|
84
|
+
function pascalCase(string) {
|
|
85
|
+
return upperFirst(camelCase(string));
|
|
86
|
+
}
|
|
87
|
+
function titleCase(string, separator = " ") {
|
|
88
|
+
return (string || "").split(separator).map(upperFirst).join(separator);
|
|
89
|
+
}
|
|
90
|
+
function isInt32(value) {
|
|
91
|
+
return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
|
|
92
|
+
}
|
|
93
|
+
function isPositiveNumber(value) {
|
|
94
|
+
return Number.isFinite(value) && value > 0;
|
|
95
|
+
}
|
|
96
|
+
function coerceObjectNumbers(object) {
|
|
97
|
+
for (const key of Object.keys(object)) {
|
|
98
|
+
const value = object[key];
|
|
99
|
+
if (typeof value === "string" && NUMBER_PATTERN.test(value)) object[key] = parseFloat(value);
|
|
100
|
+
}
|
|
101
|
+
return object;
|
|
102
|
+
}
|
|
103
|
+
function coerceObjectIntegers(object) {
|
|
104
|
+
for (const key of Object.keys(object)) {
|
|
105
|
+
const value = object[key];
|
|
106
|
+
if (typeof value === "string" && INTEGER_PATTERN.test(value)) object[key] = parseInt(value);
|
|
107
|
+
}
|
|
108
|
+
return object;
|
|
109
|
+
}
|
|
110
|
+
function findNodeByKey(key, node, pair = null) {
|
|
111
|
+
if (node && typeof node === "object") {
|
|
112
|
+
if (Object.hasOwn(node, key) && (pair === null || node[key] === pair)) return node;
|
|
113
|
+
for (const childKey of Object.keys(node)) {
|
|
114
|
+
const result = findNodeByKey(key, node[childKey], pair);
|
|
115
|
+
if (result) return result;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
function waitForProperty(object, property, timeoutMs, interval = 100) {
|
|
121
|
+
return new Promise((resolve, reject) => {
|
|
122
|
+
if (Object.hasOwn(object, property)) {
|
|
123
|
+
resolve(object[property]);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const startTime = Date.now();
|
|
127
|
+
const checkProperty = setInterval(() => {
|
|
128
|
+
if (Object.hasOwn(object, property)) {
|
|
129
|
+
clearInterval(checkProperty);
|
|
130
|
+
resolve(object[property]);
|
|
131
|
+
} else if (Date.now() - startTime >= timeoutMs) {
|
|
132
|
+
clearInterval(checkProperty);
|
|
133
|
+
reject(/* @__PURE__ */ new Error(`Property "${property}" did not appear within ${timeoutMs}ms`));
|
|
134
|
+
}
|
|
135
|
+
}, interval);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function shuffleObject(object) {
|
|
139
|
+
return Object.fromEntries(shuffle(Object.entries(object)));
|
|
140
|
+
}
|
|
141
|
+
function objectStringify(object) {
|
|
142
|
+
for (const key of Object.keys(object)) {
|
|
143
|
+
const value = object[key];
|
|
144
|
+
if (value !== null && typeof value === "object") objectStringify(value);
|
|
145
|
+
else object[key] = String(value);
|
|
146
|
+
}
|
|
147
|
+
return object;
|
|
148
|
+
}
|
|
149
|
+
function limitString(string, limit = 35, omission = "...") {
|
|
150
|
+
string = string || "";
|
|
151
|
+
if (string.length <= limit) return string;
|
|
152
|
+
return string.slice(0, limit - omission.length) + omission;
|
|
153
|
+
}
|
|
154
|
+
function safeString(string) {
|
|
155
|
+
return xss(string || "", {
|
|
156
|
+
whiteList: {},
|
|
157
|
+
stripIgnoreTag: true,
|
|
158
|
+
stripIgnoreTagBody: [
|
|
159
|
+
"script",
|
|
160
|
+
"style",
|
|
161
|
+
"iframe",
|
|
162
|
+
"object",
|
|
163
|
+
"embed",
|
|
164
|
+
"form"
|
|
165
|
+
],
|
|
166
|
+
css: false
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function shuffleString(string) {
|
|
170
|
+
return shuffle(string.split("")).join("");
|
|
171
|
+
}
|
|
172
|
+
function randomBoolean() {
|
|
173
|
+
return Math.random() < .5;
|
|
174
|
+
}
|
|
175
|
+
function randomString(length, useNumbers = true, useUppercase = false) {
|
|
176
|
+
let characters = CONSTANTS.LOWER_CASE;
|
|
177
|
+
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
178
|
+
if (useNumbers) characters += CONSTANTS.NUMBERS;
|
|
179
|
+
let result = "";
|
|
180
|
+
for (let i = 0; i < length; i++) result += characters[Math.random() * characters.length | 0];
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
function randomHex(length) {
|
|
184
|
+
let result = "";
|
|
185
|
+
for (let i = 0; i < length; i++) result += CONSTANTS.HEXADECIMAL[Math.random() * 16 | 0];
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
function randomInteger(min, max = void 0) {
|
|
189
|
+
if (typeof max === "undefined") {
|
|
190
|
+
max = min;
|
|
191
|
+
min = 0;
|
|
192
|
+
}
|
|
193
|
+
if (typeof min !== "number" || typeof max !== "number") throw new Error("min and max must be numerical values");
|
|
194
|
+
if (max <= min) throw new Error("max must be greater than min");
|
|
195
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
196
|
+
}
|
|
197
|
+
function randomUuid(useDashes = true) {
|
|
198
|
+
const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
199
|
+
const r = Math.random() * 16 | 0;
|
|
200
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
201
|
+
});
|
|
202
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
203
|
+
}
|
|
204
|
+
function randomWeighted(object) {
|
|
205
|
+
if (checkEmpty(object)) return void 0;
|
|
206
|
+
const elements = Object.keys(object);
|
|
207
|
+
const weights = Object.values(object);
|
|
208
|
+
const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
209
|
+
const randomNum = Math.random() * totalWeight;
|
|
210
|
+
let weightSum = 0;
|
|
211
|
+
for (let i = 0; i < elements.length; i++) {
|
|
212
|
+
weightSum += weights[i];
|
|
213
|
+
if (randomNum < weightSum) return elements[i];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function randomElement(object) {
|
|
217
|
+
if (checkEmpty(object)) return void 0;
|
|
218
|
+
const values = Array.isArray(object) ? object : Object.values(object);
|
|
219
|
+
if (values.length === 0) return void 0;
|
|
220
|
+
return values[Math.floor(Math.random() * values.length)];
|
|
221
|
+
}
|
|
222
|
+
function mulberry32(seed) {
|
|
223
|
+
if (typeof seed === "string") {
|
|
224
|
+
let h = 0;
|
|
225
|
+
for (let i = 0; i < seed.length; i++) h = Math.imul(h ^ seed.charCodeAt(i), 2654435761);
|
|
226
|
+
seed = h >>> 0;
|
|
227
|
+
}
|
|
228
|
+
return function() {
|
|
229
|
+
seed = seed + 1831565813 | 0;
|
|
230
|
+
let t = seed;
|
|
231
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
232
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
233
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function seedHex(seed, length) {
|
|
237
|
+
const rng = mulberry32(String(seed));
|
|
238
|
+
let result = "";
|
|
239
|
+
while (result.length < length) result += Math.floor(rng() * 4294967296).toString(16).padStart(8, "0");
|
|
240
|
+
return result.slice(0, length);
|
|
241
|
+
}
|
|
242
|
+
function cookiesFromResponse(response, decodeValues = false) {
|
|
243
|
+
const obj = {};
|
|
244
|
+
const cookies = setCookieParser.parse(response, { decodeValues });
|
|
245
|
+
for (const cookie of cookies) obj[cookie.name] = cookie.value;
|
|
246
|
+
return obj;
|
|
247
|
+
}
|
|
248
|
+
function cookiesToHeader(cookies) {
|
|
249
|
+
if (!cookies) return "";
|
|
250
|
+
return Object.entries(cookies).filter(([, value]) => value !== null && value !== void 0).map(([key, value]) => `${key}=${value}`).join("; ");
|
|
251
|
+
}
|
|
252
|
+
function cookiesFromHeader(header) {
|
|
253
|
+
const cookies = {};
|
|
254
|
+
if (!header) return cookies;
|
|
255
|
+
header.split(";").forEach((cookie) => {
|
|
256
|
+
const trimmed = cookie.trim();
|
|
257
|
+
if (!trimmed.includes("=")) return;
|
|
258
|
+
const [key, ...valueParts] = trimmed.split("=");
|
|
259
|
+
const trimmedKey = key.trim();
|
|
260
|
+
if (trimmedKey) cookies[trimmedKey] = valueParts.join("=").trim();
|
|
261
|
+
});
|
|
262
|
+
return cookies;
|
|
263
|
+
}
|
|
264
|
+
function isTransientHttpCode(httpCode) {
|
|
265
|
+
return !httpCode || isNaN(httpCode) || httpCode === 100 || httpCode === 402 || httpCode === 407 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
266
|
+
}
|
|
267
|
+
function getResponseError(error, limit = 200) {
|
|
268
|
+
let response;
|
|
269
|
+
if (error?.response?.status && error.response.data) response = `${error.response.status}|${error.response.data}`;
|
|
270
|
+
else if (error?.response?.data) response = error.response.data;
|
|
271
|
+
return limitString(response || error.message, limit).trim();
|
|
272
|
+
}
|
|
273
|
+
function normalizeProxy(proxy, protocol = "http") {
|
|
274
|
+
proxy = proxy?.trim();
|
|
275
|
+
if (!proxy) return null;
|
|
276
|
+
const schemeMatch = proxy.match(/^([a-z][a-z0-9+.-]*):\/\/(.+)$/i);
|
|
277
|
+
if (schemeMatch) {
|
|
278
|
+
protocol = schemeMatch[1];
|
|
279
|
+
proxy = schemeMatch[2];
|
|
280
|
+
}
|
|
281
|
+
let auth = "";
|
|
282
|
+
let body = proxy;
|
|
283
|
+
const atIdx = body.lastIndexOf("@");
|
|
284
|
+
if (atIdx !== -1) {
|
|
285
|
+
auth = body.slice(0, atIdx) + "@";
|
|
286
|
+
body = body.slice(atIdx + 1);
|
|
287
|
+
}
|
|
288
|
+
if (!auth) {
|
|
289
|
+
const parts = body.split(":");
|
|
290
|
+
const isPort = (s) => /^\d+$/.test(s) && +s >= 1 && +s <= 65535;
|
|
291
|
+
const isHost = (s) => s.includes(".") || /[a-z]/i.test(s);
|
|
292
|
+
if (parts.length === 4) if (isPort(parts[3]) && !isPort(parts[1])) {
|
|
293
|
+
auth = `${parts[0]}:${parts[1]}@`;
|
|
294
|
+
body = `${parts[2]}:${parts[3]}`;
|
|
295
|
+
} else if (isPort(parts[1]) && !isPort(parts[3])) {
|
|
296
|
+
auth = `${parts[2]}:${parts[3]}@`;
|
|
297
|
+
body = `${parts[0]}:${parts[1]}`;
|
|
298
|
+
} else if (isHost(parts[2]) && !isHost(parts[0])) {
|
|
299
|
+
auth = `${parts[0]}:${parts[1]}@`;
|
|
300
|
+
body = `${parts[2]}:${parts[3]}`;
|
|
301
|
+
} else {
|
|
302
|
+
auth = `${parts[2]}:${parts[3]}@`;
|
|
303
|
+
body = `${parts[0]}:${parts[1]}`;
|
|
304
|
+
}
|
|
305
|
+
else if (parts.length === 5) if (isPort(parts[3]) && isPort(parts[4]) && !isPort(parts[1])) {
|
|
306
|
+
auth = `${parts[0]}:${parts[1]}@`;
|
|
307
|
+
body = `${parts[2]}:${parts[3]}:${parts[4]}`;
|
|
308
|
+
} else if (isPort(parts[1]) && isPort(parts[2]) && !isPort(parts[3])) {
|
|
309
|
+
auth = `${parts[3]}:${parts[4]}@`;
|
|
310
|
+
body = `${parts[0]}:${parts[1]}:${parts[2]}`;
|
|
311
|
+
} else if (isHost(parts[2]) && !isHost(parts[0])) {
|
|
312
|
+
auth = `${parts[0]}:${parts[1]}@`;
|
|
313
|
+
body = `${parts[2]}:${parts[3]}:${parts[4]}`;
|
|
314
|
+
} else {
|
|
315
|
+
auth = `${parts[3]}:${parts[4]}@`;
|
|
316
|
+
body = `${parts[0]}:${parts[1]}:${parts[2]}`;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
const parts = body.split(":");
|
|
320
|
+
if (parts.length === 3) {
|
|
321
|
+
const start = Number(parts[1]);
|
|
322
|
+
const end = Number(parts[2]);
|
|
323
|
+
if (Number.isInteger(start) && Number.isInteger(end) && start >= 0 && start <= end) body = `${parts[0]}:${randomInteger(start, end + 1)}`;
|
|
324
|
+
}
|
|
325
|
+
return `${protocol}://${auth}${body}`;
|
|
326
|
+
}
|
|
327
|
+
function parseProxy(proxy, protocol = "http") {
|
|
328
|
+
const normalized = normalizeProxy(proxy, protocol);
|
|
329
|
+
if (!normalized) return null;
|
|
330
|
+
const [scheme, rest] = normalized.split("://");
|
|
331
|
+
const atIdx = rest.lastIndexOf("@");
|
|
332
|
+
const authPart = atIdx === -1 ? null : rest.slice(0, atIdx);
|
|
333
|
+
const [host, port] = (atIdx === -1 ? rest : rest.slice(atIdx + 1)).split(":");
|
|
334
|
+
const result = {
|
|
335
|
+
protocol: scheme,
|
|
336
|
+
host,
|
|
337
|
+
port: parseInt(port, 10)
|
|
338
|
+
};
|
|
339
|
+
if (authPart !== null) {
|
|
340
|
+
const colonIdx = authPart.indexOf(":");
|
|
341
|
+
const [username, password] = colonIdx === -1 ? [authPart, ""] : [authPart.slice(0, colonIdx), authPart.slice(colonIdx + 1)];
|
|
342
|
+
result.auth = {
|
|
343
|
+
username,
|
|
344
|
+
password
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
function proxyValue(rawProxy, replacements = {}) {
|
|
350
|
+
const list = splitTrim(rawProxy || "");
|
|
351
|
+
if (list.length === 0) return null;
|
|
352
|
+
const picked = list[randomInteger(0, list.length)];
|
|
353
|
+
let result = normalizeProxy(picked);
|
|
354
|
+
if (!result) return null;
|
|
355
|
+
if (result.includes("{")) {
|
|
356
|
+
const { SESSION, ...rest } = replacements;
|
|
357
|
+
let sessionValue;
|
|
358
|
+
if (!SESSION && SESSION !== 0) sessionValue = randomHex(8);
|
|
359
|
+
else if (typeof SESSION === "function") sessionValue = SESSION();
|
|
360
|
+
else sessionValue = seedHex(String(SESSION), 8);
|
|
361
|
+
result = result.replace("{SESSION}", sessionValue);
|
|
362
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
363
|
+
const v = typeof value === "function" ? value() : String(value);
|
|
364
|
+
result = result.replace(`{${key}}`, v);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return result;
|
|
368
|
+
}
|
|
369
|
+
//#endregion
|
|
370
|
+
export { CONSTANTS, Exception, checkEmpty, coerceObjectIntegers, coerceObjectNumbers, cookiesFromHeader, cookiesFromResponse, cookiesToHeader, findNodeByKey, forever, getResponseError, isInt32, isPositiveNumber, isTransientHttpCode, isValidURL, limitString, mulberry32, normalizeProxy, objectStringify, parseProxy, pascalCase, promiseSilent, promiseTimeout, proxyValue, randomBoolean, randomElement, randomHex, randomInteger, randomString, randomUuid, randomWeighted, retry, safeString, seedHex, shuffleObject, shuffleString, sleep, sleepMs, splitTrim, time, titleCase, waitForProperty };
|
package/dist/node.cjs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_general = require("./general-CAhFOkv8.cjs");
|
|
3
|
+
let fs = require("fs");
|
|
4
|
+
fs = require_general.__toESM(fs, 1);
|
|
5
|
+
let path = require("path");
|
|
6
|
+
path = require_general.__toESM(path, 1);
|
|
7
|
+
let crypto = require("crypto");
|
|
8
|
+
crypto = require_general.__toESM(crypto, 1);
|
|
9
|
+
let os = require("os");
|
|
10
|
+
let child_process = require("child_process");
|
|
11
|
+
let util = require("util");
|
|
12
|
+
let bcryptjs = require("bcryptjs");
|
|
13
|
+
bcryptjs = require_general.__toESM(bcryptjs, 1);
|
|
14
|
+
//#region src/node.js
|
|
15
|
+
const execAsync = (0, util.promisify)(child_process.exec);
|
|
16
|
+
function secureRandomBoolean() {
|
|
17
|
+
return secureRandomInteger(2) === 1;
|
|
18
|
+
}
|
|
19
|
+
function secureRandomString(length, useNumbers = true, useUppercase = false) {
|
|
20
|
+
let characters = require_general.CONSTANTS.LOWER_CASE;
|
|
21
|
+
if (useUppercase) characters += require_general.CONSTANTS.UPPER_CASE;
|
|
22
|
+
if (useNumbers) characters += require_general.CONSTANTS.NUMBERS;
|
|
23
|
+
let result = "";
|
|
24
|
+
for (let i = 0; i < length; i++) result += characters[secureRandomInteger(0, characters.length)];
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
function secureRandomHex(length) {
|
|
28
|
+
return crypto.default.randomBytes(Math.ceil(length / 2)).toString("hex").slice(0, length);
|
|
29
|
+
}
|
|
30
|
+
function secureRandomInteger(min, max = void 0) {
|
|
31
|
+
return crypto.default.randomInt(min, max);
|
|
32
|
+
}
|
|
33
|
+
function secureRandomUuid(useDashes = true) {
|
|
34
|
+
const uuid = crypto.default.randomUUID();
|
|
35
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
36
|
+
}
|
|
37
|
+
function secureRandomWeighted(object) {
|
|
38
|
+
if (require_general.checkEmpty(object)) return void 0;
|
|
39
|
+
const elements = Object.keys(object);
|
|
40
|
+
const weights = Object.values(object);
|
|
41
|
+
const randomNum = secureRandomInteger(0, weights.reduce((sum, weight) => sum + weight, 0));
|
|
42
|
+
let weightSum = 0;
|
|
43
|
+
for (let i = 0; i < elements.length; i++) {
|
|
44
|
+
weightSum += weights[i];
|
|
45
|
+
if (randomNum < weightSum) return elements[i];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function secureRandomElement(object) {
|
|
49
|
+
if (require_general.checkEmpty(object)) return void 0;
|
|
50
|
+
const values = Array.isArray(object) ? object : Object.values(object);
|
|
51
|
+
if (values.length === 0) return void 0;
|
|
52
|
+
return values[secureRandomInteger(0, values.length)];
|
|
53
|
+
}
|
|
54
|
+
function uuidFromSeed(seed, useDashes = true) {
|
|
55
|
+
const hash = crypto.default.createHash("md5").update(seed).digest();
|
|
56
|
+
hash[6] = hash[6] & 15 | 48;
|
|
57
|
+
hash[8] = hash[8] & 63 | 128;
|
|
58
|
+
const hex = hash.toString("hex");
|
|
59
|
+
if (!useDashes) return hex;
|
|
60
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
61
|
+
}
|
|
62
|
+
function hash(algorithm, data) {
|
|
63
|
+
return crypto.default.createHash(algorithm).update(data).digest("hex");
|
|
64
|
+
}
|
|
65
|
+
function md5(data) {
|
|
66
|
+
return hash("md5", data);
|
|
67
|
+
}
|
|
68
|
+
function sha256(data) {
|
|
69
|
+
return hash("sha256", data);
|
|
70
|
+
}
|
|
71
|
+
function base64Encode(data) {
|
|
72
|
+
return Buffer.from(data).toString("base64");
|
|
73
|
+
}
|
|
74
|
+
function base64Decode(data, encoding = "utf8") {
|
|
75
|
+
return Buffer.from(data, "base64").toString(encoding);
|
|
76
|
+
}
|
|
77
|
+
function bcryptHash(plainText, { key = "", strength = 12, preHash = true } = {}) {
|
|
78
|
+
let input = plainText + key;
|
|
79
|
+
if (preHash) input = sha256(input);
|
|
80
|
+
return bcryptjs.default.hashSync(input, strength);
|
|
81
|
+
}
|
|
82
|
+
function bcryptVerify(plainText, hash, { key = "", preHash = true } = {}) {
|
|
83
|
+
let input = plainText + key;
|
|
84
|
+
if (preHash) input = sha256(input);
|
|
85
|
+
return bcryptjs.default.compareSync(input, hash);
|
|
86
|
+
}
|
|
87
|
+
async function readJsonFile(filePath, defaultValue = {}) {
|
|
88
|
+
try {
|
|
89
|
+
const data = await fs.promises.readFile(filePath, "utf8");
|
|
90
|
+
return JSON.parse(data);
|
|
91
|
+
} catch {
|
|
92
|
+
return defaultValue;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function readJsonFileSync(filePath, defaultValue = {}) {
|
|
96
|
+
try {
|
|
97
|
+
const data = fs.default.readFileSync(filePath, "utf8");
|
|
98
|
+
return JSON.parse(data);
|
|
99
|
+
} catch {
|
|
100
|
+
return defaultValue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function writeJsonFile(filePath, data) {
|
|
104
|
+
const jsonData = JSON.stringify(data);
|
|
105
|
+
return fs.promises.writeFile(filePath, jsonData, "utf8");
|
|
106
|
+
}
|
|
107
|
+
function writeJsonFileSync(filePath, data) {
|
|
108
|
+
const jsonData = JSON.stringify(data);
|
|
109
|
+
return fs.default.writeFileSync(filePath, jsonData, "utf8");
|
|
110
|
+
}
|
|
111
|
+
async function clearDirectory(directoryPath, keepDir = true) {
|
|
112
|
+
await fs.promises.rm(directoryPath, {
|
|
113
|
+
recursive: true,
|
|
114
|
+
force: true
|
|
115
|
+
});
|
|
116
|
+
if (keepDir) await fs.promises.mkdir(directoryPath, { recursive: true });
|
|
117
|
+
}
|
|
118
|
+
function createNumberedDirs(mainDirectory, start = 0, end = 9) {
|
|
119
|
+
fs.default.mkdirSync(mainDirectory, { recursive: true });
|
|
120
|
+
for (let i = start; i <= end; i++) fs.default.mkdirSync(path.default.join(mainDirectory, `${i}`), { recursive: true });
|
|
121
|
+
}
|
|
122
|
+
async function executeCommand(command) {
|
|
123
|
+
const { stdout } = await execAsync(command);
|
|
124
|
+
return stdout.trim();
|
|
125
|
+
}
|
|
126
|
+
function hostIp() {
|
|
127
|
+
for (const list of Object.values((0, os.networkInterfaces)())) for (const alias of list) if (alias.family === "IPv4" && alias.address !== "127.0.0.1" && !alias.address.startsWith("192.168.") && !alias.internal) return alias.address;
|
|
128
|
+
return "127.0.0.1";
|
|
129
|
+
}
|
|
130
|
+
function gitVersion() {
|
|
131
|
+
try {
|
|
132
|
+
const raw = (0, child_process.execFileSync)("git", [
|
|
133
|
+
"show",
|
|
134
|
+
"-s",
|
|
135
|
+
"--format=%ct",
|
|
136
|
+
"HEAD"
|
|
137
|
+
], { encoding: "utf8" }).trim();
|
|
138
|
+
const timestamp = parseInt(raw, 10);
|
|
139
|
+
if (isNaN(timestamp)) return "1.0";
|
|
140
|
+
const iso = (/* @__PURE__ */ new Date(timestamp * 1e3)).toISOString();
|
|
141
|
+
return `${iso.slice(2, 4)}${iso.slice(5, 7)}${iso.slice(8, 10)}.${iso.slice(11, 13)}${iso.slice(14, 16)}`;
|
|
142
|
+
} catch {
|
|
143
|
+
return "1.0";
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
exports.base64Decode = base64Decode;
|
|
148
|
+
exports.base64Encode = base64Encode;
|
|
149
|
+
exports.bcryptHash = bcryptHash;
|
|
150
|
+
exports.bcryptVerify = bcryptVerify;
|
|
151
|
+
exports.clearDirectory = clearDirectory;
|
|
152
|
+
exports.createNumberedDirs = createNumberedDirs;
|
|
153
|
+
exports.executeCommand = executeCommand;
|
|
154
|
+
exports.gitVersion = gitVersion;
|
|
155
|
+
exports.hash = hash;
|
|
156
|
+
exports.hostIp = hostIp;
|
|
157
|
+
exports.md5 = md5;
|
|
158
|
+
exports.readJsonFile = readJsonFile;
|
|
159
|
+
exports.readJsonFileSync = readJsonFileSync;
|
|
160
|
+
exports.secureRandomBoolean = secureRandomBoolean;
|
|
161
|
+
exports.secureRandomElement = secureRandomElement;
|
|
162
|
+
exports.secureRandomHex = secureRandomHex;
|
|
163
|
+
exports.secureRandomInteger = secureRandomInteger;
|
|
164
|
+
exports.secureRandomString = secureRandomString;
|
|
165
|
+
exports.secureRandomUuid = secureRandomUuid;
|
|
166
|
+
exports.secureRandomWeighted = secureRandomWeighted;
|
|
167
|
+
exports.sha256 = sha256;
|
|
168
|
+
exports.uuidFromSeed = uuidFromSeed;
|
|
169
|
+
exports.writeJsonFile = writeJsonFile;
|
|
170
|
+
exports.writeJsonFileSync = writeJsonFileSync;
|