@zwa73/utils 1.0.240 → 1.0.242
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/UtilHttp.d.ts +0 -4
- package/dist/UtilHttp.js +17 -9
- package/package.json +1 -1
package/dist/UtilHttp.d.ts
CHANGED
|
@@ -14,10 +14,6 @@ export type RequestResult<T> = {
|
|
|
14
14
|
};
|
|
15
15
|
/**网络请求选项 */
|
|
16
16
|
export type RequestOption = {
|
|
17
|
-
/**请求之间的间隔, 单位毫秒 默认 1
|
|
18
|
-
* 如果用promise并发请求可能会导致req.write提前中断, 为避免此问题需要设置请求间隔
|
|
19
|
-
*/
|
|
20
|
-
interval?: number;
|
|
21
17
|
/**请求协议 */
|
|
22
18
|
protocol: 'http:' | 'https:';
|
|
23
19
|
/**超时时间/毫秒 最小为10_000 默认无限 */
|
package/dist/UtilHttp.js
CHANGED
|
@@ -242,9 +242,12 @@ class UtilHttp {
|
|
|
242
242
|
this._data.headers ??= {};
|
|
243
243
|
this._data.headers['Content-Length'] = Buffer.byteLength(data);
|
|
244
244
|
return {
|
|
245
|
-
proc: req => {
|
|
246
|
-
if (isPost)
|
|
247
|
-
req.write(data);
|
|
245
|
+
proc: async (req) => {
|
|
246
|
+
if (isPost) {
|
|
247
|
+
const flushed = req.write(data);
|
|
248
|
+
if (!flushed)
|
|
249
|
+
await new Promise((resolve) => req.once('drain', resolve));
|
|
250
|
+
}
|
|
248
251
|
req.end();
|
|
249
252
|
}
|
|
250
253
|
};
|
|
@@ -294,9 +297,12 @@ class UtilHttp {
|
|
|
294
297
|
this._data.headers ??= {};
|
|
295
298
|
this._data.headers['Content-Length'] = Buffer.byteLength(data);
|
|
296
299
|
return {
|
|
297
|
-
proc: req => {
|
|
298
|
-
if (isPost)
|
|
299
|
-
req.write(data);
|
|
300
|
+
proc: async (req) => {
|
|
301
|
+
if (isPost) {
|
|
302
|
+
const flushed = req.write(data);
|
|
303
|
+
if (!flushed)
|
|
304
|
+
await new Promise((resolve) => req.once('drain', resolve));
|
|
305
|
+
}
|
|
300
306
|
req.end();
|
|
301
307
|
}
|
|
302
308
|
};
|
|
@@ -376,15 +382,17 @@ class UtilHttp {
|
|
|
376
382
|
static async request(option, sendProc, acceptProc) {
|
|
377
383
|
const { reduce: reqReduce, init: reqInit } = acceptProc;
|
|
378
384
|
const { proc: reqProc } = sendProc;
|
|
379
|
-
const { protocol, timeout = 0,
|
|
385
|
+
const { protocol, timeout = 0,
|
|
386
|
+
//interval = 0,
|
|
387
|
+
...baseReqOpt } = option;
|
|
380
388
|
const plusTimeout = timeout + 1000;
|
|
381
389
|
const hasTimeLimit = timeout >= 10_000;
|
|
382
390
|
const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
|
|
383
391
|
const reduceQueue = new js_utils_1.PromiseQueue();
|
|
384
392
|
let dataPromise = null;
|
|
385
393
|
//等待间隔
|
|
386
|
-
if
|
|
387
|
-
|
|
394
|
+
//if(UtilHttp.requestQueue.length>0 && interval>0)
|
|
395
|
+
// await UtilHttp.requestQueue.enqueue(async ()=>UtilFunc.sleep(interval));
|
|
388
396
|
return new Promise(async (resolve, rejecte) => {
|
|
389
397
|
const finallyTimeout = hasTimeLimit
|
|
390
398
|
? setTimeout(() => {
|