@zwa73/utils 1.0.234 → 1.0.239

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.
@@ -11,6 +11,8 @@ type ExecOpt = Partial<{
11
11
  nodeModules: string;
12
12
  /**工作路径 */
13
13
  cwd: string;
14
+ /**额外环境变量 */
15
+ env: string;
14
16
  }>;
15
17
  /**常用函数 */
16
18
  declare class _UtilFunc {
@@ -34,6 +36,7 @@ declare class _UtilFunc {
34
36
  * @param opt.errlvl - 错误的日志等级
35
37
  * @param opt.nodeModules - nodeModules文件夹路径
36
38
  * @param opt.cwd - 执行目录
39
+ * @param opt.env - 额外环境变量
37
40
  */
38
41
  static exec(command: string, opt?: ExecOpt): Promise<{
39
42
  stdout: string;
@@ -72,11 +72,12 @@ class _UtilFunc {
72
72
  * @param opt.errlvl - 错误的日志等级
73
73
  * @param opt.nodeModules - nodeModules文件夹路径
74
74
  * @param opt.cwd - 执行目录
75
+ * @param opt.env - 额外环境变量
75
76
  */
76
77
  static async exec(command, opt) {
77
78
  return new Promise(async (resolve, reject) => {
78
79
  // 创建一个新的环境变量对象,并将项目的 node_modules/.bin 目录添加到 PATH 环境变量中
79
- const env = Object.create(process.env);
80
+ const env = Object.assign(Object.create(process.env), opt?.env ?? {});
80
81
  const penv = opt?.nodeModules
81
82
  ? path_1.default.join(UtilFileTools_1.UtilFT.currosizePath(opt.nodeModules), '.bin')
82
83
  : UtilFileTools_1.UtilFT.currosizePath(await exports.UtilFunc.memoize(UtilFileTools_1.UtilFT.findNodeModulesDir)(process.cwd(), '.bin') ?? '');
@@ -2,6 +2,7 @@ import { AnyString, JToken, MPromise, PartialOption, StatusVerifyFn } from "./Ut
2
2
  import http from 'http';
3
3
  import { PromiseRetries } from "./UtilFunctions";
4
4
  import FormData from "form-data";
5
+ import { PromiseQueue } from "@zwa73/js-utils";
5
6
  /**网络请求返回值 */
6
7
  export type RequestResult<T> = {
7
8
  /**响应头 */
@@ -13,6 +14,10 @@ export type RequestResult<T> = {
13
14
  };
14
15
  /**网络请求选项 */
15
16
  export type RequestOption = {
17
+ /**请求之间的间隔, 单位毫秒 默认 1
18
+ * 如果用promise并发请求可能会导致req.write提前中断, 为避免此问题需要设置请求间隔
19
+ */
20
+ interval?: number;
16
21
  /**请求协议 */
17
22
  protocol: 'http:' | 'https:';
18
23
  /**超时时间/毫秒 最小为10_000 默认无限 */
@@ -289,6 +294,8 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
289
294
  } : {
290
295
  option: OPT;
291
296
  })>): Promise<import("@zwa73/js-utils").PromiseRetryResult<ParseResult<A>>>;
297
+ /**控制请求间隔的队列 */
298
+ static requestQueue: PromiseQueue;
292
299
  /**发送网络请求
293
300
  * @param option - 网络请求选项
294
301
  * @param reqProc - 请求处理函数 需调用req.end()
package/dist/UtilHttp.js CHANGED
@@ -366,6 +366,8 @@ class UtilHttp {
366
366
  const procFn = async () => this.once(arg);
367
367
  return UtilFunctions_1.UtilFunc.retryPromise(procFn, verify, retries);
368
368
  }
369
+ /**控制请求间隔的队列 */
370
+ static requestQueue = new js_utils_1.PromiseQueue();
369
371
  /**发送网络请求
370
372
  * @param option - 网络请求选项
371
373
  * @param reqProc - 请求处理函数 需调用req.end()
@@ -374,12 +376,15 @@ class UtilHttp {
374
376
  static async request(option, sendProc, acceptProc) {
375
377
  const { reduce: reqReduce, init: reqInit } = acceptProc;
376
378
  const { proc: reqProc } = sendProc;
377
- const { protocol, timeout, ...baseReqOpt } = option;
378
- const plusTimeout = (timeout ?? 0) + 1000;
379
- const hasTimeLimit = (timeout ? timeout >= 10_000 : false);
379
+ const { protocol, timeout = 0, interval = 1, ...baseReqOpt } = option;
380
+ const plusTimeout = timeout + 1000;
381
+ const hasTimeLimit = timeout >= 10_000;
380
382
  const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
381
383
  const reduceQueue = new js_utils_1.PromiseQueue();
382
384
  let dataPromise = null;
385
+ //等待间隔
386
+ if (UtilHttp.requestQueue.length > 0)
387
+ await UtilHttp.requestQueue.enqueue(async () => UtilFunctions_1.UtilFunc.sleep(interval));
383
388
  return new Promise(async (resolve, rejecte) => {
384
389
  const finallyTimeout = hasTimeLimit
385
390
  ? setTimeout(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.234",
3
+ "version": "1.0.239",
4
4
  "description": "my utils",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {