@wiajs/req 1.7.7 → 1.7.11
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/node/req.cjs +663 -755
- package/dist/node/req.mjs +97 -98
- package/dist/req.js +22201 -0
- package/dist/req.min.js +6 -0
- package/dist/web/req.cjs +23042 -0
- package/dist/web/req.mjs +1 -1
- package/lib/adapters/http.js +129 -117
- package/lib/helpers/formDataToStream.js +1 -1
- package/lib/platform/node/classes/URLSearchParams.js +1 -1
- package/package.json +3 -3
package/dist/web/req.mjs
CHANGED
package/lib/adapters/http.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import request from
|
|
2
|
-
import Agent from
|
|
3
|
-
import { log as Log, name } from
|
|
4
|
-
import utils from
|
|
5
|
-
import settle from
|
|
6
|
-
import buildFullPath from
|
|
7
|
-
import buildURL from
|
|
1
|
+
import request from '@wiajs/request';
|
|
2
|
+
import Agent from '@wiajs/agent';
|
|
3
|
+
import { log as Log, name } from '@wiajs/log';
|
|
4
|
+
import utils from '../utils.js';
|
|
5
|
+
import settle from '../core/settle.js';
|
|
6
|
+
import buildFullPath from '../core/buildFullPath.js';
|
|
7
|
+
import buildURL from '../helpers/buildURL.js';
|
|
8
8
|
// import {getProxyForUrl} from 'proxy-from-env';
|
|
9
|
-
import util from
|
|
9
|
+
import util from 'node:util';
|
|
10
10
|
// import followRedirects from 'follow-redirects';
|
|
11
11
|
// import Redirect from '../request/index.js'
|
|
12
|
-
import zlib from
|
|
13
|
-
import { VERSION } from
|
|
14
|
-
import transitionalDefaults from
|
|
15
|
-
import AxiosError from
|
|
16
|
-
import CanceledError from
|
|
17
|
-
import platform from
|
|
18
|
-
import fromDataURI from
|
|
19
|
-
import stream from
|
|
20
|
-
import AxiosHeaders from
|
|
21
|
-
import AxiosTransformStream from
|
|
22
|
-
import { EventEmitter } from
|
|
23
|
-
import formDataToStream from
|
|
24
|
-
import readBlob from
|
|
25
|
-
import callbackify from
|
|
26
|
-
import { progressEventReducer, progressEventDecorator, asyncDecorator } from
|
|
12
|
+
import zlib from 'node:zlib';
|
|
13
|
+
import { VERSION } from '../env/data.js';
|
|
14
|
+
import transitionalDefaults from '../defaults/transitional.js';
|
|
15
|
+
import AxiosError from '../core/AxiosError.js';
|
|
16
|
+
import CanceledError from '../cancel/CanceledError.js';
|
|
17
|
+
import platform from '../platform/index.js';
|
|
18
|
+
import fromDataURI from '../helpers/fromDataURI.js';
|
|
19
|
+
import stream from 'node:stream';
|
|
20
|
+
import AxiosHeaders from '../core/AxiosHeaders.js';
|
|
21
|
+
import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
|
|
22
|
+
import { EventEmitter } from 'node:events';
|
|
23
|
+
import formDataToStream from '../helpers/formDataToStream.js';
|
|
24
|
+
import readBlob from '../helpers/readBlob.js';
|
|
25
|
+
import callbackify from '../helpers/callbackify.js';
|
|
26
|
+
import { progressEventReducer, progressEventDecorator, asyncDecorator } from '../helpers/progressEventReducer.js';
|
|
27
27
|
// import Agent from '../fea/agent.js'
|
|
28
28
|
const log = Log({
|
|
29
29
|
env: `wia:req:${name(import.meta.url)}`
|
|
@@ -31,16 +31,16 @@ const log = Log({
|
|
|
31
31
|
const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
|
|
32
32
|
const isHttps = /https:?/;
|
|
33
33
|
const supportedProtocols = platform.protocols.map((protocol)=>`${protocol}:`);
|
|
34
|
-
const isHttpAdapterSupported = typeof process !==
|
|
34
|
+
const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process';
|
|
35
35
|
/**
|
|
36
36
|
* !+++
|
|
37
37
|
* 将request 函数改为类,请求拆分为 init 初始化和 请求执行,
|
|
38
38
|
* 如需重新发起请求时,无需重新初始化
|
|
39
39
|
*/ let HttpAdapter = class HttpAdapter {
|
|
40
40
|
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
*
|
|
42
|
+
* @param {*} config
|
|
43
|
+
*/ constructor(config){
|
|
44
44
|
this.isDone = false;
|
|
45
45
|
this.rejected = false;
|
|
46
46
|
/** @type {*} */ this.req = null;
|
|
@@ -52,32 +52,32 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
52
52
|
this.emitter = new EventEmitter();
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return this.method ===
|
|
55
|
+
*
|
|
56
|
+
* @param {number} code
|
|
57
|
+
* @returns
|
|
58
|
+
*/ noBody(code) {
|
|
59
|
+
return this.method === 'HEAD' || // Informational
|
|
60
60
|
code >= 100 && code < 200 || // No Content
|
|
61
61
|
code === 204 || // Not Modified
|
|
62
62
|
code === 304;
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this.emitter.emit(
|
|
65
|
+
* 发起终止事件
|
|
66
|
+
* @param {*} reason
|
|
67
|
+
*/ abort(reason) {
|
|
68
|
+
this.emitter.emit('abort', !reason || reason.type ? new CanceledError(null, this.config, this.req) : reason);
|
|
69
69
|
}
|
|
70
70
|
onFinished() {
|
|
71
71
|
const { config, emitter, abort } = this;
|
|
72
72
|
config?.cancelToken?.unsubscribe(abort);
|
|
73
|
-
config?.signal?.removeEventListener(
|
|
73
|
+
config?.signal?.removeEventListener('abort', abort);
|
|
74
74
|
emitter?.removeAllListeners();
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
*
|
|
78
|
+
* @param {*} value
|
|
79
|
+
* @param {*} isRejected
|
|
80
|
+
*/ onDone(value, isRejected) {
|
|
81
81
|
this.isDone = true;
|
|
82
82
|
if (isRejected) {
|
|
83
83
|
this.rejected = true;
|
|
@@ -85,19 +85,19 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
*
|
|
89
|
+
* @param {*} value
|
|
90
|
+
* @param {*} isRejected
|
|
91
|
+
* @returns
|
|
92
|
+
*/ done(value, isRejected) {
|
|
93
93
|
if (this.isDone) return;
|
|
94
94
|
this.isDone = true;
|
|
95
95
|
this?.onDone(value, isRejected);
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
98
|
+
* 初始化,生成 options 供请求调用
|
|
99
|
+
* @returns {*} options
|
|
100
|
+
*/ async init() {
|
|
101
101
|
// biome-ignore lint/complexity/noUselessThisAlias: <explanation>
|
|
102
102
|
const _ = this;
|
|
103
103
|
const { config } = _;
|
|
@@ -110,10 +110,10 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
110
110
|
]);
|
|
111
111
|
// hotfix to support opt.all option which is required for node 20.x
|
|
112
112
|
/**
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
* @param {string} hostname
|
|
114
|
+
* @param {*} opt
|
|
115
|
+
* @param {*} cb
|
|
116
|
+
*/ lookup = (hostname, opt, cb)=>{
|
|
117
117
|
_lookup(hostname, opt, (err, arg0, arg1)=>{
|
|
118
118
|
if (err) return cb(err);
|
|
119
119
|
const addresses = utils.isArray(arg0) ? arg0.map((addr)=>buildAddressEntry(addr)) : [
|
|
@@ -127,7 +127,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
127
127
|
config.cancelToken?.subscribe(_.abort);
|
|
128
128
|
if (config.signal) {
|
|
129
129
|
if (config.signal.aborted) _.abort();
|
|
130
|
-
else config.signal.addEventListener(
|
|
130
|
+
else config.signal.addEventListener('abort', _.abort);
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
// Parse url
|
|
@@ -137,11 +137,11 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
137
137
|
// http: or https:
|
|
138
138
|
const protocol = parsed.protocol || supportedProtocols[0];
|
|
139
139
|
_.protocol = protocol;
|
|
140
|
-
if (protocol ===
|
|
140
|
+
if (protocol === 'data:' && method !== 'GET') {
|
|
141
141
|
// throw error
|
|
142
142
|
const response = {
|
|
143
143
|
status: 405,
|
|
144
|
-
statusText:
|
|
144
|
+
statusText: 'method not allowed',
|
|
145
145
|
headers: {},
|
|
146
146
|
config
|
|
147
147
|
};
|
|
@@ -159,7 +159,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
159
159
|
// User-Agent is specified; handle case where no UA header is desired
|
|
160
160
|
// Only set header if it hasn't been set in config
|
|
161
161
|
// ! headers.set('User-Agent', 'axios/' + VERSION, false);
|
|
162
|
-
headers.set(
|
|
162
|
+
headers.set('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35', false);
|
|
163
163
|
const { onDownloadProgress, onUploadProgress, maxRate } = config;
|
|
164
164
|
// support for spec compliant FormData objects
|
|
165
165
|
if (utils.isSpecCompliantForm(data)) {
|
|
@@ -178,7 +178,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
178
178
|
/*eslint no-empty:0*/ } catch (e) {}
|
|
179
179
|
}
|
|
180
180
|
} else if (utils.isBlob(data)) {
|
|
181
|
-
data.size && headers.setContentType(data.type ||
|
|
181
|
+
data.size && headers.setContentType(data.type || 'application/octet-stream');
|
|
182
182
|
headers.setContentLength(data.size || 0);
|
|
183
183
|
data = stream.Readable.from(readBlob(data));
|
|
184
184
|
} else if (data && !utils.isStream(data)) {
|
|
@@ -187,14 +187,14 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
187
187
|
} else if (utils.isArrayBuffer(data)) {
|
|
188
188
|
data = Buffer.from(new Uint8Array(data));
|
|
189
189
|
} else if (utils.isString(data)) {
|
|
190
|
-
data = Buffer.from(data,
|
|
190
|
+
data = Buffer.from(data, 'utf-8');
|
|
191
191
|
} else {
|
|
192
|
-
throw new AxiosError(
|
|
192
|
+
throw new AxiosError('Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream', AxiosError.ERR_BAD_REQUEST, config);
|
|
193
193
|
}
|
|
194
194
|
// Add Content-Length header if data exists
|
|
195
195
|
headers.setContentLength(data.length, false);
|
|
196
196
|
if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
|
|
197
|
-
throw new AxiosError(
|
|
197
|
+
throw new AxiosError('Request body larger than maxBodyLength limit', AxiosError.ERR_BAD_REQUEST, config);
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
200
|
const contentLength = utils.toFiniteNumber(headers.getContentLength());
|
|
@@ -219,13 +219,13 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
219
219
|
maxRate: utils.toFiniteNumber(maxUploadRate)
|
|
220
220
|
})
|
|
221
221
|
], utils.noop);
|
|
222
|
-
onUploadProgress && data.on(
|
|
222
|
+
onUploadProgress && data.on('progress', flushOnFinish(data, progressEventDecorator(contentLength, progressEventReducer(asyncDecorator(onUploadProgress), false, 3))));
|
|
223
223
|
}
|
|
224
224
|
// HTTP basic authentication
|
|
225
225
|
let auth;
|
|
226
226
|
if (config.auth) {
|
|
227
|
-
const username = config.auth.username ||
|
|
228
|
-
const password = config.auth.password ||
|
|
227
|
+
const username = config.auth.username || '';
|
|
228
|
+
const password = config.auth.password || '';
|
|
229
229
|
auth = `${username}:${password}`;
|
|
230
230
|
}
|
|
231
231
|
if (!auth && parsed.username) {
|
|
@@ -233,10 +233,10 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
233
233
|
const urlPassword = parsed.password;
|
|
234
234
|
auth = `${urlUsername}:${urlPassword}`;
|
|
235
235
|
}
|
|
236
|
-
auth && headers.delete(
|
|
236
|
+
auth && headers.delete('authorization');
|
|
237
237
|
let path;
|
|
238
238
|
try {
|
|
239
|
-
path = buildURL(parsed.pathname + parsed.search, config.params, config.paramsSerializer).replace(/^\?/,
|
|
239
|
+
path = buildURL(parsed.pathname + parsed.search, config.params, config.paramsSerializer).replace(/^\?/, '');
|
|
240
240
|
} catch (err) {
|
|
241
241
|
/** @type {*} */ const customErr = new Error(err.message);
|
|
242
242
|
customErr.config = config;
|
|
@@ -244,7 +244,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
244
244
|
customErr.exists = true;
|
|
245
245
|
throw customErr;
|
|
246
246
|
}
|
|
247
|
-
headers.set(
|
|
247
|
+
headers.set('Accept-Encoding', `gzip, compress, deflate${isBrotliSupported ? ', br' : ''}`, false);
|
|
248
248
|
/** @type {*} */ const options = {
|
|
249
249
|
path,
|
|
250
250
|
method,
|
|
@@ -263,9 +263,9 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
263
263
|
if (!utils.isUndefined(lookup)) options.lookup = lookup;
|
|
264
264
|
if (config.socketPath) options.socketPath = config.socketPath;
|
|
265
265
|
else {
|
|
266
|
-
options.hostname = parsed.hostname.startsWith(
|
|
266
|
+
options.hostname = parsed.hostname.startsWith('[') ? parsed.hostname.slice(1, -1) : parsed.hostname;
|
|
267
267
|
options.port = parsed.port;
|
|
268
|
-
// ! proxy
|
|
268
|
+
// ! proxy 配置了 agent,否则使用缺省 agent
|
|
269
269
|
if (config.agent) options.agents = new Agent(config.agent);
|
|
270
270
|
}
|
|
271
271
|
// 执行请求的具体对象
|
|
@@ -286,15 +286,15 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
286
286
|
_.data = data;
|
|
287
287
|
log({
|
|
288
288
|
config
|
|
289
|
-
},
|
|
289
|
+
}, 'init');
|
|
290
290
|
return options;
|
|
291
291
|
}
|
|
292
292
|
/**
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
293
|
+
* 执行请求
|
|
294
|
+
* 需抛出内部异常
|
|
295
|
+
* @param {Axios} axios 实例
|
|
296
|
+
* @returns {Promise<*>}
|
|
297
|
+
*/ async request(axios) {
|
|
298
298
|
/** @type {*} */ // biome-ignore lint/style/useConst: <explanation>
|
|
299
299
|
let R;
|
|
300
300
|
// biome-ignore lint/complexity/noUselessThisAlias: <explanation>
|
|
@@ -303,28 +303,28 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
303
303
|
await _.init();
|
|
304
304
|
const { transport, protocol, config, options, data, abort, emitter, maxDownloadRate } = _;
|
|
305
305
|
const { responseType, responseEncoding, onDownloadProgress } = config;
|
|
306
|
-
if (protocol ===
|
|
306
|
+
if (protocol === 'data:') {
|
|
307
307
|
/** @type {*} */ let convertedData;
|
|
308
308
|
try {
|
|
309
|
-
convertedData = fromDataURI(config.url, responseType ===
|
|
309
|
+
convertedData = fromDataURI(config.url, responseType === 'blob', {
|
|
310
310
|
Blob: config.env?.Blob
|
|
311
311
|
});
|
|
312
312
|
} catch (err) {
|
|
313
313
|
throw AxiosError.from(err, AxiosError.ERR_BAD_REQUEST, config);
|
|
314
314
|
}
|
|
315
|
-
if (responseType ===
|
|
315
|
+
if (responseType === 'text') {
|
|
316
316
|
convertedData = convertedData.toString(responseEncoding);
|
|
317
|
-
if (!responseEncoding || responseEncoding ===
|
|
317
|
+
if (!responseEncoding || responseEncoding === 'utf8') {
|
|
318
318
|
convertedData = utils.stripBOM(convertedData);
|
|
319
319
|
}
|
|
320
|
-
} else if (responseType ===
|
|
320
|
+
} else if (responseType === 'stream') {
|
|
321
321
|
convertedData = stream.Readable.from(convertedData);
|
|
322
322
|
}
|
|
323
323
|
// 返回响应
|
|
324
324
|
R = {
|
|
325
325
|
data: convertedData,
|
|
326
326
|
status: 200,
|
|
327
|
-
statusText:
|
|
327
|
+
statusText: 'OK',
|
|
328
328
|
headers: new AxiosHeaders(),
|
|
329
329
|
config
|
|
330
330
|
};
|
|
@@ -334,34 +334,34 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
334
334
|
transformStream = new AxiosTransformStream({
|
|
335
335
|
maxRate: utils.toFiniteNumber(maxDownloadRate)
|
|
336
336
|
});
|
|
337
|
-
onDownloadProgress && transformStream.on(
|
|
337
|
+
onDownloadProgress && transformStream.on('progress', flushOnFinish(transformStream, progressEventDecorator(transformStream.responseLength, progressEventReducer(asyncDecorator(onDownloadProgress), true, 3))));
|
|
338
338
|
}
|
|
339
339
|
options.transformStream = transformStream;
|
|
340
340
|
// 发起异步请求
|
|
341
341
|
R = await new Promise((resolve, reject)=>{
|
|
342
|
-
_.emitter.once(
|
|
342
|
+
_.emitter.once('abort', reject);
|
|
343
343
|
options.stream = config.stream;
|
|
344
344
|
options.decompress = config.decompress;
|
|
345
345
|
// Create the request,promise false: return stream
|
|
346
346
|
// log.debug('request', {options});
|
|
347
347
|
const req = transport ? transport.request(options) : request(options);
|
|
348
|
-
if (!req) return reject(new AxiosError(
|
|
348
|
+
if (!req) return reject(new AxiosError('Request failed.', AxiosError.ERR_BAD_REQUEST, config));
|
|
349
349
|
_.req = req;
|
|
350
|
-
emitter.once(
|
|
351
|
-
log(
|
|
350
|
+
emitter.once('abort', (err)=>{
|
|
351
|
+
log('onabort');
|
|
352
352
|
reject(err);
|
|
353
353
|
req.destroy(err);
|
|
354
354
|
});
|
|
355
355
|
// Handle errors
|
|
356
|
-
req.on(
|
|
357
|
-
log(
|
|
356
|
+
req.on('error', /** @param {*} err */ (err)=>{
|
|
357
|
+
log('onerror');
|
|
358
358
|
// @todo remove
|
|
359
359
|
// if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;
|
|
360
360
|
reject(AxiosError.from(err, null, config, req));
|
|
361
361
|
});
|
|
362
362
|
// set tcp keep alive to prevent drop connection by peer
|
|
363
|
-
req.on(
|
|
364
|
-
log(
|
|
363
|
+
req.on('socket', /** @param {*} socket */ (socket)=>{
|
|
364
|
+
log('onsocket');
|
|
365
365
|
// default interval of sending ack packet is 1 minute
|
|
366
366
|
socket.setKeepAlive(true, 1000 * 60);
|
|
367
367
|
});
|
|
@@ -370,7 +370,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
370
370
|
// This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
|
|
371
371
|
const timeout = Number.parseInt(config.timeout);
|
|
372
372
|
if (Number.isNaN(timeout)) {
|
|
373
|
-
reject(new AxiosError(
|
|
373
|
+
reject(new AxiosError('error trying to parse `config.timeout` to int', AxiosError.ERR_BAD_OPTION_VALUE, config, req));
|
|
374
374
|
} else {
|
|
375
375
|
// Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
|
|
376
376
|
// And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
|
|
@@ -379,7 +379,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
379
379
|
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
|
|
380
380
|
req.setTimeout(timeout, ()=>{
|
|
381
381
|
if (_.isDone) return;
|
|
382
|
-
let timeoutErrorMessage = config.timeout ? `timeout of ${config.timeout}ms exceeded` :
|
|
382
|
+
let timeoutErrorMessage = config.timeout ? `timeout of ${config.timeout}ms exceeded` : 'timeout exceeded';
|
|
383
383
|
const transitional = config.transitional || transitionalDefaults;
|
|
384
384
|
if (config.timeoutErrorMessage) {
|
|
385
385
|
timeoutErrorMessage = config.timeoutErrorMessage;
|
|
@@ -390,19 +390,19 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
390
390
|
}
|
|
391
391
|
}
|
|
392
392
|
// stream finished
|
|
393
|
-
req.on(
|
|
393
|
+
req.on('finished', _.onFinished.bind(_));
|
|
394
394
|
// ! stream 模式不等待响应数据,直接返回 req,建立pipe管道流
|
|
395
395
|
if (config.stream) resolve(req);
|
|
396
396
|
else {
|
|
397
397
|
// 非stream模式,等待响应数据,返回数据
|
|
398
|
-
req.on(
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
398
|
+
req.on('response', /**
|
|
399
|
+
* @param {*} res
|
|
400
|
+
* @param {*} stream
|
|
401
|
+
*/ (res, stream)=>{
|
|
402
402
|
if (req.destroyed) return;
|
|
403
403
|
// 'transfer-encoding': 'chunked'时,无content-length,axios v1.2 不能自动解压
|
|
404
|
-
const responseLength = +res.headers[
|
|
405
|
-
log(
|
|
404
|
+
const responseLength = +res.headers['content-length'];
|
|
405
|
+
log('onresponse', {
|
|
406
406
|
statusCode: res.statusCode,
|
|
407
407
|
responseLength,
|
|
408
408
|
headers: res.headers
|
|
@@ -417,7 +417,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
417
417
|
request: lastRequest
|
|
418
418
|
};
|
|
419
419
|
// 直接返回 responseStream
|
|
420
|
-
if (responseType ===
|
|
420
|
+
if (responseType === 'stream') {
|
|
421
421
|
response.data = stream;
|
|
422
422
|
settle(resolve, reject, response);
|
|
423
423
|
} else {
|
|
@@ -425,7 +425,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
425
425
|
/** @type {*} */ const responseBuffer = [];
|
|
426
426
|
let totalResponseBytes = 0;
|
|
427
427
|
// 处理数据
|
|
428
|
-
stream.on(
|
|
428
|
+
stream.on('data', /** @param {*} chunk */ (chunk)=>{
|
|
429
429
|
responseBuffer.push(chunk);
|
|
430
430
|
totalResponseBytes += chunk.length;
|
|
431
431
|
// make sure the content length is not over the maxContentLength if specified
|
|
@@ -436,23 +436,23 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
436
436
|
reject(new AxiosError(`maxContentLength size of ${config.maxContentLength} exceeded`, AxiosError.ERR_BAD_RESPONSE, config, lastRequest));
|
|
437
437
|
}
|
|
438
438
|
});
|
|
439
|
-
stream.on(
|
|
439
|
+
stream.on('aborted', function handlerStreamAborted() {
|
|
440
440
|
if (_.rejected) return;
|
|
441
441
|
const err = new AxiosError(`maxContentLength size of ${config.maxContentLength} exceeded`, AxiosError.ERR_BAD_RESPONSE, config, lastRequest);
|
|
442
442
|
stream.destroy(err);
|
|
443
443
|
reject(err);
|
|
444
444
|
});
|
|
445
|
-
stream.on(
|
|
445
|
+
stream.on('error', function handleStreamError(err) {
|
|
446
446
|
if (req.destroyed) return;
|
|
447
447
|
reject(AxiosError.from(err, null, config, lastRequest));
|
|
448
448
|
});
|
|
449
449
|
// 数据传输结束
|
|
450
|
-
stream.on(
|
|
450
|
+
stream.on('end', function handleStreamEnd() {
|
|
451
451
|
try {
|
|
452
452
|
let responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
|
|
453
|
-
if (responseType !==
|
|
453
|
+
if (responseType !== 'arraybuffer') {
|
|
454
454
|
responseData = responseData.toString(responseEncoding);
|
|
455
|
-
if (!responseEncoding || responseEncoding ===
|
|
455
|
+
if (!responseEncoding || responseEncoding === 'utf8') {
|
|
456
456
|
responseData = utils.stripBOM(responseData);
|
|
457
457
|
}
|
|
458
458
|
}
|
|
@@ -463,9 +463,9 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
463
463
|
}
|
|
464
464
|
});
|
|
465
465
|
}
|
|
466
|
-
emitter.once(
|
|
466
|
+
emitter.once('abort', (err)=>{
|
|
467
467
|
if (!stream.destroyed) {
|
|
468
|
-
stream.emit(
|
|
468
|
+
stream.emit('error', err);
|
|
469
469
|
stream.destroy();
|
|
470
470
|
}
|
|
471
471
|
});
|
|
@@ -475,16 +475,16 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
475
475
|
// Send the request
|
|
476
476
|
let ended = false;
|
|
477
477
|
let errored = false;
|
|
478
|
-
data.on(
|
|
478
|
+
data.on('end', ()=>{
|
|
479
479
|
ended = true;
|
|
480
480
|
});
|
|
481
|
-
data.once(
|
|
481
|
+
data.once('error', /** @param {*} err */ (err)=>{
|
|
482
482
|
errored = true;
|
|
483
483
|
req.destroy(err);
|
|
484
484
|
});
|
|
485
|
-
data.on(
|
|
485
|
+
data.on('close', ()=>{
|
|
486
486
|
if (!ended && !errored) {
|
|
487
|
-
abort(new CanceledError(
|
|
487
|
+
abort(new CanceledError('Request stream has been aborted', config, req));
|
|
488
488
|
}
|
|
489
489
|
});
|
|
490
490
|
data.pipe(req); // stream 写入数据
|
|
@@ -494,20 +494,32 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
494
494
|
}
|
|
495
495
|
_.done(R);
|
|
496
496
|
} catch (e) {
|
|
497
|
-
log.error(e,
|
|
497
|
+
log.error(e, 'request');
|
|
498
498
|
_.done(e, true);
|
|
499
499
|
throw e;
|
|
500
500
|
}
|
|
501
501
|
return R;
|
|
502
502
|
}
|
|
503
503
|
};
|
|
504
|
+
/**
|
|
505
|
+
* 排除对象字段
|
|
506
|
+
* @param {*} obj
|
|
507
|
+
* @param {...any} keys
|
|
508
|
+
* @returns {*}
|
|
509
|
+
*/ function omit(obj, ...keys) {
|
|
510
|
+
/** @type {*} */ const R = {};
|
|
511
|
+
for (const k of Object.keys(obj)){
|
|
512
|
+
if (!keys.includes(k)) R[k] = obj[k];
|
|
513
|
+
}
|
|
514
|
+
return R;
|
|
515
|
+
}
|
|
504
516
|
/**
|
|
505
517
|
*
|
|
506
518
|
* @param {*} stream
|
|
507
519
|
* @param {*} param1
|
|
508
520
|
* @returns
|
|
509
521
|
*/ const flushOnFinish = (stream, [throttled, flush])=>{
|
|
510
|
-
stream.on(
|
|
522
|
+
stream.on('end', flush).on('error', flush);
|
|
511
523
|
return throttled;
|
|
512
524
|
};
|
|
513
525
|
/** @typedef {import('../core/Axios').default} Axios */ /**
|
|
@@ -518,7 +530,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
518
530
|
* @param {*} responseDetails - The options object that was passed to the request.
|
|
519
531
|
*
|
|
520
532
|
*/ function dispatchBeforeRedirect(options, responseDetails) {
|
|
521
|
-
log.debug(
|
|
533
|
+
log.debug('dispatchBeforeRedirect', {
|
|
522
534
|
opts: options.beforeRedirects
|
|
523
535
|
});
|
|
524
536
|
if (options.beforeRedirects.proxy) options.beforeRedirects.proxy(options);
|
|
@@ -530,11 +542,11 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils.kindOf(pr
|
|
|
530
542
|
* @returns
|
|
531
543
|
*/ function resolveFamily({ address, family }) {
|
|
532
544
|
if (!utils.isString(address)) {
|
|
533
|
-
throw TypeError(
|
|
545
|
+
throw TypeError('address must be a string');
|
|
534
546
|
}
|
|
535
547
|
return {
|
|
536
548
|
address,
|
|
537
|
-
family: family || (address.indexOf(
|
|
549
|
+
family: family || (address.indexOf('.') < 0 ? 6 : 4)
|
|
538
550
|
};
|
|
539
551
|
}
|
|
540
552
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wiajs/req",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.11",
|
|
4
4
|
"description": "Promise And Stream based AXIOS client for the browser and node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -142,8 +142,8 @@
|
|
|
142
142
|
"@rollup/plugin-alias": "^5.1.0"
|
|
143
143
|
},
|
|
144
144
|
"dependencies": {
|
|
145
|
-
"@wiajs/log": "^4.3.
|
|
146
|
-
"@wiajs/request": "^3.0.
|
|
145
|
+
"@wiajs/log": "^4.3.13",
|
|
146
|
+
"@wiajs/request": "^3.0.18",
|
|
147
147
|
"@wiajs/agent": "^1.0.13",
|
|
148
148
|
"form-data": "^4.0.0",
|
|
149
149
|
"proxy-from-env": "^1.1.0"
|