@zwa73/utils 1.0.194 → 1.0.196
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/UtilClass.d.ts +7 -4
- package/dist/UtilClass.js +5 -1
- package/dist/UtilCom.d.ts +95 -121
- package/dist/UtilCom.js +192 -193
- package/dist/UtilFunctions.d.ts +3 -3
- package/dist/UtilInterfaces.d.ts +9 -7
- package/package.json +1 -1
- package/src/UtilClass.ts +15 -6
- package/src/UtilCom.ts +277 -274
- package/src/UtilCom_bak.txt +345 -0
- package/src/UtilFunctions.ts +4 -4
- package/src/UtilInterfaces.ts +9 -7
package/dist/UtilCom.js
CHANGED
|
@@ -9,37 +9,203 @@ const http_1 = __importDefault(require("http"));
|
|
|
9
9
|
const UtilLogger_1 = require("./UtilLogger");
|
|
10
10
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
11
11
|
const querystring_1 = __importDefault(require("querystring"));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const UtilSymbol_1 = require("./UtilSymbol");
|
|
13
|
+
const AcceptTypeList = ["json"];
|
|
14
|
+
const SendTypeList = ["json", "none"];
|
|
15
|
+
const defSend = {
|
|
16
|
+
proc: () => (req) => void req.end()
|
|
17
|
+
};
|
|
18
|
+
const defAccept = {
|
|
19
|
+
init: '',
|
|
20
|
+
reduce: (acc, dat) => acc + dat,
|
|
21
|
+
parse: (result) => void console.log(result)
|
|
22
|
+
};
|
|
23
|
+
/**网络请求工具 */
|
|
24
|
+
class UtilCom {
|
|
25
|
+
_data;
|
|
26
|
+
_send;
|
|
27
|
+
_accept;
|
|
28
|
+
constructor(_data, _send, _accept) {
|
|
29
|
+
this._data = _data;
|
|
30
|
+
this._send = _send;
|
|
31
|
+
this._accept = _accept;
|
|
32
|
+
}
|
|
33
|
+
//#region 快速流式创建
|
|
34
|
+
/**设为https请求 */
|
|
35
|
+
static https() {
|
|
36
|
+
return new UtilCom({ protocol: 'https:' }, defSend, defAccept);
|
|
37
|
+
}
|
|
38
|
+
/**设为http请求 */
|
|
39
|
+
static http() {
|
|
40
|
+
return new UtilCom({ protocol: 'http:' }, defSend, defAccept);
|
|
41
|
+
}
|
|
42
|
+
/**设为get方式的请求 */
|
|
43
|
+
get() {
|
|
44
|
+
return new UtilCom({ ...this._data, method: 'GET' }, this._send, this._accept);
|
|
45
|
+
}
|
|
46
|
+
/**设为Post方式的请求 */
|
|
47
|
+
post() {
|
|
48
|
+
return new UtilCom({ ...this._data, method: 'POST' }, this._send, this._accept);
|
|
49
|
+
}
|
|
50
|
+
/**补充参数 */
|
|
51
|
+
option(opt) {
|
|
52
|
+
return new UtilCom({ ...this._data, ...opt }, this._send, this._accept);
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region 接收数据类型
|
|
56
|
+
/**接收数据类型*/
|
|
57
|
+
accept(t) {
|
|
58
|
+
const map = {
|
|
59
|
+
'json': this.acceptJson(),
|
|
60
|
+
'log': this.acceptLog(),
|
|
61
|
+
};
|
|
62
|
+
return map[t];
|
|
63
|
+
}
|
|
64
|
+
acceptJson() {
|
|
65
|
+
const proc = {
|
|
66
|
+
init: '',
|
|
67
|
+
reduce: (acc, curr) => acc + curr,
|
|
68
|
+
parse: (result) => {
|
|
69
|
+
if (result == undefined)
|
|
70
|
+
return undefined;
|
|
71
|
+
const { data, ...rest } = result;
|
|
72
|
+
if (data.trim() == "") {
|
|
73
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误: 原始字符串为空`);
|
|
74
|
+
return { ...result, raw: "", data: null };
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const obj = JSON.parse(data.trim());
|
|
78
|
+
UtilLogger_1.SLogger.http(`json accept 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }));
|
|
79
|
+
return { ...rest, data: obj };
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
UtilLogger_1.SLogger.warn(`json accept 接收反馈错误:${e}\n原始字符串:${data}`);
|
|
83
|
+
return { ...result, raw: data, data: null };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return new UtilCom(this._data, this._send, proc);
|
|
88
|
+
}
|
|
89
|
+
acceptLog() {
|
|
90
|
+
const proc = {
|
|
91
|
+
init: '',
|
|
92
|
+
reduce: (acc, curr) => acc + curr,
|
|
93
|
+
parse: (result) => {
|
|
94
|
+
UtilLogger_1.SLogger.http(`log accept 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(result, { compress: true, space: 2 }));
|
|
95
|
+
if (result == undefined)
|
|
96
|
+
return '';
|
|
97
|
+
const { data, ...rest } = result;
|
|
98
|
+
return data;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
return new UtilCom(this._data, this._send, proc);
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region 发送数据类型
|
|
105
|
+
/**发送数据类型*/
|
|
106
|
+
send(t) {
|
|
107
|
+
const map = {
|
|
108
|
+
'json': this.sendJson(),
|
|
109
|
+
"none": this.sendNone(),
|
|
110
|
+
};
|
|
111
|
+
return map[t];
|
|
112
|
+
}
|
|
113
|
+
sendJson() {
|
|
114
|
+
const proc = {
|
|
115
|
+
proc: (opt, reqData) => {
|
|
116
|
+
const { method } = opt;
|
|
117
|
+
const isPost = (method == "POST");
|
|
118
|
+
if (!isPost && reqData != undefined) {
|
|
119
|
+
const queryString = querystring_1.default.stringify(reqData);
|
|
120
|
+
if (queryString) {
|
|
121
|
+
// 检查当前路径是否已经包含问号
|
|
122
|
+
const separator = opt?.path?.includes('?') ? '&' : '?';
|
|
123
|
+
opt.path += separator + queryString;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (isPost) {
|
|
127
|
+
this._data.headers ??= {
|
|
128
|
+
'Content-Type': 'application/json',
|
|
129
|
+
'Content-Length': Buffer.from(JSON.stringify(reqData)).length,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const procReq = (req) => {
|
|
133
|
+
if (isPost)
|
|
134
|
+
req.write(JSON.stringify(reqData));
|
|
135
|
+
req.end();
|
|
136
|
+
};
|
|
137
|
+
return procReq;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
return new UtilCom(this._data, proc, this._accept);
|
|
141
|
+
}
|
|
142
|
+
sendNone() {
|
|
143
|
+
const proc = {
|
|
144
|
+
proc: (opt) => {
|
|
145
|
+
const procReq = (req) => {
|
|
146
|
+
req.end();
|
|
147
|
+
};
|
|
148
|
+
return procReq;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
return new UtilCom(this._data, proc, this._accept);
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
/**发送请求
|
|
155
|
+
* @param option - 网络请求选项
|
|
156
|
+
* @param datas - 数据对象
|
|
157
|
+
*/
|
|
158
|
+
async once(option, ...datas) {
|
|
159
|
+
const fullopt = Object.assign({}, this._data, option);
|
|
160
|
+
const procReq = await this._send.proc(fullopt, ...datas);
|
|
161
|
+
const { reduce, init, parse } = this._accept;
|
|
162
|
+
const res = await UtilCom.comReq(fullopt, procReq, reduce, init);
|
|
163
|
+
return parse(res);
|
|
164
|
+
}
|
|
165
|
+
/**重复发送网络请求
|
|
166
|
+
* @param option - 网络请求选项
|
|
167
|
+
* @param verifyFn - 有效性验证函数
|
|
168
|
+
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
169
|
+
* @param datas - 数据对象
|
|
170
|
+
*/
|
|
171
|
+
async repeat(option, verifyFn, repeatOpt = {}, ...datas) {
|
|
172
|
+
repeatOpt.tryDelay = repeatOpt.tryDelay ?? 1000;
|
|
173
|
+
const procFn = () => this.once(option, ...datas);
|
|
174
|
+
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatOpt);
|
|
175
|
+
}
|
|
15
176
|
/**网络请求
|
|
16
177
|
* @param comReqOpt - 网络请求选项
|
|
17
178
|
* @param procReq - 请求处理函数 需调用req.end()
|
|
18
179
|
* @param reduceData - 数据处理函数
|
|
19
180
|
* @param initData - 初始数据
|
|
20
181
|
*/
|
|
21
|
-
async
|
|
22
|
-
const { protocol,
|
|
23
|
-
const hasTimeLimit = (
|
|
24
|
-
const flagName =
|
|
182
|
+
static async comReq(comReqOpt, procReq, reduceData, initData) {
|
|
183
|
+
const { protocol, timeout, ...baseReqOpt } = comReqOpt;
|
|
184
|
+
const hasTimeLimit = (timeout ? timeout >= 10_000 : false);
|
|
185
|
+
const flagName = `UtilCom.${protocol}${baseReqOpt.method}`;
|
|
186
|
+
const postflag = `comReq:${UtilFunctions_1.UtilFunc.genUUID()}`;
|
|
187
|
+
let dataPromise = null;
|
|
25
188
|
return new Promise(async (resolve, rejecte) => {
|
|
26
189
|
const resFunc = (res) => {
|
|
27
190
|
try {
|
|
28
191
|
//请求超时
|
|
29
192
|
if (hasTimeLimit) {
|
|
30
|
-
res.setTimeout(
|
|
31
|
-
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈超时: ${
|
|
193
|
+
res.setTimeout(timeout, () => {
|
|
194
|
+
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈超时: ${timeout} ms`);
|
|
32
195
|
resolve(undefined);
|
|
33
196
|
});
|
|
34
197
|
}
|
|
35
198
|
let mergedata = initData;
|
|
36
199
|
res.setEncoding('utf8');
|
|
37
|
-
res.on('data', chunk =>
|
|
200
|
+
res.on('data', chunk => {
|
|
201
|
+
dataPromise = UtilFunctions_1.UtilFunc.queueProc(postflag, async () => mergedata = await reduceData(mergedata, chunk));
|
|
202
|
+
});
|
|
38
203
|
res.on('error', (e) => {
|
|
39
204
|
UtilLogger_1.SLogger.warn(`${flagName} 接收反馈错误:${e}`);
|
|
40
205
|
resolve(undefined);
|
|
41
206
|
});
|
|
42
|
-
res.on('end', () => {
|
|
207
|
+
res.on('end', async () => {
|
|
208
|
+
await dataPromise;
|
|
43
209
|
resolve({
|
|
44
210
|
headers: res.headers,
|
|
45
211
|
statusCode: res.statusCode,
|
|
@@ -54,19 +220,15 @@ var UtilCom;
|
|
|
54
220
|
}
|
|
55
221
|
};
|
|
56
222
|
//路由 http/https
|
|
57
|
-
const req = protocol == "https"
|
|
223
|
+
const req = protocol == "https:"
|
|
58
224
|
? https_1.default.request(baseReqOpt, resFunc)
|
|
59
225
|
: http_1.default.request(baseReqOpt, resFunc);
|
|
60
226
|
//请求超时
|
|
61
227
|
if (hasTimeLimit) {
|
|
62
|
-
req.setTimeout(
|
|
63
|
-
UtilLogger_1.SLogger.warn(`${flagName} 发送请求超时: ${
|
|
228
|
+
req.setTimeout(timeout, () => {
|
|
229
|
+
UtilLogger_1.SLogger.warn(`${flagName} 发送请求超时: ${timeout} ms`);
|
|
64
230
|
req.destroy();
|
|
65
231
|
});
|
|
66
|
-
//req.on('timeout', ()=>{
|
|
67
|
-
// SLogger.warn(`${flagName} 发送请求超时(timeout): ${timeLimit} ms`);
|
|
68
|
-
// req.destroy();
|
|
69
|
-
//});
|
|
70
232
|
}
|
|
71
233
|
req.on('error', (e) => {
|
|
72
234
|
UtilLogger_1.SLogger.warn(`${flagName} 发送请求错误:${e}`);
|
|
@@ -75,178 +237,15 @@ var UtilCom;
|
|
|
75
237
|
await procReq(req);
|
|
76
238
|
});
|
|
77
239
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// 检查当前路径是否已经包含问号
|
|
91
|
-
const separator = comReqOpt?.path?.includes('?') ? '&' : '?';
|
|
92
|
-
comReqOpt.path += separator + queryString;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const procReq = (req) => {
|
|
96
|
-
if (isPost)
|
|
97
|
-
req.write(JSON.stringify(reqData));
|
|
98
|
-
req.end();
|
|
99
|
-
};
|
|
100
|
-
const reduceData = (acc, data) => acc + data;
|
|
101
|
-
const result = await comReq(comReqOpt, procReq, reduceData, "");
|
|
102
|
-
if (result == undefined)
|
|
103
|
-
return undefined;
|
|
104
|
-
const { data, ...rest } = result;
|
|
105
|
-
if (data.trim() == "") {
|
|
106
|
-
UtilLogger_1.SLogger.warn(`${jsonReq.name} 接收反馈错误: 原始字符串为空`);
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
try {
|
|
110
|
-
const obj = JSON.parse(data.trim());
|
|
111
|
-
UtilLogger_1.SLogger.http(`${jsonReq.name} 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }));
|
|
112
|
-
return { ...rest, data: obj };
|
|
113
|
-
}
|
|
114
|
-
catch (e) {
|
|
115
|
-
UtilLogger_1.SLogger.warn(`${jsonReq.name} 接收反馈错误:${e}\n原始字符串:${data}`);
|
|
116
|
-
return undefined;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**重复发送json的网络请求
|
|
120
|
-
* @async
|
|
121
|
-
* @param comReqOpt - 网络请求选项
|
|
122
|
-
* @param reqData - 数据对象
|
|
123
|
-
* @param verifyFn - 有效性验证函数
|
|
124
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
125
|
-
* @returns 结果 undefined 为未能成功接收
|
|
126
|
-
*/
|
|
127
|
-
async function repeatJsonComReq(comReqOpt, reqData, verifyFn, repeatOpt = {}) {
|
|
128
|
-
repeatOpt.tryDelay = repeatOpt.tryDelay ?? 1000;
|
|
129
|
-
const procFn = () => jsonReq(comReqOpt, reqData);
|
|
130
|
-
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatOpt);
|
|
131
|
-
}
|
|
132
|
-
/**发送一个 https POST 请求并接受数据
|
|
133
|
-
* @async
|
|
134
|
-
* @param comReqOpt - 请求参数
|
|
135
|
-
* @param reqData - 数据对象
|
|
136
|
-
* @returns 结果 undefined 为未能成功接收
|
|
137
|
-
*/
|
|
138
|
-
function httpsPost(comReqOpt, reqData) {
|
|
139
|
-
return jsonReq({
|
|
140
|
-
...comReqOpt,
|
|
141
|
-
method: "POST",
|
|
142
|
-
protocol: "https",
|
|
143
|
-
}, reqData);
|
|
144
|
-
}
|
|
145
|
-
UtilCom.httpsPost = httpsPost;
|
|
146
|
-
/**发送一个 http POST 请求并接受数据
|
|
147
|
-
* @async
|
|
148
|
-
* @param comReqOpt - 请求参数
|
|
149
|
-
* @param reqData - 数据对象
|
|
150
|
-
* @returns 结果 undefined 为未能成功接收
|
|
151
|
-
*/
|
|
152
|
-
function httpPost(comReqOpt, reqData) {
|
|
153
|
-
return jsonReq({
|
|
154
|
-
...comReqOpt,
|
|
155
|
-
method: "POST",
|
|
156
|
-
protocol: "http",
|
|
157
|
-
}, reqData);
|
|
158
|
-
}
|
|
159
|
-
UtilCom.httpPost = httpPost;
|
|
160
|
-
/**发送一个 https GET 请求并接受数据
|
|
161
|
-
* @async
|
|
162
|
-
* @param comReqOpt - 请求参数
|
|
163
|
-
* @param reqData - 数据对象
|
|
164
|
-
* @returns 结果 undefined 为未能成功接收
|
|
165
|
-
*/
|
|
166
|
-
function httpsGet(comReqOpt, reqData) {
|
|
167
|
-
return jsonReq({
|
|
168
|
-
...comReqOpt,
|
|
169
|
-
method: "GET",
|
|
170
|
-
protocol: "https",
|
|
171
|
-
}, reqData);
|
|
172
|
-
}
|
|
173
|
-
UtilCom.httpsGet = httpsGet;
|
|
174
|
-
/**发送一个 http GET 请求并接受数据
|
|
175
|
-
* @async
|
|
176
|
-
* @param comReqOpt - 请求参数
|
|
177
|
-
* @param reqData - 数据对象
|
|
178
|
-
* @returns 结果 undefined 为未能成功接收
|
|
179
|
-
*/
|
|
180
|
-
function httpGet(comReqOpt, reqData) {
|
|
181
|
-
return jsonReq({
|
|
182
|
-
...comReqOpt,
|
|
183
|
-
method: "GET",
|
|
184
|
-
protocol: "http",
|
|
185
|
-
}, reqData);
|
|
186
|
-
}
|
|
187
|
-
UtilCom.httpGet = httpGet;
|
|
188
|
-
/**重复一个 https POST请求并接受数据
|
|
189
|
-
* @async
|
|
190
|
-
* @param comReqOpt - 网络请求选项
|
|
191
|
-
* @param reqData - 数据对象
|
|
192
|
-
* @param verifyFn - 有效性验证函数
|
|
193
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
194
|
-
* @returns 结果 undefined 为未能成功接收
|
|
195
|
-
*/
|
|
196
|
-
function httpsRepeatPost(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
197
|
-
return repeatJsonComReq({
|
|
198
|
-
...comReqOpt,
|
|
199
|
-
method: "POST",
|
|
200
|
-
protocol: "https",
|
|
201
|
-
}, reqData, verifyFn, repeatOpt);
|
|
202
|
-
}
|
|
203
|
-
UtilCom.httpsRepeatPost = httpsRepeatPost;
|
|
204
|
-
/**重复一个 http POST请求并接受数据
|
|
205
|
-
* @async
|
|
206
|
-
* @param comReqOpt - 网络请求选项
|
|
207
|
-
* @param reqData - 数据对象
|
|
208
|
-
* @param verifyFn - 有效性验证函数
|
|
209
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
210
|
-
* @returns 结果 undefined 为未能成功接收
|
|
211
|
-
*/
|
|
212
|
-
function httpRepeatPost(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
213
|
-
return repeatJsonComReq({
|
|
214
|
-
...comReqOpt,
|
|
215
|
-
method: "POST",
|
|
216
|
-
protocol: "http",
|
|
217
|
-
}, reqData, verifyFn, repeatOpt);
|
|
218
|
-
}
|
|
219
|
-
UtilCom.httpRepeatPost = httpRepeatPost;
|
|
220
|
-
/**重复一个 https GET 请求并接受数据
|
|
221
|
-
* @async
|
|
222
|
-
* @param comReqOpt - 网络请求选项
|
|
223
|
-
* @param reqData - 数据对象
|
|
224
|
-
* @param verifyFn - 有效性验证函数
|
|
225
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
226
|
-
* @returns 结果 undefined 为未能成功接收
|
|
227
|
-
*/
|
|
228
|
-
function httpsRepeatGet(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
229
|
-
return repeatJsonComReq({
|
|
230
|
-
...comReqOpt,
|
|
231
|
-
method: "GET",
|
|
232
|
-
protocol: "https",
|
|
233
|
-
}, reqData, verifyFn, repeatOpt);
|
|
234
|
-
}
|
|
235
|
-
UtilCom.httpsRepeatGet = httpsRepeatGet;
|
|
236
|
-
/**重复一个 http GET 请求并接受数据
|
|
237
|
-
* @async
|
|
238
|
-
* @param comReqOpt - 网络请求选项
|
|
239
|
-
* @param reqData - 数据对象
|
|
240
|
-
* @param verifyFn - 有效性验证函数
|
|
241
|
-
* @param repeatOpt - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
242
|
-
* @returns 结果 undefined 为未能成功接收
|
|
243
|
-
*/
|
|
244
|
-
function httpRepeatGet(comReqOpt, reqData, verifyFn, repeatOpt) {
|
|
245
|
-
return repeatJsonComReq({
|
|
246
|
-
...comReqOpt,
|
|
247
|
-
method: "GET",
|
|
248
|
-
protocol: "http",
|
|
249
|
-
}, reqData, verifyFn, repeatOpt);
|
|
250
|
-
}
|
|
251
|
-
UtilCom.httpRepeatGet = httpRepeatGet;
|
|
252
|
-
})(UtilCom || (exports.UtilCom = UtilCom = {}));
|
|
240
|
+
}
|
|
241
|
+
exports.UtilCom = UtilCom;
|
|
242
|
+
if (false)
|
|
243
|
+
((async () => {
|
|
244
|
+
const t = await UtilCom.https().post().accept('json').send('json')
|
|
245
|
+
.repeat({
|
|
246
|
+
hostname: 'httpbin.org',
|
|
247
|
+
path: '/post',
|
|
248
|
+
timeout: 10000
|
|
249
|
+
}, () => UtilSymbol_1.Success, {}, { test: 1 });
|
|
250
|
+
console.log(t);
|
|
251
|
+
})());
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome,
|
|
1
|
+
import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome, StatusVerifyFn, ProperSubsetCheck, UnionToIntersection, AnyRecord, AllExtends, SrtSegment, MPromise } from "./UtilInterfaces";
|
|
2
2
|
import { LogLevel } from "./UtilLogger";
|
|
3
3
|
import { Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
|
|
4
4
|
declare const HashMethodList: readonly ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
|
|
@@ -113,13 +113,13 @@ export declare class UtilFunc {
|
|
|
113
113
|
* @param opt - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
|
|
114
114
|
* @returns 重复结果
|
|
115
115
|
*/
|
|
116
|
-
static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?:
|
|
116
|
+
static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: StatusVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<T>>;
|
|
117
117
|
/**创建一个限时的Promise
|
|
118
118
|
* @param func - 处理函数
|
|
119
119
|
* @param timeLimit - 毫秒限时
|
|
120
120
|
* @returns 超时则返回 处理函数委托 完成则返回结果
|
|
121
121
|
*/
|
|
122
|
-
static timelimitPromise<T>(func: () =>
|
|
122
|
+
static timelimitPromise<T>(func: () => MPromise<T>, timeLimit?: number): Promise<SuccessOut<T> | TimeoutOut<T>>;
|
|
123
123
|
/**对对象的每个属性应用映射函数,并返回一个新的对象。
|
|
124
124
|
* @template T - 对象的类型
|
|
125
125
|
* @param obj - 要处理的对象
|
package/dist/UtilInterfaces.d.ts
CHANGED
|
@@ -93,14 +93,14 @@ type ExclusiveRecursive<B, T, K extends string[], R extends unknown[] = []> = R[
|
|
|
93
93
|
export type ExclusiveRecord<B, T, K extends string[], TMP = ExclusiveRecursive<B, T, K>> = TMP[keyof TMP];
|
|
94
94
|
/**符合JObject约束的互斥表 */
|
|
95
95
|
export type ExclusiveJObject<B extends JObject, T extends JToken, K extends string[], TMP extends JArray = ExclusiveRecursive<B, T, K>> = TMP[number];
|
|
96
|
-
|
|
97
|
-
* 成功 将直接返回
|
|
98
|
-
* 终止 将直接返回
|
|
96
|
+
/**Promise完成状态 成功/失败/终止
|
|
97
|
+
* 成功 将直接返回 结果
|
|
98
|
+
* 终止 将直接返回 结果
|
|
99
99
|
* 失败 将重试
|
|
100
100
|
*/
|
|
101
|
-
export type
|
|
102
|
-
|
|
103
|
-
export type
|
|
101
|
+
export type PromiseStatus = Success | Failed | Terminated;
|
|
102
|
+
/**状态验证函数 */
|
|
103
|
+
export type StatusVerifyFn<T> = (obj: T) => Promise<PromiseStatus> | PromiseStatus;
|
|
104
104
|
/**获取Promise的结果类型 如同await那样
|
|
105
105
|
* @deprecated 请使用官方的 Awaited<T> 类型
|
|
106
106
|
*/
|
|
@@ -154,8 +154,10 @@ export type NeedInit = {
|
|
|
154
154
|
/**完成初始化的标记 */
|
|
155
155
|
inited: Promise<any>;
|
|
156
156
|
};
|
|
157
|
-
/**可选的record */
|
|
157
|
+
/**可选的record PartialRecord */
|
|
158
158
|
export type PRecord<K extends Keyable, V> = Partial<Record<K, V>>;
|
|
159
|
+
/** 可以是Promise MaybePromise*/
|
|
160
|
+
export type MPromise<T> = T | Promise<T>;
|
|
159
161
|
/**任何Record */
|
|
160
162
|
export type AnyRecord = Record<Keyable, any>;
|
|
161
163
|
/**注释元组
|
package/package.json
CHANGED
package/src/UtilClass.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { matchProc } from "./QuickExport";
|
|
2
|
-
import { Keyable, Literal } from "./UtilInterfaces";
|
|
2
|
+
import { Keyable, Literal, MPromise } from "./UtilInterfaces";
|
|
3
3
|
import Handlebars from 'handlebars';
|
|
4
4
|
import { SLogger } from "./UtilLogger";
|
|
5
5
|
|
|
@@ -38,7 +38,8 @@ export class Piper<Arg, Result = Arg> {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
//#region 并行流
|
|
42
|
+
type StreamOperation<T, U> = (item: T)=>MPromise<U>;
|
|
42
43
|
/**并行流 */
|
|
43
44
|
export class Stream<T> implements Iterable<T>{
|
|
44
45
|
/**并发数*/
|
|
@@ -52,13 +53,16 @@ export class Stream<T> implements Iterable<T>{
|
|
|
52
53
|
else this._list = new Array(...base);
|
|
53
54
|
this._concurrent = concurrent;
|
|
54
55
|
}
|
|
55
|
-
/**从arraylike创建流
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
/**从arraylike创建流
|
|
57
|
+
* @param args - arraylike
|
|
58
|
+
* @param concurrent - 并发数 默认 1
|
|
59
|
+
*/
|
|
60
|
+
static from<T>(args:ArrayLike<T>,concurrent:number=1){
|
|
61
|
+
return new Stream(Array.from(args),concurrent);
|
|
58
62
|
}
|
|
59
63
|
/**从数组创建流 */
|
|
60
64
|
static of<T>(...args:T[]){
|
|
61
|
-
return new Stream
|
|
65
|
+
return new Stream(args);
|
|
62
66
|
}
|
|
63
67
|
/**设置并发数 */
|
|
64
68
|
concurrent(count: number): Stream<T> {
|
|
@@ -204,6 +208,8 @@ export class Stream<T> implements Iterable<T>{
|
|
|
204
208
|
return this._list.length;
|
|
205
209
|
}
|
|
206
210
|
}
|
|
211
|
+
//#endregion
|
|
212
|
+
|
|
207
213
|
|
|
208
214
|
/**文本模板渲染器
|
|
209
215
|
* @template T - 上下文对象的类型,默认为记录类型
|
|
@@ -248,3 +254,6 @@ export class Hbs<T extends Record<Keyable,any> = Record<Keyable,any>>{
|
|
|
248
254
|
}
|
|
249
255
|
}
|
|
250
256
|
}
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|