@wiajs/request 3.0.26 → 3.0.28
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 +45 -15
- package/dist/request.mjs +45 -15
- package/lib/index.js +47 -15
- package/package.json +1 -1
package/dist/request.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.28
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -1596,37 +1596,45 @@ utils.createErrorType(
|
|
|
1596
1596
|
|
|
1597
1597
|
/**
|
|
1598
1598
|
* 初始化参数
|
|
1599
|
-
* @param {*} uri
|
|
1600
|
-
* @param {*}
|
|
1601
|
-
* @param {*}
|
|
1599
|
+
* @param {*} uri/opts
|
|
1600
|
+
* @param {*} opts/cb
|
|
1601
|
+
* @param {*} cb/null
|
|
1602
1602
|
* @returns
|
|
1603
1603
|
*/
|
|
1604
|
-
function init(uri,
|
|
1604
|
+
function init(uri, opts, cb) {
|
|
1605
1605
|
let R;
|
|
1606
1606
|
try {
|
|
1607
1607
|
// Parse parameters, ensuring that input is an object
|
|
1608
1608
|
if (utils.isURL(uri)) uri = utils.spreadUrlObject(uri);
|
|
1609
1609
|
else if (utils.isString(uri)) uri = utils.spreadUrlObject(utils.parseUrl(uri));
|
|
1610
1610
|
else {
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1611
|
+
cb = opts;
|
|
1612
|
+
opts = uri;
|
|
1613
|
+
const {url} = opts;
|
|
1614
|
+
if (url) {
|
|
1615
|
+
delete opts.url;
|
|
1616
|
+
if (utils.isURL(url)) uri = utils.spreadUrlObject(url);
|
|
1617
|
+
else if (utils.isString(url)) uri = utils.spreadUrlObject(utils.parseUrl(url));
|
|
1618
|
+
} else {
|
|
1619
|
+
opts = utils.validateUrl(uri);
|
|
1620
|
+
uri = {};
|
|
1621
|
+
}
|
|
1614
1622
|
}
|
|
1615
1623
|
|
|
1616
|
-
if (utils.isFunction(
|
|
1617
|
-
|
|
1618
|
-
|
|
1624
|
+
if (utils.isFunction(opts)) {
|
|
1625
|
+
cb = opts;
|
|
1626
|
+
opts = null;
|
|
1619
1627
|
}
|
|
1620
1628
|
|
|
1621
1629
|
// copy options
|
|
1622
|
-
|
|
1630
|
+
opts = {
|
|
1623
1631
|
...uri,
|
|
1624
|
-
...
|
|
1632
|
+
...opts,
|
|
1625
1633
|
};
|
|
1626
1634
|
|
|
1627
|
-
if (!utils.isString(
|
|
1635
|
+
if (!utils.isString(opts.host) && !utils.isString(opts.hostname)) opts.hostname = '::1';
|
|
1628
1636
|
|
|
1629
|
-
R = {opts:
|
|
1637
|
+
R = {opts: opts, cb: cb};
|
|
1630
1638
|
// log({R}, 'init')
|
|
1631
1639
|
} catch (e) {
|
|
1632
1640
|
log.err(e, 'init');
|
|
@@ -1709,6 +1717,28 @@ function fn(verb) {
|
|
|
1709
1717
|
const {opts, cb} = init(uri, options, callback);
|
|
1710
1718
|
opts.method = method;
|
|
1711
1719
|
const req = new Request(opts, cb);
|
|
1720
|
+
const {data, stream} = opts;
|
|
1721
|
+
// 非流模式,自动发送请求,流模式通过流写入发送
|
|
1722
|
+
if (!stream) {
|
|
1723
|
+
// 发送数据
|
|
1724
|
+
if (utils.isStream(data)) {
|
|
1725
|
+
|
|
1726
|
+
data.on('end', () => {
|
|
1727
|
+
});
|
|
1728
|
+
|
|
1729
|
+
data.once(
|
|
1730
|
+
'error',
|
|
1731
|
+
/** @param {*} err */ err => {
|
|
1732
|
+
// req.destroy(err)
|
|
1733
|
+
}
|
|
1734
|
+
);
|
|
1735
|
+
|
|
1736
|
+
data.on('close', () => {
|
|
1737
|
+
});
|
|
1738
|
+
|
|
1739
|
+
data.pipe(req); // 写入数据流
|
|
1740
|
+
} else req.end(data);
|
|
1741
|
+
}
|
|
1712
1742
|
return req
|
|
1713
1743
|
}
|
|
1714
1744
|
}
|
package/dist/request.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.28
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -1593,37 +1593,45 @@ utils.createErrorType(
|
|
|
1593
1593
|
|
|
1594
1594
|
/**
|
|
1595
1595
|
* 初始化参数
|
|
1596
|
-
* @param {*} uri
|
|
1597
|
-
* @param {*}
|
|
1598
|
-
* @param {*}
|
|
1596
|
+
* @param {*} uri/opts
|
|
1597
|
+
* @param {*} opts/cb
|
|
1598
|
+
* @param {*} cb/null
|
|
1599
1599
|
* @returns
|
|
1600
1600
|
*/
|
|
1601
|
-
function init(uri,
|
|
1601
|
+
function init(uri, opts, cb) {
|
|
1602
1602
|
let R;
|
|
1603
1603
|
try {
|
|
1604
1604
|
// Parse parameters, ensuring that input is an object
|
|
1605
1605
|
if (utils.isURL(uri)) uri = utils.spreadUrlObject(uri);
|
|
1606
1606
|
else if (utils.isString(uri)) uri = utils.spreadUrlObject(utils.parseUrl(uri));
|
|
1607
1607
|
else {
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1608
|
+
cb = opts;
|
|
1609
|
+
opts = uri;
|
|
1610
|
+
const {url} = opts;
|
|
1611
|
+
if (url) {
|
|
1612
|
+
delete opts.url;
|
|
1613
|
+
if (utils.isURL(url)) uri = utils.spreadUrlObject(url);
|
|
1614
|
+
else if (utils.isString(url)) uri = utils.spreadUrlObject(utils.parseUrl(url));
|
|
1615
|
+
} else {
|
|
1616
|
+
opts = utils.validateUrl(uri);
|
|
1617
|
+
uri = {};
|
|
1618
|
+
}
|
|
1611
1619
|
}
|
|
1612
1620
|
|
|
1613
|
-
if (utils.isFunction(
|
|
1614
|
-
|
|
1615
|
-
|
|
1621
|
+
if (utils.isFunction(opts)) {
|
|
1622
|
+
cb = opts;
|
|
1623
|
+
opts = null;
|
|
1616
1624
|
}
|
|
1617
1625
|
|
|
1618
1626
|
// copy options
|
|
1619
|
-
|
|
1627
|
+
opts = {
|
|
1620
1628
|
...uri,
|
|
1621
|
-
...
|
|
1629
|
+
...opts,
|
|
1622
1630
|
};
|
|
1623
1631
|
|
|
1624
|
-
if (!utils.isString(
|
|
1632
|
+
if (!utils.isString(opts.host) && !utils.isString(opts.hostname)) opts.hostname = '::1';
|
|
1625
1633
|
|
|
1626
|
-
R = {opts:
|
|
1634
|
+
R = {opts: opts, cb: cb};
|
|
1627
1635
|
// log({R}, 'init')
|
|
1628
1636
|
} catch (e) {
|
|
1629
1637
|
log.err(e, 'init');
|
|
@@ -1706,6 +1714,28 @@ function fn(verb) {
|
|
|
1706
1714
|
const {opts, cb} = init(uri, options, callback);
|
|
1707
1715
|
opts.method = method;
|
|
1708
1716
|
const req = new Request(opts, cb);
|
|
1717
|
+
const {data, stream} = opts;
|
|
1718
|
+
// 非流模式,自动发送请求,流模式通过流写入发送
|
|
1719
|
+
if (!stream) {
|
|
1720
|
+
// 发送数据
|
|
1721
|
+
if (utils.isStream(data)) {
|
|
1722
|
+
|
|
1723
|
+
data.on('end', () => {
|
|
1724
|
+
});
|
|
1725
|
+
|
|
1726
|
+
data.once(
|
|
1727
|
+
'error',
|
|
1728
|
+
/** @param {*} err */ err => {
|
|
1729
|
+
// req.destroy(err)
|
|
1730
|
+
}
|
|
1731
|
+
);
|
|
1732
|
+
|
|
1733
|
+
data.on('close', () => {
|
|
1734
|
+
});
|
|
1735
|
+
|
|
1736
|
+
data.pipe(req); // 写入数据流
|
|
1737
|
+
} else req.end(data);
|
|
1738
|
+
}
|
|
1709
1739
|
return req
|
|
1710
1740
|
}
|
|
1711
1741
|
}
|
package/lib/index.js
CHANGED
|
@@ -25,34 +25,42 @@ const WritebBeenAbortedError = utils.createErrorType('ERR_STREAM_WRITE_BEEN_ABOR
|
|
|
25
25
|
* 支持隧道及非隧道、http(s)代理
|
|
26
26
|
*/ /**
|
|
27
27
|
* 初始化参数
|
|
28
|
-
* @param {*} uri
|
|
29
|
-
* @param {*}
|
|
30
|
-
* @param {*}
|
|
28
|
+
* @param {*} uri/opts
|
|
29
|
+
* @param {*} opts/cb
|
|
30
|
+
* @param {*} cb/null
|
|
31
31
|
* @returns
|
|
32
|
-
*/ function init(uri,
|
|
32
|
+
*/ function init(uri, opts, cb) {
|
|
33
33
|
let R;
|
|
34
34
|
try {
|
|
35
35
|
// Parse parameters, ensuring that input is an object
|
|
36
36
|
if (utils.isURL(uri)) uri = utils.spreadUrlObject(uri);
|
|
37
37
|
else if (utils.isString(uri)) uri = utils.spreadUrlObject(utils.parseUrl(uri));
|
|
38
38
|
else {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
cb = opts;
|
|
40
|
+
opts = uri;
|
|
41
|
+
const { url } = opts;
|
|
42
|
+
if (url) {
|
|
43
|
+
delete opts.url;
|
|
44
|
+
if (utils.isURL(url)) uri = utils.spreadUrlObject(url);
|
|
45
|
+
else if (utils.isString(url)) uri = utils.spreadUrlObject(utils.parseUrl(url));
|
|
46
|
+
} else {
|
|
47
|
+
opts = utils.validateUrl(uri);
|
|
48
|
+
uri = {};
|
|
49
|
+
}
|
|
42
50
|
}
|
|
43
|
-
if (utils.isFunction(
|
|
44
|
-
|
|
45
|
-
|
|
51
|
+
if (utils.isFunction(opts)) {
|
|
52
|
+
cb = opts;
|
|
53
|
+
opts = null;
|
|
46
54
|
}
|
|
47
55
|
// copy options
|
|
48
|
-
|
|
56
|
+
opts = {
|
|
49
57
|
...uri,
|
|
50
|
-
...
|
|
58
|
+
...opts
|
|
51
59
|
};
|
|
52
|
-
if (!utils.isString(
|
|
60
|
+
if (!utils.isString(opts.host) && !utils.isString(opts.hostname)) opts.hostname = '::1';
|
|
53
61
|
R = {
|
|
54
|
-
opts:
|
|
55
|
-
cb:
|
|
62
|
+
opts: opts,
|
|
63
|
+
cb: cb
|
|
56
64
|
};
|
|
57
65
|
// log({R}, 'init')
|
|
58
66
|
} catch (e) {
|
|
@@ -121,6 +129,30 @@ const WritebBeenAbortedError = utils.createErrorType('ERR_STREAM_WRITE_BEEN_ABOR
|
|
|
121
129
|
const { opts, cb } = init(uri, options, callback);
|
|
122
130
|
opts.method = method;
|
|
123
131
|
const req = new Request(opts, cb);
|
|
132
|
+
const { data, stream } = opts;
|
|
133
|
+
// 非流模式,自动发送请求,流模式通过流写入发送
|
|
134
|
+
if (!stream) {
|
|
135
|
+
// 发送数据
|
|
136
|
+
if (utils.isStream(data)) {
|
|
137
|
+
// Send the request
|
|
138
|
+
let ended = false;
|
|
139
|
+
let errored = false;
|
|
140
|
+
data.on('end', ()=>{
|
|
141
|
+
ended = true;
|
|
142
|
+
});
|
|
143
|
+
data.once('error', /** @param {*} err */ (err)=>{
|
|
144
|
+
errored = true;
|
|
145
|
+
// req.destroy(err)
|
|
146
|
+
});
|
|
147
|
+
data.on('close', ()=>{
|
|
148
|
+
if (!ended && !errored) {
|
|
149
|
+
// throw new WritebBeenAbortedError()
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
data.pipe(req) // 写入数据流
|
|
153
|
+
;
|
|
154
|
+
} else req.end(data);
|
|
155
|
+
}
|
|
124
156
|
return req;
|
|
125
157
|
};
|
|
126
158
|
}
|
package/package.json
CHANGED