@zwa73/utils 1.0.174 → 1.0.176
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 +101 -54
- package/dist/UtilCom.js +117 -120
- package/dist/UtilFunctions.d.ts +3 -6
- package/dist/UtilFunctions.js +5 -11
- package/package.json +1 -1
- package/src/UtilCom.ts +183 -138
- package/src/UtilFunctions.ts +7 -12
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
import { AnyString, JObject, ReqVerifyFn } from "./UtilInterfaces";
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import { RepeatPromiseOpt, RepeatPromiseResult } from "./UtilFunctions";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
/**网络请求返回值 */
|
|
5
|
+
export type ComResp<T> = {
|
|
6
|
+
/**响应头 */
|
|
7
|
+
headers: http.IncomingHttpHeaders;
|
|
8
|
+
/**响应状态码 */
|
|
9
|
+
statusCode?: number;
|
|
10
|
+
/**响应数据 */
|
|
11
|
+
data: T;
|
|
12
|
+
};
|
|
13
|
+
/**网络请求选项 */
|
|
14
|
+
export type ComReqOpt = {
|
|
7
15
|
/**请求协议 */
|
|
8
16
|
protocol: 'http' | 'https';
|
|
17
|
+
/**超时时间/毫秒 最小为10_000 默认无限 */
|
|
18
|
+
timeLimit?: number;
|
|
19
|
+
} & {
|
|
9
20
|
/**请求域名 */
|
|
10
21
|
hostname: string;
|
|
11
22
|
/**请求路径 */
|
|
@@ -20,93 +31,129 @@ export type ComRequestOption = {
|
|
|
20
31
|
'Content-Type'?: 'application/json' | AnyString;
|
|
21
32
|
/**内容长度 一般无需填写 应为buffer长度而非字符串长度 */
|
|
22
33
|
'Content-Length'?: number;
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
headers: http.IncomingHttpHeaders;
|
|
28
|
-
/**响应状态码 */
|
|
29
|
-
statusCode?: number;
|
|
30
|
-
/**响应数据 */
|
|
31
|
-
data: JObject;
|
|
32
|
-
};
|
|
34
|
+
};
|
|
35
|
+
} & http.RequestOptions;
|
|
36
|
+
/**get请求所允许的数据 */
|
|
37
|
+
export type GetReqData = NodeJS.Dict<string | number | boolean | readonly string[] | readonly number[] | readonly boolean[] | null>;
|
|
33
38
|
/**网络工具 */
|
|
34
39
|
export declare namespace UtilCom {
|
|
40
|
+
/**网络请求
|
|
41
|
+
* @param comReqOpt - 网络请求选项
|
|
42
|
+
* @param procReq - 请求处理函数
|
|
43
|
+
* @param reduceData - 数据处理函数
|
|
44
|
+
* @param initData - 初始数据
|
|
45
|
+
*/
|
|
46
|
+
function comReq<T>(comReqOpt: ComReqOpt, procReq: ((req: http.ClientRequest) => void | Promise<void>), reduceData: (acc: T, data: string) => T, initData: T): Promise<ComResp<T> | undefined>;
|
|
35
47
|
/**发送一个 https POST 请求并接受数据
|
|
36
48
|
* @async
|
|
37
|
-
* @param reqData - 数据对象
|
|
38
49
|
* @param comReqOpt - 请求参数
|
|
50
|
+
* @param reqData - 数据对象
|
|
39
51
|
* @returns 结果 undefined 为未能成功接收
|
|
40
52
|
*/
|
|
41
|
-
function httpsPost(
|
|
53
|
+
function httpsPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData: JObject | undefined): Promise<{
|
|
54
|
+
data: JObject;
|
|
55
|
+
/**响应头 */
|
|
56
|
+
headers: http.IncomingHttpHeaders;
|
|
57
|
+
/**响应状态码 */
|
|
58
|
+
statusCode?: number;
|
|
59
|
+
} | undefined>;
|
|
42
60
|
/**发送一个 http POST 请求并接受数据
|
|
43
61
|
* @async
|
|
44
|
-
* @param reqData - 数据对象
|
|
45
62
|
* @param comReqOpt - 请求参数
|
|
63
|
+
* @param reqData - 数据对象
|
|
46
64
|
* @returns 结果 undefined 为未能成功接收
|
|
47
65
|
*/
|
|
48
|
-
function httpPost(
|
|
66
|
+
function httpPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData: JObject | undefined): Promise<{
|
|
67
|
+
data: JObject;
|
|
68
|
+
/**响应头 */
|
|
69
|
+
headers: http.IncomingHttpHeaders;
|
|
70
|
+
/**响应状态码 */
|
|
71
|
+
statusCode?: number;
|
|
72
|
+
} | undefined>;
|
|
49
73
|
/**发送一个 https GET 请求并接受数据
|
|
50
74
|
* @async
|
|
51
|
-
* @param reqData - 数据对象
|
|
52
75
|
* @param comReqOpt - 请求参数
|
|
76
|
+
* @param reqData - 数据对象
|
|
53
77
|
* @returns 结果 undefined 为未能成功接收
|
|
54
78
|
*/
|
|
55
|
-
function httpsGet(
|
|
79
|
+
function httpsGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData: Record<string, string | number | boolean>): Promise<{
|
|
80
|
+
data: JObject;
|
|
81
|
+
/**响应头 */
|
|
82
|
+
headers: http.IncomingHttpHeaders;
|
|
83
|
+
/**响应状态码 */
|
|
84
|
+
statusCode?: number;
|
|
85
|
+
} | undefined>;
|
|
56
86
|
/**发送一个 http GET 请求并接受数据
|
|
57
87
|
* @async
|
|
58
|
-
* @param reqData - 数据对象
|
|
59
88
|
* @param comReqOpt - 请求参数
|
|
89
|
+
* @param reqData - 数据对象
|
|
60
90
|
* @returns 结果 undefined 为未能成功接收
|
|
61
91
|
*/
|
|
62
|
-
function httpGet(
|
|
92
|
+
function httpGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData: Record<string, string | number | boolean>): Promise<{
|
|
93
|
+
data: JObject;
|
|
94
|
+
/**响应头 */
|
|
95
|
+
headers: http.IncomingHttpHeaders;
|
|
96
|
+
/**响应状态码 */
|
|
97
|
+
statusCode?: number;
|
|
98
|
+
} | undefined>;
|
|
63
99
|
/**重复一个 https POST请求并接受数据
|
|
64
100
|
* @async
|
|
65
|
-
* @param
|
|
66
|
-
* @param
|
|
67
|
-
* @param verifyFn
|
|
68
|
-
* @param repeatOpt
|
|
69
|
-
* @param opt.count - 重试次数 默认3
|
|
70
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
71
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
101
|
+
* @param comReqOpt - 网络请求选项
|
|
102
|
+
* @param reqData - 数据对象
|
|
103
|
+
* @param verifyFn - 有效性验证函数
|
|
104
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
72
105
|
* @returns 结果 undefined 为未能成功接收
|
|
73
106
|
*/
|
|
74
|
-
function httpsRepeatPost(
|
|
107
|
+
function httpsRepeatPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: JObject, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
|
|
108
|
+
data: JObject;
|
|
109
|
+
/**响应头 */
|
|
110
|
+
headers: http.IncomingHttpHeaders;
|
|
111
|
+
/**响应状态码 */
|
|
112
|
+
statusCode?: number;
|
|
113
|
+
} | undefined> | undefined>;
|
|
75
114
|
/**重复一个 http POST请求并接受数据
|
|
76
115
|
* @async
|
|
77
|
-
* @param
|
|
78
|
-
* @param
|
|
79
|
-
* @param verifyFn
|
|
80
|
-
* @param repeatOpt
|
|
81
|
-
* @param opt.count - 重试次数 默认3
|
|
82
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
83
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
116
|
+
* @param comReqOpt - 网络请求选项
|
|
117
|
+
* @param reqData - 数据对象
|
|
118
|
+
* @param verifyFn - 有效性验证函数
|
|
119
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
84
120
|
* @returns 结果 undefined 为未能成功接收
|
|
85
121
|
*/
|
|
86
|
-
function httpRepeatPost(
|
|
122
|
+
function httpRepeatPost(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: JObject, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
|
|
123
|
+
data: JObject;
|
|
124
|
+
/**响应头 */
|
|
125
|
+
headers: http.IncomingHttpHeaders;
|
|
126
|
+
/**响应状态码 */
|
|
127
|
+
statusCode?: number;
|
|
128
|
+
} | undefined> | undefined>;
|
|
87
129
|
/**重复一个 https GET 请求并接受数据
|
|
88
130
|
* @async
|
|
89
|
-
* @param
|
|
90
|
-
* @param
|
|
91
|
-
* @param verifyFn
|
|
92
|
-
* @param repeatOpt
|
|
93
|
-
* @param opt.count - 重试次数 默认3
|
|
94
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
95
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
131
|
+
* @param comReqOpt - 网络请求选项
|
|
132
|
+
* @param reqData - 数据对象
|
|
133
|
+
* @param verifyFn - 有效性验证函数
|
|
134
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
96
135
|
* @returns 结果 undefined 为未能成功接收
|
|
97
136
|
*/
|
|
98
|
-
function httpsRepeatGet(
|
|
137
|
+
function httpsRepeatGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: GetReqData, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
|
|
138
|
+
data: JObject;
|
|
139
|
+
/**响应头 */
|
|
140
|
+
headers: http.IncomingHttpHeaders;
|
|
141
|
+
/**响应状态码 */
|
|
142
|
+
statusCode?: number;
|
|
143
|
+
} | undefined> | undefined>;
|
|
99
144
|
/**重复一个 http GET 请求并接受数据
|
|
100
145
|
* @async
|
|
101
|
-
* @param
|
|
102
|
-
* @param
|
|
103
|
-
* @param verifyFn
|
|
104
|
-
* @param repeatOpt
|
|
105
|
-
* @param opt.count - 重试次数 默认3
|
|
106
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
107
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
146
|
+
* @param comReqOpt - 网络请求选项
|
|
147
|
+
* @param reqData - 数据对象
|
|
148
|
+
* @param verifyFn - 有效性验证函数
|
|
149
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
108
150
|
* @returns 结果 undefined 为未能成功接收
|
|
109
151
|
*/
|
|
110
|
-
function httpRepeatGet(
|
|
152
|
+
function httpRepeatGet(comReqOpt: Omit<ComReqOpt, 'protocol' | 'method'>, reqData?: GetReqData, verifyFn?: ReqVerifyFn<JObject | undefined>, repeatOpt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<{
|
|
153
|
+
data: JObject;
|
|
154
|
+
/**响应头 */
|
|
155
|
+
headers: http.IncomingHttpHeaders;
|
|
156
|
+
/**响应状态码 */
|
|
157
|
+
statusCode?: number;
|
|
158
|
+
} | undefined> | undefined>;
|
|
111
159
|
}
|
|
112
|
-
export {};
|
package/dist/UtilCom.js
CHANGED
|
@@ -12,240 +12,237 @@ const querystring_1 = __importDefault(require("querystring"));
|
|
|
12
12
|
/**网络工具 */
|
|
13
13
|
var UtilCom;
|
|
14
14
|
(function (UtilCom) {
|
|
15
|
-
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
18
|
-
* @param
|
|
19
|
-
* @
|
|
15
|
+
/**网络请求
|
|
16
|
+
* @param comReqOpt - 网络请求选项
|
|
17
|
+
* @param procReq - 请求处理函数
|
|
18
|
+
* @param reduceData - 数据处理函数
|
|
19
|
+
* @param initData - 初始数据
|
|
20
20
|
*/
|
|
21
|
-
function comReq(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
timeLimit *= 1000;
|
|
27
|
-
const fixlimit = timeLimit;
|
|
28
|
-
const isPost = baseOpt.method == "POST";
|
|
29
|
-
const isHttps = protocol == "https";
|
|
30
|
-
const jsonStr = isPost ? reqData != undefined ? UtilFunctions_1.UtilFunc.stringifyJToken(reqData) : '' : '';
|
|
31
|
-
const funcName = `${protocol}${baseOpt.method}`;
|
|
32
|
-
if (!isPost && reqData != undefined)
|
|
33
|
-
baseOpt.path += `?${querystring_1.default.stringify(reqData)}`;
|
|
34
|
-
return new Promise((resolve, rejecte) => {
|
|
21
|
+
async function comReq(comReqOpt, procReq, reduceData, initData) {
|
|
22
|
+
const { protocol, timeLimit, ...baseReqOpt } = comReqOpt;
|
|
23
|
+
const hasTimeLimit = (timeLimit ? timeLimit >= 10_000 : false);
|
|
24
|
+
const flagName = `${comReq.name}.${protocol}${baseReqOpt.method}`;
|
|
25
|
+
return new Promise(async (resolve, rejecte) => {
|
|
35
26
|
const resFunc = (res) => {
|
|
36
27
|
try {
|
|
37
28
|
//请求超时
|
|
38
29
|
if (hasTimeLimit) {
|
|
39
|
-
res.setTimeout(
|
|
40
|
-
|
|
41
|
-
UtilLogger_1.SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
|
|
30
|
+
res.setTimeout(timeLimit, () => {
|
|
31
|
+
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈超时(timeLimit): ${timeLimit} ms`);
|
|
42
32
|
resolve(undefined);
|
|
43
33
|
return;
|
|
44
34
|
});
|
|
45
35
|
}
|
|
46
|
-
let
|
|
36
|
+
let mergedata = initData;
|
|
47
37
|
res.setEncoding('utf8');
|
|
48
|
-
res.on('data',
|
|
38
|
+
res.on('data', chunk => mergedata = reduceData(mergedata, chunk));
|
|
49
39
|
res.on('error', (e) => {
|
|
50
|
-
UtilLogger_1.SLogger.warn(`${
|
|
40
|
+
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈错误:${e}`);
|
|
51
41
|
resolve(undefined);
|
|
52
42
|
return;
|
|
53
43
|
});
|
|
54
44
|
res.on('end', () => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
try {
|
|
61
|
-
const obj = JSON.parse(resdata);
|
|
62
|
-
UtilLogger_1.SLogger.http(`${funcName} 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }));
|
|
63
|
-
resolve({
|
|
64
|
-
headers: res.headers,
|
|
65
|
-
statusCode: res.statusCode,
|
|
66
|
-
data: obj,
|
|
67
|
-
});
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
catch (e) {
|
|
71
|
-
UtilLogger_1.SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
|
|
72
|
-
resolve(undefined);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
45
|
+
resolve({
|
|
46
|
+
headers: res.headers,
|
|
47
|
+
statusCode: res.statusCode,
|
|
48
|
+
data: mergedata,
|
|
49
|
+
});
|
|
75
50
|
});
|
|
76
51
|
}
|
|
77
52
|
catch (err) {
|
|
78
|
-
UtilLogger_1.SLogger.warn(`${
|
|
53
|
+
UtilLogger_1.SLogger.warn(`${flagName} 未知错误:${err}`);
|
|
79
54
|
resolve(undefined);
|
|
80
55
|
return;
|
|
81
56
|
}
|
|
82
57
|
};
|
|
83
58
|
//路由 http/https
|
|
84
|
-
const req =
|
|
85
|
-
? https_1.default.request(
|
|
86
|
-
: http_1.default.request(
|
|
59
|
+
const req = protocol == "https"
|
|
60
|
+
? https_1.default.request(baseReqOpt, resFunc)
|
|
61
|
+
: http_1.default.request(baseReqOpt, resFunc);
|
|
87
62
|
//请求超时
|
|
88
63
|
if (hasTimeLimit) {
|
|
89
|
-
req.setTimeout(
|
|
90
|
-
UtilLogger_1.SLogger.warn(`${
|
|
64
|
+
req.setTimeout(timeLimit, () => {
|
|
65
|
+
UtilLogger_1.SLogger.warn(`${flagName} 发送请求超时(timeLimit): ${timeLimit} ms`);
|
|
66
|
+
req.destroy();
|
|
67
|
+
});
|
|
68
|
+
req.on('timeout', () => {
|
|
69
|
+
UtilLogger_1.SLogger.warn(`${flagName} 发送请求超时(timeout): ${timeLimit} ms`);
|
|
91
70
|
req.destroy();
|
|
92
71
|
});
|
|
93
72
|
}
|
|
94
73
|
req.on('error', (e) => {
|
|
95
|
-
UtilLogger_1.SLogger.warn(`${
|
|
74
|
+
UtilLogger_1.SLogger.warn(`${flagName} 发送请求错误:${e}`);
|
|
96
75
|
resolve(undefined);
|
|
97
76
|
});
|
|
98
|
-
|
|
99
|
-
req.write(jsonStr);
|
|
77
|
+
await procReq(req);
|
|
100
78
|
req.end();
|
|
101
79
|
});
|
|
102
80
|
}
|
|
103
|
-
|
|
81
|
+
UtilCom.comReq = comReq;
|
|
82
|
+
/**发送json的网络请求
|
|
83
|
+
* @param comReqOpt - 网络请求选项
|
|
84
|
+
* @param reqData - 数据对象
|
|
85
|
+
* @returns 结果 undefined 为未能成功接收
|
|
86
|
+
*/
|
|
87
|
+
async function jsonReq(comReqOpt, reqData) {
|
|
88
|
+
const { method } = comReqOpt;
|
|
89
|
+
const isPost = (method == "POST");
|
|
90
|
+
if (!isPost && reqData != undefined)
|
|
91
|
+
comReqOpt.path += querystring_1.default.stringify(reqData);
|
|
92
|
+
const procReq = (req) => {
|
|
93
|
+
if (isPost)
|
|
94
|
+
req.write(JSON.stringify(reqData));
|
|
95
|
+
};
|
|
96
|
+
const reduceData = (acc, data) => acc + data;
|
|
97
|
+
const result = await comReq(comReqOpt, procReq, reduceData, "");
|
|
98
|
+
if (result == undefined)
|
|
99
|
+
return undefined;
|
|
100
|
+
const { data, ...rest } = result;
|
|
101
|
+
if (data == "") {
|
|
102
|
+
UtilLogger_1.SLogger.warn(`${jsonReq.name} 接收反馈错误: 原始字符串为空`);
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const obj = JSON.parse(data);
|
|
107
|
+
UtilLogger_1.SLogger.http(`${jsonReq.name} 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }));
|
|
108
|
+
return { ...rest, data: obj };
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
UtilLogger_1.SLogger.warn(`${jsonReq.name} 接收反馈错误:${e}\n原始字符串:${data}`);
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**重复发送json的网络请求
|
|
104
116
|
* @async
|
|
117
|
+
* @param comReqOpt - 网络请求选项
|
|
105
118
|
* @param reqData - 数据对象
|
|
106
|
-
* @param
|
|
107
|
-
* @param
|
|
108
|
-
* @param repeatOpt - 可选参数
|
|
109
|
-
* @param opt.count - 重试次数 默认3
|
|
110
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
111
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
119
|
+
* @param verifyFn - 有效性验证函数
|
|
120
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
112
121
|
* @returns 结果 undefined 为未能成功接收
|
|
113
122
|
*/
|
|
114
|
-
async function
|
|
115
|
-
repeatOpt.tryDelay = repeatOpt.tryDelay ??
|
|
116
|
-
const procFn = () =>
|
|
123
|
+
async function repeatJsonComReq(comReqOpt, reqData, verifyFn, repeatOpt = {}) {
|
|
124
|
+
repeatOpt.tryDelay = repeatOpt.tryDelay ?? 1000;
|
|
125
|
+
const procFn = () => jsonReq(comReqOpt, reqData);
|
|
117
126
|
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatOpt);
|
|
118
127
|
}
|
|
119
128
|
/**发送一个 https POST 请求并接受数据
|
|
120
129
|
* @async
|
|
121
|
-
* @param reqData - 数据对象
|
|
122
130
|
* @param comReqOpt - 请求参数
|
|
131
|
+
* @param reqData - 数据对象
|
|
123
132
|
* @returns 结果 undefined 为未能成功接收
|
|
124
133
|
*/
|
|
125
|
-
function httpsPost(
|
|
126
|
-
return
|
|
134
|
+
function httpsPost(comReqOpt, reqData) {
|
|
135
|
+
return jsonReq({
|
|
127
136
|
...comReqOpt,
|
|
128
137
|
method: "POST",
|
|
129
138
|
protocol: "https",
|
|
130
|
-
});
|
|
139
|
+
}, reqData);
|
|
131
140
|
}
|
|
132
141
|
UtilCom.httpsPost = httpsPost;
|
|
133
142
|
/**发送一个 http POST 请求并接受数据
|
|
134
143
|
* @async
|
|
135
|
-
* @param reqData - 数据对象
|
|
136
144
|
* @param comReqOpt - 请求参数
|
|
145
|
+
* @param reqData - 数据对象
|
|
137
146
|
* @returns 结果 undefined 为未能成功接收
|
|
138
147
|
*/
|
|
139
|
-
function httpPost(
|
|
140
|
-
return
|
|
148
|
+
function httpPost(comReqOpt, reqData) {
|
|
149
|
+
return jsonReq({
|
|
141
150
|
...comReqOpt,
|
|
142
151
|
method: "POST",
|
|
143
152
|
protocol: "http",
|
|
144
|
-
});
|
|
153
|
+
}, reqData);
|
|
145
154
|
}
|
|
146
155
|
UtilCom.httpPost = httpPost;
|
|
147
156
|
/**发送一个 https GET 请求并接受数据
|
|
148
157
|
* @async
|
|
149
|
-
* @param reqData - 数据对象
|
|
150
158
|
* @param comReqOpt - 请求参数
|
|
159
|
+
* @param reqData - 数据对象
|
|
151
160
|
* @returns 结果 undefined 为未能成功接收
|
|
152
161
|
*/
|
|
153
|
-
function httpsGet(
|
|
154
|
-
return
|
|
162
|
+
function httpsGet(comReqOpt, reqData) {
|
|
163
|
+
return jsonReq({
|
|
155
164
|
...comReqOpt,
|
|
156
165
|
method: "GET",
|
|
157
166
|
protocol: "https",
|
|
158
|
-
});
|
|
167
|
+
}, reqData);
|
|
159
168
|
}
|
|
160
169
|
UtilCom.httpsGet = httpsGet;
|
|
161
170
|
/**发送一个 http GET 请求并接受数据
|
|
162
171
|
* @async
|
|
163
|
-
* @param reqData - 数据对象
|
|
164
172
|
* @param comReqOpt - 请求参数
|
|
173
|
+
* @param reqData - 数据对象
|
|
165
174
|
* @returns 结果 undefined 为未能成功接收
|
|
166
175
|
*/
|
|
167
|
-
function httpGet(
|
|
168
|
-
return
|
|
176
|
+
function httpGet(comReqOpt, reqData) {
|
|
177
|
+
return jsonReq({
|
|
169
178
|
...comReqOpt,
|
|
170
179
|
method: "GET",
|
|
171
180
|
protocol: "http",
|
|
172
|
-
});
|
|
181
|
+
}, reqData);
|
|
173
182
|
}
|
|
174
183
|
UtilCom.httpGet = httpGet;
|
|
175
184
|
/**重复一个 https POST请求并接受数据
|
|
176
185
|
* @async
|
|
177
|
-
* @param
|
|
178
|
-
* @param
|
|
179
|
-
* @param verifyFn
|
|
180
|
-
* @param repeatOpt
|
|
181
|
-
* @param opt.count - 重试次数 默认3
|
|
182
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
183
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
186
|
+
* @param comReqOpt - 网络请求选项
|
|
187
|
+
* @param reqData - 数据对象
|
|
188
|
+
* @param verifyFn - 有效性验证函数
|
|
189
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
184
190
|
* @returns 结果 undefined 为未能成功接收
|
|
185
191
|
*/
|
|
186
|
-
function httpsRepeatPost(
|
|
187
|
-
return
|
|
192
|
+
function httpsRepeatPost(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
193
|
+
return repeatJsonComReq({
|
|
188
194
|
...comReqOpt,
|
|
189
195
|
method: "POST",
|
|
190
196
|
protocol: "https",
|
|
191
|
-
}, verifyFn, repeatOpt);
|
|
197
|
+
}, reqData, verifyFn, repeatOpt);
|
|
192
198
|
}
|
|
193
199
|
UtilCom.httpsRepeatPost = httpsRepeatPost;
|
|
194
200
|
/**重复一个 http POST请求并接受数据
|
|
195
201
|
* @async
|
|
196
|
-
* @param
|
|
197
|
-
* @param
|
|
198
|
-
* @param verifyFn
|
|
199
|
-
* @param repeatOpt
|
|
200
|
-
* @param opt.count - 重试次数 默认3
|
|
201
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
202
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
202
|
+
* @param comReqOpt - 网络请求选项
|
|
203
|
+
* @param reqData - 数据对象
|
|
204
|
+
* @param verifyFn - 有效性验证函数
|
|
205
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
203
206
|
* @returns 结果 undefined 为未能成功接收
|
|
204
207
|
*/
|
|
205
|
-
function httpRepeatPost(
|
|
206
|
-
return
|
|
208
|
+
function httpRepeatPost(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
209
|
+
return repeatJsonComReq({
|
|
207
210
|
...comReqOpt,
|
|
208
211
|
method: "POST",
|
|
209
212
|
protocol: "http",
|
|
210
|
-
}, verifyFn, repeatOpt);
|
|
213
|
+
}, reqData, verifyFn, repeatOpt);
|
|
211
214
|
}
|
|
212
215
|
UtilCom.httpRepeatPost = httpRepeatPost;
|
|
213
216
|
/**重复一个 https GET 请求并接受数据
|
|
214
217
|
* @async
|
|
215
|
-
* @param
|
|
216
|
-
* @param
|
|
217
|
-
* @param verifyFn
|
|
218
|
-
* @param repeatOpt
|
|
219
|
-
* @param opt.count - 重试次数 默认3
|
|
220
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
221
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
218
|
+
* @param comReqOpt - 网络请求选项
|
|
219
|
+
* @param reqData - 数据对象
|
|
220
|
+
* @param verifyFn - 有效性验证函数
|
|
221
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
222
222
|
* @returns 结果 undefined 为未能成功接收
|
|
223
223
|
*/
|
|
224
|
-
function httpsRepeatGet(
|
|
225
|
-
return
|
|
224
|
+
function httpsRepeatGet(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
225
|
+
return repeatJsonComReq({
|
|
226
226
|
...comReqOpt,
|
|
227
227
|
method: "GET",
|
|
228
228
|
protocol: "https",
|
|
229
|
-
}, verifyFn, repeatOpt);
|
|
229
|
+
}, reqData, verifyFn, repeatOpt);
|
|
230
230
|
}
|
|
231
231
|
UtilCom.httpsRepeatGet = httpsRepeatGet;
|
|
232
232
|
/**重复一个 http GET 请求并接受数据
|
|
233
233
|
* @async
|
|
234
|
-
* @param
|
|
235
|
-
* @param
|
|
236
|
-
* @param verifyFn
|
|
237
|
-
* @param repeatOpt
|
|
238
|
-
* @param opt.count - 重试次数 默认3
|
|
239
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
240
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
234
|
+
* @param comReqOpt - 网络请求选项
|
|
235
|
+
* @param reqData - 数据对象
|
|
236
|
+
* @param verifyFn - 有效性验证函数
|
|
237
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
241
238
|
* @returns 结果 undefined 为未能成功接收
|
|
242
239
|
*/
|
|
243
|
-
function httpRepeatGet(
|
|
244
|
-
return
|
|
240
|
+
function httpRepeatGet(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
241
|
+
return repeatJsonComReq({
|
|
245
242
|
...comReqOpt,
|
|
246
243
|
method: "GET",
|
|
247
244
|
protocol: "http",
|
|
248
|
-
}, verifyFn, repeatOpt);
|
|
245
|
+
}, reqData, verifyFn, repeatOpt);
|
|
249
246
|
}
|
|
250
247
|
UtilCom.httpRepeatGet = httpRepeatGet;
|
|
251
248
|
})(UtilCom || (exports.UtilCom = UtilCom = {}));
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -24,10 +24,10 @@ export type RepeatPromiseOpt = Partial<{
|
|
|
24
24
|
/**重试次数 默认3*/
|
|
25
25
|
count?: number;
|
|
26
26
|
/**尝试间隔时间 超过此事件会重新创建新的Promise
|
|
27
|
-
* 同时等待新的与旧的Promise
|
|
27
|
+
* 同时等待新的与旧的Promise 毫秒 默认180_000
|
|
28
28
|
*/
|
|
29
29
|
tryInterval?: number;
|
|
30
|
-
/**尝试延迟
|
|
30
|
+
/**尝试延迟 重新尝试时会先等待此毫秒数 毫秒 默认0*/
|
|
31
31
|
tryDelay?: number;
|
|
32
32
|
}>;
|
|
33
33
|
type SuccessOut<T> = Outcome<Success, T>;
|
|
@@ -110,10 +110,7 @@ export declare class UtilFunc {
|
|
|
110
110
|
* @async
|
|
111
111
|
* @param procFn - 发起函数
|
|
112
112
|
* @param verifyFn - 验证函数
|
|
113
|
-
* @param opt -
|
|
114
|
-
* @param opt.count - 重试次数 默认3
|
|
115
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
116
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 0
|
|
113
|
+
* @param opt - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
|
|
117
114
|
* @returns 结果 undefined 为全部失败/超时
|
|
118
115
|
*/
|
|
119
116
|
static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<T> | undefined>;
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -169,21 +169,15 @@ class UtilFunc {
|
|
|
169
169
|
* @async
|
|
170
170
|
* @param procFn - 发起函数
|
|
171
171
|
* @param verifyFn - 验证函数
|
|
172
|
-
* @param opt -
|
|
173
|
-
* @param opt.count - 重试次数 默认3
|
|
174
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
175
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 0
|
|
172
|
+
* @param opt - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
|
|
176
173
|
* @returns 结果 undefined 为全部失败/超时
|
|
177
174
|
*/
|
|
178
175
|
static async repeatPromise(procFn, verifyFn, opt = {}) {
|
|
179
176
|
opt.count = opt.count ?? 3;
|
|
180
|
-
opt.tryInterval = opt.tryInterval ??
|
|
181
|
-
|
|
177
|
+
opt.tryInterval = opt.tryInterval ?? 180_000;
|
|
178
|
+
const { count, tryInterval } = opt;
|
|
182
179
|
/**是否含有超时时间 */
|
|
183
|
-
const hasRepeatTime = (tryInterval >=
|
|
184
|
-
//转换为毫秒
|
|
185
|
-
if (hasRepeatTime)
|
|
186
|
-
tryInterval *= 1000;
|
|
180
|
+
const hasRepeatTime = (tryInterval >= 5000);
|
|
187
181
|
//验证处理函数
|
|
188
182
|
if (verifyFn === undefined)
|
|
189
183
|
verifyFn = () => UtilSymbol_1.Success;
|
|
@@ -194,7 +188,7 @@ class UtilFunc {
|
|
|
194
188
|
//根据最大重试次数限制进行循环
|
|
195
189
|
for (let i = 0; i < count;) {
|
|
196
190
|
if (i > 0 && opt.tryDelay)
|
|
197
|
-
await this.sleep(opt.tryDelay
|
|
191
|
+
await this.sleep(opt.tryDelay);
|
|
198
192
|
UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 repeatPromise`);
|
|
199
193
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
200
194
|
if (plist.length < i + 1) {
|
package/package.json
CHANGED
package/src/UtilCom.ts
CHANGED
|
@@ -5,11 +5,23 @@ import { SLogger } from "@src/UtilLogger";
|
|
|
5
5
|
import { RepeatPromiseOpt, RepeatPromiseResult, UtilFunc } from "@src/UtilFunctions";
|
|
6
6
|
import qs from "querystring";
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
/**网络请求返回值 */
|
|
9
|
+
export type ComResp<T> = {
|
|
10
|
+
/**响应头 */
|
|
11
|
+
headers: http.IncomingHttpHeaders;
|
|
12
|
+
/**响应状态码 */
|
|
13
|
+
statusCode?: number;
|
|
14
|
+
/**响应数据 */
|
|
15
|
+
data: T;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**网络请求选项 */
|
|
19
|
+
export type ComReqOpt = {
|
|
11
20
|
/**请求协议 */
|
|
12
21
|
protocol: 'http'|'https';
|
|
22
|
+
/**超时时间/毫秒 最小为10_000 默认无限 */
|
|
23
|
+
timeLimit?:number;
|
|
24
|
+
}&{
|
|
13
25
|
/**请求域名 */
|
|
14
26
|
hostname: string;
|
|
15
27
|
/**请求路径 */
|
|
@@ -24,269 +36,302 @@ export type ComRequestOption = {
|
|
|
24
36
|
'Content-Type'?: 'application/json'|AnyString;
|
|
25
37
|
/**内容长度 一般无需填写 应为buffer长度而非字符串长度 */
|
|
26
38
|
'Content-Length'?: number;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
39
|
+
};
|
|
40
|
+
}&http.RequestOptions;
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
/**get请求所允许的数据 */
|
|
43
|
+
export type GetReqData = NodeJS.Dict<
|
|
44
|
+
| string
|
|
45
|
+
| number
|
|
46
|
+
| boolean
|
|
47
|
+
| readonly string[]
|
|
48
|
+
| readonly number[]
|
|
49
|
+
| readonly boolean[]
|
|
50
|
+
| null
|
|
51
|
+
>;
|
|
38
52
|
|
|
39
53
|
/**网络工具 */
|
|
40
54
|
export namespace UtilCom{
|
|
41
55
|
|
|
42
|
-
/**通用post处理
|
|
43
|
-
* @param reqData - 数据对象
|
|
44
|
-
* @param comReqOpt - 请求参数
|
|
45
|
-
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
46
|
-
* @returns 结果 undefined 为未能成功接收
|
|
47
|
-
*/
|
|
48
|
-
function comReq(reqData:JObject|undefined,comReqOpt:ComRequestOption):Promise<HttpResp|undefined>{
|
|
49
|
-
let {protocol,timeLimit,...baseOpt} = comReqOpt;
|
|
50
|
-
|
|
51
|
-
//转换为毫秒
|
|
52
|
-
const hasTimeLimit = (timeLimit ? timeLimit>=10 : false );
|
|
53
|
-
if(hasTimeLimit && timeLimit!=undefined) timeLimit*=1000;
|
|
54
|
-
const fixlimit = timeLimit as number;
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
/**网络请求
|
|
58
|
+
* @param comReqOpt - 网络请求选项
|
|
59
|
+
* @param procReq - 请求处理函数
|
|
60
|
+
* @param reduceData - 数据处理函数
|
|
61
|
+
* @param initData - 初始数据
|
|
62
|
+
*/
|
|
63
|
+
export async function comReq<T>(
|
|
64
|
+
comReqOpt:ComReqOpt,
|
|
65
|
+
procReq:((req:http.ClientRequest)=>void|Promise<void>),
|
|
66
|
+
reduceData:(acc:T,data:string)=>T,
|
|
67
|
+
initData:T,
|
|
68
|
+
){
|
|
69
|
+
const {protocol,timeLimit,...baseReqOpt} = comReqOpt;
|
|
58
70
|
|
|
59
|
-
const
|
|
60
|
-
const funcName = `${protocol}${baseOpt.method}`;
|
|
71
|
+
const hasTimeLimit = (timeLimit ? timeLimit>=10_000 : false );
|
|
61
72
|
|
|
62
|
-
|
|
73
|
+
const flagName = `${comReq.name}.${protocol}${baseReqOpt.method}`;
|
|
63
74
|
|
|
64
|
-
return new Promise((resolve, rejecte)=>{
|
|
75
|
+
return new Promise<ComResp<T>|undefined>(async (resolve, rejecte)=>{
|
|
65
76
|
const resFunc = (res:http.IncomingMessage)=>{
|
|
66
77
|
try{
|
|
67
78
|
//请求超时
|
|
68
79
|
if(hasTimeLimit){
|
|
69
|
-
res.setTimeout(
|
|
70
|
-
|
|
71
|
-
SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
|
|
80
|
+
res.setTimeout(timeLimit!, ()=>{
|
|
81
|
+
SLogger.warn(`${flagName} 接收反馈超时(timeLimit): ${timeLimit} ms`);
|
|
72
82
|
resolve(undefined);
|
|
73
83
|
return;
|
|
74
84
|
});
|
|
75
85
|
}
|
|
76
86
|
|
|
77
|
-
let
|
|
87
|
+
let mergedata:T = initData;
|
|
78
88
|
res.setEncoding('utf8');
|
|
79
|
-
res.on('data',
|
|
89
|
+
res.on('data',chunk => mergedata=reduceData(mergedata,chunk));
|
|
80
90
|
|
|
81
91
|
res.on('error',(e)=>{
|
|
82
|
-
SLogger.warn(`${
|
|
92
|
+
SLogger.warn(`${flagName} 接收反馈错误:${e}`);
|
|
83
93
|
resolve(undefined);
|
|
84
94
|
return;
|
|
85
95
|
});
|
|
86
96
|
|
|
87
97
|
res.on('end',()=>{
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
try{
|
|
94
|
-
const obj = JSON.parse(resdata) as JObject;
|
|
95
|
-
SLogger.http(`${funcName} 接受信息:`,UtilFunc.stringifyJToken(obj,{compress:true,space:2}));
|
|
96
|
-
resolve({
|
|
97
|
-
headers: res.headers,
|
|
98
|
-
statusCode: res.statusCode,
|
|
99
|
-
data: obj,
|
|
100
|
-
});
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
catch(e){
|
|
104
|
-
SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
|
|
105
|
-
resolve(undefined);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
98
|
+
resolve({
|
|
99
|
+
headers: res.headers,
|
|
100
|
+
statusCode: res.statusCode,
|
|
101
|
+
data: mergedata,
|
|
102
|
+
});
|
|
108
103
|
});
|
|
109
104
|
}catch(err){
|
|
110
|
-
SLogger.warn(`${
|
|
105
|
+
SLogger.warn(`${flagName} 未知错误:${err}`);
|
|
111
106
|
resolve(undefined);
|
|
112
107
|
return;
|
|
113
108
|
}
|
|
114
109
|
};
|
|
115
110
|
//路由 http/https
|
|
116
|
-
const req:http.ClientRequest=
|
|
117
|
-
? https.request(
|
|
118
|
-
: http.request(
|
|
111
|
+
const req:http.ClientRequest= protocol=="https"
|
|
112
|
+
? https.request(baseReqOpt as http.RequestOptions, resFunc)
|
|
113
|
+
: http.request(baseReqOpt as http.RequestOptions, resFunc);
|
|
119
114
|
|
|
120
115
|
//请求超时
|
|
121
116
|
if(hasTimeLimit){
|
|
122
|
-
req.setTimeout(
|
|
123
|
-
SLogger.warn(`${
|
|
117
|
+
req.setTimeout(timeLimit!, ()=>{
|
|
118
|
+
SLogger.warn(`${flagName} 发送请求超时(timeLimit): ${timeLimit} ms`);
|
|
119
|
+
req.destroy();
|
|
120
|
+
});
|
|
121
|
+
req.on('timeout', ()=>{
|
|
122
|
+
SLogger.warn(`${flagName} 发送请求超时(timeout): ${timeLimit} ms`);
|
|
124
123
|
req.destroy();
|
|
125
124
|
});
|
|
126
125
|
}
|
|
127
126
|
|
|
128
127
|
req.on('error', (e)=>{
|
|
129
|
-
SLogger.warn(`${
|
|
128
|
+
SLogger.warn(`${flagName} 发送请求错误:${e}`);
|
|
130
129
|
resolve(undefined);
|
|
131
130
|
});
|
|
132
|
-
|
|
131
|
+
|
|
132
|
+
await procReq(req);
|
|
133
|
+
|
|
133
134
|
req.end();
|
|
134
135
|
});
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
|
|
138
|
-
* @
|
|
138
|
+
/**发送json的网络请求
|
|
139
|
+
* @param comReqOpt - 网络请求选项
|
|
139
140
|
* @param reqData - 数据对象
|
|
140
|
-
* @param comReqOpt - 请求参数
|
|
141
|
-
* @param verifyFn - 判断有效性函数
|
|
142
|
-
* @param repeatOpt - 可选参数
|
|
143
|
-
* @param opt.count - 重试次数 默认3
|
|
144
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
145
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
146
141
|
* @returns 结果 undefined 为未能成功接收
|
|
147
142
|
*/
|
|
148
|
-
async function
|
|
149
|
-
|
|
150
|
-
|
|
143
|
+
async function jsonReq<T extends ComReqOpt>(
|
|
144
|
+
comReqOpt:T,
|
|
145
|
+
reqData?:T['method'] extends "POST" ? JObject :GetReqData
|
|
146
|
+
){
|
|
147
|
+
const {method} = comReqOpt;
|
|
148
|
+
const isPost = (method=="POST");
|
|
149
|
+
|
|
150
|
+
if(!isPost && reqData!=undefined) comReqOpt.path+=qs.stringify(reqData as GetReqData);
|
|
151
151
|
|
|
152
|
-
const
|
|
152
|
+
const procReq = (req:http.ClientRequest)=>{
|
|
153
|
+
if(isPost) req.write(JSON.stringify(reqData));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const reduceData = (acc:string,data:string)=>acc+data;
|
|
157
|
+
const result = await comReq(comReqOpt,procReq,reduceData,"");
|
|
158
|
+
if(result==undefined) return undefined;
|
|
159
|
+
const {data,...rest} = result;
|
|
160
|
+
|
|
161
|
+
if(data==""){
|
|
162
|
+
SLogger.warn(`${jsonReq.name} 接收反馈错误: 原始字符串为空`);
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
try{
|
|
167
|
+
const obj = JSON.parse(data) as JObject;
|
|
168
|
+
SLogger.http(`${jsonReq.name} 接受信息:`,UtilFunc.stringifyJToken(obj,{compress:true,space:2}));
|
|
169
|
+
return{...rest,data:obj};
|
|
170
|
+
}
|
|
171
|
+
catch(e){
|
|
172
|
+
SLogger.warn(`${jsonReq.name} 接收反馈错误:${e}\n原始字符串:${data}`);
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**重复发送json的网络请求
|
|
178
|
+
* @async
|
|
179
|
+
* @param comReqOpt - 网络请求选项
|
|
180
|
+
* @param reqData - 数据对象
|
|
181
|
+
* @param verifyFn - 有效性验证函数
|
|
182
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
183
|
+
* @returns 结果 undefined 为未能成功接收
|
|
184
|
+
*/
|
|
185
|
+
async function repeatJsonComReq<T extends ComReqOpt>(
|
|
186
|
+
comReqOpt:T,
|
|
187
|
+
reqData?:T['method'] extends "POST" ? JObject :GetReqData,
|
|
188
|
+
verifyFn?:ReqVerifyFn<ComResp<JObject>|undefined>,
|
|
189
|
+
repeatOpt:RepeatPromiseOpt={},
|
|
190
|
+
){
|
|
191
|
+
repeatOpt.tryDelay = repeatOpt.tryDelay??1000;
|
|
192
|
+
const procFn = ()=>jsonReq(comReqOpt,reqData);
|
|
153
193
|
return UtilFunc.repeatPromise(procFn,verifyFn,repeatOpt);
|
|
154
194
|
}
|
|
155
195
|
|
|
156
196
|
/**发送一个 https POST 请求并接受数据
|
|
157
197
|
* @async
|
|
158
|
-
* @param reqData - 数据对象
|
|
159
198
|
* @param comReqOpt - 请求参数
|
|
199
|
+
* @param reqData - 数据对象
|
|
160
200
|
* @returns 结果 undefined 为未能成功接收
|
|
161
201
|
*/
|
|
162
|
-
export function httpsPost(
|
|
163
|
-
return
|
|
202
|
+
export function httpsPost(comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,reqData:JObject|undefined){
|
|
203
|
+
return jsonReq({
|
|
164
204
|
...comReqOpt,
|
|
165
205
|
method:"POST",
|
|
166
206
|
protocol:"https",
|
|
167
|
-
});
|
|
207
|
+
},reqData);
|
|
168
208
|
}
|
|
169
209
|
|
|
170
210
|
/**发送一个 http POST 请求并接受数据
|
|
171
211
|
* @async
|
|
172
|
-
* @param reqData - 数据对象
|
|
173
212
|
* @param comReqOpt - 请求参数
|
|
213
|
+
* @param reqData - 数据对象
|
|
174
214
|
* @returns 结果 undefined 为未能成功接收
|
|
175
215
|
*/
|
|
176
|
-
export function httpPost(
|
|
177
|
-
return
|
|
216
|
+
export function httpPost(comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,reqData:JObject|undefined){
|
|
217
|
+
return jsonReq({
|
|
178
218
|
...comReqOpt,
|
|
179
219
|
method:"POST",
|
|
180
220
|
protocol:"http",
|
|
181
|
-
});
|
|
221
|
+
},reqData);
|
|
182
222
|
}
|
|
223
|
+
|
|
183
224
|
/**发送一个 https GET 请求并接受数据
|
|
184
225
|
* @async
|
|
185
|
-
* @param reqData - 数据对象
|
|
186
226
|
* @param comReqOpt - 请求参数
|
|
227
|
+
* @param reqData - 数据对象
|
|
187
228
|
* @returns 结果 undefined 为未能成功接收
|
|
188
229
|
*/
|
|
189
|
-
export function httpsGet(
|
|
190
|
-
return
|
|
230
|
+
export function httpsGet(comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,reqData:Record<string,string|number|boolean>){
|
|
231
|
+
return jsonReq({
|
|
191
232
|
...comReqOpt,
|
|
192
233
|
method:"GET",
|
|
193
234
|
protocol:"https",
|
|
194
|
-
});
|
|
235
|
+
},reqData);
|
|
195
236
|
}
|
|
196
237
|
|
|
197
238
|
/**发送一个 http GET 请求并接受数据
|
|
198
239
|
* @async
|
|
199
|
-
* @param reqData - 数据对象
|
|
200
240
|
* @param comReqOpt - 请求参数
|
|
241
|
+
* @param reqData - 数据对象
|
|
201
242
|
* @returns 结果 undefined 为未能成功接收
|
|
202
243
|
*/
|
|
203
|
-
export function httpGet(
|
|
204
|
-
return
|
|
244
|
+
export function httpGet(comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,reqData:Record<string,string|number|boolean>){
|
|
245
|
+
return jsonReq({
|
|
205
246
|
...comReqOpt,
|
|
206
247
|
method:"GET",
|
|
207
248
|
protocol:"http",
|
|
208
|
-
});
|
|
249
|
+
},reqData);
|
|
209
250
|
}
|
|
210
251
|
|
|
211
252
|
|
|
212
253
|
/**重复一个 https POST请求并接受数据
|
|
213
254
|
* @async
|
|
214
|
-
* @param
|
|
215
|
-
* @param
|
|
216
|
-
* @param verifyFn
|
|
217
|
-
* @param repeatOpt
|
|
218
|
-
* @param opt.count - 重试次数 默认3
|
|
219
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
220
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
255
|
+
* @param comReqOpt - 网络请求选项
|
|
256
|
+
* @param reqData - 数据对象
|
|
257
|
+
* @param verifyFn - 有效性验证函数
|
|
258
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
221
259
|
* @returns 结果 undefined 为未能成功接收
|
|
222
260
|
*/
|
|
223
|
-
export function httpsRepeatPost(
|
|
224
|
-
|
|
225
|
-
|
|
261
|
+
export function httpsRepeatPost(
|
|
262
|
+
comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,
|
|
263
|
+
reqData?:JObject,
|
|
264
|
+
verifyFn?:ReqVerifyFn<JObject|undefined>,
|
|
265
|
+
repeatOpt?:RepeatPromiseOpt
|
|
266
|
+
){
|
|
267
|
+
return repeatJsonComReq({
|
|
226
268
|
...comReqOpt,
|
|
227
269
|
method:"POST",
|
|
228
270
|
protocol:"https",
|
|
229
|
-
},verifyFn,repeatOpt);
|
|
271
|
+
},reqData,verifyFn,repeatOpt);
|
|
230
272
|
}
|
|
231
273
|
|
|
232
274
|
/**重复一个 http POST请求并接受数据
|
|
233
275
|
* @async
|
|
234
|
-
* @param
|
|
235
|
-
* @param
|
|
236
|
-
* @param verifyFn
|
|
237
|
-
* @param repeatOpt
|
|
238
|
-
* @param opt.count - 重试次数 默认3
|
|
239
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
240
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
276
|
+
* @param comReqOpt - 网络请求选项
|
|
277
|
+
* @param reqData - 数据对象
|
|
278
|
+
* @param verifyFn - 有效性验证函数
|
|
279
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
241
280
|
* @returns 结果 undefined 为未能成功接收
|
|
242
281
|
*/
|
|
243
|
-
export function httpRepeatPost(
|
|
244
|
-
|
|
245
|
-
|
|
282
|
+
export function httpRepeatPost(
|
|
283
|
+
comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,
|
|
284
|
+
reqData?:JObject,
|
|
285
|
+
verifyFn?:ReqVerifyFn<JObject|undefined>,
|
|
286
|
+
repeatOpt?:RepeatPromiseOpt
|
|
287
|
+
){
|
|
288
|
+
return repeatJsonComReq({
|
|
246
289
|
...comReqOpt,
|
|
247
290
|
method:"POST",
|
|
248
291
|
protocol:"http",
|
|
249
|
-
},verifyFn,repeatOpt);
|
|
292
|
+
},reqData,verifyFn,repeatOpt);
|
|
250
293
|
}
|
|
251
294
|
|
|
252
295
|
/**重复一个 https GET 请求并接受数据
|
|
253
296
|
* @async
|
|
254
|
-
* @param
|
|
255
|
-
* @param
|
|
256
|
-
* @param verifyFn
|
|
257
|
-
* @param repeatOpt
|
|
258
|
-
* @param opt.count - 重试次数 默认3
|
|
259
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
260
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
297
|
+
* @param comReqOpt - 网络请求选项
|
|
298
|
+
* @param reqData - 数据对象
|
|
299
|
+
* @param verifyFn - 有效性验证函数
|
|
300
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
261
301
|
* @returns 结果 undefined 为未能成功接收
|
|
262
302
|
*/
|
|
263
|
-
export function httpsRepeatGet(
|
|
264
|
-
|
|
265
|
-
|
|
303
|
+
export function httpsRepeatGet(
|
|
304
|
+
comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,
|
|
305
|
+
reqData?:GetReqData,
|
|
306
|
+
verifyFn?:ReqVerifyFn<JObject|undefined>,
|
|
307
|
+
repeatOpt?:RepeatPromiseOpt
|
|
308
|
+
){
|
|
309
|
+
return repeatJsonComReq({
|
|
266
310
|
...comReqOpt,
|
|
267
311
|
method:"GET",
|
|
268
312
|
protocol:"https",
|
|
269
|
-
},verifyFn,repeatOpt);
|
|
313
|
+
},reqData,verifyFn,repeatOpt);
|
|
270
314
|
}
|
|
271
315
|
|
|
272
316
|
/**重复一个 http GET 请求并接受数据
|
|
273
317
|
* @async
|
|
274
|
-
* @param
|
|
275
|
-
* @param
|
|
276
|
-
* @param verifyFn
|
|
277
|
-
* @param repeatOpt
|
|
278
|
-
* @param opt.count - 重试次数 默认3
|
|
279
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
280
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 1
|
|
318
|
+
* @param comReqOpt - 网络请求选项
|
|
319
|
+
* @param reqData - 数据对象
|
|
320
|
+
* @param verifyFn - 有效性验证函数
|
|
321
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
281
322
|
* @returns 结果 undefined 为未能成功接收
|
|
282
323
|
*/
|
|
283
|
-
export function httpRepeatGet(
|
|
284
|
-
|
|
285
|
-
|
|
324
|
+
export function httpRepeatGet(
|
|
325
|
+
comReqOpt:Omit<ComReqOpt,'protocol'|'method'>,
|
|
326
|
+
reqData?:GetReqData,
|
|
327
|
+
verifyFn?:ReqVerifyFn<JObject|undefined>,
|
|
328
|
+
repeatOpt?:RepeatPromiseOpt
|
|
329
|
+
){
|
|
330
|
+
return repeatJsonComReq({
|
|
286
331
|
...comReqOpt,
|
|
287
332
|
method:"GET",
|
|
288
333
|
protocol:"http",
|
|
289
|
-
},verifyFn,repeatOpt);
|
|
334
|
+
},reqData,verifyFn,repeatOpt);
|
|
290
335
|
}
|
|
291
336
|
|
|
292
337
|
}
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -35,10 +35,10 @@ export type RepeatPromiseOpt = Partial<{
|
|
|
35
35
|
/**重试次数 默认3*/
|
|
36
36
|
count? :number;
|
|
37
37
|
/**尝试间隔时间 超过此事件会重新创建新的Promise
|
|
38
|
-
* 同时等待新的与旧的Promise
|
|
38
|
+
* 同时等待新的与旧的Promise 毫秒 默认180_000
|
|
39
39
|
*/
|
|
40
40
|
tryInterval? :number;
|
|
41
|
-
/**尝试延迟
|
|
41
|
+
/**尝试延迟 重新尝试时会先等待此毫秒数 毫秒 默认0*/
|
|
42
42
|
tryDelay? :number;
|
|
43
43
|
}>
|
|
44
44
|
|
|
@@ -223,22 +223,17 @@ static getNeverResolvedPromise<T>():Promise<T>{
|
|
|
223
223
|
* @async
|
|
224
224
|
* @param procFn - 发起函数
|
|
225
225
|
* @param verifyFn - 验证函数
|
|
226
|
-
* @param opt -
|
|
227
|
-
* @param opt.count - 重试次数 默认3
|
|
228
|
-
* @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
|
|
229
|
-
* @param opt.tryDelay - 重试间隔时间/秒 默认 0
|
|
226
|
+
* @param opt - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
|
|
230
227
|
* @returns 结果 undefined 为全部失败/超时
|
|
231
228
|
*/
|
|
232
229
|
@LogTimeAsync("repeatPromise ",true)
|
|
233
230
|
static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):
|
|
234
231
|
Promise<RepeatPromiseResult<T>|undefined>{
|
|
235
232
|
opt.count = opt.count??3;
|
|
236
|
-
opt.tryInterval = opt.tryInterval??
|
|
237
|
-
|
|
233
|
+
opt.tryInterval = opt.tryInterval??180_000;
|
|
234
|
+
const {count,tryInterval} = opt;
|
|
238
235
|
/**是否含有超时时间 */
|
|
239
|
-
const hasRepeatTime = (tryInterval>=
|
|
240
|
-
//转换为毫秒
|
|
241
|
-
if(hasRepeatTime) tryInterval*=1000;
|
|
236
|
+
const hasRepeatTime = (tryInterval>=5000);
|
|
242
237
|
|
|
243
238
|
//验证处理函数
|
|
244
239
|
if(verifyFn===undefined)
|
|
@@ -254,7 +249,7 @@ Promise<RepeatPromiseResult<T>|undefined>{
|
|
|
254
249
|
try{
|
|
255
250
|
//根据最大重试次数限制进行循环
|
|
256
251
|
for(let i=0;i<count;){
|
|
257
|
-
if(i>0 && opt.tryDelay) await this.sleep(opt.tryDelay
|
|
252
|
+
if(i>0 && opt.tryDelay) await this.sleep(opt.tryDelay);
|
|
258
253
|
SLogger.info(`开始第 ${i+1} 次 repeatPromise`);
|
|
259
254
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
260
255
|
if(plist.length<i+1){
|