@zwa73/utils 1.0.44 → 1.0.45

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 CHANGED
@@ -1,4 +1,4 @@
1
- import { JObject } from "./UtilInterfaces";
1
+ import { JObject, PromiseVerifyFn } from "./UtilInterfaces";
2
2
  /**网络工具 */
3
3
  export declare namespace UtilCom {
4
4
  /**发送一个 https POST请求并接受数据
@@ -7,7 +7,7 @@ export declare namespace UtilCom {
7
7
  * @param {JObject} json - 数据对象
8
8
  * @param {Object} options - 参数对象
9
9
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
10
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
10
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
11
11
  */
12
12
  function shttpsPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
13
13
  /**发送一个 http POST请求并接受数据
@@ -16,16 +16,9 @@ export declare namespace UtilCom {
16
16
  * @param {JObject} json - 数据对象
17
17
  * @param {Object} options - 参数对象
18
18
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
19
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
19
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
20
20
  */
21
21
  function shttpPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
22
- /**请求完成状态 成功/失败/终止
23
- * 成功/终止 将直接返回
24
- * 失败 将重试
25
- */
26
- type PostStat = "Completed" | "Failed" | "Terminated";
27
- /**post处理函数 */
28
- type PostProc = (respObj: JObject | null) => Promise<PostStat> | PostStat;
29
22
  /**重复一个 https POST请求并接受数据
30
23
  * Object ()
31
24
  * @async
@@ -34,10 +27,10 @@ export declare namespace UtilCom {
34
27
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
35
28
  * @param {number} [repeatCount] - 重试次数
36
29
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
37
- * @param {PostProc} [procFunc] - 判断有效性函数
38
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
30
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
31
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
39
32
  */
40
- function shttpsRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, procFunc?: PostProc): Promise<JObject | null>;
33
+ function shttpsRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, verifyFn?: PromiseVerifyFn<JObject | null>): Promise<JObject | null>;
41
34
  /**重复一个 http POST请求并接受数据
42
35
  * Object ()
43
36
  * @async
@@ -46,8 +39,8 @@ export declare namespace UtilCom {
46
39
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
47
40
  * @param {number} [repeatCount] - 重试次数
48
41
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
49
- * @param {PostProc} [procFunc] - 判断有效性函数
50
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
42
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
43
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
51
44
  */
52
- function shttpRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, procFunc?: PostProc): Promise<JObject | null>;
45
+ function shttpRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, verifyFn?: PromiseVerifyFn<JObject | null>): Promise<JObject | null>;
53
46
  }
package/dist/UtilCom.js CHANGED
@@ -14,7 +14,7 @@ var UtilCom;
14
14
  * @param {JObject} json - 数据对象
15
15
  * @param {Object} options - 参数对象
16
16
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
17
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
17
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
18
18
  */
19
19
  function sPost(posttype, json, options, timeLimit = -1) {
20
20
  //转换为毫秒
@@ -95,7 +95,7 @@ var UtilCom;
95
95
  * @param {JObject} json - 数据对象
96
96
  * @param {Object} options - 参数对象
97
97
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
98
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
98
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
99
99
  */
100
100
  function shttpsPost(json, options, timeLimit = -1) {
101
101
  return sPost("https", json, options, timeLimit);
@@ -107,7 +107,7 @@ var UtilCom;
107
107
  * @param {JObject} json - 数据对象
108
108
  * @param {Object} options - 参数对象
109
109
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
110
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
110
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
111
111
  */
112
112
  function shttpPost(json, options, timeLimit = -1) {
113
113
  return sPost("http", json, options, timeLimit);
@@ -121,84 +121,12 @@ var UtilCom;
121
121
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
122
122
  * @param {number} [repeatCount] - 重试次数
123
123
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
124
- * @param {PostProc} [procFunc] - 判断有效性函数
125
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
124
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
125
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
126
126
  */
127
- async function sRepeatPost(posttype, json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, procFunc) {
128
- //转换为毫秒
129
- const hasRepeatTime = (repeatTime >= 10);
130
- if (hasRepeatTime)
131
- repeatTime *= 1000;
132
- //验证处理函数
133
- if (procFunc === undefined)
134
- procFunc = () => "Completed";
135
- //计时器
136
- let timer = null;
137
- let timerP = null;
138
- let resolveFn = null;
139
- /**清理计时器 */
140
- const clearTimer = () => {
141
- if (timer != null)
142
- clearInterval(timer);
143
- if (resolveFn != null)
144
- resolveFn("Timeout");
145
- timerP = null;
146
- timer = null;
147
- resolveFn = null;
148
- };
149
- //进行中的请求
150
- const plist = new Array(repeatCount).fill(UtilFunctions_1.UtilFunc.getNeverResolvedPromise());
151
- for (let i = 0; i < repeatCount;) {
152
- UtilLogger_1.SLogger.info("开始第 " + (i + 1) + " 次 sRepeatPost");
153
- //创建当前任务
154
- plist[i] = sPost(posttype, json, options, timeLimit)
155
- .then(result => ({ result, stat: procFunc(result), index: i }));
156
- //创建定时器
157
- if (timerP == null) {
158
- timerP = new Promise(function (resolve, rejecte) {
159
- resolveFn = resolve;
160
- timer = setTimeout(() => resolve("Timeout"), hasRepeatTime ? repeatTime : Infinity); //无限制则无限时间
161
- });
162
- }
163
- //等待完成
164
- const currObj = await Promise.race([...plist, timerP]);
165
- //超时处理
166
- if (currObj == "Timeout") {
167
- UtilLogger_1.SLogger.warn(`第 ${(i + 1)} 次 sRepeatPost 超时 ${repeatTime} ms 开始重试`);
168
- clearTimer();
169
- i++;
170
- continue;
171
- }
172
- else {
173
- const poststat = await currObj.stat;
174
- switch (poststat) {
175
- case "Completed": //完成
176
- UtilLogger_1.SLogger.info("第 " + (currObj.index + 1) + " 次 sRepeatPost 成功");
177
- clearTimer();
178
- return currObj.result;
179
- case "Terminated": //终止
180
- UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 sRepeatPost 终止 停止重试");
181
- clearTimer();
182
- return currObj.result;
183
- case "Failed": //验证失败
184
- //是当前
185
- if (currObj.index == i) {
186
- UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 sRepeatPost 失败 开始重试");
187
- clearTimer();
188
- i++;
189
- continue;
190
- }
191
- //非当前
192
- UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 sRepeatPost 失败");
193
- //抛弃失败
194
- plist[currObj.index] = UtilFunctions_1.UtilFunc.getNeverResolvedPromise();
195
- continue;
196
- }
197
- }
198
- }
199
- clearTimer();
200
- //全部失败或超时则返回null
201
- return null;
127
+ async function sRepeatPost(posttype, json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, verifyFn) {
128
+ const procFn = () => sPost(posttype, json, options, timeLimit);
129
+ return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatCount, repeatTime);
202
130
  }
203
131
  /**重复一个 https POST请求并接受数据
204
132
  * Object ()
@@ -208,11 +136,11 @@ var UtilCom;
208
136
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
209
137
  * @param {number} [repeatCount] - 重试次数
210
138
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
211
- * @param {PostProc} [procFunc] - 判断有效性函数
212
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
139
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
140
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
213
141
  */
214
- function shttpsRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, procFunc) {
215
- return sRepeatPost("https", json, options, timeLimit, repeatCount, repeatTime, procFunc);
142
+ function shttpsRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, verifyFn) {
143
+ return sRepeatPost("https", json, options, timeLimit, repeatCount, repeatTime, verifyFn);
216
144
  }
217
145
  UtilCom.shttpsRepeatPost = shttpsRepeatPost;
218
146
  /**重复一个 http POST请求并接受数据
@@ -223,11 +151,11 @@ var UtilCom;
223
151
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
224
152
  * @param {number} [repeatCount] - 重试次数
225
153
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
226
- * @param {PostProc} [procFunc] - 判断有效性函数
227
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
154
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
155
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
228
156
  */
229
- function shttpRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, procFunc) {
230
- return sRepeatPost("http", json, options, timeLimit, repeatCount, repeatTime, procFunc);
157
+ function shttpRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, verifyFn) {
158
+ return sRepeatPost("http", json, options, timeLimit, repeatCount, repeatTime, verifyFn);
231
159
  }
232
160
  UtilCom.shttpRepeatPost = shttpRepeatPost;
233
161
  })(UtilCom = exports.UtilCom || (exports.UtilCom = {}));
@@ -1,4 +1,4 @@
1
- import { JToken } from "./UtilInterfaces";
1
+ import { JToken, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
2
2
  /**常用函数 */
3
3
  export declare namespace UtilFunc {
4
4
  /**获取当前时间戳
@@ -51,4 +51,6 @@ export declare namespace UtilFunc {
51
51
  }>;
52
52
  /**获得一个永不完成的Promise单例 */
53
53
  function getNeverResolvedPromise<T>(): Promise<T>;
54
+ /**重复尝试promise */
55
+ function repeatPromise<T>(procFn: PromiseProcFn<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
54
56
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UtilFunc = void 0;
4
4
  const crypto = require("crypto");
5
5
  const cp = require("child_process");
6
+ const UtilLogger_1 = require("./UtilLogger");
6
7
  /**常用函数 */
7
8
  var UtilFunc;
8
9
  (function (UtilFunc) {
@@ -98,4 +99,82 @@ var UtilFunc;
98
99
  return NeverResolvedPromise;
99
100
  }
100
101
  UtilFunc.getNeverResolvedPromise = getNeverResolvedPromise;
102
+ /**重复尝试promise */
103
+ async function repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
104
+ //转换为毫秒
105
+ const hasRepeatTime = (repeatTime >= 10);
106
+ if (hasRepeatTime)
107
+ repeatTime *= 1000;
108
+ //验证处理函数
109
+ if (verifyFn === undefined)
110
+ verifyFn = () => "Completed";
111
+ //计时器
112
+ let timer = null;
113
+ let timerP = null;
114
+ let resolveFn = null;
115
+ /**清理计时器 */
116
+ const clearTimer = () => {
117
+ if (timer != null)
118
+ clearInterval(timer);
119
+ if (resolveFn != null)
120
+ resolveFn("Timeout");
121
+ timerP = null;
122
+ timer = null;
123
+ resolveFn = null;
124
+ };
125
+ //进行中的请求
126
+ const plist = new Array(repeatCount)
127
+ .fill(UtilFunc.getNeverResolvedPromise());
128
+ for (let i = 0; i < repeatCount;) {
129
+ UtilLogger_1.SLogger.info("开始第 " + (i + 1) + " 次 repeatPromise");
130
+ //创建当前任务
131
+ plist[i] = procFn().then(result => ({ result, stat: verifyFn(result), index: i }));
132
+ //创建定时器
133
+ if (timerP == null) {
134
+ timerP = new Promise(function (resolve, rejecte) {
135
+ resolveFn = resolve;
136
+ timer = setTimeout(() => resolve("Timeout"), hasRepeatTime ? repeatTime : Infinity); //无限制则无限时间
137
+ });
138
+ }
139
+ //等待完成
140
+ const currObj = await Promise.race([...plist, timerP]);
141
+ //超时处理
142
+ if (currObj == "Timeout") {
143
+ UtilLogger_1.SLogger.warn(`第 ${(i + 1)} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
144
+ clearTimer();
145
+ i++;
146
+ continue;
147
+ }
148
+ else {
149
+ const poststat = await currObj.stat;
150
+ switch (poststat) {
151
+ case "Completed": //完成
152
+ UtilLogger_1.SLogger.info("第 " + (currObj.index + 1) + " 次 repeatPromise 成功");
153
+ clearTimer();
154
+ return currObj.result;
155
+ case "Terminated": //终止
156
+ UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 repeatPromise 终止 停止重试");
157
+ clearTimer();
158
+ return currObj.result;
159
+ case "Failed": //验证失败
160
+ //是当前
161
+ if (currObj.index == i) {
162
+ UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 repeatPromise 失败 开始重试");
163
+ clearTimer();
164
+ i++;
165
+ continue;
166
+ }
167
+ //非当前
168
+ UtilLogger_1.SLogger.warn("第 " + (currObj.index + 1) + " 次 repeatPromise 失败");
169
+ //抛弃失败
170
+ plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
171
+ continue;
172
+ }
173
+ }
174
+ }
175
+ clearTimer();
176
+ //全部失败或超时则返回null
177
+ return null;
178
+ }
179
+ UtilFunc.repeatPromise = repeatPromise;
101
180
  })(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
@@ -54,4 +54,22 @@ type ExclusiveRecursive<B, T, K extends string[], R extends unknown[] = []> = R[
54
54
  export type ExclusiveRecord<B, T, K extends string[], TMP = ExclusiveRecursive<B, T, K>> = TMP[keyof TMP];
55
55
  /**符合JObject约束的互斥表 */
56
56
  export type ExclusiveJObject<B extends JObject, T extends JToken, K extends string[], TMP extends JArray = ExclusiveRecursive<B, T, K>> = TMP[number];
57
+ /**进行中的请求 */
58
+ export type ProcessingPromise<T> = {
59
+ /**主体请求 */
60
+ result: T;
61
+ /**请求状态 */
62
+ stat: PromiseStat | Promise<PromiseStat>;
63
+ /**请求下标/序号 */
64
+ index: number;
65
+ };
66
+ /**请求完成状态 成功/失败/终止
67
+ * 成功/终止 将直接返回
68
+ * 失败 将重试
69
+ */
70
+ export type PromiseStat = "Completed" | "Failed" | "Terminated";
71
+ /**promise验证函数 */
72
+ export type PromiseVerifyFn<T> = (obj: T) => Promise<PromiseStat> | PromiseStat;
73
+ /**发起promise的函数 */
74
+ export type PromiseProcFn<T> = () => Promise<T>;
57
75
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilCom.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JObject, stringifyJToken } from "./UtilInterfaces";
1
+ import { JObject, ProcessingPromise, PromiseVerifyFn, stringifyJToken } from "./UtilInterfaces";
2
2
  import * as https from 'https';
3
3
  import * as http from 'http';
4
4
  import { SLogger } from "./UtilLogger";
@@ -13,7 +13,7 @@ export namespace UtilCom{
13
13
  * @param {JObject} json - 数据对象
14
14
  * @param {Object} options - 参数对象
15
15
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
16
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
16
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
17
17
  */
18
18
  function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
19
19
  //转换为毫秒
@@ -102,7 +102,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
102
102
  * @param {JObject} json - 数据对象
103
103
  * @param {Object} options - 参数对象
104
104
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
105
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
105
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
106
106
  */
107
107
  export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
108
108
  return sPost("https",json,options,timeLimit);
@@ -114,28 +114,13 @@ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Prom
114
114
  * @param {JObject} json - 数据对象
115
115
  * @param {Object} options - 参数对象
116
116
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
117
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
117
+ * @returns {Promise<JObject|null>} 结果 null 为未能成功接收
118
118
  */
119
119
  export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
120
120
  return sPost("http",json,options,timeLimit);
121
121
  }
122
122
 
123
- /**进行中的请求 */
124
- type ProcessingPost = {
125
- /**主体请求 */
126
- result:JObject|null;
127
- /**请求状态 */
128
- stat:PostStat|Promise<PostStat>;
129
- /**请求下标/序号 */
130
- index:number;
131
- }
132
- /**请求完成状态 成功/失败/终止
133
- * 成功/终止 将直接返回
134
- * 失败 将重试
135
- */
136
- export type PostStat = "Completed"|"Failed"|"Terminated";
137
- /**post处理函数 */
138
- export type PostProc = (respObj:JObject|null)=>Promise<PostStat>|PostStat;
123
+
139
124
 
140
125
  /**通用重复post处理
141
126
  * @async
@@ -145,88 +130,13 @@ export type PostProc = (respObj:JObject|null)=>Promise<PostStat>|PostStat;
145
130
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
146
131
  * @param {number} [repeatCount] - 重试次数
147
132
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
148
- * @param {PostProc} [procFunc] - 判断有效性函数
149
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
133
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
134
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
150
135
  */
151
136
  async function sRepeatPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1,
152
- repeatCount:number=3,repeatTime:number=180,procFunc?:PostProc):Promise<JObject|null>{
153
- //转换为毫秒
154
- const hasRepeatTime = (repeatTime>=10);
155
- if(hasRepeatTime) repeatTime*=1000;
156
-
157
- //验证处理函数
158
- if(procFunc===undefined)
159
- procFunc = ()=>"Completed";
160
-
161
- //计时器
162
- let timer:NodeJS.Timer|null = null;
163
- let timerP:Promise<"Timeout">|null=null;
164
- let resolveFn:((value: "Timeout" | PromiseLike<"Timeout">)=>void)|null = null;
165
- /**清理计时器 */
166
- const clearTimer = ()=>{
167
- if(timer!=null)
168
- clearInterval(timer);
169
- if(resolveFn!=null)
170
- resolveFn("Timeout");
171
- timerP=null;
172
- timer=null;
173
- resolveFn=null;
174
- }
175
-
176
- //进行中的请求
177
- const plist:Promise<ProcessingPost>[] = new Array(repeatCount).fill(UtilFunc.getNeverResolvedPromise());
178
- for(let i=0;i<repeatCount;){
179
- SLogger.info("开始第 "+(i+1)+" 次 sRepeatPost");
180
- //创建当前任务
181
- plist[i] = sPost(posttype,json,options,timeLimit)
182
- .then(result => ({result, stat:procFunc!(result), index:i}));
183
-
184
- //创建定时器
185
- if(timerP==null){
186
- timerP = new Promise<"Timeout">(function(resolve, rejecte){
187
- resolveFn = resolve;
188
- timer = setTimeout(()=>resolve("Timeout"),
189
- hasRepeatTime? repeatTime:Infinity);//无限制则无限时间
190
- })
191
- }
192
-
193
- //等待完成
194
- const currObj = await Promise.race([...plist, timerP]);
195
-
196
- //超时处理
197
- if(currObj=="Timeout"){
198
- SLogger.warn(`第 ${(i+1)} 次 sRepeatPost 超时 ${repeatTime} ms 开始重试`);
199
- clearTimer(); i++;
200
- continue;
201
- }else{
202
- const poststat = await currObj.stat;
203
- switch(poststat){
204
- case "Completed"://完成
205
- SLogger.info("第 "+(currObj.index+1)+" 次 sRepeatPost 成功");
206
- clearTimer();
207
- return currObj.result;
208
- case "Terminated"://终止
209
- SLogger.warn("第 "+(currObj.index+1)+" 次 sRepeatPost 终止 停止重试");
210
- clearTimer();
211
- return currObj.result;
212
- case "Failed"://验证失败
213
- //是当前
214
- if(currObj.index==i){
215
- SLogger.warn("第 "+(currObj.index+1)+" 次 sRepeatPost 失败 开始重试");
216
- clearTimer(); i++;
217
- continue;
218
- }
219
- //非当前
220
- SLogger.warn("第 "+(currObj.index+1)+" 次 sRepeatPost 失败");
221
- //抛弃失败
222
- plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
223
- continue;
224
- }
225
- }
226
- }
227
- clearTimer();
228
- //全部失败或超时则返回null
229
- return null;
137
+ repeatCount:number=3,repeatTime:number=180,verifyFn?:PromiseVerifyFn<JObject|null>):Promise<JObject|null>{
138
+ const procFn = ()=>sPost(posttype,json,options,timeLimit);
139
+ return UtilFunc.repeatPromise(procFn,verifyFn,repeatCount,repeatTime);
230
140
  }
231
141
 
232
142
 
@@ -238,12 +148,12 @@ async function sRepeatPost(posttype:"http"|"https",json:JObject,options:Object,t
238
148
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
239
149
  * @param {number} [repeatCount] - 重试次数
240
150
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
241
- * @param {PostProc} [procFunc] - 判断有效性函数
242
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
151
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
152
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
243
153
  */
244
154
  export function shttpsRepeatPost(json:JObject,options:Object,timeLimit:number=-1,
245
- repeatCount:number=3,repeatTime:number=180,procFunc?:PostProc):Promise<JObject|null>{
246
- return sRepeatPost("https",json,options,timeLimit,repeatCount,repeatTime,procFunc);
155
+ repeatCount:number=3,repeatTime:number=180,verifyFn?:PromiseVerifyFn<JObject|null>):Promise<JObject|null>{
156
+ return sRepeatPost("https",json,options,timeLimit,repeatCount,repeatTime,verifyFn);
247
157
  }
248
158
 
249
159
  /**重复一个 http POST请求并接受数据
@@ -254,12 +164,12 @@ export function shttpsRepeatPost(json:JObject,options:Object,timeLimit:number=-1
254
164
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
255
165
  * @param {number} [repeatCount] - 重试次数
256
166
  * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
257
- * @param {PostProc} [procFunc] - 判断有效性函数
258
- * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
167
+ * @param {PromiseVerifyFn<JObject|null>} [verifyFn] - 判断有效性函数
168
+ * @returns {Promise<JObject|null>} - 结果 null 为未能成功接收
259
169
  */
260
170
  export function shttpRepeatPost(json:JObject,options:Object,timeLimit:number=-1,
261
- repeatCount:number=3,repeatTime:number=180,procFunc?:PostProc):Promise<JObject|null>{
262
- return sRepeatPost("http",json,options,timeLimit,repeatCount,repeatTime,procFunc);
171
+ repeatCount:number=3,repeatTime:number=180,verifyFn?:PromiseVerifyFn<JObject|null>):Promise<JObject|null>{
172
+ return sRepeatPost("http",json,options,timeLimit,repeatCount,repeatTime,verifyFn);
263
173
  }
264
174
 
265
175
  }
@@ -1,19 +1,20 @@
1
1
  import * as crypto from "crypto";
2
- import { JToken } from "./UtilInterfaces";
2
+ import { JToken, ProcessingPromise, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
3
3
  import * as cp from "child_process";
4
+ import { SLogger } from "./UtilLogger";
4
5
 
5
6
  /**常用函数 */
6
7
  export namespace UtilFunc{
7
- /**获取当前时间戳
8
- * number ()
8
+ /**获取当前时间戳
9
+ * number ()
9
10
  * @returns {number} 时间戳
10
11
  */
11
12
  export function getTime(): number {
12
13
  return new Date().getTime();
13
14
  }
14
15
 
15
- /**初始化对象的字段
16
- * void (Object,string,any)
16
+ /**初始化对象的字段
17
+ * void (Object,string,any)
17
18
  * @param {Record<string,T>} obj - 所要初始化的对象
18
19
  * @param {string} field - 所要初始化的字段
19
20
  * @param {T} defaultVal - 默认值
@@ -28,15 +29,15 @@ export function initField<T>(
28
29
  return obj[field];
29
30
  }
30
31
 
31
- /**生成一串uuid
32
- * string ()
32
+ /**生成一串uuid
33
+ * string ()
33
34
  * @returns {string} uuid
34
35
  */
35
36
  export function genUUID() {
36
37
  return crypto.randomBytes(16).toString("hex");
37
38
  }
38
- /**计算Hash
39
- * string ()
39
+ /**计算Hash
40
+ * string ()
40
41
  * @param {string} str - 待计算的字符串
41
42
  * @returns {string} hash
42
43
  */
@@ -44,7 +45,7 @@ export function calcHash(str:string) {
44
45
  return crypto.createHash('md5').update(str).digest('hex');
45
46
  }
46
47
 
47
- /**深克隆 序列化并反序列化
48
+ /**深克隆 序列化并反序列化
48
49
  * @template {T} T - JToken类型的泛型
49
50
  * @param {T} obj - 克隆目标
50
51
  * @returns {T} 克隆结果
@@ -63,7 +64,7 @@ export function isSafeNumber(num: number): boolean {
63
64
  return false;
64
65
  }
65
66
 
66
- /**等待 timeMs 毫秒
67
+ /**等待 timeMs 毫秒
67
68
  * @async
68
69
  * @param {number} timeMs - 等待的毫秒数
69
70
  * @returns {Promise<boolean>}
@@ -76,7 +77,7 @@ export async function sleep(timeMs: number): Promise<boolean> {
76
77
  });
77
78
  }
78
79
 
79
- /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
80
+ /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
80
81
  * @param {string} command 指令文本
81
82
  */
82
83
  export function exec(command: string) {
@@ -97,5 +98,88 @@ export function getNeverResolvedPromise<T>():Promise<T>{
97
98
  return NeverResolvedPromise as Promise<T>;
98
99
  }
99
100
 
101
+ /**重复尝试promise */
102
+ export async function repeatPromise<T>(procFn:PromiseProcFn<T>,verifyFn?:PromiseVerifyFn<T>,
103
+ repeatCount:number=3,repeatTime:number=180):Promise<T|null>{
104
+ //转换为毫秒
105
+ const hasRepeatTime = (repeatTime>=10);
106
+ if(hasRepeatTime) repeatTime*=1000;
107
+
108
+ //验证处理函数
109
+ if(verifyFn===undefined)
110
+ verifyFn = ()=>"Completed";
111
+
112
+ //计时器
113
+ let timer:NodeJS.Timer|null = null;
114
+ let timerP:Promise<"Timeout">|null=null;
115
+ let resolveFn:((value: "Timeout" | PromiseLike<"Timeout">)=>void)|null = null;
116
+ /**清理计时器 */
117
+ const clearTimer = ()=>{
118
+ if(timer!=null)
119
+ clearInterval(timer);
120
+ if(resolveFn!=null)
121
+ resolveFn("Timeout");
122
+ timerP=null;
123
+ timer=null;
124
+ resolveFn=null;
125
+ }
126
+
127
+ //进行中的请求
128
+ const plist:Promise<ProcessingPromise<T>>[] = new Array(repeatCount)
129
+ .fill(UtilFunc.getNeverResolvedPromise());
130
+ for(let i=0;i<repeatCount;){
131
+ SLogger.info("开始第 "+(i+1)+" 次 repeatPromise");
132
+ //创建当前任务
133
+ plist[i] = procFn().then(result =>
134
+ ({result, stat:verifyFn!(result), index:i}));
135
+
136
+ //创建定时器
137
+ if(timerP==null){
138
+ timerP = new Promise<"Timeout">(function(resolve, rejecte){
139
+ resolveFn = resolve;
140
+ timer = setTimeout(()=>resolve("Timeout"),
141
+ hasRepeatTime? repeatTime:Infinity);//无限制则无限时间
142
+ })
143
+ }
144
+
145
+ //等待完成
146
+ const currObj = await Promise.race([...plist, timerP]);
147
+
148
+ //超时处理
149
+ if(currObj=="Timeout"){
150
+ SLogger.warn(`第 ${(i+1)} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
151
+ clearTimer(); i++;
152
+ continue;
153
+ }else{
154
+ const poststat = await currObj.stat;
155
+ switch(poststat){
156
+ case "Completed"://完成
157
+ SLogger.info("第 "+(currObj.index+1)+" 次 repeatPromise 成功");
158
+ clearTimer();
159
+ return currObj.result;
160
+ case "Terminated"://终止
161
+ SLogger.warn("第 "+(currObj.index+1)+" 次 repeatPromise 终止 停止重试");
162
+ clearTimer();
163
+ return currObj.result;
164
+ case "Failed"://验证失败
165
+ //是当前
166
+ if(currObj.index==i){
167
+ SLogger.warn("第 "+(currObj.index+1)+" 次 repeatPromise 失败 开始重试");
168
+ clearTimer(); i++;
169
+ continue;
170
+ }
171
+ //非当前
172
+ SLogger.warn("第 "+(currObj.index+1)+" 次 repeatPromise 失败");
173
+ //抛弃失败
174
+ plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
175
+ continue;
176
+ }
177
+ }
178
+ }
179
+ clearTimer();
180
+ //全部失败或超时则返回null
181
+ return null;
182
+ }
183
+
100
184
  }
101
185
 
@@ -70,3 +70,24 @@ export type ExclusiveRecord<B, T, K extends string[], TMP = ExclusiveRecursive<B
70
70
  /**符合JObject约束的互斥表 */
71
71
  export type ExclusiveJObject<B extends JObject,T extends JToken,K extends string[],
72
72
  TMP extends JArray = ExclusiveRecursive<B,T,K>> = TMP[number];
73
+
74
+
75
+ /**进行中的请求 */
76
+ export type ProcessingPromise<T> = {
77
+ /**主体请求 */
78
+ result:T;
79
+ /**请求状态 */
80
+ stat:PromiseStat|Promise<PromiseStat>;
81
+ /**请求下标/序号 */
82
+ index:number;
83
+ };
84
+
85
+ /**请求完成状态 成功/失败/终止
86
+ * 成功/终止 将直接返回
87
+ * 失败 将重试
88
+ */
89
+ export type PromiseStat = "Completed"|"Failed"|"Terminated";
90
+ /**promise验证函数 */
91
+ export type PromiseVerifyFn<T> = (obj:T)=>Promise<PromiseStat>|PromiseStat;
92
+ /**发起promise的函数 */
93
+ export type PromiseProcFn<T> = ()=>Promise<T>;