@zwa73/utils 1.0.240 → 1.0.241
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 +3 -2
- package/dist/UtilHttp.js +14 -8
- package/package.json +1 -1
package/dist/UtilHttp.d.ts
CHANGED
|
@@ -14,8 +14,9 @@ export type RequestResult<T> = {
|
|
|
14
14
|
};
|
|
15
15
|
/**网络请求选项 */
|
|
16
16
|
export type RequestOption = {
|
|
17
|
-
/**请求之间的间隔, 单位毫秒 默认
|
|
18
|
-
* 如果用promise
|
|
17
|
+
/**请求之间的间隔, 单位毫秒 默认 0
|
|
18
|
+
* 如果用promise并发请求可能会因缓冲区溢出导致req.write提前中断
|
|
19
|
+
* 为避免此问题需要设置请求间隔
|
|
19
20
|
*/
|
|
20
21
|
interval?: number;
|
|
21
22
|
/**请求协议 */
|
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,14 +382,14 @@ 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, interval =
|
|
385
|
+
const { protocol, timeout = 0, interval = 0, ...baseReqOpt } = option;
|
|
380
386
|
const plusTimeout = timeout + 1000;
|
|
381
387
|
const hasTimeLimit = timeout >= 10_000;
|
|
382
388
|
const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
|
|
383
389
|
const reduceQueue = new js_utils_1.PromiseQueue();
|
|
384
390
|
let dataPromise = null;
|
|
385
391
|
//等待间隔
|
|
386
|
-
if (UtilHttp.requestQueue.length > 0)
|
|
392
|
+
if (UtilHttp.requestQueue.length > 0 && interval > 0)
|
|
387
393
|
await UtilHttp.requestQueue.enqueue(async () => UtilFunctions_1.UtilFunc.sleep(interval));
|
|
388
394
|
return new Promise(async (resolve, rejecte) => {
|
|
389
395
|
const finallyTimeout = hasTimeLimit
|