@wiajs/request 3.0.7 → 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/README.md CHANGED
@@ -1,11 +1,23 @@
1
- # Deprecated!
1
+ # Rebirth!
2
2
 
3
- As of Feb 11th 2020, request is fully deprecated. No new changes are expected to land. In fact, none have landed for some time.
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
  [![npm package](https://nodei.co/npm/request.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/request/)
11
23
 
package/dist/request.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia request v3.0.7
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({options}, 'constructor');
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,26 +676,8 @@ 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.debug(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.debug('get property', {property});
642
- return val
643
- },
644
- });
645
- }
646
-
647
- m._currentRequest.once('socket', m.startTimer);
679
+ // 启动 startTimer
680
+ if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
648
681
 
649
682
  // 接收req事件,转发 到 redirectReq 发射
650
683
  for (const ev of writeEvents) req.on(ev, writeEventEmit[ev]);
@@ -703,8 +736,8 @@ class Request extends stream.Duplex {
703
736
  destroy(error) {
704
737
  const m = this;
705
738
  if (!m._ended) m.end();
706
- else if (m.response) m.response.destroy();
707
- else if (m.responseStream) m.responseStream.destroy();
739
+ if (m.response) m.response.destroy();
740
+ if (m.responseStream) m.responseStream.destroy();
708
741
 
709
742
  // m.clearTimeout();
710
743
  destroyRequest(m._currentRequest, error);
@@ -723,7 +756,7 @@ class Request extends stream.Duplex {
723
756
  write(chunk, encoding, cb) {
724
757
  const m = this;
725
758
 
726
- log$1.debug('write', {data: chunk, encoding, callback: cb});
759
+ log$1({data: chunk, encoding, callback: cb}, 'write');
727
760
 
728
761
  // Writing is not allowed if end has been called
729
762
  if (m._ending) throw new WriteAfterEndError()
@@ -866,6 +899,8 @@ class Request extends stream.Duplex {
866
899
  * @param {*} socket
867
900
  */
868
901
  function startTimer(socket) {
902
+ if (m.startTimer) m.startTimer = null;
903
+
869
904
  if (m._timeout) clearTimeout(m._timeout);
870
905
 
871
906
  m._timeout = setTimeout(() => {
@@ -903,7 +938,7 @@ class Request extends stream.Duplex {
903
938
 
904
939
  // Start the timer if or when the socket is opened
905
940
  if (m.socket) startTimer(m.socket);
906
- else m.startTimer = startTimer; // 未连接,先登记
941
+ else m.startTimer = startTimer; // 未连接,先登记,连接后启动
907
942
 
908
943
  // Clean up on events
909
944
  m.on('socket', destroyOnTimeout);
@@ -948,6 +983,7 @@ class Request extends stream.Duplex {
948
983
  */
949
984
  processResponse(response) {
950
985
  const m = this;
986
+ const {opt} = m;
951
987
 
952
988
  // Store the redirected response
953
989
  const {statusCode} = response;
@@ -969,18 +1005,20 @@ class Request extends stream.Duplex {
969
1005
  // If the response is not a redirect; return it as-is
970
1006
  const {location} = response.headers;
971
1007
 
972
- log$1('processResponse', {statusCode, headers: response.headers});
1008
+ log$1({statusCode, headers: response.headers}, 'processResponse');
973
1009
 
974
1010
  if (!location || m.opt.followRedirects === false || statusCode < 300 || statusCode >= 400) {
975
1011
  // 非重定向,返回给原始回调处理
976
1012
  response.responseUrl = m._currentUrl;
977
1013
  response.redirects = m._redirects;
978
- m.response = response;
1014
+
1015
+ if (opt.stream) m.response = response;
1016
+
979
1017
  // Be a good stream and emit end when the response is finished.
980
1018
  // Hack to emit end on close because of a core bug that never fires end
981
1019
  response.on('close', () => {
982
1020
  if (!m._respended) {
983
- m.response.emit('end');
1021
+ response.emit('end');
984
1022
  }
985
1023
  });
986
1024
 
@@ -1437,9 +1475,8 @@ function request(uri, options, callback) {
1437
1475
  let R = null;
1438
1476
 
1439
1477
  try {
1440
- log({uri, options}, 'request');
1441
-
1442
1478
  const {opts, cb} = init(uri, options, callback);
1479
+ // log({uri, options, opts}, 'request')
1443
1480
  R = new Request(opts, cb);
1444
1481
  } catch (e) {
1445
1482
  log.err(e, 'request');
package/dist/request.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia request v3.0.7
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({options}, 'constructor');
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,26 +673,8 @@ 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.debug(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.debug('get property', {property});
639
- return val
640
- },
641
- });
642
- }
643
-
644
- m._currentRequest.once('socket', m.startTimer);
676
+ // 启动 startTimer
677
+ if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
645
678
 
646
679
  // 接收req事件,转发 到 redirectReq 发射
647
680
  for (const ev of writeEvents) req.on(ev, writeEventEmit[ev]);
@@ -700,8 +733,8 @@ class Request extends Duplex {
700
733
  destroy(error) {
701
734
  const m = this;
702
735
  if (!m._ended) m.end();
703
- else if (m.response) m.response.destroy();
704
- else if (m.responseStream) m.responseStream.destroy();
736
+ if (m.response) m.response.destroy();
737
+ if (m.responseStream) m.responseStream.destroy();
705
738
 
706
739
  // m.clearTimeout();
707
740
  destroyRequest(m._currentRequest, error);
@@ -720,7 +753,7 @@ class Request extends Duplex {
720
753
  write(chunk, encoding, cb) {
721
754
  const m = this;
722
755
 
723
- log$1.debug('write', {data: chunk, encoding, callback: cb});
756
+ log$1({data: chunk, encoding, callback: cb}, 'write');
724
757
 
725
758
  // Writing is not allowed if end has been called
726
759
  if (m._ending) throw new WriteAfterEndError()
@@ -863,6 +896,8 @@ class Request extends Duplex {
863
896
  * @param {*} socket
864
897
  */
865
898
  function startTimer(socket) {
899
+ if (m.startTimer) m.startTimer = null;
900
+
866
901
  if (m._timeout) clearTimeout(m._timeout);
867
902
 
868
903
  m._timeout = setTimeout(() => {
@@ -900,7 +935,7 @@ class Request extends Duplex {
900
935
 
901
936
  // Start the timer if or when the socket is opened
902
937
  if (m.socket) startTimer(m.socket);
903
- else m.startTimer = startTimer; // 未连接,先登记
938
+ else m.startTimer = startTimer; // 未连接,先登记,连接后启动
904
939
 
905
940
  // Clean up on events
906
941
  m.on('socket', destroyOnTimeout);
@@ -945,6 +980,7 @@ class Request extends Duplex {
945
980
  */
946
981
  processResponse(response) {
947
982
  const m = this;
983
+ const {opt} = m;
948
984
 
949
985
  // Store the redirected response
950
986
  const {statusCode} = response;
@@ -966,18 +1002,20 @@ class Request extends Duplex {
966
1002
  // If the response is not a redirect; return it as-is
967
1003
  const {location} = response.headers;
968
1004
 
969
- log$1('processResponse', {statusCode, headers: response.headers});
1005
+ log$1({statusCode, headers: response.headers}, 'processResponse');
970
1006
 
971
1007
  if (!location || m.opt.followRedirects === false || statusCode < 300 || statusCode >= 400) {
972
1008
  // 非重定向,返回给原始回调处理
973
1009
  response.responseUrl = m._currentUrl;
974
1010
  response.redirects = m._redirects;
975
- m.response = response;
1011
+
1012
+ if (opt.stream) m.response = response;
1013
+
976
1014
  // Be a good stream and emit end when the response is finished.
977
1015
  // Hack to emit end on close because of a core bug that never fires end
978
1016
  response.on('close', () => {
979
1017
  if (!m._respended) {
980
- m.response.emit('end');
1018
+ response.emit('end');
981
1019
  }
982
1020
  });
983
1021
 
@@ -1434,9 +1472,8 @@ function request(uri, options, callback) {
1434
1472
  let R = null;
1435
1473
 
1436
1474
  try {
1437
- log({uri, options}, 'request');
1438
-
1439
1475
  const {opts, cb} = init(uri, options, callback);
1476
+ // log({uri, options, opts}, 'request')
1440
1477
  R = new Request(opts, cb);
1441
1478
  } catch (e) {
1442
1479
  log.err(e, 'request');
package/lib/request.js CHANGED
@@ -199,7 +199,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
199
199
  'setSocketKeepAlive'
200
200
  ]){
201
201
  m[method] = (a, b)=>{
202
- log.debug(method, {
202
+ log(method, {
203
203
  a,
204
204
  b
205
205
  });
@@ -215,14 +215,15 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
215
215
  Object.defineProperty(m, property, {
216
216
  get () {
217
217
  const val = m._currentRequest[property];
218
- log.debug('get property', {
218
+ log('get property', {
219
219
  property
220
220
  });
221
221
  return val;
222
222
  }
223
223
  });
224
224
  }
225
- m._currentRequest.once('socket', m.startTimer);
225
+ // 启动 startTimer
226
+ if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
226
227
  // 接收req事件,转发 到 redirectReq 发射
227
228
  for (const ev of writeEvents)req.on(ev, writeEventEmit[ev]);
228
229
  // RFC7230§5.3.1: When making a request directly to an origin server, […]
@@ -402,6 +403,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
402
403
  * Sets up a timer to trigger a timeout event
403
404
  * @param {*} socket
404
405
  */ function startTimer(socket) {
406
+ if (m.startTimer) m.startTimer = null;
405
407
  if (m._timeout) clearTimeout(m._timeout);
406
408
  m._timeout = setTimeout(()=>{
407
409
  m.emit('timeout');
@@ -432,7 +434,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
432
434
  if (callback) m.on('timeout', callback);
433
435
  // Start the timer if or when the socket is opened
434
436
  if (m.socket) startTimer(m.socket);
435
- else m.startTimer = startTimer // 未连接,先登记
437
+ else m.startTimer = startTimer // 未连接,先登记,连接后启动
436
438
  ;
437
439
  // Clean up on events
438
440
  m.on('socket', destroyOnTimeout);
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.7",
5
+ "version": "3.0.16",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "exports": {