melperjs 13.0.0 → 14.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/cjs/index.js +39 -4
- package/lib/cjs/node.js +2 -2
- package/lib/esm/index.js +36 -5
- package/lib/esm/node.js +2 -2
- package/package.json +8 -8
package/lib/cjs/index.js
CHANGED
|
@@ -8,10 +8,13 @@ exports.Exception = Exception;
|
|
|
8
8
|
exports.checkEmpty = checkEmpty;
|
|
9
9
|
exports.cookieDict = cookieDict;
|
|
10
10
|
exports.cookieHeader = cookieHeader;
|
|
11
|
+
exports.cookieStringToObject = cookieStringToObject;
|
|
11
12
|
exports.findKeyNode = findKeyNode;
|
|
12
13
|
exports.flipObject = flipObject;
|
|
13
14
|
exports.forever = forever;
|
|
15
|
+
exports.getResponseError = getResponseError;
|
|
14
16
|
exports.indexByTime = indexByTime;
|
|
17
|
+
exports.isInt32 = isInt32;
|
|
15
18
|
exports.isIntlHttpCode = isIntlHttpCode;
|
|
16
19
|
exports.isIntlHttpError = isIntlHttpError;
|
|
17
20
|
exports.isValidURL = isValidURL;
|
|
@@ -31,6 +34,7 @@ exports.randomUuid = randomUuid;
|
|
|
31
34
|
exports.randomWeighted = randomWeighted;
|
|
32
35
|
exports.retryFn = retryFn;
|
|
33
36
|
exports.safeString = safeString;
|
|
37
|
+
exports.shuffleObject = shuffleObject;
|
|
34
38
|
exports.shuffleString = shuffleString;
|
|
35
39
|
exports.sleep = sleep;
|
|
36
40
|
exports.sleepMs = sleepMs;
|
|
@@ -57,7 +61,10 @@ function Exception(message, response = {}, name = null) {
|
|
|
57
61
|
const error = new Error(message);
|
|
58
62
|
error.name = name || "Exception";
|
|
59
63
|
error.response = response;
|
|
60
|
-
if (
|
|
64
|
+
if (checkEmpty(response)) {
|
|
65
|
+
error.response = {};
|
|
66
|
+
}
|
|
67
|
+
if (!error.response?.status) {
|
|
61
68
|
error.response.status = 400;
|
|
62
69
|
}
|
|
63
70
|
return error;
|
|
@@ -184,9 +191,6 @@ function findKeyNode(key, node, pair = null) {
|
|
|
184
191
|
}
|
|
185
192
|
return null;
|
|
186
193
|
}
|
|
187
|
-
function flipObject(obj) {
|
|
188
|
-
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
189
|
-
}
|
|
190
194
|
function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
|
|
191
195
|
return new Promise((resolve, reject) => {
|
|
192
196
|
const startTime = Date.now();
|
|
@@ -201,6 +205,17 @@ function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
|
|
|
201
205
|
}, interval);
|
|
202
206
|
});
|
|
203
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
|
+
}
|
|
204
219
|
function objectStringify(obj) {
|
|
205
220
|
for (let key in obj) {
|
|
206
221
|
if (obj.hasOwnProperty(key)) {
|
|
@@ -342,10 +357,30 @@ function cookieDict(res, decodeValues = false) {
|
|
|
342
357
|
function cookieHeader(cookieDict) {
|
|
343
358
|
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
344
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
|
+
}
|
|
345
371
|
function isIntlHttpCode(httpCode) {
|
|
346
372
|
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
347
373
|
}
|
|
348
374
|
function isIntlHttpError(e) {
|
|
349
375
|
const message = e?.message?.toLowerCase?.() || "";
|
|
350
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();
|
|
351
386
|
}
|
package/lib/cjs/node.js
CHANGED
|
@@ -137,8 +137,8 @@ function md5(data) {
|
|
|
137
137
|
function sha256(data) {
|
|
138
138
|
return hash("sha256", data);
|
|
139
139
|
}
|
|
140
|
-
function hashBcrypt(plainText, encryptionKey = "") {
|
|
141
|
-
return _bcryptjs.default.hashSync(plainText + encryptionKey, _bcryptjs.default.genSaltSync(
|
|
140
|
+
function hashBcrypt(plainText, encryptionKey = "", rounds = 12) {
|
|
141
|
+
return _bcryptjs.default.hashSync(plainText + encryptionKey, _bcryptjs.default.genSaltSync(rounds));
|
|
142
142
|
}
|
|
143
143
|
function verifyBcrypt(plainText, hash, encryptionKey = "") {
|
|
144
144
|
return _bcryptjs.default.compareSync(plainText + encryptionKey, hash);
|
package/lib/esm/index.js
CHANGED
|
@@ -16,7 +16,10 @@ export function Exception(message, response = {}, name = null) {
|
|
|
16
16
|
const error = new Error(message);
|
|
17
17
|
error.name = name || "Exception";
|
|
18
18
|
error.response = response;
|
|
19
|
-
if (
|
|
19
|
+
if (checkEmpty(response)) {
|
|
20
|
+
error.response = {};
|
|
21
|
+
}
|
|
22
|
+
if (!error.response?.status) {
|
|
20
23
|
error.response.status = 400;
|
|
21
24
|
}
|
|
22
25
|
return error;
|
|
@@ -105,7 +108,7 @@ export function titleCase(str, separator = " ") {
|
|
|
105
108
|
const words = str.split(separator);
|
|
106
109
|
return words.map(word => upperFirst(word)).join(separator);
|
|
107
110
|
}
|
|
108
|
-
function isInt32(value) {
|
|
111
|
+
export function isInt32(value) {
|
|
109
112
|
return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
|
|
110
113
|
}
|
|
111
114
|
export function parseNumFromObj(obj) {
|
|
@@ -143,9 +146,6 @@ export function findKeyNode(key, node, pair = null) {
|
|
|
143
146
|
}
|
|
144
147
|
return null;
|
|
145
148
|
}
|
|
146
|
-
export function flipObject(obj) {
|
|
147
|
-
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
148
|
-
}
|
|
149
149
|
export function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
|
|
150
150
|
return new Promise((resolve, reject) => {
|
|
151
151
|
const startTime = Date.now();
|
|
@@ -160,6 +160,17 @@ export function waitForProperty(obj, propertyName, timeout = 5000, interval = 10
|
|
|
160
160
|
}, interval);
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
|
+
export function flipObject(obj) {
|
|
164
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
165
|
+
}
|
|
166
|
+
export function shuffleObject(obj) {
|
|
167
|
+
const arr = Object.entries(obj);
|
|
168
|
+
for (let i = arr.length - 1; i > 0; i--) {
|
|
169
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
170
|
+
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
171
|
+
}
|
|
172
|
+
return Object.fromEntries(arr);
|
|
173
|
+
}
|
|
163
174
|
export function objectStringify(obj) {
|
|
164
175
|
for (let key in obj) {
|
|
165
176
|
if (obj.hasOwnProperty(key)) {
|
|
@@ -301,10 +312,30 @@ export function cookieDict(res, decodeValues = false) {
|
|
|
301
312
|
export function cookieHeader(cookieDict) {
|
|
302
313
|
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
303
314
|
}
|
|
315
|
+
export function cookieStringToObject(cookieString) {
|
|
316
|
+
const cookies = {};
|
|
317
|
+
if (!cookieString) return cookies;
|
|
318
|
+
cookieString.split(';').forEach(cookie => {
|
|
319
|
+
const [key, ...valueParts] = cookie.trim().split('=');
|
|
320
|
+
if (key) {
|
|
321
|
+
cookies[key.trim()] = valueParts.join('=').trim();
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
return cookies;
|
|
325
|
+
}
|
|
304
326
|
export function isIntlHttpCode(httpCode) {
|
|
305
327
|
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
306
328
|
}
|
|
307
329
|
export function isIntlHttpError(e) {
|
|
308
330
|
const message = e?.message?.toLowerCase?.() || "";
|
|
309
331
|
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);
|
|
332
|
+
}
|
|
333
|
+
export function getResponseError(e, limit = 115) {
|
|
334
|
+
let response;
|
|
335
|
+
if (e?.response?.status && e.response.data) {
|
|
336
|
+
response = `${e.response.status}|${e.response.data}`;
|
|
337
|
+
} else if (e?.response?.data) {
|
|
338
|
+
response = e.response.data;
|
|
339
|
+
}
|
|
340
|
+
return limitString(response || e.message, limit).trim();
|
|
310
341
|
}
|
package/lib/esm/node.js
CHANGED
|
@@ -108,8 +108,8 @@ export function md5(data) {
|
|
|
108
108
|
export function sha256(data) {
|
|
109
109
|
return hash("sha256", data);
|
|
110
110
|
}
|
|
111
|
-
export function hashBcrypt(plainText, encryptionKey = "") {
|
|
112
|
-
return bcrypt.hashSync(plainText + encryptionKey, bcrypt.genSaltSync(
|
|
111
|
+
export function hashBcrypt(plainText, encryptionKey = "", rounds = 12) {
|
|
112
|
+
return bcrypt.hashSync(plainText + encryptionKey, bcrypt.genSaltSync(rounds));
|
|
113
113
|
}
|
|
114
114
|
export function verifyBcrypt(plainText, hash, encryptionKey = "") {
|
|
115
115
|
return bcrypt.compareSync(plainText + encryptionKey, hash);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melperjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "14.1.0",
|
|
4
4
|
"description": "Javascript module to use predefined common functions and utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"melperjs",
|
|
@@ -41,15 +41,15 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"bcryptjs": "^3.0.2",
|
|
43
43
|
"hpagent": "^1.2.0",
|
|
44
|
-
"lodash": "
|
|
45
|
-
"set-cookie-parser": "^
|
|
44
|
+
"lodash": "4.17.23",
|
|
45
|
+
"set-cookie-parser": "^3.0.1",
|
|
46
46
|
"xss": "^1.0.15"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@babel/cli": "^7.
|
|
50
|
-
"@babel/core": "^7.
|
|
51
|
-
"@babel/preset-env": "^7.
|
|
52
|
-
"axios": "
|
|
53
|
-
"cross-env": "^
|
|
49
|
+
"@babel/cli": "^7.28.6",
|
|
50
|
+
"@babel/core": "^7.29.0",
|
|
51
|
+
"@babel/preset-env": "^7.29.0",
|
|
52
|
+
"axios": "1.13.5",
|
|
53
|
+
"cross-env": "^10.1.0"
|
|
54
54
|
}
|
|
55
55
|
}
|