@zwa73/utils 1.0.168 → 1.0.170
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/dist/UtilCom.d.ts +5 -5
- package/dist/UtilCom.js +22 -22
- package/dist/UtilFunctions.d.ts +11 -5
- package/dist/UtilFunctions.js +37 -16
- package/dist/UtilLogger.d.ts +1 -1
- package/dist/UtilLogger.js +1 -1
- package/package.json +1 -1
- package/src/UtilCom.ts +22 -19
- package/src/UtilFunctions.ts +44 -18
- package/src/UtilLogger.ts +2 -2
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnyString, JObject, ReqVerifyFn } from "./UtilInterfaces";
|
|
2
|
-
import { RepeatPromiseOpt } from "./UtilFunctions";
|
|
2
|
+
import { RepeatPromiseOpt, RepeatPromiseResult } from "./UtilFunctions";
|
|
3
3
|
export type ComPostOption = {
|
|
4
4
|
/**请求域名 */
|
|
5
5
|
hostname: string;
|
|
@@ -30,7 +30,7 @@ export declare namespace UtilCom {
|
|
|
30
30
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
31
31
|
* @returns 结果 null 为未能成功接收
|
|
32
32
|
*/
|
|
33
|
-
function
|
|
33
|
+
function httpsPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | undefined>;
|
|
34
34
|
/**发送一个 http POST请求并接受数据
|
|
35
35
|
* @async
|
|
36
36
|
* @param json - 数据对象
|
|
@@ -38,7 +38,7 @@ export declare namespace UtilCom {
|
|
|
38
38
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
39
39
|
* @returns 结果 null 为未能成功接收
|
|
40
40
|
*/
|
|
41
|
-
function
|
|
41
|
+
function httpPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | undefined>;
|
|
42
42
|
/**重复一个 https POST请求并接受数据
|
|
43
43
|
* @async
|
|
44
44
|
* @param json - 数据对象
|
|
@@ -51,7 +51,7 @@ export declare namespace UtilCom {
|
|
|
51
51
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
52
52
|
* @returns 结果 null 为未能成功接收
|
|
53
53
|
*/
|
|
54
|
-
function
|
|
54
|
+
function httpsRepeatPost(json: JObject, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | undefined>, opt?: RepeatPostOpt): Promise<RepeatPromiseResult<JObject | undefined> | undefined>;
|
|
55
55
|
/**重复一个 http POST请求并接受数据
|
|
56
56
|
* Object ()
|
|
57
57
|
* @async
|
|
@@ -65,6 +65,6 @@ export declare namespace UtilCom {
|
|
|
65
65
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
66
66
|
* @returns 结果 null 为未能成功接收
|
|
67
67
|
*/
|
|
68
|
-
function
|
|
68
|
+
function httpRepeatPost(json: JObject, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | undefined>, opt?: RepeatPostOpt): Promise<RepeatPromiseResult<JObject | undefined> | undefined>;
|
|
69
69
|
}
|
|
70
70
|
export {};
|
package/dist/UtilCom.js
CHANGED
|
@@ -36,9 +36,9 @@ var UtilCom;
|
|
|
36
36
|
* @param json - 数据对象
|
|
37
37
|
* @param options - 参数对象
|
|
38
38
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
39
|
-
* @returns 结果
|
|
39
|
+
* @returns 结果 undefined 为未能成功接收
|
|
40
40
|
*/
|
|
41
|
-
function
|
|
41
|
+
function post(posttype, json, options, timeLimit) {
|
|
42
42
|
//转换为毫秒
|
|
43
43
|
const hasTimeLimit = (timeLimit ? timeLimit >= 10 : false);
|
|
44
44
|
if (hasTimeLimit && timeLimit != undefined)
|
|
@@ -54,7 +54,7 @@ var UtilCom;
|
|
|
54
54
|
res.setTimeout(fixlimit, () => {
|
|
55
55
|
//res.abort();
|
|
56
56
|
UtilLogger_1.SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
|
|
57
|
-
resolve(
|
|
57
|
+
resolve(undefined);
|
|
58
58
|
return;
|
|
59
59
|
});
|
|
60
60
|
}
|
|
@@ -63,13 +63,13 @@ var UtilCom;
|
|
|
63
63
|
res.on('data', (chunk) => resdata += chunk);
|
|
64
64
|
res.on('error', (e) => {
|
|
65
65
|
UtilLogger_1.SLogger.warn(`${funcName} 接收反馈错误:${e}`);
|
|
66
|
-
resolve(
|
|
66
|
+
resolve(undefined);
|
|
67
67
|
return;
|
|
68
68
|
});
|
|
69
69
|
res.on('end', () => {
|
|
70
70
|
if (resdata == "") {
|
|
71
71
|
UtilLogger_1.SLogger.warn(funcName + " 接收反馈错误: resdata 为空");
|
|
72
|
-
resolve(
|
|
72
|
+
resolve(undefined);
|
|
73
73
|
return;
|
|
74
74
|
}
|
|
75
75
|
try {
|
|
@@ -80,14 +80,14 @@ var UtilCom;
|
|
|
80
80
|
}
|
|
81
81
|
catch (e) {
|
|
82
82
|
UtilLogger_1.SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
|
|
83
|
-
resolve(
|
|
83
|
+
resolve(undefined);
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
catch (err) {
|
|
89
89
|
UtilLogger_1.SLogger.warn(`${funcName} 未知错误:${err}`);
|
|
90
|
-
resolve(
|
|
90
|
+
resolve(undefined);
|
|
91
91
|
return;
|
|
92
92
|
}
|
|
93
93
|
};
|
|
@@ -106,7 +106,7 @@ var UtilCom;
|
|
|
106
106
|
}
|
|
107
107
|
req.on('error', (e) => {
|
|
108
108
|
UtilLogger_1.SLogger.warn(`${funcName} 发送请求错误:${e}`);
|
|
109
|
-
resolve(
|
|
109
|
+
resolve(undefined);
|
|
110
110
|
});
|
|
111
111
|
req.write(jsonStr);
|
|
112
112
|
req.end();
|
|
@@ -119,10 +119,10 @@ var UtilCom;
|
|
|
119
119
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
120
120
|
* @returns 结果 null 为未能成功接收
|
|
121
121
|
*/
|
|
122
|
-
function
|
|
123
|
-
return
|
|
122
|
+
function httpsPost(json, options, timeLimit) {
|
|
123
|
+
return post("https", json, options, timeLimit);
|
|
124
124
|
}
|
|
125
|
-
UtilCom.
|
|
125
|
+
UtilCom.httpsPost = httpsPost;
|
|
126
126
|
/**发送一个 http POST请求并接受数据
|
|
127
127
|
* @async
|
|
128
128
|
* @param json - 数据对象
|
|
@@ -130,10 +130,10 @@ var UtilCom;
|
|
|
130
130
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
131
131
|
* @returns 结果 null 为未能成功接收
|
|
132
132
|
*/
|
|
133
|
-
function
|
|
134
|
-
return
|
|
133
|
+
function httpPost(json, options, timeLimit) {
|
|
134
|
+
return post("http", json, options, timeLimit);
|
|
135
135
|
}
|
|
136
|
-
UtilCom.
|
|
136
|
+
UtilCom.httpPost = httpPost;
|
|
137
137
|
/**通用重复post处理
|
|
138
138
|
* @async
|
|
139
139
|
* @param posttype - post类型
|
|
@@ -147,11 +147,11 @@ var UtilCom;
|
|
|
147
147
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
148
148
|
* @returns 结果 null 为未能成功接收
|
|
149
149
|
*/
|
|
150
|
-
async function
|
|
150
|
+
async function repeatPost(posttype, json, options, verifyFn, opt = {}) {
|
|
151
151
|
opt.count = opt.count ?? 3;
|
|
152
152
|
opt.tryInterval = opt.tryInterval ?? 180;
|
|
153
153
|
opt.tryDelay = opt.tryDelay ?? 1;
|
|
154
|
-
const procFn = () =>
|
|
154
|
+
const procFn = () => post(posttype, json, options, opt.postTimeLimit);
|
|
155
155
|
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, opt);
|
|
156
156
|
}
|
|
157
157
|
/**重复一个 https POST请求并接受数据
|
|
@@ -166,10 +166,10 @@ var UtilCom;
|
|
|
166
166
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
167
167
|
* @returns 结果 null 为未能成功接收
|
|
168
168
|
*/
|
|
169
|
-
function
|
|
170
|
-
return
|
|
169
|
+
function httpsRepeatPost(json, options, verifyFn, opt) {
|
|
170
|
+
return repeatPost("https", json, options, verifyFn, opt);
|
|
171
171
|
}
|
|
172
|
-
UtilCom.
|
|
172
|
+
UtilCom.httpsRepeatPost = httpsRepeatPost;
|
|
173
173
|
/**重复一个 http POST请求并接受数据
|
|
174
174
|
* Object ()
|
|
175
175
|
* @async
|
|
@@ -183,8 +183,8 @@ var UtilCom;
|
|
|
183
183
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
184
184
|
* @returns 结果 null 为未能成功接收
|
|
185
185
|
*/
|
|
186
|
-
function
|
|
187
|
-
return
|
|
186
|
+
function httpRepeatPost(json, options, verifyFn, opt) {
|
|
187
|
+
return repeatPost("http", json, options, verifyFn, opt);
|
|
188
188
|
}
|
|
189
|
-
UtilCom.
|
|
189
|
+
UtilCom.httpRepeatPost = httpRepeatPost;
|
|
190
190
|
})(UtilCom || (exports.UtilCom = UtilCom = {}));
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -32,6 +32,12 @@ export type RepeatPromiseOpt = Partial<{
|
|
|
32
32
|
}>;
|
|
33
33
|
type SuccessOut<T> = Outcome<Success, T>;
|
|
34
34
|
type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
|
|
35
|
+
/**完成的重试请求 */
|
|
36
|
+
export type RepeatPromiseResult<T> = {
|
|
37
|
+
completed: T;
|
|
38
|
+
/**还未完成的其他Promise 若是验证失败则会返回undefined */
|
|
39
|
+
pending: Promise<T | undefined>[];
|
|
40
|
+
};
|
|
35
41
|
/**遍历对象的回调函数
|
|
36
42
|
* @param key - 字段名
|
|
37
43
|
* @param value - 字段值
|
|
@@ -105,12 +111,12 @@ export declare class UtilFunc {
|
|
|
105
111
|
* @param procFn - 发起函数
|
|
106
112
|
* @param verifyFn - 验证函数
|
|
107
113
|
* @param opt - 可选参数
|
|
108
|
-
* @param opt.count
|
|
109
|
-
* @param opt.
|
|
110
|
-
* @param opt.
|
|
111
|
-
* @returns 结果
|
|
114
|
+
* @param opt.count - 重试次数 默认3
|
|
115
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
116
|
+
* @param opt.tryDelay - 重试间隔时间 默认0
|
|
117
|
+
* @returns 结果 undefined 为全部失败/超时
|
|
112
118
|
*/
|
|
113
|
-
static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<T |
|
|
119
|
+
static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<T> | undefined>;
|
|
114
120
|
/**创建一个限时的Promise
|
|
115
121
|
* @param func - 处理函数
|
|
116
122
|
* @param timeLimit - 毫秒限时
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -170,10 +170,10 @@ class UtilFunc {
|
|
|
170
170
|
* @param procFn - 发起函数
|
|
171
171
|
* @param verifyFn - 验证函数
|
|
172
172
|
* @param opt - 可选参数
|
|
173
|
-
* @param opt.count
|
|
174
|
-
* @param opt.
|
|
175
|
-
* @param opt.
|
|
176
|
-
* @returns 结果
|
|
173
|
+
* @param opt.count - 重试次数 默认3
|
|
174
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
175
|
+
* @param opt.tryDelay - 重试间隔时间 默认0
|
|
176
|
+
* @returns 结果 undefined 为全部失败/超时
|
|
177
177
|
*/
|
|
178
178
|
static async repeatPromise(procFn, verifyFn, opt = {}) {
|
|
179
179
|
opt.count = opt.count ?? 3;
|
|
@@ -247,15 +247,34 @@ class UtilFunc {
|
|
|
247
247
|
},
|
|
248
248
|
});
|
|
249
249
|
if (result !== UtilSymbol_1.None)
|
|
250
|
-
return
|
|
250
|
+
return {
|
|
251
|
+
completed: result,
|
|
252
|
+
pending: plist
|
|
253
|
+
.filter((p, i) => i != postresult.index)
|
|
254
|
+
.filter(p => p != UtilFunc.getNeverResolvedPromise())
|
|
255
|
+
.map(async (p) => {
|
|
256
|
+
const curObj = await p;
|
|
257
|
+
if (curObj.status == UtilSymbol_1.Success) {
|
|
258
|
+
const outres = curObj.result;
|
|
259
|
+
if (outres.stat == UtilSymbol_1.Success)
|
|
260
|
+
return outres.result;
|
|
261
|
+
}
|
|
262
|
+
if (curObj.status == UtilSymbol_1.Timeout) {
|
|
263
|
+
const outres = await curObj.result;
|
|
264
|
+
if (outres.stat == UtilSymbol_1.Success)
|
|
265
|
+
return outres.result;
|
|
266
|
+
}
|
|
267
|
+
return undefined;
|
|
268
|
+
}),
|
|
269
|
+
};
|
|
251
270
|
}
|
|
252
|
-
//全部失败或超时则返回
|
|
271
|
+
//全部失败或超时则返回 undefined
|
|
253
272
|
UtilLogger_1.SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
|
|
254
|
-
return
|
|
273
|
+
return undefined;
|
|
255
274
|
}
|
|
256
275
|
catch (err) {
|
|
257
276
|
UtilLogger_1.SLogger.warn(`repeatPromise 发生错误`, err);
|
|
258
|
-
return
|
|
277
|
+
return undefined;
|
|
259
278
|
}
|
|
260
279
|
}
|
|
261
280
|
/**创建一个限时的Promise
|
|
@@ -273,17 +292,19 @@ class UtilFunc {
|
|
|
273
292
|
if (clearTimer)
|
|
274
293
|
clearTimer();
|
|
275
294
|
});
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
const timer = setTimeout(() =>
|
|
295
|
+
if (timeLimit) {
|
|
296
|
+
const timerP = new Promise((timeResolve) => {
|
|
297
|
+
const timer = setTimeout(() => timeResolve(UtilFunc.outcome(UtilSymbol_1.Timeout, procer)), timeLimit);
|
|
279
298
|
clearTimer = () => {
|
|
280
|
-
|
|
299
|
+
timeResolve(UtilFunc.outcome(UtilSymbol_1.Timeout, procer));
|
|
281
300
|
clearInterval(timer);
|
|
282
301
|
};
|
|
283
|
-
})
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
302
|
+
});
|
|
303
|
+
reslove(Promise.race([procerP, timerP]));
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
reslove(procerP);
|
|
307
|
+
}
|
|
287
308
|
});
|
|
288
309
|
}
|
|
289
310
|
/**对对象的每个属性应用映射函数,并返回一个新的对象。
|
package/dist/UtilLogger.d.ts
CHANGED
|
@@ -83,7 +83,7 @@ export declare class SLogger {
|
|
|
83
83
|
* @param level - log等级 === null时不产生log
|
|
84
84
|
* @returns 格式化的时间字符串
|
|
85
85
|
*/
|
|
86
|
-
timeEnd(flag: string, level?: LogLevel | null): string |
|
|
86
|
+
timeEnd(flag: string, level?: LogLevel | null): string | undefined;
|
|
87
87
|
/**名称为default的slogger实例 */
|
|
88
88
|
private static get defaultInstance();
|
|
89
89
|
/**让名称为default的logger 产生一条对应等级的log 返回自身
|
package/dist/UtilLogger.js
CHANGED
|
@@ -242,7 +242,7 @@ class SLogger {
|
|
|
242
242
|
const start = SLogger.timeTable[flag];
|
|
243
243
|
if (start == null) {
|
|
244
244
|
this.warn("SLogger.timeEnd 错误 flag:" + flag + " 不存在");
|
|
245
|
-
return
|
|
245
|
+
return;
|
|
246
246
|
}
|
|
247
247
|
const timelen = process.hrtime(start);
|
|
248
248
|
const totalMicroseconds = (timelen[0] * 1e9 + timelen[1]) / 1000;
|
package/package.json
CHANGED
package/src/UtilCom.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { AnyString, JObject, ReqVerifyFn } from "@src/UtilInterfaces";
|
|
|
2
2
|
import * as https from 'https';
|
|
3
3
|
import * as http from 'http';
|
|
4
4
|
import { SLogger } from "@src/UtilLogger";
|
|
5
|
-
import { RepeatPromiseOpt, UtilFunc } from "@src/UtilFunctions";
|
|
5
|
+
import { RepeatPromiseOpt, RepeatPromiseResult, UtilFunc } from "@src/UtilFunctions";
|
|
6
6
|
|
|
7
7
|
export type ComPostOption = {
|
|
8
8
|
/**请求域名 */
|
|
@@ -35,9 +35,9 @@ export namespace UtilCom{
|
|
|
35
35
|
* @param json - 数据对象
|
|
36
36
|
* @param options - 参数对象
|
|
37
37
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
38
|
-
* @returns 结果
|
|
38
|
+
* @returns 结果 undefined 为未能成功接收
|
|
39
39
|
*/
|
|
40
|
-
function
|
|
40
|
+
function post(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|undefined>{
|
|
41
41
|
//转换为毫秒
|
|
42
42
|
const hasTimeLimit = (timeLimit ? timeLimit>=10 : false );
|
|
43
43
|
if(hasTimeLimit && timeLimit!=undefined) timeLimit*=1000;
|
|
@@ -54,7 +54,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLi
|
|
|
54
54
|
res.setTimeout(fixlimit, ()=>{
|
|
55
55
|
//res.abort();
|
|
56
56
|
SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
|
|
57
|
-
resolve(
|
|
57
|
+
resolve(undefined);
|
|
58
58
|
return;
|
|
59
59
|
});
|
|
60
60
|
}
|
|
@@ -65,14 +65,14 @@ function sPost(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLi
|
|
|
65
65
|
|
|
66
66
|
res.on('error',(e)=>{
|
|
67
67
|
SLogger.warn(`${funcName} 接收反馈错误:${e}`);
|
|
68
|
-
resolve(
|
|
68
|
+
resolve(undefined);
|
|
69
69
|
return;
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
res.on('end',()=>{
|
|
73
73
|
if(resdata==""){
|
|
74
74
|
SLogger.warn(funcName+" 接收反馈错误: resdata 为空");
|
|
75
|
-
resolve(
|
|
75
|
+
resolve(undefined);
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
78
|
try{
|
|
@@ -83,13 +83,13 @@ function sPost(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLi
|
|
|
83
83
|
}
|
|
84
84
|
catch(e){
|
|
85
85
|
SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
|
|
86
|
-
resolve(
|
|
86
|
+
resolve(undefined);
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
89
|
});
|
|
90
90
|
}catch(err){
|
|
91
91
|
SLogger.warn(`${funcName} 未知错误:${err}`);
|
|
92
|
-
resolve(
|
|
92
|
+
resolve(undefined);
|
|
93
93
|
return;
|
|
94
94
|
}
|
|
95
95
|
};
|
|
@@ -110,7 +110,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLi
|
|
|
110
110
|
|
|
111
111
|
req.on('error', (e)=>{
|
|
112
112
|
SLogger.warn(`${funcName} 发送请求错误:${e}`);
|
|
113
|
-
resolve(
|
|
113
|
+
resolve(undefined);
|
|
114
114
|
});
|
|
115
115
|
|
|
116
116
|
req.write(jsonStr);
|
|
@@ -125,8 +125,8 @@ function sPost(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLi
|
|
|
125
125
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
126
126
|
* @returns 结果 null 为未能成功接收
|
|
127
127
|
*/
|
|
128
|
-
export function
|
|
129
|
-
return
|
|
128
|
+
export function httpsPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|undefined>{
|
|
129
|
+
return post("https",json,options,timeLimit);
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
/**发送一个 http POST请求并接受数据
|
|
@@ -136,8 +136,8 @@ export function shttpsPost(json:JObject,options:ComPostOption,timeLimit?:number)
|
|
|
136
136
|
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
137
137
|
* @returns 结果 null 为未能成功接收
|
|
138
138
|
*/
|
|
139
|
-
export function
|
|
140
|
-
return
|
|
139
|
+
export function httpPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|undefined>{
|
|
140
|
+
return post("http",json,options,timeLimit);
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
|
|
@@ -155,12 +155,13 @@ export function shttpPost(json:JObject,options:ComPostOption,timeLimit?:number):
|
|
|
155
155
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
156
156
|
* @returns 结果 null 为未能成功接收
|
|
157
157
|
*/
|
|
158
|
-
async function
|
|
158
|
+
async function repeatPost(posttype:"http"|"https",json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|undefined>,opt:RepeatPostOpt={}):
|
|
159
|
+
Promise<RepeatPromiseResult<JObject|undefined>|undefined>{
|
|
159
160
|
opt.count = opt.count??3;
|
|
160
161
|
opt.tryInterval = opt.tryInterval??180;
|
|
161
162
|
opt.tryDelay = opt.tryDelay??1;
|
|
162
163
|
|
|
163
|
-
const procFn = ()=>
|
|
164
|
+
const procFn = ()=>post(posttype,json,options,opt.postTimeLimit);
|
|
164
165
|
return UtilFunc.repeatPromise(procFn,verifyFn,opt);
|
|
165
166
|
}
|
|
166
167
|
|
|
@@ -177,8 +178,9 @@ async function sRepeatPost(posttype:"http"|"https",json:JObject,options:ComPostO
|
|
|
177
178
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
178
179
|
* @returns 结果 null 为未能成功接收
|
|
179
180
|
*/
|
|
180
|
-
export function
|
|
181
|
-
|
|
181
|
+
export function httpsRepeatPost(json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|undefined>,opt?:RepeatPostOpt):
|
|
182
|
+
Promise<RepeatPromiseResult<JObject|undefined>|undefined>{
|
|
183
|
+
return repeatPost("https",json,options,verifyFn,opt);
|
|
182
184
|
}
|
|
183
185
|
|
|
184
186
|
/**重复一个 http POST请求并接受数据
|
|
@@ -194,8 +196,9 @@ export function shttpsRepeatPost(json:JObject,options:ComPostOption,verifyFn?:Re
|
|
|
194
196
|
* @param opt.try_delay - 重试间隔 秒 默认0
|
|
195
197
|
* @returns 结果 null 为未能成功接收
|
|
196
198
|
*/
|
|
197
|
-
export function
|
|
198
|
-
|
|
199
|
+
export function httpRepeatPost(json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|undefined>,opt?:RepeatPostOpt):
|
|
200
|
+
Promise<RepeatPromiseResult<JObject|undefined>|undefined>{
|
|
201
|
+
return repeatPost("http",json,options,verifyFn,opt);
|
|
199
202
|
}
|
|
200
203
|
|
|
201
204
|
}
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -56,6 +56,14 @@ type PromiseResult<T> = {
|
|
|
56
56
|
index:number;
|
|
57
57
|
};
|
|
58
58
|
|
|
59
|
+
/**完成的重试请求 */
|
|
60
|
+
export type RepeatPromiseResult<T> = {
|
|
61
|
+
completed:T;
|
|
62
|
+
/**还未完成的其他Promise 若是验证失败则会返回undefined */
|
|
63
|
+
pending:Promise<T|undefined>[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
59
67
|
/**遍历对象的回调函数
|
|
60
68
|
* @param key - 字段名
|
|
61
69
|
* @param value - 字段值
|
|
@@ -216,13 +224,14 @@ static getNeverResolvedPromise<T>():Promise<T>{
|
|
|
216
224
|
* @param procFn - 发起函数
|
|
217
225
|
* @param verifyFn - 验证函数
|
|
218
226
|
* @param opt - 可选参数
|
|
219
|
-
* @param opt.count
|
|
220
|
-
* @param opt.
|
|
221
|
-
* @param opt.
|
|
222
|
-
* @returns 结果
|
|
227
|
+
* @param opt.count - 重试次数 默认3
|
|
228
|
+
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
229
|
+
* @param opt.tryDelay - 重试间隔时间 默认0
|
|
230
|
+
* @returns 结果 undefined 为全部失败/超时
|
|
223
231
|
*/
|
|
224
232
|
@LogTimeAsync("repeatPromise ",true)
|
|
225
|
-
static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):
|
|
233
|
+
static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):
|
|
234
|
+
Promise<RepeatPromiseResult<T>|undefined>{
|
|
226
235
|
opt.count = opt.count??3;
|
|
227
236
|
opt.tryInterval = opt.tryInterval??180;
|
|
228
237
|
let {count,tryInterval} = opt;
|
|
@@ -300,14 +309,31 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt
|
|
|
300
309
|
return None;
|
|
301
310
|
},
|
|
302
311
|
});
|
|
303
|
-
if(result !== None) return
|
|
312
|
+
if(result !== None) return {
|
|
313
|
+
completed:result,
|
|
314
|
+
pending:plist
|
|
315
|
+
.filter((p,i)=>i!=postresult.index)
|
|
316
|
+
.filter(p=>p!=UtilFunc.getNeverResolvedPromise())
|
|
317
|
+
.map(async p=>{
|
|
318
|
+
const curObj = await p;
|
|
319
|
+
if(curObj.status==Success){
|
|
320
|
+
const outres = curObj.result;
|
|
321
|
+
if(outres.stat==Success) return outres.result;
|
|
322
|
+
}
|
|
323
|
+
if(curObj.status==Timeout){
|
|
324
|
+
const outres = await curObj.result;
|
|
325
|
+
if(outres.stat==Success) return outres.result;
|
|
326
|
+
}
|
|
327
|
+
return undefined;
|
|
328
|
+
}),
|
|
329
|
+
};
|
|
304
330
|
}
|
|
305
|
-
//全部失败或超时则返回
|
|
331
|
+
//全部失败或超时则返回 undefined
|
|
306
332
|
SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
|
|
307
|
-
return
|
|
333
|
+
return undefined;
|
|
308
334
|
}catch(err){
|
|
309
335
|
SLogger.warn(`repeatPromise 发生错误`,err);
|
|
310
|
-
return
|
|
336
|
+
return undefined;
|
|
311
337
|
}
|
|
312
338
|
}
|
|
313
339
|
|
|
@@ -329,21 +355,21 @@ static timelimitPromise<T>
|
|
|
329
355
|
if(clearTimer) clearTimer();
|
|
330
356
|
});
|
|
331
357
|
|
|
332
|
-
|
|
333
|
-
|
|
358
|
+
if(timeLimit){
|
|
359
|
+
const timerP = new Promise<TimeoutOut<T>>((timeResolve)=>{
|
|
334
360
|
const timer = setTimeout(()=>
|
|
335
|
-
|
|
336
|
-
,timeLimit);//无限制则无限时间
|
|
361
|
+
timeResolve(UtilFunc.outcome(Timeout,procer)), timeLimit);
|
|
337
362
|
|
|
338
363
|
clearTimer = ()=>{
|
|
339
|
-
|
|
364
|
+
timeResolve(UtilFunc.outcome(Timeout,procer))
|
|
340
365
|
clearInterval(timer)
|
|
341
366
|
}
|
|
342
|
-
})
|
|
343
|
-
: UtilFunc.getNeverResolvedPromise<TimeoutOut<T>>();
|
|
367
|
+
});
|
|
344
368
|
|
|
345
|
-
|
|
346
|
-
|
|
369
|
+
reslove(Promise.race([procerP,timerP]));
|
|
370
|
+
}else{
|
|
371
|
+
reslove(procerP);
|
|
372
|
+
}
|
|
347
373
|
})
|
|
348
374
|
}
|
|
349
375
|
|
package/src/UtilLogger.ts
CHANGED
|
@@ -230,11 +230,11 @@ export class SLogger{
|
|
|
230
230
|
* @param level - log等级 === null时不产生log
|
|
231
231
|
* @returns 格式化的时间字符串
|
|
232
232
|
*/
|
|
233
|
-
timeEnd(flag:string,level:LogLevel|null="info"):string|
|
|
233
|
+
timeEnd(flag:string,level:LogLevel|null="info"):string|undefined{
|
|
234
234
|
const start = SLogger.timeTable[flag];
|
|
235
235
|
if(start==null){
|
|
236
236
|
this.warn("SLogger.timeEnd 错误 flag:"+flag+" 不存在");
|
|
237
|
-
return
|
|
237
|
+
return;
|
|
238
238
|
}
|
|
239
239
|
const timelen = process.hrtime(start as any as [number,number]);
|
|
240
240
|
|