@zwa73/utils 1.0.197 → 1.0.199
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 +147 -54
- package/dist/UtilCom.js +174 -80
- package/dist/UtilFileTools.d.ts +0 -1
- package/dist/UtilFileTools.js +0 -1
- package/dist/UtilFunctions.d.ts +11 -7
- package/dist/UtilFunctions.js +27 -23
- package/dist/UtilInterfaces.d.ts +7 -7
- package/package.json +2 -1
- package/src/UtilCom.macro.ts +28 -0
- package/src/UtilCom.ts +215 -116
- package/src/UtilFileTools.ts +0 -1
- package/src/UtilFunctions.ts +39 -27
- package/src/UtilInterfaces.ts +9 -11
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ type StringifyOpt = Partial<{
|
|
|
20
20
|
space: string | number | null | undefined;
|
|
21
21
|
}>;
|
|
22
22
|
/**Promise重试选项 */
|
|
23
|
-
export type
|
|
23
|
+
export type PromiseRetries = Partial<{
|
|
24
24
|
/**重试次数 默认3*/
|
|
25
25
|
count?: number;
|
|
26
26
|
/**尝试间隔时间 超过此事件会重新创建新的Promise
|
|
@@ -29,11 +29,15 @@ export type RepeatPromiseOpt = Partial<{
|
|
|
29
29
|
tryInterval?: number;
|
|
30
30
|
/**尝试延迟 重新尝试时会先等待此毫秒数 毫秒 默认0*/
|
|
31
31
|
tryDelay?: number;
|
|
32
|
+
/**是否使用指数回退 默认false 仅在tryDelay被设置时有效 */
|
|
33
|
+
expBackoff?: boolean;
|
|
34
|
+
/**指数回退上限值 默认无限*/
|
|
35
|
+
expBackoffMax?: number;
|
|
32
36
|
}>;
|
|
33
37
|
type SuccessOut<T> = Outcome<Success, T>;
|
|
34
38
|
type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
|
|
35
39
|
/**完成的重试请求 */
|
|
36
|
-
export type
|
|
40
|
+
export type PromiseRetryResult<T> = {
|
|
37
41
|
completed: T | undefined;
|
|
38
42
|
/**还未完成的其他Promise 若是验证失败则会返回undefined */
|
|
39
43
|
pending: Promise<T | undefined>[];
|
|
@@ -108,12 +112,12 @@ export declare class UtilFunc {
|
|
|
108
112
|
static getNeverResolvedPromise<T>(): Promise<T>;
|
|
109
113
|
/**重复尝试promise
|
|
110
114
|
* @async
|
|
111
|
-
* @param
|
|
112
|
-
* @param
|
|
113
|
-
* @param
|
|
115
|
+
* @param proc - 发起函数
|
|
116
|
+
* @param verify - 验证函数
|
|
117
|
+
* @param retries - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
|
|
114
118
|
* @returns 重复结果
|
|
115
119
|
*/
|
|
116
|
-
static
|
|
120
|
+
static retryPromise<T>(proc: () => Promise<T>, verify?: StatusVerifyFn<T>, retries?: PromiseRetries): Promise<PromiseRetryResult<T>>;
|
|
117
121
|
/**创建一个限时的Promise
|
|
118
122
|
* @param func - 处理函数
|
|
119
123
|
* @param timeLimit - 毫秒限时
|
|
@@ -292,7 +296,7 @@ export declare class UtilFunc {
|
|
|
292
296
|
* @param func - 待转换函数
|
|
293
297
|
* @returns 转换完成的函数
|
|
294
298
|
*/
|
|
295
|
-
static eitherize<T extends AnyFunc>(func: T): (...args: Parameters<T>) => Outcome<typeof Success, ReturnType<T>> | Outcome<typeof Failed, Error>;
|
|
299
|
+
static eitherize<T extends AnyFunc>(func: ReturnType<T> extends Promise<any> ? Error & "对异步函数请使用taskEitherize" : T): (...args: Parameters<T>) => Outcome<typeof Success, ReturnType<T>> | Outcome<typeof Failed, Error>;
|
|
296
300
|
/**将传入的异步函数包装为显式处理错误的函数
|
|
297
301
|
* @param func - 待转换函数
|
|
298
302
|
* @returns 转换完成的函数
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -168,35 +168,39 @@ class UtilFunc {
|
|
|
168
168
|
}
|
|
169
169
|
/**重复尝试promise
|
|
170
170
|
* @async
|
|
171
|
-
* @param
|
|
172
|
-
* @param
|
|
173
|
-
* @param
|
|
171
|
+
* @param proc - 发起函数
|
|
172
|
+
* @param verify - 验证函数
|
|
173
|
+
* @param retries - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
|
|
174
174
|
* @returns 重复结果
|
|
175
175
|
*/
|
|
176
|
-
static async
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const { count, tryInterval } =
|
|
176
|
+
static async retryPromise(proc, verify, retries = {}) {
|
|
177
|
+
retries.count = retries.count ?? 3;
|
|
178
|
+
retries.tryInterval = retries.tryInterval ?? 180_000;
|
|
179
|
+
const { count, tryInterval, tryDelay } = retries;
|
|
180
180
|
/**是否含有超时时间 */
|
|
181
181
|
const hasRepeatTime = (tryInterval >= 5000);
|
|
182
182
|
//验证处理函数
|
|
183
|
-
if (
|
|
184
|
-
|
|
183
|
+
if (verify === undefined)
|
|
184
|
+
verify = () => UtilSymbol_1.Success;
|
|
185
185
|
//进行中的请求
|
|
186
186
|
const plist = [];
|
|
187
187
|
//开始处理
|
|
188
188
|
try {
|
|
189
189
|
//根据最大重试次数限制进行循环
|
|
190
190
|
for (let i = 0; i < count;) {
|
|
191
|
-
if (i > 0 &&
|
|
192
|
-
|
|
193
|
-
|
|
191
|
+
if (i > 0 && tryDelay) {
|
|
192
|
+
const delay = retries.expBackoff
|
|
193
|
+
? Math.min((Math.pow(2, i) - 1) * tryDelay, retries.expBackoffMax ?? Infinity)
|
|
194
|
+
: tryDelay;
|
|
195
|
+
await UtilFunc.sleep(delay);
|
|
196
|
+
}
|
|
197
|
+
UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 retryPromise`);
|
|
194
198
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
195
199
|
if (plist.length < i + 1) {
|
|
196
200
|
plist.push(UtilFunc.timelimitPromise(async () => {
|
|
197
201
|
const index = i;
|
|
198
|
-
const result = await
|
|
199
|
-
const stat = await
|
|
202
|
+
const result = await proc();
|
|
203
|
+
const stat = await verify(result);
|
|
200
204
|
return { result, stat, index };
|
|
201
205
|
}, hasRepeatTime ? tryInterval : undefined));
|
|
202
206
|
}
|
|
@@ -209,7 +213,7 @@ class UtilFunc {
|
|
|
209
213
|
const res = await currObj.result;
|
|
210
214
|
rslove(UtilFunc.outcome(UtilSymbol_1.Success, res));
|
|
211
215
|
});
|
|
212
|
-
UtilLogger_1.SLogger.warn(`第 ${i + 1} 次
|
|
216
|
+
UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 retryPromise 超时 ${tryInterval} ms 开始重试`);
|
|
213
217
|
i++;
|
|
214
218
|
continue;
|
|
215
219
|
}
|
|
@@ -217,14 +221,14 @@ class UtilFunc {
|
|
|
217
221
|
const postresult = currObj.result;
|
|
218
222
|
const result = UtilFunc.matchProc(postresult.stat, {
|
|
219
223
|
[UtilSymbol_1.Success]() {
|
|
220
|
-
UtilLogger_1.SLogger.info(`第 ${postresult.index + 1} 次
|
|
224
|
+
UtilLogger_1.SLogger.info(`第 ${postresult.index + 1} 次 retryPromise 成功`);
|
|
221
225
|
//非当前
|
|
222
226
|
if (postresult.index != i)
|
|
223
227
|
UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试时间\n当前index: ${i}\n当前重试时间: ${tryInterval}`);
|
|
224
228
|
return postresult.result;
|
|
225
229
|
},
|
|
226
230
|
[UtilSymbol_1.Terminated]() {
|
|
227
|
-
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次
|
|
231
|
+
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 retryPromise 终止 停止重试`);
|
|
228
232
|
return postresult.result;
|
|
229
233
|
},
|
|
230
234
|
[UtilSymbol_1.Failed]() {
|
|
@@ -232,12 +236,12 @@ class UtilFunc {
|
|
|
232
236
|
plist[postresult.index] = UtilFunc.getNeverResolvedPromise();
|
|
233
237
|
//是当前
|
|
234
238
|
if (postresult.index == i) {
|
|
235
|
-
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次
|
|
239
|
+
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 retryPromise 失败 开始重试`);
|
|
236
240
|
i++;
|
|
237
241
|
return UtilSymbol_1.None;
|
|
238
242
|
}
|
|
239
243
|
//非当前
|
|
240
|
-
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次
|
|
244
|
+
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 retryPromise 失败`);
|
|
241
245
|
return UtilSymbol_1.None;
|
|
242
246
|
},
|
|
243
247
|
});
|
|
@@ -264,7 +268,7 @@ class UtilFunc {
|
|
|
264
268
|
};
|
|
265
269
|
}
|
|
266
270
|
//全部失败或超时则返回 全pending
|
|
267
|
-
UtilLogger_1.SLogger.warn(`${count} 次
|
|
271
|
+
UtilLogger_1.SLogger.warn(`${count} 次 retryPromise 尝试均失败`);
|
|
268
272
|
return {
|
|
269
273
|
completed: undefined,
|
|
270
274
|
pending: plist
|
|
@@ -286,7 +290,7 @@ class UtilFunc {
|
|
|
286
290
|
};
|
|
287
291
|
}
|
|
288
292
|
catch (err) {
|
|
289
|
-
UtilLogger_1.SLogger.warn(`
|
|
293
|
+
UtilLogger_1.SLogger.warn(`retryPromise 发生意外错误`, err);
|
|
290
294
|
return { completed: undefined, pending: [] };
|
|
291
295
|
}
|
|
292
296
|
}
|
|
@@ -792,8 +796,8 @@ class UtilFunc {
|
|
|
792
796
|
}
|
|
793
797
|
exports.UtilFunc = UtilFunc;
|
|
794
798
|
__decorate([
|
|
795
|
-
(0, UtilDecorators_1.LogTimeAsync)("
|
|
799
|
+
(0, UtilDecorators_1.LogTimeAsync)("retryPromise ", true),
|
|
796
800
|
__metadata("design:type", Function),
|
|
797
801
|
__metadata("design:paramtypes", [Function, Function, Object]),
|
|
798
802
|
__metadata("design:returntype", Promise)
|
|
799
|
-
], UtilFunc, "
|
|
803
|
+
], UtilFunc, "retryPromise", null);
|
package/dist/UtilInterfaces.d.ts
CHANGED
|
@@ -124,7 +124,7 @@ export type RequiredOnly<T> = {
|
|
|
124
124
|
export type WithPrefix<T, P extends string> = {
|
|
125
125
|
[K in (keyof T) as K extends string ? `${P}${K}` : K]: T[K];
|
|
126
126
|
};
|
|
127
|
-
|
|
127
|
+
/**附带状态的返回结果用于状态返回值的封装
|
|
128
128
|
* @template K - 类型键值
|
|
129
129
|
* @template V - 值
|
|
130
130
|
*/
|
|
@@ -134,16 +134,16 @@ export type Outcome<K extends Keyable, V> = {
|
|
|
134
134
|
/**值 */
|
|
135
135
|
readonly result: V;
|
|
136
136
|
};
|
|
137
|
-
|
|
138
|
-
*
|
|
139
|
-
* @template F - 失败时的结果
|
|
137
|
+
/**可进行匹配的对象
|
|
138
|
+
* 如果是Outcome则自动处理status
|
|
140
139
|
*/
|
|
141
|
-
export type Eithercome<S, F> = Outcome<Success, S> | Outcome<Failed, F>;
|
|
142
|
-
/**可进行匹配的 outcome或symbol */
|
|
143
140
|
export type Matchable<T extends Keyable> = T | Outcome<T, unknown>;
|
|
144
141
|
/**可进行匹配的 outcome或symbol 中的状态类型 */
|
|
145
142
|
export type MatchableFlag<T> = T extends infer O | Outcome<infer O, unknown> ? O : never;
|
|
146
|
-
/**从联合 Outcome 中 根据
|
|
143
|
+
/**从联合 Outcome 中 根据 K 提取对应 Outcome
|
|
144
|
+
* @template T - 联合Outcome匹配的类型
|
|
145
|
+
* @template K - 所需的状态
|
|
146
|
+
*/
|
|
147
147
|
export type ExtractOutcome<T, K extends Keyable> = T extends Outcome<infer O, unknown> ? O extends Exclude<O, K> ? never : T : never;
|
|
148
148
|
/**用于辅助解析智能补全的类型
|
|
149
149
|
* 输出schema后替换为 ^.*$ 的 string 匹配
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.199",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@deepkit/type": "^1.0.1-alpha.153",
|
|
20
20
|
"fluent-ffmpeg": "2.1.2",
|
|
21
|
+
"form-data": "^4.0.2",
|
|
21
22
|
"glob": "^10.4.1",
|
|
22
23
|
"handlebars": "^4.7.8",
|
|
23
24
|
"html-entities": "^2.3.3",
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { regionMacro } from "@zwa73/dev-utils";
|
|
2
|
+
import { dedent } from "./QuickExport";
|
|
3
|
+
|
|
4
|
+
const sep1 = ["http","https"];
|
|
5
|
+
const sep2 = ["get","post"];
|
|
6
|
+
const AcceptTypeList = ["json","raw"]as const;
|
|
7
|
+
const SendTypeList = ["json","query","formData","none","raw"] as const;
|
|
8
|
+
const sep3 = SendTypeList;
|
|
9
|
+
const up = (str:string)=>{
|
|
10
|
+
if (str.length === 0) return str; // 处理空字符串
|
|
11
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
12
|
+
}
|
|
13
|
+
if(false)
|
|
14
|
+
regionMacro("UtilCom宏定义",()=>{
|
|
15
|
+
const result:string[] = [];
|
|
16
|
+
sep1.forEach(s1=>{
|
|
17
|
+
sep2.forEach(s2=>{
|
|
18
|
+
sep3.filter(s=>s!='raw').forEach(s3=>{
|
|
19
|
+
result.push(dedent`
|
|
20
|
+
/** 宏 UtilCom.${s1}().${s2}().send('${s3}').acceptJson() */
|
|
21
|
+
static ${s1}${up(s2)}${up(s3)}(){
|
|
22
|
+
return UtilCom.${s1}().${s2}().send('${s3}' as const).acceptJson();
|
|
23
|
+
}`.trim());
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
return result.reverse().join('\n');
|
|
28
|
+
});
|