@wiajs/request 3.0.6 → 3.0.8
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/README.md +15 -3
- package/dist/request.cjs +37 -31
- package/dist/request.mjs +37 -31
- package/lib/index.js +4 -0
- package/lib/request.js +40 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Rebirth!
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `request` library is undoubtedly one of the most influential libraries in Node.js. It was first released in 2011 as one of the earliest libraries in Node.js and npm, and it reached 25.7k stars on GitHub by 2019. Unfortunately, the author announced its deprecation in 2019. The reason was not due to any functional issues; in fact, its stream functionality is still among the best. Neither of its successors, Axios nor Got, comes close to request in this regard. The author believed the code was outdated and didn’t have the energy to rewrite the entire library using ES6.
|
|
4
|
+
|
|
5
|
+
`@wiajs/request` has forked `request` and rewritten the entire library using `ES6` and the latest Node.js `http`, `https`, and `stream.Duplex`. The focus was to retain its powerful stream capabilities.
|
|
6
|
+
|
|
7
|
+
How does it compare to the currently most popular library, `Axios`?
|
|
8
|
+
|
|
9
|
+
`Axios` lacks stream functionality, and the goal of rewriting request was precisely to fork `Axios` and replace Axios's transport layer to make up for this shortcoming.
|
|
10
|
+
|
|
11
|
+
- `@wiajs/request` (forked from `request`) provides powerful stream capabilities.
|
|
12
|
+
- `@wiajs/req` (forked from `axios`) provides powerful data handling capabilities.
|
|
13
|
+
- `@wiajs/request`, as the default transport for `@wiajs/req`, provides both powerful data handling and stream capabilities, as well as perfect proxy support.
|
|
14
|
+
- `@wiajs/request` is the rebirth version of request.
|
|
15
|
+
- `@wiajs/req` is a better version of axios.
|
|
4
16
|
|
|
5
17
|
For more information about why request is deprecated and possible alternatives refer to
|
|
6
18
|
[this issue](https://github.com/request/request/issues/3142).
|
|
7
19
|
|
|
8
|
-
# Request - Simplified HTTP client
|
|
20
|
+
# @wiajs/Request - Simplified HTTP client
|
|
9
21
|
|
|
10
22
|
[](https://nodei.co/npm/request/)
|
|
11
23
|
|
package/dist/request.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.8
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -500,6 +500,8 @@ class Request extends stream.Duplex {
|
|
|
500
500
|
pipesrc = null // 被 pipe 时的 src stream
|
|
501
501
|
/** @type {stream.Writable[]} */
|
|
502
502
|
pipedests = [] // pipe dest
|
|
503
|
+
/** @type {*} */
|
|
504
|
+
startTimer = null
|
|
503
505
|
|
|
504
506
|
/**
|
|
505
507
|
* responseCallback 原消息处理回调
|
|
@@ -538,25 +540,6 @@ class Request extends stream.Duplex {
|
|
|
538
540
|
}
|
|
539
541
|
};
|
|
540
542
|
|
|
541
|
-
// Proxy all other public ClientRequest methods
|
|
542
|
-
for (const method of ['flushHeaders', 'setNoDelay', 'setSocketKeepAlive']) {
|
|
543
|
-
m[method] = (a, b) => {
|
|
544
|
-
log$1.debug(method, {a, b});
|
|
545
|
-
m._currentRequest[method](a, b);
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
// Proxy all public ClientRequest properties
|
|
550
|
-
for (const property of ['aborted', 'connection', 'socket']) {
|
|
551
|
-
Object.defineProperty(m, property, {
|
|
552
|
-
get() {
|
|
553
|
-
const val = m._currentRequest[property];
|
|
554
|
-
log$1.debug('get property', {property});
|
|
555
|
-
return val
|
|
556
|
-
},
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
|
|
560
543
|
// 流模式
|
|
561
544
|
if (opts.stream) {
|
|
562
545
|
// 被 pipe 作为目标时触发,拷贝 src headers
|
|
@@ -640,8 +623,30 @@ class Request extends stream.Duplex {
|
|
|
640
623
|
// Create the native request and set up its event handlers
|
|
641
624
|
const req = httpModule.request(opt, m._onResponse);
|
|
642
625
|
m._currentRequest = req;
|
|
643
|
-
|
|
644
626
|
req.redirectReq = m;
|
|
627
|
+
|
|
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
|
+
// 启动 startTimer
|
|
648
|
+
if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
|
|
649
|
+
|
|
645
650
|
// 接收req事件,转发 到 redirectReq 发射
|
|
646
651
|
for (const ev of writeEvents) req.on(ev, writeEventEmit[ev]);
|
|
647
652
|
|
|
@@ -838,9 +843,8 @@ class Request extends stream.Duplex {
|
|
|
838
843
|
return this._currentRequest?.headersSent
|
|
839
844
|
}
|
|
840
845
|
|
|
841
|
-
// Global timeout for all underlying requests
|
|
842
846
|
/**
|
|
843
|
-
*
|
|
847
|
+
* Global timeout for all underlying requests
|
|
844
848
|
* @param {*} msecs
|
|
845
849
|
* @param {*} callback
|
|
846
850
|
* @returns
|
|
@@ -848,9 +852,8 @@ class Request extends stream.Duplex {
|
|
|
848
852
|
setTimeout(msecs, callback) {
|
|
849
853
|
const m = this;
|
|
850
854
|
|
|
851
|
-
// Destroys the socket on timeout
|
|
852
855
|
/**
|
|
853
|
-
*
|
|
856
|
+
* Destroys the socket on timeout
|
|
854
857
|
* @param {*} socket
|
|
855
858
|
*/
|
|
856
859
|
function destroyOnTimeout(socket) {
|
|
@@ -859,19 +862,20 @@ class Request extends stream.Duplex {
|
|
|
859
862
|
socket.addListener('timeout', socket.destroy);
|
|
860
863
|
}
|
|
861
864
|
|
|
862
|
-
// Sets up a timer to trigger a timeout event
|
|
863
865
|
/**
|
|
864
|
-
*
|
|
866
|
+
* Sets up a timer to trigger a timeout event
|
|
865
867
|
* @param {*} socket
|
|
866
868
|
*/
|
|
867
869
|
function startTimer(socket) {
|
|
868
|
-
if (m.
|
|
869
|
-
|
|
870
|
-
|
|
870
|
+
if (m.startTimer) m.startTimer = null;
|
|
871
|
+
|
|
872
|
+
if (m._timeout) clearTimeout(m._timeout);
|
|
873
|
+
|
|
871
874
|
m._timeout = setTimeout(() => {
|
|
872
875
|
m.emit('timeout');
|
|
873
876
|
clearTimer();
|
|
874
877
|
}, msecs);
|
|
878
|
+
|
|
875
879
|
destroyOnTimeout(socket);
|
|
876
880
|
}
|
|
877
881
|
|
|
@@ -902,7 +906,7 @@ class Request extends stream.Duplex {
|
|
|
902
906
|
|
|
903
907
|
// Start the timer if or when the socket is opened
|
|
904
908
|
if (m.socket) startTimer(m.socket);
|
|
905
|
-
else m.
|
|
909
|
+
else m.startTimer = startTimer; // 未连接,先登记,连接后启动
|
|
906
910
|
|
|
907
911
|
// Clean up on events
|
|
908
912
|
m.on('socket', destroyOnTimeout);
|
|
@@ -1436,6 +1440,8 @@ function request(uri, options, callback) {
|
|
|
1436
1440
|
let R = null;
|
|
1437
1441
|
|
|
1438
1442
|
try {
|
|
1443
|
+
log({uri, options}, 'request');
|
|
1444
|
+
|
|
1439
1445
|
const {opts, cb} = init(uri, options, callback);
|
|
1440
1446
|
R = new Request(opts, cb);
|
|
1441
1447
|
} catch (e) {
|
package/dist/request.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.8
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -497,6 +497,8 @@ class Request extends Duplex {
|
|
|
497
497
|
pipesrc = null // 被 pipe 时的 src stream
|
|
498
498
|
/** @type {stream.Writable[]} */
|
|
499
499
|
pipedests = [] // pipe dest
|
|
500
|
+
/** @type {*} */
|
|
501
|
+
startTimer = null
|
|
500
502
|
|
|
501
503
|
/**
|
|
502
504
|
* responseCallback 原消息处理回调
|
|
@@ -535,25 +537,6 @@ class Request extends Duplex {
|
|
|
535
537
|
}
|
|
536
538
|
};
|
|
537
539
|
|
|
538
|
-
// Proxy all other public ClientRequest methods
|
|
539
|
-
for (const method of ['flushHeaders', 'setNoDelay', 'setSocketKeepAlive']) {
|
|
540
|
-
m[method] = (a, b) => {
|
|
541
|
-
log$1.debug(method, {a, b});
|
|
542
|
-
m._currentRequest[method](a, b);
|
|
543
|
-
};
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// Proxy all public ClientRequest properties
|
|
547
|
-
for (const property of ['aborted', 'connection', 'socket']) {
|
|
548
|
-
Object.defineProperty(m, property, {
|
|
549
|
-
get() {
|
|
550
|
-
const val = m._currentRequest[property];
|
|
551
|
-
log$1.debug('get property', {property});
|
|
552
|
-
return val
|
|
553
|
-
},
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
|
|
557
540
|
// 流模式
|
|
558
541
|
if (opts.stream) {
|
|
559
542
|
// 被 pipe 作为目标时触发,拷贝 src headers
|
|
@@ -637,8 +620,30 @@ class Request extends Duplex {
|
|
|
637
620
|
// Create the native request and set up its event handlers
|
|
638
621
|
const req = httpModule.request(opt, m._onResponse);
|
|
639
622
|
m._currentRequest = req;
|
|
640
|
-
|
|
641
623
|
req.redirectReq = m;
|
|
624
|
+
|
|
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
|
+
// 启动 startTimer
|
|
645
|
+
if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
|
|
646
|
+
|
|
642
647
|
// 接收req事件,转发 到 redirectReq 发射
|
|
643
648
|
for (const ev of writeEvents) req.on(ev, writeEventEmit[ev]);
|
|
644
649
|
|
|
@@ -835,9 +840,8 @@ class Request extends Duplex {
|
|
|
835
840
|
return this._currentRequest?.headersSent
|
|
836
841
|
}
|
|
837
842
|
|
|
838
|
-
// Global timeout for all underlying requests
|
|
839
843
|
/**
|
|
840
|
-
*
|
|
844
|
+
* Global timeout for all underlying requests
|
|
841
845
|
* @param {*} msecs
|
|
842
846
|
* @param {*} callback
|
|
843
847
|
* @returns
|
|
@@ -845,9 +849,8 @@ class Request extends Duplex {
|
|
|
845
849
|
setTimeout(msecs, callback) {
|
|
846
850
|
const m = this;
|
|
847
851
|
|
|
848
|
-
// Destroys the socket on timeout
|
|
849
852
|
/**
|
|
850
|
-
*
|
|
853
|
+
* Destroys the socket on timeout
|
|
851
854
|
* @param {*} socket
|
|
852
855
|
*/
|
|
853
856
|
function destroyOnTimeout(socket) {
|
|
@@ -856,19 +859,20 @@ class Request extends Duplex {
|
|
|
856
859
|
socket.addListener('timeout', socket.destroy);
|
|
857
860
|
}
|
|
858
861
|
|
|
859
|
-
// Sets up a timer to trigger a timeout event
|
|
860
862
|
/**
|
|
861
|
-
*
|
|
863
|
+
* Sets up a timer to trigger a timeout event
|
|
862
864
|
* @param {*} socket
|
|
863
865
|
*/
|
|
864
866
|
function startTimer(socket) {
|
|
865
|
-
if (m.
|
|
866
|
-
|
|
867
|
-
|
|
867
|
+
if (m.startTimer) m.startTimer = null;
|
|
868
|
+
|
|
869
|
+
if (m._timeout) clearTimeout(m._timeout);
|
|
870
|
+
|
|
868
871
|
m._timeout = setTimeout(() => {
|
|
869
872
|
m.emit('timeout');
|
|
870
873
|
clearTimer();
|
|
871
874
|
}, msecs);
|
|
875
|
+
|
|
872
876
|
destroyOnTimeout(socket);
|
|
873
877
|
}
|
|
874
878
|
|
|
@@ -899,7 +903,7 @@ class Request extends Duplex {
|
|
|
899
903
|
|
|
900
904
|
// Start the timer if or when the socket is opened
|
|
901
905
|
if (m.socket) startTimer(m.socket);
|
|
902
|
-
else m.
|
|
906
|
+
else m.startTimer = startTimer; // 未连接,先登记,连接后启动
|
|
903
907
|
|
|
904
908
|
// Clean up on events
|
|
905
909
|
m.on('socket', destroyOnTimeout);
|
|
@@ -1433,6 +1437,8 @@ function request(uri, options, callback) {
|
|
|
1433
1437
|
let R = null;
|
|
1434
1438
|
|
|
1435
1439
|
try {
|
|
1440
|
+
log({uri, options}, 'request');
|
|
1441
|
+
|
|
1436
1442
|
const {opts, cb} = init(uri, options, callback);
|
|
1437
1443
|
R = new Request(opts, cb);
|
|
1438
1444
|
} catch (e) {
|
package/lib/index.js
CHANGED
package/lib/request.js
CHANGED
|
@@ -94,7 +94,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
94
94
|
*/ constructor(opts, resCallback){
|
|
95
95
|
super(), this._timeout = 0, /** @type {*} */ this.socket = null, /** @type {http.ClientRequest} */ this._currentRequest = null, /** @type {stream.Readable} */ this.response = null, /** @type {stream.Readable} */ this.responseStream = null, this.timing = false, this.responseStarted = false, this.responseStartTime = 0, this._destdata = false, this._paused = false, this._respended = false, /** @type {stream.Readable} */ this.pipesrc = null // 被 pipe 时的 src stream
|
|
96
96
|
, /** @type {stream.Writable[]} */ this.pipedests = [] // pipe dest
|
|
97
|
-
;
|
|
97
|
+
, /** @type {*} */ this.startTimer = null;
|
|
98
98
|
const m = this;
|
|
99
99
|
// log({options}, 'constructor');
|
|
100
100
|
// Initialize the request
|
|
@@ -121,36 +121,6 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
121
121
|
}));
|
|
122
122
|
}
|
|
123
123
|
};
|
|
124
|
-
// Proxy all other public ClientRequest methods
|
|
125
|
-
for (const method of [
|
|
126
|
-
'flushHeaders',
|
|
127
|
-
'setNoDelay',
|
|
128
|
-
'setSocketKeepAlive'
|
|
129
|
-
]){
|
|
130
|
-
m[method] = (a, b)=>{
|
|
131
|
-
log.debug(method, {
|
|
132
|
-
a,
|
|
133
|
-
b
|
|
134
|
-
});
|
|
135
|
-
m._currentRequest[method](a, b);
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
// Proxy all public ClientRequest properties
|
|
139
|
-
for (const property of [
|
|
140
|
-
'aborted',
|
|
141
|
-
'connection',
|
|
142
|
-
'socket'
|
|
143
|
-
]){
|
|
144
|
-
Object.defineProperty(m, property, {
|
|
145
|
-
get () {
|
|
146
|
-
const val = m._currentRequest[property];
|
|
147
|
-
log.debug('get property', {
|
|
148
|
-
property
|
|
149
|
-
});
|
|
150
|
-
return val;
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
124
|
// 流模式
|
|
155
125
|
if (opts.stream) {
|
|
156
126
|
// 被 pipe 作为目标时触发,拷贝 src headers
|
|
@@ -222,6 +192,38 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
222
192
|
const req = httpModule.request(opt, m._onResponse);
|
|
223
193
|
m._currentRequest = req;
|
|
224
194
|
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
|
+
// 启动 startTimer
|
|
226
|
+
if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
|
|
225
227
|
// 接收req事件,转发 到 redirectReq 发射
|
|
226
228
|
for (const ev of writeEvents)req.on(ev, writeEventEmit[ev]);
|
|
227
229
|
// RFC7230§5.3.1: When making a request directly to an origin server, […]
|
|
@@ -382,31 +384,27 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
382
384
|
*/ get headersSent() {
|
|
383
385
|
return this._currentRequest?.headersSent;
|
|
384
386
|
}
|
|
385
|
-
// Global timeout for all underlying requests
|
|
386
387
|
/**
|
|
387
|
-
*
|
|
388
|
+
* Global timeout for all underlying requests
|
|
388
389
|
* @param {*} msecs
|
|
389
390
|
* @param {*} callback
|
|
390
391
|
* @returns
|
|
391
392
|
*/ setTimeout(msecs, callback) {
|
|
392
393
|
const m = this;
|
|
393
|
-
// Destroys the socket on timeout
|
|
394
394
|
/**
|
|
395
|
-
*
|
|
395
|
+
* Destroys the socket on timeout
|
|
396
396
|
* @param {*} socket
|
|
397
397
|
*/ function destroyOnTimeout(socket) {
|
|
398
398
|
socket.setTimeout(msecs);
|
|
399
399
|
socket.removeListener('timeout', socket.destroy);
|
|
400
400
|
socket.addListener('timeout', socket.destroy);
|
|
401
401
|
}
|
|
402
|
-
// Sets up a timer to trigger a timeout event
|
|
403
402
|
/**
|
|
404
|
-
*
|
|
403
|
+
* Sets up a timer to trigger a timeout event
|
|
405
404
|
* @param {*} socket
|
|
406
405
|
*/ function startTimer(socket) {
|
|
407
|
-
if (m.
|
|
408
|
-
|
|
409
|
-
}
|
|
406
|
+
if (m.startTimer) m.startTimer = null;
|
|
407
|
+
if (m._timeout) clearTimeout(m._timeout);
|
|
410
408
|
m._timeout = setTimeout(()=>{
|
|
411
409
|
m.emit('timeout');
|
|
412
410
|
clearTimer();
|
|
@@ -436,7 +434,8 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
436
434
|
if (callback) m.on('timeout', callback);
|
|
437
435
|
// Start the timer if or when the socket is opened
|
|
438
436
|
if (m.socket) startTimer(m.socket);
|
|
439
|
-
else m.
|
|
437
|
+
else m.startTimer = startTimer // 未连接,先登记,连接后启动
|
|
438
|
+
;
|
|
440
439
|
// Clean up on events
|
|
441
440
|
m.on('socket', destroyOnTimeout);
|
|
442
441
|
m.on('abort', clearTimer);
|