@wiajs/request 3.0.8 → 3.0.16
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/package.json +1 -1
package/dist/request.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.16
|
|
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.16
|
|
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');
|