@zwa73/utils 1.0.216 → 1.0.218

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.
@@ -1 +1 @@
1
- export { Piper, Stream, Hbs, BridgeInterface, Bridge, SmartCache, DListMiddleNode, DLinkedList, PromiseQueue } from "@zwa73/js-utils";
1
+ export { Piper, Stream, Hbs, BridgeInterface, Bridge, SmartCache, DListMiddleNode, DLinkedList, PromoseQueueOption, PromiseQueue } from "@zwa73/js-utils";
package/dist/UtilCom.d.ts CHANGED
@@ -32,6 +32,8 @@ export type RequestOption = {
32
32
  /**内容长度 一般无需填写 应为buffer长度而非字符串长度 */
33
33
  'Content-Length'?: number;
34
34
  };
35
+ /**响应buffer的编码 默认utf8 */
36
+ responseEncode?: BufferEncoding;
35
37
  } & http.RequestOptions;
36
38
  /**getquery请求所允许的数据 */
37
39
  export type QueryRequestData = NodeJS.Dict<string | number | boolean | readonly string[] | readonly number[] | readonly boolean[] | null>;
@@ -57,10 +59,11 @@ type SendProc<T extends any[]> = {
57
59
  };
58
60
  declare const SendNoneProc: SendProc<[]>;
59
61
  declare const AcceptStringProc: AcceptProc<string, RequestResult<string> | undefined>;
62
+ declare const AcceptNoneProc: AcceptProc<undefined, undefined>;
60
63
  /**send处理的参数 */
61
- type SendParams<T extends SendProc<any>> = T extends SendProc<infer T> ? T : never;
64
+ type SendParams<P extends SendProc<any>> = P extends SendProc<infer T> ? T : never;
62
65
  /**accept处理的结果 */
63
- type ParseResult<D extends AcceptProc<any, any>> = D extends AcceptProc<any, infer T> ? Awaited<T> : never;
66
+ type ParseResult<P extends AcceptProc<any, any>> = P extends AcceptProc<any, infer T> ? Awaited<T> : never;
64
67
  /**网络请求工具 */
65
68
  export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<RequestOption, 'protocol'>>, S extends SendProc<any>, A extends AcceptProc<any, any>> {
66
69
  private _data;
@@ -165,9 +168,11 @@ export declare class UtilCom<D extends Partial<RequestOption> & Required<Pick<Re
165
168
  accept<T extends AcceptType>(t: T): {
166
169
  readonly json: UtilCom<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
167
170
  readonly string: UtilCom<D, S, AcceptProc<string, RequestResult<string> | undefined>>;
171
+ readonly none: UtilCom<D, S, AcceptProc<undefined, undefined>>;
168
172
  }[T];
169
173
  acceptJson(): UtilCom<D, S, AcceptProc<string, RequestResult<JToken> | undefined>>;
170
174
  acceptString(): UtilCom<D, S, typeof AcceptStringProc>;
175
+ acceptNone(): UtilCom<D, S, typeof AcceptNoneProc>;
171
176
  /**自定的接收数据类型*/
172
177
  acceptRaw<AD, AT>(proc: AcceptProc<AD, AT>): UtilCom<D, S, typeof proc>;
173
178
  /**预设的发送数据类型*/
package/dist/UtilCom.js CHANGED
@@ -22,6 +22,11 @@ const AcceptStringProc = {
22
22
  reduce: (acc, dat) => acc + dat,
23
23
  parse: (result) => result
24
24
  };
25
+ const AcceptNoneProc = {
26
+ init: undefined,
27
+ reduce: () => undefined,
28
+ parse: () => undefined
29
+ };
25
30
  /**网络请求工具 */
26
31
  class UtilCom {
27
32
  _data;
@@ -128,6 +133,7 @@ class UtilCom {
128
133
  const map = {
129
134
  'json': this.acceptJson(),
130
135
  'string': this.acceptString(),
136
+ 'none': this.acceptNone(),
131
137
  };
132
138
  return map[t];
133
139
  }
@@ -163,6 +169,10 @@ class UtilCom {
163
169
  this._accept = AcceptStringProc;
164
170
  return this;
165
171
  }
172
+ acceptNone() {
173
+ this._accept = AcceptNoneProc;
174
+ return this;
175
+ }
166
176
  /**自定的接收数据类型*/
167
177
  acceptRaw(proc) {
168
178
  this._accept = proc;
@@ -273,6 +283,7 @@ class UtilCom {
273
283
  const { protocol, timeout, ...baseReqOpt } = option;
274
284
  const hasTimeLimit = (timeout ? timeout >= 10_000 : false);
275
285
  const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
286
+ const reduceQueue = new js_utils_1.PromiseQueue();
276
287
  let dataPromise = null;
277
288
  return new Promise(async (resolve, rejecte) => {
278
289
  const resFunc = (res) => {
@@ -281,15 +292,20 @@ class UtilCom {
281
292
  if (hasTimeLimit) {
282
293
  res.setTimeout(timeout, () => {
283
294
  UtilLogger_1.SLogger.warn(`${flagName} 接收反馈超时: ${timeout} ms`);
284
- resolve(undefined);
295
+ res.destroy();
285
296
  });
286
297
  }
287
298
  let mergedata = init;
288
- res.setEncoding('utf8');
299
+ res.setEncoding(option.responseEncode ?? 'utf8');
289
300
  res.on('data', chunk => {
290
- dataPromise = js_utils_1.PromiseQueue.enqueue(flagName, async () => mergedata = await reduce(mergedata, chunk));
301
+ dataPromise = reduceQueue
302
+ .enqueue(async () => mergedata = await reduce(mergedata, chunk))
303
+ .catch(e => {
304
+ UtilLogger_1.SLogger.error(`${flagName} reduce函数错误:${e}\nchunk:${chunk}\nmergedata:`, mergedata);
305
+ resolve(undefined);
306
+ });
291
307
  });
292
- res.on('error', (e) => {
308
+ res.on('error', e => {
293
309
  UtilLogger_1.SLogger.warn(`${flagName} 接收反馈错误:${e}`);
294
310
  resolve(undefined);
295
311
  });
@@ -319,11 +335,17 @@ class UtilCom {
319
335
  req.destroy();
320
336
  });
321
337
  }
322
- req.on('error', (e) => {
338
+ req.on('error', e => {
323
339
  UtilLogger_1.SLogger.warn(`${flagName} 发送请求错误:${e}`);
324
340
  resolve(undefined);
325
341
  });
326
- await proc(req);
342
+ try {
343
+ await proc(req);
344
+ }
345
+ catch (e) {
346
+ UtilLogger_1.SLogger.error(`${flagName} proc函数错误:${e}`);
347
+ resolve(undefined);
348
+ }
327
349
  });
328
350
  }
329
351
  /**构建query */
@@ -158,7 +158,7 @@ export declare namespace UtilFT {
158
158
  * @param opt.recursive - 搜索子目录
159
159
  * @returns 文件名路径数组
160
160
  */
161
- function fileSearchRegex(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): Promise<string[]>;
161
+ function fileSearchRegex(dir: string, traitRegex: string | RegExp, opt?: FileSearchRegexOpt): Promise<string[]>;
162
162
  /**搜索路径符合正则表达式的文件 同步版本
163
163
  * @param dir - 起始目录
164
164
  * @param traitRegex - 正则表达式
@@ -166,7 +166,7 @@ export declare namespace UtilFT {
166
166
  * @param opt.relative - 搜索子目录
167
167
  * @returns 文件名路径数组
168
168
  */
169
- function fileSearchRegexSync(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
169
+ function fileSearchRegexSync(dir: string, traitRegex: string | RegExp, opt?: FileSearchRegexOpt): string[];
170
170
  /**搜索符合Glob匹配的文件
171
171
  * @param dir - 起始目录
172
172
  * @param globPattern - glob匹配
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.216",
3
+ "version": "1.0.218",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {