@wiajs/request 3.0.23 → 3.0.26

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.23
2
+ * wia request v3.0.26
3
3
  * (c) 2022-2024 Sibyl Yu and contributors
4
4
  * Released under the MIT License.
5
5
  */
@@ -469,6 +469,7 @@ const log$1 = log$2.log({env: `wia:req:${log$2.name((typeof document === 'undefi
469
469
  * @prop {number} [maxRedirects=21]
470
470
  * @prop {number} [maxBodyLength = 0]
471
471
  * @prop {*} [trackRedirects]
472
+ * @prop {*} [data]
472
473
  */
473
474
 
474
475
  /** @typedef {object} ResponseExt
@@ -661,7 +662,7 @@ class Request extends stream.Duplex {
661
662
  for (const method of writeMethods) {
662
663
  // @ts-ignore
663
664
  m[method] = (a, b) => {
664
- log$1(method, {a, b});
665
+ // log(method, {a, b})
665
666
  // @ts-ignore
666
667
  m._currentRequest?.[method](a, b);
667
668
  };
@@ -765,7 +766,7 @@ class Request extends stream.Duplex {
765
766
  const httpModule = httpModules[protocol];
766
767
  if (!httpModule) throw TypeError(`Unsupported protocol: ${protocol}`)
767
768
 
768
- log$1({opt, protocol}, 'request');
769
+ // log({opt, protocol}, 'request')
769
770
  // Create the native request and set up its event handlers
770
771
  // @ts-ignore
771
772
  const req = httpModule.request(opt, m._onResponse);
@@ -848,6 +849,35 @@ class Request extends stream.Duplex {
848
849
  return this
849
850
  }
850
851
 
852
+ /**
853
+ * 发送数据
854
+ */
855
+ send() {
856
+ const m = this;
857
+ const {data} = m.opt;
858
+ // 发送数据
859
+ if (utils.isStream(data)) {
860
+
861
+ data.on('end', () => {
862
+ });
863
+
864
+ data.once(
865
+ 'error',
866
+ /** @param {*} err */ err => {
867
+ // req.destroy(err)
868
+ }
869
+ );
870
+
871
+ data.on('close', () => {
872
+ // if (!ended && !errored) {
873
+ // throw new WritebBeenAbortedError()
874
+ // }
875
+ });
876
+
877
+ data.pipe(m); // 写入数据流
878
+ } else m.end(data);
879
+ }
880
+
851
881
  /**
852
882
  * Writes buffered data to the current native request
853
883
  * 如 request 不存在,则创建连接,pipe 时可写入 header
@@ -1208,7 +1238,9 @@ class Request extends stream.Duplex {
1208
1238
 
1209
1239
  // Create the redirected request
1210
1240
  const redirectUrl = utils.resolveUrl(location, currentUrl);
1241
+
1211
1242
  log$1({redirectUrl}, 'redirecting to');
1243
+
1212
1244
  m._isRedirect = true;
1213
1245
  // 覆盖原 url 解析部分,包括 protocol、hostname、port等
1214
1246
  utils.spreadUrlObject(redirectUrl, m.opt);
@@ -1540,7 +1572,7 @@ function isSubdomain(subdomain, domain) {
1540
1572
 
1541
1573
  const log = log$2.log({env: `wia:req:${log$2.name((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('request.cjs', document.baseURI).href)))}`}); // __filename
1542
1574
 
1543
- const WritebBeenAbortedError = utils.createErrorType(
1575
+ utils.createErrorType(
1544
1576
  'ERR_STREAM_WRITE_BEEN_ABORTED',
1545
1577
  'Request stream has been aborted'
1546
1578
  )
@@ -1621,31 +1653,9 @@ function request(uri, options, callback) {
1621
1653
  try {
1622
1654
  const {opts, cb} = init(uri, options, callback);
1623
1655
  // log({uri, options, opts}, 'request')
1624
- R = new Request(opts, cb);
1625
- } catch (e) {
1626
- log.err(e, 'request');
1627
- }
1628
-
1629
- return R
1630
- }
1631
-
1632
- /**
1633
- * 执行简单的数据(支持strean)请求
1634
- * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
1635
- * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
1636
- * organize params for patch, post, put, head, del
1637
- * @param {string} verb
1638
- * @returns {Request} Duplex 流
1639
- */
1640
- function fn(verb) {
1641
- const method = verb.toUpperCase();
1642
-
1643
- // @ts-ignore
1644
- return (uri, options, callback) => {
1645
- const {opts, cb} = init(uri, options, callback);
1646
- opts.method = method;
1647
1656
  const req = new Request(opts, cb);
1648
1657
  const {data, stream} = opts;
1658
+ // 非流模式,自动发送请求,流模式通过流写入发送
1649
1659
  if (!stream) {
1650
1660
  // 发送数据
1651
1661
  if (utils.isStream(data)) {
@@ -1661,19 +1671,44 @@ function fn(verb) {
1661
1671
  'error',
1662
1672
  /** @param {*} err */ err => {
1663
1673
  errored = true;
1664
- req.destroy(err);
1674
+ // req.destroy(err)
1665
1675
  }
1666
1676
  );
1667
1677
 
1668
1678
  data.on('close', () => {
1669
1679
  if (!ended && !errored) {
1670
- throw new WritebBeenAbortedError()
1680
+ // throw new WritebBeenAbortedError()
1671
1681
  }
1672
1682
  });
1673
1683
 
1674
1684
  data.pipe(req); // 写入数据流
1675
1685
  } else req.end(data);
1676
1686
  }
1687
+
1688
+ R = req;
1689
+ } catch (e) {
1690
+ log.err(e, 'request');
1691
+ }
1692
+
1693
+ return R
1694
+ }
1695
+
1696
+ /**
1697
+ * 执行简单的数据(支持strean)请求
1698
+ * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
1699
+ * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
1700
+ * organize params for patch, post, put, head, del
1701
+ * @param {string} verb
1702
+ * @returns {Request} Duplex 流
1703
+ */
1704
+ function fn(verb) {
1705
+ const method = verb.toUpperCase();
1706
+
1707
+ // @ts-ignore
1708
+ return (uri, options, callback) => {
1709
+ const {opts, cb} = init(uri, options, callback);
1710
+ opts.method = method;
1711
+ const req = new Request(opts, cb);
1677
1712
  return req
1678
1713
  }
1679
1714
  }
package/dist/request.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia request v3.0.23
2
+ * wia request v3.0.26
3
3
  * (c) 2022-2024 Sibyl Yu and contributors
4
4
  * Released under the MIT License.
5
5
  */
@@ -466,6 +466,7 @@ const log$1 = log$2({env: `wia:req:${name(import.meta.url)}`}); // __filename
466
466
  * @prop {number} [maxRedirects=21]
467
467
  * @prop {number} [maxBodyLength = 0]
468
468
  * @prop {*} [trackRedirects]
469
+ * @prop {*} [data]
469
470
  */
470
471
 
471
472
  /** @typedef {object} ResponseExt
@@ -658,7 +659,7 @@ class Request extends Duplex {
658
659
  for (const method of writeMethods) {
659
660
  // @ts-ignore
660
661
  m[method] = (a, b) => {
661
- log$1(method, {a, b});
662
+ // log(method, {a, b})
662
663
  // @ts-ignore
663
664
  m._currentRequest?.[method](a, b);
664
665
  };
@@ -762,7 +763,7 @@ class Request extends Duplex {
762
763
  const httpModule = httpModules[protocol];
763
764
  if (!httpModule) throw TypeError(`Unsupported protocol: ${protocol}`)
764
765
 
765
- log$1({opt, protocol}, 'request');
766
+ // log({opt, protocol}, 'request')
766
767
  // Create the native request and set up its event handlers
767
768
  // @ts-ignore
768
769
  const req = httpModule.request(opt, m._onResponse);
@@ -845,6 +846,35 @@ class Request extends Duplex {
845
846
  return this
846
847
  }
847
848
 
849
+ /**
850
+ * 发送数据
851
+ */
852
+ send() {
853
+ const m = this;
854
+ const {data} = m.opt;
855
+ // 发送数据
856
+ if (utils.isStream(data)) {
857
+
858
+ data.on('end', () => {
859
+ });
860
+
861
+ data.once(
862
+ 'error',
863
+ /** @param {*} err */ err => {
864
+ // req.destroy(err)
865
+ }
866
+ );
867
+
868
+ data.on('close', () => {
869
+ // if (!ended && !errored) {
870
+ // throw new WritebBeenAbortedError()
871
+ // }
872
+ });
873
+
874
+ data.pipe(m); // 写入数据流
875
+ } else m.end(data);
876
+ }
877
+
848
878
  /**
849
879
  * Writes buffered data to the current native request
850
880
  * 如 request 不存在,则创建连接,pipe 时可写入 header
@@ -1205,7 +1235,9 @@ class Request extends Duplex {
1205
1235
 
1206
1236
  // Create the redirected request
1207
1237
  const redirectUrl = utils.resolveUrl(location, currentUrl);
1238
+
1208
1239
  log$1({redirectUrl}, 'redirecting to');
1240
+
1209
1241
  m._isRedirect = true;
1210
1242
  // 覆盖原 url 解析部分,包括 protocol、hostname、port等
1211
1243
  utils.spreadUrlObject(redirectUrl, m.opt);
@@ -1537,7 +1569,7 @@ function isSubdomain(subdomain, domain) {
1537
1569
 
1538
1570
  const log = log$2({env: `wia:req:${name(import.meta.url)}`}); // __filename
1539
1571
 
1540
- const WritebBeenAbortedError = utils.createErrorType(
1572
+ utils.createErrorType(
1541
1573
  'ERR_STREAM_WRITE_BEEN_ABORTED',
1542
1574
  'Request stream has been aborted'
1543
1575
  )
@@ -1618,31 +1650,9 @@ function request(uri, options, callback) {
1618
1650
  try {
1619
1651
  const {opts, cb} = init(uri, options, callback);
1620
1652
  // log({uri, options, opts}, 'request')
1621
- R = new Request(opts, cb);
1622
- } catch (e) {
1623
- log.err(e, 'request');
1624
- }
1625
-
1626
- return R
1627
- }
1628
-
1629
- /**
1630
- * 执行简单的数据(支持strean)请求
1631
- * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
1632
- * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
1633
- * organize params for patch, post, put, head, del
1634
- * @param {string} verb
1635
- * @returns {Request} Duplex 流
1636
- */
1637
- function fn(verb) {
1638
- const method = verb.toUpperCase();
1639
-
1640
- // @ts-ignore
1641
- return (uri, options, callback) => {
1642
- const {opts, cb} = init(uri, options, callback);
1643
- opts.method = method;
1644
1653
  const req = new Request(opts, cb);
1645
1654
  const {data, stream} = opts;
1655
+ // 非流模式,自动发送请求,流模式通过流写入发送
1646
1656
  if (!stream) {
1647
1657
  // 发送数据
1648
1658
  if (utils.isStream(data)) {
@@ -1658,19 +1668,44 @@ function fn(verb) {
1658
1668
  'error',
1659
1669
  /** @param {*} err */ err => {
1660
1670
  errored = true;
1661
- req.destroy(err);
1671
+ // req.destroy(err)
1662
1672
  }
1663
1673
  );
1664
1674
 
1665
1675
  data.on('close', () => {
1666
1676
  if (!ended && !errored) {
1667
- throw new WritebBeenAbortedError()
1677
+ // throw new WritebBeenAbortedError()
1668
1678
  }
1669
1679
  });
1670
1680
 
1671
1681
  data.pipe(req); // 写入数据流
1672
1682
  } else req.end(data);
1673
1683
  }
1684
+
1685
+ R = req;
1686
+ } catch (e) {
1687
+ log.err(e, 'request');
1688
+ }
1689
+
1690
+ return R
1691
+ }
1692
+
1693
+ /**
1694
+ * 执行简单的数据(支持strean)请求
1695
+ * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
1696
+ * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
1697
+ * organize params for patch, post, put, head, del
1698
+ * @param {string} verb
1699
+ * @returns {Request} Duplex 流
1700
+ */
1701
+ function fn(verb) {
1702
+ const method = verb.toUpperCase();
1703
+
1704
+ // @ts-ignore
1705
+ return (uri, options, callback) => {
1706
+ const {opts, cb} = init(uri, options, callback);
1707
+ opts.method = method;
1708
+ const req = new Request(opts, cb);
1674
1709
  return req
1675
1710
  }
1676
1711
  }
package/lib/index.js CHANGED
@@ -76,27 +76,9 @@ const WritebBeenAbortedError = utils.createErrorType('ERR_STREAM_WRITE_BEEN_ABOR
76
76
  try {
77
77
  const { opts, cb } = init(uri, options, callback);
78
78
  // log({uri, options, opts}, 'request')
79
- R = new Request(opts, cb);
80
- } catch (e) {
81
- log.err(e, 'request');
82
- }
83
- return R;
84
- }
85
- /**
86
- * 执行简单的数据(支持strean)请求
87
- * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
88
- * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
89
- * organize params for patch, post, put, head, del
90
- * @param {string} verb
91
- * @returns {Request} Duplex 流
92
- */ function fn(verb) {
93
- const method = verb.toUpperCase();
94
- // @ts-ignore
95
- return (uri, options, callback)=>{
96
- const { opts, cb } = init(uri, options, callback);
97
- opts.method = method;
98
79
  const req = new Request(opts, cb);
99
80
  const { data, stream } = opts;
81
+ // 非流模式,自动发送请求,流模式通过流写入发送
100
82
  if (!stream) {
101
83
  // 发送数据
102
84
  if (utils.isStream(data)) {
@@ -108,17 +90,37 @@ const WritebBeenAbortedError = utils.createErrorType('ERR_STREAM_WRITE_BEEN_ABOR
108
90
  });
109
91
  data.once('error', /** @param {*} err */ (err)=>{
110
92
  errored = true;
111
- req.destroy(err);
93
+ // req.destroy(err)
112
94
  });
113
95
  data.on('close', ()=>{
114
96
  if (!ended && !errored) {
115
- throw new WritebBeenAbortedError();
97
+ // throw new WritebBeenAbortedError()
116
98
  }
117
99
  });
118
100
  data.pipe(req) // 写入数据流
119
101
  ;
120
102
  } else req.end(data);
121
103
  }
104
+ R = req;
105
+ } catch (e) {
106
+ log.err(e, 'request');
107
+ }
108
+ return R;
109
+ }
110
+ /**
111
+ * 执行简单的数据(支持strean)请求
112
+ * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
113
+ * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
114
+ * organize params for patch, post, put, head, del
115
+ * @param {string} verb
116
+ * @returns {Request} Duplex 流
117
+ */ function fn(verb) {
118
+ const method = verb.toUpperCase();
119
+ // @ts-ignore
120
+ return (uri, options, callback)=>{
121
+ const { opts, cb } = init(uri, options, callback);
122
+ opts.method = method;
123
+ const req = new Request(opts, cb);
122
124
  return req;
123
125
  };
124
126
  }
package/lib/request.js CHANGED
@@ -35,6 +35,7 @@ const log = Log({
35
35
  * @prop {number} [maxRedirects=21]
36
36
  * @prop {number} [maxBodyLength = 0]
37
37
  * @prop {*} [trackRedirects]
38
+ * @prop {*} [data]
38
39
  */ /** @typedef {object} ResponseExt
39
40
  * @prop {*[]} [redirects]
40
41
  * @prop {string} [responseUrl]
@@ -174,10 +175,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
174
175
  for (const method of writeMethods){
175
176
  // @ts-ignore
176
177
  m[method] = (a, b)=>{
177
- log(method, {
178
- a,
179
- b
180
- });
178
+ // log(method, {a, b})
181
179
  // @ts-ignore
182
180
  m._currentRequest?.[method](a, b);
183
181
  };
@@ -259,10 +257,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
259
257
  }
260
258
  const httpModule = httpModules[protocol];
261
259
  if (!httpModule) throw TypeError(`Unsupported protocol: ${protocol}`);
262
- log({
263
- opt,
264
- protocol
265
- }, 'request');
260
+ // log({opt, protocol}, 'request')
266
261
  // Create the native request and set up its event handlers
267
262
  // @ts-ignore
268
263
  const req = httpModule.request(opt, m._onResponse);
@@ -327,6 +322,32 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
327
322
  return this;
328
323
  }
329
324
  /**
325
+ * 发送数据
326
+ */ send() {
327
+ const m = this;
328
+ const { data } = m.opt;
329
+ // 发送数据
330
+ if (utils.isStream(data)) {
331
+ // Send the request
332
+ let ended = false;
333
+ let errored = false;
334
+ data.on('end', ()=>{
335
+ ended = true;
336
+ });
337
+ data.once('error', /** @param {*} err */ (err)=>{
338
+ errored = true;
339
+ // req.destroy(err)
340
+ });
341
+ data.on('close', ()=>{
342
+ // if (!ended && !errored) {
343
+ // throw new WritebBeenAbortedError()
344
+ // }
345
+ });
346
+ data.pipe(m) // 写入数据流
347
+ ;
348
+ } else m.end(data);
349
+ }
350
+ /**
330
351
  * Writes buffered data to the current native request
331
352
  * 如 request 不存在,则创建连接,pipe 时可写入 header
332
353
  * @override - 重写父类方法
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.23",
5
+ "version": "3.0.26",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "types": "types/index.d.ts",