@wiajs/request 3.0.8 → 3.0.17
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/request.cjs +68 -34
- package/dist/request.mjs +68 -34
- package/lib/index.js +1 -4
- package/lib/request.js +70 -44
- package/package.json +2 -2
package/dist/request.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.17
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -422,10 +422,36 @@ const brotliOptions = {
|
|
|
422
422
|
|
|
423
423
|
const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
|
|
424
424
|
|
|
425
|
+
// clientRequest 属性转发
|
|
426
|
+
const writeProps = [
|
|
427
|
+
'protocol',
|
|
428
|
+
'method',
|
|
429
|
+
'path',
|
|
430
|
+
'host',
|
|
431
|
+
'reusedSocket',
|
|
432
|
+
'socket',
|
|
433
|
+
'closed',
|
|
434
|
+
'destroyed',
|
|
435
|
+
'writable',
|
|
436
|
+
'writableAborted',
|
|
437
|
+
'writableEnded',
|
|
438
|
+
'writableCorked',
|
|
439
|
+
'errored',
|
|
440
|
+
'writableFinished',
|
|
441
|
+
'writableHighWaterMark',
|
|
442
|
+
'writableLength',
|
|
443
|
+
'writableNeedDrain',
|
|
444
|
+
'writableObjectMode',
|
|
445
|
+
];
|
|
446
|
+
|
|
447
|
+
// clientReq 方法转发
|
|
448
|
+
const writeMethods = ['cork', 'flushHeaders', 'setNoDelay', 'setSocketKeepAlive'];
|
|
449
|
+
|
|
425
450
|
// Create handlers that pass events from native requests
|
|
451
|
+
// 在 clientRequest 事件转发
|
|
426
452
|
const writeEvents = [
|
|
427
|
-
'abort', // 弃用
|
|
428
|
-
'aborted', // 弃用
|
|
453
|
+
// 'abort', // 弃用
|
|
454
|
+
// 'aborted', // 弃用
|
|
429
455
|
'close',
|
|
430
456
|
'connect',
|
|
431
457
|
'continue',
|
|
@@ -449,7 +475,7 @@ for (const ev of writeEvents)
|
|
|
449
475
|
m.redirectReq.emit(ev, ...args); // req 事情映射到 redirectReq 上触发
|
|
450
476
|
};
|
|
451
477
|
|
|
452
|
-
// stream.Readable
|
|
478
|
+
// stream.Readable,在响应流上转发读流取事件
|
|
453
479
|
// data 单独处理
|
|
454
480
|
const readEvents = ['close', 'end', 'error', 'pause', 'readable', 'resume'];
|
|
455
481
|
const readEventEmit = Object.create(null);
|
|
@@ -512,13 +538,15 @@ class Request extends stream.Duplex {
|
|
|
512
538
|
super();
|
|
513
539
|
const m = this;
|
|
514
540
|
|
|
515
|
-
// log({
|
|
541
|
+
// log({opts}, 'constructor');
|
|
516
542
|
|
|
517
543
|
// Initialize the request
|
|
518
544
|
m.sanitizeOptions(opts);
|
|
519
545
|
m.opt = opts;
|
|
520
546
|
m.headers = opts.headers;
|
|
547
|
+
|
|
521
548
|
// log({opts}, 'constructor')
|
|
549
|
+
|
|
522
550
|
m._ended = false;
|
|
523
551
|
m._ending = false;
|
|
524
552
|
m._redirectCount = 0;
|
|
@@ -540,8 +568,31 @@ class Request extends stream.Duplex {
|
|
|
540
568
|
}
|
|
541
569
|
};
|
|
542
570
|
|
|
543
|
-
//
|
|
571
|
+
// Proxy all other public ClientRequest methods 'getHeader'
|
|
572
|
+
for (const method of writeMethods) {
|
|
573
|
+
// @ts-ignore
|
|
574
|
+
m[method] = (a, b) => {
|
|
575
|
+
log$1(method, {a, b});
|
|
576
|
+
// @ts-ignore
|
|
577
|
+
m._currentRequest?.[method](a, b);
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// Proxy all public ClientRequest properties
|
|
582
|
+
// 'aborted', 'connection' 弃用
|
|
583
|
+
for (const property of writeProps) {
|
|
584
|
+
Object.defineProperty(m, property, {
|
|
585
|
+
get() {
|
|
586
|
+
// @ts-ignore
|
|
587
|
+
const val = m._currentRequest?.[property];
|
|
588
|
+
log$1('get property', {property});
|
|
589
|
+
return val
|
|
590
|
+
},
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
|
|
544
594
|
if (opts.stream) {
|
|
595
|
+
// 流模式
|
|
545
596
|
// 被 pipe 作为目标时触发,拷贝 src headers
|
|
546
597
|
m.on(
|
|
547
598
|
'pipe',
|
|
@@ -573,7 +624,7 @@ class Request extends stream.Duplex {
|
|
|
573
624
|
}
|
|
574
625
|
|
|
575
626
|
// Perform the first request
|
|
576
|
-
// m.request(); // 写入数据时执行,否则 pipe时无法写入header
|
|
627
|
+
// m.request(); // 写入数据时执行,否则 pipe 时无法写入header
|
|
577
628
|
}
|
|
578
629
|
|
|
579
630
|
/**
|
|
@@ -625,25 +676,6 @@ class Request extends stream.Duplex {
|
|
|
625
676
|
m._currentRequest = req;
|
|
626
677
|
req.redirectReq = m;
|
|
627
678
|
|
|
628
|
-
// Proxy all other public ClientRequest methods
|
|
629
|
-
for (const method of ['flushHeaders', 'setNoDelay', 'setSocketKeepAlive']) {
|
|
630
|
-
m[method] = (a, b) => {
|
|
631
|
-
log$1(method, {a, b});
|
|
632
|
-
m._currentRequest[method](a, b);
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
// Proxy all public ClientRequest properties
|
|
637
|
-
for (const property of ['aborted', 'connection', 'socket']) {
|
|
638
|
-
Object.defineProperty(m, property, {
|
|
639
|
-
get() {
|
|
640
|
-
const val = m._currentRequest[property];
|
|
641
|
-
log$1('get property', {property});
|
|
642
|
-
return val
|
|
643
|
-
},
|
|
644
|
-
});
|
|
645
|
-
}
|
|
646
|
-
|
|
647
679
|
// 启动 startTimer
|
|
648
680
|
if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
|
|
649
681
|
|
|
@@ -704,8 +736,8 @@ class Request extends stream.Duplex {
|
|
|
704
736
|
destroy(error) {
|
|
705
737
|
const m = this;
|
|
706
738
|
if (!m._ended) m.end();
|
|
707
|
-
|
|
708
|
-
|
|
739
|
+
if (m.response) m.response.destroy();
|
|
740
|
+
if (m.responseStream) m.responseStream.destroy();
|
|
709
741
|
|
|
710
742
|
// m.clearTimeout();
|
|
711
743
|
destroyRequest(m._currentRequest, error);
|
|
@@ -724,7 +756,7 @@ class Request extends stream.Duplex {
|
|
|
724
756
|
write(chunk, encoding, cb) {
|
|
725
757
|
const m = this;
|
|
726
758
|
|
|
727
|
-
log$1
|
|
759
|
+
log$1({data: chunk, encoding, callback: cb}, 'write');
|
|
728
760
|
|
|
729
761
|
// Writing is not allowed if end has been called
|
|
730
762
|
if (m._ending) throw new WriteAfterEndError()
|
|
@@ -951,6 +983,7 @@ class Request extends stream.Duplex {
|
|
|
951
983
|
*/
|
|
952
984
|
processResponse(response) {
|
|
953
985
|
const m = this;
|
|
986
|
+
const {opt} = m;
|
|
954
987
|
|
|
955
988
|
// Store the redirected response
|
|
956
989
|
const {statusCode} = response;
|
|
@@ -972,18 +1005,20 @@ class Request extends stream.Duplex {
|
|
|
972
1005
|
// If the response is not a redirect; return it as-is
|
|
973
1006
|
const {location} = response.headers;
|
|
974
1007
|
|
|
975
|
-
log$1(
|
|
1008
|
+
log$1({statusCode, headers: response.headers}, 'processResponse');
|
|
976
1009
|
|
|
977
1010
|
if (!location || m.opt.followRedirects === false || statusCode < 300 || statusCode >= 400) {
|
|
978
1011
|
// 非重定向,返回给原始回调处理
|
|
979
1012
|
response.responseUrl = m._currentUrl;
|
|
980
1013
|
response.redirects = m._redirects;
|
|
981
|
-
|
|
1014
|
+
|
|
1015
|
+
if (opt.stream) m.response = response;
|
|
1016
|
+
|
|
982
1017
|
// Be a good stream and emit end when the response is finished.
|
|
983
1018
|
// Hack to emit end on close because of a core bug that never fires end
|
|
984
1019
|
response.on('close', () => {
|
|
985
1020
|
if (!m._respended) {
|
|
986
|
-
|
|
1021
|
+
response.emit('end');
|
|
987
1022
|
}
|
|
988
1023
|
});
|
|
989
1024
|
|
|
@@ -1440,9 +1475,8 @@ function request(uri, options, callback) {
|
|
|
1440
1475
|
let R = null;
|
|
1441
1476
|
|
|
1442
1477
|
try {
|
|
1443
|
-
log({uri, options}, 'request');
|
|
1444
|
-
|
|
1445
1478
|
const {opts, cb} = init(uri, options, callback);
|
|
1479
|
+
// log({uri, options, opts}, 'request')
|
|
1446
1480
|
R = new Request(opts, cb);
|
|
1447
1481
|
} catch (e) {
|
|
1448
1482
|
log.err(e, 'request');
|
package/dist/request.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.17
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -419,10 +419,36 @@ const brotliOptions = {
|
|
|
419
419
|
|
|
420
420
|
const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
|
|
421
421
|
|
|
422
|
+
// clientRequest 属性转发
|
|
423
|
+
const writeProps = [
|
|
424
|
+
'protocol',
|
|
425
|
+
'method',
|
|
426
|
+
'path',
|
|
427
|
+
'host',
|
|
428
|
+
'reusedSocket',
|
|
429
|
+
'socket',
|
|
430
|
+
'closed',
|
|
431
|
+
'destroyed',
|
|
432
|
+
'writable',
|
|
433
|
+
'writableAborted',
|
|
434
|
+
'writableEnded',
|
|
435
|
+
'writableCorked',
|
|
436
|
+
'errored',
|
|
437
|
+
'writableFinished',
|
|
438
|
+
'writableHighWaterMark',
|
|
439
|
+
'writableLength',
|
|
440
|
+
'writableNeedDrain',
|
|
441
|
+
'writableObjectMode',
|
|
442
|
+
];
|
|
443
|
+
|
|
444
|
+
// clientReq 方法转发
|
|
445
|
+
const writeMethods = ['cork', 'flushHeaders', 'setNoDelay', 'setSocketKeepAlive'];
|
|
446
|
+
|
|
422
447
|
// Create handlers that pass events from native requests
|
|
448
|
+
// 在 clientRequest 事件转发
|
|
423
449
|
const writeEvents = [
|
|
424
|
-
'abort', // 弃用
|
|
425
|
-
'aborted', // 弃用
|
|
450
|
+
// 'abort', // 弃用
|
|
451
|
+
// 'aborted', // 弃用
|
|
426
452
|
'close',
|
|
427
453
|
'connect',
|
|
428
454
|
'continue',
|
|
@@ -446,7 +472,7 @@ for (const ev of writeEvents)
|
|
|
446
472
|
m.redirectReq.emit(ev, ...args); // req 事情映射到 redirectReq 上触发
|
|
447
473
|
};
|
|
448
474
|
|
|
449
|
-
// stream.Readable
|
|
475
|
+
// stream.Readable,在响应流上转发读流取事件
|
|
450
476
|
// data 单独处理
|
|
451
477
|
const readEvents = ['close', 'end', 'error', 'pause', 'readable', 'resume'];
|
|
452
478
|
const readEventEmit = Object.create(null);
|
|
@@ -509,13 +535,15 @@ class Request extends Duplex {
|
|
|
509
535
|
super();
|
|
510
536
|
const m = this;
|
|
511
537
|
|
|
512
|
-
// log({
|
|
538
|
+
// log({opts}, 'constructor');
|
|
513
539
|
|
|
514
540
|
// Initialize the request
|
|
515
541
|
m.sanitizeOptions(opts);
|
|
516
542
|
m.opt = opts;
|
|
517
543
|
m.headers = opts.headers;
|
|
544
|
+
|
|
518
545
|
// log({opts}, 'constructor')
|
|
546
|
+
|
|
519
547
|
m._ended = false;
|
|
520
548
|
m._ending = false;
|
|
521
549
|
m._redirectCount = 0;
|
|
@@ -537,8 +565,31 @@ class Request extends Duplex {
|
|
|
537
565
|
}
|
|
538
566
|
};
|
|
539
567
|
|
|
540
|
-
//
|
|
568
|
+
// Proxy all other public ClientRequest methods 'getHeader'
|
|
569
|
+
for (const method of writeMethods) {
|
|
570
|
+
// @ts-ignore
|
|
571
|
+
m[method] = (a, b) => {
|
|
572
|
+
log$1(method, {a, b});
|
|
573
|
+
// @ts-ignore
|
|
574
|
+
m._currentRequest?.[method](a, b);
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// Proxy all public ClientRequest properties
|
|
579
|
+
// 'aborted', 'connection' 弃用
|
|
580
|
+
for (const property of writeProps) {
|
|
581
|
+
Object.defineProperty(m, property, {
|
|
582
|
+
get() {
|
|
583
|
+
// @ts-ignore
|
|
584
|
+
const val = m._currentRequest?.[property];
|
|
585
|
+
log$1('get property', {property});
|
|
586
|
+
return val
|
|
587
|
+
},
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
|
|
541
591
|
if (opts.stream) {
|
|
592
|
+
// 流模式
|
|
542
593
|
// 被 pipe 作为目标时触发,拷贝 src headers
|
|
543
594
|
m.on(
|
|
544
595
|
'pipe',
|
|
@@ -570,7 +621,7 @@ class Request extends Duplex {
|
|
|
570
621
|
}
|
|
571
622
|
|
|
572
623
|
// Perform the first request
|
|
573
|
-
// m.request(); // 写入数据时执行,否则 pipe时无法写入header
|
|
624
|
+
// m.request(); // 写入数据时执行,否则 pipe 时无法写入header
|
|
574
625
|
}
|
|
575
626
|
|
|
576
627
|
/**
|
|
@@ -622,25 +673,6 @@ class Request extends Duplex {
|
|
|
622
673
|
m._currentRequest = req;
|
|
623
674
|
req.redirectReq = m;
|
|
624
675
|
|
|
625
|
-
// Proxy all other public ClientRequest methods
|
|
626
|
-
for (const method of ['flushHeaders', 'setNoDelay', 'setSocketKeepAlive']) {
|
|
627
|
-
m[method] = (a, b) => {
|
|
628
|
-
log$1(method, {a, b});
|
|
629
|
-
m._currentRequest[method](a, b);
|
|
630
|
-
};
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
// Proxy all public ClientRequest properties
|
|
634
|
-
for (const property of ['aborted', 'connection', 'socket']) {
|
|
635
|
-
Object.defineProperty(m, property, {
|
|
636
|
-
get() {
|
|
637
|
-
const val = m._currentRequest[property];
|
|
638
|
-
log$1('get property', {property});
|
|
639
|
-
return val
|
|
640
|
-
},
|
|
641
|
-
});
|
|
642
|
-
}
|
|
643
|
-
|
|
644
676
|
// 启动 startTimer
|
|
645
677
|
if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
|
|
646
678
|
|
|
@@ -701,8 +733,8 @@ class Request extends Duplex {
|
|
|
701
733
|
destroy(error) {
|
|
702
734
|
const m = this;
|
|
703
735
|
if (!m._ended) m.end();
|
|
704
|
-
|
|
705
|
-
|
|
736
|
+
if (m.response) m.response.destroy();
|
|
737
|
+
if (m.responseStream) m.responseStream.destroy();
|
|
706
738
|
|
|
707
739
|
// m.clearTimeout();
|
|
708
740
|
destroyRequest(m._currentRequest, error);
|
|
@@ -721,7 +753,7 @@ class Request extends Duplex {
|
|
|
721
753
|
write(chunk, encoding, cb) {
|
|
722
754
|
const m = this;
|
|
723
755
|
|
|
724
|
-
log$1
|
|
756
|
+
log$1({data: chunk, encoding, callback: cb}, 'write');
|
|
725
757
|
|
|
726
758
|
// Writing is not allowed if end has been called
|
|
727
759
|
if (m._ending) throw new WriteAfterEndError()
|
|
@@ -948,6 +980,7 @@ class Request extends Duplex {
|
|
|
948
980
|
*/
|
|
949
981
|
processResponse(response) {
|
|
950
982
|
const m = this;
|
|
983
|
+
const {opt} = m;
|
|
951
984
|
|
|
952
985
|
// Store the redirected response
|
|
953
986
|
const {statusCode} = response;
|
|
@@ -969,18 +1002,20 @@ class Request extends Duplex {
|
|
|
969
1002
|
// If the response is not a redirect; return it as-is
|
|
970
1003
|
const {location} = response.headers;
|
|
971
1004
|
|
|
972
|
-
log$1(
|
|
1005
|
+
log$1({statusCode, headers: response.headers}, 'processResponse');
|
|
973
1006
|
|
|
974
1007
|
if (!location || m.opt.followRedirects === false || statusCode < 300 || statusCode >= 400) {
|
|
975
1008
|
// 非重定向,返回给原始回调处理
|
|
976
1009
|
response.responseUrl = m._currentUrl;
|
|
977
1010
|
response.redirects = m._redirects;
|
|
978
|
-
|
|
1011
|
+
|
|
1012
|
+
if (opt.stream) m.response = response;
|
|
1013
|
+
|
|
979
1014
|
// Be a good stream and emit end when the response is finished.
|
|
980
1015
|
// Hack to emit end on close because of a core bug that never fires end
|
|
981
1016
|
response.on('close', () => {
|
|
982
1017
|
if (!m._respended) {
|
|
983
|
-
|
|
1018
|
+
response.emit('end');
|
|
984
1019
|
}
|
|
985
1020
|
});
|
|
986
1021
|
|
|
@@ -1437,9 +1472,8 @@ function request(uri, options, callback) {
|
|
|
1437
1472
|
let R = null;
|
|
1438
1473
|
|
|
1439
1474
|
try {
|
|
1440
|
-
log({uri, options}, 'request');
|
|
1441
|
-
|
|
1442
1475
|
const {opts, cb} = init(uri, options, callback);
|
|
1476
|
+
// log({uri, options, opts}, 'request')
|
|
1443
1477
|
R = new Request(opts, cb);
|
|
1444
1478
|
} catch (e) {
|
|
1445
1479
|
log.err(e, 'request');
|
package/lib/index.js
CHANGED
|
@@ -71,11 +71,8 @@ const log = Log({
|
|
|
71
71
|
*/ function request(uri, options, callback) {
|
|
72
72
|
let R = null;
|
|
73
73
|
try {
|
|
74
|
-
log({
|
|
75
|
-
uri,
|
|
76
|
-
options
|
|
77
|
-
}, 'request');
|
|
78
74
|
const { opts, cb } = init(uri, options, callback);
|
|
75
|
+
// log({uri, options, opts}, 'request')
|
|
79
76
|
R = new Request(opts, cb);
|
|
80
77
|
} catch (e) {
|
|
81
78
|
log.err(e, 'request');
|
package/lib/request.js
CHANGED
|
@@ -30,10 +30,39 @@ const brotliOptions = {
|
|
|
30
30
|
finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
|
|
31
31
|
};
|
|
32
32
|
const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
|
|
33
|
+
// clientRequest 属性转发
|
|
34
|
+
const writeProps = [
|
|
35
|
+
'protocol',
|
|
36
|
+
'method',
|
|
37
|
+
'path',
|
|
38
|
+
'host',
|
|
39
|
+
'reusedSocket',
|
|
40
|
+
'socket',
|
|
41
|
+
'closed',
|
|
42
|
+
'destroyed',
|
|
43
|
+
'writable',
|
|
44
|
+
'writableAborted',
|
|
45
|
+
'writableEnded',
|
|
46
|
+
'writableCorked',
|
|
47
|
+
'errored',
|
|
48
|
+
'writableFinished',
|
|
49
|
+
'writableHighWaterMark',
|
|
50
|
+
'writableLength',
|
|
51
|
+
'writableNeedDrain',
|
|
52
|
+
'writableObjectMode'
|
|
53
|
+
];
|
|
54
|
+
// clientReq 方法转发
|
|
55
|
+
const writeMethods = [
|
|
56
|
+
'cork',
|
|
57
|
+
'flushHeaders',
|
|
58
|
+
'setNoDelay',
|
|
59
|
+
'setSocketKeepAlive'
|
|
60
|
+
];
|
|
33
61
|
// Create handlers that pass events from native requests
|
|
62
|
+
// 在 clientRequest 事件转发
|
|
34
63
|
const writeEvents = [
|
|
35
|
-
'abort',
|
|
36
|
-
'aborted',
|
|
64
|
+
// 'abort', // 弃用
|
|
65
|
+
// 'aborted', // 弃用
|
|
37
66
|
'close',
|
|
38
67
|
'connect',
|
|
39
68
|
'continue',
|
|
@@ -58,7 +87,7 @@ for (const ev of writeEvents)writeEventEmit[ev] = function(...args) {
|
|
|
58
87
|
m.redirectReq.emit(ev, ...args) // req 事情映射到 redirectReq 上触发
|
|
59
88
|
;
|
|
60
89
|
};
|
|
61
|
-
// stream.Readable
|
|
90
|
+
// stream.Readable,在响应流上转发读流取事件
|
|
62
91
|
// data 单独处理
|
|
63
92
|
const readEvents = [
|
|
64
93
|
'close',
|
|
@@ -96,7 +125,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
96
125
|
, /** @type {stream.Writable[]} */ this.pipedests = [] // pipe dest
|
|
97
126
|
, /** @type {*} */ this.startTimer = null;
|
|
98
127
|
const m = this;
|
|
99
|
-
// log({
|
|
128
|
+
// log({opts}, 'constructor');
|
|
100
129
|
// Initialize the request
|
|
101
130
|
m.sanitizeOptions(opts);
|
|
102
131
|
m.opt = opts;
|
|
@@ -121,8 +150,34 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
121
150
|
}));
|
|
122
151
|
}
|
|
123
152
|
};
|
|
124
|
-
//
|
|
153
|
+
// Proxy all other public ClientRequest methods 'getHeader'
|
|
154
|
+
for (const method of writeMethods){
|
|
155
|
+
// @ts-ignore
|
|
156
|
+
m[method] = (a, b)=>{
|
|
157
|
+
log(method, {
|
|
158
|
+
a,
|
|
159
|
+
b
|
|
160
|
+
});
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
m._currentRequest?.[method](a, b);
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
// Proxy all public ClientRequest properties
|
|
166
|
+
// 'aborted', 'connection' 弃用
|
|
167
|
+
for (const property of writeProps){
|
|
168
|
+
Object.defineProperty(m, property, {
|
|
169
|
+
get () {
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
const val = m._currentRequest?.[property];
|
|
172
|
+
log('get property', {
|
|
173
|
+
property
|
|
174
|
+
});
|
|
175
|
+
return val;
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
125
179
|
if (opts.stream) {
|
|
180
|
+
// 流模式
|
|
126
181
|
// 被 pipe 作为目标时触发,拷贝 src headers
|
|
127
182
|
m.on('pipe', /** @type {stream.Readable} */ (src)=>{
|
|
128
183
|
// m.ntick &&
|
|
@@ -147,7 +202,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
147
202
|
});
|
|
148
203
|
}
|
|
149
204
|
// Perform the first request
|
|
150
|
-
// m.request(); // 写入数据时执行,否则 pipe时无法写入header
|
|
205
|
+
// m.request(); // 写入数据时执行,否则 pipe 时无法写入header
|
|
151
206
|
}
|
|
152
207
|
/**
|
|
153
208
|
* Executes the next native request (initial or redirect)
|
|
@@ -192,36 +247,6 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
192
247
|
const req = httpModule.request(opt, m._onResponse);
|
|
193
248
|
m._currentRequest = req;
|
|
194
249
|
req.redirectReq = m;
|
|
195
|
-
// Proxy all other public ClientRequest methods
|
|
196
|
-
for (const method of [
|
|
197
|
-
'flushHeaders',
|
|
198
|
-
'setNoDelay',
|
|
199
|
-
'setSocketKeepAlive'
|
|
200
|
-
]){
|
|
201
|
-
m[method] = (a, b)=>{
|
|
202
|
-
log(method, {
|
|
203
|
-
a,
|
|
204
|
-
b
|
|
205
|
-
});
|
|
206
|
-
m._currentRequest[method](a, b);
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
// Proxy all public ClientRequest properties
|
|
210
|
-
for (const property of [
|
|
211
|
-
'aborted',
|
|
212
|
-
'connection',
|
|
213
|
-
'socket'
|
|
214
|
-
]){
|
|
215
|
-
Object.defineProperty(m, property, {
|
|
216
|
-
get () {
|
|
217
|
-
const val = m._currentRequest[property];
|
|
218
|
-
log('get property', {
|
|
219
|
-
property
|
|
220
|
-
});
|
|
221
|
-
return val;
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
250
|
// 启动 startTimer
|
|
226
251
|
if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
|
|
227
252
|
// 接收req事件,转发 到 redirectReq 发射
|
|
@@ -268,8 +293,8 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
268
293
|
*/ destroy(error) {
|
|
269
294
|
const m = this;
|
|
270
295
|
if (!m._ended) m.end();
|
|
271
|
-
|
|
272
|
-
|
|
296
|
+
if (m.response) m.response.destroy();
|
|
297
|
+
if (m.responseStream) m.responseStream.destroy();
|
|
273
298
|
// m.clearTimeout();
|
|
274
299
|
destroyRequest(m._currentRequest, error);
|
|
275
300
|
super.destroy(error);
|
|
@@ -284,11 +309,11 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
284
309
|
* @returns {boolean}
|
|
285
310
|
*/ write(chunk, encoding, cb) {
|
|
286
311
|
const m = this;
|
|
287
|
-
log
|
|
312
|
+
log({
|
|
288
313
|
data: chunk,
|
|
289
314
|
encoding,
|
|
290
315
|
callback: cb
|
|
291
|
-
});
|
|
316
|
+
}, 'write');
|
|
292
317
|
// Writing is not allowed if end has been called
|
|
293
318
|
if (m._ending) throw new WriteAfterEndError();
|
|
294
319
|
// ! 数据写入时连接,pipe 时可设置 header
|
|
@@ -474,6 +499,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
474
499
|
* @returns
|
|
475
500
|
*/ processResponse(response) {
|
|
476
501
|
const m = this;
|
|
502
|
+
const { opt } = m;
|
|
477
503
|
// Store the redirected response
|
|
478
504
|
const { statusCode } = response;
|
|
479
505
|
if (m.opt.trackRedirects) {
|
|
@@ -491,20 +517,20 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
491
517
|
// even if the specific status code is not understood.
|
|
492
518
|
// If the response is not a redirect; return it as-is
|
|
493
519
|
const { location } = response.headers;
|
|
494
|
-
log(
|
|
520
|
+
log({
|
|
495
521
|
statusCode,
|
|
496
522
|
headers: response.headers
|
|
497
|
-
});
|
|
523
|
+
}, 'processResponse');
|
|
498
524
|
if (!location || m.opt.followRedirects === false || statusCode < 300 || statusCode >= 400) {
|
|
499
525
|
// 非重定向,返回给原始回调处理
|
|
500
526
|
response.responseUrl = m._currentUrl;
|
|
501
527
|
response.redirects = m._redirects;
|
|
502
|
-
m.response = response;
|
|
528
|
+
if (opt.stream) m.response = response;
|
|
503
529
|
// Be a good stream and emit end when the response is finished.
|
|
504
530
|
// Hack to emit end on close because of a core bug that never fires end
|
|
505
531
|
response.on('close', ()=>{
|
|
506
532
|
if (!m._respended) {
|
|
507
|
-
|
|
533
|
+
response.emit('end');
|
|
508
534
|
}
|
|
509
535
|
});
|
|
510
536
|
response.once('end', ()=>{
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@wiajs/request",
|
|
3
3
|
"description": "Stream HTTP request client.",
|
|
4
4
|
"keywords": ["http", "simple", "util", "utility"],
|
|
5
|
-
"version": "3.0.
|
|
5
|
+
"version": "3.0.17",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"exports": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"node": ">= 6"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@wiajs/log": "^4.3.
|
|
48
|
+
"@wiajs/log": "^4.3.12",
|
|
49
49
|
"mime-types": "^2.1.35"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|