@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/node/req.mjs
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wia/req v1.7.
|
|
2
|
+
* @wia/req v1.7.10
|
|
3
3
|
* (c) 2024 Sibyl Yu, Matt Zabriskie and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
import FormData$1 from 'form-data';
|
|
7
|
-
import url from 'url';
|
|
7
|
+
import url from 'node:url';
|
|
8
8
|
import request from '@wiajs/request';
|
|
9
9
|
import Agent from '@wiajs/agent';
|
|
10
10
|
import { log as log$1, name } from '@wiajs/log';
|
|
11
|
-
import util from 'node:util';
|
|
11
|
+
import util, { TextEncoder } from 'node:util';
|
|
12
12
|
import zlib from 'node:zlib';
|
|
13
13
|
import stream$1 from 'node:stream';
|
|
14
14
|
import stream, { Readable } from 'stream';
|
|
15
15
|
import { EventEmitter } from 'node:events';
|
|
16
|
-
import { TextEncoder } from 'util';
|
|
17
16
|
|
|
18
17
|
function bind(fn, thisArg) {
|
|
19
18
|
return function wrap() {
|
|
@@ -2155,16 +2154,16 @@ const log = log$1({
|
|
|
2155
2154
|
const isBrotliSupported = utils$1.isFunction(zlib.createBrotliDecompress);
|
|
2156
2155
|
const isHttps = /https:?/;
|
|
2157
2156
|
const supportedProtocols = platform.protocols.map((protocol)=>`${protocol}:`);
|
|
2158
|
-
const isHttpAdapterSupported = typeof process !==
|
|
2157
|
+
const isHttpAdapterSupported = typeof process !== 'undefined' && utils$1.kindOf(process) === 'process';
|
|
2159
2158
|
/**
|
|
2160
2159
|
* !+++
|
|
2161
2160
|
* 将request 函数改为类,请求拆分为 init 初始化和 请求执行,
|
|
2162
2161
|
* 如需重新发起请求时,无需重新初始化
|
|
2163
2162
|
*/ let HttpAdapter = class HttpAdapter {
|
|
2164
2163
|
/**
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2164
|
+
*
|
|
2165
|
+
* @param {*} config
|
|
2166
|
+
*/ constructor(config){
|
|
2168
2167
|
this.isDone = false;
|
|
2169
2168
|
this.rejected = false;
|
|
2170
2169
|
/** @type {*} */ this.req = null;
|
|
@@ -2176,32 +2175,32 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2176
2175
|
this.emitter = new EventEmitter();
|
|
2177
2176
|
}
|
|
2178
2177
|
/**
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
return this.method ===
|
|
2178
|
+
*
|
|
2179
|
+
* @param {number} code
|
|
2180
|
+
* @returns
|
|
2181
|
+
*/ noBody(code) {
|
|
2182
|
+
return this.method === 'HEAD' || // Informational
|
|
2184
2183
|
code >= 100 && code < 200 || // No Content
|
|
2185
2184
|
code === 204 || // Not Modified
|
|
2186
2185
|
code === 304;
|
|
2187
2186
|
}
|
|
2188
2187
|
/**
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
this.emitter.emit(
|
|
2188
|
+
* 发起终止事件
|
|
2189
|
+
* @param {*} reason
|
|
2190
|
+
*/ abort(reason) {
|
|
2191
|
+
this.emitter.emit('abort', !reason || reason.type ? new CanceledError$1(null, this.config, this.req) : reason);
|
|
2193
2192
|
}
|
|
2194
2193
|
onFinished() {
|
|
2195
2194
|
const { config, emitter, abort } = this;
|
|
2196
2195
|
config?.cancelToken?.unsubscribe(abort);
|
|
2197
|
-
config?.signal?.removeEventListener(
|
|
2196
|
+
config?.signal?.removeEventListener('abort', abort);
|
|
2198
2197
|
emitter?.removeAllListeners();
|
|
2199
2198
|
}
|
|
2200
2199
|
/**
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2200
|
+
*
|
|
2201
|
+
* @param {*} value
|
|
2202
|
+
* @param {*} isRejected
|
|
2203
|
+
*/ onDone(value, isRejected) {
|
|
2205
2204
|
this.isDone = true;
|
|
2206
2205
|
if (isRejected) {
|
|
2207
2206
|
this.rejected = true;
|
|
@@ -2209,19 +2208,19 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2209
2208
|
}
|
|
2210
2209
|
}
|
|
2211
2210
|
/**
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2211
|
+
*
|
|
2212
|
+
* @param {*} value
|
|
2213
|
+
* @param {*} isRejected
|
|
2214
|
+
* @returns
|
|
2215
|
+
*/ done(value, isRejected) {
|
|
2217
2216
|
if (this.isDone) return;
|
|
2218
2217
|
this.isDone = true;
|
|
2219
2218
|
this?.onDone(value, isRejected);
|
|
2220
2219
|
}
|
|
2221
2220
|
/**
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2221
|
+
* 初始化,生成 options 供请求调用
|
|
2222
|
+
* @returns {*} options
|
|
2223
|
+
*/ async init() {
|
|
2225
2224
|
// biome-ignore lint/complexity/noUselessThisAlias: <explanation>
|
|
2226
2225
|
const _ = this;
|
|
2227
2226
|
const { config } = _;
|
|
@@ -2234,10 +2233,10 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2234
2233
|
]);
|
|
2235
2234
|
// hotfix to support opt.all option which is required for node 20.x
|
|
2236
2235
|
/**
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2236
|
+
* @param {string} hostname
|
|
2237
|
+
* @param {*} opt
|
|
2238
|
+
* @param {*} cb
|
|
2239
|
+
*/ lookup = (hostname, opt, cb)=>{
|
|
2241
2240
|
_lookup(hostname, opt, (err, arg0, arg1)=>{
|
|
2242
2241
|
if (err) return cb(err);
|
|
2243
2242
|
const addresses = utils$1.isArray(arg0) ? arg0.map((addr)=>buildAddressEntry(addr)) : [
|
|
@@ -2251,7 +2250,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2251
2250
|
config.cancelToken?.subscribe(_.abort);
|
|
2252
2251
|
if (config.signal) {
|
|
2253
2252
|
if (config.signal.aborted) _.abort();
|
|
2254
|
-
else config.signal.addEventListener(
|
|
2253
|
+
else config.signal.addEventListener('abort', _.abort);
|
|
2255
2254
|
}
|
|
2256
2255
|
}
|
|
2257
2256
|
// Parse url
|
|
@@ -2261,11 +2260,11 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2261
2260
|
// http: or https:
|
|
2262
2261
|
const protocol = parsed.protocol || supportedProtocols[0];
|
|
2263
2262
|
_.protocol = protocol;
|
|
2264
|
-
if (protocol ===
|
|
2263
|
+
if (protocol === 'data:' && method !== 'GET') {
|
|
2265
2264
|
// throw error
|
|
2266
2265
|
const response = {
|
|
2267
2266
|
status: 405,
|
|
2268
|
-
statusText:
|
|
2267
|
+
statusText: 'method not allowed',
|
|
2269
2268
|
headers: {},
|
|
2270
2269
|
config
|
|
2271
2270
|
};
|
|
@@ -2283,7 +2282,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2283
2282
|
// User-Agent is specified; handle case where no UA header is desired
|
|
2284
2283
|
// Only set header if it hasn't been set in config
|
|
2285
2284
|
// ! headers.set('User-Agent', 'axios/' + VERSION, false);
|
|
2286
|
-
headers.set(
|
|
2285
|
+
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);
|
|
2287
2286
|
const { onDownloadProgress, onUploadProgress, maxRate } = config;
|
|
2288
2287
|
// support for spec compliant FormData objects
|
|
2289
2288
|
if (utils$1.isSpecCompliantForm(data)) {
|
|
@@ -2302,21 +2301,21 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2302
2301
|
/*eslint no-empty:0*/ } catch (e) {}
|
|
2303
2302
|
}
|
|
2304
2303
|
} else if (utils$1.isBlob(data)) {
|
|
2305
|
-
data.size && headers.setContentType(data.type ||
|
|
2304
|
+
data.size && headers.setContentType(data.type || 'application/octet-stream');
|
|
2306
2305
|
headers.setContentLength(data.size || 0);
|
|
2307
2306
|
data = stream$1.Readable.from(readBlob$1(data));
|
|
2308
2307
|
} else if (data && !utils$1.isStream(data)) {
|
|
2309
2308
|
if (Buffer.isBuffer(data)) ; else if (utils$1.isArrayBuffer(data)) {
|
|
2310
2309
|
data = Buffer.from(new Uint8Array(data));
|
|
2311
2310
|
} else if (utils$1.isString(data)) {
|
|
2312
|
-
data = Buffer.from(data,
|
|
2311
|
+
data = Buffer.from(data, 'utf-8');
|
|
2313
2312
|
} else {
|
|
2314
|
-
throw new AxiosError$1(
|
|
2313
|
+
throw new AxiosError$1('Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream', AxiosError$1.ERR_BAD_REQUEST, config);
|
|
2315
2314
|
}
|
|
2316
2315
|
// Add Content-Length header if data exists
|
|
2317
2316
|
headers.setContentLength(data.length, false);
|
|
2318
2317
|
if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
|
|
2319
|
-
throw new AxiosError$1(
|
|
2318
|
+
throw new AxiosError$1('Request body larger than maxBodyLength limit', AxiosError$1.ERR_BAD_REQUEST, config);
|
|
2320
2319
|
}
|
|
2321
2320
|
}
|
|
2322
2321
|
const contentLength = utils$1.toFiniteNumber(headers.getContentLength());
|
|
@@ -2341,13 +2340,13 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2341
2340
|
maxRate: utils$1.toFiniteNumber(maxUploadRate)
|
|
2342
2341
|
})
|
|
2343
2342
|
], utils$1.noop);
|
|
2344
|
-
onUploadProgress && data.on(
|
|
2343
|
+
onUploadProgress && data.on('progress', flushOnFinish(data, progressEventDecorator(contentLength, progressEventReducer(asyncDecorator(onUploadProgress), false, 3))));
|
|
2345
2344
|
}
|
|
2346
2345
|
// HTTP basic authentication
|
|
2347
2346
|
let auth;
|
|
2348
2347
|
if (config.auth) {
|
|
2349
|
-
const username = config.auth.username ||
|
|
2350
|
-
const password = config.auth.password ||
|
|
2348
|
+
const username = config.auth.username || '';
|
|
2349
|
+
const password = config.auth.password || '';
|
|
2351
2350
|
auth = `${username}:${password}`;
|
|
2352
2351
|
}
|
|
2353
2352
|
if (!auth && parsed.username) {
|
|
@@ -2355,10 +2354,10 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2355
2354
|
const urlPassword = parsed.password;
|
|
2356
2355
|
auth = `${urlUsername}:${urlPassword}`;
|
|
2357
2356
|
}
|
|
2358
|
-
auth && headers.delete(
|
|
2357
|
+
auth && headers.delete('authorization');
|
|
2359
2358
|
let path;
|
|
2360
2359
|
try {
|
|
2361
|
-
path = buildURL(parsed.pathname + parsed.search, config.params, config.paramsSerializer).replace(/^\?/,
|
|
2360
|
+
path = buildURL(parsed.pathname + parsed.search, config.params, config.paramsSerializer).replace(/^\?/, '');
|
|
2362
2361
|
} catch (err) {
|
|
2363
2362
|
/** @type {*} */ const customErr = new Error(err.message);
|
|
2364
2363
|
customErr.config = config;
|
|
@@ -2366,7 +2365,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2366
2365
|
customErr.exists = true;
|
|
2367
2366
|
throw customErr;
|
|
2368
2367
|
}
|
|
2369
|
-
headers.set(
|
|
2368
|
+
headers.set('Accept-Encoding', `gzip, compress, deflate${isBrotliSupported ? ', br' : ''}`, false);
|
|
2370
2369
|
/** @type {*} */ const options = {
|
|
2371
2370
|
path,
|
|
2372
2371
|
method,
|
|
@@ -2385,9 +2384,9 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2385
2384
|
if (!utils$1.isUndefined(lookup)) options.lookup = lookup;
|
|
2386
2385
|
if (config.socketPath) options.socketPath = config.socketPath;
|
|
2387
2386
|
else {
|
|
2388
|
-
options.hostname = parsed.hostname.startsWith(
|
|
2387
|
+
options.hostname = parsed.hostname.startsWith('[') ? parsed.hostname.slice(1, -1) : parsed.hostname;
|
|
2389
2388
|
options.port = parsed.port;
|
|
2390
|
-
// ! proxy
|
|
2389
|
+
// ! proxy 配置了 agent,否则使用缺省 agent
|
|
2391
2390
|
if (config.agent) options.agents = new Agent(config.agent);
|
|
2392
2391
|
}
|
|
2393
2392
|
// 执行请求的具体对象
|
|
@@ -2408,15 +2407,15 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2408
2407
|
_.data = data;
|
|
2409
2408
|
log({
|
|
2410
2409
|
config
|
|
2411
|
-
},
|
|
2410
|
+
}, 'init');
|
|
2412
2411
|
return options;
|
|
2413
2412
|
}
|
|
2414
2413
|
/**
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2414
|
+
* 执行请求
|
|
2415
|
+
* 需抛出内部异常
|
|
2416
|
+
* @param {Axios} axios 实例
|
|
2417
|
+
* @returns {Promise<*>}
|
|
2418
|
+
*/ async request(axios) {
|
|
2420
2419
|
/** @type {*} */ // biome-ignore lint/style/useConst: <explanation>
|
|
2421
2420
|
let R;
|
|
2422
2421
|
// biome-ignore lint/complexity/noUselessThisAlias: <explanation>
|
|
@@ -2425,28 +2424,28 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2425
2424
|
await _.init();
|
|
2426
2425
|
const { transport, protocol, config, options, data, abort, emitter, maxDownloadRate } = _;
|
|
2427
2426
|
const { responseType, responseEncoding, onDownloadProgress } = config;
|
|
2428
|
-
if (protocol ===
|
|
2427
|
+
if (protocol === 'data:') {
|
|
2429
2428
|
/** @type {*} */ let convertedData;
|
|
2430
2429
|
try {
|
|
2431
|
-
convertedData = fromDataURI(config.url, responseType ===
|
|
2430
|
+
convertedData = fromDataURI(config.url, responseType === 'blob', {
|
|
2432
2431
|
Blob: config.env?.Blob
|
|
2433
2432
|
});
|
|
2434
2433
|
} catch (err) {
|
|
2435
2434
|
throw AxiosError$1.from(err, AxiosError$1.ERR_BAD_REQUEST, config);
|
|
2436
2435
|
}
|
|
2437
|
-
if (responseType ===
|
|
2436
|
+
if (responseType === 'text') {
|
|
2438
2437
|
convertedData = convertedData.toString(responseEncoding);
|
|
2439
|
-
if (!responseEncoding || responseEncoding ===
|
|
2438
|
+
if (!responseEncoding || responseEncoding === 'utf8') {
|
|
2440
2439
|
convertedData = utils$1.stripBOM(convertedData);
|
|
2441
2440
|
}
|
|
2442
|
-
} else if (responseType ===
|
|
2441
|
+
} else if (responseType === 'stream') {
|
|
2443
2442
|
convertedData = stream$1.Readable.from(convertedData);
|
|
2444
2443
|
}
|
|
2445
2444
|
// 返回响应
|
|
2446
2445
|
R = {
|
|
2447
2446
|
data: convertedData,
|
|
2448
2447
|
status: 200,
|
|
2449
|
-
statusText:
|
|
2448
|
+
statusText: 'OK',
|
|
2450
2449
|
headers: new AxiosHeaders$2(),
|
|
2451
2450
|
config
|
|
2452
2451
|
};
|
|
@@ -2456,34 +2455,34 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2456
2455
|
transformStream = new AxiosTransformStream$1({
|
|
2457
2456
|
maxRate: utils$1.toFiniteNumber(maxDownloadRate)
|
|
2458
2457
|
});
|
|
2459
|
-
onDownloadProgress && transformStream.on(
|
|
2458
|
+
onDownloadProgress && transformStream.on('progress', flushOnFinish(transformStream, progressEventDecorator(transformStream.responseLength, progressEventReducer(asyncDecorator(onDownloadProgress), true, 3))));
|
|
2460
2459
|
}
|
|
2461
2460
|
options.transformStream = transformStream;
|
|
2462
2461
|
// 发起异步请求
|
|
2463
2462
|
R = await new Promise((resolve, reject)=>{
|
|
2464
|
-
_.emitter.once(
|
|
2463
|
+
_.emitter.once('abort', reject);
|
|
2465
2464
|
options.stream = config.stream;
|
|
2466
2465
|
options.decompress = config.decompress;
|
|
2467
2466
|
// Create the request,promise false: return stream
|
|
2468
2467
|
// log.debug('request', {options});
|
|
2469
2468
|
const req = transport ? transport.request(options) : request(options);
|
|
2470
|
-
if (!req) return reject(new AxiosError$1(
|
|
2469
|
+
if (!req) return reject(new AxiosError$1('Request failed.', AxiosError$1.ERR_BAD_REQUEST, config));
|
|
2471
2470
|
_.req = req;
|
|
2472
|
-
emitter.once(
|
|
2473
|
-
log(
|
|
2471
|
+
emitter.once('abort', (err)=>{
|
|
2472
|
+
log('onabort');
|
|
2474
2473
|
reject(err);
|
|
2475
2474
|
req.destroy(err);
|
|
2476
2475
|
});
|
|
2477
2476
|
// Handle errors
|
|
2478
|
-
req.on(
|
|
2479
|
-
log(
|
|
2477
|
+
req.on('error', /** @param {*} err */ (err)=>{
|
|
2478
|
+
log('onerror');
|
|
2480
2479
|
// @todo remove
|
|
2481
2480
|
// if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;
|
|
2482
2481
|
reject(AxiosError$1.from(err, null, config, req));
|
|
2483
2482
|
});
|
|
2484
2483
|
// set tcp keep alive to prevent drop connection by peer
|
|
2485
|
-
req.on(
|
|
2486
|
-
log(
|
|
2484
|
+
req.on('socket', /** @param {*} socket */ (socket)=>{
|
|
2485
|
+
log('onsocket');
|
|
2487
2486
|
// default interval of sending ack packet is 1 minute
|
|
2488
2487
|
socket.setKeepAlive(true, 1000 * 60);
|
|
2489
2488
|
});
|
|
@@ -2492,7 +2491,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2492
2491
|
// This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
|
|
2493
2492
|
const timeout = Number.parseInt(config.timeout);
|
|
2494
2493
|
if (Number.isNaN(timeout)) {
|
|
2495
|
-
reject(new AxiosError$1(
|
|
2494
|
+
reject(new AxiosError$1('error trying to parse `config.timeout` to int', AxiosError$1.ERR_BAD_OPTION_VALUE, config, req));
|
|
2496
2495
|
} else {
|
|
2497
2496
|
// Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
|
|
2498
2497
|
// And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
|
|
@@ -2501,7 +2500,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2501
2500
|
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
|
|
2502
2501
|
req.setTimeout(timeout, ()=>{
|
|
2503
2502
|
if (_.isDone) return;
|
|
2504
|
-
let timeoutErrorMessage = config.timeout ? `timeout of ${config.timeout}ms exceeded` :
|
|
2503
|
+
let timeoutErrorMessage = config.timeout ? `timeout of ${config.timeout}ms exceeded` : 'timeout exceeded';
|
|
2505
2504
|
const transitional = config.transitional || transitionalDefaults;
|
|
2506
2505
|
if (config.timeoutErrorMessage) {
|
|
2507
2506
|
timeoutErrorMessage = config.timeoutErrorMessage;
|
|
@@ -2512,19 +2511,19 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2512
2511
|
}
|
|
2513
2512
|
}
|
|
2514
2513
|
// stream finished
|
|
2515
|
-
req.on(
|
|
2514
|
+
req.on('finished', _.onFinished.bind(_));
|
|
2516
2515
|
// ! stream 模式不等待响应数据,直接返回 req,建立pipe管道流
|
|
2517
2516
|
if (config.stream) resolve(req);
|
|
2518
2517
|
else {
|
|
2519
2518
|
// 非stream模式,等待响应数据,返回数据
|
|
2520
|
-
req.on(
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2519
|
+
req.on('response', /**
|
|
2520
|
+
* @param {*} res
|
|
2521
|
+
* @param {*} stream
|
|
2522
|
+
*/ (res, stream)=>{
|
|
2524
2523
|
if (req.destroyed) return;
|
|
2525
2524
|
// 'transfer-encoding': 'chunked'时,无content-length,axios v1.2 不能自动解压
|
|
2526
|
-
const responseLength = +res.headers[
|
|
2527
|
-
log(
|
|
2525
|
+
const responseLength = +res.headers['content-length'];
|
|
2526
|
+
log('onresponse', {
|
|
2528
2527
|
statusCode: res.statusCode,
|
|
2529
2528
|
responseLength,
|
|
2530
2529
|
headers: res.headers
|
|
@@ -2539,7 +2538,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2539
2538
|
request: lastRequest
|
|
2540
2539
|
};
|
|
2541
2540
|
// 直接返回 responseStream
|
|
2542
|
-
if (responseType ===
|
|
2541
|
+
if (responseType === 'stream') {
|
|
2543
2542
|
response.data = stream;
|
|
2544
2543
|
settle(resolve, reject, response);
|
|
2545
2544
|
} else {
|
|
@@ -2547,7 +2546,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2547
2546
|
/** @type {*} */ const responseBuffer = [];
|
|
2548
2547
|
let totalResponseBytes = 0;
|
|
2549
2548
|
// 处理数据
|
|
2550
|
-
stream.on(
|
|
2549
|
+
stream.on('data', /** @param {*} chunk */ (chunk)=>{
|
|
2551
2550
|
responseBuffer.push(chunk);
|
|
2552
2551
|
totalResponseBytes += chunk.length;
|
|
2553
2552
|
// make sure the content length is not over the maxContentLength if specified
|
|
@@ -2558,23 +2557,23 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2558
2557
|
reject(new AxiosError$1(`maxContentLength size of ${config.maxContentLength} exceeded`, AxiosError$1.ERR_BAD_RESPONSE, config, lastRequest));
|
|
2559
2558
|
}
|
|
2560
2559
|
});
|
|
2561
|
-
stream.on(
|
|
2560
|
+
stream.on('aborted', function handlerStreamAborted() {
|
|
2562
2561
|
if (_.rejected) return;
|
|
2563
2562
|
const err = new AxiosError$1(`maxContentLength size of ${config.maxContentLength} exceeded`, AxiosError$1.ERR_BAD_RESPONSE, config, lastRequest);
|
|
2564
2563
|
stream.destroy(err);
|
|
2565
2564
|
reject(err);
|
|
2566
2565
|
});
|
|
2567
|
-
stream.on(
|
|
2566
|
+
stream.on('error', function handleStreamError(err) {
|
|
2568
2567
|
if (req.destroyed) return;
|
|
2569
2568
|
reject(AxiosError$1.from(err, null, config, lastRequest));
|
|
2570
2569
|
});
|
|
2571
2570
|
// 数据传输结束
|
|
2572
|
-
stream.on(
|
|
2571
|
+
stream.on('end', function handleStreamEnd() {
|
|
2573
2572
|
try {
|
|
2574
2573
|
let responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
|
|
2575
|
-
if (responseType !==
|
|
2574
|
+
if (responseType !== 'arraybuffer') {
|
|
2576
2575
|
responseData = responseData.toString(responseEncoding);
|
|
2577
|
-
if (!responseEncoding || responseEncoding ===
|
|
2576
|
+
if (!responseEncoding || responseEncoding === 'utf8') {
|
|
2578
2577
|
responseData = utils$1.stripBOM(responseData);
|
|
2579
2578
|
}
|
|
2580
2579
|
}
|
|
@@ -2585,9 +2584,9 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2585
2584
|
}
|
|
2586
2585
|
});
|
|
2587
2586
|
}
|
|
2588
|
-
emitter.once(
|
|
2587
|
+
emitter.once('abort', (err)=>{
|
|
2589
2588
|
if (!stream.destroyed) {
|
|
2590
|
-
stream.emit(
|
|
2589
|
+
stream.emit('error', err);
|
|
2591
2590
|
stream.destroy();
|
|
2592
2591
|
}
|
|
2593
2592
|
});
|
|
@@ -2597,16 +2596,16 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2597
2596
|
// Send the request
|
|
2598
2597
|
let ended = false;
|
|
2599
2598
|
let errored = false;
|
|
2600
|
-
data.on(
|
|
2599
|
+
data.on('end', ()=>{
|
|
2601
2600
|
ended = true;
|
|
2602
2601
|
});
|
|
2603
|
-
data.once(
|
|
2602
|
+
data.once('error', /** @param {*} err */ (err)=>{
|
|
2604
2603
|
errored = true;
|
|
2605
2604
|
req.destroy(err);
|
|
2606
2605
|
});
|
|
2607
|
-
data.on(
|
|
2606
|
+
data.on('close', ()=>{
|
|
2608
2607
|
if (!ended && !errored) {
|
|
2609
|
-
abort(new CanceledError$1(
|
|
2608
|
+
abort(new CanceledError$1('Request stream has been aborted', config, req));
|
|
2610
2609
|
}
|
|
2611
2610
|
});
|
|
2612
2611
|
data.pipe(req); // stream 写入数据
|
|
@@ -2616,7 +2615,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2616
2615
|
}
|
|
2617
2616
|
_.done(R);
|
|
2618
2617
|
} catch (e) {
|
|
2619
|
-
log.error(e,
|
|
2618
|
+
log.error(e, 'request');
|
|
2620
2619
|
_.done(e, true);
|
|
2621
2620
|
throw e;
|
|
2622
2621
|
}
|
|
@@ -2629,7 +2628,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2629
2628
|
* @param {*} param1
|
|
2630
2629
|
* @returns
|
|
2631
2630
|
*/ const flushOnFinish = (stream, [throttled, flush])=>{
|
|
2632
|
-
stream.on(
|
|
2631
|
+
stream.on('end', flush).on('error', flush);
|
|
2633
2632
|
return throttled;
|
|
2634
2633
|
};
|
|
2635
2634
|
/** @typedef {import('../core/Axios').default} Axios */ /**
|
|
@@ -2640,7 +2639,7 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2640
2639
|
* @param {*} responseDetails - The options object that was passed to the request.
|
|
2641
2640
|
*
|
|
2642
2641
|
*/ function dispatchBeforeRedirect(options, responseDetails) {
|
|
2643
|
-
log.debug(
|
|
2642
|
+
log.debug('dispatchBeforeRedirect', {
|
|
2644
2643
|
opts: options.beforeRedirects
|
|
2645
2644
|
});
|
|
2646
2645
|
if (options.beforeRedirects.proxy) options.beforeRedirects.proxy(options);
|
|
@@ -2652,11 +2651,11 @@ const isHttpAdapterSupported = typeof process !== "undefined" && utils$1.kindOf(
|
|
|
2652
2651
|
* @returns
|
|
2653
2652
|
*/ function resolveFamily({ address, family }) {
|
|
2654
2653
|
if (!utils$1.isString(address)) {
|
|
2655
|
-
throw TypeError(
|
|
2654
|
+
throw TypeError('address must be a string');
|
|
2656
2655
|
}
|
|
2657
2656
|
return {
|
|
2658
2657
|
address,
|
|
2659
|
-
family: family || (address.indexOf(
|
|
2658
|
+
family: family || (address.indexOf('.') < 0 ? 6 : 4)
|
|
2660
2659
|
};
|
|
2661
2660
|
}
|
|
2662
2661
|
/**
|