@zwa73/utils 1.0.42 → 1.0.44

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,7 +1,7 @@
1
1
  import { JObject } from "./UtilInterfaces";
2
2
  /**网络工具 */
3
3
  export declare namespace UtilCom {
4
- /**发送一个POST请求并接受数据
4
+ /**发送一个 https POST请求并接受数据
5
5
  * Object ()
6
6
  * @async
7
7
  * @param {JObject} json - 数据对象
@@ -10,7 +10,7 @@ export declare namespace UtilCom {
10
10
  * @returns {Promise<Object|null>} 结果 null 为未能成功接收
11
11
  */
12
12
  function shttpsPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
13
- /**发送一个POST请求并接受数据
13
+ /**发送一个 http POST请求并接受数据
14
14
  * Object ()
15
15
  * @async
16
16
  * @param {JObject} json - 数据对象
@@ -19,4 +19,35 @@ export declare namespace UtilCom {
19
19
  * @returns {Promise<Object|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
+ /**重复一个 https POST请求并接受数据
30
+ * Object ()
31
+ * @async
32
+ * @param {JObject} json - 数据对象
33
+ * @param {Object} options - 参数对象
34
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
35
+ * @param {number} [repeatCount] - 重试次数
36
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
37
+ * @param {PostProc} [procFunc] - 判断有效性函数
38
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
39
+ */
40
+ function shttpsRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, procFunc?: PostProc): Promise<JObject | null>;
41
+ /**重复一个 http POST请求并接受数据
42
+ * Object ()
43
+ * @async
44
+ * @param {JObject} json - 数据对象
45
+ * @param {Object} options - 参数对象
46
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
47
+ * @param {number} [repeatCount] - 重试次数
48
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
49
+ * @param {PostProc} [procFunc] - 判断有效性函数
50
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
51
+ */
52
+ function shttpRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, procFunc?: PostProc): Promise<JObject | null>;
22
53
  }
package/dist/UtilCom.js CHANGED
@@ -5,16 +5,24 @@ const UtilInterfaces_1 = require("./UtilInterfaces");
5
5
  const https = require("https");
6
6
  const http = require("http");
7
7
  const UtilLogger_1 = require("./UtilLogger");
8
+ const UtilFunctions_1 = require("./UtilFunctions");
8
9
  /**网络工具 */
9
10
  var UtilCom;
10
11
  (function (UtilCom) {
11
- function sPost(type, json, options, timeLimit = -1) {
12
+ /**通用post处理
13
+ * @param {"http"|"https"} posttype - post类型
14
+ * @param {JObject} json - 数据对象
15
+ * @param {Object} options - 参数对象
16
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
17
+ * @returns {Promise<Object|null>} 结果 null 为未能成功接收
18
+ */
19
+ function sPost(posttype, json, options, timeLimit = -1) {
12
20
  //转换为毫秒
13
21
  const hasTimeLimit = (timeLimit >= 10);
14
22
  if (hasTimeLimit)
15
23
  timeLimit *= 1000;
16
24
  const jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
17
- const funcName = "s" + type + "Psot";
25
+ const funcName = "s" + posttype + "Psot";
18
26
  return new Promise((resolve, rejecte) => {
19
27
  const resFunc = (res) => {
20
28
  try {
@@ -62,9 +70,9 @@ var UtilCom;
62
70
  };
63
71
  //路由 http/https
64
72
  let req = null;
65
- if (type === "https")
73
+ if (posttype === "https")
66
74
  req = https.request(options, resFunc);
67
- else if (type === "http")
75
+ else if (posttype === "http")
68
76
  req = http.request(options, resFunc);
69
77
  //请求超时
70
78
  if (hasTimeLimit) {
@@ -81,7 +89,7 @@ var UtilCom;
81
89
  req.end();
82
90
  });
83
91
  }
84
- /**发送一个POST请求并接受数据
92
+ /**发送一个 https POST请求并接受数据
85
93
  * Object ()
86
94
  * @async
87
95
  * @param {JObject} json - 数据对象
@@ -93,7 +101,7 @@ var UtilCom;
93
101
  return sPost("https", json, options, timeLimit);
94
102
  }
95
103
  UtilCom.shttpsPost = shttpsPost;
96
- /**发送一个POST请求并接受数据
104
+ /**发送一个 http POST请求并接受数据
97
105
  * Object ()
98
106
  * @async
99
107
  * @param {JObject} json - 数据对象
@@ -105,4 +113,121 @@ var UtilCom;
105
113
  return sPost("http", json, options, timeLimit);
106
114
  }
107
115
  UtilCom.shttpPost = shttpPost;
116
+ /**通用重复post处理
117
+ * @async
118
+ * @param {"http"|"https"} posttype - post类型
119
+ * @param {JObject} json - 数据对象
120
+ * @param {Object} options - 参数对象
121
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
122
+ * @param {number} [repeatCount] - 重试次数
123
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
124
+ * @param {PostProc} [procFunc] - 判断有效性函数
125
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
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;
202
+ }
203
+ /**重复一个 https POST请求并接受数据
204
+ * Object ()
205
+ * @async
206
+ * @param {JObject} json - 数据对象
207
+ * @param {Object} options - 参数对象
208
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
209
+ * @param {number} [repeatCount] - 重试次数
210
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
211
+ * @param {PostProc} [procFunc] - 判断有效性函数
212
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
213
+ */
214
+ function shttpsRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, procFunc) {
215
+ return sRepeatPost("https", json, options, timeLimit, repeatCount, repeatTime, procFunc);
216
+ }
217
+ UtilCom.shttpsRepeatPost = shttpsRepeatPost;
218
+ /**重复一个 http POST请求并接受数据
219
+ * Object ()
220
+ * @async
221
+ * @param {JObject} json - 数据对象
222
+ * @param {Object} options - 参数对象
223
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
224
+ * @param {number} [repeatCount] - 重试次数
225
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
226
+ * @param {PostProc} [procFunc] - 判断有效性函数
227
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
228
+ */
229
+ function shttpRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, procFunc) {
230
+ return sRepeatPost("http", json, options, timeLimit, repeatCount, repeatTime, procFunc);
231
+ }
232
+ UtilCom.shttpRepeatPost = shttpRepeatPost;
108
233
  })(UtilCom = exports.UtilCom || (exports.UtilCom = {}));
@@ -49,4 +49,6 @@ export declare namespace UtilFunc {
49
49
  stdout: string;
50
50
  stderr: string;
51
51
  }>;
52
+ /**获得一个永不完成的Promise单例 */
53
+ function getNeverResolvedPromise<T>(): Promise<T>;
52
54
  }
@@ -92,4 +92,10 @@ var UtilFunc;
92
92
  });
93
93
  }
94
94
  UtilFunc.exec = exec;
95
+ const NeverResolvedPromise = new Promise(() => { });
96
+ /**获得一个永不完成的Promise单例 */
97
+ function getNeverResolvedPromise() {
98
+ return NeverResolvedPromise;
99
+ }
100
+ UtilFunc.getNeverResolvedPromise = getNeverResolvedPromise;
95
101
  })(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.42",
3
+ "version": "1.0.44",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilCom.ts CHANGED
@@ -2,19 +2,27 @@ import { JObject, stringifyJToken } from "./UtilInterfaces";
2
2
  import * as https from 'https';
3
3
  import * as http from 'http';
4
4
  import { SLogger } from "./UtilLogger";
5
+ import { UtilFunc } from "./UtilFunctions";
5
6
 
6
7
 
7
8
  /**网络工具 */
8
9
  export namespace UtilCom{
9
10
 
10
- function sPost(type:"http"|"https",json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
11
+ /**通用post处理
12
+ * @param {"http"|"https"} posttype - post类型
13
+ * @param {JObject} json - 数据对象
14
+ * @param {Object} options - 参数对象
15
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
16
+ * @returns {Promise<Object|null>} 结果 null 为未能成功接收
17
+ */
18
+ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
11
19
  //转换为毫秒
12
20
  const hasTimeLimit = (timeLimit>=10);
13
21
  if(hasTimeLimit)
14
22
  timeLimit*=1000
15
23
 
16
24
  const jsonStr = stringifyJToken(json);
17
- const funcName = "s"+type+"Psot";
25
+ const funcName = "s"+posttype+"Psot";
18
26
 
19
27
  return new Promise((resolve, rejecte)=>{
20
28
  const resFunc = (res:http.IncomingMessage)=>{
@@ -65,9 +73,9 @@ function sPost(type:"http"|"https",json:JObject,options:Object,timeLimit:number=
65
73
  };
66
74
  //路由 http/https
67
75
  let req:http.ClientRequest=null as any as http.ClientRequest;
68
- if(type === "https")
76
+ if(posttype === "https")
69
77
  req = https.request(options, resFunc);
70
- else if(type === "http")
78
+ else if(posttype === "http")
71
79
  req = http.request(options, resFunc);
72
80
 
73
81
  //请求超时
@@ -88,8 +96,8 @@ function sPost(type:"http"|"https",json:JObject,options:Object,timeLimit:number=
88
96
  });
89
97
  }
90
98
 
91
- /**发送一个POST请求并接受数据
92
- * Object ()
99
+ /**发送一个 https POST请求并接受数据
100
+ * Object ()
93
101
  * @async
94
102
  * @param {JObject} json - 数据对象
95
103
  * @param {Object} options - 参数对象
@@ -100,8 +108,8 @@ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Prom
100
108
  return sPost("https",json,options,timeLimit);
101
109
  }
102
110
 
103
- /**发送一个POST请求并接受数据
104
- * Object ()
111
+ /**发送一个 http POST请求并接受数据
112
+ * Object ()
105
113
  * @async
106
114
  * @param {JObject} json - 数据对象
107
115
  * @param {Object} options - 参数对象
@@ -111,4 +119,147 @@ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Prom
111
119
  export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
112
120
  return sPost("http",json,options,timeLimit);
113
121
  }
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;
139
+
140
+ /**通用重复post处理
141
+ * @async
142
+ * @param {"http"|"https"} posttype - post类型
143
+ * @param {JObject} json - 数据对象
144
+ * @param {Object} options - 参数对象
145
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
146
+ * @param {number} [repeatCount] - 重试次数
147
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
148
+ * @param {PostProc} [procFunc] - 判断有效性函数
149
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
150
+ */
151
+ 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;
230
+ }
231
+
232
+
233
+ /**重复一个 https POST请求并接受数据
234
+ * Object ()
235
+ * @async
236
+ * @param {JObject} json - 数据对象
237
+ * @param {Object} options - 参数对象
238
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
239
+ * @param {number} [repeatCount] - 重试次数
240
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
241
+ * @param {PostProc} [procFunc] - 判断有效性函数
242
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
243
+ */
244
+ 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);
247
+ }
248
+
249
+ /**重复一个 http POST请求并接受数据
250
+ * Object ()
251
+ * @async
252
+ * @param {JObject} json - 数据对象
253
+ * @param {Object} options - 参数对象
254
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
255
+ * @param {number} [repeatCount] - 重试次数
256
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
257
+ * @param {PostProc} [procFunc] - 判断有效性函数
258
+ * @returns {Promise<Object|null>} - 结果 null 为未能成功接收
259
+ */
260
+ 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);
263
+ }
264
+
114
265
  }
@@ -91,5 +91,11 @@ export function exec(command: string) {
91
91
  });
92
92
  }
93
93
 
94
+ const NeverResolvedPromise = new Promise(()=>{});
95
+ /**获得一个永不完成的Promise单例 */
96
+ export function getNeverResolvedPromise<T>():Promise<T>{
97
+ return NeverResolvedPromise as Promise<T>;
98
+ }
99
+
94
100
  }
95
101
 
@@ -69,4 +69,4 @@ export type ExclusiveRecord<B, T, K extends string[], TMP = ExclusiveRecursive<B
69
69
 
70
70
  /**符合JObject约束的互斥表 */
71
71
  export type ExclusiveJObject<B extends JObject,T extends JToken,K extends string[],
72
- TMP extends JArray = ExclusiveRecursive<B,T,K>> = TMP[number];
72
+ TMP extends JArray = ExclusiveRecursive<B,T,K>> = TMP[number];