@zwa73/utils 1.0.57 → 1.0.61
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/UtilCodecs.d.ts +16 -16
- package/dist/UtilCodecs.js +16 -16
- package/dist/UtilCom.d.ts +22 -25
- package/dist/UtilCom.js +35 -38
- package/dist/UtilDecorators.d.ts +15 -8
- package/dist/UtilDecorators.js +79 -25
- package/dist/UtilFP.d.ts +46 -0
- package/dist/UtilFP.js +52 -0
- package/dist/UtilFfmpegTools.d.ts +30 -30
- package/dist/UtilFfmpegTools.js +32 -32
- package/dist/UtilFileTools.d.ts +30 -35
- package/dist/UtilFileTools.js +17 -18
- package/dist/UtilFunctions.d.ts +37 -78
- package/dist/UtilFunctions.js +27 -62
- package/dist/UtilInterfaces.d.ts +11 -11
- package/dist/UtilInterfaces.js +2 -2
- package/dist/UtilLogger.d.ts +55 -55
- package/dist/UtilLogger.js +55 -55
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/test/composeTest.d.ts +21 -0
- package/dist/test/composeTest.js +33 -0
- package/dist/test/importtest.d.ts +1 -0
- package/dist/test/importtest.js +4 -0
- package/dist/test/test.js +5 -3
- package/package.json +2 -2
- package/src/UtilClass.ts +1051 -1051
- package/src/UtilCodecs.ts +117 -117
- package/src/UtilCom.ts +171 -174
- package/src/UtilDecorators.ts +174 -116
- package/src/UtilFP.ts +98 -0
- package/src/UtilFfmpegTools.ts +271 -271
- package/src/UtilFileTools.ts +231 -236
- package/src/UtilFunctions.ts +289 -364
- package/src/UtilInterfaces.ts +137 -137
- package/src/UtilLogger.ts +386 -386
- package/src/index.ts +10 -9
- package/src/test/composeTest.ts +37 -0
- package/src/test/importtest.ts +5 -0
- package/src/test/test.ts +8 -6
- package/src/test/test2.ts +2 -3
- package/tsconfig.json +2 -7
package/src/UtilCodecs.ts
CHANGED
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
import * as he from 'html-entities';
|
|
2
|
-
import {get_encoding,Tiktoken} from 'tiktoken';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**编码/解码器 */
|
|
7
|
-
export namespace UtilCodec{
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let encoderTurbo:Tiktoken|null = null;
|
|
11
|
-
let encoderDavinci:Tiktoken|null = null;
|
|
12
|
-
const textDecoder = new TextDecoder();
|
|
13
|
-
|
|
14
|
-
// 定义一个对象,存储常见的HTML实体和对应的字符
|
|
15
|
-
let htmlEntities:Record<string,string> = {
|
|
16
|
-
"<": "<",
|
|
17
|
-
">": ">",
|
|
18
|
-
"&": "&",
|
|
19
|
-
""": "\"",
|
|
20
|
-
"'": "'",
|
|
21
|
-
"[": "[",
|
|
22
|
-
"]": "]"
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
|
|
26
|
-
* @param
|
|
27
|
-
* @returns
|
|
28
|
-
*/
|
|
29
|
-
export function decodeHtmlEntities(str:string) {
|
|
30
|
-
//for(let code in htmlEntities){
|
|
31
|
-
// let cha = htmlEntities[code]
|
|
32
|
-
// str = str.replaceAll(code, cha);
|
|
33
|
-
//}
|
|
34
|
-
//return str
|
|
35
|
-
return he.decode(str);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
|
|
39
|
-
* @param
|
|
40
|
-
* @returns
|
|
41
|
-
*/
|
|
42
|
-
export function encodeHtmlEntities(str:string) {
|
|
43
|
-
//for(let code in htmlEntities){
|
|
44
|
-
// let cha = htmlEntities[code]
|
|
45
|
-
// str = str.replaceAll(cha, code);
|
|
46
|
-
//}
|
|
47
|
-
//return str
|
|
48
|
-
return he.encode(str);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
//token长度计算器
|
|
53
|
-
//cl100k_base ChatGPT models, text-embedding-ada-002
|
|
54
|
-
//p50k_base Code models, text-davinci-002, text-davinci-003
|
|
55
|
-
//r50k_base (or gpt2) GPT-3 models like davinci
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
//避免在nextjs调用时出错
|
|
59
|
-
function initTikTokenEncoder (){
|
|
60
|
-
if(encoderTurbo!=null && encoderDavinci!=null)
|
|
61
|
-
return;
|
|
62
|
-
|
|
63
|
-
encoderTurbo = get_encoding("cl100k_base");
|
|
64
|
-
encoderDavinci = get_encoding("p50k_base");
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**token长度计算器 Turbo模型
|
|
68
|
-
* @param
|
|
69
|
-
* @returns
|
|
70
|
-
*/
|
|
71
|
-
export function tokenNumTurbo(str:string):number{
|
|
72
|
-
initTikTokenEncoder();
|
|
73
|
-
//return encoder.encode(str).length
|
|
74
|
-
return encoderTurbo?.encode(str).length as any as number;
|
|
75
|
-
}
|
|
76
|
-
/**token长度计算器 Davinci模型
|
|
77
|
-
* @param
|
|
78
|
-
* @returns
|
|
79
|
-
*/
|
|
80
|
-
export function tokenNumDavinci(str:string):number{
|
|
81
|
-
initTikTokenEncoder();
|
|
82
|
-
return encoderDavinci?.encode(str).length as any as number;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**token编码 Turbo模型
|
|
86
|
-
* @param
|
|
87
|
-
* @returns
|
|
88
|
-
*/
|
|
89
|
-
export function encodeTokenTurbo(str:string):Uint32Array{
|
|
90
|
-
initTikTokenEncoder();
|
|
91
|
-
return encoderTurbo?.encode(str) as any as Uint32Array
|
|
92
|
-
}
|
|
93
|
-
/**token编码 Davinci模型
|
|
94
|
-
* @param
|
|
95
|
-
* @returns
|
|
96
|
-
*/
|
|
97
|
-
export function encodeTokenDavinci(str:string):Uint32Array{
|
|
98
|
-
initTikTokenEncoder();
|
|
99
|
-
return encoderDavinci?.encode(str) as any as Uint32Array;
|
|
100
|
-
}
|
|
101
|
-
/**token解码 Turbo模型
|
|
102
|
-
* @param
|
|
103
|
-
* @returns
|
|
104
|
-
*/
|
|
105
|
-
export function decodeTokenTurbo(arr:Uint32Array):string{
|
|
106
|
-
initTikTokenEncoder();
|
|
107
|
-
return textDecoder.decode(encoderTurbo?.decode(arr));
|
|
108
|
-
}
|
|
109
|
-
/**token解码 Davinci模型
|
|
110
|
-
* @param
|
|
111
|
-
* @returns
|
|
112
|
-
*/
|
|
113
|
-
export function decodeTokenDavinci(arr:Uint32Array):string{
|
|
114
|
-
initTikTokenEncoder();
|
|
115
|
-
return textDecoder.decode(encoderDavinci?.decode(arr));
|
|
116
|
-
}
|
|
117
|
-
|
|
1
|
+
import * as he from 'html-entities';
|
|
2
|
+
import {get_encoding,Tiktoken} from 'tiktoken';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**编码/解码器 */
|
|
7
|
+
export namespace UtilCodec{
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
let encoderTurbo:Tiktoken|null = null;
|
|
11
|
+
let encoderDavinci:Tiktoken|null = null;
|
|
12
|
+
const textDecoder = new TextDecoder();
|
|
13
|
+
|
|
14
|
+
// 定义一个对象,存储常见的HTML实体和对应的字符
|
|
15
|
+
let htmlEntities:Record<string,string> = {
|
|
16
|
+
"<": "<",
|
|
17
|
+
">": ">",
|
|
18
|
+
"&": "&",
|
|
19
|
+
""": "\"",
|
|
20
|
+
"'": "'",
|
|
21
|
+
"[": "[",
|
|
22
|
+
"]": "]"
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
|
|
26
|
+
* @param str - 要转换的字符串
|
|
27
|
+
* @returns 转换后的字符串
|
|
28
|
+
*/
|
|
29
|
+
export function decodeHtmlEntities(str:string) {
|
|
30
|
+
//for(let code in htmlEntities){
|
|
31
|
+
// let cha = htmlEntities[code]
|
|
32
|
+
// str = str.replaceAll(code, cha);
|
|
33
|
+
//}
|
|
34
|
+
//return str
|
|
35
|
+
return he.decode(str);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
|
|
39
|
+
* @param str - 要转换的字符串
|
|
40
|
+
* @returns 转换后的字符串
|
|
41
|
+
*/
|
|
42
|
+
export function encodeHtmlEntities(str:string) {
|
|
43
|
+
//for(let code in htmlEntities){
|
|
44
|
+
// let cha = htmlEntities[code]
|
|
45
|
+
// str = str.replaceAll(cha, code);
|
|
46
|
+
//}
|
|
47
|
+
//return str
|
|
48
|
+
return he.encode(str);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
//token长度计算器
|
|
53
|
+
//cl100k_base ChatGPT models, text-embedding-ada-002
|
|
54
|
+
//p50k_base Code models, text-davinci-002, text-davinci-003
|
|
55
|
+
//r50k_base (or gpt2) GPT-3 models like davinci
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
//避免在nextjs调用时出错
|
|
59
|
+
function initTikTokenEncoder (){
|
|
60
|
+
if(encoderTurbo!=null && encoderDavinci!=null)
|
|
61
|
+
return;
|
|
62
|
+
|
|
63
|
+
encoderTurbo = get_encoding("cl100k_base");
|
|
64
|
+
encoderDavinci = get_encoding("p50k_base");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**token长度计算器 Turbo模型
|
|
68
|
+
* @param str = 所要计算的消息
|
|
69
|
+
* @returns 整数长度结果
|
|
70
|
+
*/
|
|
71
|
+
export function tokenNumTurbo(str:string):number{
|
|
72
|
+
initTikTokenEncoder();
|
|
73
|
+
//return encoder.encode(str).length
|
|
74
|
+
return encoderTurbo?.encode(str).length as any as number;
|
|
75
|
+
}
|
|
76
|
+
/**token长度计算器 Davinci模型
|
|
77
|
+
* @param str = 所要计算的消息
|
|
78
|
+
* @returns 整数长度结果
|
|
79
|
+
*/
|
|
80
|
+
export function tokenNumDavinci(str:string):number{
|
|
81
|
+
initTikTokenEncoder();
|
|
82
|
+
return encoderDavinci?.encode(str).length as any as number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**token编码 Turbo模型
|
|
86
|
+
* @param str = 所要计算的消息
|
|
87
|
+
* @returns Token数组
|
|
88
|
+
*/
|
|
89
|
+
export function encodeTokenTurbo(str:string):Uint32Array{
|
|
90
|
+
initTikTokenEncoder();
|
|
91
|
+
return encoderTurbo?.encode(str) as any as Uint32Array
|
|
92
|
+
}
|
|
93
|
+
/**token编码 Davinci模型
|
|
94
|
+
* @param str = 所要计算的消息
|
|
95
|
+
* @returns Token数组
|
|
96
|
+
*/
|
|
97
|
+
export function encodeTokenDavinci(str:string):Uint32Array{
|
|
98
|
+
initTikTokenEncoder();
|
|
99
|
+
return encoderDavinci?.encode(str) as any as Uint32Array;
|
|
100
|
+
}
|
|
101
|
+
/**token解码 Turbo模型
|
|
102
|
+
* @param arr = Token数组
|
|
103
|
+
* @returns 消息字符串
|
|
104
|
+
*/
|
|
105
|
+
export function decodeTokenTurbo(arr:Uint32Array):string{
|
|
106
|
+
initTikTokenEncoder();
|
|
107
|
+
return textDecoder.decode(encoderTurbo?.decode(arr));
|
|
108
|
+
}
|
|
109
|
+
/**token解码 Davinci模型
|
|
110
|
+
* @param arr = Token数组
|
|
111
|
+
* @returns 消息字符串
|
|
112
|
+
*/
|
|
113
|
+
export function decodeTokenDavinci(arr:Uint32Array):string{
|
|
114
|
+
initTikTokenEncoder();
|
|
115
|
+
return textDecoder.decode(encoderDavinci?.decode(arr));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
118
|
}
|
package/src/UtilCom.ts
CHANGED
|
@@ -1,175 +1,172 @@
|
|
|
1
|
-
import { JObject, PromiseVerifyFn, stringifyJToken } from "
|
|
2
|
-
import * as https from 'https';
|
|
3
|
-
import * as http from 'http';
|
|
4
|
-
import { SLogger } from "
|
|
5
|
-
import { UtilFunc } from "
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**网络工具 */
|
|
9
|
-
export namespace UtilCom{
|
|
10
|
-
|
|
11
|
-
/**通用post处理
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
14
|
-
* @param
|
|
15
|
-
* @param
|
|
16
|
-
* @returns
|
|
17
|
-
*/
|
|
18
|
-
function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
19
|
-
//转换为毫秒
|
|
20
|
-
const hasTimeLimit = (timeLimit>=10);
|
|
21
|
-
if(hasTimeLimit)
|
|
22
|
-
timeLimit*=1000
|
|
23
|
-
|
|
24
|
-
const jsonStr = stringifyJToken(json);
|
|
25
|
-
const funcName = `s${posttype}Psot`;
|
|
26
|
-
|
|
27
|
-
return new Promise((resolve, rejecte)=>{
|
|
28
|
-
const resFunc = (res:http.IncomingMessage)=>{
|
|
29
|
-
try{
|
|
30
|
-
//请求超时
|
|
31
|
-
if(hasTimeLimit){
|
|
32
|
-
res.setTimeout(timeLimit, ()=>{
|
|
33
|
-
//res.abort();
|
|
34
|
-
SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
|
|
35
|
-
resolve(null);
|
|
36
|
-
return;
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let resdata = "";
|
|
41
|
-
res.setEncoding('utf8');
|
|
42
|
-
res.on('data',(chunk)=>resdata+=chunk);
|
|
43
|
-
|
|
44
|
-
res.on('error',(e)=>{
|
|
45
|
-
SLogger.warn(`${funcName} 接收反馈错误:${e}`);
|
|
46
|
-
resolve(null);
|
|
47
|
-
return;
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
res.on('end',()=>{
|
|
51
|
-
if(resdata==""){
|
|
52
|
-
SLogger.warn(funcName+" 接收反馈错误: resdata 为空");
|
|
53
|
-
resolve(null);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
try{
|
|
57
|
-
let obj = JSON.parse(resdata);
|
|
58
|
-
SLogger.http(funcName+" 接受信息:",stringifyJToken(obj));
|
|
59
|
-
resolve(obj);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
catch(e){
|
|
63
|
-
SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
|
|
64
|
-
resolve(null);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}catch(err){
|
|
69
|
-
SLogger.warn(funcName+" 未知错误:"+err);
|
|
70
|
-
resolve(null);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
//路由 http/https
|
|
75
|
-
let req:http.ClientRequest=null as any as http.ClientRequest;
|
|
76
|
-
if(posttype === "https")
|
|
77
|
-
req = https.request(options, resFunc);
|
|
78
|
-
else if(posttype === "http")
|
|
79
|
-
req = http.request(options, resFunc);
|
|
80
|
-
|
|
81
|
-
//请求超时
|
|
82
|
-
if(hasTimeLimit){
|
|
83
|
-
req.setTimeout(timeLimit, ()=>{
|
|
84
|
-
SLogger.warn(`${funcName} 发送请求超时: ${timeLimit} ms`);
|
|
85
|
-
req.destroy();
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
req.on('error', (e)=>{
|
|
90
|
-
SLogger.warn(`${funcName} 发送请求错误:${e}`);
|
|
91
|
-
resolve(null);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
req.write(jsonStr);
|
|
95
|
-
req.end();
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**发送一个 https POST请求并接受数据
|
|
100
|
-
*
|
|
101
|
-
* @
|
|
102
|
-
* @param
|
|
103
|
-
* @param
|
|
104
|
-
* @
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
*
|
|
113
|
-
* @
|
|
114
|
-
* @param
|
|
115
|
-
* @
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
* @
|
|
127
|
-
* @param
|
|
128
|
-
* @param
|
|
129
|
-
* @param
|
|
130
|
-
* @param
|
|
131
|
-
* @param
|
|
132
|
-
* @
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
repeatCount
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
145
|
-
* @
|
|
146
|
-
* @param
|
|
147
|
-
* @param
|
|
148
|
-
* @param
|
|
149
|
-
* @
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
*
|
|
161
|
-
* @
|
|
162
|
-
* @param
|
|
163
|
-
* @param
|
|
164
|
-
* @param
|
|
165
|
-
* @
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return sRepeatPost("http",json,options,timeLimit,repeatCount,repeatTime,verifyFn);
|
|
173
|
-
}
|
|
174
|
-
|
|
1
|
+
import { JObject, PromiseVerifyFn, stringifyJToken } from "@src/UtilInterfaces";
|
|
2
|
+
import * as https from 'https';
|
|
3
|
+
import * as http from 'http';
|
|
4
|
+
import { SLogger } from "@src/UtilLogger";
|
|
5
|
+
import { UtilFunc } from "@src/UtilFunctions";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**网络工具 */
|
|
9
|
+
export namespace UtilCom{
|
|
10
|
+
|
|
11
|
+
/**通用post处理
|
|
12
|
+
* @param posttype - post类型
|
|
13
|
+
* @param json - 数据对象
|
|
14
|
+
* @param options - 参数对象
|
|
15
|
+
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
16
|
+
* @returns 结果 null 为未能成功接收
|
|
17
|
+
*/
|
|
18
|
+
function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
19
|
+
//转换为毫秒
|
|
20
|
+
const hasTimeLimit = (timeLimit>=10);
|
|
21
|
+
if(hasTimeLimit)
|
|
22
|
+
timeLimit*=1000
|
|
23
|
+
|
|
24
|
+
const jsonStr = stringifyJToken(json);
|
|
25
|
+
const funcName = `s${posttype}Psot`;
|
|
26
|
+
|
|
27
|
+
return new Promise((resolve, rejecte)=>{
|
|
28
|
+
const resFunc = (res:http.IncomingMessage)=>{
|
|
29
|
+
try{
|
|
30
|
+
//请求超时
|
|
31
|
+
if(hasTimeLimit){
|
|
32
|
+
res.setTimeout(timeLimit, ()=>{
|
|
33
|
+
//res.abort();
|
|
34
|
+
SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
|
|
35
|
+
resolve(null);
|
|
36
|
+
return;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let resdata = "";
|
|
41
|
+
res.setEncoding('utf8');
|
|
42
|
+
res.on('data',(chunk)=>resdata+=chunk);
|
|
43
|
+
|
|
44
|
+
res.on('error',(e)=>{
|
|
45
|
+
SLogger.warn(`${funcName} 接收反馈错误:${e}`);
|
|
46
|
+
resolve(null);
|
|
47
|
+
return;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
res.on('end',()=>{
|
|
51
|
+
if(resdata==""){
|
|
52
|
+
SLogger.warn(funcName+" 接收反馈错误: resdata 为空");
|
|
53
|
+
resolve(null);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
try{
|
|
57
|
+
let obj = JSON.parse(resdata);
|
|
58
|
+
SLogger.http(funcName+" 接受信息:",stringifyJToken(obj));
|
|
59
|
+
resolve(obj);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
catch(e){
|
|
63
|
+
SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
|
|
64
|
+
resolve(null);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}catch(err){
|
|
69
|
+
SLogger.warn(funcName+" 未知错误:"+err);
|
|
70
|
+
resolve(null);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
//路由 http/https
|
|
75
|
+
let req:http.ClientRequest=null as any as http.ClientRequest;
|
|
76
|
+
if(posttype === "https")
|
|
77
|
+
req = https.request(options, resFunc);
|
|
78
|
+
else if(posttype === "http")
|
|
79
|
+
req = http.request(options, resFunc);
|
|
80
|
+
|
|
81
|
+
//请求超时
|
|
82
|
+
if(hasTimeLimit){
|
|
83
|
+
req.setTimeout(timeLimit, ()=>{
|
|
84
|
+
SLogger.warn(`${funcName} 发送请求超时: ${timeLimit} ms`);
|
|
85
|
+
req.destroy();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
req.on('error', (e)=>{
|
|
90
|
+
SLogger.warn(`${funcName} 发送请求错误:${e}`);
|
|
91
|
+
resolve(null);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
req.write(jsonStr);
|
|
95
|
+
req.end();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**发送一个 https POST请求并接受数据
|
|
100
|
+
* @async
|
|
101
|
+
* @param json - 数据对象
|
|
102
|
+
* @param options - 参数对象
|
|
103
|
+
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
104
|
+
* @returns 结果 null 为未能成功接收
|
|
105
|
+
*/
|
|
106
|
+
export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
107
|
+
return sPost("https",json,options,timeLimit);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**发送一个 http POST请求并接受数据
|
|
111
|
+
* @async
|
|
112
|
+
* @param json - 数据对象
|
|
113
|
+
* @param options - 参数对象
|
|
114
|
+
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
115
|
+
* @returns 结果 null 为未能成功接收
|
|
116
|
+
*/
|
|
117
|
+
export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
118
|
+
return sPost("http",json,options,timeLimit);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
/**通用重复post处理
|
|
124
|
+
* @async
|
|
125
|
+
* @param posttype - post类型
|
|
126
|
+
* @param json - 数据对象
|
|
127
|
+
* @param options - 参数对象
|
|
128
|
+
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
129
|
+
* @param repeatCount - 重试次数
|
|
130
|
+
* @param repeatTime - 超时时间/秒 最小为10秒
|
|
131
|
+
* @param verifyFn - 判断有效性函数
|
|
132
|
+
* @returns 结果 null 为未能成功接收
|
|
133
|
+
*/
|
|
134
|
+
async function sRepeatPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1,
|
|
135
|
+
repeatCount:number=3,repeatTime:number=180,verifyFn?:PromiseVerifyFn<JObject|null>):Promise<JObject|null>{
|
|
136
|
+
const procFn = ()=>sPost(posttype,json,options,timeLimit);
|
|
137
|
+
return UtilFunc.repeatPromise(procFn,verifyFn,repeatCount,repeatTime);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
/**重复一个 https POST请求并接受数据
|
|
142
|
+
* @async
|
|
143
|
+
* @param json - 数据对象
|
|
144
|
+
* @param options - 参数对象
|
|
145
|
+
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
146
|
+
* @param repeatCount - 重试次数
|
|
147
|
+
* @param repeatTime - 超时时间/秒 最小为10秒
|
|
148
|
+
* @param verifyFn - 判断有效性函数
|
|
149
|
+
* @returns 结果 null 为未能成功接收
|
|
150
|
+
*/
|
|
151
|
+
export function shttpsRepeatPost(json:JObject,options:Object,timeLimit:number=-1,
|
|
152
|
+
repeatCount:number=3,repeatTime:number=180,verifyFn?:PromiseVerifyFn<JObject|null>):Promise<JObject|null>{
|
|
153
|
+
return sRepeatPost("https",json,options,timeLimit,repeatCount,repeatTime,verifyFn);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**重复一个 http POST请求并接受数据
|
|
157
|
+
* Object ()
|
|
158
|
+
* @async
|
|
159
|
+
* @param json - 数据对象
|
|
160
|
+
* @param options - 参数对象
|
|
161
|
+
* @param timeLimit - 超时时间/秒 最小为10秒
|
|
162
|
+
* @param repeatCount - 重试次数
|
|
163
|
+
* @param repeatTime - 超时时间/秒 最小为10秒
|
|
164
|
+
* @param verifyFn - 判断有效性函数
|
|
165
|
+
* @returns 结果 null 为未能成功接收
|
|
166
|
+
*/
|
|
167
|
+
export function shttpRepeatPost(json:JObject,options:Object,timeLimit:number=-1,
|
|
168
|
+
repeatCount:number=3,repeatTime:number=180,verifyFn?:PromiseVerifyFn<JObject|null>):Promise<JObject|null>{
|
|
169
|
+
return sRepeatPost("http",json,options,timeLimit,repeatCount,repeatTime,verifyFn);
|
|
170
|
+
}
|
|
171
|
+
|
|
175
172
|
}
|