melperjs 12.0.0 → 13.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 +39 -15
- package/lib/cjs/node.js +14 -10
- package/lib/esm/index.js +37 -15
- package/lib/esm/node.js +14 -10
- package/package.json +1 -1
package/lib/cjs/index.js
CHANGED
|
@@ -9,6 +9,7 @@ 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;
|
|
13
14
|
exports.indexByTime = indexByTime;
|
|
14
15
|
exports.isIntlHttpCode = isIntlHttpCode;
|
|
@@ -36,6 +37,7 @@ exports.sleepMs = sleepMs;
|
|
|
36
37
|
exports.splitClear = splitClear;
|
|
37
38
|
exports.time = time;
|
|
38
39
|
exports.titleCase = titleCase;
|
|
40
|
+
exports.waitForProperty = waitForProperty;
|
|
39
41
|
var _xss = _interopRequireDefault(require("xss"));
|
|
40
42
|
var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
|
|
41
43
|
var _camelCase = _interopRequireDefault(require("lodash/camelCase.js"));
|
|
@@ -47,7 +49,9 @@ const CONSTANTS = exports.CONSTANTS = {
|
|
|
47
49
|
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
48
50
|
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
49
51
|
HEXADECIMAL: "0123456789abcdef",
|
|
50
|
-
NUMBERS: "0123456789"
|
|
52
|
+
NUMBERS: "0123456789",
|
|
53
|
+
INT32_MIN: -2147483648,
|
|
54
|
+
INT32_MAX: 2147483647
|
|
51
55
|
};
|
|
52
56
|
function Exception(message, response = {}, name = null) {
|
|
53
57
|
const error = new Error(message);
|
|
@@ -108,7 +112,7 @@ async function retryFn(fn, retries, errorFn = null) {
|
|
|
108
112
|
result = await fn();
|
|
109
113
|
return result;
|
|
110
114
|
} catch (error) {
|
|
111
|
-
errorFn
|
|
115
|
+
errorFn && (await errorFn(attempt, error, result));
|
|
112
116
|
if (attempt >= retries) {
|
|
113
117
|
throw error;
|
|
114
118
|
}
|
|
@@ -127,19 +131,6 @@ function splitClear(rawText, separator = null) {
|
|
|
127
131
|
separator = separator ?? /\r?\n/;
|
|
128
132
|
return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
|
|
129
133
|
}
|
|
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
134
|
function checkEmpty(value) {
|
|
144
135
|
if (typeof value === "number") {
|
|
145
136
|
return value === 0;
|
|
@@ -155,6 +146,9 @@ function titleCase(str, separator = " ") {
|
|
|
155
146
|
const words = str.split(separator);
|
|
156
147
|
return words.map(word => (0, _upperFirst.default)(word)).join(separator);
|
|
157
148
|
}
|
|
149
|
+
function isInt32(value) {
|
|
150
|
+
return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
|
|
151
|
+
}
|
|
158
152
|
function parseNumFromObj(obj) {
|
|
159
153
|
for (let key in obj) {
|
|
160
154
|
let value = obj[key];
|
|
@@ -177,6 +171,36 @@ function parseIntFromObj(obj) {
|
|
|
177
171
|
}
|
|
178
172
|
return obj;
|
|
179
173
|
}
|
|
174
|
+
function findKeyNode(key, node, pair = null) {
|
|
175
|
+
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
176
|
+
return node;
|
|
177
|
+
} else if (typeof node === 'object') {
|
|
178
|
+
for (let index in node) {
|
|
179
|
+
const result = findKeyNode(key, node[index], pair);
|
|
180
|
+
if (result) {
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
function flipObject(obj) {
|
|
188
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
189
|
+
}
|
|
190
|
+
function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
const startTime = Date.now();
|
|
193
|
+
const checkProperty = setInterval(() => {
|
|
194
|
+
if (obj.hasOwnProperty(propertyName)) {
|
|
195
|
+
clearInterval(checkProperty);
|
|
196
|
+
resolve();
|
|
197
|
+
} else if (Date.now() - startTime > timeout) {
|
|
198
|
+
clearInterval(checkProperty);
|
|
199
|
+
reject(new Error(`Property ${propertyName} did not appear within ${timeout} milliseconds`));
|
|
200
|
+
}
|
|
201
|
+
}, interval);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
180
204
|
function objectStringify(obj) {
|
|
181
205
|
for (let key in obj) {
|
|
182
206
|
if (obj.hasOwnProperty(key)) {
|
package/lib/cjs/node.js
CHANGED
|
@@ -98,13 +98,17 @@ function serverIp() {
|
|
|
98
98
|
}
|
|
99
99
|
function getVersion() {
|
|
100
100
|
try {
|
|
101
|
-
const
|
|
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.
|
|
104
|
-
const month = formatDatePart(date.
|
|
105
|
-
const day = formatDatePart(date.
|
|
106
|
-
const hour = formatDatePart(date.
|
|
107
|
-
const minute = formatDatePart(date.
|
|
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,7 +8,9 @@ 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);
|
|
@@ -69,7 +71,7 @@ export async function retryFn(fn, retries, errorFn = null) {
|
|
|
69
71
|
result = await fn();
|
|
70
72
|
return result;
|
|
71
73
|
} catch (error) {
|
|
72
|
-
errorFn
|
|
74
|
+
errorFn && (await errorFn(attempt, error, result));
|
|
73
75
|
if (attempt >= retries) {
|
|
74
76
|
throw error;
|
|
75
77
|
}
|
|
@@ -88,19 +90,6 @@ export function splitClear(rawText, separator = null) {
|
|
|
88
90
|
separator = separator ?? /\r?\n/;
|
|
89
91
|
return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
|
|
90
92
|
}
|
|
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
93
|
export function checkEmpty(value) {
|
|
105
94
|
if (typeof value === "number") {
|
|
106
95
|
return value === 0;
|
|
@@ -116,6 +105,9 @@ export function titleCase(str, separator = " ") {
|
|
|
116
105
|
const words = str.split(separator);
|
|
117
106
|
return words.map(word => upperFirst(word)).join(separator);
|
|
118
107
|
}
|
|
108
|
+
function isInt32(value) {
|
|
109
|
+
return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
|
|
110
|
+
}
|
|
119
111
|
export function parseNumFromObj(obj) {
|
|
120
112
|
for (let key in obj) {
|
|
121
113
|
let value = obj[key];
|
|
@@ -138,6 +130,36 @@ export function parseIntFromObj(obj) {
|
|
|
138
130
|
}
|
|
139
131
|
return obj;
|
|
140
132
|
}
|
|
133
|
+
export function findKeyNode(key, node, pair = null) {
|
|
134
|
+
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
135
|
+
return node;
|
|
136
|
+
} else if (typeof node === 'object') {
|
|
137
|
+
for (let index in node) {
|
|
138
|
+
const result = findKeyNode(key, node[index], pair);
|
|
139
|
+
if (result) {
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
export function flipObject(obj) {
|
|
147
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
|
|
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
|
+
}
|
|
141
163
|
export function objectStringify(obj) {
|
|
142
164
|
for (let key in obj) {
|
|
143
165
|
if (obj.hasOwnProperty(key)) {
|
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
|
|
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.
|
|
75
|
-
const month = formatDatePart(date.
|
|
76
|
-
const day = formatDatePart(date.
|
|
77
|
-
const hour = formatDatePart(date.
|
|
78
|
-
const minute = formatDatePart(date.
|
|
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) {
|