melperjs 12.0.0 → 14.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/lib/cjs/index.js CHANGED
@@ -9,8 +9,11 @@ exports.checkEmpty = checkEmpty;
9
9
  exports.cookieDict = cookieDict;
10
10
  exports.cookieHeader = cookieHeader;
11
11
  exports.findKeyNode = findKeyNode;
12
+ exports.flipObject = flipObject;
12
13
  exports.forever = forever;
14
+ exports.getResponseError = getResponseError;
13
15
  exports.indexByTime = indexByTime;
16
+ exports.isInt32 = isInt32;
14
17
  exports.isIntlHttpCode = isIntlHttpCode;
15
18
  exports.isIntlHttpError = isIntlHttpError;
16
19
  exports.isValidURL = isValidURL;
@@ -30,12 +33,14 @@ exports.randomUuid = randomUuid;
30
33
  exports.randomWeighted = randomWeighted;
31
34
  exports.retryFn = retryFn;
32
35
  exports.safeString = safeString;
36
+ exports.shuffleObject = shuffleObject;
33
37
  exports.shuffleString = shuffleString;
34
38
  exports.sleep = sleep;
35
39
  exports.sleepMs = sleepMs;
36
40
  exports.splitClear = splitClear;
37
41
  exports.time = time;
38
42
  exports.titleCase = titleCase;
43
+ exports.waitForProperty = waitForProperty;
39
44
  var _xss = _interopRequireDefault(require("xss"));
40
45
  var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
41
46
  var _camelCase = _interopRequireDefault(require("lodash/camelCase.js"));
@@ -47,13 +52,18 @@ const CONSTANTS = exports.CONSTANTS = {
47
52
  LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
48
53
  UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
49
54
  HEXADECIMAL: "0123456789abcdef",
50
- NUMBERS: "0123456789"
55
+ NUMBERS: "0123456789",
56
+ INT32_MIN: -2147483648,
57
+ INT32_MAX: 2147483647
51
58
  };
52
59
  function Exception(message, response = {}, name = null) {
53
60
  const error = new Error(message);
54
61
  error.name = name || "Exception";
55
62
  error.response = response;
56
- if (!error.response?.status && typeof error.response === "object") {
63
+ if (checkEmpty(response)) {
64
+ error.response = {};
65
+ }
66
+ if (!error.response?.status) {
57
67
  error.response.status = 400;
58
68
  }
59
69
  return error;
@@ -108,7 +118,7 @@ async function retryFn(fn, retries, errorFn = null) {
108
118
  result = await fn();
109
119
  return result;
110
120
  } catch (error) {
111
- errorFn?.(attempt, error, result);
121
+ errorFn && (await errorFn(attempt, error, result));
112
122
  if (attempt >= retries) {
113
123
  throw error;
114
124
  }
@@ -127,19 +137,6 @@ function splitClear(rawText, separator = null) {
127
137
  separator = separator ?? /\r?\n/;
128
138
  return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
129
139
  }
130
- function findKeyNode(key, node, pair = null) {
131
- if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
132
- return node;
133
- } else if (typeof node === 'object') {
134
- for (let index in node) {
135
- const result = findKeyNode(key, node[index], pair);
136
- if (result) {
137
- return result;
138
- }
139
- }
140
- }
141
- return null;
142
- }
143
140
  function checkEmpty(value) {
144
141
  if (typeof value === "number") {
145
142
  return value === 0;
@@ -155,6 +152,9 @@ function titleCase(str, separator = " ") {
155
152
  const words = str.split(separator);
156
153
  return words.map(word => (0, _upperFirst.default)(word)).join(separator);
157
154
  }
155
+ function isInt32(value) {
156
+ return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
157
+ }
158
158
  function parseNumFromObj(obj) {
159
159
  for (let key in obj) {
160
160
  let value = obj[key];
@@ -177,6 +177,44 @@ function parseIntFromObj(obj) {
177
177
  }
178
178
  return obj;
179
179
  }
180
+ function findKeyNode(key, node, pair = null) {
181
+ if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
182
+ return node;
183
+ } else if (typeof node === 'object') {
184
+ for (let index in node) {
185
+ const result = findKeyNode(key, node[index], pair);
186
+ if (result) {
187
+ return result;
188
+ }
189
+ }
190
+ }
191
+ return null;
192
+ }
193
+ function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
194
+ return new Promise((resolve, reject) => {
195
+ const startTime = Date.now();
196
+ const checkProperty = setInterval(() => {
197
+ if (obj.hasOwnProperty(propertyName)) {
198
+ clearInterval(checkProperty);
199
+ resolve();
200
+ } else if (Date.now() - startTime > timeout) {
201
+ clearInterval(checkProperty);
202
+ reject(new Error(`Property ${propertyName} did not appear within ${timeout} milliseconds`));
203
+ }
204
+ }, interval);
205
+ });
206
+ }
207
+ function flipObject(obj) {
208
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
209
+ }
210
+ function shuffleObject(obj) {
211
+ const arr = Object.entries(obj);
212
+ for (let i = arr.length - 1; i > 0; i--) {
213
+ const j = Math.floor(Math.random() * (i + 1));
214
+ [arr[i], arr[j]] = [arr[j], arr[i]];
215
+ }
216
+ return Object.fromEntries(arr);
217
+ }
180
218
  function objectStringify(obj) {
181
219
  for (let key in obj) {
182
220
  if (obj.hasOwnProperty(key)) {
@@ -324,4 +362,13 @@ function isIntlHttpCode(httpCode) {
324
362
  function isIntlHttpError(e) {
325
363
  const message = e?.message?.toLowerCase?.() || "";
326
364
  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);
365
+ }
366
+ function getResponseError(e, limit = 115) {
367
+ let response;
368
+ if (e?.response?.status && e.response.data) {
369
+ response = `${e.response.status}|${e.response.data}`;
370
+ } else if (e?.response?.data) {
371
+ response = e.response.data;
372
+ }
373
+ return limitString(response || e.message, limit).trim();
327
374
  }
package/lib/cjs/node.js CHANGED
@@ -98,13 +98,17 @@ function serverIp() {
98
98
  }
99
99
  function getVersion() {
100
100
  try {
101
- const date = new Date((0, _child_process.execSync)('git show -s --format=%ci HEAD').toString().trim());
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);
102
106
  const formatDatePart = value => value.toString().padStart(2, '0');
103
- const year = date.getFullYear().toString().slice(-2);
104
- const month = formatDatePart(date.getMonth() + 1);
105
- const day = formatDatePart(date.getDate());
106
- const hour = formatDatePart(date.getHours());
107
- const minute = formatDatePart(date.getMinutes());
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());
108
112
  return `${year}${month}${day}.${hour}${minute}`;
109
113
  } catch {
110
114
  return "1.0";
@@ -191,20 +195,20 @@ function proxyValue(proxies) {
191
195
  proxy = proxy.replace("{SESSION}", tokenHex(8));
192
196
  return proxy || null;
193
197
  }
194
- async function readJsonFile(filePath) {
198
+ async function readJsonFile(filePath, defaultValue = {}) {
195
199
  try {
196
200
  const data = await _fs.promises.readFile(filePath, 'utf8');
197
201
  return JSON.parse(data);
198
202
  } catch (error) {
199
- return {};
203
+ return defaultValue;
200
204
  }
201
205
  }
202
- function readJsonFileSync(filePath) {
206
+ function readJsonFileSync(filePath, defaultValue = {}) {
203
207
  try {
204
208
  const data = _fs.default.readFileSync(filePath, 'utf8');
205
209
  return JSON.parse(data);
206
210
  } catch (error) {
207
- return {};
211
+ return defaultValue;
208
212
  }
209
213
  }
210
214
  async function writeJsonFile(filePath, data) {
package/lib/esm/index.js CHANGED
@@ -8,13 +8,18 @@ export const CONSTANTS = {
8
8
  LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
9
9
  UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
10
10
  HEXADECIMAL: "0123456789abcdef",
11
- NUMBERS: "0123456789"
11
+ NUMBERS: "0123456789",
12
+ INT32_MIN: -2147483648,
13
+ INT32_MAX: 2147483647
12
14
  };
13
15
  export function Exception(message, response = {}, name = null) {
14
16
  const error = new Error(message);
15
17
  error.name = name || "Exception";
16
18
  error.response = response;
17
- if (!error.response?.status && typeof error.response === "object") {
19
+ if (checkEmpty(response)) {
20
+ error.response = {};
21
+ }
22
+ if (!error.response?.status) {
18
23
  error.response.status = 400;
19
24
  }
20
25
  return error;
@@ -69,7 +74,7 @@ export async function retryFn(fn, retries, errorFn = null) {
69
74
  result = await fn();
70
75
  return result;
71
76
  } catch (error) {
72
- errorFn?.(attempt, error, result);
77
+ errorFn && (await errorFn(attempt, error, result));
73
78
  if (attempt >= retries) {
74
79
  throw error;
75
80
  }
@@ -88,19 +93,6 @@ export function splitClear(rawText, separator = null) {
88
93
  separator = separator ?? /\r?\n/;
89
94
  return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
90
95
  }
91
- export function findKeyNode(key, node, pair = null) {
92
- if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
93
- return node;
94
- } else if (typeof node === 'object') {
95
- for (let index in node) {
96
- const result = findKeyNode(key, node[index], pair);
97
- if (result) {
98
- return result;
99
- }
100
- }
101
- }
102
- return null;
103
- }
104
96
  export function checkEmpty(value) {
105
97
  if (typeof value === "number") {
106
98
  return value === 0;
@@ -116,6 +108,9 @@ export function titleCase(str, separator = " ") {
116
108
  const words = str.split(separator);
117
109
  return words.map(word => upperFirst(word)).join(separator);
118
110
  }
111
+ export function isInt32(value) {
112
+ return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
113
+ }
119
114
  export function parseNumFromObj(obj) {
120
115
  for (let key in obj) {
121
116
  let value = obj[key];
@@ -138,6 +133,44 @@ export function parseIntFromObj(obj) {
138
133
  }
139
134
  return obj;
140
135
  }
136
+ export function findKeyNode(key, node, pair = null) {
137
+ if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
138
+ return node;
139
+ } else if (typeof node === 'object') {
140
+ for (let index in node) {
141
+ const result = findKeyNode(key, node[index], pair);
142
+ if (result) {
143
+ return result;
144
+ }
145
+ }
146
+ }
147
+ return null;
148
+ }
149
+ export function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
150
+ return new Promise((resolve, reject) => {
151
+ const startTime = Date.now();
152
+ const checkProperty = setInterval(() => {
153
+ if (obj.hasOwnProperty(propertyName)) {
154
+ clearInterval(checkProperty);
155
+ resolve();
156
+ } else if (Date.now() - startTime > timeout) {
157
+ clearInterval(checkProperty);
158
+ reject(new Error(`Property ${propertyName} did not appear within ${timeout} milliseconds`));
159
+ }
160
+ }, interval);
161
+ });
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
+ }
141
174
  export function objectStringify(obj) {
142
175
  for (let key in obj) {
143
176
  if (obj.hasOwnProperty(key)) {
@@ -285,4 +318,13 @@ export function isIntlHttpCode(httpCode) {
285
318
  export function isIntlHttpError(e) {
286
319
  const message = e?.message?.toLowerCase?.() || "";
287
320
  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);
321
+ }
322
+ export function getResponseError(e, limit = 115) {
323
+ let response;
324
+ if (e?.response?.status && e.response.data) {
325
+ response = `${e.response.status}|${e.response.data}`;
326
+ } else if (e?.response?.data) {
327
+ response = e.response.data;
328
+ }
329
+ return limitString(response || e.message, limit).trim();
288
330
  }
package/lib/esm/node.js CHANGED
@@ -69,13 +69,17 @@ export function serverIp() {
69
69
  }
70
70
  export function getVersion() {
71
71
  try {
72
- const date = new Date(execSync('git show -s --format=%ci HEAD').toString().trim());
72
+ const timestamp = parseInt(execSync('git show -s --format=%ct HEAD').toString().trim());
73
+ if (isNaN(timestamp)) {
74
+ return "1.0";
75
+ }
76
+ const date = new Date(timestamp * 1000);
73
77
  const formatDatePart = value => value.toString().padStart(2, '0');
74
- const year = date.getFullYear().toString().slice(-2);
75
- const month = formatDatePart(date.getMonth() + 1);
76
- const day = formatDatePart(date.getDate());
77
- const hour = formatDatePart(date.getHours());
78
- const minute = formatDatePart(date.getMinutes());
78
+ const year = date.getUTCFullYear().toString().slice(-2);
79
+ const month = formatDatePart(date.getUTCMonth() + 1);
80
+ const day = formatDatePart(date.getUTCDate());
81
+ const hour = formatDatePart(date.getUTCHours());
82
+ const minute = formatDatePart(date.getUTCMinutes());
79
83
  return `${year}${month}${day}.${hour}${minute}`;
80
84
  } catch {
81
85
  return "1.0";
@@ -162,20 +166,20 @@ export function proxyValue(proxies) {
162
166
  proxy = proxy.replace("{SESSION}", tokenHex(8));
163
167
  return proxy || null;
164
168
  }
165
- export async function readJsonFile(filePath) {
169
+ export async function readJsonFile(filePath, defaultValue = {}) {
166
170
  try {
167
171
  const data = await fsp.readFile(filePath, 'utf8');
168
172
  return JSON.parse(data);
169
173
  } catch (error) {
170
- return {};
174
+ return defaultValue;
171
175
  }
172
176
  }
173
- export function readJsonFileSync(filePath) {
177
+ export function readJsonFileSync(filePath, defaultValue = {}) {
174
178
  try {
175
179
  const data = fs.readFileSync(filePath, 'utf8');
176
180
  return JSON.parse(data);
177
181
  } catch (error) {
178
- return {};
182
+ return defaultValue;
179
183
  }
180
184
  }
181
185
  export async function writeJsonFile(filePath, data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "melperjs",
3
- "version": "12.0.0",
3
+ "version": "14.0.0",
4
4
  "description": "Javascript module to use predefined common functions and utilities",
5
5
  "keywords": [
6
6
  "melperjs",