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