@wiajs/request 3.0.6 → 3.0.7

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia request v3.0.5
2
+ * wia request v3.0.7
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,29 @@ 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.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);
648
+
645
649
  // 接收req事件,转发 到 redirectReq 发射
646
650
  for (const ev of writeEvents) req.on(ev, writeEventEmit[ev]);
647
651
 
@@ -838,9 +842,8 @@ class Request extends stream.Duplex {
838
842
  return this._currentRequest?.headersSent
839
843
  }
840
844
 
841
- // Global timeout for all underlying requests
842
845
  /**
843
- *
846
+ * Global timeout for all underlying requests
844
847
  * @param {*} msecs
845
848
  * @param {*} callback
846
849
  * @returns
@@ -848,9 +851,8 @@ class Request extends stream.Duplex {
848
851
  setTimeout(msecs, callback) {
849
852
  const m = this;
850
853
 
851
- // Destroys the socket on timeout
852
854
  /**
853
- *
855
+ * Destroys the socket on timeout
854
856
  * @param {*} socket
855
857
  */
856
858
  function destroyOnTimeout(socket) {
@@ -859,19 +861,18 @@ class Request extends stream.Duplex {
859
861
  socket.addListener('timeout', socket.destroy);
860
862
  }
861
863
 
862
- // Sets up a timer to trigger a timeout event
863
864
  /**
864
- *
865
+ * Sets up a timer to trigger a timeout event
865
866
  * @param {*} socket
866
867
  */
867
868
  function startTimer(socket) {
868
- if (m._timeout) {
869
- clearTimeout(m._timeout);
870
- }
869
+ if (m._timeout) clearTimeout(m._timeout);
870
+
871
871
  m._timeout = setTimeout(() => {
872
872
  m.emit('timeout');
873
873
  clearTimer();
874
874
  }, msecs);
875
+
875
876
  destroyOnTimeout(socket);
876
877
  }
877
878
 
@@ -902,7 +903,7 @@ class Request extends stream.Duplex {
902
903
 
903
904
  // Start the timer if or when the socket is opened
904
905
  if (m.socket) startTimer(m.socket);
905
- else m._currentRequest.once('socket', startTimer);
906
+ else m.startTimer = startTimer; // 未连接,先登记
906
907
 
907
908
  // Clean up on events
908
909
  m.on('socket', destroyOnTimeout);
@@ -1436,6 +1437,8 @@ function request(uri, options, callback) {
1436
1437
  let R = null;
1437
1438
 
1438
1439
  try {
1440
+ log({uri, options}, 'request');
1441
+
1439
1442
  const {opts, cb} = init(uri, options, callback);
1440
1443
  R = new Request(opts, cb);
1441
1444
  } catch (e) {
package/dist/request.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia request v3.0.5
2
+ * wia request v3.0.7
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,29 @@ 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.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);
645
+
642
646
  // 接收req事件,转发 到 redirectReq 发射
643
647
  for (const ev of writeEvents) req.on(ev, writeEventEmit[ev]);
644
648
 
@@ -835,9 +839,8 @@ class Request extends Duplex {
835
839
  return this._currentRequest?.headersSent
836
840
  }
837
841
 
838
- // Global timeout for all underlying requests
839
842
  /**
840
- *
843
+ * Global timeout for all underlying requests
841
844
  * @param {*} msecs
842
845
  * @param {*} callback
843
846
  * @returns
@@ -845,9 +848,8 @@ class Request extends Duplex {
845
848
  setTimeout(msecs, callback) {
846
849
  const m = this;
847
850
 
848
- // Destroys the socket on timeout
849
851
  /**
850
- *
852
+ * Destroys the socket on timeout
851
853
  * @param {*} socket
852
854
  */
853
855
  function destroyOnTimeout(socket) {
@@ -856,19 +858,18 @@ class Request extends Duplex {
856
858
  socket.addListener('timeout', socket.destroy);
857
859
  }
858
860
 
859
- // Sets up a timer to trigger a timeout event
860
861
  /**
861
- *
862
+ * Sets up a timer to trigger a timeout event
862
863
  * @param {*} socket
863
864
  */
864
865
  function startTimer(socket) {
865
- if (m._timeout) {
866
- clearTimeout(m._timeout);
867
- }
866
+ if (m._timeout) clearTimeout(m._timeout);
867
+
868
868
  m._timeout = setTimeout(() => {
869
869
  m.emit('timeout');
870
870
  clearTimer();
871
871
  }, msecs);
872
+
872
873
  destroyOnTimeout(socket);
873
874
  }
874
875
 
@@ -899,7 +900,7 @@ class Request extends Duplex {
899
900
 
900
901
  // Start the timer if or when the socket is opened
901
902
  if (m.socket) startTimer(m.socket);
902
- else m._currentRequest.once('socket', startTimer);
903
+ else m.startTimer = startTimer; // 未连接,先登记
903
904
 
904
905
  // Clean up on events
905
906
  m.on('socket', destroyOnTimeout);
@@ -1433,6 +1434,8 @@ function request(uri, options, callback) {
1433
1434
  let R = null;
1434
1435
 
1435
1436
  try {
1437
+ log({uri, options}, 'request');
1438
+
1436
1439
  const {opts, cb} = init(uri, options, callback);
1437
1440
  R = new Request(opts, cb);
1438
1441
  } catch (e) {
package/lib/index.js CHANGED
@@ -71,6 +71,10 @@ 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');
74
78
  const { opts, cb } = init(uri, options, callback);
75
79
  R = new Request(opts, cb);
76
80
  } catch (e) {
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,37 @@ 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.debug(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.debug('get property', {
219
+ property
220
+ });
221
+ return val;
222
+ }
223
+ });
224
+ }
225
+ m._currentRequest.once('socket', m.startTimer);
225
226
  // 接收req事件,转发 到 redirectReq 发射
226
227
  for (const ev of writeEvents)req.on(ev, writeEventEmit[ev]);
227
228
  // RFC7230§5.3.1: When making a request directly to an origin server, […]
@@ -382,31 +383,26 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
382
383
  */ get headersSent() {
383
384
  return this._currentRequest?.headersSent;
384
385
  }
385
- // Global timeout for all underlying requests
386
386
  /**
387
- *
387
+ * Global timeout for all underlying requests
388
388
  * @param {*} msecs
389
389
  * @param {*} callback
390
390
  * @returns
391
391
  */ setTimeout(msecs, callback) {
392
392
  const m = this;
393
- // Destroys the socket on timeout
394
393
  /**
395
- *
394
+ * Destroys the socket on timeout
396
395
  * @param {*} socket
397
396
  */ function destroyOnTimeout(socket) {
398
397
  socket.setTimeout(msecs);
399
398
  socket.removeListener('timeout', socket.destroy);
400
399
  socket.addListener('timeout', socket.destroy);
401
400
  }
402
- // Sets up a timer to trigger a timeout event
403
401
  /**
404
- *
402
+ * Sets up a timer to trigger a timeout event
405
403
  * @param {*} socket
406
404
  */ function startTimer(socket) {
407
- if (m._timeout) {
408
- clearTimeout(m._timeout);
409
- }
405
+ if (m._timeout) clearTimeout(m._timeout);
410
406
  m._timeout = setTimeout(()=>{
411
407
  m.emit('timeout');
412
408
  clearTimer();
@@ -436,7 +432,8 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
436
432
  if (callback) m.on('timeout', callback);
437
433
  // Start the timer if or when the socket is opened
438
434
  if (m.socket) startTimer(m.socket);
439
- else m._currentRequest.once('socket', startTimer);
435
+ else m.startTimer = startTimer // 未连接,先登记
436
+ ;
440
437
  // Clean up on events
441
438
  m.on('socket', destroyOnTimeout);
442
439
  m.on('abort', clearTimer);
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.6",
5
+ "version": "3.0.7",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "exports": {