@wiajs/request 3.0.25 → 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.25
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
@@ -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
@@ -1542,7 +1572,7 @@ function isSubdomain(subdomain, domain) {
1542
1572
 
1543
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
1544
1574
 
1545
- const WritebBeenAbortedError = utils.createErrorType(
1575
+ utils.createErrorType(
1546
1576
  'ERR_STREAM_WRITE_BEEN_ABORTED',
1547
1577
  'Request stream has been aborted'
1548
1578
  )
@@ -1623,31 +1653,9 @@ function request(uri, options, callback) {
1623
1653
  try {
1624
1654
  const {opts, cb} = init(uri, options, callback);
1625
1655
  // log({uri, options, opts}, 'request')
1626
- R = new Request(opts, cb);
1627
- } catch (e) {
1628
- log.err(e, 'request');
1629
- }
1630
-
1631
- return R
1632
- }
1633
-
1634
- /**
1635
- * 执行简单的数据(支持strean)请求
1636
- * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
1637
- * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
1638
- * organize params for patch, post, put, head, del
1639
- * @param {string} verb
1640
- * @returns {Request} Duplex 流
1641
- */
1642
- function fn(verb) {
1643
- const method = verb.toUpperCase();
1644
-
1645
- // @ts-ignore
1646
- return (uri, options, callback) => {
1647
- const {opts, cb} = init(uri, options, callback);
1648
- opts.method = method;
1649
1656
  const req = new Request(opts, cb);
1650
1657
  const {data, stream} = opts;
1658
+ // 非流模式,自动发送请求,流模式通过流写入发送
1651
1659
  if (!stream) {
1652
1660
  // 发送数据
1653
1661
  if (utils.isStream(data)) {
@@ -1663,19 +1671,44 @@ function fn(verb) {
1663
1671
  'error',
1664
1672
  /** @param {*} err */ err => {
1665
1673
  errored = true;
1666
- req.destroy(err);
1674
+ // req.destroy(err)
1667
1675
  }
1668
1676
  );
1669
1677
 
1670
1678
  data.on('close', () => {
1671
1679
  if (!ended && !errored) {
1672
- throw new WritebBeenAbortedError()
1680
+ // throw new WritebBeenAbortedError()
1673
1681
  }
1674
1682
  });
1675
1683
 
1676
1684
  data.pipe(req); // 写入数据流
1677
1685
  } else req.end(data);
1678
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);
1679
1712
  return req
1680
1713
  }
1681
1714
  }
package/dist/request.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * wia request v3.0.25
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
@@ -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
@@ -1539,7 +1569,7 @@ function isSubdomain(subdomain, domain) {
1539
1569
 
1540
1570
  const log = log$2({env: `wia:req:${name(import.meta.url)}`}); // __filename
1541
1571
 
1542
- const WritebBeenAbortedError = utils.createErrorType(
1572
+ utils.createErrorType(
1543
1573
  'ERR_STREAM_WRITE_BEEN_ABORTED',
1544
1574
  'Request stream has been aborted'
1545
1575
  )
@@ -1620,31 +1650,9 @@ function request(uri, options, callback) {
1620
1650
  try {
1621
1651
  const {opts, cb} = init(uri, options, callback);
1622
1652
  // log({uri, options, opts}, 'request')
1623
- R = new Request(opts, cb);
1624
- } catch (e) {
1625
- log.err(e, 'request');
1626
- }
1627
-
1628
- return R
1629
- }
1630
-
1631
- /**
1632
- * 执行简单的数据(支持strean)请求
1633
- * 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
1634
- * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
1635
- * organize params for patch, post, put, head, del
1636
- * @param {string} verb
1637
- * @returns {Request} Duplex 流
1638
- */
1639
- function fn(verb) {
1640
- const method = verb.toUpperCase();
1641
-
1642
- // @ts-ignore
1643
- return (uri, options, callback) => {
1644
- const {opts, cb} = init(uri, options, callback);
1645
- opts.method = method;
1646
1653
  const req = new Request(opts, cb);
1647
1654
  const {data, stream} = opts;
1655
+ // 非流模式,自动发送请求,流模式通过流写入发送
1648
1656
  if (!stream) {
1649
1657
  // 发送数据
1650
1658
  if (utils.isStream(data)) {
@@ -1660,19 +1668,44 @@ function fn(verb) {
1660
1668
  'error',
1661
1669
  /** @param {*} err */ err => {
1662
1670
  errored = true;
1663
- req.destroy(err);
1671
+ // req.destroy(err)
1664
1672
  }
1665
1673
  );
1666
1674
 
1667
1675
  data.on('close', () => {
1668
1676
  if (!ended && !errored) {
1669
- throw new WritebBeenAbortedError()
1677
+ // throw new WritebBeenAbortedError()
1670
1678
  }
1671
1679
  });
1672
1680
 
1673
1681
  data.pipe(req); // 写入数据流
1674
1682
  } else req.end(data);
1675
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);
1676
1709
  return req
1677
1710
  }
1678
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]
@@ -321,6 +322,32 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
321
322
  return this;
322
323
  }
323
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
+ /**
324
351
  * Writes buffered data to the current native request
325
352
  * 如 request 不存在,则创建连接,pipe 时可写入 header
326
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.25",
5
+ "version": "3.0.26",
6
6
  "type": "module",
7
7
  "main": "index.js",
8
8
  "types": "types/index.d.ts",